// JavaScript Document

$(document).ready(function () {
		var userZIP = getUrlVars()["zipcode"];
		var checkzipcode2 = false; 
		if (userZIP){	// Check for parameter and launch next step if it exists
			if(userZIP >= '02801' && userZIP <= '02940') {
				checkzipcode2 = true;
			} else if(userZIP >= '03901' && userZIP <= '04992') {
				checkzipcode2 = true;
			} else if(userZIP >= '05001' && userZIP <= '05907') {
				checkzipcode2 = true;
			}
			
			if(checkzipcode2) {
				confirm('We currently do not offer Individual and Family Health Insurance in your state!');
				$("#diShowZip").show('blind', 1000, function(){
					$('#idTxtZipCode').val(userZIP);
					document.getElementById('idTxtZipCode').select();
				});	
				//document.getElementById('idTxtZipCode').select();
				return false;
			} else {
				$('#idTxtZipCode').val(userZIP);
				if ($('#contentform').is('.received')) { // content requested via "previous" link
					//alert('already received');
				} else {  // content requested via site root - auto submit
					//alert('first timer');
					//if(userZIP!='') {
						validateZip();
					//}
				}
			}
		}
		
		$("form#zipFrm").submit(function(){
			event.preventDefault();
			alert('stop');
		});
		$("#buttZipCode").click(function(){
			var txtZ = $("#idTxtZipCode").val();
			var checkzipcode2 = false;
			if(txtZ!='') {
				if(txtZ >= '02801' && txtZ <= '02940') {
					checkzipcode2 = true;
				} else if(txtZ >= '03901' && txtZ <= '04992') {
					checkzipcode2 = true;
				} else if(txtZ >= '05001' && txtZ <= '05907') {
					checkzipcode2 = true;
				}
				
				if(checkzipcode2) {
					confirm('We currently do not offer Individual and Family Health Insurance in your state!');
					$("#diShowZip").show('blind', 1000, function(){
						$('#idTxtZipCode').val(txtZ);
						document.getElementById('idTxtZipCode').select();
					});	
					//document.getElementById('idTxtZipCode').select();
					return false;
				} else {
					validateZip();
					//return false;
				}
			}
		});
		
		$("#diShowZip").show('blind', 1000, function(){ 
			$('#idTxtZipCode').focus();
		});	
	});

function validateZip(){
	var txtZ = $("#idTxtZipCode").val();
		
	if(isNaN(txtZ) || txtZ == ''){							
		confirm('Invalid Zip Code, Zip Code must be number');				
		$("#diShowZip").show('blind', 1000, function(){
			$('#idTxtZipCode').focus();
		});				
		return false;
	}
	$('#contentform').addClass('received');
	$("#diShowZip").hide('blind', 1000, function() {
		getZipNew(txtZ);
		//return false;
	})
}
/*
 * Lee - 04202009
 *
 * Checck zip 
 */

function checkzipcode() {
	var zipcode = document.getElementById("idTxtZipCode").value;
	///document.getElementById("zip123").value = zipcode;
	var zipcode_form = document.getElementById("zipFrm");
	var checkzipcode = false;
	var checkzipcode2 = false;
	//alert(zipcode);
	if (zipcode >= '01001' && zipcode <= '02791') {
		checkzipcode = true;
	} else if(zipcode >= '03031' && zipcode <= '03897') {
		checkzipcode = true;
	} else if(zipcode >= '07001' && zipcode <= '08989') {
		checkzipcode = true;
	} else if(zipcode >= '10001' && zipcode <= '14925') {
		checkzipcode = true;
	} else if(zipcode >= '57001' && zipcode <= '57799') {
		checkzipcode = true;
	} else if(zipcode >= '58001' && zipcode <= '58856') {
		checkzipcode = true;
	} else if(zipcode >= '59001' && zipcode <= '59937') {
		checkzipcode = true;
	} else if(zipcode >= '82001' && zipcode <= '83128') {
		checkzipcode = true;
	} else if(zipcode >= '96701' && zipcode <= '96898') {
		checkzipcode = true;
	} else if(zipcode >= '98001' && zipcode <= '99403') {
		checkzipcode = true;
	} else if(zipcode >= '99501' && zipcode <= '99950') {
		checkzipcode = true;
	} else if(zipcode >= '02801' && zipcode <= '02940') {
		checkzipcode2 = true;
	} else if(zipcode >= '03901' && zipcode <= '04992') {
		checkzipcode2 = true;
	} else if(zipcode >= '05001' && zipcode <= '05907') {
		checkzipcode2 = true;
	} 
	
	if (checkzipcode) {
		zipcode_form.action = 'https://www.ehealthinsurance.com/ehi/Alliance?allid=Hea23610&type=IFP&zip=' + zipcode;
		zipcode_form.submit();
		//return true;
	} else if(checkzipcode2) {
		confirm('We currently do not offer Individual and Family Health Insurance in your state!');
		$("#diShowZip").show('blind', 1000, function(){
			$('#idTxtZipCode').val(zipcode);
			document.getElementById('idTxtZipCode').select();
		});	
		//document.getElementById('idTxtZipCode').select();
		return false;
	} else {
		//zipcode_form.action = 'http://www.healthcareshopper.com/webform';
		zipcode_form.submit();
		//return true;
	}
	
}


function getZipNew(zipcode){
	$.ajax({
	  type: "POST",
	  url: "index.php?action=logon&zipcode="+zipcode,
	  success: function(msg){
		if (msg.indexOf(":#:FAILURE")>-1){
			$("#diShowZip").show('blind', 1000);
			confirm('Sorry, we do not offer coverage for your area');					
		} else {
			$("#contentform").html(msg);
		};
	  }
	});
}

/* parse parameters from url */
function getUrlVars(){
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++){
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}