//定义全局变量
var sOrderForm  = '24_OrderForm';

/*--有ID获取html对象--*/
function GetE( elementId )
{
	return document.getElementById( elementId )  ;		
}	

//<--Start--Cookie操作函数
//设定Cookie值
/*
function SetCookie(name, value)
{
	var expdate = new Date();
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
	document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
	+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
	+((secure == true) ? "; secure" : "");
}
*/

//获得Cookie解码后的值
function GetCookieVal(offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	
	if (endstr == -1)
		endstr = document.cookie.length;
		
	//return unescape(document.cookie.substring(offset, endstr));
	return decodeURIComponent(document.cookie.substring(offset, endstr));
}

function DelCookie(name)
//删除Cookie
{
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval +  ";path=/" + "; expires="+ exp.toGMTString();
}

function GetCookie_2(name)
//获得Cookie的原始值
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return GetCookieVal (j);
			
		i = document.cookie.indexOf(" ", i) + 1;
		
		if (i == 0) break;
	}
	
	return "";
}
//-->End

//<--Start--从cookie中读出订单数据的函数
function ReadOrderForm(name)
{
	var cookieString = document.cookie;
	if (cookieString == "")
	{
		return false;
	} else
	{
    var firstChar,lastChar;
    firstChar = cookieString.indexOf(name);
    if(firstChar != -1)
    {
      firstChar += name.length+1;
      lastChar = cookieString.indexOf(';', firstChar);
      
      if(lastChar == -1) 
      	lastChar = cookieString.length;
      	
      return cookieString.substring(firstChar,lastChar);
    } else
    {
    	return false;
    }
	}    
}
//-->End

//<--Start--添加商品至购物车的函数
function SetOrderForm(item_id,item_name,item_price)
{
  var cookieString = document.cookie;                       
  if ( cookieString.length >= 4000 )               //cookie不能超过4K
  {
  	alert("您的订单已满\n请结束此次订单操作后添加新订单！");
  } else
  {
    var mer_list    = ReadOrderForm(sOrderForm);
		var item_detail = item_id+"&"+item_name+"&"+item_price+"&1";
    
    if( mer_list == false )
    {
  		document.cookie = sOrderForm + "=" + encodeURIComponent(item_detail) +  ";path=/";
      alert("“"+item_name+"”\n"+"已经加入您的订单！");
      updateGoStr();
    } else
    {
      if (mer_list.indexOf(item_id) != -1)
      {
      	alert('此商品您已添加\n请进入订单修改数量！')
      } else
      {
      	item_detail = "||" + item_detail;
				document.cookie = sOrderForm + "=" + mer_list + encodeURIComponent(item_detail) +  ";path=/";
        alert("“"+item_name+"”\n"+"已经加入您的订单！");
        updateGoStr();
      }
    }
  }
}
//-->End

//<--Start--订单修改更新cookie的函数
function updateCookie()
{
/*  var limit=document.OrderForm.elements.length-3;
  if (document.OrderForm.update.value=="修改订单")
  {
    document.OrderForm.update.value="更新订单";
    document.OrderForm.submit.disabled=true;
    //<--
    for (i=1;i<limit;i++)
    {
      document.OrderForm.elements[i].disabled=false;
      i=i+2;
      document.OrderForm.elements[i].disabled=false;
    }
    //-->
  } else
  {
    document.OrderForm.update.value="修改订单"
    document.OrderForm.submit.disabled=false;
    //<--
    for (i=1;i<limit;i++)
    {
      document.OrderForm.elements[i].disabled=true;
      i=i+2;
      document.OrderForm.elements[i].disabled=true;
    }
    //-->
    var Then = new Date();
    Then.setTime(Then.getTime()+30*60*1000);
    var values=document.OrderForm.storage.value
    document.cookie = sOrderForm + "="+escape(values) +  ";path=/";
  }*/
}
//-->End

//<--Start--订单修改
function updateShoppingNum(nNum,id)
{
  var cookieString  = GetCookie_2(sOrderForm);
  var aShoppingList = cookieString.split("||");

	for ( i = 0;i < aShoppingList.length;i++ )
	{
		if(aShoppingList[i].indexOf(id)!=-1) 
		{
			aInfo = aShoppingList[i].split('&');
			aInfo[3] = nNum;
			
			new_Info = '';

			for (n=0;n < aInfo.length;n++  )
			{
				if (new_Info == '')
					new_Info = aInfo[n];
    		else	
    			new_Info = new_Info + "&" + aInfo[n];
			}
			
			aShoppingList[i] = new_Info;
		}
	}
	
  var new_ShoppingList = "";

	for (j = 0;j < aShoppingList.length;j++)
  {
  	if ( aShoppingList[j] != undefined ) 
  	{	
  		if ( new_ShoppingList == '' )    	
  			new_ShoppingList = aShoppingList[j];
  		else	
  			new_ShoppingList = new_ShoppingList + "||" + aShoppingList[j];
  	}		
  }
	document.cookie = sOrderForm + "=" + encodeURIComponent(new_ShoppingList) +  ";path=/";
	updateSubTotal(id);
	updateTotal();
}
//-->End

//<--Start--删除单个商品的函数
function Delete(TableID,id)
{
  if ( window.confirm("确定要删除吗？") )
  {
  	var cookieString  = GetCookie_2(sOrderForm);
  	var aShoppingList = cookieString.split("||");

  	for ( i = 0;i < aShoppingList.length;i++ )
  	{
  		if(aShoppingList[i].indexOf(id)!=-1) 
  			delete aShoppingList[i];
  	}

  	var new_ShoppingList = "";
  	for (i = 0;i < aShoppingList.length;i++)
    {
    	if ( aShoppingList[i] != undefined ) 
    	{	
    		if ( new_ShoppingList == '' )    	
    			new_ShoppingList = aShoppingList[i];
    		else	
    			new_ShoppingList = new_ShoppingList + "||" + aShoppingList[i];
    	}		
    }

    document.cookie = sOrderForm + "=" + encodeURIComponent(new_ShoppingList) +  ";path=/";
    GetE(TableID).deleteRow();
    updateTotal();
  }
}
//-->End

//<--Start--更新价格总计的函数
function updateTotal()
{
	var cookieString  = GetCookie_2(sOrderForm);
	
	if (cookieString == null || cookieString == '' )
		return false;
	
  var aShoppingList = cookieString.split("||");
	
	var nSum   = 0;
	var amount = 0;
	for ( i = 0;i < aShoppingList.length;i++ )
	{
		aInfo = aShoppingList[i].split('&');
		
		if ( aInfo[3] != null && aInfo[3] != '' && aInfo[3] != 0)
		{
			nSum   = nSum + (aInfo[3]-0);
			amount = amount + (aInfo[2] * aInfo[3]-0);
		} else
		{
			nSum = nSum - 0 + 1;
			amount = amount + (aInfo[2]-0);
		}
	}
	
	GetE('td_total').innerHTML = amount;
}
//-->End

//<--Start--更新价格小计的函数
function updateSubTotal(id)
{
  var cookieString  = GetCookie_2(sOrderForm);
  var aShoppingList = cookieString.split("||");

	for ( i = 0;i < aShoppingList.length;i++ )
	{
		if(aShoppingList[i].indexOf(id)!=-1) 
		{
			aInfo = aShoppingList[i].split('&');
			price = aInfo[2];
			
			if (aInfo[3] == null || aInfo[3] == 0 || aInfo[3] == '')
				nNum  = 1;
			else	
				nNum  = aInfo[3];
		}
	}
	
	nSubTotal = nNum * price;

	stdid = 'td_price_'+id;
	
	GetE(stdid).innerHTML = nSubTotal;

}
//-->End

//<--Start--更新storage的函数
function updateStorage(m_id,new_amount)
{
  if(isNaN(new_amount)||new_amount<1||new_amount.indexOf('.')!=-1)
  {
    alert("数量填写错误！")
    document.OrderForm.elements["aid"+m_id].focus();
    document.OrderForm.elements["aid"+m_id].select();
  } else
  {
    var the_list=document.OrderForm.storage.value;
    var single_list=the_list.split("|")
    
    for (i=1;i<single_list.length;i++)
    {
      if (single_list[i].indexOf(m_id)!=-1)
      {
        var old_single=single_list[i];
        var broken_single_list=single_list[i].split("&");
        broken_single_list[2]=new_amount;
        var new_single=broken_single_list[0]+"&"+broken_single_list[1]+"&"+broken_single_list[2]+"&"+broken_single_list[3];
        the_list=the_list.replace(old_single,new_single);
      }
    }
    document.OrderForm.storage.value=the_list;
  }
}
//-->End 

//<--Start--更新storage的函数
function updateGoStr()
{
	var cookieString  = GetCookie_2(sOrderForm);
	
	if (cookieString == null || cookieString == '' )
		return false;
	
  var aShoppingList = cookieString.split("||");
	
	var nSum   = 0;
	var amount = 0;
	for ( i = 0;i < aShoppingList.length;i++ )
	{
		aInfo = aShoppingList[i].split('&');
		
		if ( aInfo[3] != null && aInfo[3] != '' && aInfo[3] != 0)
		{
			nSum   = nSum + (aInfo[3]-0);
			amount = amount + (aInfo[2] * aInfo[3]-0);
		} else
		{
			nSum = nSum - 0 + 1;
			amount = amount + (aInfo[2]-0);
		}
	}
	
	sNewStr = '共' + nSum + '件产品合计' + amount + '元 进入购物车>>';
	
	GetE('gostr').innerHTML=sNewStr;
}
//-->End 
