// JavaScript Document
// for whenDoICovert

var xmlhttp;

function GetXmlHttpObject() {
	if(window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if(window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function getStatesAJAX() {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Your browser does not support XMLHTTP!");
		return;
	}
	xmlhttp.onreadystatechange=showStates;
	xmlhttp.open("GET","getStates.jsp",true);
	xmlhttp.send(null);
}

function getCountiesAJAX(selState) {
	if(selState=="") {
		document.getElementById("countyOptions").innerHTML="<select id=\"county\" name=\"county\" size=\"1\"><option value=\"\">Select One</option></select>";
		return;
	}	
	xmlhttp=GetXmlHttpObject();
	if(xmlhttp==null) {
		alert ("Your browser does not support XMLHTTP!");
		return;
	}
	var url="getCounties.jsp";
	url=url+"?state="+selState;
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
	var innerTextCounty = "<select id=\"county\" name=\"county\" size=\"1\">";
	innerTextCounty += "<option value=\"\">Select One</option>";
	innerTextCounty += xmlhttp.responseText;
	innerTextCounty += "</select>";
	document.getElementById("countyOptions").innerHTML=innerTextCounty;

}

function showStates() {
	if(xmlhttp.readyState==4) {
		var innerText = "<select id=\"state\" name=\"state\" size=\"1\" onchange=\"getCountiesAJAX(document.WDIC.state.value);\">";
		innerText += "<option value=\"\">Select One</option>";
		innerText += xmlhttp.responseText;
		innerText += "</select>";
		document.getElementById("stateOptions").innerHTML=innerText;
	}
}

function showCountyies() {
	if(xmlhttp.readyState==4) {
		var innerTextCounty = "<select id=\"county\" name=\"county\" size=\"1\">";
		innerTextCounty += "<option value=\"\">Select One</option>";
		innerTextCounty += xmlhttp.responseText;
		innerTextCounty += "</select>";
		document.getElementById("countyOptions").innerHTML=innerTextCounty;
	}
}

// validate the WDIC form
function validateStateAndCounty(f){
	if(f.state.value == '' || f.county.value == '') {
		alert('Please select state and county');
		return false;
	}
	setCookieValue('WDICState',f.state.value,'1000');
	setCookieValue('WDICCounty',f.county.value,'1000');
	return true;
}	
function validateZipcode(f){
	if(f.zipcode.value == '' || f.zipcode.value.length != 5) {
		alert('Please enter valid zipcode');
		return false;
	}
	setCookieValue('WDICZipcode',f.zipcode.value,'1000');
	return true;
}	

function setCookieValue(cookieName,cookieValue,expiredays) {
	var expdate=new Date();
	expdate.setDate(expdate.getDate()+expiredays);
	document.cookie=cookieName+ "=" +escape(cookieValue)+
	((expiredays==null) ? "" : ";expires="+expdate.toGMTString());
}

function getCookieValue(cookieName)
{
	var cookieValue;
	if (document.cookie.length>0) {
		cookie_start=document.cookie.indexOf(cookieName + "=");
		if (cookie_start!=-1) {
			cookie_start=cookie_start + cookieName.length+1;
			cookie_end=document.cookie.indexOf(";",cookie_start);
			if (cookie_end==-1) cookie_end=document.cookie.length;
			cookieValue = unescape(document.cookie.substring(cookie_start,cookie_end));
			return cookieValue;
		}
	}

	return "";
}

function checkCookies(f)
{
	f.zipcode.value = getCookieValue('WDICZipcode');
}

