// FB Product Filter
var containerID = "productListings";
var url = window.location;
function treadmillFilter(){
	showLoadingScreen();
	var envelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	envelope += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
	envelope += "<soap:Body>";
	envelope += "<treadmillFilter xmlns=\"http://aws.fitnessblowout.com/\">";
	envelope += "<priceMin>"+document.getElementById("treadmillFilter-priceMin").value+"</priceMin>";
	envelope += "<priceMax>"+document.getElementById("treadmillFilter-priceMax").value+"</priceMax>";
	envelope += "<body>"+document.getElementById("treadmillFilter-body").value+"</body>";
	envelope += "<grade>"+document.getElementById("treadmillFilter-grade").value+"</grade>";
	envelope += "<maxUserWeight>"+document.getElementById("treadmillFilter-maxUserWeight").value+"</maxUserWeight>";
	envelope += (document.getElementById("treadmillFilter-heartRateProgram").checked == true) ?
		"<heartRateProgram>1</heartRateProgram>" :
		"<heartRateProgram/>";
	envelope += (document.getElementById("treadmillFilter-iPod").checked == true) ?
		"<iPod>1</iPod>" :
		"<iPod/>";
	envelope += (document.getElementById("treadmillFilter-tv").checked == true) ?
		"<tv>1</tv>" :
		"<tv/>";
	envelope += "<orderBy>"+document.getElementById("treadmillFilter-orderBy").value+"</orderBy>";
	envelope += "</treadmillFilter>";
	envelope += "</soap:Body>";
	envelope += "</soap:Envelope>";
	window.setTimeout("sendRequest('"+envelope+"', \"treadmillFilter\")", 500);
}
function ellipticalFilter(){
	showLoadingScreen();
	var envelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	envelope += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
	envelope += "<soap:Body>";
	envelope += "<ellipticalFilter xmlns=\"http://aws.fitnessblowout.com/\">";
	envelope += "<priceMin>"+document.getElementById("ellipticalFilter-priceMin").value+"</priceMin>";
	envelope += "<priceMax>"+document.getElementById("ellipticalFilter-priceMax").value+"</priceMax>";
	envelope += "<drive>"+document.getElementById("ellipticalFilter-drive").value+"</drive>";
	envelope += "<grade>"+document.getElementById("ellipticalFilter-grade").value+"</grade>";
	envelope += "<maxUserWeight>"+document.getElementById("ellipticalFilter-maxUserWeight").value+"</maxUserWeight>";
	envelope += (document.getElementById("ellipticalFilter-heartRateProgram").checked == true) ?
		"<heartRateProgram>1</heartRateProgram>" :
		"<heartRateProgram/>";
	envelope += (document.getElementById("ellipticalFilter-iPod").checked == true) ?
		"<iPod>1</iPod>" :
		"<iPod/>";
	envelope += (document.getElementById("ellipticalFilter-adjustableStride").checked == true) ?
		"<adjustableStride>1</adjustableStride>" :
		"<adjustableStride/>";
	envelope += "<orderBy>"+document.getElementById("ellipticalFilter-orderBy").value+"</orderBy>";
	envelope += "</ellipticalFilter>";
	envelope += "</soap:Body>";
	envelope += "</soap:Envelope>";
	window.setTimeout("sendRequest('"+envelope+"', \"ellipticalFilter\")", 500);
}
function showLoadingScreen(){
	clearContainer();
	container = document.getElementById(containerID);
	loadingScreen = container.appendChild(document.createElement("div"));
	loadingScreen.className = "loading";
	loadingImg = loadingScreen.appendChild(document.createElement("img"));
	loadingImg.src = "http://www.fitnessblowout.com/images/loading.gif";
	br = loadingScreen.appendChild(document.createElement("br"));
	br = loadingScreen.appendChild(document.createElement("br"));
	message = loadingScreen.appendChild(document.createTextNode("Loading..."));
}
function sendRequest(envelope, method){
	httpRequest = Request();
	httpRequest.onreadystatechange = function() {
		if (httpRequest.readyState == 4) {
			//alert(httpRequest.responseText);
			soapEnvelope = httpRequest.responseXML.getElementsByTagName("SOAP-ENV:Envelope")[0];
				           soapBody = soapEnvelope.getElementsByTagName("SOAP-ENV:Body")[0];
					           response = soapBody.getElementsByTagName("ns1:"+method+"Response")[0];
						  responseArray = response.getElementsByTagName("return")[0];
							 items = responseArray.getElementsByTagName("item");
	
			clearContainer();
			document.getElementById("results").style.visibility = "visible";
			document.getElementById("results").innerHTML = items.length+" Matching Products";
			for(i = 0; i < items.length; i++){
				product = items[i];
				writeProduct(i, product);
			}
			//if(!url.indexOf("#filter")){ window.location = url+"#filter"; }
		}
	}
	httpRequest.open("POST", "http://www.fitnessblowout.com/awsProxy.php?service=FilterServer", true);
	httpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
	httpRequest.setRequestHeader("Content-length", envelope.length);
	httpRequest.setRequestHeader("SOAPAction", "http://aws.fitnesssblowout.com/"+method);
	httpRequest.send(envelope);
}
function results(products){
	container = document.getElementById(containerID);
	results = container.appendChild(document.createElement("div"));
	results.className = "results";
	message = results.appendChild(document.createTextNode(products+" Matching Products"));
}
function writeProduct(increment, productNode){
	container = document.getElementById(containerID);
	product = container.appendChild(document.createElement("div"));
	product.className = "product";
	product.id = productNode.getElementsByTagName("productID")[0].firstChild.nodeValue;

	productImage = product.appendChild(document.createElement("div"));
	productImage.className = "productImage";
	image = productImage.appendChild(document.createElement("img"));
	image.src = "http://www.fitnessblowout.com/images/products/medium/"+productNode.getElementsByTagName("productNameLink")[0].firstChild.nodeValue+".jpg"

	productFeatures = product.appendChild(document.createElement("div"));
	productFeatures.className = "productFeatures";
		productTitle = productFeatures.appendChild(document.createElement("span"))
		productTitle.className = "productTitle";
			title = productTitle.appendChild(document.createTextNode(unicodeFormat(productNode.getElementsByTagName("productTitle")[0].firstChild.nodeValue)));
		br = productFeatures.appendChild(document.createElement("br"));
		//stars
		spacer = productFeatures.appendChild(document.createElement("img"));
		spacer.src = "http://www.fitnessblowout.com/images/clearImg2.gif";
		br = productFeatures.appendChild(document.createElement("br"));

		rating = productFeatures.appendChild(document.createElement("span"));
		rating.className = "Features";
			ratingImage = rating.appendChild(document.createElement("img"));
			ratingImage.src = "http://www.fitnessblowout.com/images/"+productNode.getElementsByTagName("ratingImage")[0].firstChild.nodeValue;
			ratingValue = rating.appendChild(document.createTextNode(" ("+productNode.getElementsByTagName("rating")[0].firstChild.nodeValue+")"));
		
		br = productFeatures.appendChild(document.createElement("br"));
		
		highlights = productFeatures.appendChild(document.createElement("span"));
		highlights.className = "highlights";
			highlightsTitle = highlights.appendChild(document.createTextNode("Highlights:"));
		br = productFeatures.appendChild(document.createElement("br"));

		highlightsFeatures = productFeatures.appendChild(document.createElement("span"));
		highlightsFeatures.className = "highlightsText";
			featureA = highlightsFeatures.appendChild(document.createTextNode("\u25CF "+unicodeFormat(productNode.getElementsByTagName("featureA")[0].firstChild.nodeValue)));
			br = highlightsFeatures.appendChild(document.createElement("br"));
			featureB = highlightsFeatures.appendChild(document.createTextNode("\u25CF "+unicodeFormat(productNode.getElementsByTagName("featureB")[0].firstChild.nodeValue)));
			br = highlightsFeatures.appendChild(document.createElement("br"));
			featureC = highlightsFeatures.appendChild(document.createTextNode("\u25CF "+unicodeFormat(productNode.getElementsByTagName("featureC")[0].firstChild.nodeValue)));
			br = highlightsFeatures.appendChild(document.createElement("br"));

	productPrice = product.appendChild(document.createElement("div"));
	productPrice.className = "productPrice";
	salePrice = productPrice.appendChild(document.createTextNode("Was $"+productNode.getElementsByTagName("salePrice")[0].firstChild.nodeValue));
	br = productPrice.appendChild(document.createElement("br"));
	freeShipping = productPrice.appendChild(document.createTextNode("FREE Shipping!"));
	br = productPrice.appendChild(document.createElement("br"));
	noSalesTax = productPrice.appendChild(document.createTextNode("NO Sales Tax Today!*"));
	br = productPrice.appendChild(document.createElement("br"));
	productLink = productPrice.appendChild(document.createElement("a"));
	productLink.href = "http://www.fitnessblowout.com/equipment/"+productNode.getElementsByTagName("categoryNameLink")[0].firstChild.nodeValue.replace(/_/g, "-")+"/"+productNode.getElementsByTagName("productNameLink")[0].firstChild.nodeValue.replace(/_/g, "-");
	viewDetails = productLink.appendChild(document.createElement("img"));
	viewDetails.src = "http://www.fitnessblowout.com/Images/ViewDetails.jpg";
	viewDetails.border = 0;

	clear = product.appendChild(document.createElement("div"));
	clear.className = "clear";

  	product.style.visibility = "visible";
}
function Request(){
	var httpRequest;
	if (window.XMLHttpRequest) {
		httpRequest = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return httpRequest;
}
function clearContainer(){
	container = document.getElementById(containerID);
	if(container.hasChildNodes()){
		while(container.childNodes.length >= 1){
			container.removeChild(container.firstChild);       
		}
	}
}
function unicodeFormat(string){
	string = string.replace(/trademark/g, "\u2122");
	string = string.replace(/registered/g, "\u00AE");
	return string;
}