var zipDefaultText = '';
function OnInit(){
    var zipCodeElement = document.getElementById('ZipCode');
    var insuranceTypeIDElement = document.getElementById('InsuranceTypeID');        
    if (insuranceTypeIDElement.type != "hidden" && zipCodeElement.value == ''){
        zipCodeElement.value = zipDefaultText;
        insuranceTypeIDElement.focus(); 
    }    
    testJavaScriptEnabled();
}

function OnZipFocus(){
    var zipCodeElement = document.getElementById('ZipCode');
    if (zipCodeElement.value == zipDefaultText){
        zipCodeElement.value = '';
    }
}
function OnZipBlur(){
    var zipCodeElement = document.getElementById('ZipCode');
    if (zipCodeElement.value == '' || zipCodeElement.value == null){
        zipCodeElement.value = zipDefaultText;
    }
}
function ValidateForm(){
    //Clear any old error messages;     
    var zipCode = document.getElementById('ZipCode').value;
    var insuranceType = document.getElementById('InsuranceTypeID').value;
    if (insuranceType == "" && document.getElementById('InsuranceTypeID').type == "hidden"){
        insuranceType = document.getElementById('InsuranceTypeID').defaultValue;
    }
	
	var isValid = true;
	if (!IsValidInsuranceType(insuranceType)){	    
	    isValid = false;	    
	}
	if (!IsValidZip(zipCode)){
	    isValid = false;
	}
	return isValid;
}
var insuranceTypes = new Array('auto', 'home', 'health', 'life', 'ltc', 'autohome');
function IsValidInsuranceType(type)
{    
    var isValid = false;    
    for (var i = 0; i < insuranceTypes.length && !isValid; i++)
    {
        var currentInsuranceID = insuranceTypes[i];
        if (type.toLowerCase() == currentInsuranceID){
            isValid = true;            
        }
    }
    if (isValid == false){
        var insuranceTypeIDElement = document.getElementById('InsuranceTypeID');        
        if (insuranceTypeIDElement.type != "hidden"){
            insuranceTypeIDElement.focus();
        }
        var errorMessageElement = document.getElementById('errorMessageNoInsTypeSelected');
        //If the user has already entered a correct insurance type they will not be shown the 
        //page that has this error message.
        if (errorMessageElement != undefined){
            document.getElementById('errorMessageNoInsTypeSelected').style.display = 'block';
            insuranceTypeIDElement.focus();
        }        
    }
    else {
        var errorMessageElement = document.getElementById('errorMessageNoInsTypeSelected');
        //If the user has already entered a correct insurance type they will not be shown the 
        //page that has this error message.
        if (errorMessageElement != undefined){
            document.getElementById('errorMessageNoInsTypeSelected').style.display = 'none';
        }
    }
    return isValid;
}
function IsValidZip(zipCode)
{
    var isValid = true;
    document.getElementById('errorMessageNoZipEntered').style.display = 'none';
    document.getElementById('errorMessageIncorrectZipFormat').style.display = 'none';
    document.getElementById('errorMessageInvalidZip').style.display = 'none';
    if (IsEmptyZip(zipCode)){
        isValid = false;
        document.getElementById('errorMessageNoZipEntered').style.display = 'block';     
    }
    else if (!IsValidZipFormat(zipCode)){
        isValid = false;
        document.getElementById('errorMessageIncorrectZipFormat').style.display = 'block';
    }
    else if (!IsZipVerifiedByDatabase(zipCode)){
        isValid = false;
        document.getElementById('errorMessageInvalidZip').style.display = 'block';
    }
    if (isValid == false){
        document.getElementById('ZipCode').focus();
    }
    return isValid;
}
function IsEmptyZip(z){
	var isValid = false;
	if(z=="" || z==null){
		isValid = true;
	}	
	return isValid;
}
function IsValidZipFormat(zip){
	// strip all non-numerics
	var valid_chars = "01234567890";
	var stripped_zip = "";
	//Zip was given in the format 99999-9999
	if (zip.length == 10 && zip.length > 5 && zip.charAt(5) == '-'){
	    zip = zip.substring(0,4);
	    document.getElementById('ZipCode').value = zip;
	}
	//Check that all the remaining characters are digits.
	var isValid = true;
	for(var i=0;i<zip.length;i++){
		if(valid_chars.indexOf(zip.charAt(i)) == -1){
		    isValid = false;
		}
	}	
	if(isValid == true){
		return true;
	}
	else
		return false;
}
//This does not use the same ajax call as the AutoVehicleSelector page.
function IsZipVerifiedByDatabase(zip){
	var ret = true;
	var url = '../Common/VerifyZip.php?zip=' + zip;
	var xmlHttp = CreateXMLHttp();
	xmlHttp.open("get",url,false);
	xmlHttp.send(null);
	ret=xmlHttp.responseText;
	if(ret=="false")
		return false;
	else
		return true;
}

function CreateXMLHttp() {
	if(typeof XMLHttpRequest != "undefined") {
		return new XMLHttpRequest();	
	}
	else if (window.ActiveXObject){
		var aVersions = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
		
		for( var i=0; i<aVersions.length; i++ ){
			try {
				var oXmlHttp = new ActiveXObject(aVersions[i]);
				return oXmlHttp;
			}
			catch (oError) {
				// Do nothing	
			}
		}
	}
	throw new Error("XMLHttp object could not be created.");
}