// JavaScript Document

function borderOn(obj) {
	if (document.getElementById(obj)) document.getElementById(obj).style.border = '1px solid #CC0000';
}

function borderOff(obj) {
	if (document.getElementById(obj)) document.getElementById(obj).style.border = '1px solid #CCCCCC';
}

// Clears the field on focus if it is filled with the default "Search by Keyword" value
function clearEmptyFindForm(textfield) {
	if (textfield.value == "Find a Product") textfield.value = '';
}

// Changes the sort order of the find product page
function changeFindProductSortResultsPerPage(field) {
	location.href = field.value;
}

// Switches to another child product
function changeChild(form) {
	if (form.c) {
		// Hide existing child information
		document.getElementById("product-child").style.visibility = 'hidden';
		document.getElementById("waiting").style.visibility = 'visible';
		// Parse URL string and separate the query string into individual parts;
		var base = location.pathname;
		var qs = location.search.substring(1, location.search.length);
		qs = qs.replace(/\+/g, ' ');
		var args = qs.split('&');
		// Formulate new URL string
		var url = location.pathname + '?i=' + form.c.value;
		for (var i = 0; i < args.length; i++) {
			// Split name/value pair to get the variable name
			var pair = args[i].split('=');
			var name = unescape(pair[0]);
			if (name != 'i') url = url + '&' + args[i];
		}
		location.href = url;
	}
}

// Prints the shopping list
function printShoppingList() {
	var form = document.ShoppingList;
	if (form) if (form.hdnTotal) sendShoppingListTotal();
}

// Send the shopping list total to the ajax print page
function sendShoppingListTotal() {
	if (timer != null) clearTimeout(timer);
	if (request.readyState == 0 || request.readyState == 4) {
		var total_field = document.getElementById("hdnTotal");
		var total = parseFloat(total_field.value);
		scriptURL = "print.aspx?t=" + escape(total) + "&q=" + new Date().getTime();
		request.open("GET", scriptURL, true);
		request.onreadystatechange = endShoppingListTotal;
		request.send(null);
	} else timer = setTimeout(sendShoppingListTotal, 0);
}

// Stub function for finishing the shopping list send request 
function endShoppingListTotal() {
	if (request.readyState == 4) {
		window.print();
	}
}

// Switches the child product display back to the default condition
function resetDisplay() {
	if (document.getElementById("product-child") && document.getElementById("waiting")) {
		document.getElementById("product-child").style.visibility = 'visible';
		document.getElementById("waiting").style.visibility = 'hidden';
	}
}

// Opens a window with a full-size image view
function viewFullImage(id) {
	popup = window.open('image.aspx?i=' + id,'ImageViewer','width=547,height=600,resizable=yes,scrollbars=yes');
	if (window.focus) popup.focus();
}

// Opens a popup window to select a store
function setLocalStore() {
	popup = window.open('set_store.aspx', 'SetStore','width=530,height=345,status=no,scrollbars=no,toolbar=no,location=no,directories=no,resizable=no');
	if (window.focus) popup.focus();
}

// Changes the tab display
function displayTab(val) {
	if (val == 'P') {
		toggleTab(true	, "Promo"		, "promo_on.gif");
		toggleTab(false	, "Features"	, "features_off.gif");
		toggleTab(false	, "Swatches"	, "colors_styles_off.gif");
		toggleTab(false	, "Technical"	, "technical_description_off.gif");
		toggleTab(false	, "Warranty"	, "warranty_off.gif");
		toggleTab(false	, "Diagrams"	, "product_diagrams_off.gif");
	} else if (val == 'F') {
		toggleTab(false	, "Promo"		, "promo_off.gif");
		toggleTab(true	, "Features"	, "features_on.gif");
		toggleTab(false	, "Swatches"	, "colors_styles_off.gif");
		toggleTab(false	, "Technical"	, "technical_description_off.gif");
		toggleTab(false	, "Warranty"	, "warranty_off.gif");
		toggleTab(false	, "Diagrams"	, "product_diagrams_off.gif");
	} else if (val == "S") {
		toggleTab(false	, "Promo"		, "promo_off.gif");
		toggleTab(false	, "Features"	, "features_off.gif");
		toggleTab(true	, "Swatches"	, "colors_styles_on.gif");
		toggleTab(false	, "Technical"	, "technical_description_off.gif");
		toggleTab(false	, "Warranty"	, "warranty_off.gif");
		toggleTab(false	, "Diagrams"	, "product_diagrams_off.gif");
	} else if (val == "T") {
		toggleTab(false	, "Promo"		, "promo_off.gif");
		toggleTab(false	, "Features"	, "features_off.gif");
		toggleTab(false	, "Swatches"	, "colors_styles_off.gif");
		toggleTab(true	, "Technical"	, "technical_description_on.gif");
		toggleTab(false	, "Warranty"	, "warranty_off.gif");
		toggleTab(false	, "Diagrams"	, "product_diagrams_off.gif");
	} else if (val == "W") {
		toggleTab(false	, "Promo"		, "promo_off.gif");
		toggleTab(false	, "Features"	, "features_off.gif");
		toggleTab(false	, "Swatches"	, "colors_styles_off.gif");
		toggleTab(false	, "Technical"	, "technical_description_off.gif");
		toggleTab(true	, "Warranty"	, "warranty_on.gif");
		toggleTab(false	, "Diagrams"	, "product_diagrams_off.gif");
	} else if (val == "D") {
		toggleTab(false	, "Promo"		, "promo_off.gif");
		toggleTab(false	, "Features"	, "features_off.gif");
		toggleTab(false	, "Swatches"	, "colors_styles_off.gif");
		toggleTab(false	, "Technical"	, "technical_description_off.gif");
		toggleTab(false	, "Warranty"	, "warranty_off.gif");
		toggleTab(true	, "Diagrams"	, "product_diagrams_on.gif");
	}
}

function toggleTab(val, id, file) {
	if (document.getElementById('pn' + id)) {
		if (val) document.getElementById('pn' + id).className = "product-tabshow";
		else document.getElementById('pn' + id).className = "product-tabhide";
	}
	if (document.getElementById('hl' + id) && document.getElementById('img' + id)) {
		document.getElementById('img' + id).src = "/images/headers/" + file;
	}
}

// Toggles the display/non-display of the filter list
function toggleFilterList(link_elem, divname) {
	var div = document.getElementById(divname);
	if (div) {
		if (div.style.display == "none") {
			div.style.display = ""
			link_elem.className = "open";
		} else {
			div.style.display = "none"
			link_elem.className = "closed";
		}
	}
}

// Validates qty field
function validateQty(form) {
	if (form.txtQty) {
		if (isNaN(form.txtQty.value) || form.txtQty.value < 1 || form.txtQty.value > 9999 || form.txtQty.value.indexOf('.') != -1) {
			alert("Please enter a valid whole number for the quantity");
			return false;
		} else return true
	} else return true;
}
