var zipDefaultText = 'Enter your Zip Code';
var noInsuranceQuestion;
var postedZipCode;
var postedInsuranceType;
var postedInsuranceQuestion;
var hasInit = false;
var inInit = false;

function OnInit(){
       
    inInit = true;
    var zipCodeElement = document.getElementById('ZipCode');
    var insuranceTypeIDElement = document.getElementById('InsuranceType');
              
    if ((insuranceTypeIDElement.type != "hidden" && insuranceTypeIDElement.type != "none") && zipCodeElement.value == ''){
        zipCodeElement.value = zipDefaultText;
    }
    
    if (document.getElementById('ZipCode') != null) 
    {
        postedZipCode = document.getElementById('ZipCode').value;
    }
    if(document.getElementById('ZipHid') != null){
        postedZipCode = document.getElementById('ZipHid').value;
        document.getElementById('ZipCode').value = postedZipCode;
    }
    else
    {
        postedZipCode = "";
    }
        
    if (document.getElementById('InsuranceType') != null) 
        postedInsuranceType = document.getElementById('InsuranceType').value;
    if (document.getElementById('InsuranceTypeHid') != null)     
        postedInsuranceType = document.getElementById('InsuranceTypeHid').value;
    if (document.getElementById('Type') != null)     
        postedInsuranceType = document.getElementById('Type').value;
    
    postedInsuranceType = postedInsuranceType.toLowerCase();
    if(postedInsuranceType == 'auto' || postedInsuranceType == 'autohome'){
            AddInsuranceQuestion();
    }
    else
    {
        postedInsuranceType = "";
    } 
    
    if (document.getElementById('noinsurance') != null) 
    {
        postedInsuranceQuestion = document.getElementById('noinsurance').value;
    }
    else
    {
        postedInsuranceQuestion = "";
    } 
        
    hasInit = true;
    testJavaScriptEnabled();
    checkpostedvalues();
    inInit = false;
}

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 checkpostedvalues()
{
    var postedvalues = new Array(3);
    postedvalues[0]="";
    postedvalues[1]="";
    postedvalues[2]="";
        
    if (!IsValidInsuranceType(postedInsuranceType)){	    
	    AddInsuranceType();	
	}
	else
	{
	    // Insurance Type is valid set array value
	    postedvalues[0]="Good Type";
	}
	
	if (!IsValidZip(postedZipCode)){
	    AddZipCode();
	}
	else
	{
	    postedvalues[1]="Good Zip";
	}
	
	if(hasInit && postedInsuranceQuestion !== undefined)
	{
	    if(noInsuranceQuestion == 'yes' && (postedInsuranceType.toLowerCase() == "auto"  || postedInsuranceType.toLowerCase() == "autohome" || document.getElementById('InsuranceType').value == "auto"))
	    {
	        if (!IsValidInsuranceQuestion())
	        {
	            AddInsuranceQuestion();
	        }
	        else
	        {
	            postedvalues[2]="Good Question";
	        }
	    }
	}
	
}

function ValidateForm(){
             
    if(!inInit){
        var zipCode = document.getElementById('ZipCode').value;
        var insuranceType = document.getElementById('InsuranceType').value;
        var insuranceTypeHid = document.getElementById('InsuranceTypeHid').value;
        if (insuranceType == "none" && document.getElementById('InsuranceTypeDiv').style.display == "none"){
            insuranceType = insuranceTypeHid;
        }
    	
	    var isValid = true;
	    if (!IsValidInsuranceType(insuranceType)){	    
	        isValid = false;
	    }
    	
	    if (!IsValidZip(zipCode)){
	        isValid = false;
	    }
    	
	    if (document.getElementById('NoInsuranceQuestion') != null) 
        {
            noInsuranceQuestion = document.getElementById('NoInsuranceQuestion').value;
        }
    	
	    if(noInsuranceQuestion == 'yes' && (currentInsuranceID == "auto"))
	    {
	        if (!IsValidInsuranceQuestion())
	        {
	            isValid = false;
	        }
	    }
        return isValid;
    }
}

var insuranceTypes = new Array('auto', 'home', 'health', 'life', 'ltc', 'autohome', 'renter');
var currentInsuranceID;
function IsValidInsuranceType(type)
{    
    var isValid = false;    
    for (var i = 0; i < insuranceTypes.length && !isValid; i++)
    {
        currentInsuranceID = insuranceTypes[i];
        if (type.toLowerCase() == currentInsuranceID){
            isValid = true;
            document.getElementById('InsuranceTypeHid').value = currentInsuranceID;
            break;            
        }
        else
            currentInsuranceID = "";
    }
    if(document.getElementById('InsuranceTypeHid').value != 'NOTSELECTED' ){
        isValid = true;
        currentInsuranceID = document.getElementById('InsuranceTypeHid').value;
        }
    
    if (isValid == false){
        var insuranceTypeIDElement = document.getElementById('InsuranceTypeDiv');        
        if (insuranceTypeIDElement != null){ 
            if(insuranceTypeIDElement.style.display != "none"){
                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){
            if (insuranceTypeIDElement != null){ 
                if (insuranceTypeIDElement.style.display != "none"){
                    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';
        }
    }
    
    if (document.getElementById('NoInsuranceQuestion') != null) 
    {
        noInsuranceQuestion = document.getElementById('NoInsuranceQuestion').value;
    }
    else
    {
        noInsuranceQuestion = "";
    }
    
	if(hasInit && postedInsuranceQuestion !== undefined)
	{
	    if(noInsuranceQuestion == 'yes' && (currentInsuranceID == "auto" || currentInsuranceID == "autohome") && postedInsuranceQuestion.length <= 0)
	    {
	        AddInsuranceQuestion();    	    
	    }
	    else
	    {
	        RemoveInsuranceQuestion();
	    }
	}
	
    return isValid;
}

function AddInsuranceType(){
    if(document.getElementById('InsuranceTypeHid') != null){
        TypeValue = document.getElementById('InsuranceTypeHid').value;
        if(TypeValue == 'NOTSELECTED' || TypeValue == '')
        {
            document.getElementById('InsuranceTypeDiv').style.display = 'block';
        }
    }
}

function AddZipCode(){
    if(document.getElementById('ZipDiv') != null)   
        document.getElementById('ZipDiv').style.display = 'block';
}

function AddInsuranceQuestion(){
    if(document.getElementById('NoInsuranceDiv') != null){  
        document.getElementById('NoInsuranceDiv').style.display = 'block';
        document.getElementById('NoInsuranceDiv').style.visible = true;
    }    
}

function RemoveInsuranceQuestion(){
    
    if(document.getElementById('NoInsuranceDiv') != null)
        document.getElementById('NoInsuranceDiv').style.display = 'none';
}
function IsValidInsuranceQuestion()
{
    var isValid = true;
    document.getElementById('errorMessageNoQuestion').style.display = 'none';
    if(noInsuranceQuestion == 'yes' && (currentInsuranceID == "auto" || currentInsuranceID == "autohome"))
    {
        if(document.getElementById('rblInsurance').checked || document.getElementById('rblNoInsurance').checked)
        {
            if(document.getElementById('rblInsurance').checked)
                document.getElementById('NoInsuranceHid').value = 'is yes';
            if(document.getElementById('rblNoInsurance').checked)
                document.getElementById('NoInsuranceHid').value = 'is no';    
            isValid = true;
        }
        else if(postedInsuranceQuestion !== undefined)
        {
            if(postedInsuranceQuestion.length > 0)
                isValid = true;
            else
                isValid = false;    
        }
        else
        {
            isValid = false;
        }
        
        if (isValid == false && inInit == false){
            document.getElementById('errorMessageNoQuestion').style.display = 'block';
        }
    }
    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;
        if(!inInit)
            document.getElementById('errorMessageNoZipEntered').style.display = 'block';     
    }
    else if (!IsValidZipFormat(zipCode) && !inInit){
        isValid = false;
        if(!inInit){
            document.getElementById('errorMessageIncorrectZipFormat').style.display = 'block';
        }
    }
    else if (!IsZipVerifiedByDatabase(zipCode)){
        isValid = false;
        if(!inInit){
            document.getElementById('errorMessageInvalidZip').style.display = 'block';
        }
    }
    
    if (isValid == true){
        document.getElementById('ZipHid').value = zipCode;
    }
    
    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;
}

//Get the redirect URL from the PartnerSite and return to caller
function getRedirectURL(hostName, zip, sessionId){
	var ret;
	var url = hostName + 'GetCarrierRedirectURL.aspx?sessionId=' + sessionId + '&zip=' + zip;
	var xmlHttp = CreateXMLHttp();
	xmlHttp.open("get",url,false);
	xmlHttp.send(null);
	ret=xmlHttp.responseText;
	return ret;
}

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.");
}
