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 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);
}
var t;
function searchSuggest(str) {
	if(str.length > 2) {
		if(t) clearTimeout(t);
		t = setTimeout("lookupterm()", 500);
	} else {
		if(t) clearTimeout(t);
		document.getElementById('searchlist').innerHTML = "";
		document.getElementById('searchlist').style.border = "1px solid #EAEBD8";
	}
}
function GoSearch() {
	var q = document.getElementById('wgo-search').value;
	if(q.length == 0) {
		alert("Please enter a keyword or item number.");
	} else if (q == "Keyword or Item #") {
		alert("Please enter a keyword or item number.");
	} else {
		window.location = "http://www.workgearonline.com/search.asp?query=" + q;
	}
}
function lookupterm() {
	var str = document.getElementById('wgo-search').value;
	var xmlHttp = newAjaxObject();
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState==4) {
			var obj = eval('(' + xmlHttp.responseText + ')');
			var num = obj.items.length;
			if(num > 0) {
				document.getElementById('searchlist').innerHTML = "";
				document.getElementById('searchlist').style.border = "1px solid #bbb";
				for(var i = 0; i < num; i++) {
					var listitem = document.createElement('li');
					var pc = obj.items[i].productcode.toUpperCase();
					var pn = (obj.items[i].productname.length <= 40) ? obj.items[i].productname : obj.items[i].productname.slice(0,40)+"...";
					var link = "http://www.workgearonline.com/" + pc.toLowerCase() + "/";
					link += obj.items[i].seopagename.replace(/ /g,"-") + ".htm";
					listitem.style.backgroundImage = "url('http://www.workgearonline.com/photos/" + pc + "-2S.jpg')";
					listitem.innerHTML = '<a href="'+link+'">'+pn+'</a>';
					if(i < num-1) listitem.style.borderBottom = "1px solid #ddd";
					document.getElementById('searchlist').appendChild(listitem); 
				}
			} else {
				document.getElementById('searchlist').innerHTML = "";
				document.getElementById('searchlist').style.border = "1px solid #EAEBD8";
			}
		}
	} 
	var strURL = "http://www.workgearonline.com/searchsuggest.asp?q=" + str;
	xmlHttp.open("GET",strURL,true);
	xmlHttp.send(null);
}
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 validateAddToCart(fm) {
	// Validate form
	boolValid = false;
	for(var i = 0; i < fm.elements.length; i++) {
		if(fm.elements[i].value.length != 0) {
			if (IsNumeric(fm.elements[i].value) == false || fm.elements[i].value == 0) {
				alert("Please enter a valid numeric quantity greater than 0.");
				return false;
			} else {
				if (IsNumeric(fm.elements[i].name)) boolValid = true;
			}
		}
	}
	if(boolValid) {
		return true;
	} else {
		alert("Please enter the quantities you desire to purchase.");
		return false;
	}
}
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() {
	document.getElementById("updatequantities").innerHTML = '<img src="/images/loading.gif" border="0">';
	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) {
		var xmlHttp = newAjaxObject();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState==4) {
				document.getElementById("shoppingcart").innerHTML=xmlHttp.responseText;
				document.getElementById("updatequantities").innerHTML = '<a href="javascript:updateCartItems()"><img src="/images/checkout/update_quantities.gif" border="0"></a>';
				highlightTotal();
			}
		} 
		xmlHttp.open("GET",strURL,true);
		xmlHttp.send(null);
	}
}
function showOrderDetails(orderid,e) {
	var rowIdx = e.parentNode.parentNode.rowIndex;
	var newRow = document.getElementById('orderTable').insertRow(rowIdx+1);
	newRow.bgColor = e.parentNode.parentNode.bgColor;
	var newCell = newRow.insertCell(0);
	newCell.colSpan = "4";
	newCell.innerHTML = "<img src='/images/loading.gif' />";
	var xmlHttp = newAjaxObject();
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState==4) {
			e.parentNode.innerHTML = e.value;
			newCell.innerHTML=xmlHttp.responseText;
		}
	} 
	xmlHttp.open("GET","orderdetails.asp?orderid="+orderid,true);
	xmlHttp.send(null);
}
function showOrderTracking(orderid,e) {
	var rowIdx = e.parentNode.parentNode.rowIndex;
	var newRow = document.getElementById('orderTable').insertRow(rowIdx+1);
	newRow.bgColor = e.parentNode.parentNode.bgColor;
	var newCell = newRow.insertCell(0);
	newCell.colSpan = "4";
	newCell.innerHTML = "<img src='/images/loading.gif' />";
	var xmlHttp = newAjaxObject();
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState==4) {
			e.parentNode.innerHTML = e.value;
			newCell.innerHTML=xmlHttp.responseText;
		}
	} 
	xmlHttp.open("GET","ordertracking.asp?orderid="+orderid,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;
}
function selectSecondOption(i,n) {
	document.getElementById('secondoption'+n).selectedIndex = 0;
	document.getElementById('productid').value = 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;
	for(var j = 0; j <= i; j++) {
		document.getElementById('secondoption'+j).style.display = "none";
	}
	document.getElementById('secondoption'+n).style.display = "inline";
	swapColor(clr);
}
var navtimeout = 200;
var ct;
var navitem;
function navopen(id) {	
	if(ct) clearTimeout(ct);
	if(navitem) {
		navitem.style.visibility = 'hidden';
		navitem.parentNode.style.backgroundColor = "";
	}
	navitem = document.getElementById(id);
	navitem.style.top = (findPosY(navitem.parentNode) - (navitem.clientHeight/2) + 10) + 'px';
	navitem.style.visibility = 'visible';
	navitem.parentNode.style.backgroundColor = "#bbbbbb";
}
function navclose() {
	if(navitem) {
		navitem.style.visibility = 'hidden';
		navitem.style.top = (findPosY(navitem.parentNode)) + 'px';
		navitem.parentNode.style.backgroundColor = "";
	}
}
function findPosX(obj) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}
function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1) {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}
function navclosetimer() {
	ct = setTimeout(navclose, navtimeout);
}
function showShippingInfo() {
	document.getElementById('prodfeatures').style.display = "none";
	document.getElementById('shippinginfo').style.display = "block";
	document.getElementById('prodfeatures-tab').style.backgroundImage = "url('/images/layout/tab_off.gif')";
	document.getElementById('shippinginfo-tab').style.backgroundImage = "url('/images/layout/tab_on.gif')";
}
function showProductInfo() {
	document.getElementById('prodfeatures').style.display = "block";
	document.getElementById('shippinginfo').style.display = "none";
	document.getElementById('prodfeatures-tab').style.backgroundImage = "url('/images/layout/tab_on.gif')";
	document.getElementById('shippinginfo-tab').style.backgroundImage = "url('/images/layout/tab_off.gif')";
}
function showLargePhoto(x, y) {
	document.getElementById('largephotodiv').style.zIndex = "10";
	document.getElementById('largephotodiv').style.visibility = "visible";
	document.getElementById('largephotodiv').style.left = (findPosX(document.getElementById('productphotodiv')) + 256) + "px";
	document.getElementById('largephotodiv').style.top = (findPosY(document.getElementById('productphotodiv')) - 1) + "px";
	var offsetX = ((x - 254) * 3.2) - 150;
	var offsetY = ((y - 170) * 3.2) - 125;
	document.getElementById('largephoto').style.left = '-' + offsetX + 'px';
	document.getElementById('largephoto').style.top = '-' + offsetY + 'px';
	document.getElementById('photoinstructions').innerHTML = "Roll off to zoom out";
}
function hideLargePhoto() {
	document.getElementById('largephotodiv').style.zIndex = "-10";
	document.getElementById('largephotodiv').style.visibility = "hidden";
	document.getElementById('photoinstructions').innerHTML = "Click photo to zoom in";
}