// JavaScript Document

var ar_atrib;

function addEvents() { 
	ar_atrib = [];
	
	inputs = document.getElementsByTagName('input'); 
	sortBy = document.getElementsByName('sort'); 
	
	var arIndex;
	
	for(var i=0; i < inputs.length; i++) { 
		if(inputs[i].getAttribute('type') == 'checkbox' && inputs[i].getAttribute('name') == 'finder_attributes') { 
			arIndex = inputs[i].value.split(";");
			arIndex = arIndex[0];

			ar_atrib[arIndex] = inputs[i];
			
			inputs[i].onclick = updateFinderForm; 
		} 
	} 
	
	if(sortBy.length > 0){
		sortBy[0].onchange = function(){
			currentSort = this.value;
			updateFinderForm();
		}
	}
} 
	
function getAttributesURL() {
	attributes = document.getElementsByName("finder_attributes");
	
	url = "";
	attributesChecked = false;
	
	if (!attributes.length) {
		if (attributes.checked){
			url = url + "/" + fixAttribute(attributes.value);
		}
	} else {
		for (i = 0; i < attributes.length; i++){
			if (attributes[i].checked){
				attributesChecked = true;
				
				addToUrl = fixAttribute(attributes[i].value);
				groupAndAttribute = addToUrl.split(":");
				if (url.indexOf(groupAndAttribute[0]) > 0)
					url = url + ";" + groupAndAttribute[1];
				else						
					url = url + "/" + addToUrl;
			}
		}
	}
	
	if (attributesChecked == false)
		url = url + "/" + allURL; 
	
	if (havePrice) 
		url = url + priceURL;
	
	if (haveBrand) 
		url = url + brandURL;
	
	return url;
	
}

function getQueryURL(){
	attributes = document.getElementsByName("finder_attributes");
	
	qs = "attributes=0";
	url = "";
	attributesChecked = false;
	
	if (!attributes.length) {
		if (attributes.checked){
			
			fix = attributes[i].value.split(";");
			
			addToUrl = fix[1];
			addToQS = fix[0];
			
			url = url + "/" + addToUrl;
			qs = qs + "," + addToQS;
		}
	} else {
		for (i = 0; i < attributes.length; i++){
			if (attributes[i].checked){
				attributesChecked = true;
				
				fix = attributes[i].value.split(";");
				
				addToUrl = fix[1];
				addToQS = fix[0];
				
				groupAndAttribute = addToUrl.split(":");
				if (url.indexOf(groupAndAttribute[0]) > 0){
					url = url + ";" + groupAndAttribute[1];
					qs = qs + "-" + addToQS;
				} else {
					url = url + "/" + addToUrl;
					qs = qs + "," + addToQS;
				}
			}
		}
	}
	
	if (attributesChecked == false)
		url = url + "/" + allURL; 
	
	if (havePrice) {
		url = url + priceURL;
		qs = qs + "&p=" + priceID;
	}
	
	if (haveBrand) {
		qs = qs + "&b=" + brandID;
		url = url + escape(brandURL).replace("%3A", ":");
	}

	return qs + "&link=" + url + "&sort=" + currentSort;
}

last_finder_token = 0;

function updateFinderForm(ev, start, doFade) {
	url = "?customPage=" + document.getElementsByName("finder_custom_page")[0].value + "&" + getQueryURL();
	
	last_finder_token = Math.floor((Math.random() * 10000) * (Math.random() * 10000)) + 1;
	//alert(last_finder_token);
     
	document.getElementById("finderMatches").style.display = "block";
	
	loading = document.createElement("img");
	loading.setAttribute("src","/images/finder_loading.gif");

	var finderMatches = document.getElementById("finderMatches");
	var finderSubmit = document.getElementById("finder_submit");
	
	while (finderSubmit.firstChild)
		finderSubmit.removeChild(finderSubmit.firstChild);
	
	while (finderMatches.firstChild)
		finderMatches.removeChild(finderMatches.firstChild);
		
	finderMatches.appendChild(loading);
	
	if (start == null)
		start = 1;
	
	url = url + "&startRow=" + start;
	
	if (doFade == null)
		doFade = true;
		
	LoadDoc(url, last_finder_token, doFade);
}

function resetFinderForm() {
	
	if (location.pathname == pageURL || location.pathname == (pageURL + "/" + allURL)){
		
		inputs = document.getElementsByTagName('input'); 
		
		for(var i=0; i < inputs.length; i++) { 
			inputs[i].removeAttribute("disabled");
			inputs[i].checked = false;
		} 
		
		updateFinderForm();
	} 
	else {
		location.href = pageURL + "/" + allURL;
	}
}

function syncFinder(attribs){
	if (window.ignoreHistory) {
		return false;
	}
	
	inputs = document.getElementsByTagName('input'); 
	sel = attribs.split(","); 
	
	var arIndex;
	
	for(var i=0; i < inputs.length; i++) {
		if(inputs[i].getAttribute('type') == 'checkbox' && inputs[i].getAttribute('name') == 'finder_attributes') { 
			checked = false;
			
			for (var j = 0; j < sel.length; j++){
				if (parseInt(inputs[i].value) == sel[j])
					checked = true;
			}
			
			inputs[i].checked = checked;
		} 
	} 
}

function LoadHistory(url){
	// force fade in of products
	document.getElementById("products").removeAttribute("doFade");

	if (window.ignoreHistory) {
		window.ignoreHistory = false;
		return false;
	}
		
	last_finder_token = Math.floor((Math.random() * 10000) * (Math.random() * 10000)) + 1;
	
	LoadDoc("?" + url, last_finder_token, true, true);

	window.ignoreHistory = false;
}

var rx;
function LoadDoc(url, token, doFade, history) {
	if (!history && document.getElementById("finderHistory")){
		window.ignoreHistory = true;
		loadHistory = "/shop/_finder_history/" + url + "&doFade=" + doFade;
		document.getElementById("finderHistory").src = loadHistory;
		if(navigator.userAgent.indexOf("WebKit") > -1) {
			/*if (location.hash == "")
				hashCount = 123;
			else
				hashCount = parseInt(location.hash.substr(1)) + 1;*/
			location.hash = escape(url.substr(1)).replace("/", "%2F");
		}
	}
	else
		window.ignoreHistory = false;

	baseURL = "/shop/_finder_selection.cfm";
	
	reqURL = baseURL + url + "&token=" + token;
	
	if (typeof XMLHttpRequest == "undefined") {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) {
				rx = xmlDoc;
				process();
			}
		}
		
		xmlDoc.load(reqURL);
	}
	else {
		xmlDoc = new XMLHttpRequest();

		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) {
				rx = xmlDoc.responseXML;
				process();
			}
		}

		xmlDoc.open( "GET", reqURL, true );
		xmlDoc.send( null );
	}

	
	var products = document.getElementById("products");
	if (products != null){
		if(doFade){
				Effects.fade("products", 100, 0, 500, null);
			}
		else
			products.setAttribute("doFade", "false");
	}
		
	//xmlDoc.load(baseURL + url + "?token=" + token);
}

/* old version, changed Dec 6, 2007 to work in safari 2
	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = new XMLHttpRequest();
		xmlDoc.onload = function () {
			//rx = document.implementation.createDocument("", "", null);
			//rx.onload = process;
			//alert(xmlDoc.responseText);
			//rx.load(xmlDoc.responseText);
			var parser = new DOMParser();
			rx = parser.parseFromString(xmlDoc.responseText, "text/xml");

			process();
		}

		xmlDoc.open( "GET", reqURL, true );
		xmlDoc.send( null );
		//xmlDoc = document.implementation.createDocument("", "", null);
		//xmlDoc.onload = process;
	}
	else if (window.ActiveXObject) {
		//xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		/xmlDoc.onreadystatechange = function () {if ( xmlDoc.readyState == 4) {process();}}
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) {
				rx = xmlDoc;
				process()
			}
		};
		
		xmlDoc.load(reqURL);
		//xmlDoc.open( "GET", reqURL, true );
		//xmlDoc.send( null );
 	}
	else {
		//alert('Your browser can\'t handle this script');
		return;
	}
*/

function process() {
	token = rx.getElementsByTagName('token')[0].childNodes[0].nodeValue;
	
	if (token == last_finder_token) {
		
		matchCount = rx.getElementsByTagName('finder-match-count')[0].firstChild.nodeValue;
		matchText = rx.getElementsByTagName('finder-match-text')[0].firstChild.nodeValue;
		
		countSpan = document.createElement("span");
		countSpan.setAttribute("id","count");
		countSpan.appendChild(document.createTextNode(matchCount));

		var finderMatches = document.getElementById("finderMatches");
		var finderSubmit = document.getElementById("finder_submit");
		
		while (finderSubmit.firstChild)
			finderSubmit.removeChild(finderSubmit.firstChild);
			
		while (finderMatches.firstChild)
			finderMatches.removeChild(finderMatches.firstChild);
			
		finderSubmit.appendChild(countSpan.cloneNode(true));
		finderSubmit.appendChild(document.createTextNode(matchText));
		
		finderMatches.appendChild(countSpan);
		finderMatches.appendChild(document.createTextNode(matchText));
		
		if (typeof(finder_form_image_reset) == 'undefined')
			finder_form_image_reset = "/images/finder_reset.gif";
			
		if (typeof(finder_form_image_submit) == 'undefined')
			finder_form_image_submit = "/images/finder_submit.gif";
			
		buttons = document.createElement("div");
		buttons.setAttribute("id", "buttons");
		
		try {
			resetButton = document.createElement("<input name='resetForm' type='image' onClick='resetFinderForm(); return false;' src='" + finder_form_image_reset + "' />");
		} catch (e) {
			resetButton = document.createElement("input");
			resetButton.setAttribute("name", "resetForm");
			resetButton.setAttribute("type", "image");
			resetButton.setAttribute("src", finder_form_image_reset);
			resetButton.onclick = function () {resetFinderForm(); return false;}
		}
		
		try {
			submitButton = document.createElement("<input name='resetForm' type='image' src='" + finder_form_image_submit + "' />");
		} catch (e) {
			submitButton = document.createElement("input");
			submitButton.setAttribute("name", "resetForm");
			submitButton.setAttribute("type", "image");
			submitButton.setAttribute("src", finder_form_image_submit);
		}
		
		buttons.appendChild(resetButton);
		buttons.appendChild(submitButton);
		
		finderMatches.appendChild(buttons);
		finderSubmit.appendChild(buttons.cloneNode(true));

		attribs = rx.getElementsByTagName('attribute');

		for (i = 0; i < attribs.length; i++){
			id = attribs[i].getElementsByTagName('attribute-id')[0].firstChild.nodeValue;
			hasMatch = attribs[i].getElementsByTagName('has-match')[0].firstChild.nodeValue;
			//if (i < 5) alert(hasMatch);
			if (hasMatch == 1 || ar_atrib[id].checked)
				ar_atrib[id].removeAttribute("disabled");
			else
				ar_atrib[id].setAttribute("disabled", 1);
		}
		
		renderProducts(rx, matchCount);
		
		//finderMatches.firstChild.innerHTML = matchCount;
	}

}

function renderProducts(rx, matchCount) {
	allProdsDiv = document.getElementById("products");
	if(allProdsDiv == null)
		return;

	var prods = rx.getElementsByTagName("product");
	var brands = rx.getElementsByTagName("brand");
	var prices = rx.getElementsByTagName("price-range");
	var browseLinks = parseSubDivs(allProdsDiv.parentNode, "browseLinks");
	
	var displayText = rx.getElementsByTagName("products-display-text");
	while(browseLinks["viewing"].firstChild)
		browseLinks["viewing"].removeChild(browseLinks["viewing"].firstChild);
		
	dtval = displayText[0].firstChild.nodeValue;
	while(dtval.indexOf("&amp;") != -1)
	 dtval = dtval.replace("&amp;", "&");
	while(dtval.indexOf("&quot;") > -1)
	 dtval = dtval.replace("&quot;", '"');
	while(dtval.indexOf("&laquo;") > -1)
		dtval = dtval.replace("&laquo;", "«")
	while(dtval.indexOf("&raquo;") > -1)
		dtval = dtval.replace("&raquo;", "»")
	 
	browseLinks["viewing"].appendChild(document.createTextNode(dtval));
	
	var displayPrev = rx.getElementsByTagName("products-display-previous")[0];	
	for(i = 0; i < browseLinks["prev"].length; i++){
		dp = browseLinks["prev"][i];
		
		while(dp.firstChild)
			dp.removeChild(dp.firstChild);
			
		pageTextObj = displayPrev.getElementsByTagName("text")[0];
		if(pageTextObj.firstChild)
			pageText = pageTextObj.firstChild.nodeValue.replace("&laquo;", "«").replace("&raquo;", "»");
		else
			pageText = null;
		pageLinkObj = displayPrev.getElementsByTagName("link")[0];
		if(pageLinkObj.firstChild)
			pageLink = pageLinkObj.firstChild.nodeValue;
		else
			pageLink = null;
		pageStartObj = displayPrev.getElementsByTagName("start")[0];
		if(pageStartObj.firstChild)
			pageStart = pageStartObj.firstChild.nodeValue;
		else
			pageStart = null;
			
		if (pageLink == null){
			prev = document.createElement("span");
			prev.appendChild(document.createTextNode(pageText));
		}
		else {
			prev = document.createElement("a");
			prev.setAttribute("href", pageLink);
			prev.setAttribute("pageStart", pageStart);
			prev.onclick = function(){updateFinderForm(null, this.getAttribute("pageStart"));	return false;}
			prev.appendChild(document.createTextNode(pageText));
		}
		
		dp.appendChild(prev);
	}
	
	var displayNext = rx.getElementsByTagName("products-display-next")[0];	
	for(i = 0; i < browseLinks["next"].length; i++){
		dp = browseLinks["next"][i];
		
		while(dp.firstChild)
			dp.removeChild(dp.firstChild);
			
		pageTextObj = displayNext.getElementsByTagName("text")[0];
		if(pageTextObj.firstChild)
			pageText = pageTextObj.firstChild.nodeValue.replace("&laquo;", "«").replace("&raquo;", "»");
		else
			pageText = null;
		pageLinkObj = displayNext.getElementsByTagName("link")[0];
		if(pageLinkObj.firstChild)
			pageLink = pageLinkObj.firstChild.nodeValue;
		else
			pageLink = null;
		pageStartObj = displayNext.getElementsByTagName("start")[0];
		if(pageStartObj.firstChild)
			pageStart = pageStartObj.firstChild.nodeValue;
		else
			pageStart = null;
			
		if (pageLink == null){
			next = document.createElement("span");
			next.appendChild(document.createTextNode(pageText));
		}
		else {
			next = document.createElement("a");
			next.setAttribute("href", pageLink);
			next.setAttribute("pageStart", pageStart);
			next.onclick = function(){updateFinderForm(null, this.getAttribute("pageStart"));	return false;}
			next.appendChild(document.createTextNode(pageText));
		}
		
		dp.appendChild(next);
	}
	
	var displayPages = rx.getElementsByTagName("products-display-page");
	for(i = 0; i < browseLinks["pages"].length; i++){
		dp = browseLinks["pages"][i];
		
		while(dp.firstChild)
			dp.removeChild(dp.firstChild);
		
		dp.appendChild(document.createTextNode("Page "));
		for (j = 0; j < displayPages.length; j++){
			pageTextObj = displayPages[j].getElementsByTagName("text")[0];
			if(pageTextObj.firstChild)
				pageText = pageTextObj.firstChild.nodeValue;
			else
				pageText = null;
			pageLinkObj = displayPages[j].getElementsByTagName("link")[0];
			if(pageLinkObj.firstChild)
				pageLink = pageLinkObj.firstChild.nodeValue;
			else
				pageLink = null;
			pageStartObj = displayPages[j].getElementsByTagName("start")[0];
			if(pageStartObj.firstChild)
				pageStart = pageStartObj.firstChild.nodeValue;
			else
				pageStart = null;

			if (pageLink == null){
				if (pageText == "..." || pageText == "show all"){
					if (pageText == "show all") {
						dp.appendChild(document.createTextNode(" - "));
						pageTextLink = document.createElement("strong");
						
						pageTextLink.appendChild(document.createTextNode(pageText));
						
						dp.appendChild(pageTextLink);
						
					}
					else
						dp.appendChild(document.createTextNode(pageText));
				}
				else {
					pageTextLink = document.createElement("strong");
					
					pageTextLink.appendChild(document.createTextNode(pageText));
					
					dp.appendChild(pageTextLink);
					dp.appendChild(document.createTextNode(" "));
				}
			}
			else {
				pageTextLink = document.createElement("a");
				pageTextLink.setAttribute("href", pageLink);
				pageTextLink.setAttribute("pageStart", pageStart);
				pageTextLink.onclick = function(){updateFinderForm(null, this.getAttribute("pageStart"));	return false;}
				
				pageTextLink.appendChild(document.createTextNode(pageText));
				
				if (pageText == "show all"){
					dp.appendChild(document.createTextNode(" - "));
				}
				
				dp.appendChild(pageTextLink);
				dp.appendChild(document.createTextNode(" "));
			}
		}
	}
	
	if(allProdsDiv == null)
		return;
		
	var curEl;
	var insertBefore;
	
	while(curEl = allProdsDiv.firstChild){
		if(curEl.nodeName == "DIV" || curEl.nodeType == 3)
			allProdsDiv.removeChild(curEl);
		else {
			insertBefore = curEl;
			break;
		}
	}
	
	for (i = 0; i < prods.length; i++){
		prodTitle = prods[i].getElementsByTagName('title')[0].firstChild.nodeValue;
		while(prodTitle.indexOf("&amp;") != -1)
		 prodTitle = prodTitle.replace("&amp;", "&");
		while(prodTitle.indexOf("&quot;") > -1)
		 prodTitle = prodTitle.replace("&quot;", '"');
		prodLink = prods[i].getElementsByTagName('link')[0].firstChild.nodeValue;
		prodImage = prods[i].getElementsByTagName('image')[0].firstChild.nodeValue;
		prodDesc = prods[i].getElementsByTagName('short-description')[0].firstChild.nodeValue;
		while(prodDesc.indexOf("&amp;") != -1)
		 prodDesc = prodDesc.replace("&amp;", "&");
		while(prodDesc.indexOf("&quot;") > -1)
		 prodDesc = prodDesc.replace("&quot;", '"');
		prodPrice = prods[i].getElementsByTagName('price')[0].firstChild.nodeValue;
		betterPrice = prods[i].getElementsByTagName('better-price')[0].firstChild.nodeValue;
		prodPriceFlagObj = prods[i].getElementsByTagName("price-flag")[0];
		if(prodPriceFlagObj.firstChild)
			prodPriceFlag = prodPriceFlagObj.firstChild.nodeValue;
		else
			prodPriceFlag = null;
		loggedInObj = prods[i].getElementsByTagName("logged-in")[0];
		if(loggedInObj.firstChild)
			loggedIn = loggedInObj.firstChild.nodeValue;
		else
			loggedIn = "false";
		saleNoticeObj = prods[i].getElementsByTagName("sale-notice")[0];
		if(saleNoticeObj.firstChild)
			saleNotice = saleNoticeObj.firstChild.nodeValue;
		else
			saleNotice = "false";
		listPrice = prods[i].getElementsByTagName('list-price')[0].firstChild.nodeValue;
		listPriceObj = prods[i].getElementsByTagName('list-price')[0];
		
		prodDiv = document.createElement("div");
		prodDiv.setAttribute("class", "prod");
		prodDiv.setAttribute("className", "prod");

		/*if(allProdsDiv.getAttribute("doFade") == null){
			prodDiv.style.opacity = 0;
			prodDiv.style.MozOpacity = 0;
			prodDiv.style.KhtmlOpacity = 0;
			prodDiv.style.filter = "alpha(opacity=0)";
		}*/

		itemImage = document.createElement("div");
		itemImage.setAttribute("class", "itemImage");
		itemImage.setAttribute("className", "itemImage");
		imageLink = document.createElement("a");
		imageLink.setAttribute("href", prodLink);
		imageSrc = document.createElement("img");
		imageSrc.setAttribute("src", prodImage);
		imageSrc.setAttribute("alt", prodTitle);
		
		if (imageSrc.height > 0)
			imageSrc.style.marginTop = (160 - imageSrc.height) / 2 + "px"

		imageLink.appendChild(imageSrc);
		itemImage.appendChild(imageLink);
		
		if (saleNotice != "false"){
			saleBadgeSpan = document.createElement("span");
			saleBadgeSpan.appendChild(document.createTextNode(saleNotice + "%"));
			
			saleBadge = document.createElement("div");
			saleBadge.setAttribute("class", "salenotice");
			saleBadge.setAttribute("className", "salenotice");
			saleBadge.appendChild(document.createTextNode("Sale!"));
			saleBadge.appendChild(document.createElement("br"));
			saleBadge.appendChild(saleBadgeSpan);
			
			itemImage.appendChild(saleBadge);
		}
		
		itemInfo = document.createElement("div");
		itemInfo.setAttribute("class", "itemInfo");
		itemInfo.setAttribute("className", "itemInfo");
		
		itemTitle = document.createElement("h5");
		itemTitleLink = document.createElement("a");
		itemTitleLink.setAttribute("href", prodLink);
		itemTitleLink.appendChild(document.createTextNode(prodTitle));
		itemTitle.appendChild(itemTitleLink);
		
		detLink = document.createElement("a");
		detLink.setAttribute("href", prodLink);
		detImage = document.createElement("img");
		detImage.setAttribute("src", "/images/view_item_details.gif");
		detImage.setAttribute("alt", "View " + prodTitle + " Details");
		detLink.appendChild(detImage);

		itemDesc = document.createElement("p");
		itemDesc.appendChild(document.createTextNode(prodDesc));
		
		itemPrice = document.createElement("p");
		if (listPriceObj.getAttribute("show") == "yes"){
			listPriceSpan = document.createElement("span");
			listPriceSpan.appendChild(document.createTextNode("List Price: " + listPrice));
			listPriceSpan.setAttribute("style", "color: #990000; font-weight: bold;");
			
			itemPrice.appendChild(listPriceSpan);
			itemPrice.appendChild(document.createElement("br"));
		}
		
		if (prodPriceFlag == "call"){
			itemPriceCall = document.createElement("img");
			itemPriceCall.setAttribute("src", "/images/callforpricing-small.gif");
			itemPriceCall.onmouseover = function(){ddrivetip("We're unable to publish our current price for this item because it is below the minimum advertised price allowed by the manufacturer. Please call our showroom to purchase this item.", "white", 240);}
			itemPriceCall.onmouseout = function(){hideddrivetip();}
			itemPrice.appendChild(itemPriceCall);
		}
		else if (prodPriceFlag == "callNoPop"){
			itemPriceCall = document.createElement("img");
			itemPriceCall.setAttribute("src", "/images/callforpricing-small.gif");
			itemPrice.appendChild(itemPriceCall);
		}
		else if (prodPriceFlag == "email" || prodPriceFlag == "add"){
			itemPriceBold = document.createElement("strong");
			itemPriceBold.appendChild(document.createTextNode(prodPrice));
			itemPrice.appendChild(document.createTextNode("Priced from "));
			itemPrice.appendChild(itemPriceBold);
			
			if (loggedIn == "true"){
				betterDiv = document.createElement("div");
				betterDiv.setAttribute("class", "betterPrice");
				betterDiv.setAttribute("className", "betterPrice");
				
				betterSpan = document.createElement("span");
				betterSpan.appendChild(document.createTextNode("Better Price: "));
				betterSpan.setAttribute("class", "better");
				betterSpan.setAttribute("className", "better");
				
				priceSpan = document.createElement("span");
				priceSpan.appendChild(document.createTextNode(betterPrice));
				priceSpan.setAttribute("class", "price");
				priceSpan.setAttribute("className", "price");
				
				betterDiv.appendChild(betterSpan);
				betterDiv.appendChild(priceSpan);
				
				itemPrice.appendChild(betterDiv);
			}
			else if (prodPriceFlag == "email" ){
				itemPriceEmail = document.createElement("a");
				itemPriceEmail.setAttribute("href", prodLink.replace("detail", "emailPrice").replace("Group_ID", "group").replace("Product_ID", "prod"));
				itemPriceEmail.onmouseover = function(){ddrivetip("We're unable to publish our current price for this item because it is below the minimum advertised price allowed by the manufacturer. To get your instant price, Click Here.", "white", 240);}
				itemPriceEmail.onmouseout = function(){hideddrivetip();}
				itemPriceEmail.onclick = function(){
						MM_openBrWindow(this.href, "enlarged", "resizable=yes,scrollbars=yes,width=350,height=350"); 
						return false;
					}
				itemPriceEmailImg = document.createElement("img");
				itemPriceEmailImg.setAttribute("src", "/images/requestprice-detail-click.gif");
				itemPriceEmailImg.setAttribute("border", "0");
				itemPriceEmail.appendChild(itemPriceEmailImg);
				itemPrice.appendChild(itemPriceEmail);
			}
			else {
				/*itemPriceEmail = document.createElement("a");
				itemPriceEmail.setAttribute("href", prodLink.replace("detail", "emailPrice").replace("Group_ID", "group").replace("Product_ID", "prod"));
				itemPriceEmail.onmouseover = function(){ddrivetip("We're unable to publish our current price for this item because it is below the minimum advertised price allowed by the manufacturer. To get your instant price, Click Here.", "white", 240);}
				itemPriceEmail.onmouseout = function(){hideddrivetip();}
				itemPriceEmail.onclick = function(){
						MM_openBrWindow(this.href, "enlarged", "resizable=yes,scrollbars=yes,width=350,height=350"); 
						return false;
					}*/
				itemPriceEmailImg = document.createElement("img");
				itemPriceEmailImg.setAttribute("src", "/images/requestprice-prods.gif");
				itemPriceEmailImg.setAttribute("border", "0");
				/*itemPriceEmail.appendChild(itemPriceEmailImg);
				itemPrice.appendChild(itemPriceEmail);*/
				itemPrice.appendChild(itemPriceEmailImg);
			}
		}
		else {
			itemPriceBold = document.createElement("strong");
			itemPriceBold.appendChild(document.createTextNode(prodPrice));
			itemPrice.appendChild(document.createTextNode("Starting at "));
			itemPrice.appendChild(itemPriceBold);
		}
		
		
		itemInfo.appendChild(itemTitle);
		itemInfo.appendChild(itemDesc);
		itemInfo.appendChild(detLink);
		itemInfo.appendChild(document.createTextNode(" "));
		itemInfo.appendChild(itemPrice);
		
		prodDiv.appendChild(itemImage);
		prodDiv.appendChild(itemInfo);
		
		allProdsDiv.insertBefore(prodDiv, insertBefore);
	}
	
	var brandsSel = document.getElementsByName("brand");
	if(brands.length > 0 && brandsSel.length > 0){
		while(brandsSel[0].firstChild)
			brandsSel[0].removeChild(brandsSel[0].firstChild);
		
		for (i = 0; i < brands.length; i++){
			brandTitle = brands[i].getElementsByTagName('title')[0].firstChild.nodeValue;
			while(brandTitle.indexOf("&amp;") != -1)
			 	brandTitle = brandTitle.replace("&amp;", "&");
			while(brandTitle.indexOf("&quot;") > -1)
			 	brandTitle = brandTitle.replace("&quot;", '"');
			brandValue = brands[i].getElementsByTagName('option-value');
			if (brandValue[0].firstChild)
				brandValue = brandValue[0].firstChild.nodeValue;
			else
				brandValue = "";
			brandSelected = brands[i].getElementsByTagName('current');
			if (brandSelected[0].firstChild)
				brandSelected = true;
			else
				brandSelected = false;
			thisBrand = brands[i].getElementsByTagName('brand-id')[0].firstChild.nodeValue;
			numProducts = brands[i].getElementsByTagName('num-products')[0].firstChild.nodeValue;
			
			b = document.createElement("option");
			b.setAttribute("thisBrand", thisBrand);
			b.setAttribute("value", brandValue);
			if (brandSelected)
				b.setAttribute("selected", "selected");

			b.appendChild(document.createTextNode(brandTitle + " (" + numProducts + ")"));
		
			brandsSel[0].appendChild(b);
		}
		
		brandsSel[0].onchange = function(){
			
			if (this.value == "0"){
				haveBrand = false;
				brandID = 0;
				brandURL = "";
			}
			else {
				brandLink = this.value;
				arBrandLink = brandLink.split("/");
				
				haveBrand = true;
				brandURL = "/" + arBrandLink[arBrandLink.length - 1];
				brandID = this.childNodes[this.selectedIndex].getAttribute("thisBrand");
			}
			
			updateFinderForm();
		}
	}
	
	
	var pricesSel = document.getElementsByName("price");
	if(prices.length > 0 && pricesSel.length > 0){
		while(pricesSel[0].firstChild)
			pricesSel[0].removeChild(pricesSel[0].firstChild);
		
		for (i = 0; i < prices.length; i++){
			priceTitle = prices[i].getElementsByTagName('title')[0].firstChild.nodeValue;
			while(priceTitle.indexOf("&amp;") != -1)
			 	priceTitle = priceTitle.replace("&amp;", "&");
			while(priceTitle.indexOf("&quot;") > -1)
			 	priceTitle = priceTitle.replace("&quot;", '"');
				
			priceValue = prices[i].getElementsByTagName('option-value');
			if (priceValue[0].firstChild)
				priceValue = priceValue[0].firstChild.nodeValue;
			else
				priceValue = "";
				
			priceSelected = prices[i].getElementsByTagName('current');
			if (priceSelected[0].firstChild)
				priceSelected = true;
			else
				priceSelected = false;
			thisPrice = prices[i].getElementsByTagName('price-id')[0].firstChild.nodeValue;
			numProducts = prices[i].getElementsByTagName('num-products')[0].firstChild.nodeValue;
			
			p = document.createElement("option");
			p.setAttribute("thisPrice", thisPrice);
			p.setAttribute("value", priceValue);
			if (priceSelected)
				p.setAttribute("selected", "selected");
			
			if (numProducts == 0){
				p.setAttribute("class", "unavailable");	
				p.setAttribute("className", "unavailable");	
			}
			
			p.appendChild(document.createTextNode(priceTitle + " (" + numProducts + ")"));
		
			pricesSel[0].appendChild(p);
		}
		
		pricesSel[0].onchange = function(){

			if (this.childNodes[this.selectedIndex].getAttribute("class") == "unavailable" || this.childNodes[this.selectedIndex].getAttribute("className") == "unavailable"){
				alert("No matching results found. Please choose another price range.");
				return false;
			}
			
			if (this.value == "0"){
				havePrice = false;
				priceID = 0;
				priceURL = "";
			}
			else {
				priceLink = this.value;
				arPriceLink = priceLink.split("/");
				
				havePrice = true;
				priceURL = "/" + arPriceLink[arPriceLink.length - 1];
				priceID = this.childNodes[this.selectedIndex].getAttribute("thisPrice");
			}
			
			updateFinderForm();
		}
	}
	
	if(allProdsDiv.getAttribute("doFade") == "false")
		allProdsDiv.removeAttribute("doFade")
	else
		Effects.fade("products", 0, 100, 1000, null);
	
}

function parseSubDivs(start, varName){
	if (typeof(window.parsedDivs) == 'undefined')
		window.parsedDivs = new Array();
		
	if (typeof(window.parsedDivs[varName]) != 'undefined')
		return window.parsedDivs[varName];
		
	window.parsedDivs[varName] = new Array();
	var divs = start.getElementsByTagName("div");
	
	for (i = 0; i < divs.length; i++){
		cn = divs[i].getAttribute("className");
		if (cn == null)
			cn = divs[i].getAttribute("class");
		
		if (typeof(window.parsedDivs[varName][cn]) == 'undefined')
			window.parsedDivs[varName][cn] = divs[i];
		else {
			if (typeof(window.parsedDivs[varName][cn]) == "object" && window.parsedDivs[varName][cn].constructor == Array){
				window.parsedDivs[varName][cn].push(divs[i]);
			}
			else {
				tempArray = new Array();
				tempArray.push(window.parsedDivs[varName][cn]);
				window.parsedDivs[varName][cn] = tempArray;
				window.parsedDivs[varName][cn].push(divs[i]);
			}
		}
	}
	
	return window.parsedDivs[varName];
}

function hashPolling(){
	if (location.hash == window.currentHash)
		return;
		
	LoadHistory(unescape(location.hash.substr(1)).replace("%2F", "/"));
}

shop_current_window_onload = window.onload;

function init_ajax(){
	if(navigator.userAgent.indexOf("WebKit") > -1) {
		window.currentHash = location.hash;
		if (window.currentHash != ""){
			LoadHistory(unescape(location.hash.substr(1)).replace("%2F", "/"));
		}
		window.hashPollTimeout = setTimeout(hashPolling, 100);
	}
	
	addEvents();
	
	if (shop_current_window_onload)
		shop_current_window_onload();
	
	if (doOnLoad)
		updateFinderForm(null, getStart(), false);
	//placeHelp();
}

var doOnLoad = true;
function dontOnLoad(){
	doOnLoad = false;
}

window.onload = init_ajax;

function placeHelp(){
	grps = document.getElementsByName('attributeGroup');
	
	gr = grps[0];
	child = gr.childNodes[1];
	att = child.getAttribute("offsetWidth");
	alert(att);
}

/*
	if (typeof window.ActiveXObject != 'undefined' ) {
		xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
		xmlDoc.onreadystatechange = function () {if ( xmlDoc.readyState == 4) {process();}}
	}
	else {
		xmlDoc = new XMLHttpRequest();
		xmlDoc.onload = process;
	}

	xmlDoc.open( "GET", baseURL + url + "?token=" + token, true );
	xmlDoc.send( null );*/
	/*var newEl = document.createElement('TABLE');
	newEl.setAttribute('cellPadding',5);
	var tmp = document.createElement('TBODY');
	newEl.appendChild(tmp);
	var row = document.createElement('TR');
	for (j=0;j<x[0].childNodes.length;j++)
	{
		if (x[0].childNodes[j].nodeType != 1) continue;
		var container = document.createElement('TH');
		var theData = document.createTextNode(x[0].childNodes[j].nodeName);
		container.appendChild(theData);
		row.appendChild(container);
	}
	tmp.appendChild(row);
	for (i=0;i<x.length;i++)
	{
		var row = document.createElement('TR');
		for (j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1) continue;
			var container = document.createElement('TD');
			var theData = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
			container.appendChild(theData);
			row.appendChild(container);
		}
		tmp.appendChild(row);
	}
	document.getElementById('writeroot').appendChild(newEl);*/
