function runStockCheck(catalog_id) {
	var zip = getZip('zipcode');
	if (checkZip(zip)) {
		returnObjById('check_'+catalog_id).innerHTML = "<b class=\"red\">Checking Availability...<b>";
		var params = '?catalog_id='+catalog_id+"&zip="+zip;
		var xmlHttp = getXmlHttpObject();
		xmlHttp.onreadystatechange=function(){handleStockReply(xmlHttp,catalog_id);}
		xmlHttp.open("GET",'/shop/stockCheck.php'+params,true);
		xmlHttp.send(null);
		return true;
	}
	else {
		zipPrompt('first');
		return false;
	}
}

function checkMaxQty(catalog_id) {
	var qtyLookup = getXmlHttpObject();
	var params = '?catalog_id='+catalog_id;
	//qtyLookup.onreadystatechange=function(){handleQtyCheck(qtyLookup,catalog_id);}
	qtyLookup.open("GET",'/shop/qtyLookup.php'+params,false);
	qtyLookup.send(null);
	var max_qty = parseInt(trimValue(returnObjById(catalog_id+'_max_qty').value));
	var page = document.location.href;
	// If we're on a product page, subtract amount currently in cart
	if (page.indexOf("updatecart") == -1 && page.indexOf("viewcart") == -1 && page.indexOf("remove") == -1)
		var max_available = max_qty - parseInt(qtyLookup.responseText);
 	else
		// If we're in the cart, we don't care how many they previously added
		var max_available = max_qty;
	//alert("max qty is "+max_qty);
 	var added_qty = parseInt(returnObjById(catalog_id+'_qty').value);
 	if (max_available < added_qty) {
 		alert('Sorry, there are only '+max_qty+' of this item currently available.');
 		returnObjById(catalog_id+'_qty').value = max_available;
 		return false;
 	}
 	else
 		return true;
}

function getZip(c_name) {
if (document.cookie.length>0) {
  var c_start = document.cookie.indexOf(c_name + "=");
  if (c_start != -1) { 
    c_start=c_start + c_name.length+1; 
    var c_end = document.cookie.indexOf(";",c_start);
    if (c_end == -1) c_end=document.cookie.length;
    	return document.cookie.substring(c_start,c_end);
    } 
  }
return false;
}

// Open zip code prompt window. 
function zipPrompt(time) {
	if (time == 'first')
		var zipwindow = dhtmlwindow.open('zipwindow', 'div', 'zipform', 'Please enter your zipcode', 'width=300px,height=100px,left=300px,top=200px,resize=0,scrolling=1');
	else {
		alert('Your zipcode is not in a valid format. Please enter your 5 digit shipping zipcode.');
		var zipwindow = dhtmlwindow.open('zipwindow', 'div', 'zipform', 'Please enter your zipcode', 'width=300px,height=100px,left=300px,top=200px,resize=0,scrolling=1');
	}
}

function setZip(zip) {
	if (!checkZip(zip)) {
		if (zip == 'NULL')
			return false;
		zipPrompt('second');
	}
	else {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+365);
		returnObjById('zipwindow').close();
		document.cookie="zipcode=" +zip +";path=\/;expires="+exdate.toGMTString();
		return true;
	}
}

function checkZip(zip) {
	var pattern = /^\d\d\d\d\d$|^\w\w\w \w\w\w/;
	if (pattern.test(zip))
		return true;
	else
		return false;
}

function handleStockReply(xmlHttp,catalog_id) { 
	if (xmlHttp.readyState==4) {
		//alert(xmlHttp.responseText);
		var qty = parseInt(xmlHttp.responseText);
		//alert(qty);
		if (qty == 0 || isNaN(qty)) {
			returnObjById('check_'+catalog_id).innerHTML = "<b class=\"red\">Out of Stock</b>";
			toggleDiv(catalog_id+'_cart', 'hide');
		}
		else {
			returnObjById(catalog_id+'_max_qty').value = qty;
			var page = document.location.href;
			// The following should not occur when viewing the cart, so we'll make sure we're not there
			if (page.indexOf("updatecart") == -1 && page.indexOf("viewcart") == -1 && page.indexOf("remove") == -1) {
				toggleDiv('check_'+catalog_id,'hide');
				returnObjById(catalog_id+'_display_qty').innerHTML = qty;
				toggleDiv(catalog_id+'_cart','show'); 
			}
		}
	}
}

function getXmlHttpObject() {
	var xmlHttp;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  return xmlHttp;
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    try
	      {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	    catch (e)
	      {
	      alert("Your browser is out of date and will not display this site. Please upgrade to a newer browser in order to continue.");
	      return false;
	      }
	    }
	  }
	  return xmlHttp;
}
 
function trimValue(string) {
    while (string.substring(0,1) == ' ' || string.substring(0,1) == '0') 
    	string = string.substring(1);
    while (string.substring(string.length-1,string.length) == ' ') 
    	string = string.substring(0,string.length-1);
    return string;
}

function returnObjById( id )
{
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
        var returnVar = document.layers[id];
    return returnVar;
}
