var phoneDivHtmlIntl =  "<div style='float:left;'>" +
                        "<font class=small>Country<br/>Code</font><br/>" +
                        "<input type=text name='frmPhoneCountryCode' size=3 maxlength=3>" +
                        "</div>" +
                        "<div style='float:left;'>" +
                        "<font class=small><br/>Number</font><br/>" +
                        "<input type=text name='frmPhoneNumber' size=20 maxlength=20>" +
                        "</div>" +
                        "<div style='float:left;'>" +
                        "<font class=small><br/>Ext.</font><br/>" +
                        "<input type=text name='frmPhoneExt' size=5 maxlength=5>" +
                        "</div>";

var phoneDivHtmlUS   =  "<div style='float:left;'>" +
                        "<font class=small>Number</font><br/>" +
                        "<input type=text name='frmPhoneNumber' size=20 maxlength=20>" +
                        "</div>" +
                        "<div style='float:left;'>" +
                        "<font class=small>Ext.</font><br/>" +
                        "<input type=text name='frmPhoneExt' size=5 maxlength=5>" +
                        "</div>";

var faxDivHtmlIntl   =  "<div style='float:left;'>" +
                        "<font class=small>Country<br/>Code</font><br/>" +
                        "<input type=text name='frmFaxCountryCode' size=3 maxlength=3>" +
                        "</div>" +
                        "<div style='float:left;'>" +
                        "<font class=small><br/>Number</font><br/>" +
                        "<input type=text name='frmFaxNumber' size=20 maxlength=20>" +
                        "</div>" +
                        "<div style='float:left;'>" +
                        "<font class=small><br/>Ext.</font><br/>" +
                        "<input type=text name='frmFaxExt' size=5 maxlength=5>" +
                        "</div><br clear=all />" +
                        "<font class=small>Please enter 'None' in the Number field if not applicable</font>";

var faxDivHtmlUS     =  "<div style='float:left;'>" +
                        "<font class=small>Number</font><br/>" +
                        "<input type=text name='frmFaxNumber' size=20 maxlength=20>" +
                        "</div>" +
                        "<div style='float:left;'>" +
                        "<font class=small>Ext.</font><br/>" +
                        "<input type=text name='frmFaxExt' size=5 maxlength=5>" +
                        "</div><br clear=all />" +
                        "<font class=small>Please enter 'None' in the Number field if not applicable</font>";


function changePhoneFields(type) {

    var phoneDiv = getObjectById('phoneDiv');

    if(document.contactForm.Phone.value.length>0) {

        phoneDiv.innerHTML = phoneDivHtmlUS;
        document.contactForm.frmPhoneNumber.value=document.contactForm.Phone.value;
        document.contactForm.frmPhoneExt.value=document.contactForm.PhoneExt.value;

    }  else {

        switch (type){
            case "INTL":
                phoneDiv.innerHTML = phoneDivHtmlIntl;
                break;
            case "USA":
                phoneDiv.innerHTML = phoneDivHtmlUS;
                break;
        }
    }
}

function changeFaxFields(type) {

    var faxDiv = getObjectById('faxDiv');
    
    if(document.contactForm.Fax.value.length>0) {

        faxDiv.innerHTML = faxDivHtmlUS;
        document.contactForm.frmFaxNumber.value=document.contactForm.Fax.value;
        document.contactForm.frmFaxExt.value=document.contactForm.FaxExt.value;

    }  else {


        switch (type){
            case "INTL":
                faxDiv.innerHTML = faxDivHtmlIntl;
                break;
            case "USA":
                faxDiv.innerHTML = faxDivHtmlUS;
                break;
        }
    }
}

function showPassword() {
    hidePassword();

    var thisTable=document.getElementById("passTable");
    if(thisTable!=null) {
        for(i=0; i<thisTable.rows.length;i++) {
            if(thisTable.rows[i].id == 'changePwd') {

                //var x=document.getElementById('passTable').insertRow(i+1)
                var row1 = thisTable.insertRow(i+1);
                row1.id = "pwd";
                var cell1=row1.insertCell(0);
                var cell2=row1.insertCell(1);
                cell1.className = 'alignRight';
                cell1.innerHTML="<b>Password</b>";
                cell2.className = 'alignLeft';
                cell2.innerHTML="<input type=password name=Password>";

                //var x=document.getElementById('passTable').insertRow(i+2)
                var row2 = thisTable.insertRow(i+2);
                row2.id = "vpwd";
                var cell3=row2.insertCell(0);
                var cell4=row2.insertCell(1);
                cell3.className = 'alignRight';
                cell3.innerHTML="<b>Verify Password</b>";
                cell4.className = 'alignLeft';
                cell4.innerHTML="<input type=password name=VerifyPassword>";
            }
        }
    }
}

function hidePassword() {
    var thisTable = document.getElementById("passTable");
    if(thisTable!=null) {
        for(i=0; i<thisTable.rows.length;i++) {
            if(thisTable.rows[i].id == 'pwd') {
                thisTable.deleteRow(i);
            }
            if(thisTable.rows[i].id == 'vpwd') {
                thisTable.deleteRow(i);
            }
        }
    }
}

function changeCountry(personState) {

    var thisStateRow=document.getElementById('stateRow');
    var thisAddress3Row=document.getElementById('address3Row');
    var thisAddress4Row=document.getElementById('address4Row');

    thisStateRow.style.display ='none';
    document.contactForm.StateProvince.options.length=0;
    document.contactForm.StateProvince.options[0] = new Option("- Select One -", "", true, false);

    thisAddress3Row.style.display = '';

    var optionPointer = 0;
    var selectedIndex = 0;

    if(document.contactForm.CountryName.value == "USA") {
        for(s=0; s<stateArray[1].length; s++) {
            optionPointer = s + 1;
            document.contactForm.StateProvince.options[optionPointer] = new Option(stateArray[1][s][1],stateArray[1][s][0],false,false);
            if(personState==stateArray[1][s][0]) {
                selectedIndex = optionPointer;
            }
        }
        document.contactForm.StateProvince.options.selectedIndex = selectedIndex;
        thisStateRow.style.display = '';
        if(document.contactForm.Address3.value.length==0) {
            thisAddress3Row.style.display = 'none';
        }
    } else if(document.contactForm.CountryName.value == "CAN") {
        for(s=0; s<stateArray[0].length; s++) {
            optionPointer = s + 1;
            document.contactForm.StateProvince.options[optionPointer] = new Option(stateArray[0][s][1],stateArray[0][s][0],false,false);
            if(personState==stateArray[1][s][0]) {
                selectedIndex = optionPointer;
            }
        }
        document.contactForm.StateProvince.options.selectedIndex = selectedIndex;
        thisStateRow.style.display = '';
        if(document.contactForm.Address3.value.length==0) {
            thisAddress3Row.style.display = 'none';
        }
    }

    if(document.contactForm.CountryName.value == "USA" || document.contactForm.CountryName.value == "CAN" ) {
        changePhoneFields("USA");
    } else {
        changePhoneFields("INTL");
    }
    if(document.contactForm.CountryName.value == "USA" || document.contactForm.CountryName.value == "CAN" ) {
        changeFaxFields("USA");
    } else {
        changeFaxFields("INTL");
    }

}

function formatPhoneNumbers(num) {
    newNum="";

    for(x=0; x<num.length; x++) {
        c = num.substring(x,x+1);
        if(!isNumeric(c)) {
            if(c=="-" || c==" ") {
                newNum+=" ";
            }
        } else {
            newNum+=c;
        }
    }

    return newNum;
}

function checkPriorityCode(z) {
    if(z.value.length > 0) {
        document.contactForm.MktgSrcCodeSelect.disabled=true;
    } else {
        document.contactForm.MktgSrcCodeSelect.disabled=false;
    }
}

function popUp() {

    var x = screen.width-325;
    window.open("ShowPriorityCode.jsp","priorityCode","width=300,height=400,left="+x+",top=25,screenX="+x+",screenY=25,resizable=no,status=no,scrollbars=no,titlebar=0");
}

function verifyContactForm() {

    var tmpPhone = "";
    var tmpPhoneExt = "";
    var tmpFax = "";
    var tmpFaxExt = "";

    var thisTable=document.getElementById("passTable");
    if(thisTable) {
        for(i=0; i<thisTable.rows.length;i++) {
            if(thisTable.rows[i].id == 'pwd') {
                if(document.contactForm.Password && document.contactForm.Password.value.length < 6) {
                    alert("Please Provide Your Password (minimum 6 characters)");
                    return false;
                }
            }
            if(thisTable.rows[i].id == 'vpwd') {
                if(document.contactForm.VerifyPassword && document.contactForm.VerifyPassword.value.length < 6) {
                    alert("Please Verify Your Password (minimum 6 characters)");
                    return false;
                } else if((document.contactForm.Password && document.contactForm.VerifyPassword) && document.contactForm.Password.value != document.contactForm.VerifyPassword.value) {
                    alert("The passwords you have entered do not match.  Please correct these two fields.");
                    return false;
                }
            }
        }
    }

    if(document.contactForm.Email.value.length < 6) {
        alert("Please Provide Your Email");
        return false;
    } else {
        if(document.contactForm.Email.value.indexOf(' ') > 0) {
            alert("Spaces are not allowed in Email Address");
            return false;
        } else if (document.contactForm.Email.value.indexOf('@') == -1) {
            alert ("You need to put a @ in your email address.  Your email address should look like yourname@domain.com");
            return false;
        } else if (document.contactForm.Email.value.indexOf('.') == -1) {
            alert ("You need to place a period in your email address.  Your email address should look like yourname@domain.com");
            return false;
        }
    }


    if(document.contactForm.FirstName && document.contactForm.FirstName.value.length < 1) {
        alert("Please Provide Your First Name");
        return false;
    }

    if(document.contactForm.LastName && document.contactForm.LastName.value.length < 1) {
        alert("Please Provide Your Last Name");
        return false;
    }

    if(document.contactForm.Title.value.length < 1) {
        alert("Please Provide Your Position/Job Title");
        return false;
    }

    if(document.contactForm.CompanyName.value.length < 1) {
        alert("Please Provide Your Company Name");
        return false;
    }

    if(document.contactForm.CountryName.value.length < 1) {
        alert("Please Select Your Country");
        return false;
    }
    if(document.contactForm.Address1.value.length < 1) {
        alert("Please Enter Your Address");
        return false;
    }
    if(document.contactForm.City.value.length < 1) {
        alert("Please Enter Your City");
        return false;
    }
    if(document.contactForm.CountryName.value == "USA" || document.contactForm.CountryName.value == "CAN") {
        if(document.contactForm.StateProvince.selectedIndex==0) {
            alert("Please Select Your State / Province");
            return false;
        }
    }

    if(document.contactForm.PostalCode.value.length < 1) {
        alert("Please Provide Your Postal Code or enter 'None' if not applicable");
        return false;
    }

    /********* IF INTERNATIONAL ***********/
    if(document.contactForm.CountryName.value != "USA" && document.contactForm.CountryName.value != "CAN" ) {

        /*** NEW PHONE RECORD - DISPLAY COUNTRY CODE ***/
        if(document.contactForm.Phone.value.length<1) {

            if(document.contactForm.frmPhoneCountryCode.value.length<1) {
                alert("Please Provide the Country Code for your Phone Number");
                return false;
            }
            if(document.contactForm.frmPhoneNumber.value.length<1) {
                alert("Please Provide your Phone Number");
                return false;
            }
            tmpPhone = formatPhoneNumbers(document.contactForm.frmPhoneCountryCode.value + " " + document.contactForm.frmPhoneNumber.value);
            tmpPhoneExt = formatPhoneNumbers(document.contactForm.frmPhoneExt.value);

        /*** EXISTING PHONE RECORD - NO COUNTRY CODE ***/
        } else {

            if(document.contactForm.frmPhoneNumber.value.length<1) {
                alert("Please Provide your Phone Number");
                return false;
            }
            tmpPhone = formatPhoneNumbers(document.contactForm.frmPhoneNumber.value);
            tmpPhoneExt = formatPhoneNumbers(document.contactForm.frmPhoneExt.value);
        }

        /*** REQUIRE 'NONE' IF LESS THAN 5 ***/
        if(document.contactForm.frmFaxNumber.value.length < 5) {

                if(document.contactForm.frmFaxNumber.value.toUpperCase()!='NONE') {
                    alert("Please Provide Your Fax Number or enter 'None' if not applicable");
                    return false;
                } else {
                    document.contactForm.frmFaxNumber.value = "NONE";
                    document.contactForm.frmFaxExt.value = "";
                    tmpFax = document.contactForm.frmFaxNumber.value;
                    tmpFaxExt = document.contactForm.frmFaxExt.value;
                }

        } else {

                /*** NEW FAX RECORD - DISPLAY COUNTRY CODE ***/
                if(document.contactForm.Fax.value.length<1) {

                    if(document.contactForm.frmFaxCountryCode.value.length<1) {
                        alert("Please Provide the Country Code for your Fax Number or Enter 'NONE' in the Fax Number field");
                        return false;
                    }
                    tmpFax = formatPhoneNumbers(document.contactForm.frmFaxCountryCode.value + " " + document.contactForm.frmFaxNumber.value);
                    tmpFaxExt = formatPhoneNumbers(document.contactForm.FaxExt.value);

                /*** EXISTING FAX RECORD ***/
                } else {
                    tmpFax = formatPhoneNumbers(document.contactForm.frmFaxNumber.value);
                    tmpFaxExt = formatPhoneNumbers(document.contactForm.frmFaxExt.value);
                }
        }

    /********* IF USA OR CAN ***********/
    } else {

        if(document.contactForm.frmPhoneNumber.value.length < 1) {
            alert("Please Provide Your Phone Number");
            return false;
        } else {
            tmpPhone = formatPhoneNumbers(document.contactForm.frmPhoneNumber.value);
            tmpPhoneExt = formatPhoneNumbers(document.contactForm.frmPhoneExt.value);
        }

        if(document.contactForm.frmFaxNumber.value.length < 5) {
            if(document.contactForm.frmFaxNumber.value.toUpperCase()!='NONE') {
                alert("Please Provide Your Fax Number or enter 'None' if not applicable");
                return false;
            } else {
                document.contactForm.frmFaxNumber.value = "NONE";
                document.contactForm.frmFaxExt.value = "";
                tmpFax = document.contactForm.frmFaxNumber.value;
                tmpFaxExt = document.contactForm.frmFaxExt.value;
            }
        } else {
            var faxNum = formatPhoneNumbers(document.contactForm.frmFaxNumber.value);
            var faxExt = formatPhoneNumbers(document.contactForm.frmFaxExt.value);
            if(faxNum!="") {
                tmpFax = faxNum;
                tmpFaxExt = faxExt;
            } else {
                alert("Invalid Fax Format - Please Provide Your Fax Number or enter 'None' if not applicable");
                document.contactForm.Fax.value = "";
                return false;
            }
        }
    }

    if(document.contactForm.AdditionalEmail.value.length > 0) {
         if(document.contactForm.AdditionalEmail.value.indexOf(' ') > 0) {
            alert("Spaces are not allowed in Email Address");
            return false;
        } else if (document.contactForm.AdditionalEmail.value.indexOf('@') == -1) {
            alert ("You need to put a @ in your email address.  Your email address should look like yourname@domain.com");
            return false;
        } else if (document.contactForm.AdditionalEmail.value.indexOf('.') == -1) {
            alert ("You need to place a period in your email address.  Your email address should look like yourname@domain.com");
            return false;
        }
    }

    document.contactForm.Phone.value = tmpPhone;
    document.contactForm.PhoneExt.value = tmpPhoneExt;
    document.contactForm.Fax.value = tmpFax;
    document.contactForm.FaxExt.value = tmpFaxExt;

    return true;
}

function submitContactForm() {
    if(verifyContactForm()) {
        document.contactForm.submitButton.value="PLEASE WAIT";
        document.contactForm.submitButton.disabled = true;
        document.contactForm.submit();
    }
}


