﻿// Shopping.js
// CopyRight (c) 2009 HangJing Teconology. All Rights Reserved.
// zjf@ihangjing.com
// 2009-07-15

var reference_url;
var book_id;
//将书加入购物车 并跳转到购物车页面 返回页面url
function AddToShoppingCart(bookid)
{
    var url=null;

    if(bookid==null || bookid<1)
    {
        reference_url = "ShoppingCart.aspx";
    }
    else
    {
        reference_url = "ShoppingCart.aspx?"+escape(window.location.href);
        book_id = bookid;
        //ajax
        new net.ContentLoader("Ajax/Shopping.aspx","type=shoppingcart&value1="+bookid+"","POST",addtoshoppingcart_deal,addtoshoppingcart_err);
    }
}

//将书加入购物车 并跳转到购物车页面 返回页面url
function AddToShoppingCartNew(bookid, bookstock)//2010.10.15 zjf@ihangjing.com
{
    var url=null;
    var buynum = document.getElementById("ctl00_ContentPlaceHolder1_tbBuyNum").value;
    
    //判断是否超出库存
    if( parseInt(buynum) < 1 )
    {
        alert("请正确填写购买数量！");
        return;
    }
    
    if( parseInt(bookstock) < parseInt(buynum) )
    {
        alert("购买数量不能超过库存量！");//"+buynum+","+bookstock+"
        return;
    }
    
    if(bookid==null || bookid<1)
    {
        reference_url = "ShoppingCart.aspx";
    }
    else
    {
        reference_url = "ShoppingCart.aspx?"+escape(window.location.href);
        book_id = bookid;
        //ajax
        new net.ContentLoader("Ajax/Shopping.aspx","type=shoppingcart&value1="+bookid+"&value2="+buynum+"","POST",addtoshoppingcart_deal,addtoshoppingcart_err);
    }
}

function addtoshoppingcart_deal()
{
    if(this.req.responseText == "1")
    {
        //var popup=window.open(reference_url,"shoppingcart");
        //popup.focus();
        
        window.location.href = reference_url;
    }
    else if(this.req.responseText == "-1")
    {
       window.location.href =  "Login.aspx?returnurl=~/BookDetail.aspx?id="+book_id+"";
    }
}

function addtoshoppingcart_err()
{
    ShowWindow("服务器忙...", false);
}

function ShowMSg(obj,msg,iserr)
{
    obj.innerHTML = msg;
    if(iserr == true)
    {
        obj.className = "notice_error";
    }
    else
    {
        obj.className = "notice";
    }
    obj.style.display = "inline";
}

function ChangeNum(lbdataid,tbnum)
{
    var dataid = 0;
    var value = 1;
    dataid = $(lbdataid).innerHTML;
    value = $(tbnum).value;
    
    new net.ContentLoader("Ajax/Shopping.aspx","type=changenum&value1="+dataid+"&value2="+value+"","POST",changenum_deal,changenum_err);
}

function changenum_deal()
{
    if(this.req.responseText == "1")
    {
        //ShowWindow("修改数量成功", "");
        //成功则需要修改总价
        location.href=location.href;
    }
    else
    {
        ShowWindow("修改数量失败", "error");
    }
}

function changenum_err()
{
    ShowWindow("服务器忙...", false);
}

function ShowWindow(msg,type)
{
    var innerHTML = "<div style='background-color: #f8e1c1;height: 19px;padding: 3px 4px 0 10px;'>";
    innerHTML  += "<div style='float:left;font-size:12px;'>提示信息</div>";
    innerHTML  += "<div style='float:right;'>";
    innerHTML  += "<a href='javascript:HiddenWindow();'><img src='Images/window_close.gif' alt='关闭窗口' /> </a>";
    innerHTML  += "</div></div>";
    innerHTML  += "<div style='text-align:center;font-size:12px;' id='divMassage'>";
    if(type == "error")
    {
        innerHTML  +="<p><img src='images/error.gif' style='margin-top:10px;margin-bottom:10px;' /></p><p>";
    }
    else
    {            
        innerHTML  +="<p><img src='images/right.gif' style='margin-top:10px;margin-bottom:10px;' /></p><p>";
    }    
    innerHTML  += msg;
    innerHTML  += "</p></div></div>";
        
    if(!document.getElementById("divMsg"))
    {
        
        
        var div = document.createElement('div');
        div.id = 'divMsg';
        div.setAttribute('style','bottom:0px; right:20px; width:200px; height:100px; position: absolute;z-index: 100; background-color:#FFFFFF;border: 2px solid #f8e1c1;');              
        div.setAttribute('innerHTML',innerHTML); 
        div.innerHTML = innerHTML;
        document.body.appendChild(div);
        with(document.getElementById("divMsg").style)
        {
            bottom="0px";
            right="20px";
            width="200px";
            height="100px";
            position="absolute";            
            background="#FFFFFF";
            border="2px solid #f8e1c1";
        }
    }
    else{
        document.getElementById("divMsg").style.display = "block";
        document.getElementById("divMsg").setAttribute('innerHTML',innerHTML); 
        document.getElementById("divMsg").innerHTML = innerHTML;
    }
    setTimeout(HiddenWindow,3000);
}
function HiddenWindow()
{
     if(document.getElementById("divMsg"))
    {
        document.getElementById("divMsg").style.display = "none";
    }
}

function SetProvince()
{
    for(var i = 0;i < china.province.length;i++)
    {
        $("selProvince").options.add(new Option(china.province[i].cityname,china.province[i].id));
    }
}

function SetCity()
{
    var provID = $("selProvince").options[$("selProvince").selectedIndex].value;
    var provText = $("selProvince").options[$("selProvince").selectedIndex].text;
    $("selCity").options.length = 0; 
    if(provID == "")
    {
        $("selCity").options.add(new Option("--选择城市--",""));
        return ;
    }    
    for(var i = 0;i < china.city.length;i++)
    {
        if(china.city[i].superior == provID)
            $("selCity").options.add(new Option(china.city[i].cityname,china.city[i].id));
    }
    if($("selCity").options.length == 0)
    {
         $("selCity").options.add(new Option(provText,provID));
    }
    
    InitProvinceName();
    CountPrice();
    InitPayType();
    TransferTime();
}
 
function city_focus()
{
    $('errCity').innerHTML = "请正确填写您的地址,确保订单可以及时准确送达";
}

//获取地址 省份 城市
function  InitProvinceName()
{
    $('hidProvince').value = $('selProvince').options[$("selProvince").selectedIndex].value;
    $('hidAddress').value = $('selProvince').options[$("selProvince").selectedIndex].text;
}

function InitCityName()
{
    $('hidCity').value =  $('selCity').options[$("selCity").selectedIndex].text;
}

var name = false;
var address = false;
var zipcode = false;
var phone = false;
var code = false;

function tbName_check()
{
    if($("tbName").value == "")
    {
        ShowMSg($('errName'),"请填写收货人姓名",true)
        name = false;
        return false;
    }
    else
    {
        $('errName').innerHTML = "";
        name = true;
    }
}

function tbAddress_check()
{
    if($("tbAddress").value == "" || $("hidProvince").value=="" || $("hidProvince").value == "--选择省份--" || $("hidCity").value == "--选择城市--" || $("hidCity").value == "")
    {
        ShowMSg($('errCity'),"请正确填写您的地址,确保订单可以及时准确送达",true)
        address = false;
        $('errCode').innerHTML = "请填写地址";
        return false;
    }
    else
    {
        $('errCity').innerHTML = "";
        address = true;
    }
}

function tbZipcode_check()
{
    if($("tbZipcode").value == "")
    {
        ShowMSg($('errZipcode'),"请填写您的邮编",true)
        zipcode = false;
        return false;
    }
    else
    {
        $('errZipcode').innerHTML = "";
        zipcode = true;
    }
}

function tbPhone_check()
{
    if($("tbPhone").value == "" && $("tbTel").value == "")
    {
        ShowMSg($('errPhone'),"请填写移动电话或者固定电话",true)
        phone = false;
        return false;
    }
    else
    {
        $('errPhone').innerHTML = "";
        phone = true;
    }
}

//验证码检查
function code_check()
{
    if($('tbCode').value.trim()=="")
    { 
        code = false;
        $('errCode').innerHTML = "请填写验证码";
        return false;
    }
    new net.ContentLoader("Ajax/AjaxCheck.aspx","type=code&value="+$('tbCode').value,"POST",code_deal,code_err);
}

function code_deal()
{
    if(this.req.responseText == "1")
    {
        $('errCode').innerHTML = "";
        code = true;
    }
    else
    {
        $('errCode').innerHTML = "验证码错误";
        code = false;
    }
}

function code_err()
{
    $('errCode').innerHTML = "服务器错误";
}

function InitTransferType()
{
    var radios = $N('rd_transfer_type');
    for(var i=0;i<radios.length;i++)
    {
        if(radios[i].checked==true)
        {
            $('hidTransferType').value = radios[i].value;
        }
    }
}

function InitPayType()
{
    var radios = $N('rd_pay_type');
    for(var i=0;i<radios.length;i++)
    {
        if(radios[i].checked==true)
        {
            $('hidPayType').value = radios[i].value;
        }
    }
}

function TransferTime()
{
    $('lbTransferTime').value = $('sel_ship_time').options[$("sel_ship_time").selectedIndex].text;
}

//提交订单时检查表单
function CheckForm()
{
    InitProvinceName();
    InitCityName();
    InitTransferType();
    
    code_check();
    tbName_check();
    tbAddress_check();
    tbZipcode_check();
    tbPhone_check();
    
    if(name&&address&&zipcode&&phone&&code)
    {
        return true;
    }
    
    code_check();
    tbName_check();
    tbAddress_check();
    tbZipcode_check();
    tbPhone_check();
    
    return false;
}

function ZfbClick()
{
    $("btZfb").click();
}

function WsyhClick()
{
    $("btWsyh").click();
}


function CountPrice()
{
    InitProvinceName();
    InitCityName();
    InitTransferType();
    var value1 = $('hidProvince').value;
    var value2 = $('hidTransferType').value;
    var value3 = $('hidBookPrice').value;
    var value4 = $('hidBookNum').value;
    var url = "type=countprice&value1="+value1+"&value2="+value2+"&value3="+value3+"&value4="+value4+"";
    //Ajax/Shopping.aspx?type=countprice&value1=北京&value2=KD&value3=44&value4=3
    new net.ContentLoader("Ajax/Shopping.aspx",url,"POST",countprice_deal,countprice_err);
}
function countprice_deal()
{
    if(this.req.responseText == "-1")
    {
        ShowWindow("计算运费出错", "error");
        $('lbTransferPrice').innerText = "0";
        $('hidTransferPrice').value = "0";
    }
    else
    {
        $('lbTransferPrice').innerText = this.req.responseText;
        var all_price = parseFloat($('hidBookPrice').value.toString()) + parseFloat(this.req.responseText);
        $('lbAllPrice').innerText = (Math.round(all_price * 10)/10).toString();
        $('hidAllPrice').value = (Math.round(all_price * 10)/10).toString();
        $('hidTransferPrice').value = this.req.responseText;
    }
}

function countprice_err()
{
    ShowWindow("服务器忙...", false);
}

//显示索要发票填写发票信息框
function ShowInvoice()
{
    ShowAtCenterFix("divInvoince");
}

function CheckOut()
{
    $('btCheckOut').click();
}

//索要发票 确定按钮 保存发票信息
function AddInvoice()
{
    //检查是否是空
    if($("tbTitle").value == "")
    {
        ShowMSg($('errInvoice'),"发票抬头不能为空",true)
        return false;
    }
    else
    {
        $('errInvoice').innerHTML = "";
    }
    
    if($("tbContent").value == "")
    {
        ShowMSg($('errInvoice'),"发票内容不能为空",true)
        return false;
    }
    else
    {
        $('errInvoice').innerHTML = "";
    }
    
    $('hidNeedInvoice').value = "1";
    
    Hidewindow('divInvoince');
    ShowWindowFix('申请发票成功','true');
    
}


function ShowWindowFix(msg,type)
{
    var innerHTML = "<div style='background-color: #f8e1c1;height: 19px;padding: 3px 4px 0 10px;'>";
    innerHTML  += "<div style='float:left;font-size:12px;'>提示信息</div>";
    innerHTML  += "<div style='float:right;'>";
    innerHTML  += "<a href='javascript:HiddenWindow();'><img src='Images/window_close.gif' alt='关闭窗口' /> </a>";
    innerHTML  += "</div></div>";
    innerHTML  += "<div style='text-align:center;font-size:12px;' id='divMassage'>";
    if(type == "error")
    {
        innerHTML  +="<p><img src='images/error.gif' style='margin-top:10px;margin-bottom:10px;' /></p><p>";
    }
    else
    {            
        innerHTML  +="<p><img src='images/right.gif' style='margin-top:10px;margin-bottom:10px;' /></p><p>";
    }    
    innerHTML  += msg;
    innerHTML  += "</p></div></div>";
        
    if(!document.getElementById("divMsg"))
    {
        
        
        var div = document.createElement('div');
        div.id = 'divMsg';
        div.setAttribute('style','width:200px; height:100px; position: absolute;z-index: 100; background-color:#FFFFFF;border: 2px solid #f8e1c1;');              
        div.setAttribute('innerHTML',innerHTML); 
        div.innerHTML = innerHTML;
        document.body.appendChild(div);
        with(document.getElementById("divMsg").style)
        {
            width="200px";
            height="100px";
            position="absolute";            
            background="#FFFFFF";
            border="2px solid #f8e1c1";
        }
    }
    else{
        //document.getElementById("divMsg").style.display = "block";
        document.getElementById("divMsg").setAttribute('innerHTML',innerHTML); 
        document.getElementById("divMsg").innerHTML = innerHTML;
    }
    
    ShowAtCenterFix('divMsg');
    setTimeout(HiddenWindow,3000);
}


