var sVersion = "1";
function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function ReadCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}


function SetCook() {
	var f = document.horsesearch;
	var si = f.stateid.selectedIndex;
	var cval = sVersion + ";" + f.stateid.options[si].text 
			+ ":" + f.stateid.options[si].value + ";";
	cval += f.breeds.options[f.breeds.selectedIndex].text + ";";
	cval += f.minprice.value + ";";
	cval += f.maxprice.value + ";";
	cval += f.numdays.selectedIndex + ";";
	if ( f.typeSale.checked ) { cval += "1;" } else { cval += "0;" }
	if ( f.typeLease.checked ) { cval += "1;" } else { cval += "0;" }
	if ( f.typeStud.checked ) { cval += "1" } else { cval += "0" }
	SetCookie("HPFORM2", cval, 90);
}

var breedoptionid=1;
var stateoptionid=65;
var hpcookie = ReadCookie("HPFORM2");
var f = document.horsesearch;
if ( hpcookie != null && hpcookie.length > 3 ) {
	var hpclist = hpcookie.split(";");
	if ( hpclist[0] == sVersion ) {
		if ( hpclist.length == 9) {
			var stateind = hpclist[1];
			var breedind = hpclist[2];
			var p1 = hpclist[3];
			var p2 = hpclist[4];
			var pwithin = hpclist[5];
			var c1 = hpclist[6];
			var c2 = hpclist[7];
			var c3 = hpclist[8];

				var stateinfo = stateind.split(":");
				if (stateinfo.length == 2) {
					var statename = stateinfo[0];
					var stateid = stateinfo[1];
					if (stateid == parseInt(stateid)) {
						f.stateid.options[stateoptionid].value = stateid;
						f.stateid.options[stateoptionid].text = statename;
						f.stateid.selectedIndex=stateoptionid;
					}
				}


				f.breeds.options[breedoptionid].value=breedind;
				f.breeds.options[breedoptionid].text=breedind;
				f.breeds.selectedIndex=breedoptionid;


			f.minprice.value = p1;
			f.maxprice.value = p2;
			f.numdays.selectedIndex = pwithin;
			if ( c1 == 1 ) 
				f.typeSale.checked = true;
			else
				f.typeSale.checked = false;
			if ( c2 == 1 ) 
				f.typeLease.checked = true;
			else
				f.typeLease.checked = false;
			if ( c3 == 1 ) 
				f.typeStud.checked = true;
			else
				f.typeStud.checked = false;
		}
	}
}
