function shoppingTool()
{
	xmlHttp=GetXmlHttpObject();
	var acurrency = document.getElementById('currency').value;
	var url = '/currency.php';
	url=url+"?currency="+acurrency;
	url=url+"&rand="+Math.random();
	xmlHttp.onreadystatechange=calculate;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);		
}

function calculate(originalRequest)
{
	if (xmlHttp.readyState==4)
	{ 	
		var aStr = xmlHttp.responseText;
		//alert("calculate"+aStr);	
		var aArray = aStr.split(",");

		//Computer Tool
		var aQtys = aQty.split(",");
		var aPrices = aPrice.split(",");
		var aCurrent = document.getElementById("Qty").value;
		//alert(aQtys.length);
		for(var i=0;i<aQtys.length;i++)		
		{
			//JPY need integer
			if(aArray[3]=="JPY")
			{		
				var unitprice 		= Math.round(parseFloat(aPrices[i-1])*parseFloat(aArray[2]));
				var totalprice 		= Math.round(parseFloat(aPrices[i-1])*parseFloat(aArray[2])*parseFloat(aCurrent));
				var unitprice_last 	= Math.round(parseFloat(aPrices[i])*parseFloat(aArray[2]));
				var totalprice_last = Math.round(parseFloat(aPrices[i])*parseFloat(aArray[2])*parseFloat(aCurrent));
			}
			else
			{
				var unitprice 		= Math.round(parseFloat(aPrices[i-1])*parseFloat(aArray[2])*100)/100;
				var totalprice 		= Math.round(parseFloat(aPrices[i-1])*parseFloat(aArray[2])*parseFloat(aCurrent)*100)/100;
				var unitprice_last 	= Math.round(parseFloat(aPrices[i])*parseFloat(aArray[2])*100)/100;
				var totalprice_last = Math.round(parseFloat(aPrices[i])*parseFloat(aArray[2])*parseFloat(aCurrent)*100)/100;			
			}
			//money label position 0:font 1:back
			if(aArray[1]=="0")
			{			
				unitprice 		= aArray[0] + unitprice;
				totalprice 		= aArray[0] + totalprice;
				unitprice_last 	= aArray[0] + unitprice_last;
				totalprice_last = aArray[0] + totalprice_last;
			}
			else
			{
				unitprice 		= unitprice + aArray[0];
				totalprice 		= totalprice + aArray[0];
				unitprice_last 	= unitprice_last + aArray[0];
				totalprice_last = totalprice_last + aArray[0];
			}
			
			if(i==2 && aArray[7]=="" && parseFloat(aCurrent)>=parseFloat(aQtys[2]))
			{
				document.getElementById("Qty").value = parseFloat(aQtys[2])-1;
				aCurrent=parseFloat(aQtys[2])-1;
				document.getElementById("result").innerHTML="For pricing above <b>"+document.getElementById("Qty").value+"</b> pieces, please <a href=\"/sign-in.php\"><b>log in</b></a> now.<br>"+"<a name=\"shopping_price\"><b>"+unitprice+"</b></a> x "+aCurrent+" = <b><a name=\"shopping_total\">"+totalprice+"</b></a>";
				break;
			}
			
			if(parseFloat(aQtys[i])>parseFloat(aCurrent))
			{
				document.getElementById("result").innerHTML="<b>"+unitprice+"</b> x "+aCurrent+" = <b>"+totalprice+"</b>";
				break;
			}
			if(i==aQtys.length-1)
			{
				document.getElementById("result").innerHTML="<b>"+unitprice_last+"</b> x "+aCurrent+" = <b>"+totalprice_last+"</b>";
			}		
		}
		
		//Login Price
		var cb = document.getElementsByName("login_prices");
		var aCurrencyPrice="";
		//alert(cb.length);
		for(i=0;i<cb.length;i++)
		{
			if(aArray[7]!="")
			{				
				aCurrencyPrice = cb[i].rel;		
				if(aArray[1]=="0")
				{ 
					if(aArray[3]=="JPY")
					{
						cb[i].innerHTML = "<span style=\"color:#000;\">for</span> <b>"+aArray[0] + Math.round(parseFloat(aCurrencyPrice)*parseFloat(aArray[2]))+"</b> <span style=\"color:#000;\">each</span>";
					}
					else
					{
						cb[i].innerHTML = "<span style=\"color:#000;\">for</span> <b>"+aArray[0] + Math.round(parseFloat(aCurrencyPrice)*parseFloat(aArray[2])*100)/100+"</b> <span style=\"color:#000;\">each</span>";
					}
				}
				else
				{ cb[i].innerHTML = "<span style=\"color:#000;\">for</span> <b>"+Math.round(parseFloat(aCurrencyPrice)*parseFloat(aArray[2])*100)/100 + aArray[0]+"</b> <span style=\"color:#000;\">each</span>"; }
			}
			else
			{
				cb[i].innerHTML = '<b>Log In</b>';
				cb[i].href="/sign-in.php";
			}				
		}
	}
}

/*plus*/
function plus()
{
	document.getElementById('Qty').value++;
	shoppingTool();
}
/*minus*/
function minus()
{
	document.getElementById('Qty').value--;
	if(document.getElementById('Qty').value <1)
	{ 
		document.getElementById('Qty').value = 1;
	}
	shoppingTool();
}
/*qty*/
function qty_onkeyup()
{
	setTimeout('shoppingTool()',100);
	if(document.getElementById('Qty').value <1)
	{ 
		document.getElementById('Qty').value = 1;
	}
}
function qty_onkeydown()
{
	if(event.keyCode==13)
	{	
		event.keyCode=9; 
	}
}
function qty_onkeypress()
{
	if((event.keyCode<48 || event.keyCode>57)) 
	{ event.returnValue=false; }
}

function prior_page()
{
	window.history.go(-1);
}
/*print*/
function doPrint() 
{
	bdhtml=window.document.body.innerHTML;
	sprnstr="<!--StartPrint-->";
	eprnstr="<!--EndPrint-->";
	prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
	prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
	window.document.body.innerHTML=prnhtml;
	window.print();
}
/*notify*/
function checkNotify(arel)
{
	var aStrs = arel.split("|");
	if(aStrs[0]=="signout")
	{ 
		if(confirm("This action need login,login now?"))
		{ window.location="http://www.esaleschina.com/sign-in.php?type=2&item="+aStrs[1]; return false; } 
		else 
		{ return false;}
	}
	else { return true; }
}