/*
9/14

*/
function getQueryStringVariable(variable)
{

        var var_array = [];

	//get the CD number from the query
	var query = window.location.search.substring(1);
        var vars = query.split('&');
        for (var i=0;i<vars.length;i++) {
	    var pair = vars[i].split('=');
            if (pair[0] == variable) {
               return pair[1];
            }
        }
}


function OpenNewForm(theForm)
{

  var strURL = "";
  var insured = "";
  var source = "";
  
  //get CD from URL if available
  source=getQueryStringVariable('source');
  if (!(source) || source.length < 3){
  	source="CD198";
  }

  subid3 = getQueryStringVariable('subid3');
  subid4 = getQueryStringVariable('subid4');
  subid5 = getQueryStringVariable('subid5');


  if (document.getElementById('insurancetype').value == "auto")
  {

        if (document.getElementById('yes').checked)
        { 
           insured='Y';
        }else{
           insured='N';
        }
         
        if (document.getElementById('yes').checked) {
          
          strURL="http://www.insurance.com/auto.aspx?cp=2354&zip=";
          strURL=strURL + document.getElementById('zipcode').value;
          strURL=strURL + "&insured=Y&customersource=" + source;
          strURL=strURL + "&customerprovidersessionid=" + subid3;
        }else{
          
          strURL="http://www.insurance.com/auto.aspx?cp=2383&zip=";
          strURL=strURL + document.getElementById('zipcode').value;
          strURL=strURL + "&insured=Y&customersource=" + source;
          strURL=strURL + "&customerprovidersessionid=" + subid3;
   
        }


  }

  if (document.getElementById('insurancetype').value == "life")
  {
	
 	strURL="http://www.termfinder.com/smartquotetfc.aspx?source=599-";
        strURL=strURL + source;
 	
	if (document.getElementById('gender')) {
	  strURL=strURL + "&gender=" + document.getElementById('gender').value;
	  strURL=strURL + "&height=" + document.getElementById('height').value;
	  strURL=strURL + "&weight=" + document.getElementById('weight').value;
        }
	strURL=strURL + "&zipcode=" + document.getElementById('zipcode').value;
  }

  window.open(strURL,null,"fullscreen=yes, toolbars=yes, scrollbars=yes, menubar=yes, location=yes");
 
   
}


function redirect(theForm) 
{

          
	var strErr = "";

	if(document.getElementById('zipcode').value=="")

	{

		strErr = strErr + "Please enter zip code.\n";

		theForm.Zip.focus();

	}
	if(document.getElementById('insurancetype').value =="")
	{
		strErr = strErr + " Please select insurance type.";
	}
      
     if (strErr != "")

     {

           alert(strErr);

           return false;

     }

     else

    {
        var strURL = ""; 
	var insured = "";
	
	if (document.getElementById('yes') && document.getElementById('yes').checked)
	{ insured='Y';
	}else{
	  insured='N';
	}

        var source=getQueryStringVariable('source');
        var subid3 = getQueryStringVariable('subid3');

		strURL = "results.html";

	        strURL = strURL + "?zipcode=" + document.getElementById('zipcode').value;
	        strURL = strURL + "&insured=" + insured;
	        strURL = strURL + "&type=" + document.getElementById('insurancetype').value;
		strURL = strURL + "&source=" + source + "_" + subid3;

	window.location=strURL;	
     }

}

function ValidateForm(theForm)
 
{
	var strErr = "";
     
	if(theForm.insurancetype.value=="")
	{
		strErr = strErr + "Please select insurance type.\n";

	}
 
	if(theForm.zipcode.value=="")
	{
 		strErr = strErr + "Please enter zip code.\n";
 		theForm.zip.focus();
 	}
	else
        {        
          if(theForm.zipcode.value.length!=5)
          {
      		strErr = strErr + "Please enter valid zip code.\n";
	  }
        }

       if (strErr != "")
       {
           alert(strErr);
           return false;
       }
       else
       {
	   OpenNewForm(theForm);
       }


       return true;
}	
