﻿//判断是否为数字
function IsNum(s)
{
    if (s!=null && s!="")
    { 
        return !isNaN(s);
    }
    return false;
}

function CheckSubmit()
{
	var oBusiness = document.getElementById("business"); 
	oBusiness.value = business_email;
	//alert(oBusiness.value);
}
function IsExistThisProduct(productid, products)
{
	var aryProduct = products.split("|");

	for(var i=0; i<aryProduct.length; i++)
	{
		var aryProperty = aryProduct[i].split("&");
		for(var j=0; j<aryProperty.length; j++)
		{
			if(aryProperty[j].split("=")[0] == "productid")
			{
				if(aryProperty[j].split("=")[1] == productid) return true;
			}
		}
	}
	return false;
}
function GetValueByProperty(property, strValue)
{
	var aryProperty = strValue.split("&");

	for(var i=0; i<aryProperty.length; i++)
	{
		if(aryProperty[i].split("=")[0] == property)
			return aryProperty[i].split("=")[1];
	}
	return "";
}
function PrintCoupon(isPreview)
{
    var strCouponList = getCookie("mylist");
    if(strCouponList == "")
    {
        return;
    }
    var aryCoupon = strCouponList.split(";");
    var strCouponPic = "";
    for(i=0; i<aryCoupon.length; i++)
    {
        var json_Coupon = eval("("+aryCoupon[i]+")");
        if(i==0) 
            strCouponPic = json_Coupon.PIC + "|" + json_Coupon.QUANTITY;
        else
            strCouponPic += ";" + json_Coupon.PIC + "|" + json_Coupon.QUANTITY;
    }
document.getElementById("couponlist").value=strCouponPic; 
document.getElementById("preview").value=isPreview; 
document.form.target="aaa"; 
document.form.action="print.aspx"; 
document.form.method = "POST"; 
document.form.encoding="application/x-www-form-urlencoded"; 
document.form.submit(); 

    
}
function DownloadCoupon()
{
    var strCouponList = getCookie("mylist");
    if(strCouponList == "")
    {
        return;
    }
    var aryCoupon = strCouponList.split(";");
    var strCouponPic = "";
    for(i=0; i<aryCoupon.length; i++)
    {
        var json_Coupon = eval("("+aryCoupon[i]+")");
        if(i==0) 
            strCouponPic = json_Coupon.PIC;
        else
            strCouponPic += "," + json_Coupon.PIC;
    }
    //alert(strCouponPic);
    var ajax1 = new Ajax("getDownloadUrl.aspx",1,"couponlist="+strCouponPic, GetDownloadUrl);
    ajax1.post();    
    function GetDownloadUrl(downloadurl)
    {
        window.location = downloadurl;
    }
}

//quantity为0则为删除该记录
function UpdateCounpon(id, quantity)
{
//    var isAdd ="0";
    var strCouponList = getCookie("mylist");
    if(strCouponList == "")
    {
	    return;
    }
    ClearAllRows();
    var aryCoupon = strCouponList.split(";");
    var newCoupon = "";
    for(i=0; i<aryCoupon.length; i++)
    {
        var json_Coupon = eval("("+aryCoupon[i]+")");
    	if(json_Coupon.ID == id)
    	{
    	    if(quantity == 0) 
    	        continue;
    	    else
    	    {
        //{"ID":"21", "NAME":"深海鳕鱼条买一份送一份", "QUANTITY":"1", "PIC":"200905/20090502035209_44.jpg"};"
                //aryCoupon[i].replace('"QUANTITY":"'+ json_Coupon.QUANTITY + '"','"QUANTITY":"'+ quantity + '"');

       	        newCoupon += ";" + aryCoupon[i].replace('"QUANTITY":"'+ json_Coupon.QUANTITY + '"','"QUANTITY":"'+ quantity + '"');
    	    }    	
//    	    isAdd = "1";
    	}
    	else
    	{
  	        newCoupon += ";" + aryCoupon[i];
    	} 
    }
    //去掉最前面的;号
//    if(isAdd=="0")
//    {
//        newCoupon += ";" + aryCoupon[i];
//    }
//    alert(newCoupon.substr(0, 1));
    if(newCoupon.substr(0, 1)== ";") newCoupon = newCoupon.substr(1, newCoupon.length -1);
    setCookie("mylist", newCoupon);
    DisplayMyCoupon();
}
function AddtoMyList(id, quantity)
{
   if(!IsNum(quantity)) 
    {
        alertWin('肯德基优惠网提示您！','请输入正确的数量',300,150);
        return;
    }
//url发送地址 recvT 接受数据类型 0为xml 1为text resultF 返回结果处理函数
var ajax1 = new Ajax("getCouponInfo.aspx",1,"id="+id+"&quantity="+quantity, backcall);
ajax1.post();

    function backcall(EcouponInfo)
    {
        var json = eval("("+EcouponInfo+")");
        var strCouponList = getCookie("mylist");
        var updated="0";
        var iSum = 0;
        if(strCouponList == "")
        {
        //检查新加的优惠券是否已经存在，是就改原来记录的数量，而不新加记录
            cookie_coupon = EcouponInfo;
            setCookie("mylist", cookie_coupon);
        }
        else
        {
            var aryCoupon = strCouponList.split(";");
            var newCoupon = "";
            for(i=0; i<aryCoupon.length; i++)
            {

                var json_Coupon = eval("("+aryCoupon[i]+")");  
                if(json_Coupon.ID == json.ID)
                {
                    iSum = parseInt(json_Coupon.QUANTITY) + parseInt(quantity)
        	        newCoupon += ";" + aryCoupon[i].replace('"QUANTITY":"'+ json_Coupon.QUANTITY + '"','"QUANTITY":"'+ iSum + '"');
                    updated = "1";
                }
                else
                    newCoupon += ";" + aryCoupon[i];
            } 

            if(updated=="0") newCoupon += ";" + EcouponInfo;  
            if(newCoupon.substr(0, 1)== ";") newCoupon = newCoupon.substr(1, newCoupon.length -1);
            setCookie("mylist", newCoupon);
        }
        //	window.location.reload();
        DisplayMyCoupon();
//        alertWin('肯德基优惠网提示您！','添加到我的优惠券成功。',300,150);
    }
}

function ResetEcoupon()
{
    if(confirm("你确定要清空吗？"))
    {
        ClearAllRows();
        setCookie("mylist", "");
        document.getElementById("MyEcoupon_total_coupon").innerHTML = "0";
        document.getElementById("MyEcoupon_total_paper").innerHTML = "0";
    }
}
function ClearAllRows()
{
    var tb = document.getElementById('MyEcoupon_tbCouponList');
    var rowNum=tb.rows.length;
    for (i=1;i<rowNum;i++)
    {
//    alert(rowNum);
     tb.deleteRow(i);
     rowNum=rowNum-1;
     i=i-1;
    }
}


function DisplayMyCoupon()
{
	var oTable = document.getElementById("MyEcoupon_tbCouponList");
	var oRow;   
	var oCell; 
	var iTotal = 0;
	var iPaper = 1;
	var strCouponList = getCookie("mylist");
//	alert(strCouponList);
	if(strCouponList == "")
	{
    document.getElementById("MyEcoupon_total_coupon").innerHTML = "0";
    document.getElementById("MyEcoupon_total_paper").innerHTML = "0";
	
//		alert("購物籃為空");
		return;
	}
    ClearAllRows();
	var aryCoupon = strCouponList.split(";");

	for(i=0; i<aryCoupon.length; i++)
	{
		oRow = oTable.insertRow(-1);
        var json_Coupon = eval("("+aryCoupon[i]+")");
//优惠券名称
//			var e = document.createElement("input");   
			oCell = oRow.insertCell(-1);  
			oCell.innerHTML = json_Coupon.NAME;  
//			oCell.align = "left";
//			oCell.className = "aligncenter";
//数量
			oCell = oRow.insertCell(-1);  
			oCell.align = "center";
			oCell.innerHTML = "<span class='sty_yellow'><input name='textfield4' type='text' class='inputNumSty' value='" + json_Coupon.QUANTITY + "' maxlength='2' onblur='UpdateCounpon(" + json_Coupon.ID + ", this.value);' /></span>";
//			<input type='text' name='quantity_" + (i+1).toString() + "' class='quantity' style='width=28px;' maxlength='4' value='" + json_Coupon.Quatity + "' onblur='UpdateCart(this.value, " + json_Coupon.ID + ");' />"; 
//取消
			oCell = oRow.insertCell(-1);  
			oCell.align = "center";
			oCell.innerHTML = "<a href='javascript:if(confirm(\"确定要取消吗？\"))UpdateCounpon(" + json_Coupon.ID + ", 0)' class='red_link'>取消</a>";

            iTotal += parseInt(json_Coupon.QUANTITY);
	}
	iPaper = Math.ceil(iTotal / 9)
    document.getElementById("MyEcoupon_total_coupon").innerHTML = iTotal.toString();
    document.getElementById("MyEcoupon_total_paper").innerHTML = iPaper.toString();
}



function UpdateCoupon1(id, quantity)
{
	//alert(product_id);return;
	var strProducts = getCookie("product");
	var aryProduct = strProducts.split("|");
	var fTotal = 0.00;
	for(var i=0; i<aryProduct.length; i++)
	{
		var aryProperty = aryProduct[i].split("&");
		if(aryProperty[0] == "productid=" + product_id.toString())
		{
			strProducts = strProducts.replace(aryProperty[2], "quantity=" + strValue);
			fTotal = parseFloat(aryProperty[1].split("=")[1]) * parseFloat(strValue);
			strProducts = strProducts.replace(aryProperty[3], "total=" + fTotal);
		}
	}
	//alert(strProducts);
	setCookie("product", strProducts);
	window.location.reload();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function EmptyCart(ver)
{
	if(confirm(eval("msg_empty_" + ver)))
	{
		deleteCookie('product');
		window.location.reload();
	}
}
function DeleteProduct(productid, ver)
{
	if(!confirm(eval("msg_del_" + ver))) return;
	var strNewProducts = "";
	var strProducts = getCookie("product");
	if(strProducts == "") return;
	var aryProduct = strProducts.split("|");

	for(i=0; i<aryProduct.length; i++)
	{
		if("productid="+productid+"&" == aryProduct[i].split("&")[0] + "&")
			strNewProducts += "";
		else
			strNewProducts += aryProduct[i] + "|";
	}

	if(strNewProducts.substring(strNewProducts.length - 1) == "|") strNewProducts = strNewProducts.substring(0, strNewProducts.length - 1);
	//DisplayProductTable();
	//var oTable = document.getElementById("tbProduct"); 
	//oTable.deleteRow(1);

	setCookie("product", strNewProducts);
	window.location.reload();
}


