﻿//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;

//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;
}

function onServiceChange(DDLctl,ControlType){
    
    var ProdName='';
    if(DDLctl.selectedIndex>0){
        ProdName=DDLctl.value;
    }
    document.getElementById(productTextBoxIdEx).value=ProdName;
}
//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(ValidateZipCode(document.all(zipTextBoxId), true,tdTagId))
    {  
        var zipCode = document.getElementById(zipTextBox).value;
        //var bHasServices=getServiceListByZipCode(zipCode);
        //if(bHasServices){
            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;
            }
            return true;
        //}
        //return false;
    }
    else{
        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);
       }
    }
}
//OnFocus, 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();
        }
    }
}

//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;
    }
}

