
var fCatId = "";	
var fBrandId = "";			
var fSize = "";		
var fTension = "";
var fPriceMin = "";
var fPriceMax	= "";
var updateFieldId;

function refreshFilterFormCount()
{
	var fFrm = document.getElementById("filterform");
	//alert("Refreshing: " + fFrm.name);	
	if (fFrm)
		filterFormCount(fFrm, "");	
		
	//alert("count updated");			
}

function filterForm(myFrm, myFieldId)
{
	updateFieldId = myFieldId;
	//alert(myField.name + " - " + myField.id);
	
	if (myFrm.ffCategoryid)
		fCatId = myFrm.ffCategoryid.options[myFrm.ffCategoryid.selectedIndex].value;	
	else if (myFrm.ffCategoryidHidden)
		fCatId = myFrm.ffCategoryidHidden.value;
	else
		fCatId = "";
			
	fBrandId = myFrm.ffManufacturerid.options[myFrm.ffManufacturerid.selectedIndex].value;			
	fSize = myFrm.ffSize.options[myFrm.ffSize.selectedIndex].value;		
	fTension = myFrm.ffTension.options[myFrm.ffTension.selectedIndex].value;
	
	fPriceMin = myFrm.ffPriceMin.options[myFrm.ffPriceMin.selectedIndex].value;
	fPriceMax = myFrm.ffPriceMax.options[myFrm.ffPriceMax.selectedIndex].value;
				
	//var divContent = document.getElementById("ffCategoryid");
	//alert("Select Box Changed: " + myFieldId);
	
	if (myFieldId == "ffCategoryid")
		var url = "/adata/filterform.php?catid=" + fCatId;
	/*	
	else if(myFieldId == "ffManufacturerid")
		var url = "/adata/filterform.php?catid=" + fCatId + "&brand=" + fBrandId + "&pmin=" + fPriceMin + "&pmax=" + fPriceMax;
	else if(myFieldId == "ffSize")	
		var url = "/adata/filterform.php?catid=" + fCatId + "&brand=" + fBrandId + "&size=" + fSize + "&pmin=" + fPriceMin + "&pmax=" + fPriceMax;
	else if(myFieldId == "ffTension")	
		var url = "/adata/filterform.php?catid=" + fCatId + "&brand=" + fBrandId + "&size=" + fSize + "&tension=" + fTension + "&pmin=" + fPriceMin + "&pmax=" + fPriceMax;		
	*/ 
	else
		var url = "/adata/filterform.php?catid=" + fCatId + "&brand=" + fBrandId + "&size=" + fSize + "&tension=" + fTension + "&pmin=" + fPriceMin + "&pmax=" + fPriceMax;
	
	
	//alert(myFieldId + " - " + url);
	
	/*
	var divX = document.getElementById("divXml");
	divX.innerHTML = "<a href=\""+  url+"\" target=\"_blank\">AJAX XML</a>";
	*/
	//var divContent = document.getElementById("divFFtest");
	//divContent.innerHTML = "Loading AJAX xml....<br />";
	
	http_s.open("GET", url, true);
	http_s.onreadystatechange = ffHttpResponse;
	http_s.send(null);	
}



function filterFormCount(myFrm, myFieldId)
{
	updateFieldId = myFieldId;
	//alert(myField.name + " - " + myField.id);
	
	if (myFrm.ffCategoryid)
		fCatId = myFrm.ffCategoryid.options[myFrm.ffCategoryid.selectedIndex].value;	
	else if (myFrm.ffCategoryidHidden)
		fCatId = myFrm.ffCategoryidHidden.value;
	else
		fCatId = "";	
	fBrandId = myFrm.ffManufacturerid.options[myFrm.ffManufacturerid.selectedIndex].value;			
	fSize = myFrm.ffSize.options[myFrm.ffSize.selectedIndex].value;		
	fTension = myFrm.ffTension.options[myFrm.ffTension.selectedIndex].value;
	
	fPriceMin = myFrm.ffPriceMin.options[myFrm.ffPriceMin.selectedIndex].value;
	fPriceMax = myFrm.ffPriceMax.options[myFrm.ffPriceMax.selectedIndex].value;
				
	//var divContent = document.getElementById("ffCategoryid");
	var url = "/adata/filterformcount.php?catid=" + fCatId + "&brand=" + fBrandId + "&size=" + fSize + "&tension=" + fTension + "&pmin=" + fPriceMin + "&pmax=" + fPriceMax;
	//alert(myFieldId + " - " + url);
	
//	var divX = document.getElementById("divXml");
//	divX.innerHTML = "<a href=\""+  url+"\" target=\"_blank\">AJAX XML</a>";
	
	//var divContent = document.getElementById("divFFtest");
	//divContent.innerHTML = "Loading AJAX xml....";
	
	http_s.open("GET", url, true);
	http_s.onreadystatechange = ffCountHttpResponse;
	http_s.send(null);	
}


function ffCountHttpResponse()
{
	//var divContent = document.getElementById("divFFtest");
	
	if (http_s.readyState == 4 )
	{
		var xmlDoc = http_s.responseXML.documentElement;
		
		// Count
		filter_count = upsdateFilterCount(xmlDoc);
		//divContent.innerHTML = "Filter Count OK ("+ filter_count +")<br />";	
	}
}
function ffHttpResponse() 
{	
	//var divContent = document.getElementById("divFFtest");
	var buff = "";
	
	if (http_s.readyState == 4 && http_s.status == 200) 
	{	
		var xmlDoc = http_s.responseXML.documentElement;
		//alert("xml loaded");
		
		// Count
		filter_count = upsdateFilterCount(xmlDoc);
		//divContent.innerHTML += "Product hits OK ("+ filter_count +")<br />";
		
		//Price
		var min_price = 0;
		var max_price = 0;
		//hasChildNodes()
		if (xmlDoc.getElementsByTagName("minprice")[0].hasChildNodes() && xmlDoc.getElementsByTagName("minprice")[0].childNodes[0].nodeValue)
			min_price = xmlDoc.getElementsByTagName("minprice")[0].childNodes[0].nodeValue;
		else
			alert("MIN price has no child nodes");
			
		if (xmlDoc.getElementsByTagName("maxprice")[0].hasChildNodes() && xmlDoc.getElementsByTagName("maxprice")[0].childNodes[0].nodeValue)
			max_price = xmlDoc.getElementsByTagName("maxprice")[0].childNodes[0].nodeValue;
		else
			alert("MAX price has no child nodes");
		buff += "Price: &pound; " + min_price + " to &pound; "+ max_price +"<br>";
		//divContent.innerHTML += "Min/Max Price OK<br/>";
							

		// Categories		
		//updateFilterSelect(xmlDoc, "cat", "ffCategoryid");
		//divContent.innerHTML += "Cats OK<br/>";
						
		
		// Brands
		updateFilterSelect(xmlDoc, "brand", "ffManufacturerid");
		
		// Size
		updateFilterSelect(xmlDoc, "size", "ffSize");
		
		// Tension
		updateFilterSelect(xmlDoc, "tension", "ffTension");
			
			
		// To Price (MAX)
		updateFilterSelect(xmlDoc, "toprice", "ffPriceMax");
			
		// From Price (MIN)
		updateFilterSelect(xmlDoc, "fromprice", "ffPriceMin");		
			
			
		//divContent.innerHTML += buff;
		
		
		http_s = getHTTPObject(); // We create the HTTP Object
	}	
	else
	{
	//	alert("readyState: " + http_s.readyState);
	}
	
}

function upsdateFilterCount(xmlDoc)
{
	var filter_count =0;		
	if (xmlDoc.getElementsByTagName("count")[0].childNodes[0].nodeValue)
		filter_count = xmlDoc.getElementsByTagName("count")[0].childNodes[0].nodeValue;
								
	var divFilterHits = document.getElementById("ffFilterHits");		
	divFilterHits.innerHTML = filter_count;
	return filter_count;
}

function updateFilterSelect(xmlDoc, xmlParentTag, selectId)
{
	
	var xmlObj = xmlDoc.getElementsByTagName(xmlParentTag)[0].getElementsByTagName("el");	
	var selectObj = document.getElementById(selectId);	
	
	if (!selectObj) alert("SELECT element not found - ID:" + selectId);	
	
	if (selectObj.selectedIndex && selectObj.options[selectObj.selectedIndex].value)
		selectedValue = selectObj.options[selectObj.selectedIndex].value;
	else
		selectedValue = "";		
		
	selectObj.options.length=0; // delete old options	

	// debug
	/*
	var divContent = document.getElementById("divFFtest");
	divContent.innerHTML += " <b>"+ selectId + "</b> ";
	*/
	var selectedIsSet = false;
	
	for (x=0; x< xmlObj.length; x++)
	{
		subNode = xmlObj[x];
		optText = subNode.childNodes[0].nodeValue;
		optValue = subNode.attributes[0].value;
		
		if (selectedValue != "" && optValue == selectedValue)
		{
			isSelected = true;
			selectedIsSet = true;
		}
		else
			isSelected = false;
			
		if (selectId == "ffPriceMax" && isSelected == false && (x == (xmlObj.length-1)) )
		{
			//alert(selectId + " "+ optText);	
			isSelected = true;
			selectedIsSet = true;
		}
		var option = new Option(optText,optValue, false, isSelected);
		try{
			selectObj.add(option,null);
		}catch (e){
			selectObj.add(option,-1);
		}
		
		
	}	
	
	
	// divContent.innerHTML += " OK - (nodes: "+ xmlObj.length + ")<br />";
}


function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var http_s = getHTTPObject(); // We create the HTTP Object


function swatchPopUp(URL) {
newWindow = window.open(URL, 'swatch', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=400');
}

