function validateForgotPasswordForm(theForm) {
    document.getElementById("error").innerHTML = "";
    
    if (trimAll(theForm.username.value) == "") {
        theForm.username.focus();
        displayError("Please enter your Username.");
        return false;
    }
    
    if (trimAll(theForm.email.value) == "" || !SeflUtilities.checkEmail(theForm.email.value)) {
        theForm.email.focus();
        displayError("Please enter a valid Email Address.");
        return false;    
    }
    
    return true;
}

function validateCustProfileFormsBtnDisProc(theForm){
    var submitBtn = document.getElementById("submitProfileValues");
    var returnValue = false;
    if(submitBtn){
        submitBtn.disabled = true;
    }
    returnValue = validateCustProfileForms(theForm);
    if(!returnValue){
        submitBtn.disabled = false;
    }
    return returnValue;
}

function validateCustProfileForms(theForm) {
    var hasName, hasTitle, hasEmail, hasPhone, hasFax, hasCity, hasState, hasAcct;
    document.getElementById("error").innerHTML = "";
    
    if (trimAll(theForm.CompanyName.value) == "") {
        theForm.CompanyName.focus();
        displayError("Please enter a company name.");
        return false;
    }
    
    if (trimAll(theForm.Address1.value) == "") {
        theForm.Address1.focus();
        displayError("Please enter an address.");
        return false;
    }
    
    if (trimAll(theForm.City.value) == "") {
        theForm.City.focus();
        displayError("Please enter a city.");
        return false;
    }
    
    if (theForm.State.selectedIndex == 0) {
        theForm.State.focus();
        displayError("Please select a state.");
        return false;
    }
    
    if (trimAll(theForm.ZipCode.value).length < 5) {
        theForm.ZipCode.focus();
        displayError("Please enter at least a 5 digit zip code.");
        return false;
    }
    
    if (trimAll(theForm.ContactName.value) == "") {
        theForm.ContactName.focus();
        displayError("Please enter a contact name.");
        return false;
    }
    
    if (trimAll(theForm.PhoneNum1.value).length < 3) {
        theForm.PhoneNum1.focus();
        displayError("Please enter your phone number area code.");
        return false;
    }
    
    if (trimAll(theForm.PhoneNum1.value).length == 3) {
        if (!isNumeric(theForm.PhoneNum1.value)) {
            theForm.PhoneNum1.focus();
            displayError("Phone number must be numeric.");
            return false;
        }        
    }
    
    if (trimAll(theForm.PhoneNum2.value).length < 3) {
        theForm.PhoneNum2.focus();
        displayError("Please enter your phone number.");
        return false;
    }
    
    if (trimAll(theForm.PhoneNum2.value).length == 3) {
        if (!isNumeric(theForm.PhoneNum2.value)){
            theForm.PhoneNum2.focus();
            displayError("Phone number must be numeric.");
            return false;
        }        
    }
    
    if (trimAll(theForm.PhoneNum3.value).length < 4) {
        theForm.PhoneNum3.focus();
        displayError("Please enter your phone number.");
        return false;
    }
    
    if (trimAll(theForm.PhoneNum3.value).length == 4) {
        if (!isNumeric(theForm.PhoneNum3.value)) {
            theForm.PhoneNum3.focus();
            displayError("Phone number must be numeric.");
            return false;
        }       
    }
    
    hasFax = false;
    if (trimAll(theForm.FaxNum1.value).length > 0 &&
    trimAll(theForm.FaxNum1.value).length < 3) {        
        theForm.FaxNum1.focus();
        displayError("Fax number is incomplete.");
        return false;
    } else if (trimAll(theForm.FaxNum1.value).length == 3) {
        hasFax = true;
    }
    
    if (trimAll(theForm.FaxNum1.value).length == 3) {
        if (!isNumeric(theForm.FaxNum1.value)) {
            theForm.FaxNum1.focus();
            displayError("Fax number must be numeric.");
            return false;
        }        
    } else if (trimAll(theForm.FaxNum2.value).length == 3) {
        hasFax = true;
    }
    
    if (trimAll(theForm.FaxNum2.value).length < 0 &&
    trimAll(theForm.FaxNum2.value).length < 3) {        
        theForm.FaxNum2.focus();
        displayError("Fax number is incomplete.");
        return false;
    } else if (trimAll(theForm.FaxNum1.value).length == 4) {
        hasFax = true;
    } 
    
    if (hasFax && (trimAll(theForm.FaxNum1.value).length == 0 ||
    trimAll(theForm.FaxNum2.value).length == 0 ||
    trimAll(theForm.FaxNum3.value).length == 0)) {        
        theForm.FaxNum1.focus();
        displayError("Fax number is incomplete.");
        return false;
    }
    
    if (trimAll(theForm.FaxNum2.value).length == 3) {
        if (!isNumeric(theForm.FaxNum2.value)) {
            theForm.FaxNum2.focus();
            displayError("Fax number must be numeric.");
            return false;
        }        
    }
    
    if (trimAll(theForm.FaxNum3.value).length > 0 &&
    trimAll(theForm.FaxNum3.value).length < 4) {        
        theForm.FaxNum3.focus();
        displayError("Fax number is incomplete.");
        return false;
    }
    
    if (trimAll(theForm.FaxNum3.value).length == 4) {
        if (!isNumeric(theForm.FaxNum3.value)) {
            theForm.FaxNum3.focus();
            displayError("Fax number must be numeric.");
            return false;
        }        
    }    
    
    if (trimAll(theForm.Email.value) == "" ||
    !SeflUtilities.checkEmail(theForm.Email.value)) {
        theForm.Email.focus();
        displayError("Please enter a valid e-mail address.");
        return false;
    }
    
    var contactInputs = document.getElementById("contactTable").getElementsByTagName("input"); 
    var line = 0;
    
    for (var i=0;i<contactInputs.length;i=i+9) {
        line++;
        if (contactInputs[i].type == "hidden") {
            i = i - 8;
            continue;
        }
        if (trimAll(contactInputs[i].value) != "") {
            hasName = true;
        } else {
            hasName = false;
        }
        if (trimAll(contactInputs[i+1].value) != "") {
            hasTitle = true;
        } else {
            hasTitle = false;
        }
        if (trimAll(contactInputs[i+2].value) != "") {
            hasEmail = true;
        } else {
            hasEmail = false;
        }
        if (trimAll(contactInputs[i+3].value) != "" ||
        trimAll(contactInputs[i+4].value) != "" ||
        trimAll(contactInputs[i+5].value) != "") {
            hasPhone = true;
        } else {
            hasPhone = false;
        }
        if (trimAll(contactInputs[i+6].value) != "" ||
        trimAll(contactInputs[i+7].value) != "" ||
        trimAll(contactInputs[i+8].value) != "") {
            hasFax = true;
        } else {
            hasFax = false;
        }
        
        if (hasName || hasTitle || hasEmail || hasPhone || hasFax) {
            if (!hasName) {
                contactInputs[i].focus();
                displayError("Error on Additional Contact line " + line + ".  Please enter a contact name or remove all entries from this contact line.");                                
                return false;
            }
            
            if (!hasEmail || !SeflUtilities.checkEmail(contactInputs[i+2].value)) {
                contactInputs[i+2].focus();
                displayError("Error on Additional Contact line " + line + ".  Please enter a valid email address or remove all entries from this contact line.");                
                return false;
            }
            
            if (!hasPhone) {
                contactInputs[i+3].focus();
                displayError("Error on Additional Contact line " + line + ".  Please enter a phone number or remove all entries from this contact line.");                
                return false;
            } else {
                if (trimAll(contactInputs[i+3].value).length < 3) {
                    contactInputs[i+3].focus();
                    displayError("Error on Additional Contact line " + line + ".  Incomplete phone number.");                    
                    return false;
                }
                if (!isNumeric(contactInputs[i+3].value)) {
                    contactInputs[i+3].focus();
                    displayError("Error on Additional Contact line " + line + ".  Invalid phone number.");                    
                    return false;
                }  
                if (trimAll(contactInputs[i+4].value).length < 3) {
                    contactInputs[i+4].focus();
                    displayError("Error on Additional Contact line " + line + ".  Incomplete phone number.");                    
                    return false;
                }
                if (!isNumeric(contactInputs[i+4].value)) {
                    contactInputs[i+4].focus();
                    displayError("Error on Additional Contact line " + line + ".  Invalid phone number.");                    
                    return false;
                } 
                if (trimAll(contactInputs[i+5].value).length < 4) {
                    contactInputs[i+5].focus();
                    displayError("Error on Additional Contact line " + line + ".  Incomplete phone number.");                    
                    return false;
                }
                if (!isNumeric(contactInputs[i+5].value)) {
                    contactInputs[i+5].focus();
                    displayError("Error on Additional Contact line " + line + ".  Invalid phone number.");                    
                    return false;
                } 
            }
            
            if (hasFax) {
                if (trimAll(contactInputs[i+6].value).length < 3) {
                    contactInputs[i+6].focus();
                    displayError("Error on Additional Contact line " + line + ".  Incomplete fax number.");                    
                    return false;
                }
                if (!isNumeric(contactInputs[i+6].value)) {
                    contactInputs[i+6].focus();
                    displayError("Error on Additional Contact line " + line + ".  Invalid fax number.");                    
                    return false;
                }  
                if (trimAll(contactInputs[i+7].value).length < 3) {
                    contactInputs[i+7].focus();
                    displayError("Error on Additional Contact line " + line + ".  Incomplete fax number.");                    
                    return false;
                }
                if (!isNumeric(contactInputs[i+7].value)) {
                    contactInputs[i+7].focus();
                    displayError("Error on Additional Contact line " + line + ".  Invalid fax number.");                    
                    return false;
                } 
                if (trimAll(contactInputs[i+8].value).length < 4) {
                    contactInputs[i+8].focus();
                    displayError("Error on Additional Contact line " + line + ".  Incomplete fax number.");
                    return false;
                }
                if (!isNumeric(contactInputs[i+8].value)) {
                    contactInputs[i+8].focus();
                    displayError("Error on Additional Contact line " + line + ".  Invalid fax number.");
                    return false;
                }
            }
        }
    }
    
    if (theForm.name == "RequestLoginId") {
        var accountAcctFields = document.getElementsByName("AccountNumber");
        var accountCityFields = document.getElementsByName("AccountCity");
        var accountStateFields = document.getElementsByName("AccountState");
        for (var j=0;j<accountCityFields.length;j++) {
            var acctNum = trimAll(accountAcctFields[j].value);
            hasAcct = acctNum != "";
            hasCity = trimAll(accountCityFields[j].value) != "";
            hasState = accountStateFields[j].selectedIndex != 0;    
            if (hasAcct) {
                if (!isAlphaNumeric(acctNum)) {
                    accountAcctFields[j].focus();
                    displayError("Error on SEFL Accounts line " + (j+1) + ".  Account number must be either all numeric or begin with an 'N' followed by numbers.");
                    return false;
                }
                if (acctNum.substring(0,1) == "N" || acctNum.substring(0,1) == "n") {
                    if (!isNumeric(acctNum.substring(1,acctNum.length))) {
                        accountAcctFields[j].focus();
                        displayError("Error on SEFL Accounts line " + (j+1) + ".  Account number must be either all numeric or begin with an 'N' followed by numbers.");
                        return false;
                    }
                    if (acctNum.length != 6 && acctNum.length != 8) {
                        accountAcctFields[j].focus();
                        displayError("Error on SEFL Accounts line " + (j+1) + ".  National account numbers must be either 6 or 8 characters long.");
                        return false;
                    }
                } else {
                    if (!isNumeric(acctNum.substring(0,acctNum.length))) {
                        accountAcctFields[j].focus();
                        displayError("Error on SEFL Accounts line " + (j+1) + ".  Account number must be either all numeric or begin with an 'N' followed by numbers.");
                        return false;
                    }
                    if (acctNum.length != 9) {
                        accountAcctFields[j].focus();
                        displayError("Error on SEFL Accounts line " + (j+1) + ".  Account numbers must 9 characters long.");
                        return false;
                    }
                }
            }
            if (hasCity && !hasState) {
                accountStateFields[j].focus();
                displayError("Error on SEFL Accounts line " + (j+1) + ".  Must enter state if city is specified.");
                return false;
            }
            if (!hasCity && hasState) {
                accountCityFields[j].focus();
                displayError("Error on SEFL Accounts line " + (j+1) + ".  Must enter city if state is specified.");
                return false;
            }
        }
    }
    
    return true;
}

function isAlphaNumeric(str) {
    for(var j=0; j<str.length; j++) {
        var char = str.charAt(j);
        var hh = char.charCodeAt(0);
        if (!((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))) {
            return false;
        }
    }
    return true;
}

function isNumeric(entry) {   
    entry = trimAll(entry);
    var flg=0;
    var str="";
    var spc="";
    var arw="";
    for (var i=0;i<entry.length;i++){
        var cmp="0123456789";
        var tst=entry.substring(i,i+1);
        if (cmp.indexOf(tst)<0){
            flg++;
            str+=" "+tst;
            spc+=tst;
            arw+="^";
        }
        else{arw+="_";}
    }
    if (flg!=0){
        if (spc.indexOf(" ")>-1) {
            str+=" and a space";
        }        
        return false;
    }
    return true;
}

function displayError(msg) {
    document.getElementById("error").innerHTML = msg;
    window.scrollTo(0,0);
}

function lookupSalesmen(elem) {
    var svcCtrCode = elem.options[elem.selectedIndex].value;    
    if (svcCtrCode == "") {
        var salesDropdown = document.getElementById("SalesContact");
        salesDropdown.options.length = 0;
        salesDropdown.disabled = true;
    } else {
        var URLLink = "/seflWebsite/servlet?action=mySeflLogin_getSalesmen" +            
        "&output=JSON&serviceCtr=" + svcCtrCode;        
        var ajax = new SeflAjax();
        ajax.submit(URLLink, buildSalesmenDropdown);
    }
}

function buildSalesmenDropdown(_req) {
    var salesmenTable = eval("(" + _req.responseText + ")");
    salesmenTable = salesmenTable.data;
    var salesDropdown = document.getElementById("SalesContact");
    salesDropdown.options.length = 0;
    var option, i;
    if (salesmenTable.length <= 0) {        
        salesDropdown.disabled = true;
    } else {
        option = document.createElement("OPTION");
        option.text = "";
        option.value = "";                
        salesDropdown.options.add(option);
        salesDropdown.disabled = false;
    }
    for (i=0;i<salesmenTable.length;i++) {
        option = document.createElement("OPTION");
        option.text = salesmenTable[i].name;
        option.value = salesmenTable[i].number;
        salesDropdown.options.add(option);
    }
}

function addContactRows() {
    var contactTable = document.getElementById("contactTable");
    var numRows = contactTable.rows.length;
    var rowIndex = numRows - 1;
    var row, cell, inputBox, textNode, i;
    
    for (i=1;i<=5;i++) {
        //insert new row
        row = contactTable.insertRow(rowIndex++);
        
        //create first cell
        cell = row.insertCell(0);
        cell.className = "textSmall alignRight";        
        textNode = document.createTextNode(rowIndex + ".");
        cell.appendChild(textNode);
        
        //create second cell
        cell = row.insertCell(1);
        cell.className = "textSmall alignCenter";
        inputBox = document.createElement("input");
        inputBox.size = "15";
        inputBox.maxLength = "30";
        inputBox.name = "AdditionalContactName";
        cell.appendChild(inputBox);
        
        //create third cell
        cell = row.insertCell(2);
        cell.className = "textSmall alignCenter";
        inputBox = document.createElement("input");
        inputBox.size = "15";
        inputBox.maxLength = "25";
        inputBox.name = "AdditionalContactTitle";
        cell.appendChild(inputBox);
        
        //create fourth cell
        cell = row.insertCell(3);
        cell.className ="textSmall alignCenter";
        inputBox = document.createElement("input");
        inputBox.size = "15";
        inputBox.maxLength = "40";
        inputBox.name = "AdditionalContactEmail";
        cell.appendChild(inputBox);
        
        //create fifth cell
        cell = row.insertCell(4);
        cell.className = "textSmall alignCenter";
        inputBox = document.createElement("input");
        inputBox.size = "1";
        inputBox.maxLength = "3";
        inputBox.name = "AdditionalPhoneNum1";
        cell.appendChild(inputBox);
        textNode = document.createTextNode(" - ");
        cell.appendChild(textNode);
        inputBox = document.createElement("input");
        inputBox.size = "1";
        inputBox.maxLength = "3";
        inputBox.name = "AdditionalPhoneNum2";
        cell.appendChild(inputBox);
        textNode = document.createTextNode(" - ");
        cell.appendChild(textNode);
        inputBox = document.createElement("input");
        inputBox.size = "2";
        inputBox.maxLength = "4";
        inputBox.name = "AdditionalPhoneNum3";
        cell.appendChild(inputBox);    
        
        //create sixth cell
        cell = row.insertCell(5);
        cell.className = "textSmall alignCenter";
        inputBox = document.createElement("input");
        inputBox.size = "1";
        inputBox.maxLength = "3";
        inputBox.name = "AdditionalFaxNum1";
        cell.appendChild(inputBox);
        textNode = document.createTextNode(" - ");
        cell.appendChild(textNode);
        inputBox = document.createElement("input");
        inputBox.size = "1";
        inputBox.maxLength = "3";
        inputBox.name = "AdditionalFaxNum2";
        cell.appendChild(inputBox);
        textNode = document.createTextNode(" - ");
        cell.appendChild(textNode);
        inputBox = document.createElement("input");
        inputBox.size = "2";
        inputBox.maxLength = "4";
        inputBox.name = "AdditionalFaxNum3";
        cell.appendChild(inputBox);    
    }
}

function addAccountRows() {
    var acctTable = document.getElementById("accountTable");
    var numRows = acctTable.rows.length;
    var rowIndex = numRows - 1;
    var row, cell, inputBox, textNode;
    var stateDropdown = document.getElementById("State");
    var i;
    
    for (i=1;i<=20;i++) {
        //insert new row
        row = acctTable.insertRow(rowIndex++);
        
        //create first cell
        cell = row.insertCell(0);
        cell.className = "textSmall alignRight";        
        textNode = document.createTextNode(rowIndex + ".");
        cell.appendChild(textNode);
        
        //create second cell
        cell = row.insertCell(1);
        cell.className = "textSmall alignCenter";
        inputBox = document.createElement("input");
        inputBox.size = "9";
        inputBox.maxLength = "9";
        inputBox.name = "AccountNumber";
        cell.appendChild(inputBox);
        
        //create third cell
        cell = row.insertCell(2);
        cell.className = "textSmall alignCenter";
        inputBox = document.createElement("input");
        inputBox.size = "22";
        inputBox.maxLength = "22";
        inputBox.name = "AccountCity";
        cell.appendChild(inputBox);
        
        //create fourth cell
        cell = row.insertCell(3);
        cell.className = "textSmall alignCenter";                
        cell.appendChild(cloneStateDropdown(stateDropdown));
        
        //create fifth cell
        cell = row.insertCell(4);
        cell.className = "textSmall alignCenter";
        inputBox = document.createElement("input");
        inputBox.size = "25";
        inputBox.maxLength = "25";
        inputBox.name = "AccountAlias";
        cell.appendChild(inputBox);    
    }
}

function cloneStateDropdown(elem) {
    var selectElem = document.createElement("select");
    var option, i;
    selectElem.name = "AccountState";
    for (i=0;i<elem.options.length;i++) {
        option = document.createElement("option");
        option.value = elem.options[i].value;
        option.text = elem.options[i].text;
        selectElem.options.add(option);
    }    
    return selectElem;
}

function displayPrompt() {
    displayAndCenterDiv(500, 150, "promptBox");
    grayOut(true, {'zindex':'999'});
}

function continueWithProfile() {
    document.getElementById("promptBox").style.display = "none";
    grayOut(false);
}

function displayAndCenterDiv(Xwidth, Yheight, divId) {
    
    // Determine how much the visitor has scrolled
    var scrolledX, scrolledY;
    if( self.pageYoffset ) {
        scrolledX = self.pageXoffset;
        scrolledY = self.pageYoffset;
    } else if( document.documentElement && document.documentElement.scrollTop ) {
        scrolledX = document.documentElement.scrollLeft;
        scrolledY = document.documentElement.scrollTop;
    } else if( document.body ) {
        scrolledX = document.body.scrollLeft;
        scrolledY = document.body.scrollTop;
    }
    
    // Determine the coordinates of the center of browser's window
    var centerX, centerY;
    if( self.innerHeight ) {
        centerX = self.innerWidth;
        centerY = self.innerHeight;
    } else if( document.documentElement && document.documentElement.clientHeight ) {
        centerX = document.documentElement.clientWidth;
        centerY = document.documentElement.clientHeight;
    } else if( document.body ) {
        centerX = document.body.clientWidth;
        centerY = document.body.clientHeight;
    }
    
    // Xwidth is the width of the div, Yheight is the height of the
    // div passed as arguments to the function:
    var leftoffset = scrolledX + (centerX - Xwidth) / 2;
    var topoffset = scrolledY + (centerY - Yheight) / 2;
    
    // The initial width and height of the div can be set in the
    // style sheet with display:none; divId is passed as an argument to // the function
    var o=document.getElementById(divId);
    var r=o.style;
    r.position='absolute';
    r.top = topoffset + 'px';
    r.left = leftoffset + 'px';
    r.display = "block";
} 

function grayOut(vis, options) {
    // Pass true to gray out screen, false to ungray
    // options are optional.  This is a JSON object with the following (optional) properties
    // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
    // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
    // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
    // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
    // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
    // in any order.  Pass only the properties you need to set.
    var options = options || {}; 
    var zindex = options.zindex || 50;
    var opacity = options.opacity || 70;
    var opaque = (opacity / 100);
    var bgcolor = options.bgcolor || '#000000';
    var dark=document.getElementById('darkenScreenObject');
    if (!dark) {
        // The dark layer doesn't exist, it's never been created.  So we'll
        // create it here and apply some basic styles.
        // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
        var tbody = document.getElementsByTagName("body")[0];
        var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
        tbody.appendChild(tnode);                            // Add it to the web page
        dark=document.getElementById('darkenScreenObject');  // Get the object.
    }
    if (vis) {
        // Calculate the page width and height 
        if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
            var pageWidth = document.body.scrollWidth+'px';
            var pageHeight = document.body.scrollHeight+'px';
        } else if( document.body.offsetWidth ) {
            var pageWidth = document.body.offsetWidth+'px';
            var pageHeight = document.body.offsetHeight+'px';
        } else {
            var pageWidth='100%';
            var pageHeight='100%';
        }   
        //set the shader to cover the entire page and make it visible.
        dark.style.opacity=opaque;                      
        dark.style.MozOpacity=opaque;                   
        dark.style.filter='alpha(opacity='+opacity+')'; 
        dark.style.zIndex=zindex;        
        dark.style.backgroundColor=bgcolor;  
        dark.style.width= pageWidth;
        dark.style.height= pageHeight;
        dark.style.display='block';                          
    } else {
        dark.style.display='none';
    }
}

