var totalling=0;
//date last modified: 2005-02-17 by 黄文海
/*
by: 黄文海
*/
Number.prototype.toPrecision=function(numerOfDigitsAfterDot){
	var dotPos;
	var number;
	number=String(this);
	dotPos=number.indexOf(".");
	if(-1==dotPos){
		return this;
	}
	var digitsAfterDot;
	 digitsAfterDot=number.substring(dotPos+1);
	digitsAfterDot=digitsAfterDot.substring(0,numerOfDigitsAfterDot);
	return parseFloat(number.substring(0,dotPos)+"."+digitsAfterDot);

}

function refreshSubTotalling(id){
/*
版权：北京无忧电脑技术开发有限公司南京分公司
CopyRight:2001-2004 wyks8.com All rights reserved
创建：黄文海
Created by:hengzhe
创建时间：2004-11-08
Created on:
功能：购物车中修改商品数量时，重新计算被修改数量项目的小计数值。
Function:
	recalculate the subtotal of the item with quantity being modified.(apply to shoppingcart)
输入参数：商品编号，表示要修改数量的商品项目。
Parameters:
	id--commodity id,indicates  on which item the quantity modification is performed.
返回值：
Return Value: None
*/
	//totalling
	var newValue=parseFloat(document.getElementById('price'+id).innerText)*parseInt(document.getElementById('quantity'+id).value);
	document.getElementById('subtotoal'+id).innerText=newValue+"元";
	document.getElementById('subtotoal'+id).title=newValue;
	refreshTotalling();
	return(newValue);
}

function refreshTotalling(){
/*
版权：北京无忧电脑技术开发有限公司南京分公司
CopyRight:2001-2004 wyks8.com All rights reserved
创建：黄文海
Created by:hengzhe
创建时间：2004-11-08
Created on:
功能：重新计算购物车商品的总额。
Function:
输入参数：无
Parameters: 
返回值：无
Return Value:
*/	
	var list=document.getElementById('list');
	totalling=0;
	for(var i=1;i<list.rows.length;i++){
	//alert(list.rows[i].cells[5].title);
		if(list.rows[i].style.display=='') totalling+=parseFloat(list.rows[i].cells[5].title);
	}
		//status=(totalling);
	document.getElementById('totalling').innerText=totalling.toPrecision(2);	

}
function shopping(action,id,quantity,batchOperation){
/*
版权：北京无忧电脑技术开发有限公司南京分公司
CopyRight:2001-2004 wyks8.com All rights reserved
创建：黄文海
Created by:hengzhe
创建时间：2004-11-08
Created on:
功能：向后台服务程序shoppingcart.logic.asp发送购物车操作请求。
Function:
输入参数：action--操作类型，如addItem表示往购物车中添加项目。 id--商品编号  quantity--商品数量 batchOperation--是否是批操作（删除）
Parameters:
返回值：无
Return Value:
调用实例：
Call sample: shopping('addItem',32,2,false);----将编号为32的商品添加2个到购物车中。
*/
	try{
	//var xmlDoc=new ActiveXObject("Msxml2.DOMDocument");
		var xml=new XML();
		xml.init(0);
		var xmlDoc=xml.dom;
		xmlDoc.async=false;
		var qurString;
		if(batchOperation==null) batchOperation=false;
		qurString="/netstore/shoppingcart.logic.asp?action="+action+"&id="+id;
		if(action=='addItem' || action=='modifyQuantity')
			qurString+="&Quan="+quantity;
		qurString+="&batch="+batchOperation;
		//window.open(qurString,"_blank","");
		//status=qurString;
		var r=xmlDoc.load(qurString);
		if(r){
			var root=xmlDoc.documentElement;
			var info=xmlDoc.selectSingleNode("/feedback/alert").text;
			//alert(info);
			if(info!=''){
				var re=/先/i;
				if(info.search(re)==-1)
					info='【'+event.srcElement.title+'】'+info;
			//window.open("/netstore/sc.asp");
				if(action=='addItem'){
					info+="\n确定要查看购物车？";
					if(confirm(info)) window.open("/netstore/sc.asp","_blank","");
						return;
				}
			//alert(info);
				status=info;
			}
		}
		else
			//alert("抱歉，程序处理您的请求时发生例外。");
			alert(xmlDoc.parseError.line+xmlDoc.parseError.reason+"\n\r"+xmlDoc.parseError.srcText);
		}
	catch(e){
		var errInfo;
		errInfo=e.name+"\n";
		errInfo+="Number:"+e.number+"\n";
		errInfo+=e.message;
		alert(errInfo);	
	}
}

function batchRemove(t){
/*
版权：北京无忧电脑技术开发有限公司南京分公司
CopyRight:2001-2004 wyks8.com All rights reserved
创建：黄文海
Created by:hengzhe
创建时间：2004-11-08
Created on:
功能：批量删除购物车中的商品项目。
Function:
输入参数：t--0,表删除选定的项目 1，表示清空购物车
Parameters:
返回值：info--操作的结果
Return Value:
调用实例：
Call sample:
*/
			/*
		Since IE5 does not support push method of Array,we patch it!
		*/
		Array.prototype.push=function(){
			this[this.length]=arguments[0];
		}

		var l=document.getElementById("list");
		var selectors=document.getElementsByName('del');
		var selectorCount=selectors.length;
		var itemsToRemove=new Array();
		for(var i=0;i<selectorCount;i++){
			if(t==0){
				if(selectors[i].checked){
					itemsToRemove.push(selectors[i].value);
				}
			}
			else{
				itemsToRemove.push(selectors[i].value);
			}
		}
		var removeCount=itemsToRemove.length;
		for(var i=0;i<removeCount;i++){
				if(document.getElementById("item"+itemsToRemove[i])){
					l.tBodies[0].removeChild(document.getElementById("item"+itemsToRemove[i])); 
					shopping('removeItem',itemsToRemove[i],'',true);
				}

		}
	
		refreshTotalling();
		//info="批量删除成功！";


}

function checkModification(id){
//id indicates which item the user want to modify quantity.
//功能：检测修改商品数量时，输入的数据是否有效。
	var currentQuantity=document.getElementById('quantity'+id).value;
	var re;
	re=/^\d{1,3}$/;
	if(!re.test(currentQuantity)){
		alert('商品数量请输入整数！');
		return(false);
	}
	return(true);
}