﻿//写cookies函数,//两个参数，一个是cookie的名子，一个是值，保存cookie的天数
function setCookie(name,value,days){var exp   = new Date();exp.setTime(exp.getTime() + days*24*60*60*1000);document.cookie = name + "="+ escape (value) +(days>0?";expires=" + exp.toGMTString():"");}
//取cookies函数 
function getCookie(name){var arr=document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));if(arr!=null)return unescape(arr[2]); return null;}
//删除cookie
function delCookie(name){var exp=new Date();exp.setTime(exp.getTime()-1);var cval=getCookie(name);if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();}

/*########################页面人性化美化功能####################################*/

function NoLookupInfo(ClientID,Title,info,ByTagName,showCount)
{
    var obj = document.getElementById(ClientID).getElementsByTagName(ByTagName);
    var count = obj.length;
    if(count==showCount)
    {
        var ShowContent='<div style="width:88%;margin:5px auto;padding:1px;border:solid 1px #dddddd;font-size:13px;"><div style="height:22px;padding-left:5px;line-height:22px;vertical-align:middle;background-color:#2882b9;color:#ffffff;font-weight:bold;">'+
        Title+'</div><ul style="width:98%;padding-left:10px;padding-top:6px;">';
        var infoList = info.split("|");
        count = infoList.length;
        for(var i=0;i<count;i++)
        {
            ShowContent+='<li style="width:100%;height:25px;padding:0;margin:0;text-align:left;">'+infoList[i]+'</li>';
        }
        ShowContent+='</div>';
        document.write(ShowContent);
        ShowContent=null;
    }
}

function productsSearch()
{
    if(document.getElementById("key")!=null)
    {
        var key = document.getElementById("key").value;
        window.location.href="/Products_Search.aspx?key="+key;
    }else
    {
        alert("System Error!");
    }
}
var shop000="Online Shopping";
var shop001="请输入大于0的整数！";
var shop002="数量已成功修改！";
var shop003="请选择你要购买的商品！";

function buy()
{
    var id = getCookie("Product");
    if(id==null||id=="")
    {
        alert(shop003);
        return;
    }
    window.parent.location.href="/Buy.aspx";
}
//添加购买产品
function openBuy(id)
{
    //添加产品ID cookie
    var idlist = getCookie("Product");
   
    var isUser=false;
    if(idlist!=null)
    {
        var arrId = idlist.split(",");
        var len =arrId.length;
        var arrpid = new Array();
        var num= new Array();
        
        for(var i=0;i<len;i++)
        {
            arrpid[i] = arrId[i].split("|")[0];
            if(arrpid[i]==id)
            {
                isUser=true;
                break;
            }
        }
    }
    if(!isUser)
    {
        if(idlist!=null&&idlist!="")
        {
            setCookie("Product",idlist+","+id+"|1");
        }
        else
        {
            setCookie("Product",id+"|1");
        }
    }
    ymPrompt.win('<iframe src="BuyThing.aspx" width="600" height="500" scrolling="auto"></iframe>'
            ,600,500,shop000);
            return false;
}
//清空所有的产品
function emptyBuy()
{
    delCookie("Product");
    //
    window.location.reload();
//    var table = document.getElementById("gounds");
//    var len = table.rows.length;
//    for(var i=1; i<len; i++){
//        alert(i);
//      table.delRow(0);    
//    }  
}

//删除一个产品
function delBuy(id)
{
    var idlist = getCookie("Product");
    var newIdList="";
    if(idlist!=null)
    {
        var arrId = idlist.split(",");
        var len =arrId.length;
        for(var i=0;i<len;i++)
        {
            
            var temp = arrId[i].split("|");
            if(temp[0]!=id)
            {
                if(newIdList=="")
                {
                    newIdList=temp[0]+"|"+temp[1];
                }else
                {
                    newIdList=newIdList+","+temp[0]+"|"+temp[1];
                }
            }
        }
        setCookie("Product",newIdList);
        window.location.reload();
    }
}
function checkIsInteger(str)
{
    if(str == "")
        return false;
        
    if(/^(\d+)$/.test(str))
        return true;
    else
    return false;
}

//修改产品数量
function updateNum(id)
{
    var count=document.getElementById("num_"+id).value;
    if(!checkIsInteger(count))
    {
        alert(shop001);
        return;
    }
    var idlist = getCookie("Product");
    var newIdList="";
    if(idlist!=null)
    {
        var arrId = idlist.split(",");
        var len =arrId.length;
        for(var i=0;i<len;i++)
        {
            var temp = arrId[i].split("|");
            if(temp[0]!=id)
            {
                if(newIdList=="")
                {
                    newIdList=temp[0]+"|"+temp[1];
                }else
                {
                    newIdList=","+temp[0]+"|"+temp[1];
                }
            }else
            {
                if(newIdList=="")
                {
                    newIdList=temp[0]+"|"+count;
                }else
                {
                    newIdList=","+temp[0]+"|"+count;
                }
            }
        }
        
        setCookie("Product",newIdList);
        alert(shop002);
    }
}



