﻿function pageLoad()
{
	if (document.getElementById("productFormats_0").checked)
	{
		var o = document.getElementById("productFormats_1");
		var i = 1;
		while (o)
		{
			o.disabled = "disabled";
			o = document.getElementById("productFormats_" + (++i));
		}
	}
}

function setBlendTop()
{
	var browser = navigator.appName;
	if (browser == "Safari")
		document.getElementById("resultsTable").style.top = "-4px";
	else
		document.getElementById("resultsTable").style.top = "-2px";
}

function textChangeEvent(o, label)
{
	if (o.value == "")
	{
		o.style.fontWeight = "bold";
		o.style.color = "#A0A0A0";
		o.style.fontSize = "11pt";
		o.value = label;
	}
	else
	{
		if (o.value == label)
		{
			o.style.fontSize = "10pt";
			o.style.fontWeight = "normal";
			o.style.color = "#000000";
			o.value = "";
		}
	}
}

function textFocusEvent(o, label)
{
	o.style.color = "#000000";
	o.style.fontWeight = "normal";
	o.style.fontSize = "10pt";
	if (o.value == label)
	{
		o.value = "";
	}
}

function textBlurEvent(o, label)
{
	if (o.value == "")
	{
		o.style.color = "#A0A0A0";
		o.style.fontWeight = "bold";
		o.style.fontSize = "10pt";
		o.value = label;
	}
}

function toggleFilters()
{
	var o = document.getElementById("filtersTable");
	var p = document.getElementById("up_dn_arrow");
	var q = document.getElementById("advancedFlag");
	if (o.style.display == "none")
	{
		q.checked = true;
		o.style.display = "";
		p.src = "images/filter_results_by_uparrow.png";
		if (document.getElementById("bubbleImg"))
			document.getElementById("bubbleImg").style.display = "none";
	}
	else
	{
		q.checked = false;
		o.style.display = "none";
		p.src = "images/filter_results_by_downarrow.png";
		if (document.getElementById("bubbleImg"))
			document.getElementById("bubbleImg").style.display = "";
	}
}

function changePageLinks(displayFilters)
{
	/* get all the anchor (<a>) elements */
	var anchors = document.getElementsByTagName("a")
	for (i = 0; i < anchors.length; i++)
		if (anchors[i].id.indexOf("nav_") == 0)
			if (anchors[i].style.cursor == "pointer")
				anchors[i].href = newHref(anchors[i].href, displayFilters);
}

function newHref(curHref, putFiltersDisplayParam)
{
	var hrefparts = curHref.split("=");
	var navigationStr = hrefparts[0];
	separator = "^^";
	var params = hrefparts[1].split(separator);
	var tblname = params[0];
	var navPage = params[1];
	var startPage = params.length > 2 ? params[2] : 0;
	return (navigationStr + "=" + tblname + separator + navPage + separator + startPage + separator + (putFiltersDisplayParam ? "1" : "0"));
}

function evalFormatSelect()
{
	if (document.getElementById("productFormats_0").checked)
	{
		concatCheckedItems(true);
		var i = 0;
		var o = document.getElementById("productFormats_" + (++i));
		while (o != null)
		{
			o.disabled = true;
			o = document.getElementById("productFormats_" + (++i));
		}
	}
	else
	{
		concatCheckedItems(false);
		var i = 0;
		var o = document.getElementById("productFormats_" + (++i));
		while (o != null)
		{
			o.disabled = false;
			o = document.getElementById("productFormats_" + (++i));
		}
	}
}

function concatCheckedItems(all)
{
	var txt = document.getElementById("txtCheckedFormatItems");
	txt.value = "";
	var labels = document.getElementsByTagName("label");
	var i = 0;
	var o = document.getElementById("productFormats_" + (++i));
	while (o != null)
	{
		var j = 0;
		if (navigator.appName == "Microsoft Internet Explorer")
		{
			while (labels[++j].getAttribute("htmlFor") != o.getAttribute("id"));
			txt.value += o.checked || all ? labels[j].innerText + "|" : "";
		}
		else
		{
			while (labels[++j].getAttribute("for") != o.getAttribute("id"));
			txt.value += o.checked || all ? labels[j].textContent + "|" : "";
		}
		o = document.getElementById("productFormats_" + (++i));
	}
}

function doFormatsDoctoring()
{
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		var font_size = "9pt";
		document.getElementById("txtAuthor").style.fontSize = font_size;
		document.getElementById("txtTranslator").style.fontSize = font_size;
		document.getElementById("txtIllustrator").style.fontSize = font_size;
		document.getElementById("txtSeries").style.fontSize = font_size;
		document.getElementById("txtPublisher").style.fontSize = font_size;
		document.getElementById("txtImprint").style.fontSize = font_size;
		document.getElementById("txtCopyright").style.fontSize = font_size;
		document.getElementById("txtEdition").style.fontSize = font_size;
		
		document.getElementById("divAdvanced").style.fontSize = font_size;
		document.getElementById("noteLabel").style.fontSize = font_size;
		document.getElementById("authorLabel").style.fontSize = font_size;
		document.getElementById("translatorLabel").style.fontSize = font_size;
		document.getElementById("illustratorLabel").style.fontSize = font_size;
		document.getElementById("seriesLabel").style.fontSize = font_size;
		document.getElementById("publisherLabel").style.fontSize = font_size;
		document.getElementById("imprintLabel").style.fontSize = font_size;
		document.getElementById("copyrightLabel").style.fontSize = font_size;
		document.getElementById("editionLabel").style.fontSize = font_size;
		document.getElementById("formatLabel").style.fontSize = font_size;
	}
	var o = document.getElementById("productFormats_0");
	if (o != null)
	{
		o.onclick = function() {evalFormatSelect();};
	}

	/* concatenate the text of all the "checked" checkboxes (with a delimiter, of course)
	   and put it in a hidden textbox. this is because the "selected" server side property
	   of a checkbox in the checkboxlist always returns false even if the checkbox is
	   checked. this has something to do with the way the checkboxlist is rendered to
	   a web form (checkboxes are created individually inside a table or a <span> element,
	   depending on the repeatlayout property). the checkboxes are not marked to runat="server"
	   and the way to see if they are "checked" at the server is to query the selected property
	   of the table or the <span> that represents the collection. unfortunately the collection
	   does not reflect the client side "checked" state unless it is databound, which is completely
	   useless and inappropriate here because of our 3 tier architecture. if any one finds
	   a better alternative (a trick that I missed), please let me know (jakedimano@gmail.com) */
	var i = 0;
	o = document.getElementById("productFormats_" + (++i));
	while (o != null)
	{
		o.onclick = function() {concatCheckedItems(false);};
		o = document.getElementById("productFormats_" + (++i));
	}
}

function toggleDesc(pRow, pParentRow, persistParent)
{
	var o = document.getElementById(pRow);
	var ip = document.getElementById(pRow + "ImageCellPad");
	var pr = document.getElementById(pParentRow);
	if (o.style.display == "none")
	{
		o.style.display = "";
		ip.style.display = "";
		if (!persistParent)
			pr.style.display = "none";
	}
	else
	{
		o.style.display = "none";
		ip.style.display = "none";
		if (!persistParent)
			pr.style.display = "";
	}
}

function toggleGeneral(chkBox)
{
	var chkGeneral = document.getElementById("chkGeneral");
	var chkTitle = document.getElementById("chkTitle");
	var chkSeriesTitle = document.getElementById("chkSeriesTitle");
	var chkAuthor = document.getElementById("chkAuthor");
	var chkTranslator = document.getElementById("chkTranslator");
	var chkIsbn = document.getElementById("chkIsbn");
	var chkDesc = document.getElementById("chkIncludeDescriptions");
	var searchTypes = document.getElementById("searchTypes");
	
	if (chkBox.id === chkGeneral.id)
	{
		if (chkBox.checked)
		{
			chkTitle.checked = false;
			chkSeriesTitle.checked = false;
			chkAuthor.checked = false;
			chkTranslator.checked = false;
			chkIsbn.checked = false;
			if (chkDesc.getAttribute("disabled") !== null)
				chkDesc.removeAttribute("disabled");
		}
	}
	
	if (chkBox.id === chkTitle.id || chkBox.id === chkSeriesTitle.id || chkBox.id === chkAuthor.id || chkBox.id === chkTranslator.id || chkBox.id === chkIsbn.id)
	{
		chkGeneral.checked = false;
		chkDesc.setAttribute("disabled", "disabled");
	}
	
	if (!chkTitle.checked && !chkSeriesTitle.checked && !chkAuthor.checked && !chkTranslator.checked && !chkIsbn.checked)
	{
		chkGeneral.checked = true;
		chkDesc.removeAttribute("disabled");
	}
	
	var searchTypeValues = (chkGeneral.checked?"chkGeneral=true|":"") + (chkTitle.checked?"chkTitle=true|":"") + (chkSeriesTitle.checked?"chkSeriesTitle=true|":"") + (chkAuthor.checked?"chkAuthor=true|":"") + (chkTranslator.checked?"chkTranslator=true|":"") + (chkIsbn.checked?"chkIsbn=true|":"") + (chkDesc.checked?"chkIncludeDescriptions=true":"");
	searchTypes.setAttribute("value", searchTypeValues);
}

function browseMouseOver(row)
{
	var highlightColor = "#5588CC";
	var forgroundColor = "#FFFFFF";
	
	document.getElementById("tdIsbn" + row).style.backgroundColor = highlightColor;
	document.getElementById("tdTitle" + row).style.backgroundColor = highlightColor;
	document.getElementById("tdAuthor" + row).style.backgroundColor = highlightColor;
	document.getElementById("tdListPrice" + row).style.backgroundColor = highlightColor;
	document.getElementById("tdDiscountString" + row).style.backgroundColor = highlightColor;
	document.getElementById("tdAddToCart" + row).style.backgroundColor = highlightColor;
	document.getElementById("tdAddToCart" + row).style.backgroundImage = "url('images/addtocart4short.png')";
	
	document.getElementById("tdIsbn" + row).style.color = forgroundColor;
	document.getElementById("tdTitle" + row).style.color = forgroundColor;
	document.getElementById("tdAuthor" + row).style.color = forgroundColor;
	document.getElementById("tdListPrice" + row).style.color = forgroundColor;
	document.getElementById("tdDiscountString" + row).style.color = forgroundColor;
	document.getElementById("tdAddToCart" + row).style.color = forgroundColor;
	
	//document.getElementById("tdDiscountString" + row).style.fontWeight = "bold";
	//document.getElementById("tdDiscountString" + row).style.fontSize = "9pt";
}

function browseMouseOut(row)
{
	var cellBackgroundColor = "#AACCFF";
	var foregroundColor = "#000000";
	
	document.getElementById("tdIsbn" + row).style.backgroundColor = cellBackgroundColor;
	document.getElementById("tdTitle" + row).style.backgroundColor = cellBackgroundColor;
	document.getElementById("tdAuthor" + row).style.backgroundColor = cellBackgroundColor;
	document.getElementById("tdListPrice" + row).style.backgroundColor = cellBackgroundColor;
	document.getElementById("tdDiscountString" + row).style.backgroundColor = cellBackgroundColor;
	document.getElementById("tdAddToCart" + row).style.backgroundColor = cellBackgroundColor;
	document.getElementById("tdAddToCart" + row).style.backgroundImage = "url('images/addtocart2words.png')";

	document.getElementById("tdIsbn" + row).style.color = foregroundColor;
	document.getElementById("tdTitle" + row).style.color = foregroundColor;
	document.getElementById("tdAuthor" + row).style.color = foregroundColor;
	document.getElementById("tdListPrice" + row).style.color = foregroundColor;
	document.getElementById("tdDiscountString" + row).style.color = foregroundColor;
	document.getElementById("tdAddToCart" + row).style.color = foregroundColor;
	
	//document.getElementById("tdDiscountString" + row).style.fontWeight = "normal";
	//document.getElementById("tdDiscountString" + row).style.fontSize = "10pt";
}

function setBrowseLeftBufferWidth()
{
	if (navigator.appName == "Microsoft Internet Explorer")
		winW = document.body.offsetWidth;
	else
		winW = window.innerWidth;
	
	var tblWidth = document.getElementById("resultsTable").style.width;
	var setWidth = ((winH - tblWidth ) / 2);
	return setWidth + "px";
}