﻿// JScript 文件
var itemAmount;
var shippingcosts;
var credit;
var maxcredit;
var saleTax;
var total;
var agentId;

var head="";

function setHead_cart(p)
{
    head=p;
}

function isNum()
{
    if(event.keyCode<45 || event.keyCode>57)
    {
        event.keyCode=0;
    }
}

function changePackageCount(portfolioId)
{
    var count=$("#quantity_"+portfolioId).val();
    if(count=="0"||count=="")
    {
        alert("The quantity must greater than zero");
        return;
    }
    $.ajax({
        type:"GET",
        cache:false,
        async: false,
        url:head+"Ajax/changeCart.ashx?portfolioId="+portfolioId+"&count="+count+"&action=update",
        success:function(msg){rtnSuccess(msg,portfolioId,count);},
        error:function(){alert("occur error")}
        });
}

function removePackage(portfolioId)
{
    $.ajax({
        type:"GET",
        cache:false,
        async: false,
        url:head+"Ajax/changeCart.ashx?portfolioId="+portfolioId+"&action=delete",
        success:function(msg){rtnDelSuccess(msg,portfolioId);},
        error:function(){alert("occur error")}
        });
}

function rtnDelSuccess(msg,portfolioId)
{
    if(msg=="-1")
    {
        alert("error");
    }
    else
    {
       var rsts=msg.split("|");
       itemAmount=parseFloat(rsts[1]);
       caculatePayment();
       $("#div"+portfolioId).remove();
       $("#subtoatalInfo").empty();
       var count=parseInt(rsts[2]);
       var countInfo="("+rsts[2]+"&nbsp"+(count>1?"items":"item")+")";
       $("#subtoatalInfo").append(countInfo)
       countInfo="You Have (" + rsts[2] + ")&nbsp" + (count>1?"items":"item") + "&nbspin Your Shopping Cart";
       $("#countInfo").empty();
       $("#countInfo").append(countInfo);
       changeSingleDiscount(rsts[3]);
    }
}

function changeSingleCount(portfolioId)
{
    var count=$("#quantity_"+portfolioId).val();
    if(count=="0"||count=="")
    {
        alert("The quantity must greater than zero");
        return;
    }
    $.ajax({
        type:"GET",
        cache:false,
        async: false,
        url:head+"Ajax/changeCart.ashx?portfolioId="+portfolioId+"&count="+count+"&action=update",
        success:function(msg){rtnSuccess(msg,portfolioId,count);},
        error:function(){alert("occur error")}
        });
}

function rtnSuccess(msg,portfolioId,count)
{
    if(msg=="0")
    {
        alert("short in supply");
    }
    else if(msg=="-1")
    {
        alert("unkown error");
    }
    else
    {
        rsts=msg.split("|");
        $("#sum"+portfolioId).empty();
        $("#sum"+portfolioId).append("$"+(parseFloat(rsts[1])).toFixed(2));
        itemAmount=parseFloat(rsts[2]);
        caculatePayment();
        //var cunts=document.getElementByName(portfolioId);
        var cunts=$(".classname"+portfolioId);
        $.each(cunts,function(i,n)
            {
            if($.browser.msie)
            {
                n.innerText=count;
            }
            else
            {
                n.textContent=count;
            }
        });
       $("#subtoatalInfo").empty();
       var count=parseInt(rsts[3]);
       var countInfo="("+rsts[3]+"&nbsp"+(count>1?"items":"item")+")";
       $("#subtoatalInfo").append(countInfo)
       countInfo="You Have (" + rsts[3] + ")&nbsp" + (count>1?"items":"item") + "&nbspin Your Shopping Cart";
       $("#countInfo").empty();
       $("#countInfo").append(countInfo);
       changeSingleDiscount(rsts[4]);
    }
}

function changeSingleDiscount(msg)
{
    var msgs=msg.split("&");
    for(i=0;i<msgs.length;i++)
    {
        submsgs=msgs[i].split(",");
        $("#discountName"+submsgs[0]).empty();
        $("#discountName"+submsgs[0]).append("$"+(parseFloat(submsgs[2])).toFixed(2));
        $("#sum"+submsgs[0]).empty();
        $("#sum"+submsgs[0]).append("$"+(parseFloat(submsgs[1])).toFixed(2));
    }
}

function removeSingle(portfolioId)
{
    $.ajax({
        type:"GET",
        cache:false,
        async: false,
        url:head+"Ajax/changeCart.ashx?portfolioId="+portfolioId+"&action=delete",
        success:function(msg){rtnDelSuccess(msg,portfolioId);},
        error:function(){alert("occur error")}
        });
}

function removeAll()
{
    $.ajax({
        type:"GET",
        cache:false,
        async: false,
        url:head+"Ajax/changeCart.ashx?action=deleteAll",
        success:function(msg){rtnDelAllSuccess(msg);},
        error:function(){alert("occur error")}
        });
}

function rtnDelAllSuccess(msg)
{
    if(msg=="-1")
    {
        alert("error");
    }
    else
    {
       var rsts=msg.split("|");
       itemAmount=parseFloat(rsts[1]);
       caculatePayment();
       $("#products").remove();
       $("#subtoatalInfo").empty();
       var count=parseInt(rsts[2]);
       var countInfo="("+rsts[2]+"&nbsp"+(count>1?"items":"item")+")";
       $("#subtoatalInfo").append(countInfo)
       countInfo="You Have (" + rsts[2] + ")&nbsp" + (count>1?"items":"item") + "&nbspin Your Shopping Cart";
       $("#countInfo").empty();
       $("#countInfo").append(countInfo);
    }
}

function creditChange()
{
    var tcredit=$("#by_Credit").val();
    tcredit=$.trim(tcredit);
    tcredit=(tcredit=="")?0.00:parseFloat(tcredit).toFixed(2);
    
    if(tcredit>parseFloat(itemAmount))
    {
        
        $("#by_Credit").val("");
        $("#by_Credit").select();
        credit=parseFloat(0.00);
        alert("The amount of used store credit can not be larger than the amount payable.");          //储值大于应付金额
       
    }
    else if(tcredit>parseFloat(maxcredit))
    {
        $("#by_Credit").val("");
        $("#by_Credit").select();
        credit=parseFloat(0.00);
        alert("Your current store credit is low or you have no store  credit.");      //储值大于最大储值
        
    }
    else
    {
        credit=tcredit;
        $("#CreditValue").val(credit);
    }
     caculatePayment();   
}

function shippingAddressChang(obj)
{
    var addressId=obj.value;
    getAddressInfo(addressId,"shipping")
}

function getAddressInfo(addressId,type)
{
    $.ajax({
        type:"GET",
        cache:false,
        async: false,
        url:head+"Ajax/addressChange.ashx?addressId="+addressId+"&type="+type,
        success:function(msg){rtnAddressChangeInfo(msg,type);},
        error:function(){alert("occur error")}
        });
}

function rtnAddressChangeInfo(msg,type)
{
    if(msg!="0")
    {
        var ads=msg.split("|");
        if(type=="shipping")
        {
            $("#txtFirstName").val(ads[0]=="-"?"":ads[0]);
            $("#txtLastName").val(ads[1]=="-"?"":ads[1]);
            $("#txtPhone").val(ads[2]=="-"?"":ads[2]);
            $("#txtAddress1").val(ads[3]=="-"?"":ads[3]);
            $("#txtAddress2").val(ads[4]=="-"?"":ads[4]);
            $("#txtCity").val(ads[5]=="-"?"":ads[5]);
            var cityName=ads[5]=="-"?"":ads[5];
            var val=ads[6]=="-"?"":ads[6];
            var obj=document.getElementById("ddlStateShipping");
            changeOptionsByValue(val,obj);
            if(val=="41")
            {
                if(cityName.toLowerCase()=="houston")
                {
                    saleTax=itemAmount*0.0825;
                }
                else
                {
                    saleTax=itemAmount*0.0625;
                }
            }
            else
            {
                saleTax=0.00;
            }
            $("#txtZipCode").val(ads[7]=="-"?"":ads[7]);
            caculatePayment();
            reset();
        }
    }
}

function shippingOptionsChange(obj)
{
    var tmps=obj.value.split("|");
    shippingcosts=parseFloat(tmps[1]);
    caculatePayment();
}

function shippingStateChange(obj)
{
    var stateId=obj.value;
    var cityName=document.getElementById("txtCity").value;
    if(stateId=="41")
    {
        if(cityName.toLowerCase()=="houston")
        {
            saleTax=itemAmount*0.0825;
        }
        else
        {
            saleTax=itemAmount*0.0625;
        }
    }
    else
    {
        saleTax=0.00;
    }
    caculatePayment();
}

function cityNameChange(obj)
{
    var stateId=document.getElementById("ddlStateShipping").value;
    var cityName=obj.value;
    if(stateId=="41")
    {
        if(cityName.toLowerCase()=="houston")
        {
            saleTax=itemAmount*0.0825;
        }
        else
        {
            saleTax=itemAmount*0.0625;
        }
    }
    else
    {
        saleTax=0.00;
    }
    caculatePayment();
}

function showErrors()
{
     if($("#txtFirstName").val()=="")
    {
        $("#tipfirstname").empty();
        $("#tipfirstname").append("First name is required.");
    }
    else
    {
        $("#tipfirstname").empty();
    }
    if($("#txtLastName").val()=="")
    {
        $("#tiplastname").empty();
        $("#tiplastname").append("Last name is required.");
    }
     else
    {
        $("#tiplastname").empty();
    }
    if($("#txtPhone").val()=="")
    {
        $("#tipphone").empty();
        $("#tipphone").append("Phone is required.");
    }
     else
    {
        $("#tipphone").empty();
    }
    if($("#txtAddress1").val()=="")
    {
        $("#tipaddress1").empty();
        $("#tipaddress1").append("Address is required.");
    }
     else
    {
        $("#tipaddress1").empty();
    }
    if($("#txtCity").val()=="")
    {
        $("#tipcity").empty();
        $("#tipcity").append("City is required.");
    }
     else
    {
        $("#tipcity").empty();
    }
    if($("#txtZipCode").val()=="")
    {
        $("#tipzipcode").empty();
        $("#tipzipcode").append("Zipcode is required.");
    }
     else
    {
        $("#tipzipcode").empty();
    }
}

function reset()
{
    $("#tipfirstname").empty();
    $("#tiplastname").empty();
    $("#tipphone").empty();
    $("#tipaddress1").empty();
    $("#tipcity").empty();
    $("#tipzipcode").empty();
}

function checkShippingInfo()
{
    showErrors();
    if($("#txtFirstName").val()=="")
    {
        $("#txtFirstName").select();
        $("#txtFirstName").focus();
        
        return false;
    }
    if($("#txtLastName").val()=="")
    {
        $("#txtLastName").select();
        $("#txtLastName").focus();
        
        return false;
    }
    if($("#txtPhone").val()=="")
    {
        $("#txtPhone").select();
        $("#txtPhone").focus();
       
        return false;
    }
    if($("#txtAddress1").val()=="")
    {
        $("#txtAddress1").select();
        $("#txtAddress1").focus();
       
        return false;
    }
    if($("#txtCity").val()=="")
    {
        $("#txtCity").select();
        $("#txtCity").focus();
        
        return false;
    }
    if($("#txtZipCode").val()=="")
    {
        $("#txtZipCode").select();
        $("#txtZipCode").focus();
        
        return false;
    }
    return true;
}



function changeOptionsByValue(val,obj)
{
    var ops=obj.options;
    var index=0;
    for(var i=0;i<ops.length;i++)
    {
        if(ops[i].value==val)
        {
            index=i;
            break;
        }
    }
    obj.selectedIndex=index;
}

function checkZipCode()
{
    if(event.keyCode<45 || event.keyCode>57)
    {
        event.keyCode=0;
    }
}

function caculatePayment()
{
    var tmpTotal=itemAmount+shippingcosts+saleTax;
    credit=(tmpTotal>maxcredit)?maxcredit:tmpTotal;
    total=tmpTotal-credit;
    
    $("#itemAmount").empty();
    $("#itemAmount").append("$"+itemAmount.toFixed(2));
    $("#shipingcosts").empty();
    $("#shipingcosts").append("$"+shippingcosts.toFixed(2));
    $("#by_Credit").empty();
    $("#by_Credit").append("-$"+credit.toFixed(2));
    $("#credit").empty();
    $("#credit").append("-$"+credit.toFixed(2));
    $("#saletax").empty();
    $("#saletax").append("$"+saleTax.toFixed(2));
    $("#total").empty();
    $("#total").append("$"+total.toFixed(2));
}


function useAgentId()
{
    var agencyId=$("#txt_agentId").val();
    if(agencyId==agentId)
    {return;}
    agentId=agencyId;
    $.ajax({
        type:"GET",
        cache:false,
        async: false,
        url:head+"Ajax/useAgencyId.ashx?AgentID="+agentId,
        success:function(msg){rtnUseAgentId(msg);},
        error:function(){alert("occur error")}
        });
}

function rtnUseAgentId(msg)
{
    if(msg!="1")
    {
        $("#tipAgentID").empty();
        $("#tipAgentID").append("The encoding you enter are incorrect, please re-enter.");
    }
    else
    {
        window.open("Clearing.aspx?tmp="+(new Date().getTime().toString(36)),"_self");
    }
}

function Intial(itemAmount1,shippingcosts1,credit1,maxcredit1,saleTax1,total1,agentId1)
{
    itemAmount=parseFloat(itemAmount1);
    shippingcosts=parseFloat(shippingcosts1);
    credit=parseFloat(credit1);
    maxcredit=parseFloat(maxcredit1);
    saleTax=parseFloat(saleTax1);
    total=parseFloat(total1);
    agentId=agentId1;
}
