// JavaScript Document
function ClearCart()
{
	var cookie_date = new Date ( );  // current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	var cookies = document.cookie.split("; ");
	for (x in cookies)
	{
		if (unescape(cookies[x]).substr(0,2) == "##")
			document.cookie=cookies[x]+";expires="+cookie_date.toGMTString();
	}
}

function ValidatePrice(elementID)
{
	var elementValue = document.getElementById(elementID).value.replace("$","");
	
	while (isNaN(elementValue) && elementValue.length>0)
	{
		elementValue = elementValue.substr(0,elementValue.length-1);
	}
	document.getElementById(elementID).value = elementValue;
	ValidateQty(elementID.replace("OFF-INPUT-PR","OFF-INPUT-RQ"));
	//alert(document.getElementById('TEST').value*1);
}

function ValidateQty(elementID)
{
	//alert(elementID);
	var elementValue = document.getElementById(elementID).value;
	while (isNaN(elementValue) && elementValue.length>0)
	{
		elementValue = elementValue.substr(0,elementValue.length-1);
	}
	document.getElementById(elementID).value = elementValue;
	//alert(document.getElementById('TEST').value*1);
	/////////////////////////////////
	var partnumber, description, condition, saleprice, listedprice, tag, row, quantity, valueQty;
	row=document.getElementById(elementID).parentNode.parentNode;
	for (x in row.childNodes)
	{
//		alert(row.childNodes[x].nodeName);
		if (row.childNodes[x].nodeName == "TD")
		{
//			alert(row.childNodes[x].id.substr(4,2));
			if (row.childNodes[x].id.substr(4,2) == "PN")
				partnumber = row.childNodes[x].innerHTML;
			else if (row.childNodes[x].id.substr(4,2) == "DE")
			description = row.childNodes[x].innerHTML;
			else if (row.childNodes[x].id.substr(4,2) == "QT")
			quantity = row.childNodes[x].innerHTML;
			else if (row.childNodes[x].id.substr(4,2) == "CD")
			condition = row.childNodes[x].innerHTML;
			else if (row.childNodes[x].id.substr(4,2) == "TG")
			tag = row.childNodes[x].innerHTML;
			else if (row.childNodes[x].id.substr(4,2) == "PR")
			{
				if (row.childNodes[x].firstChild.nodeName == "INPUT")
				{//offer row rather than purchase.  There will be another input box for the price
					listedprice = "make offer";
					saleprice = row.childNodes[x].firstChild.value;
					if (saleprice.length == 0 || isNaN(saleprice) || saleprice==0)
						saleprice="make offer";
				}
				else
				{
					listedprice = row.childNodes[x].innerHTML;
					saleprice = listedprice;
				}
			}
		}
	}
	if (Number(elementValue) > Number(quantity))
	{
		alert("Quantity availble is "+quantity+".\r\nPlease enter a number no greater than that. You may enter 0 to remove this item from your cart");
		document.getElementById(elementID).value = quantity;
	}
	else
	{
		setCookie("##"+partnumber+"##"+description+"##"+quantity+"##"+condition+"##"+tag+"##"+listedprice,saleprice+"^^"+elementValue,7);
		//alert("##"+partnumber+"##"+description+"##"+quantity+"##"+condition+"##"+tag+"##"+listedprice+"=="+saleprice+"^^"+elementValue);
	}
	//UpdateCart();
}

function convertToOffer(elementID)
{
	var partnumber, description, condition, listedprice, tag, row, quantity, fieldsFound;
	fieldsFound=0;
	row=document.getElementById(elementID).parentNode.parentNode;
	for (x in row.childNodes)
	{
//		alert(row.childNodes[x].nodeName);
		if (row.childNodes[x].nodeName == "TD")
		{
//			alert(row.childNodes[x].id.substr(4,2));
			if (row.childNodes[x].id.substr(4,2) == "PN")
			{
				partnumber = row.childNodes[x].innerHTML;
				fieldsFound=fieldsFound+1;
			}
			else if (row.childNodes[x].id.substr(4,2) == "DE")
			{
				description = row.childNodes[x].innerHTML;
				fieldsFound=fieldsFound+1;
			}
			else if (row.childNodes[x].id.substr(4,2) == "QT")
			{
				quantity = row.childNodes[x].innerHTML;
				fieldsFound=fieldsFound+1;
			}
			else if (row.childNodes[x].id.substr(4,2) == "CD")
			{
				condition = row.childNodes[x].innerHTML;
				fieldsFound=fieldsFound+1;
			}
			else if (row.childNodes[x].id.substr(4,2) == "TG")
			{
				tag = row.childNodes[x].innerHTML;
				fieldsFound=fieldsFound+1;
			}
			else if (row.childNodes[x].id.substr(4,2) == "PR")
			{
				listedprice = row.childNodes[x].innerHTML;
				fieldsFound=fieldsFound+1;
			}
		}
	}
	if (fieldsFound == 6)
	{//found all we need to convert to an offer
		setCookie("##"+partnumber+"##"+description+"##"+quantity+"##"+condition+"##"+tag+"##"+listedprice,listedprice+"^^"+0,7);
		setCookie("##"+partnumber+"##"+description+"##"+quantity+"##"+condition+"##"+tag+"##make offer","make offer^^"+quantity,7);
	}
	else
		alert("There was a probelm converting this item to an offer.\r\nPlease contact the webmaster");
		
		//alert("##"+partnumber+"##"+description+"##"+quantity+"##"+condition+"##"+tag+"##"+listedprice+"=="+saleprice+"^^"+elementValue);
}


function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=escape(c_name)+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
c_name = escape(c_name);
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
	{ 
	c_start=c_start + c_name.length+1; 
	c_end=document.cookie.indexOf(";",c_start);
	if (c_end==-1) c_end=document.cookie.length;
	return unescape(document.cookie.substring(c_start,c_end));
	} 
  }
return "";
}

function ToggleCartCheck(row)
{
	//"this.cells[0].firstChild.checked=true;"
	var chkbox;
	//if (row.nodeName == "INPUT")
		//chkbox = row;
	//else
		chkbox = row.cells[0].firstChild;
		
	if (chkbox.checked == true)
		chkbox.checked = false;
	else
		chkbox.checked = true;
	ProcessRowSelectionChange(chkbox);
}

function CancelHandlers(evt)
{
	if (window.event)
		window.event.cancelBubble = true; //IE
	else
		evt.cancelBubble = true;//FF
}

function ProcessRowSelectionChange(chkbox)
{
	var partnumber, description, condition, price, tag, row, quantity, valueQty;
	partnumber="NPN"
	description="PART"
	condition="UNK"
	price="make offer"
	tag="n/a"
	quantity="1"
	valueQty=1
	row=chkbox.parentNode.parentNode;
	for (x in row.childNodes)
	{
		if (row.childNodes[x].id == "PN")
			partnumber = row.childNodes[x].firstChild.innerHTML;
		else if (row.childNodes[x].id == "DE")
		description = row.childNodes[x].innerHTML;
		else if (row.childNodes[x].id == "QT")
		quantity = row.childNodes[x].innerHTML;
		else if (row.childNodes[x].id == "CD")
		condition = row.childNodes[x].innerHTML;
		else if (row.childNodes[x].id == "TG")
		tag = row.childNodes[x].innerHTML;
		else if (row.childNodes[x].id == "PR")
		price = row.childNodes[x].firstChild.innerHTML;
	}
	if (chkbox.checked==false)
		valueQty=0;
	else
		valueQty = quantity;
	setCookie("##"+partnumber+"##"+description+"##"+quantity+"##"+condition+"##"+tag+"##"+price,price+"^^"+valueQty,7);
	UpdateCart();
}

function UpdateCart()
{
	var itemcount =0;
	var cookies = unescape(document.cookie).split("; ");
	for (x in cookies)
	{
			if (cookies[x].substr(0,2) == "##" && cookies[x].substr(cookies[x].length-1,1) > 0)
				itemcount = itemcount+1;
	}
	document.getElementById("CartQTY").innerHTML = itemcount;
	/*	if (itemcount == 0)
		document.getElementById("ShoppingCart").style.visibility="hidden";
	else
		document.getElementById("ShoppingCart").style.visibility="visible";*/
}
UpdateCart();
