// JavaScript Document
function Right(str, n) {
	if (n <= 0)     // Invalid bound, return blank string
		return "";
	else if (n > String(str).length)   // Invalid bound, return
		return str;                     // entire string
	else { // Valid bound, return appropriate substring
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}

// Used to show/hide the categories on secondary pages
function expandCategories() {
	if (document.getElementById("nav").className == "categories-hide") {
		document.getElementById("expand-link").getElementsByTagName("a")[0].innerHTML = "Hide Categories";
		document.getElementById("nav").className = "categories-show";
	} else {
		document.getElementById("expand-link").getElementsByTagName("a")[0].innerHTML = "Show Categories";
		document.getElementById("nav").className = "categories-hide";
	}
}

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

function initTopLinks() {
	var arrAnchors = document.getElementsByTagName("a");
	for (var i=0; i<arrAnchors.length; i++) {
		if (arrAnchors[i].className.indexOf("top-link")!=-1) {
			arrAnchors[i].onclick = function() {
				window.scrollTo(0,0);
				return false;
			}
		}
	}
}

//quick order
function doQuickOrder(argForm) {
	var whichForm = argForm;
	var qoValue = argForm.quickorder.value;
	if (qoValue.length == 10) {
		//qoValue = stringMakeISBN(qoValue,10);
	argForm.quickorder.value = qoValue;
	}
	if (qoValue.length == 13) {
		//qoValue = stringMakeISBN(qoValue,13);
	argForm.quickorder.value = qoValue;
	}
	if (qoValue != '') {
		window.location.href = 'review.asp?ProductCode=' + qoValue;
	}
}

function stringMakeISBN(strValue, intType) {
	if (strValue.indexOf("-") == -1) {
		var strTempValue = "";
		if (intType==10) {
			for (var i=0; i<strValue.length; i++) {
				if ((i==1) || (i==5) || (i==9)) {
					strTempValue += "-";
				}
				strTempValue += strValue.charAt(i);
			}
			return strTempValue;
		}
		if (intType==13) {
			for (var i=0; i<strValue.length; i++) {
				if ((i==3) || (i==4) || (i==8) || (i==12)) {
					strTempValue += "-";
				}
				strTempValue += strValue.charAt(i);
			}
			return strTempValue;
		}
	} else {
		return strValue;
	}
}

function linkList(strURL) {
	prompt('Copy/Paste the following link into an email',strURL);
}

function saveOrder() {
	alert("Save order!");
}
function confirmDelete(strURL) {
	if (confirm("Are you sure you want to delete this item?")) {
		location = strURL;
	}
}
function doQuickSearch() {
	//if (document.getElementById("Search").value.length == 10) {
	//	document.getElementById("Search").value = stringMakeISBN(document.getElementById("Search").value,10);
	//}
	//if (document.getElementById("Search").value.length == 13) {
	//	document.getElementById("Search").value = stringMakeISBN(document.getElementById("Search").value,13);
	//}
	document.getElementById("Search").value = document.getElementById("Search").value;
	document.getElementById("leftColumnSearchForm").submit()
}

function addQuantity(strISBN) {
	if (document.getElementById("addQuantity"+strISBN).value != "") {
		location = 'Review.asp?ProductCode='+strISBN+'&Quantity='+document.getElementById("addQuantity"+strISBN).value+'&returnpage=yes';
//		alert('Review.asp?ProductCode='+strISBN+'&Quantity='+document.getElementById("addQuantity"+strISBN).value+'&returnpage=yes');
	} else {
		alert("You must provide a quantity.")
	}
}

addEvent(window, 'load', initTopLinks);

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);