﻿//////////////////////////////////////////////////////////////
// variables declared on the server for use in this js code //
// var jsTxtZipCodeID                                       //
// var jsDdInsuranceTypeID                                  //
//                                                          //   
// var jsTxtZipCodeAgentID                                  //
// var jsTxtAddressAgentID                                  //
// var jsTxtCityAgentID                                     //
// var jsDdStateAgentID                                     //
//                                                          //
// var lblErrInvalidZipID                                   //
// var isErrorOnID                                          //
//////////////////////////////////////////////////////////////
// variables declared on small quote control                //
// var jsTxtZipCodeSQID                                     //
// var jsDdInsuranceTypeSQID                                //
//                                                          //   
// var lblErrInvalidZipSQID                                //
//////////////////////////////////////////////////////////////
// variables declared in this file below                    //
    var jsTxtZipCode
    var jsDdInsuranceType
  
    var jsTxtZipCodeAgent
    var jsTxtAddressAgent
    var jsTxtCityAgent
    var jsDdStateAgent
    
    var invalidZipError;
    
    var jsTxtZipCodeSQ
    var jsDdInsuranceTypeSQ
    
    var invalidZipErrorSQ
    
    var zipPattern = /\d{5}/;
    var ddInsurReplacement;
    var insurPrompt;
    
    var ddInsurReplacementSQ;
    var insurPromptSQ;
   
    var cityPattern = /[^\a-\z\A-\Z\s]/;
    var ddStateReplacement;
    var statePrompt;
    
    var zipQuoteErrorOn;
    var zipAgentErrorOn;
    var addressErrorOn;
    var cityErrorOn;
    
    var zipQuoteErrorOnSQ;
//////////////////////////////////////////////////////////////

function initVariables()
{
    jsTxtZipCode = document.getElementById(jsTxtZipCodeID);
    jsDdInsuranceType = document.getElementById(jsDdInsuranceTypeID);    
  
    jsTxtZipCodeAgent = document.getElementById(jsTxtZipCodeAgentID);
    jsTxtAddressAgent = document.getElementById(jsTxtAddressAgentID);
    jsTxtCityAgent = document.getElementById(jsTxtCityAgentID);
    jsDdStateAgent = document.getElementById(jsDdStateAgentID);       
    
    invalidZipError = document.getElementById(lblErrInvalidZipID);
    
    zipQuoteErrorOn = document.getElementById(zipQuoteErrorOnID);
    zipAgentErrorOn = document.getElementById(zipAgentErrorOnID);
    addressErrorOn = document.getElementById(addressErrorOnID);
    cityErrorOn = document.getElementById(cityErrorOnID);
        
}

function initVariablesSQ()
{
    jsTxtZipCodeSQ = document.getElementById(jsTxtZipCodeSQID);
    jsDdInsuranceTypeSQ = document.getElementById(jsDdInsuranceTypeSQID);
    
    invalidZipErrorSQ = document.getElementById(lblErrInvalidZipSQID);
    
    zipQuoteErrorOnSQ = document.getElementById(zipQuoteErrorOnSQID);
}

function showHideTextPrompt(textBox, prompt, error)
{
    if (textBox.value == prompt || textBox.value == error)
	{
	    if (textBox.value == error)
	    {
	        Sys.UI.DomElement.removeCssClass(textBox, 'error');	        
	    }
	    textBox.style.color='black';
	    textBox.value = "";
	}
	else if (textBox.value == "")
	{
	    textBox.value = prompt;	    
	}
}

function errorCheck()
{
    if (zipQuoteErrorOn.value == 'yes')
    {
        showZipQuoteErrorOn();
    }
    else
    {
        showZipQuoteErrorOff();
    }
    
    if (zipAgentErrorOn.value == 'yes')
    {
        showZipAgentErrorOn();
    }
    else
    {
        showZipAgentErrorOff();
    }
    
    if (cityErrorOn.value == 'yes')
    {
        showCityErrorOn();
    }
    else
    {
        showCityErrorOff();
    }
    
    if (addressErrorOn.value == 'yes')
    {
        showAddressErrorOn();
    }
    else
    {
        showAddressErrorOff();
    }
}

function errorCheckSQ()
{
    if (zipQuoteErrorOnSQ.value == 'yes')
    {
        showZipQuoteErrorOnSQ();
    }
    else
    {
        showZipQuoteErrorOffSQ();
    }
}

function setupSelectStartQuoteInsuranceType()
{
    selectRestyle(jsDdInsuranceType);
    ddInsurReplacement = jsDdInsuranceType.previousSibling;    
    insurPrompt = jsDdInsuranceType.parentNode.previousSibling.previousSibling;
    insurancePromptOn(jsTxtZipCode, ddInsurReplacement, insurPrompt)

}

function setupSelectStartQuoteInsuranceTypeSQ()
{
    selectRestyle(jsDdInsuranceTypeSQ);
    ddInsurReplacementSQ = jsDdInsuranceTypeSQ.previousSibling;
    insurPromptSQ = jsDdInsuranceTypeSQ.parentNode.previousSibling.previousSibling;
    insurancePromptOn(jsTxtZipCodeSQ, ddInsurReplacementSQ, insurPromptSQ);
}

function insurancePromptOn(txtZip, ddSelectReplaced, prompt)
{
    if (txtZip.value == 'Enter ZIP Code' || txtZip.value == '' || txtZip.value ==  'Enter A Valid Zip Code')
    {
        prompt.className = 'promptInsur';   
        ddSelectReplaced.style.display = 'none';
        return true;
    }
    else
    {
        prompt.className = 'hidePrompt';     
        ddSelectReplaced.style.display = 'block'; 
        return false;       
    }
}

function setupFindAgentAddress()
{
    if (jsTxtZipCodeAgent.value == 'Enter ZIP Code' || 
        jsTxtZipCodeAgent.value == 'Please enter a valid ZIP Code' ||
        jsTxtZipCodeAgent.value == '')
    {
        jsTxtAddressAgent.className = 'smallDisabled';
        jsTxtAddressAgent.disabled = true;
    }
    else
    {
        jsTxtAddressAgent.className = 'small';
        jsTxtAddressAgent.disabled = false;
    }
}

function setupSelectFindAgentState()
{
    selectRestyle(jsDdStateAgent);
    ddStateReplacement = jsDdStateAgent.previousSibling;
    statePrompt = jsDdStateAgent.parentNode.previousSibling.previousSibling; 
    statePromptOn();
}

function statePromptOn()
{
    if (jsTxtCityAgent.value == 'City' || jsTxtCityAgent.value == '')
    {
        statePrompt.className = 'promptState';     
        ddStateReplacement.style.display = 'none';
        return true;        	    
	}
	else
	{
	    statePrompt.className = 'hidePrompt';     
        ddStateReplacement.style.display = 'block';
        return false;	    
	}
}

function validateTxtZipCode(txtZipCode)
{     
    if (!insurancePromptOn(jsTxtZipCode, ddInsurReplacement, insurPrompt)){                     
        if (!txtZipCode.value.match(zipPattern))
        {
            // zip must be 5 numbers between 0-9        
            disableSelectRestyle(ddInsurReplacement, jsDdInsuranceType);
            jsDdInsuranceType.disabled = true; 
        }
        else
        {
            // zip pattern satisfied enable insurance dropdown                    
            enableSelectRestyle(ddInsurReplacement, jsDdInsuranceType);
            jsDdInsuranceType.disabled = false;
        }
    }
	
}

function validateZipCode(e)
{
   var pattern = /[^\d]/g;
   var zipCode;

    if (e.keyCode != 0)
    {
        zipCode = e.keyCode;
    }
    else if (e.charCode != 0)
    {
       zipCode = e.charCode;
    }

    if (zipCode == 8)
    {
       return true;
    } 
   
    return /^(\d)$/.test(String.fromCharCode(zipCode) );
}

function validateTxtZipCodeSQ(txtZipCode)
{     
    if (!insurancePromptOn(jsTxtZipCodeSQ, ddInsurReplacementSQ, insurPromptSQ)){                     
        if (!txtZipCode.value.match(zipPattern))
        {
            // zip must be 5 numbers between 0-9       
            disableSelectRestyle(ddInsurReplacementSQ, jsDdInsuranceTypeSQ);
            jsDdInsuranceTypeSQ.disabled = true;           
        }
        else
        {
            // zip pattern satisfied enable insurance dropdown                    
            enableSelectRestyle(ddInsurReplacementSQ, jsDdInsuranceTypeSQ);
            jsDdInsuranceTypeSQ.disabled = false;
        }
    }
	
}

function validateTxtZipCodeAgent(txtZipCode)
{              
    if (!txtZipCode.value.match(zipPattern))
    {
        // zip must be 5 numbers between 0-9
        jsTxtAddressAgent.className = 'smallDisabled';
        jsTxtAddressAgent.disabled = true;          
    }
    else
    {
        // zip pattern satisfied enable insurance dropdown                    
        jsTxtAddressAgent.className = 'small';
        jsTxtAddressAgent.disabled = false;   
    }	
}


function validateCityAgent(txtCityAgent)
{   
    if (!statePromptOn()){                  
        if (txtCityAgent.value == "City" || txtCityAgent.value == "" ||
         txtCityAgent.value.match(cityPattern) || txtCityAgent.value == "Specified City was not found")
        {	   
            disableSelectRestyle(ddStateReplacement, jsDdStateAgent); 
            jsDdStateAgent.disabled = true;
        }
        else
        {	    
            enableSelectRestyle(ddStateReplacement, jsDdStateAgent); 
            jsDdStateAgent.disabled = false;
        }
    }
}

function showZipQuoteErrorOn()
{ 
    invalidZipError.className = 'zipError';
}

function showZipQuoteErrorOff()
{
    invalidZipError.className = 'zipErrorHide';
}

function showZipAgentErrorOn()
{ 
    jsTxtZipCodeAgent.className = 'error';
}

function showZipAgentErrorOff()
{
    jsTxtZipCodeAgent.className = '';
}

function showCityErrorOn()
{ 
    jsTxtCityAgent.className = 'error';
    jsTxtCityAgent.value = 'Specified City was not found';
}

function showCityErrorOff()
{
    Sys.UI.DomElement.removeCssClass(jsTxtCityAgent, 'error');
}

function showAddressErrorOn()
{ 
    jsTxtAddressAgent.className += ' error';
    jsTxtAddressAgent.value = 'Unknown Address';
}

function showAddressErrorOff()
{
    Sys.UI.DomElement.removeCssClass(jsTxtAddressAgent, 'error');    
}

function showZipQuoteErrorOnSQ()
{ 
    invalidZipErrorSQ.className = 'zipError';
}

function showZipQuoteErrorOffSQ()
{
    invalidZipErrorSQ.className = 'zipErrorHide';
}

function StartAQuoteValidation(txtZip, error)
{    
    if (txtZip.value.match(zipPattern))
    {  
        error.className = 'zipErrorHide';
        if (zipQuoteErrorOn != null)
        {
            zipQuoteErrorOn.value = 'no';
        }
        if (zipQuoteErrorOnSQ != null)
        {
            zipQuoteErrorOnSQ.value = 'no';
        }
        return true;
    }
    else
    {
        error.className = 'zipError';
        return false;
    }
    
}

function FindAnAgentValidationZipStreet(txtZipAgent)
{         	
    // zip isn't 5 didgits, display error
    if (!txtZipAgent.value.match(zipPattern))
    {
        txtZipAgent.value = 'Please enter a valid ZIP Code';
        if (!Sys.UI.DomElement.containsCssClass(txtZipAgent, 'error'))
            txtZipAgent.className = 'error';
        return false;	        
    }
    zipAgentErrorOn.value = 'no'
    return true;   
}

function FindAnAgentValidationCityState(txtCityAgent)
{        
    return (txtCityAgent.value != "" && txtCityAgent.value != "City" && !txtCityAgent.value.match(cityPattern));    
}

function Showbox(DivID,DivState)
{
	if (document.getElementById){
		document.getElementById(DivID).style.display = DivState;
	}
	else {
	document[DivID].style.display = DivState;
	}
}

function setOpacity(elem, amount)
{
    elem.style.opacity = amount;
    elem.style.filter  = "alpha(opacity=" + (amount*100) + ")";     
}
//A New change required for Online Marketing Enhancements.
// New method added to read cookies
//Included new boolean parameter to validate whether the URL has to open in new or the same window.
function readCookie(strURL,strBool)
{   
    var aetURL = "";
    //If query string is present in the URL just passing them to AET applciation without checking for cookie.
    var strURLQuery = isQueryStringAvailable();
    if(strURLQuery!="")
    {   
       aetURL = strURL + strURLQuery;
    }
    else
    {
         //If query string is not present in the URL checking for cookie.
        var search = "AllTracker" + "="   
        var splitter ='&';
        var cookiestr = '';
        
        if (document.cookie.length > 0)
        { 
            //check for AllTracker Cookie
            var offset = document.cookie.indexOf(search)       
            if (offset != -1)
            {
                // if cookie exists          
                offset += search.length          
                // set index of beginning of value 
                var end = document.cookie.indexOf(";", offset)          

                // set index of end of cookie value         
                if (end == -1)
                {             
                    end = document.cookie.length
                }
                //getting the full keys and values of the cookie
                var str = unescape(document.cookie.substring(offset, end));
                var arInfo =[5];
                //splitting the kyes and values       
                arInfo = str.split(splitter);
                   
                for (var i = 0; i <arInfo.length; i++)
                {                        
                   if(arInfo[i].toUpperCase().startsWith("QUOTE")
                   ||arInfo[i].toUpperCase().startsWith("TFN")
                   ||arInfo[i].toUpperCase().startsWith("CAMPAIGN"))
                   {
                       if(cookiestr.length>0)
                       {
                          cookiestr+="&";
                       }
                      cookiestr += arInfo[i];
                   }                                                  
                }
                //appending cookie parameters to the AET URL.
                aetURL = strURL + "?" + cookiestr;
            }              
        }
    }
    //if aetURL is empty, then there was no cookie or ad banner params in the querystring of the URL.
    //redirect to AET without a querystring
    if (aetURL == "")
    {
        aetURL = strURL;
    }  
    
    //checking for boolean parameter to validate whether the URL has to open in new or the same window.
    if(strBool)
    {
        window.open(aetURL);
    }
    else
    {
        location.href = aetURL;
    }  
}
//A new change made for Online Marketign Enhancements project.
//added new method to read the URL , to check for the query string. 
function isQueryStringAvailable()
{   
    var strParams;
    var strApplicationURL = window.location.href;

    var indexstart = strApplicationURL.indexOf('?');
    var strQuerystring = "";
    var splitter = '&';
    var arrList = [5];
    //If indexstart does not equal -1, that means there's a querystring in the URL
    if (indexstart != -1)
    {   
        strParams = strApplicationURL.substring(indexstart + 1);
        
        //if the length of the params is less than 0, then there was nothing after the ? in the querystring
        if (strParams.length > 0)
        {
            //check for index of CID.  This is a unique identifier for ad banner metrix we need to capture and pass to AET
            if(strParams.indexOf('cid=') != -1 || strParams.indexOf('CID=') != -1)
            {
                arrList = strParams.split(splitter);
                for (var j = 0; j <arrList.length; j++)
                {  
                    //Metrix that need to be captured from the querystring and passed to AET
                    if(arrList[j].toUpperCase().startsWith("QUOTE")
                    ||arrList[j].toUpperCase().startsWith("TFN")
                    ||arrList[j].toUpperCase().startsWith("CAMPAIGN"))
                    {
                        if(strQuerystring.length > 0)
                        {      
                            strQuerystring += "&" + arrList[j];
                        }
                        else
                        {
                            strQuerystring = "?" + arrList[j];
                        }
                    }                 
                }
            }
        }        
    }

    //will return querystring params that associate with ad banner campaigns
    //if return is an empty string, it will search for cookies
    return strQuerystring;
}
//New check introduced for HBX piece of change 
//Passing the values of cid, att values.
function initHbxValues(strCID,strATT)
{
 try
    {   
        var hbxValue = strCID + '|' + strATT;
       //we are using custom metric 23 which is greater than 10, we have to use _hbSend in conjunction with _hbSet
       _hbSet('cv.c23',hbxValue);
       _hbSend();
                        
    }
 catch (err) {}   
 
}

