function ValidateClick() {

  var zip = document.getElementById('zip').value;
  var refby = document.getElementById('refby').value;
  var under65 = document.getElementById("over65f").checked;
  var over65 = document.getElementById("over65t").checked;  
  var formAction = document.getElementById("qgForm").action;
  var siteURL = "https://www.healthinsurancesort.com/app/";
  var currentState = zipToState(zip);
  
  var pageUrl = document.URL;
  pageUrl = pageUrl.split("/");
  pageUrl = pageUrl[pageUrl.length-1].split("?");
  pageUrl = pageUrl[0];
  
  formAction = document.getElementById("qgForm").action;
  
  //formAction = "http://www.healthinsurancesort.com/results.html";
  
	if (pageUrl == "ehealthinsurance.htm"){
		siteURL += "-onepager.php?refby=" + refby + "&zip="+ zip;
	} else if (over65 && (zipToState(zip) == "IL" || zipToState(zip) == "NC" || zipToState(zip) == "IA" || zipToState(zip) == "LA" || zipToState(zip) == "WY" || zipToState(zip) == "IN" || zipToState(zip) == "NE" || zipToState(zip) == "VA" || zipToState(zip) == "UT" || zipToState(zip) == "AZ" || zipToState(zip) == "KY")){
		siteURL += "ms-medicare.php?refby=" + refby + "&zip="+ zip;
		formAction = "http://www.healthinsurancesort.com/results-medicare.html";
	}else if (under65 && (pageUrl == "individual-health-plansAQ2.htm" || pageUrl == "low-cost-health-insurance-v2.htm")){
		siteURL += "leadworks-regular-two-cols-v2.php?refby=" + refby + "&zip="+ zip;
	}else if(under65 && (zipToState(zip) == "NY" || zipToState(zip) == "NJ" || zipToState(zip) == "KS")){
		siteURL += "onepager.php?noredir&refby=" + refby + "&zip="+ zip;
	}else if(under65 && ((zip > 7000 && zip < 9000) || (zip > 99500 && zip < 99951) || zipToState(zip) == "DC" || zipToState(zip) == "MA" || excludedStates(currentState, zip, "leadworks"))){
		siteURL += "onepager.php?noredir&refby=" + refby + "&zip="+ zip;
	}else if(under65 && pageUrl === "individual-health-plans-vm.htm"){
		siteURL += "vimo-regular-two-cols.php?refby=" + refby + "&zip="+ zip;
	}else if(under65){
		siteURL += "leadworks-regular-two-cols.php?refby=" + refby + "&zip="+ zip;
	}else{
		siteURL += "leadworks-medicare.php?zip=" + zip;
		formAction = "http://www.healthinsurancesort.com/results-medicare.html";
	}

  document.getElementById("qgForm").action = formAction;
  
  openWindowWithPost (siteURL,null);
  setTimeout ("setdocument.getElementById('form_IHP').submit();",3000);
  this.focus();
  return true;
}

function openWindowWithPost(url,name) {
  var params="";if (navigator.appName!="Netscape"){params="newwindow=yes,toolbars=1,menubar=1,resizable=1,fullscreen=yes,scrollbars=1";}
  var newWindow = window.open(url, name,params);
  
  //if (!newWindow) return false;
  var html = "";
  html += "<html><head></head><body><form id='formid' name='formenv' method='post' action='" + url + "'>";
  if (keys && values && (keys.length == values.length))
  for (var i=0; i < keys.length; i++){
  html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";}
  html += "</form><scr";
  html += "ipt type=\'text/javascript\'>document.getElementById(\"formid\").submit()</scr";
  html += "ipt></body></html>";
  newWindow.document.write(html);
  
  return newWindow;
  if (!newWindow) return false;
}

function openW(url){
	window.open(url, "Customer Pledge", 'width=580,height=450,resizable=yes,scrollbars=yes');
	return false;
}

// rename this to something more descriptive: for icd it gives states that are 
// excluded
function excludedStates(currentState, zip, provider){
  if (provider == "icd" ){
    var states = ["WA", "OR", "MT", "ND", "ME", "RI", "CT", "MD", "KS", "NH", "VT"];
  }else if (provider == "leadworks" ){
    var states = ["NJ", "MN", "MN", "KS", "DC", "AK", "WA"];
  }
  
  return contains(states, currentState);  
}


function icdPages(page){
  var pages = ["catastrophic-health-plans.htm", 
              "maternity.htm", 
              "pre-existing-conditions.htm",
              "diabetics-health-insurance.htm",
              "pregnancy-health-insurance.htm",
              "pre-existing-conditions.htm",
              "cancer.htm",
              "multiple-sclerosis.htm",
              "obesity.htm",
              "smokers.htm"];

  return contains(pages, page);
}

function contains(a, obj) {
  var i = a.length;
  while (i--) {
    if (a[i] === obj) {
      return true;
    }
  }
  return false;
}


// order this by most visited states
function zipToState(zip) {
  var state;
	if (zip >= '99501' && zip <= '99950')  { state = 'AK'; }
	else if (zip >= '35004' && zip <= '36925')  { state = 'AL'; }
	else if (zip >= '71601' && zip <= '72959')  { state = 'AR'; }
	else if (zip >= '75502' && zip <= '75502')  { state = 'AR'; }
	else if (zip >= '85001' && zip <= '86556')  { state = 'AZ'; }
	else if (zip >= '90001' && zip <= '96162')  { state = 'CA'; }
	else if (zip >= '80001' && zip <= '81658')  { state = 'CO'; }
	else if (zip >= '06001' && zip <= '06389')  { state = 'CT'; }
	else if (zip >= '06401' && zip <= '06928')  { state = 'CT'; }
	else if (zip >= '20001' && zip <= '20039')  { state = 'DC'; }
	else if (zip >= '20101' && zip <= '20199')  { state = 'VA'; }
	else if (zip >= '20042' && zip <= '20599')  { state = 'DC'; }
	else if (zip >= '20799' && zip <= '20799')  { state = 'DC'; }
	else if (zip >= '19701' && zip <= '19980')  { state = 'DE'; }
	else if (zip >= '32004' && zip <= '34997')  { state = 'FL'; }
	else if (zip >= '30001' && zip <= '31999')  { state = 'GA'; }
	else if (zip >= '39901' && zip <= '39901')  { state = 'GA'; }
	else if (zip >= '96701' && zip <= '96898')  { state = 'HI'; }
	else if (zip >= '50001' && zip <= '52809')  { state = 'IA'; }
	else if (zip >= '68119' && zip <= '68120')  { state = 'IA'; }
	else if (zip >= '83201' && zip <= '83876')  { state = 'ID'; }
	else if (zip >= '60001' && zip <= '62999')  { state = 'IL'; }
	else if (zip >= '46001' && zip <= '47997')  { state = 'IN'; }
	else if (zip >= '66002' && zip <= '67954')  { state = 'KS'; }
	else if (zip >= '40003' && zip <= '42788')  { state = 'KY'; }
	else if (zip >= '70001' && zip <= '71232')  { state = 'LA'; }
	else if (zip >= '71234' && zip <= '71497')  { state = 'LA'; }
	else if (zip >= '01001' && zip <= '02791')  { state = 'MA'; }
	else if (zip >= '05501' && zip <= '05544')  { state = 'MA'; }
	else if (zip >= '20331' && zip <= '20331')  { state = 'MD'; }
	else if (zip >= '20335' && zip <= '20797')  { state = 'MD'; }
	else if (zip >= '20812' && zip <= '21930')  { state = 'MD'; }
	else if (zip >= '03901' && zip <= '04992')  { state = 'ME'; }
	else if (zip >= '48001' && zip <= '49971')  { state = 'MI'; }
	else if (zip >= '55001' && zip <= '56763')  { state = 'MN'; }
	else if (zip >= '63001' && zip <= '65899')  { state = 'MO'; }
	else if (zip >= '38601' && zip <= '39776')  { state = 'MS'; }
	else if (zip >= '71233' && zip <= '71233')  { state = 'MS'; }
	else if (zip >= '59001' && zip <= '59937')  { state = 'MT'; }
	else if (zip >= '27006' && zip <= '28909')  { state = 'NC'; }
	else if (zip >= '58001' && zip <= '58856')  { state = 'ND'; }
	else if (zip >= '68001' && zip <= '68118')  { state = 'NE'; }
	else if (zip >= '68122' && zip <= '69367')  { state = 'NE'; }
	else if (zip >= '03031' && zip <= '03897')  { state = 'NH'; }
	else if (zip >= '07001' && zip <= '08989')  { state = 'NJ'; }
	else if (zip >= '87001' && zip <= '88441')  { state = 'NM'; }
	else if (zip >= '88901' && zip <= '89883')  { state = 'NV'; }
	else if (zip >= '06390' && zip <= '06390')  { state = 'NY'; }
	else if (zip >= '10001' && zip <= '14975')  { state = 'NY'; }
	else if (zip >= '43001' && zip <= '45999')  { state = 'OH'; }
	else if (zip >= '73001' && zip <= '73199')  { state = 'OK'; }
	else if (zip >= '73401' && zip <= '74966')  { state = 'OK'; }
	else if (zip >= '97001' && zip <= '97920')  { state = 'OR'; }
	else if (zip >= '15001' && zip <= '19640')  { state = 'PA'; }
	else if (zip >= '02801' && zip <= '02940')  { state = 'RI'; }
	else if (zip >= '29001' && zip <= '29948')  { state = 'SC'; }
	else if (zip >= '57001' && zip <= '57799')  { state = 'SD'; }
	else if (zip >= '37010' && zip <= '38589')  { state = 'TN'; }
	else if (zip >= '73301' && zip <= '73301')  { state = 'TX'; }
	else if (zip >= '75001' && zip <= '75501')  { state = 'TX'; }
	else if (zip >= '75503' && zip <= '79999')  { state = 'TX'; }
	else if (zip >= '88510' && zip <= '88589')  { state = 'TX'; }
	else if (zip >= '84001' && zip <= '84784')  { state = 'UT'; }
	else if (zip >= '20101' && zip <= '20199')  { state = 'VA'; }
	else if (zip >= '20040' && zip <= '20041')  { state = 'VA'; }
	else if (zip >= '20040' && zip <= '20167')  { state = 'VA'; }
	else if (zip >= '20042' && zip <= '20042')  { state = 'VA'; }
	else if (zip >= '22001' && zip <= '24658')  { state = 'VA'; }
	else if (zip >= '05001' && zip <= '05495')  { state = 'VT'; }
	else if (zip >= '05601' && zip <= '05907')  { state = 'VT'; }
	else if (zip >= '98001' && zip <= '99403')  { state = 'WA'; }
	else if (zip >= '53001' && zip <= '54990')  { state = 'WI'; }
	else if (zip >= '24701' && zip <= '26886')  { state = 'WV'; }
	else if (zip >= '82001' && zip <= '83128')  { state = 'WY'; }
	return state;
}

function f(){
  document.getElementById("slideshow").visibility='visible';	
}

