function OpenNewWindow(url, width, height)
{
	var randomnumber=Math.floor(Math.random()*5001);
	window.open(url, randomnumber,"top=10,left=10,menubar=0,resizable=1,scrollbars=1,width=" + width + ",height=" + height);
}
function newAjaxObject() {
	var xmlHttp;
	try  {  // Firefox, Opera 8.0+, Safari  
		xmlHttp = new XMLHttpRequest();  
	}
	catch (e)  {  // Internet Explorer 
	 try    {    
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");    
	 }
	  catch (e)  {    
		try  {      
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");      
		}
		catch (e) {          
			return false;      
		}    
	  }  
	} 
	return xmlHttp;
}
function IsNumeric(strString) //  check for valid numeric strings	
{
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;
   if (strString.length == 0) return false;
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}
function ajaxCartSubmit(strURL) {
	// Validate form
	if (document.getElementById("productid").value == 0) {
		alert("Please make a selection.");
		return false;
	}
	if (document.getElementById("productid").value == -1) {
		alert("Please select an item that is available.");
		return false;
	}
	if (IsNumeric(document.getElementById("quantity").value) == false) {
		alert("Please enter a valid quantity greater than 0.");
		return false;
	}
	if (document.getElementById("quantity").value == 0) {
		alert("Please enter a valid quantity greater than 0.");
		return false;
	}
	
	window.location = strURL + "&noajax=1";
	
	// Send product to cart
	/*document.getElementById("addbtn").style.display = "none";
	document.getElementById("addbtn2").style.display = "block";
	var xmlHttp = newAjaxObject();
	if(xmlHttp == false) {
		window.location = strURL + "&noajax=1";	
	}
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState==4) {
			if (xmlHttp.responseText.indexOf("Total: $") > 0) {
				document.getElementById("addbtn").style.display = "block";
				document.getElementById("addbtn2").style.display = "none";
				document.getElementById("cart").innerHTML=xmlHttp.responseText;
				//Display pop-up window with what was added and an option to close, go to cart, and checkout
				htmlstring = document.getElementById("quantity").value + " " + document.getElementById("pn").value;
				if (document.getElementById('productid').selectedIndex) htmlstring += " - " + document.getElementById('productid')[document.getElementById('productid').selectedIndex].innerHTML;
				if (document.getElementById("quantity").value > 1) htmlstring += " have";
				else htmlstring += " has";
				htmlstring += " been added to your cart."
				document.getElementById('cartpopinfo').innerHTML = htmlstring;
				document.getElementById('cartpop').style.display = 'block';
				dropDownCart();
				//Reset form
				document.getElementById("productid").selectedIndex = 0;
				document.getElementById("quantity").value = 1;
			} else {
				document.getElementById("addbtn").style.display = "block";
				document.getElementById("addbtn2").style.display = "none";
				alert("Unable to add to cart. Product is not available.");
			}
		}
	} 
	xmlHttp.open("GET",strURL,true);
	xmlHttp.send(null);*/
}
function removeCartItem(btn,n) {
	// Remove product from cart
	// btn.disabled = true;
	var xmlHttp = newAjaxObject();
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("shoppingcart").innerHTML=xmlHttp.responseText;
		}
	} 
	var strURL = "cartremove.asp?ciid=" + n
	xmlHttp.open("GET",strURL,true);
	xmlHttp.send(null);
}
function updateCartItems() {
	var numItems = document.getElementById("cartrows").value;
	var strURL = "cartupdate.asp?rows=" + numItems;
	var error = false;
	for (n = 1; n <= numItems; n++) {
		qtystr = "quantity" + n;
		num = document.getElementById(qtystr).value;
		if (IsNumeric(num) == false) {
			alert("Please enter a valid quantity greater than 0.");
			error = true;
		}
		strURL += "&" + qtystr + "=" + num;
	}
	if (error == false) {
		//document.getElementById("updatequantities").innerHTML = "<img src='/images/update_quantities2.gif' width='241' height='55' border='0'>";
		var xmlHttp = newAjaxObject();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState==4) {
				document.getElementById("shoppingcart").innerHTML=xmlHttp.responseText;
				highlightTotal();
			}
		} 
		xmlHttp.open("GET",strURL,true);
		xmlHttp.send(null);
	}
}
var cp = -130;
function dropDownCart() {
	document.getElementById("cartpop").style.top = cp;
	cp += 15;
	if(cp < 165) setTimeout("dropDownCart()", 15);
}
function closePopup() {
	document.getElementById('cartpop').style.display = "none";
	document.getElementById('cartpop').style.top = -130;
	cp = -130;
}
function fadeHighlight(n) {
	if(n < 250) {
		document.getElementById('gtotal').style.backgroundColor = "rgb(255,255,"+n+")";
		setTimeout("fadeHighlight("+(n+10)+")", 50);
	}
}
function highlightTotal() {
	document.getElementById('gtotal').style.backgroundColor = "rgb(255,255,150)";
	setTimeout("fadeHighlight(100)", 100);
}
function selectProductID(x) {
	document.getElementById('productid').value = x;
	loadAvailability(x);
}
function selectSecondOption(i,n) {
	document.getElementById('secondoption'+n).selectedIndex = 0;
	document.getElementById('productid').value = 0;
	loadAvailability(0);
	for(var j = 0; j <= i; j++) {
		document.getElementById('secondoption'+j).style.display = "none";
	}
	document.getElementById('secondoption'+n).style.display = "inline";
}
function selectSecondOptionColor(i,n,clr) {
	document.getElementById('firstoption').selectedIndex = n;
	document.getElementById('secondoption'+n).selectedIndex = 0;
	document.getElementById('productid').value = 0;
	loadAvailability(0);
	for(var j = 0; j <= i; j++) {
		document.getElementById('secondoption'+j).style.display = "none";
	}
	document.getElementById('secondoption'+n).style.display = "inline";
	swapColor(clr);
}
function swatch(pc,color) {
	document.getElementById(pc).src = "/photos/options/"+pc+"-"+color+"-R.jpg";
}
function loadAvailability(pid) {
	if(pid == 0 || IsNumeric(document.getElementById("quantity").value) == false || document.getElementById("quantity").value == 0) {
		document.getElementById('availtext').style.display = "none";
		document.getElementById("addbtn").style.display = "block";
		document.getElementById("addbtn2").style.display = "none";
	} else {
		document.getElementById('availtext').innerHTML = "<strong>Availability:</strong> <img src='/images/loading.gif' />";
		document.getElementById('availtext').style.display = "block";
		var xmlHttp = newAjaxObject();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState==4) {
				document.getElementById('availtext').innerHTML = xmlHttp.responseText;
				if(xmlHttp.responseText.indexOf("out of stock") > 0) {
					document.getElementById("addbtn").style.display = "none";
					document.getElementById("addbtn2").style.display = "block";
					
					if(document.getElementById('firstoption')) {
						var nSelect = document.getElementById('firstoption').selectedIndex;
						if(nSelect > 0) {
							document.getElementById('secondoption'+nSelect).selectedIndex = 0;
							document.getElementById('productid').value = 0;
						}
					}
					if(document.getElementById('productid')) {
						nSelect = document.getElementById('productid').selectedIndex;
						if(nSelect > 0) {
							document.getElementById('productid').selectedIndex = 0;
							document.getElementById('productid').value = 0;
						}
					}
				} else {
					document.getElementById("addbtn").style.display = "block";
					document.getElementById("addbtn2").style.display = "none";
				}
			}
		}
		var strURL = "/loadavailability.asp?pid=" + pid + "&qty=" + document.getElementById("quantity").value;
		xmlHttp.open("GET",strURL,true);
		xmlHttp.send(null);
	}
}
function adjustQty() {
	if(document.getElementById('productid').value > 0) loadAvailability(document.getElementById('productid').value);
}