/*  McC_Form.js
 *
 *  Purpose:
 *  ----------------------------------------------------------------------------
 *  This defines the JavaScript functions called throughout the site.
 *
 *  functions:
 *  ----------------------------------------------------------------------------
 *  function on_change(that)
 *  function on_submit()
 *  function on_submit_McC_Care(bool)
 *  function on_submit_McC_Cont(bool)
 *  function on_submit_McC_Comp(bool,subs)
 *  function on_submit_McC_MSCF(bool)
 *  function on_submit_validate_all(bool,parm,misc)
 *  function on_submit_validate_one(list)
 *
 *  History:
 *  ----------------------------------------------------------------------------
 *  09-Jul-2009.0  Changed re "McC_MSCF.asp" enhancements by Chris McCloud.
 *  27-Jun-2009.0  Changed re "McC_Cont.asp" enhancements by Chris McCloud.
 *  26-Jun-2009.0  Changed re "McC_MSCF.asp" enhancements by Chris McCloud.
 *  23-Jan-2009.0  Changed re "MyC" form logic removed.
 *  10-Dec-2008.0  Changed re "McC_Care.asp" Desired Salary and Education added.
 *  26-Nov-2008.0  Changed re "McC_MSCF.asp" Address, City, State, Zip added.
 *  10-Nov-2008.0  Changed re "window.onload" conflicted with "McCloud1.js".
 *  27-Oct-2008.0  Changed re "function on_submit_McC_Care(bool)" revised.
 *  19-Sep-2008.0  Changed re "function on_submit_McC_Care(bool)" revised.
 *  12-Sep-2008.0  Changed re "function on_submit_McC_Care(bool)" revised.
 *  11-Sep-2008.0  Changed re "function on_submit_McC_Care(bool)" revised.
 *  27-Jun-2008.0  Changed re "Company Literature (Comp) was "Downloads" (Down).
 *  20-May-2008.0  Changed re "function yyyymmdd(what)".
 *  16-May-2008.0  Changed.
 *  12-May-2008.0  Changed re "function on_submit_McC_Care(bool)".
 *  08-May-2008.0  Changed re "function on_submit_McC_Down(bool,subs)".
 *  12-Mar-2008.0  Changed.
 *  11-Mar-2008.0  Changed.
 *  10-Mar-2008.0  Changed.
 *  09-Mar-2008.0  Changed.
 *  08-Mar-2008.0  Changed.
 *  07-Mar-2008.0  Created.
 *  ----------------------------------------------------------------------------
 */

/*
 *  Global Constants and Variables
 */
    var dbug = false;                                   // debug? (true/false)
    var errs;                                           // error counter
    var fail;                                           // failure message
    var href = document.location.href;                  // href
    var page;                                           // page
        page = href.substr(href.lastIndexOf("/")+1);    // page!
    var path = document.location.href;                  // path
        path = href.substr(0,href.lastIndexOf("/")+1);  // path!
    var rex0 = " < > ^ ` |";                            // < > ^ ` |
    var rex1 = /[\x3C\x3E\x5E\x60\x7C]/;                // < > ^ ` |
    var rex2 = /^(\s*)([\W\w]*)(\b\s*$)/;               // trim spaces
    var rex3 = /^[\r\n]+|[\r\n]+$/g;                    // trim CrLf's
    var rex4 = /(\r\n\s*){2,}/g;                        // 2max CrLf's
    var rex5 = /(\n\s*){2,}/g;                          // 2max Lf's (Firefox!)
    var rexP = /^(\d{3}-\d{3}-\d{4})/                   // phone: xxx-xxx-xxxx
    var rexE = /^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/;
    var rexD = /^[0-1][0-9]\/[0-3][0-9]\/[1-2][0-9][0-9][0-9]$/
    var sbgc = new Array("white","yellow");             // style.backgroundColor
    var when = new Date();                              // current date
    var xmdy = (when.getMonth()+1) + "/" + when.getDate() + "/" + when.getFullYear();
    var yymd = (when.getFullYear()*10000)+((when.getMonth()+1)*100)+when.getDate();

function on_change(that) {
/*
 *  onchange(): all fields
 */
 // remove leading and trailing spaces:
    var what = that.value.replace(rex2,'$2');
    if (what != that.value) that.value = what;
 // cleanup textarea:
    if (what != "" && that.type == "textarea") {
        var crlf = 0;
        if (what.indexOf("\x0A") >= 0) crlf = 1;
        if (what.indexOf("\x0D\x0A") >= 0) crlf = 2;
     // remove leading and trailing CrLf's from "textarea":
        what = what.replace(rex3,"");
     // replace 3+ consecutive CrLf's with 2 in "textarea":
        if (crlf == 2) what = what.replace(rex4,"\r\n\r\n");
     // replace 3+ consecutive Lf's with 2 in "textarea":
        if (crlf == 1) what = what.replace(rex5,"\r\n\r\n");
        that.value = what;
    }
 // display "About the position" and "Qualifications" for a "Position"
    var page = document.getElementById("Z_Page").value;
    if (page == "McC_Care.asp") {
        if (that.name.substr(2) == "Position") {
            var code = new Array("CSA","SR","SSp");
            var disp;
            var posi;
            for (var i=0; i<code.length; i++) {
                posi = "Position_" + code[i];
                if (code[i] == that.value) {
                    document.getElementById(posi).style.display = "block";
                } else {
                    document.getElementById(posi).style.display = "none";
                }
            }
        }
    }
}

function on_submit() {
/*
 *  onsubmit():
 */
    var form = document.McC_Form;
    if (page != document.getElementById("Z_Page").value) return false;
    if (dbug) {
        var elem;
        var what = "";
        for (var i=0; i<form.elements.length; i++) {
            elem = form.elements[i];
            what += "\n" + elem.name + " : " + elem.value;
            if (elem.type == "radio" || elem.type == "checkbox") {
                what += " (" + elem.checked + ")";
            }
        }
        alert(what + "\n\nThis form is still under construction!");
    }
    form.submit();
}

function on_submit_McC_Care(bool) {
/*
 *  "Care" = "Careers"
 */
    var sub1 = document.getElementById("Z_Subject").value + " [";
        sub1 = sub1.substr(0,sub1.indexOf(" [")-1);
    var sub2 = document.getElementById("D_Position").value;
    var sub3 = document.getElementById("E_Location").value;
    var subj = sub1 + " [" + sub2 + "@" + sub3 + "]";
    document.getElementById("Z_Subject").value = subj;
    var list = new Array();
    var list = new Array();
        list[0]  = "1234.DEP|#_Field";
        list[1]  = "1234....|A_FirstName";
        list[2]  = "1234....|B_LastName";
        list[3]  = "1234..E.|C_Email";
        list[4]  = "1...5...|D_Position";
        list[5]  = "1...5...|E_Location";
        list[6]  = "........|F_Salary";
        list[7]  = "........|G_Education";
        list[8]  = "1...5...|H_Source";
        list[9]  = ".234....|I_Details";
        list[10] = "12.4....|J_Message";
     // list[##] = "........|K_Filename";
        list[11] = "1...5...|L_Ethnicity";
        list[12] = "1...5...|M_Gender";
    var parm = list.join("^");
    on_submit_validate_all(bool,parm,"");
}

function on_submit_McC_Cont(bool) {
/*
 *  "Cont" = "Contact Us"
 */
    var list = new Array();
        list[0] = "1234.DEP|#_Field";
        list[1] = "1234....|A_Name";
        list[2] = ".234....|B_Company";
        list[3] = "1234..E.|C_Email";
        list[4] = "1234....|D_Message";
    var form = document.McC_Form;
    var parm = list.join("^");
    on_submit_validate_all(bool,parm,"");
}

function on_submit_McC_Comp(bool,subs) {
/*
 *  "Subscribe"...
 */
    document.getElementById("F_Subscribe").value = subs;
    var list = new Array();
        list[0] = "1234.DEP|#_Field";
        list[1] = "1234....|A_FirstName";
        list[2] = "1234....|B_LastName";
        list[3] = ".234....|C_Company";
        list[4] = ".234...P|D_Phone";
        list[5] = "1234..E.|E_Email";
     // list[#] = "........|F_Subscribe";
    var parm = list.join("^");
    on_submit_validate_all(bool,parm,"");
}

function on_submit_McC_MSCF(bool) {
/*
 *  "MSCF" = "McCloud Services Consultation Form"
 */
    var list = new Array();
        list[0] = "1234.DEP|#_Field";
        list[1] = "1234....|A_FirstName";
        list[2] = "1234....|B_LastName";
        list[3] = ".234....|C_Company";
        list[4] = ".234....|D_Phone";
        list[5] = "1234..E.|E_Email";
        list[6] = "....4...|F_Address";
        list[7] = "....4...|G_City";
        list[8] = "....4...|H_State";
        list[9] = "....4...|I_Zip";
     // list[#] = "........|J_Subscribe";
     // list[#] = "........|K_Aquatic Services";
     // list[#] = "........|K_Bed Bug Management";
     // list[#] = "........|K_Bird Management / Exclusion";
     // list[#] = "........|K_Electronic Data Collection";
     // list[#] = "........|K_Food Safety Auditing";
     // list[#] = "........|K_Fumigation Alternatives";
     // list[#] = "........|K_Integrated Pest Management (IPM)";
     // list[#] = "........|K_Organic Compliant Program";
     // list[#] = "........|K_Sanitation Consultation";
     // list[#] = "........|K_Structural and Container Fumigation";
     // list[#] = "........|K_Termite Inspection / Management";
     // list[#] = "........|K_Vegetation Management";
     // list[#] = "........|K_Training Programs";
     // list[#] = "........|K_Other";
     // list[#] = "........|L_Interest";
        list[10] = "1...5...|M_Source";
    var chec = 0;
    var form = document.McC_Form;
    var hint = "";
    for (var i=0; i<form.elements.length; i++) {
        elem = form.elements[i];
        if (elem.type == "checkbox" && elem.checked) {
            chec++;
            if (elem.name.substr(0,2) == "K_") {
                if (hint != "") hint += "; "
                hint += elem.name.substr(2).replace(/_/g," ");
            }
        }
    }
    form.L_Interest.value += hint;
    var misc = "";
    if (chec == 0) misc = "No checkboxes were checked.";
    var parm = list.join("^");
    on_submit_validate_all(bool,parm,misc);
}

function on_submit_validate_all(bool,parm,misc) {
/*
 *  Validate all form fields.
 */
    document.getElementById("Z_Return").value = "False";
    errs = 0;
    fail = "Please fix these error(s):\n";
    var item;
    var list = parm.split("^");
 // alert(list);
 // ... (fields) ...............................................................
    for (var i=1; i<list.length; i++) {
        item = list[i].split("|")[1];
        if (document.getElementById(item).type != "hidden") {
            document.getElementById(item+"_").style.backgroundColor = sbgc[0];
            document.getElementById(item+"_").title = "";
            if (!bool) {
                document.getElementById(item).value = "";
            } else {
             // alert(list[i]);
                on_submit_validate_one(list[i])
            }
        }
    }
    if (!bool) return;
 // ... (return) ...............................................................
    if (errs > 0 || misc != "") {
        if (fail.indexOf("invalid character") > 0) {
            fail += "\n\nThe invalid characters are: " + rex0;
        }
        if (misc != "") fail += "\n\n" + misc;
        alert(fail);
        return;
    }
    document.getElementById("Z_Return").value = "True";
    on_submit();
}

function on_submit_validate_one(list) {
/*
 *  Validate one list form field.
 */
    var diff;
    var flag = list.split("|")[0];
    var item = list.split("|")[1];
    var leng = 50;
    var opts;
    var sele;
//    var sing = "";
    var type = document.getElementById(item).type;
    var valu = document.getElementById(item).value;
// alert(valu);
    var what = "";
    if (type == "select-one") {
        sele = document.getElementById(item).selectedIndex;
//    } else if (type == "select-multiple") {
//        sing = document.getElementById(item).name;
//        sing = sing.substr(0,sing.length-1);
//  alert(sing);
//        opts = document.getElementById(item).options;
//        for (var i=0; i<opts.length; i++) {
//            if (opts[i].selected) {
//                document.getElementById(sing).value == opts[i].value;
//            }
//        }
    }
    if (type == "textarea") {
        leng = 1024;
        diff = valu.length - leng;
    }
         if (flag.indexOf("1") <  0 && valu == "")         return;
         if (flag.indexOf("1") >= 0 && valu == "")         what = " is missing; required.";
    else if (flag.indexOf("2") >= 0 && valu.length < 3)    what = " has too few characters.";
    else if (flag.indexOf("3") >= 0 && valu.length > leng) what = " has " + diff + " too many characters.";
    else if (flag.indexOf("4") >= 0 && rex1.test(valu))    what = " contains an invalid character.";
    else if (flag.indexOf("5") >= 0 && sele < 1)           what = " requires a selection.";
    else if (flag.indexOf("D") >= 0 && !rexD.test(valu))   what = " is not a valid date format.";
    else if (flag.indexOf("E") >= 0 && !rexE.test(valu))   what = " is not a valid email address.";
    else if (flag.indexOf("P") >= 0 && !rexP.test(valu))   what = " is not a valid phone format.";
    if (what == "" && flag.indexOf("D") >= 0) {
        if (!yyyymmdd(valu))                               what = " is not a date <= " + xmdy;
    }
    if (what != "") {
        errs++;
        fail += "\n" + errs + ". " + item.substr(2) + what;
        document.getElementById(item+"_").style.backgroundColor = sbgc[1];
        document.getElementById(item+"_").title = "This field" + what;
    }
    return;
}

function yyyymmdd(what) {
/*
 *  Date must not be in the future.
 */
    if (what.length != 10) return false;
    if (what.charAt(2) != "/" || what.charAt(5) != "/") return false;
    var valu = what.substr(6,4)+what.substr(0,2)+what.substr(3,2);
    if (isNaN(valu)) return false;
    try {
        if (parseInt(valu) > yymd) return false;
    } catch(err) {
        alert("yyyymmdd('" + what + "')\n\n " + err.description);
    }
    return true;
}
