// JavaScript Document

//Variable that stores the form customers data
var cstData = "";

//Function to resize Iframe
function resizeIframe(frm){
	
	var insPlan = frm['hdnPlan'].value;
	var ieIfrmSizes = {'child':23500,'ifp':23500,'medicare':8700,'short':2650,'dental':6000};
	var ffSizeDiff = 300;
	var ifrm = parent.document.getElementById('ifrmPanel');
	var isIE = (navigator.userAgent.indexOf('MSIE') > 0)? true : false;
	
	if (ifrm){
		ifrm.height = (isIE == true)? ieIfrmSizes[insPlan]+220 : ieIfrmSizes[insPlan]+ffSizeDiff;
	}
	
}

//Function to check if a zip code was submitted from the home page
//then it is inserted into the plan form
function checkZip(){
	
	var zip = "";
	var ifrm = document.getElementById('ifrmPanel');
	
	//Get Zip from Url
	if(document.location['search']){ 
		zip = document.location['search'];
	}
	//Add Zip Param to Iframe Src
	if (ifrm && zip != "")
	{
		ifrm.src = ifrm.src+zip;		
	}
		
}

function onHomeFormKeyPress(e,form)
{
	if(e && e.which){ //if which property of event object is supported (NN4)
	e = e;
	characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
	e = event
	characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
	//document.forms[0].submit(); //submit the form
	onHomeFormSubmit(form);
	return false
	}
	else{
	return true
	}
}

//Function to submit show plans form on home page
function onHomeFormSubmit(form)
{
	var page = form['p'];
	var zip = form['zipCode'];
	
	for(i=0;i<page.length;i++)
	{
		try {
			if(page[i].checked)
			{
				page = page[i].value;
			}
		}
		catch(e)
		{
			
		}
	}
	
	if (zip.value != "")
	{
		//var strLoc = 'http://'+document.domain+'/'+page+'/?zipCode='+zip.value;
		var strLoc = 'http://'+document.domain+'/'+page+'/';
		//document.href = strLoc;
		form.action = strLoc;
		form.submit();
	}else{
		alert('Please Enter Your Zip Code');
		return false;
	}
}

//Function to switch forms and
//Carry Over Data between forms
function onTabClick(id){
	
	var win = window;
	var frmSrc = "http://"+document.domain+"/";
	var ckFrmCstData = Cookie.get('frmCstData');
	var frmData = getFormData();
	//Save Form Data to Cookie
	saveFormData(frmData);
	
	//Check if frmCstData Cookie has a value
	if(ckFrmCstData.getValue() != 'undefined' && ckFrmCstData.getValue().length > 0)
	{
		if(frmData == "")
		{
			frmData = ckFrmCstData.getValue();
		}
	}
	//Add ? if not empty
	if (frmData != ""){ frmData = "?"+frmData; /*Store Form Data in Local Var*/ cstData = frmData; }
	//Check if data is stored in local var
	if (frmData == "" && cstData != ""){ frmData = cstData; }
	
	if (win){
		switch(id){
			//Individual and Family Plan
			case 'ifp':
				win.location = frmSrc+"individual-family-insurance-quote/"+frmData;
			break;
			//Short Term
			case 'st':
				win.location = frmSrc+"short-term-insurance-quote/"+frmData;
			break;
			//Dental Plan
			case 'dp':
				win.location = frmSrc+"dental-plan-quote/"+frmData;
			break;
			//Child Only Plan
			case 'cop':
				win.location = frmSrc+"child-health-insurance-quote/"+frmData;
			break;
			//Medicare Plan
			case 'mp':
				win.location = frmSrc+"medicare-plan-quote/"+frmData;
			break;
			default:
			break;
		}
	}
}

//Function to save form data on unload event of the form
function saveFormData(rStr)
{
	//var frmFields = "applicantFirstName,applicantLastName,applicantEmail,applicantPhone,zipCode";
	//var ifrm = document.getElementById('ifrmPanel');
	var ckFrmCstData = Cookie.get('frmCstData');
	
	//alert(rStr);
	if(ckFrmCstData.getValue() == 'undefined')
	{
		//Create new cookie
		ckFrmCstData = new Cookie();
		ckFrmCstData.setName('frmCstData');
		if (rStr == "")
		{
			ckFrmCstData.setValue('');
		}else{
			ckFrmCstData.setValue(rStr);
		}
		ckFrmCstData.save();
	}
}

//Function to Grab Form Data
function getFormData(){
	
	var frmFields = "applicantFirstName,applicantLastName,applicantEmail,applicantPhone,zipCode";
	var ifrm = document.forms['form1'];
	var rStr = "";
	
	//if (ifrm = ifrm.contentWindow){
		
		frmFields = frmFields.split(",");
		fCount = frmFields.length;
		
		for (i=0;i < fCount;i++){
			
			var frmEleId = frmFields[i].toString();
			var frmEle;
			
			try {
				
				frmEle = ifrm[frmEleId];
				
				if (frmEle.value != "")
				{
					rStr = rStr+frmEleId+"="+frmEle.value+"&";
				}
			}
			catch(e)
			{
				rStr = rStr;
			}
		}
	//}
	
	//alert(rStr);
	return rStr;
	
}

//Cookie Class used to create and manage cookies
//From CodePost.org Posted by nir_tayeb
function Cookie(){
        var name, value, expires, path="/", domain, secure=false;
        this.setName = function(sName){ name=sName; }
        this.getName = function(){ return name; }
        this.getValue = function(){
            return decodeURI(
                (value instanceof Array)?
                    value.join("&"):
                    value
            );
        }

        this.setValue = function(sValue){ /*value=escape(sValue);*/ value=encodeURI(sValue); }
        this.add = function(key, val){
            if(!(value instanceof Array)){
                var temp = value;
                value = [];
                if(temp){
                    value[value.length] = temp;
                }
            }
            value[value.length] = key+"="+escape(val);
        }
        this.setExpires = function(date){ expires=date; }
        this.getExpires = function(){ return expires; }
        this.setDomain = function(sD){ domain=sD; }
        this.getDomain = function(){ return domain; }
        this.setSecure = function(scr){ secure = !!scr; }
        this.getSecure = function(){ return secure; }
        this.save = function(){
            if(name  && value){
                if(value instanceof Array){
                    // if the value is as array, we join the array
                    value=value.join("&");
                }
                document.cookie = name+'='+escape( value ) +
                    ( ( expires ) ? ';expires='+expires.toGMTString() : '' ) + //expires.toGMTString()
                    ( ( path ) ? ';path=' + path : '' ) +
                    ( ( domain ) ? ';domain=' + domain : '' ) +
                    ( ( secure ) ? ';secure' : '' );
            }
        }
        this.remove = function(){
            // The cookie removed if the expires date is before today
            expires = new Date(new Date().getTime()-1000*60*60*24);
            this.save();
        }
    };

    Cookie.get = function(name){
        /*
            the document.cookie return a string contains all the cookies available in the browser for the specific domain in format of: Name=Value;Name=Value;Name=Value
            this function take only the cookie with the requested name and return a Cookie object with it's value.
            if there isn't a cookie with the requested name, the function returns an empty cookie object with the requested name
        */
        var start = document.cookie.indexOf( name + "=" );
        var len = start + name.length + 1;
        var cookie = new Cookie();
        if ( !((( !start ) && ( name != document.cookie.substring( 0, name.length ) )) || (start==-1) ) ) {
            var end = document.cookie.indexOf( ';', len );
            if ( end == -1 ) end = document.cookie.length;
            cookie.setValue(unescape( document.cookie.substring( len, end ) ));
        }

        cookie.setName(name);
        return cookie;
    }

