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;
	}
	
	// Send product to cart
	//document.getElementById("addbtn").style.display = "none";
	//document.getElementById("addbtn2").style.display = "block";
	window.location = strURL + "&noajax=1";
	/*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
				var divpop = document.createElement('div');
				divpop.setAttribute('id', 'cartpop');
				htmlstring = "<table class='bodytext' border='0' cellpadding='5' cellspacing='0' width='270'>";
				htmlstring += "<tr><td colspan='2' align='right' style='padding:0px'><a href='javascript:closePopup()'>Close</a></td></tr>";
				htmlstring += "<tr><td width='110' valign='top'>";
				htmlstring += "<img src='/photos/" + document.getElementById("pc").value + "-0.jpg' width='100' height='100'>";
				htmlstring += "</td><td valign='top'>";
				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.<p><a href='/shoppingcart.asp'><b>View Cart</b></a> ";
				htmlstring += " &nbsp;&nbsp;&nbsp; <a href='https://www.overshoesonline.com/checkout.asp'><b>Checkout</b></a></p></td></tr></table>";
				divpop.innerHTML = htmlstring;
				document.body.appendChild(divpop);
				window.scrollTo(0,0);
				//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);
	}
}
function closePopup() {
	document.body.removeChild(document.getElementById("cartpop"));
}
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 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";
	} 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";
					
					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";
				}
			}
		} 
		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);
}
function popupImage(w,h,url) {
	var height = document.body.clientHeight;
    var width = document.body.clientWidth;
	var divpop = document.createElement('div');
	divpop.setAttribute('id', 'popupImage');
	document.body.appendChild(divpop);
	divpop.innerHTML = "&nbsp;";
	divpop.style.height = height +'px';
	divpop.style.opacity = 0.5;
	var divpop2 = document.createElement('div');
	divpop2.setAttribute('id', 'popupPhotoDiv');
	divpop2.innerHTML = "<div class='popPhoto'><img src='"+url+"' width='"+w+"' height='"+h+"' border='0' onClick='closePopupImage()' /><br /><a href='javascript:closePopupImage()'>Close Photo</a></div>";
	document.body.appendChild(divpop2);
}
function closePopupImage() {
	document.body.removeChild(document.getElementById("popupImage"));
	document.body.removeChild(document.getElementById("popupPhotoDiv"));
}
function sendGCEmail() {
	if (document.getElementById("gcfromemail").value == "") {
		alert("Please enter your email address.");
		return false;
	}
	if (document.getElementById("gcrecipientemail").value == "") {
		alert("Please enter your recipient's email address.");
		return false;
	}
	var str;
	str = document.getElementById("gcfromemail").value
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	    alert("Please enter a valid Email Address.");
	    return (false);
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	    alert("Please enter a valid Email Address.");
	    return (false);
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Please enter a valid Email Address.");
	    return (false);
	}
	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("Please enter a valid Email Address.");
	    return (false);
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Please enter a valid Email Address.");
	    return (false);
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("Please enter a valid Email Address.");
	    return (false);
	 }
	 if (str.indexOf(" ")!=-1){
	    alert("Please enter a valid Email Address.");
	    return (false);
	 }
	str = document.getElementById("gcrecipientemail").value;
	lat = str.indexOf(at);
	lstr = str.length;
	ldot = str.indexOf(dot);
	if (str.indexOf(at)==-1){
	    alert("Please enter a valid Email Address.");
	    return (false);
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	    alert("Please enter a valid Email Address.");
	    return (false);
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Please enter a valid Email Address.");
	    return (false);
	}
	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("Please enter a valid Email Address.");
	    return (false);
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Please enter a valid Email Address.");
	    return (false);
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("Please enter a valid Email Address.");
	    return (false);
	 }
	 if (str.indexOf(" ")!=-1){
	    alert("Please enter a valid Email Address.");
	    return (false);
	 }

	var xmlHttp = newAjaxObject();
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState==4) {
			document.getElementById("emailnotify").innerHTML=xmlHttp.responseText;
		}
	} 
	var strURL = "emailgc.asp?n="+document.getElementById("gcid").value+"&f="+document.getElementById("gcfromemail").value+"&t="+document.getElementById("gcrecipientemail").value;
	xmlHttp.open("GET",strURL,true);
	xmlHttp.send(null);
}