﻿// JScript file
var Events=new Object();
// Verify the user is not logged on
Events.ajaxTxt="temp";
Events.UserLoad=function()
{
    var GetVal = AjaxAction.IsLoad();
    var items = GetVal.value; 
    if(items!="1")//表示没有登录
    {
       location.href = "/SignIn.html?url=" + items;
       return;
    }
    else//已登录
    {
       document.body.style.display = "block";
    }
}

Events.BillingInfoLoad=function()
{
    ShopCart.ShowCounts();//显示购物车信息,add by 袁之梅 2010-11-12 11:57
    var AjaxIsLoging = AjaxAction.GetIsNoLogin();
    if(AjaxIsLoging.value != "1")//表示会员才可以下订单
    {
        var getSignIn = AjaxAction.IsLoad();
        if(getSignIn.value != "1")//表示没有登录
        {
           location.href = "/SignIn.html?url=/BillingInfo.html";
           return;
        }
    }
    var GetVal = AjaxAction.getEmployeeModel();
    var returnSTR = GetVal.value;
    if(returnSTR!="")
    {
        var obj=eval(returnSTR);
        document.getElementById("Eid").value = obj.ID;
        document.getElementById("qty").innerHTML = obj.counts;
        document.getElementById("weight").innerHTML = obj.weight;
        document.getElementById("cmoney").innerHTML = obj.money;
        document.getElementById("s_firstname").value = obj.Name;
        document.getElementById("s_phone").value = obj.Telephone;
        document.getElementById("s_postcode").value=obj.PostCode;
        document.getElementById("email").value=obj.EMail;
        document.getElementById("curren").innerHTML = obj.curren;
        document.getElementById("curren1").innerHTML = obj.curren;
        document.getElementById("Gtmoney").innerHTML = obj.money;
        
        var arrayL = obj.Address.split("|");
        if(arrayL[0]!=null)
        {
            document.getElementById("s_street").value = arrayL[0];
            if(arrayL[1]!=null)
            {
                document.getElementById("s_city").value = arrayL[1];
                if(arrayL[2]!=null)
                {
                    document.getElementById("s_province").value = arrayL[2];
                }
            }
        }
        var URI = window.location.toString();
        var Country = Utils.request(URI,"Country");
        
        if(obj.CountryID != null && obj.CountryID != "")
        {
            Country = obj.CountryID;
        }
        var select=document.getElementById("send_country");
        var i=0;
        for(i=0;i<select.options.length;i++)
        {
            if(Country==select.options[i].value)
            {
                select.options[i].selected=true;
            }
        }
        var str = AjaxAction.GetPid();
        if(str.value!="0")
        {
            document.getElementById("Promotion").value = str.value;
        }
        Events.shipfeeload(Country);
        Events.CheckAllt();
    }
}

Events.shipfeeload=function(obj)
{
    var GetVal = AjaxAction.getDeliveryType(obj);
    document.getElementById("shippingment").innerHTML =GetVal.value;
}

Events.getShippingListByCountry=function(obj)
{
    var GetVal = AjaxAction.getDeliveryType(obj.value);
    document.getElementById("shippingment").innerHTML =GetVal.value;
}

Events.ShopCartLoad=function(ObjectId)
{
    try
    {
        var GetVal = AjaxAction.CartLoad();
        document.getElementById(ObjectId).innerHTML = GetVal.value;
        ShopCart.ShowCartInfo();
    }
    catch(e)
    {}
}

//网站首页加载
Events.IndexLoad=function(ObjectId)
{
    ShopCart.ShowCounts();
}

Events.RemoveCart=function(pid,attribute)
{    
    var GetVal = AjaxAction.ReMoveCart(pid,attribute);
    window.onload();
    ShopCart.GetSpecialOffers();
}

Events.ModifyCart=function(obj,pid,attribute)
{
    var GetVal = AjaxAction.ModifyCart(pid,attribute,obj.value);
    window.onload();
    ShopCart.GetSpecialOffers();
}

Events.CheckAllt=function()
{    
    var name=document.getElementById("s_firstname").value; 
    var email=document.getElementById("email").value;
    var postcode=document.getElementById("s_postcode").value;
    var address= document.getElementById("s_street").value;
    var phone=document.getElementById("s_phone").value;
    var country=document.getElementById("send_country").value;
   
    if(name!="")
    { 
        document.getElementById("t_1").style.color="green";
        document.getElementById("t_1").innerHTML="√";  
    }
      if(email!="")
    {
        document.getElementById("t_2").style.color="green";
        document.getElementById("t_2").innerHTML="√";  
    }
      if(postcode!="")
    {
        document.getElementById("t_7").style.color="green";
        document.getElementById("t_7").innerHTML="√";  
    }
      if(address!="")
    {
        document.getElementById("t_4").style.color="green";
        document.getElementById("t_4").innerHTML="√";  
    }
      if(phone!="")
    {
        document.getElementById("t_3").style.color="green";
        document.getElementById("t_3").innerHTML="√";  
    }
      if(country!="")
    {
        document.getElementById("t_8").style.color="green";
        document.getElementById("t_8").innerHTML="√";  
    }
}

//产品页图片切换
Events.ShowPictures=function(obj)
{
    document.getElementById("medium_img").src=obj;
}

//客户留言验证
Events.SubmitMessage=function(mail,mg,code)
{
    var txtMail=document.getElementById(mail);
    if(!Utils.isEmail(txtMail.value))
    {
        info=document.getElementById("mail");
        info.innerHTML="×";
        return;
    }
    var txtMg=document.getElementById(mg);
    if(Utils.isEmpty(txtMg.value))
    {
        info=document.getElementById("content");
        info.innerHTML="×";
        return;
    }
    var Code=document.getElementById(code);
    if(Code.value!=Utils.GetCookie("Code"))
    {
        info=document.getElementById("code");
        info.innerHTML="×";
        return;
    }
    var Form=document.getElementById("form1");
    Form.action="/Message.aspx";
    Form.method="post"
    Form.submit();
}
//通过产品属性改变产品价格
//谢文华 2010-12-7
function ChagShowPrice()
{
    var pid=document.getElementById("products_id").value;
    var arr=document.getElementsByName("Listattributes");
    var values="";
    for(i=0;i<arr.length;i++)
    {
     if (arr[i].value!="") {
        values=values+"'"+arr[i].value.split('_')[1]+"'";
        values=values+",";
     }
    }
    if (values.substring(values.length-1)==",") {
        values=values.substring(0,values.length-1);
        
    }
    var price=Loads.GetPriceByAttribute(pid,values).value;
    if (price!=null) {
    document.getElementById("ProductPrice").innerHTML=price;
    }
}

//函数名：Events.SelectSpec
//生成产品特性参数列表
Events.SelectSpec=function(objdiv)
{
   
    var div=objdiv.id;
    var str;
    var Index=document.getElementById(div).selectedIndex;
    if(Index!="0")
    {   
        var obj=document.getElementById(div).options[document.getElementById(div).options.selectedIndex].value; //获取用户选择的值
        var objs=document.getElementById("Spec");
        var arr=obj.split("_");
        var flag=false;
        if(objs.value.length>0)
        {
            var array=objs.value.split("|");
            var arrtemp=null;

            for(i=0;i<array.length;i++)
            {
              arrtemp=array[i].split("_");

                if(arrtemp[0]==arr[0])
                {
                    array[i]=obj;
                    flag=true;
                    objs.value=array.toString().replace(",","|");
                    break; 
                }
            }
            if(flag==false)
            {
                objs.value+="|";
                objs.value+=obj;
            } 
        }
        else
        {
            objs.value=obj;
        }
    }
 }

//转到上一个页面
Events.history=function()
{
    history.back(-1);
}

Events.JudgeCounpons=function()
{
    var price = document.getElementById("ItemMoney").value;
    var email = document.getElementById("email").value;
    var offamount = document.getElementById("spanOffAmount").innerHTML;
    var tamount = document.getElementById("GrandMoney").innerHTML;
    var num = document.getElementById("TextCouponID").value;
    var GetVal = AjaxAction.JudgeCounpons(num,price,email);
    var str = GetVal.value;
    if(str=="0")
    {
        alert("Sorry , The coupons is invalid !");
    }
    else if(str=="-1")
    {
        alert("Sorry , The coupons has been used !");
    }
    else if(str=="-2")
    {
        alert("Sorry , The coupons do not meet the conditions of use !");
    }
    else if(str=="-3")
    {
        alert("Sorry , You can't use the coupons !");
    }
    else
    {
         alert("OK , you can use it !");
         document.getElementById("spanOffAmount").innerHTML = parseFloat(offamount)+parseFloat(str);
         document.getElementById("GrandMoney").innerHTML = parseFloat(tamount)-parseFloat(str);
    }
}

Events.ShowReviews=function()
{
     var pid = document.getElementById("Pid").value;
     var GetVal = AjaxAction.GetReview(pid);
     document.getElementById("Reviews").innerHTML=GetVal.value;
}

Events.getUserID = function()
{
    var GetVal=AjaxAction.getUser();
    document.getElementById("UserID").value=GetVal.value;
}

Events.addtolist = function(obj)
{
    var userid = document.getElementById("UserID");
    if(userid.value == "null" || userid.value=="")
    {
        alert("please sign in first");
        return;
    }
    var GetVal = AjaxAction.AddToCollectionProduct(userid.value,obj);
    if(GetVal.value == "1")
    {
        alert("Add Successful");
    }
    if(GetVal.value =="0")
    {
        alert("Add Failed");
    }
}
