window.onload = getLastFilter;
window.onunload = saveFilter;

function filterWatches(checkBox) {
	var strResults="<results>\r\n\r\n";
	strResults+="  <reference>" + getEl("referenceName").value + "</reference>\r\n";
	strResults+="	<genres>\r\n";
	strResults+="		<genre type='men' id='2'>"+getEl("box2").checked+"</genre>\r\n";
	strResults+="		<genre type='ladies' id='1'>"+getEl("box3").checked+"</genre>\r\n";
	strResults+="	</genres>\r\n";
	strResults+="	<themes>\r\n";
	strResults+="		<theme type='complicated' id='3'>"+getEl("box5").checked+"</theme>\r\n";
	strResults+="		<theme type='classic' id='2'>"+getEl("box6").checked+"</theme>\r\n";
	strResults+="		<theme type='casual' id='1'>"+getEl("box7").checked+"</theme>\r\n";
	strResults+="		<theme type='jeweled' id='4'>"+getEl("box8").checked+"</theme>\r\n";
	strResults+="	</themes>\r\n";
	strResults+="	<collections>\r\n";
	strResults+="		<collection type='grandcomplication' id='91'>"+getEl("box10").checked+"</collection>\r\n";
	strResults+="		<collection type='complication' id='86'>"+getEl("box11").checked+"</collection>\r\n";
	strResults+="		<collection type='calatrava' id='84'>"+getEl("box12").checked+"</collection>\r\n";
	strResults+="		<collection type='gondolo' id='90'>"+getEl("box13").checked+"</collection>\r\n";
	strResults+="		<collection type='goldenellipse' id='88'>"+getEl("box14").checked+"</collection>\r\n";
	strResults+="		<collection type='aquanaut' id='82'>"+getEl("box15").checked+"</collection>\r\n";
	strResults+="		<collection type='nautilus' id='93'>"+getEl("box16").checked+"</collection>\r\n";
	strResults+="		<collection type='twenty4' id='96'>"+getEl("box17").checked+"</collection>\r\n";
	strResults+="		<collection type='jewellery' id='98'>"+getEl("box18").checked+"</collection>\r\n";
	strResults+="		<collection type='lepine' id='99'>"+getEl("box48").checked+"</collection>\r\n";
	strResults+="		<collection type='hunter' id='100'>"+getEl("box49").checked+"</collection>\r\n";
	strResults+="	</collections>\r\n";
	strResults+="	<materials>\r\n";
	strResults+="		<material type='P' id='4'>"+getEl("box20").checked+"</material>\r\n";
	strResults+="		<material type='J' id='3'>"+getEl("box22").checked+"</material>\r\n";
	strResults+="		<material type='G' id='2'>"+getEl("box23").checked+"</material>\r\n";
	strResults+="		<material type='R' id='5'>"+getEl("box24").checked+"</material>\r\n";
	strResults+="		<material type='A' id='1'>"+getEl("box25").checked+"</material>\r\n";
	strResults+="	</materials>\r\n";
	strResults+="	<sizes>\r\n";
	strResults+="		<size type='small' id='2'>"+getEl("box27").checked+"</size>\r\n";
	strResults+="		<size type='medium' id='2'>"+getEl("box28").checked+"</size>\r\n";
	strResults+="		<size type='large' id='1'>"+getEl("box29").checked+"</size>\r\n";
	strResults+="	</sizes>\r\n";
	strResults+="	<movements>\r\n";
	strResults+="		<movement type='manual' id='1'>"+getEl("box32").checked+"</movement>\r\n";
	strResults+="		<movement type='automatic' id='2'>"+getEl("box33").checked+"</movement>\r\n";
	strResults+="		<movement type='quartz' id='3'>"+getEl("box35").checked+"</movement>\r\n";
	strResults+="	</movements>\r\n";
	strResults+="	<complications>\r\n";
	strResults+="		<complication type='CH'>"+getEl("box37").checked+"</complication>\r\n";
	strResults+="		<complication type='Q'>"+getEl("box38").checked+"</complication>\r\n";
	strResults+="		<complication type='QA'>"+getEl("box39").checked+"</complication>\r\n";
	strResults+="		<complication type='LU'>"+getEl("box40").checked+"</complication>\r\n";
	strResults+="		<complication type='HU'>"+getEl("box41").checked+"</complication>\r\n";
	strResults+="		<complication type='FUS'>"+getEl("box42").checked+"</complication>\r\n";
	strResults+="		<complication type='TO'>"+getEl("box43").checked+"</complication>\r\n";
	strResults+="		<complication type='R'>"+getEl("box44").checked+"</complication>\r\n";
	strResults+="		<complication type='IRM'>"+getEl("box45").checked+"</complication>\r\n";

	strResults+="	</complications>\r\n";
	strResults+="</results>\r\n";




	watchXmlHttp=getXMLHTTP();
  var requestUrl = "/scripts/findWatches.aspx";
	if (watchXmlHttp) {
	  watchXmlHttp.onreadystatechange = function () {watchXmlHttp_success(watchXmlHttp); };
		watchXmlHttp.open("POST",requestUrl,true);
		watchXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		watchXmlHttp.send(strResults);
	  getEl("textRequest").value = strResults;
	}
}

var watchXmlHttp;

function watchXmlHttp_success(watchXmlHttp) {
	//alert(watchXmlHttp.readyState)
	if(watchXmlHttp.readyState!=4) {
 	  return;
 	} try {

		var resultsDiv = getEl("findWatchesResults");
		var thumbs = watchXmlHttp.responseXML.getElementsByTagName("thumb");
		//alert(watchXmlHttp.responseXML.xml)

		var resultsTable ="<table style='width:100%'><tr>";
		for(var thumbCount=0; thumbCount<thumbs.length; thumbCount++) {
			var watchName = thumbs[thumbCount].getAttributeNode("name").nodeValue;
			var thumbSrc = "/images/watches/thumb_bottom_gif/" + watchName + ".gif";
			var watchLink = "/html/en/ref_" + watchName + ".html";


			resultsTable +="<td><a href='" + watchLink + "'>";
			resultsTable +="<img src='" + thumbSrc + "'/><br/>";
			resultsTable += thumbs[thumbCount].getAttributeNode("longname").nodeValue;
			resultsTable +="</a></td>";
			//new row every 6 watches
			if((thumbCount+1) % 5 == 0) { resultsTable +="</tr><tr>"; }
		}

		resultsDiv.innerHTML = resultsTable;

	} catch(e) {
		alert("error getting results");
		return;
	}
}

function getXMLHTTP() {
	var xmlhttp =	false;
	if (window.XMLHttpRequest)	{
		xmlhttp = new XMLHttpRequest();
		if (xmlhttp.overrideMimeType) {
			xmlhttp.overrideMimeType('text/xml');
		}
	}
	//code	for IE
	else if	(window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E)	{
			xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}


//get last filter from cookie
function getLastFilter() {
	//has a cookie already been set?
	var filter = readCookie('patekFilterWatches');
	if(!filter) {
		return;
	}
	checkboxNamesValues = filter.split("#");
	for(var i=0; i<checkboxNamesValues.length; i++) {
		checkboxNameValue = checkboxNamesValues[i].split(":");
		if(!checkboxNameValue.length==2 || !getEl(checkboxNameValue[0])) {
			continue;
		}
		getEl(checkboxNameValue[0]).checked = (checkboxNameValue[1]=="true");
	}
	filterWatches("");
}

//save filter to cookie
function saveFilter() {
	var filter ="";
	//loop through all checkboxes to see ahat is checked
	if(!document.getElementsByTagName) {
		return;
	}
	var checkboxes = document.getElementsByTagName("input");
	for(var i = 0; i<checkboxes.length; i++) {
		filter += checkboxes[i].id + ":" + checkboxes[i].checked + "#";
	}
	createCookie('patekFilterWatches',filter,2);
}
