// ----------------------------------------------------------------------------
//       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;
    }
}
