//ajax中的httpRequest.open("GET", url, true);
//中的url参数不识别形如”../../cart.php"的地址

//cart.js文件位于include/js/文件夹下，但是通过smarty技术
//cart.js是被包含在根目录下的show.php文件中，所以cart.js中的
//httpRequest.open("GET", url, true);中的url参数值为cart.php，
//这个cart.php是根目录下的文件！！！！！！！！！！！！！

//由于samrty模板引擎不能作用于模板所包含的文件，这里被包含的文件是js文件，所以js文件当中另外定义全局变量siteURL，以便于程序移植
var siteURL="http://www.jy668.com/";
//购物车窗口的ID
var winID;
var xmlHttp;
//响应函数
function orderFinish()
{
	if ( xmlHttp.readyState == 4 ) {
	    var xmlResult = xmlHttp.responseText;
	    // 获取物品数,物品数可能为空（不是零）
		if(xmlResult>0)
		{
			alert("邮品已经加入购物车！");
			//如果购物车没有关闭，则购物成功后刷新购物车
			if(winID && winID.open &&!winID.closed)
			{
				winID.location.reload();
				//winID=window.open(page,null,strFeatures);
			}
		}
		
		//选择了重复的邮品
		else if(xmlResult<0)
		{
			var t;
			t=confirm("您已经订购了此项，您可在购物车中修改数量，现在打开购物车吗？");
			if(t)
				showDialog();
		}
		else
		{
			alert("出现错误导致Session混乱!");
		}
	}	
}

// 建立XMLHttpRequest对象
function getHttpRequestObject(handler) {
	// 建立XMLHttpRequest对象
	var httpRequest=null;
	if ( window.XMLHttpRequest) {
	   // IE7, Mozilla, Safari等浏览程序
	   httpRequest = new XMLHttpRequest(handler);
	} else if ( window.ActiveXObject ) { // IE5, IE6
	   // 找出最新版MSXML剖析器
	   var msxmls = [ "MSXML2.XMLHttp.4.0",
	                 "MSXML2.XMLHttp.3.0",
	                 "MSXML2.XMLHttp",
	                 "Microsoft.XMLHttp"];
	   for ( i=0; i< msxmls.length; i++ ) {
	      try {  // 建立XMLHttpRequest对象
	         httpRequest = new ActiveXObject(msxmls[i]);
	         break;
	      } catch ( e ) {  }
	   }
   }
   // 指定事件处理程序的名称
   httpRequest.onreadystatechange = handler;
   return httpRequest;
}
// 打开和发送非同步请求
function makeRequest(httpRequest, url) {
   httpRequest.open("GET", url, true); // 打开
   httpRequest.send(null);             // 发送
}

// 发送HTTP请求来获取信息
function sendYpId(id) {
   // 建立XMLHttpRequest对象
   xmlHttp = getHttpRequestObject(orderFinish);
   if ( xmlHttp != null ) {
      // 建立URL网址
      var url = "cartSession.php";
      url = url + "?yp_id=" + id;
      makeRequest(xmlHttp, url); // 建立HTTP请求
   }
   else {
      alert ("错误! 浏览程序不支持XMLHttpRequest对象!");
      return;
   }   
}
//显示购物车窗口
function showDialog(){
	page="goToMyCart.php";
	var strFeatures = "width=750,height=500,menubar=yes,toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes";
	if(winID && winID.open &&!winID.closed)
	{
		winID.close();
		winID=window.open(page,null,strFeatures);
	}
	else
		winID=window.open(page,null,strFeatures);

}

function cartClose(){
	window.close();
}
//firefox中改变输入框中的内容焦点不改变即可触发onchange事件，IE中则不行！！！
//onkeyup事件在body标签中则只要在页面中按键即可触发该事件，onkeyup还可以在input标签中，这时只针对此标签触发该事件！！！
function updateTotal(yp_id)
{
	var cssID="total_"+yp_id;
	//根据css 中的id得到值
	var total=document.getElementById(cssID).value;
	if(total<=0)
	{
		alert("数量必须大于零！");
		document.getElementById(cssID).value=1;
	}
	else
	{
		var url=siteURL+"cart/updateTotal.php?id="+yp_id+"&total="+total;
		//alert(url);
		window.location.replace(url);
	}
}
function deleteItem(id)
{
	if(confirm("您确实要删除此条邮品吗？"))
	{
		url="delete.php?id="+id;
		window.location.replace(url);
	}
}
function addItem(id)
{
		url="itemAddForm.php";
		window.location.replace(url);
}
