// ----------------------------------------------------------------------------
//       File Name: enduser.js
//       Subsystem: enduser
//   Document Type: Javascript include file
//         Purpose: contains all non-page specific enduser page Javascript
// ----------------------------------------------------------------------------

function submenu(code, items)
{
    this.code  = code;
    this.items = items;
}

// ----------------------------------------------------------------------------

function subitem(code, name)
{
    this.code = code;
    this.name = name;
}

// ----------------------------------------------------------------------------

function field_data(int_msg, reqd_msg, not_complete_msg, oversz_msg,
                    ascii_msg, email_msg,
                    mon_lbl, day_lbl, yr_lbl, hr_lbl, min_lbl, email_expr)
{
    this.int_msg          = int_msg;
    this.reqd_msg         = reqd_msg;
    this.not_complete_msg = not_complete_msg;
    this.oversz_msg       = oversz_msg;
    this.ascii_msg        = ascii_msg;
    this.email_msg        = email_msg;

    this.dt_lbl           = new Array(mon_lbl, day_lbl, yr_lbl, hr_lbl, min_lbl);

    this.email_expr       = new RegExp(email_expr ? email_expr : '.*');
}

// ----------------------------------------------------------------------------

function field(name, label, type, maxlen, flags)
{
    this.name   = name;
    this.label  = label;
    this.type   = type;
    this.maxlen = maxlen;

    // flags is a bitmask:
    //   1  required
    //   2  ascii only
    //   4  must match email pattern

    this.flags  = flags;
}

// ----------------------------------------------------------------------------

function _upd_submenu(menu, submenu, submenu_data, all_str)
{
    var i, j = 1;

    submenu.length = 0;

    submenu.options[0]       = new Option();
    submenu.options[0].text  = all_str;
    submenu.options[0].value = '~any~';

    for (i = 0; i < submenu_data.length; i++)
        if (submenu_data[i].code == menu.options[menu.selectedIndex].value)
        {
            for ( ; j <= submenu_data[i].items.length; j++)
            {
                submenu.options[j]       = new Option();
                submenu.options[j].text  = submenu_data[i].items[j-1].name;
                submenu.options[j].value = submenu_data[i].items[j-1].code;
            }

            break;
        }

    submenu.length        = j;
    submenu.selectedIndex = 0;
}

// ----------------------------------------------------------------------------

function _alp_onload(page, gridsort)
{
    if (document.grid)
    {
        if (document.grid.p_page)
            document.grid.p_page.selectedIndex = page - 1;
        if (document.grid.p_gridsort)
            document.grid.p_gridsort.value = gridsort;
    }
}

// ----------------------------------------------------------------------------

function _adp_print(url)
{
    window.open(url, 'print_answer', 'resizable,menubar,toolbar');
}

// ----------------------------------------------------------------------------

function _adp_email(url)
{
    window.open(url, 'email_answer', 'resizable,width=700,height=392');
}

// ----------------------------------------------------------------------------
// CDT_DATE and CDT_DATETIME components are processed as individual CDT_MENU
// fields

function _check_fields(form_name, fld_data, fields)
{
    var fld, i, j, v, str;
    var ws_exp      = new RegExp("(^\\s*|\\s*$)", "g");
    var strtok_exp  = new RegExp("%s");
    var numtok_exp  = new RegExp("%d");
    var valid_ascii = new RegExp("^[\x20-\x7e]+$");

    with (fld_data) for (i = 0; (i < fields.length) && fields[i].type; i++)
    {
        if ((fields[i].type != 4) && (fields[i].type != 7))
            fld = eval('document.'+form_name+'.'+fields[i].name);

        switch (fields[i].type)
        {
            case 1: // CDT_MENU
                if ((fields[i].flags & 1) &&
                    (fld.length > 1) && (fld.selectedIndex < 1))
                {
                    alert('\''+fields[i].label+'\' '+reqd_msg);
                    fld.focus();
                    return(false);
                }
                break;

            case 2: // CDT_BOOL
                if ((fields[i].flags & 1) &&
                    !fld[0].checked && !fld[1].checked)
                {
                    alert('\''+fields[i].label+'\' '+reqd_msg);
                    fld[0].focus();
                    return(false);
                }
                break;

            case 3: // CDT_INT
                fld.value = fld.value.replace(ws_exp, '');
                if (fld.value.length && isNaN(fld.value))
                {
                    alert('\''+fields[i].label+'\' '+int_msg);
                    fld.focus();
                    return(false);
                }

                // deliberate drop through

            case 5: // CDT_VARCHAR
            case 6: // CDT_MEMO
                if (fields[i].maxlen && (fields[i].maxlen < fld.value.length))
                {
                    str = oversz_msg.replace(strtok_exp, fields[i].label);
                    str = str.replace(numtok_exp, fields[i].maxlen);
                    str = str.replace(numtok_exp,
                                      fld.value.length - fields[i].maxlen);

                    alert(str);
                    fld.focus();
                    return(false);
                }

                if (fields[i].type != 3)
                    fld.value = fld.value.replace(ws_exp, '');

                if ((fields[i].flags & 1) && (fld.value.length == 0))
                {
                    alert('\''+fields[i].label+'\' '+reqd_msg);
                    fld.focus();
                    return(false);
                }

                // if not required and not set, don't do checks
                if ((fld.value.length == 0))
                    break;

                if ((fields[i].flags & 2) && !valid_ascii.test(fld.value))
                {
                    alert('\''+fields[i].label+'\' '+ascii_msg);
                    fld.focus();
                    return(false);
                }

                if ((fields[i].flags & 4) && !email_expr.test(fld.value))
                {
                    alert('\''+fields[i].label+'\' '+email_msg);
                    fld.focus();
                    return(false);
                }

                break;


            case 4: // CDT_DATETIME
            case 7: // CDT_DATE
                fld = new Array();

                fld[0] = eval('document.'+form_name+'.'+fields[i].name+'_mon');
                fld[1] = eval('document.'+form_name+'.'+fields[i].name+'_day');
                fld[2] = eval('document.'+form_name+'.'+fields[i].name+'_yr');

                if (fields[i].type == 4)
                {
                    fld[3] = eval('document.'+form_name+'.'+fields[i].name+'_hr');
                    fld[4] = eval('document.'+form_name+'.'+fields[i].name+'_min');
                }

                if (!(fields[i].flags & 1))
                {
                    for (j = v = 0; j < fld.length; j++)
                        v += (fld[j].selectedIndex > 0) ? 0 : 1;

                    if ((v != 0) && (v != fld.length))
                    {
                        alert('\''+fields[i].label+'\' '+not_complete_msg);
                        fld[0].focus();
                        return(false);
                    }

                    break;
                }

                for (j = 0; j < fld.length; j++)
                    if ((fld[j].selectedIndex < 1))
                    {
                        alert('\''+fields[i].label+' ('+dt_lbl[j]+')\' '+reqd_msg);
                        fld[j].focus();
                        return(false);
                    }

                break;
        }
    }

    return(true);
}

// ----------------------------------------------------------------------------

function _validate_acctinfo(userid, passwd1, passwd2, min_passwd_len, msgs)
{
   var msg = -1, fld;

   if (userid.value.indexOf(' ') != -1)
   {
       msg = 0;
       fld = userid;
   }

   if (userid.value.indexOf('\"') != -1)
   {
       msg = 1;
       fld = userid;
   }

   if (passwd1 && (passwd1.value != passwd2.value))
   {
       msg = 2;
       fld = passwd1;
   }

   if (passwd1 && (passwd1.value.length < min_passwd_len))
   {
       msg = 3;
       fld = passwd1;
   }

   if (msg != -1)
   {
       alert(msgs[msg]);
       fld.focus();
       fld.select();
       return(false);
   }

   return(true);
}

// ----------------------------------------------------------------------------

var cursor_set = false;

function _set_cursor()
{
    var i, j;

    if (cursor_set)
        return;

    cursor_set = true;

    if (document.location.href.indexOf('#') > -1)
        return;

    for (i = 0; i < document.forms.length; i++)
        for (j = 0; j < document.forms[i].length; j++)
           with (document.forms[i])
               if (elements[j].type && ((elements[j].type == 'text') || (elements[j].type == 'textarea')))
               {
                   elements[j].focus();
                   if (elements[j].value.length)
                       elements[j].select();
                   return;
               }
}

// ----------------------------------------------------------------------------

function notif_label(prod_lbl, cat_lbl, exp_lbl)
{
    this.prod_lbl = prod_lbl;
    this.cat_lbl  = cat_lbl;
    this.exp_lbl  = exp_lbl;
}

// ----------------------------------------------------------------------------

function add_notif_item(notif_fld, ntype, lvl1_fld, lvl2_fld)
{
    var i, add_item, sub_val, sub_txt, ntype_txt;

    if (lvl1_fld.options[lvl1_fld.selectedIndex].text == '--')
        return;

    if ((lvl2_fld.type == 'hidden') ||
        (lvl2_fld.options[lvl2_fld.selectedIndex].value == '~any~'))
    {
        sub_val = '0';
        sub_txt = '--';
    }
    else
    {
        sub_val = lvl2_fld.options[lvl2_fld.selectedIndex].value;
        sub_txt = lvl2_fld.options[lvl2_fld.selectedIndex].text;
    }

    if (ntype == 13)
        ntype_txt = notif_lbl.prod_lbl + " - ";
    else
        ntype_txt = notif_lbl.cat_lbl + " - ";

    add_item = ntype + "," +
           lvl1_fld.options[lvl1_fld.selectedIndex].value + "," + sub_val +
           ">"+ start_time;

    for (i = 0; i < notif_fld.length; i++)
        if (add_item.split(">")[0] == notif_fld.options[i].value.split(">")[0])
        {
            notif_fld.selectedIndex = i;
            return;
        }

    notif_fld.length++;
    notif_fld.options[notif_fld.length-1].value = add_item;
    notif_fld.options[notif_fld.length-1].text = ntype_txt +
                  lvl1_fld.options[lvl1_fld.selectedIndex].text + "/" +
                  sub_txt + " " + expire_time;
}

// ----------------------------------------------------------------------------

function delete_notif_item(notif_fld)
{
    if (notif_fld.selectedIndex >= 0)
    {
        for (i = notif_fld.selectedIndex; i < notif_fld.length-1; i++)
        {
            notif_fld.options[i].value = notif_fld.options[i+1].value;
            notif_fld.options[i].text = notif_fld.options[i+1].text;
        }

        notif_fld.length--;
        notif_fld.selectedIndex = -1;
    }
}

// ----------------------------------------------------------------------------

function renew_notif_item(notif_fld)
{
    i = notif_fld.selectedIndex;
    tmp_str = "(" + notif_lbl.exp_lbl;

    if (notif_fld.selectedIndex >= 0)
    {
        notif_fld.options[i].value =
                             notif_fld.options[i].value.split(">")[0] +
                             ">" + start_time;
        notif_fld.options[i].text = 
                             notif_fld.options[i].text.split(tmp_str)[0] +
                             expire_time;
    }
}


/* ||||||||||||||||||||| start: logic ||||||||||||||||||||| 
//----------------------------------------------------------------------------
Application: myjobcoach/rnw remote system
Document Type: javascript include
Developer: rsymonds@infotree.com
Created: 12.22.2001
Last Modified: 02.10.2003
Req. Parameters In:   
Opt. Parameters In: 
Req. Parameters Out: 
Opt. Parameters Out: 
Included Files:  
Description: This file includes the methods for creating various bridges to pass 
	information to MJC's remote system from the RNW system 
			
// ----------------------------------------------------------------------------*/
// V2.0


// ------------------------------------------------------------------
// start: variable declarations  
storeURL = "https://www.myjobcoach.com/remote/store.handler.cfm";
behavioralStyleURL = "http://www.myjobcoach.com/remote/behavioral_style.handler.cfm";
// ------------------------------------------------------------------


// ------------------------------------------------------------------
// Start: get cookie 
function getCookie(strCookieName) {
	var strCookieName = strCookieName + "=";               
	var dc = document.cookie;             
  	if (dc.length > 0) {              
	    begin = dc.indexOf(strCookieName);       
		if (begin != -1) {           
			begin += strCookieName.length;       
			end = dc.indexOf(";", begin);
			if (end == -1) end = dc.length;
			return unescape(dc.substring(begin, end));
			//return dc.substring(begin, end);
		} 
  	}
 	return false;
}
// ------------------------------------------------------------------


// ------------------------------------------------------------------
// Start: get RNW cookie
function rnwLinkCustomerData() {
 	return (getCookie("rnwLinkCustomerData"))?getCookie("rnwLinkCustomerData"):"";
}

function rnwFormCustomerData() {
	return (getCookie("rnwFormCustomerData"))?getCookie("rnwFormCustomerData"):"";
}
// ------------------------------------------------------------------


// ------------------------------------------------------------------
// start: writeBehaviorLink(),popBehavioralStyle()  
/*
This method will create a link to the Behavioral Style Assessment.  
The one argument is for changing the text label of the link.  If the RNW
cookie cannot be read then we still will create the link but just not pass the 
customer data
*/
function writeBehaviorLink(strDefaultLabel) {
	strDefaultLabel = (strDefaultLabel)?strDefaultLabel:"Click Here to Take the Behavioral Style Assessment";
	document.write("<a href=\"javascript:popBehavioralStyle()\">" + strDefaultLabel + "</a>");
}

// this is the method that the link created above will call to open the window
function popBehavioralStyle() {
	strURL = behavioralStyleURL + "?customerData=" 
		+ rnwLinkCustomerData()  + "&boolSuppressErrors=true&return="; 
	window.open(strURL,'','status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=640,height=480');
}
// ------------------------------------------------------------------


function addToCart(pCode,pQty,totalItems,promotionID){
	strURL = storeURL + "?"; 
	strURL += "associationCode=" + getAssociationCode() + "&";
	strURL += "associationID=" + getAssociationID() + "&";
	strURL += "coachID=" + getCoachID() + "&"; 
	if (promotionID) strURL += "PID=" + promotionID + "&"; 
	strURL += "customerData=" + rnwLinkCustomerData() + "&"; 
	strURL += "p1Code=" + pCode + "&"; 
	strURL += "p1Qty=" + pQty + "&"; 
	strURL += "totalItems=" + totalItems + "&"; 
	strURL += "boolSuppressErrors=true"; 
	window.open(strURL,'cart','width=610,height=400,resizable=yes,address=yes,scrollbars=yes,status=yes,menubar=yes');
}
// ------------------------------------------------------------------


// ------------------------------------------------------------------
// start: openAddToCartForm()  
/*
This method opens up an HTML form that will include all the inputs generated by the 
'Add to Cart' Code Wizard' when the administrator chooses to create a 'FORM based' 
products listing.  The one argument, totalItems, is an integer reflecting the number 
of items (starting with 1) that will be listed in the form.  This method contains all of 
the necessary information to get the customerData from the RNW cookie and send 
all the information through act_unpack.cfm and on to the shopping cart.
*/
function openAddToCartForm(intTotalItems,promotionID) {
	strOutput = "<form method=\"GET\" action=\""+ storeURL +"\" name=\"listItems\" target=\"cart\" onSubmit=\"return openCart(document.listItems)\">";
	strOutput += "<input type=\"hidden\" name=\"customerData\" value=\"" + rnwFormCustomerData() + "\">\n";
	strOutput += "<input type=\"hidden\" name=\"associationCode\" value=\"" + getAssociationCode() +"\">\n";
	strOutput += "<input type=\"hidden\" name=\"associationID\" value=\"" + getAssociationID() +"\">\n";
	strOutput += "<input type=\"hidden\" name=\"coachID\" value=\"" + getCoachID() +"\">\n";
	if (promotionID) strOutput += "<input type=\"hidden\" name=\"PID\" value=\""+ promotionID +"\">\n";
	strOutput += "<input type=\"hidden\" name=\"totalItems\" value=\""+ intTotalItems +"\">\n";
	strOutput += "<input type=\"hidden\" name=\"boolSuppressErrors\" value=\"false\">\n";
	strOutput += "<input type=\"submit\" value=\"Add To Cart\"><p />";
	document.write(strOutput);
}
// ------------------------------------------------------------------



// ------------------------------------------------------------------
// start: closeAddToCartForm()  
/*
This method ends the product listing form that openAddToCartForm() opened.
*/
function closeAddToCartForm() {
	strOutput = "<input type=\"submit\" value=\"Add To Cart\">\n";
	strOutput += "</form>";
	document.write(strOutput);
}
// ------------------------------------------------------------------

// ------------------------------------------------------------------
// start: openCart()  
function openCart(form){
	var cart = window.open(storeURL,'cart','width=610,height=400,resizable=yes,address=yes,scrollbars=yes,status=yes,menubar=yes');
	cart.focus();
}
// ------------------------------------------------------------------



// ------------------------------------------------------------------
// associationCode was mistakenly called associationID in the beginning
function getAssociationCode() {
	// cookie holds delimitd associationID|coachID, grab associationID
 	if (getCookie("associationID")) {
	 	cdata = getCookie("associationID").split("|");
		return cdata[0];
	}
	else {
		return "";
	}
}

function getCoachID() {
	// cookie holds delimitd associationID|coachID, grab coachID
 	if (getCookie("associationID")) {
	 	cdata = getCookie("associationID").split("|");
		return cdata[1];
	}
	else {
		return "";
	}
}

function getAssociationID() {
	// cookie holds delimitd associationID|coachID, grab associationID
 	if (getCookie("associationID")) {
	 	cdata = getCookie("associationID").split("|");
		return cdata[2];
	}
	else {
		return "";
	}
}
// ------------------------------------------------------------------


