﻿var theFormSubmitted = false;
var OPTION_PREFIX = "o_";
var bEventAttached=false;
var AppCurrentSiteName;
var bSubmitClicked=false;
var RootFolder;

function onServiceClick(ctl)
{
    if(!bSubmitClicked) return;
    document.getElementById('lblError').innerText=GetErrors(false);
}
function onCustomerTypeChange(ctl){
    if(ctl.id=="chkResidential")
        if(ctl.checked){
            document.getElementById('chkCommercial').checked=false;
            document.getElementById('spanBusinessName').innerText='Business Name';
        }
        else{
            document.getElementById('chkCommercial').checked=true;
            document.getElementById('spanBusinessName').innerHTML='<font class="Sharequired">*</font>Business Name';
        }
        /*if(!this.checked && !chkCommercial.checked) {
            document.getElementById('chkCommercial').checked=true;
            document.getElementById('spanBusinessName').innerText='*Business Name';
        }*/
    else{
        if(ctl.checked){
            document.getElementById('chkResidential').checked=false;
            document.getElementById('spanBusinessName').innerHTML='<font class="Sharequired">*</font>Business Name';
        }
        else{
            document.getElementById('chkResidential').checked=true;
            document.getElementById('spanBusinessName').innerText='Business Name';
        }
        /*if(!this.checked && !chkResidential.checked) {
            document.getElementById('chkResidential').checked=true;
            document.getElementById('spanBusinessName').innerText='*Business Name';
        }*/
    }
}
function removeDoubleLine(value) {
   var str = value;
   while(str.indexOf("\r\n\r\n") >= 0) {
      str = str.replace(/\r\n\r\n/g, "\r\n")      
   }
   return str;
}

function setTimeZone()
{
	var now = new Date();
    var gmtoffset = now.getTimezoneOffset();
    theForm.userTimeZone.value = gmtoffset;     
    return false;
}

function onTimeSelected(oSrc)
{
    if (oSrc.selectedIndex == (oSrc.length - 1))
        callMessage.style.display = "block";
    else
        callMessage.style.display = "none";
}
function ShowCalender(cllerId)
{   
    var oTarget=event.srcElement;
    if(cllerId==undefined){        
        divCalendar.style.display = "none";
        return;
    }
    else if(cllerId.id=="ImgCalender"){
        setEventListener();
        if( divCalendar.style.display == "none")
        {
            divCalendar.style.display = "inline";
        }      
        else
            divCalendar.style.display = "none";
    }
    else if(oTarget.nodeName=="A"){
        divCalendar.style.display = "inline";
        return true;
    }
    else{
        divCalendar.style.display = "none";
    } 
}
function setEventListener() {

  if (document.addEventListener) {
    document.addEventListener('keypress', ShowCalender, false);
    //document.addEventListener('onkeydown', ShowCalender, true);
    document.addEventListener('onmousedown', ShowCalender, false);
  }
  else if (document.attachEvent  && bEventAttached==false) {
    bEventAttached=true;
    document.attachEvent('onkeydown', ShowCalender);
    document.attachEvent('onmousedown', ShowCalender);
  }
  else document.onkeydown = ShowCalender;

}
function onControlChange(control){
    if(!bSubmitClicked) return;
    if(control.value==''){
        document.getElementById('lblError').innerText=control.ErrorText;        
    }
    else{
        var bac=document.getElementById('lblError').innerText.replace(control.ErrorText,'');
        document.getElementById('lblError').innerText.replace(control.ErrorText,'');
    }
}
function onEmailBlur(control){
    if(!bSubmitClicked) return;
    document.getElementById('lblError').innerText=GetErrors(false);
}
function onFoucsOut(control){
    if(!bSubmitClicked) return;
    if(document.getElementById('lblError').innerText!=''){
        if(control.value!=''){
            document.getElementById('lblError').innerText=GetErrors(false);
        }
    }
}
function ValidateForm(theForm1){
    if (theFormSubmitted) {
        alert("This form has already been submitted.");
        bSubmitClicked=false;
	    return false;    
	}
	bSubmitClicked=true;
	var errorMsg=GetErrors(true);
	
	if (errorMsg.length > 0) {
	    document.getElementById('lblError').innerText=errorMsg;
	    theFormSubmitted=false;
	    bSubmitClicked=false;
		return false;
	}
	else{
	    var bHasService=checkAppServiceByZipCode(theForm.zip.value);
	    if(bHasService){
		    theFormSubmitted = true;
		    bSubmitClicked=false;
		    return true;
		}
		else{
		    document.getElementById('lblError').innerText='We do not currently offer service in your area.';
		    theFormSubmitted=false;
		    bSubmitClicked=false;
		    return false;
		}
	}
}
function GetErrors(showFoucs)
{
    var bSetFocus=true;
	var errorCount = 0;
	var errorArray = new Array();

	var errorMessage = "";
	
	var dayPhoneSectionError = false;
	var evePhoneSectionError = false;
    var ControlToFoucs=new Array();
    
    // Clean and validate the name.       
	if (theForm.fName.value != null) {
		theForm.fName.value = theForm.fName.value.replace(/\"/g,"");
	}
                      
	errorMessage = requiredField(theForm.fName, "First Name", false);//errorCount==0
	if (errorMessage.length > 0) {
		errorArray[errorCount++] = errorMessage;
		ControlToFoucs[errorCount]=theForm.fName;
	}

    if (theForm.lName.value != null) {
		theForm.lName.value = theForm.lName.value.replace(/\"/g,"");
	}

	errorMessage = requiredField(theForm.lName, "Last Name", false);
	if (errorMessage.length > 0) {
		errorArray[errorCount++] = errorMessage;
		ControlToFoucs[errorCount]=theForm.lName;
	}

	// Clean and validate the address.
	if (theForm.add1.value != null) {
		theForm.add1.value = theForm.add1.value.replace(/\"/g,"");
	}

	errorMessage = requiredField(theForm.add1, "Address 1", false);
	if (errorMessage.length > 0) {
		errorArray[errorCount++] = errorMessage;
		ControlToFoucs[errorCount]=theForm.add1;
	}

	if (theForm.add2.value != null) {
		theForm.add2.value = theForm.add2.value.replace(/\"/g,"");
	}

	if (theForm.city.value != null) {
		theForm.city.value = theForm.city.value.replace(/\"/g,"");
	}

	errorMessage = requiredField(theForm.city, "City", false);
	if (errorMessage.length > 0) {
		errorArray[errorCount++] = errorMessage;
		ControlToFoucs[errorCount]=theForm.city;
	}

	if (!validSelect(theForm.state)) {
		errorArray[errorCount++] = "State";
		if (errorCount == 1) {
			theForm.state.focus();
			ControlToFoucs[errorCount]=theForm.state;
		}
	}
	
	if (theForm.zip.value != null) {
		theForm.zip.value = theForm.zip.value.replace(/ /g,"");
	}

	errorMessage = requiredField(theForm.zip, "ZIP Code", false);
	if (errorMessage.length == 0) {
		errorMessage = isZip(theForm.zip, "ZIP Code", false)		
	}
	
	if (errorMessage.length > 0) {
		errorArray[errorCount++] = errorMessage;
		ControlToFoucs[errorCount]=theForm.zip;
	}

	// Validate the phone numbers.
	errorMessage = requiredField(theForm.dayPhoneArea, "Day Phone Area Code", false);
	if (errorMessage.length == 0) {
		errorMessage = isNumber(theForm.dayPhoneArea, "Day Phone Area Code", 3, false); 
	}
	if (errorMessage.length > 0) {
		dayPhoneSectionError = true;
		errorArray[errorCount++] = errorMessage;
		ControlToFoucs[errorCount]=theForm.dayPhoneArea;
	}

	errorMessage = requiredField(theForm.dayPhoneExch, "Day Phone Exchange", false);
	if (errorMessage.length == 0) {
		errorMessage = isNumber(theForm.dayPhoneExch, "Day Phone Exchange", 3, false); 		
	}
	if (errorMessage.length > 0) {
		dayPhoneSectionError = true;
		errorArray[errorCount++] = errorMessage;
		ControlToFoucs[errorCount]=theForm.dayPhoneExch;
	}

	errorMessage = requiredField(theForm.dayPhoneNum, "Day Phone Suffix", false);
	if (errorMessage.length == 0) {
		errorMessage = isNumber(theForm.dayPhoneNum, "Day Phone Suffix", 4, false); 
	}
	if (errorMessage.length > 0) {
		dayPhoneSectionError = true;
		errorArray[errorCount++] = errorMessage;
		ControlToFoucs[errorCount]=theForm.dayPhoneNum;
	}

	//errorMessage = isNumber(theForm.dayPhoneExt, "Day Phone Extension", 0, false);
	//if (errorMessage.length > 0) {
	//	errorArray[errorCount++] = errorMessage;
	//}

   	if ((theForm.dayPhoneArea.value != "" && theForm.dayPhoneExch.value != "" && theForm.dayPhoneNum.value != "")
   	&& (!dayPhoneSectionError))
 
   	{
		errorMessage = customPhoneValidations(theForm.dayPhoneArea, theForm.dayPhoneExch, theForm.dayPhoneNum, "Day phone", false);
		if (errorMessage.length > 0) {
			errorArray[errorCount++] = errorMessage;
			ControlToFoucs[errorCount]=theForm.dayPhoneExch;
		}
	}

    //errorMessage = requiredField(theForm.evePhoneArea, "Evening Phone Area Code", false);
   	if (theForm.evePhoneArea.value != "" || theForm.evePhoneExch.value != "" || theForm.evePhoneNum.value != "") {
		errorMessage = requiredField(theForm.evePhoneArea, "Evening Phone Area Code", false);
		if (errorMessage.length == 0) {
			errorMessage = isNumber(theForm.evePhoneArea, "Evening Phone Area Code", 3, false); 
		}
		if (errorMessage.length > 0) {
			evePhoneSectionError = true;
			errorArray[errorCount++] = errorMessage;
			ControlToFoucs[errorCount]=theForm.evePhoneArea;
		}
	}
	
	//errorMessage = requiredField(theForm.evePhoneExch, "Evening Phone Exchange", false);
   	if (theForm.evePhoneArea.value != "" || theForm.evePhoneExch.value != "" || theForm.evePhoneNum.value != "") {
		errorMessage = requiredField(theForm.evePhoneExch, "Evening Phone Exchange", false);
		if (errorMessage.length == 0) {
			errorMessage = isNumber(theForm.evePhoneExch, "Evening Phone Exchange", 3, false); 
		}
		if (errorMessage.length > 0) {
			evePhoneSectionError = true;
			errorArray[errorCount++] = errorMessage;
			ControlToFoucs[errorCount]=theForm.evePhoneExch;
		}
	}
	
	//errorMessage = requiredField(theForm.evePhoneNum,  "Evening Phone Suffix", false);
   	if (theForm.evePhoneArea.value != "" || theForm.evePhoneExch.value != "" || theForm.evePhoneNum.value != "") {
		errorMessage = requiredField(theForm.evePhoneNum, "Evening Phone Suffix", false);
		if (errorMessage.length == 0) {
			errorMessage = isNumber(theForm.evePhoneNum, "Evening Phone Suffix", 4, false);
		} 
		if (errorMessage.length > 0) {
			evePhoneSectionError = true;
			errorArray[errorCount++] = errorMessage;
			ControlToFoucs[errorCount]=theForm.evePhoneNum;
		}
	}
	
	//errorMessage = isNumber(theForm.evePhoneExt, "Evening Phone Extension", 0, false);
	//if (errorMessage.length > 0) {
	//	errorArray[errorCount++] = errorMessage;
	//}
	

   	if ((theForm.evePhoneArea.value != "" && theForm.evePhoneExch.value != "" && theForm.evePhoneNum.value != "") 
   	&& (!evePhoneSectionError))
   	{
		errorMessage = customPhoneValidations(theForm.evePhoneArea, theForm.evePhoneExch, theForm.evePhoneNum, "Evening phone", false);
		if (errorMessage.length > 0) {
			errorArray[errorCount++] = errorMessage;     
			ControlToFoucs[errorCount]=theForm.evePhoneArea;   
		}
	}
	
	// Check the email
	errorMessage = requiredField(theForm.email, "Email address", false);
	if (errorMessage.length == 0) {
		errorMessage = isEmail(theForm.email, false);
	}
	if (errorMessage.length > 0) {
		errorArray[errorCount++] = "Email address"; 
		ControlToFoucs[errorCount]=theForm.email;
	}

	// Check the Type of Service
	if (theForm.productSelect.tagName != null && theForm.productSelect.tagName == 'SELECT')
	{
		// Check the Type of Service
	    if (!validSelect(theForm.productSelect)) {
		    errorArray[errorCount++] = "Type of Service";
		    if (errorCount == 1) {
			    theForm.productSelect.focus();
			    ControlToFoucs[errorCount]=theForm.productSelect;
		    }
	    }
	}
	else
	{
	    if (!validRadio(theForm.productSelect)) {
		    errorArray[errorCount++] = "Type of Service";
		    if (errorCount == 1) {
			    theForm.productSelect[0].focus();
			    ControlToFoucs[errorCount]=theForm.productSelect;
		    }
	    }
	}
	
	errorMessage = ""; 
	for (var i=0; i < theForm.elements.length; i++) {
		var element = theForm.elements[i];
		var eName = element.name.substr(OPTION_PREFIX.length)
		if (element.name.substr(0,OPTION_PREFIX.length) == OPTION_PREFIX) {
			if ((element.type == "select-one") || (element.type == "select-multiple")) {
				if (!validSelect(element)) {
					element.focus();
					errorMessage = "unspecified option";
				}
			}
			else {
				if (element.type == "text") {
					errorMessage = requiredField(element,  "unspecified option", false);
				}
			}
		}
	}
	if (errorMessage.length > 0) {
		errorArray[errorCount++] = "Service Options";
	}
    
    if(theForm.chkCommercial.checked && trim(theForm.busName.value)=="")
    {
        errorArray[errorCount++] = "Business Name";
	    if (errorCount == 1) {
		    theForm.busName.focus();
		    ControlToFoucs[errorCount]=theForm.busName;
	    }
    }
	// Set the max. chars for the comment to 900 to allow 100 chars for things we add behind the scenes.
	if(theForm.comment!=undefined){
	    var commentMaxChars = 900;
	    var commentLength = theForm.comment.value.length;
	    var commentOverage = commentLength - commentMaxChars;
	    if (commentLength > commentMaxChars) {
	         errorMessage = "The current number of characters in the comment is " + commentLength + ", \n   but it cannot accept more than " + commentMaxChars + ".";
	    }
	    else {
		    errorMessage = "";
	    }
	    if (errorMessage.length > 0) {
		    errorArray[errorCount++] = errorMessage;
	    }
    }
	// handle errors
	var errorMsg = "";

	// multiple standard errors
	if (errorCount > 1) {
		errorMsg = "Please specify a valid value for the following:\n";
		for (idx = 0; idx < errorCount; idx++) {
			errorMsg += "- " + errorArray[idx] + "\n";
		}
	}
	// single standard error
	else { 
		if (errorCount == 1) {
			errorMsg = "Please specify a valid value for \n" + errorArray[0];
		}
	}

	if(showFoucs && errorMsg!=""){
        for(var iIndex=0;iIndex<ControlToFoucs.length;iIndex++){
            try{
                ControlToFoucs[iIndex].focus();
                break;
            }
            catch(e) {}
        }
    }
	return errorMsg;
}

function getSelectedProduct()
{
    var prodSel = theForm.productSelect;
    var prodVal = "";

    for (i=0; i<prodSel.length; i++)
      if (prodSel[i].checked == true)
		    prodVal = prodSel[i].value;

	    return prodVal;
}

//Call WS to get the available services for the enterd zip code
function checkAppServiceByZipCode(zipCode){
    if(zipCode!="" && zipCode!=undefined){
        var SelectedService='';
        var navRoot = document.getElementById('tdAppOptionServiceLabel');
        var iCount=navRoot.children.length;
        var bShowError=false;
        if(navRoot!=null){                
            for(var iIndex=0;iIndex<navRoot.children.length;iIndex++)
            {
                var oTag=navRoot.children[iIndex];
                if(oTag.type=="radio" && oTag.nodeName=="INPUT")
                {
                    if(oTag.checked){
                        SelectedService=oTag.value;
                        break;
                    }
                }
            }
            if(SelectedService!=''){
                var bIsOK=checkIsServiceInZip(zipCode,AppCurrentSiteName,SelectedService);
                return bIsOK;
            }
        }
        else{
            return false;
        }        
    }
    return false;
}
