﻿//ControlType: RS=Request Serive User control,ZCS=ZipCodeSearch User Control
//Request Service Control declations
var zipTextBoxId;
var resultLableId;
var productsDdlId;
var zipTextBoxIdEx;
var productTextBoxIdEx;
var userZip;
var SweepWS;
var CurrentSiteName;
var RootFolder;

var bestDateId;
var bestTimeId;
var fnameTextBoxId;
var lnameTextBoxId;
var addressTextBoxId;
var cityTextBoxId;
var stateTextBoxId;
var phoneTextBoxId;
var emailTextBoxId;
var promoTextBoxId;
var commentsTextBoxId;
var receiveOffers;
var commentsTextBoxIdEx;

//Zip Code Search Control declarations
var ZipCodeSearchZipTextBoxId;
var ZipCodeSearchErrorLableId;
var ZipCodeSearchCurrentSiteName;
var ZipCodeSearchUserZip;

//ESignup
var SignUpResultLableId;

//Validate Zip code and display relevent error message if appliable.
function ValidateZipCode(ZipControl, setFocus,ErrorCtlId)
{
    var valid = "0123456789";
    var zipCode = ZipControl;// document.getElementById(fieldname);        
    
    var fieldValue = zipCode.value;
    if (fieldValue.length == 0)
    {
        if (setFocus) zipCode.focus();
        document.getElementById(ErrorCtlId).innerHTML='Please enter your 5 digit ZIP code.';
        return false;
    }

    // return error if there are not 5 or 10 characters in the zip
    if (fieldValue.length!=5 && fieldValue.length!=10)
    {
        if (setFocus) zipCode.focus();
        document.getElementById(ErrorCtlId).innerHTML='Please make sure your ZIP code is 5 digits.';
        return false;
    }

    // return error if zip code is formatted incorrectly
    for (var i=0; i < fieldValue.length; i++)
    {
        fieldString = fieldValue.substring(i, i+1);

        // return error if the character is not a digit or hyphen
        if (valid.indexOf(fieldString) == "-1") {
            if (setFocus) zipCode.focus();
            document.getElementById(ErrorCtlId).innerHTML='Your ZIP code must contain only numbers.';
	        return false;
        }

        // return error if the character is a hyphen, but it is not in position 6
        if (fieldString == "-") {
            if (i != 5) {
                if (setFocus) zipCode.focus();
                document.getElementById(ErrorCtlId).innerHTML='Please place the dash between the fifth and sixth digits of a ten digit ZIP code.';
		        return false;
            }
        }

        // return error if there are 10 characters and the 6th is not a hyphen
        if ((fieldValue.length==10) && fieldValue.charAt(5)!="-") {
            if (setFocus) zipCode.focus();
            document.getElementById(ErrorCtlId).innerHTML='Please place the dash between the fifth and sixth digits of a ten digit ZIP code.';
	        return false;
        }
    }
    if (fieldValue.length==5 && (fieldValue=='00000' || fieldValue=='11111' || fieldValue=='22222' || fieldValue=='33333' || fieldValue=='44444' || fieldValue=='55555' || 
                                fieldValue=='66666' || fieldValue=='77777' || fieldValue=='88888' || fieldValue=='99999'))
    {
        if (setFocus) zipCode.focus();
        document.getElementById(ErrorCtlId).innerHTML='Please enter a valid ZIP code.';
        return false;
    }
    return true;
}


//IF zip filed is valied, then check for available services by calling WS
function OnAppointmentClicked()
{
    var zipTextBox=document.all(zipTextBoxId).id;
    var tdTagId=document.all(resultLableId).id;
    var ddlProduct=document.all(productsDdlId).id;

    if (document.getElementById(zipTextBoxId).value.trim() == '' || document.getElementById(zipTextBoxId).value == 'ZIP Code'){
        document.getElementById(resultLableId).innerHTML='Please enter your zip code.';
        return false;
    }
    else if (document.getElementById(productsDdlId).selectedIndex == 0){
        document.getElementById(resultLableId).innerHTML='Please select a service.';
        return false;
    }
    else if (document.getElementById(bestDateId).value.trim() == '' || document.getElementById(bestDateId).value == 'Select a date') {
        document.getElementById(resultLableId).innerHTML='Please enter best date for appointment.';
        return false;
    }
    else if (document.getElementById(bestTimeId).selectedIndex == 0) {
        document.getElementById(resultLableId).innerHTML='Please select best time for appointment.';
        return false;
    }
    else if (document.getElementById(fnameTextBoxId).value.trim() == '' || document.getElementById(fnameTextBoxId).value == 'First Name') {
        document.getElementById(resultLableId).innerHTML='Please enter your first name.';
        return false;
    }
    else if (document.getElementById(lnameTextBoxId).value.trim() == '' || document.getElementById(lnameTextBoxId).value == 'Last Name') {
        document.getElementById(resultLableId).innerHTML='Please enter your last name.';
        return false;
    }
    else if (document.getElementById(addressTextBoxId).value.trim() == '' || document.getElementById(addressTextBoxId).value == 'Address') {
        document.getElementById(resultLableId).innerHTML='Please enter your address.';
        return false;
    }
    else if (document.getElementById(cityTextBoxId).value.trim() == '' || document.getElementById(cityTextBoxId).value == 'City') {
        document.getElementById(resultLableId).innerHTML='Please enter your city.';
        return false;
    }
    else if (document.getElementById(stateTextBoxId).selectedIndex == 0){
        document.getElementById(resultLableId).innerHTML='Please select your state.';
        return false;
    }
    else if (document.getElementById(phoneAreaTextBoxId).value.trim() == '' || document.getElementById(phoneAreaTextBoxId).value == '###' || document.getElementById(phoneAreaTextBoxId).value.trim().length != 3) {
        document.getElementById(resultLableId).innerHTML='Please enter a valid phone area code.';
        return false;
    }
    else if (document.getElementById(phoneExchTextBoxId).value.trim() == '' || document.getElementById(phoneExchTextBoxId).value == '###' || document.getElementById(phoneExchTextBoxId).value.trim().length != 3) {
    document.getElementById(resultLableId).innerHTML = 'Please enter a valid phone exchange.';
        return false;
    }
    else if (document.getElementById(phoneNumTextBoxId).value.trim() == '' || document.getElementById(phoneNumTextBoxId).value == '####' || document.getElementById(phoneNumTextBoxId).value.trim().length != 4) {
    document.getElementById(resultLableId).innerHTML = 'Please enter a valid phone number.';
        return false;
    }
    else if (document.getElementById(emailTextBoxId).value.trim() == '' || document.getElementById(emailTextBoxId).value == 'Email') {
        document.getElementById(resultLableId).innerHTML='Please enter your email address.';
        return false;
    }
    else if (!IsValidEmail(document.getElementById(emailTextBoxId).value)){
        document.getElementById(resultLableId).innerHTML='Please enter a valid email address.';
        return false;
    }
    else
    {
        if(ValidateZipCode(document.all(zipTextBoxId), true,tdTagId))
        {  
            var zipCode = document.getElementById(zipTextBox).value;
            var ProdName='';
            if(document.getElementById(productsDdlId).selectedIndex>0){
                ProdName=document.getElementById(productsDdlId).value;
            }
            document.getElementById(productTextBoxIdEx).value=ProdName;
            document.getElementById(zipTextBoxIdEx).value=zipCode;
            if(ProdName==''){
                document.getElementById(resultLableId).innerHTML='Select a service.';
                return false;
            }
            //20101129 - if promo code is default value, clear it out
            if (document.getElementById(promoTextBoxId).value.toUpperCase() == 'PROMOTION CODE')
                document.getElementById(promoTextBoxId).value = '';
            return true;
        }
    }
    return false;
}

//Validate the Zip Code on change
function onZipChange(oZipControl,ControlType){
    if(ControlType=='RS'){
        var sZipText = userZip;
        if(oZipControl.value==''){
            oZipControl.value=sZipText;
            document.getElementById(resultLableId).innerHTML='';
        }
        else if(oZipControl.value==sZipText){
            document.getElementById(resultLableId).innerHTML='';
        }
        else{
            var zipTextBox=oZipControl.id;                
            var bOk=ValidateZipCode(oZipControl, true,resultLableId);
       }
       document.getElementById(zipTextBoxIdEx).value=oZipControl.value;
    }
    else if(ControlType=='ZCS'){
        var sZipText = ZipCodeSearchUserZip;
        if(oZipControl.value==''){
            oZipControl.value=sZipText;
            document.getElementById(ZipCodeSearchErrorLableId).innerHTML='';
        }
        else if(oZipControl.value==sZipText){
            document.getElementById(ZipCodeSearchErrorLableId).innerHTML='';
        }
        else{
            var zipTextBox=oZipControl.id;                
            var bOk=ValidateZipCode(oZipControl, true,ZipCodeSearchErrorLableId);
       }
    }
}
//OnBlur, set the default value if empty
function SetZipField(oZipField,ControlType)
{
    if(ControlType=='RS'){
        var sZipText = userZip;    
        if (oZipField.value=='')
        {
            oZipField.value=sZipText;
            document.getElementById(resultLableId).innerHTML='';
        }
    }
    else if(ControlType=='ZCS'){
        var sZipText = ZipCodeSearchUserZip;    
        if (oZipField.value=='')
        {
            oZipField.value=sZipText;
            document.getElementById(ZipCodeSearchErrorLableId).innerHTML='';
        }
    }
}

//OnFocus, set the default value if empty
function onZipFocs(oZipField,ControlType){
    if(ControlType=='RS'){
        if (oZipField.value==userZip){
            oZipField.value='';
            document.getElementById(resultLableId).innerHTML='';
        }
        else{
            oZipField.select();
        }
    }
    else if(ControlType=='ZCS'){
        if (oZipField.value==ZipCodeSearchUserZip){
            oZipField.value='';
            document.getElementById(ZipCodeSearchErrorLableId).innerHTML='';
        }
        else{
            oZipField.select();
        }
    }
}

function onFieldFocus(oField, defaultValue){
    document.getElementById(resultLableId).innerHTML='';
    oField.select();
}

//Call WS to get the available services for the enterd zip code
function getServiceListByZipCode(zipCode){
    if(zipCode!="" && zipCode!=undefined){
        var sList='';
        sList=ServiceListByZipCode(zipCode,CurrentSiteName);
        if(sList==''){
            document.getElementById(resultLableId).innerHTML='We do not currently offer service in your area.';
            return false;
        }
        else{
            var ddlProduct=document.all(productsDdlId).id;
            var oRows=sList.split('|');
            for(var iRow=0;iRow<oRows.length;iRow++){
                var sCols=oRows[iRow].split('~');
                if(sCols[0]!='' && sCols[0]!=undefined){
                    var OptSerive = document.createElement("OPTION");
                    OptSerive.text = sCols[1];
                    OptSerive.value = sCols[0];
                    document.all(productsDdlId).options.add(OptSerive); 
                }
            }
            document.getElementById(resultLableId).innerHTML='';
            return true;
        }
    }
    return false;
}

//################### Zip Code Search Function #################
//Call form Zip Code Search control button click.
function OnUserZipClick(){
    var zipTextBox=document.all(ZipCodeSearchZipTextBoxId).id;
    var tdTagId=document.all(ZipCodeSearchErrorLableId).id;
    if(ValidateZipCode(document.all(ZipCodeSearchZipTextBoxId), true,tdTagId))
    {  
        return true;
    }
    else{
        return false;
    }
}



