﻿//------js扩展方法---------
String.prototype.Trim = function() { return this.replace(/^\s+|\s+$/g, ""); }
String.prototype.LTrim = function() { return this.replace(/^\s+/, ""); }
String.prototype.RTrim = function() { return this.replace(/\s+$/, "").replace("&nbsp;", ""); }
String.prototype.ReplaceHTML = function() { return this.replace(/^<.+?>/g, ""); }
//---------------------------
//Js验证方法---------------------
 
 
 $(document).ready(function() {
		   var currentIndex = 0;
            var DEMO; //函数对象
            var currentID = 0; //取得鼠标下方的对象ID
            var pictureID = 0; //索引ID
            $("#flash_piclist li").eq(0).show(); //默认
            autoScroll();
            $("#flash_btn li").hover(function() {
                StopScrolll();
                $("#flash_btn li").removeClass("current")//所有的li去掉当前的样式加上正常的样式
                $(this).addClass("current"); //而本身则加上当前的样式去掉正常的样式
                currentID = $(this).attr("id"); //取当前元素的ID
                pictureID = currentID.substring(currentID.length - 1); //取最后一个字符
                $("#flash_piclist li").eq(pictureID).fadeIn("slow"); //本身显示
                $("#flash_piclist li").not($("#flash_piclist li")[pictureID]).hide(); //除了自身别的全部隐藏
                $("#flash_tx li").hide();
                $("#flash_tx li").eq(pictureID).show();

            }, function() {
                //当鼠标离开对象的时候获得当前的对象的ID以便能在启动自动时与其同步
                currentID = $(this).attr("id"); //取当前元素的ID
                pictureID = currentID.substring(currentID.length - 1); //取最后一个字符
                currentIndex = pictureID;
                autoScroll();
            });
            //自动滚动
            function autoScroll() {
                $("#flash_btn li:last").removeClass("current");
                $("#flash_btn li").eq(currentIndex).addClass("current");
                $("#flash_btn li").eq(currentIndex - 1).removeClass("current");
                $("#flash_piclist li").eq(currentIndex).fadeIn("slow");
                $("#flash_piclist li").eq(currentIndex - 1).hide();
                currentIndex++; currentIndex = currentIndex >= 4 ? 0 : currentIndex;
                DEMO = setTimeout(autoScroll, 2000);
            }
            function StopScrolll()//当鼠标移动到对象上面的时候停止自动滚动
            {
                clearTimeout(DEMO);
            }
	});
	
	
	
//设置URL参数
function seturl(type,para)
{
    var newurl="";
    var tag=0;
    var url = window.location.search;
    if(url!="")
    {
        if (url.indexOf("?") != -1)
        {
            var str = url.substr(1);
            strs = str.split("&");

               for(var i = 0; i < strs.length; i ++) 
               {
                    if(strs[i].split("=")[0]==type)
                    {
                        if(i==strs.length-1)
                        {
                            newurl+=strs[i].split("=")[0]+"="+para;
                        }
                        else
                        {
                            newurl+=strs[i].split("=")[0]+"="+para+"&";
                        }
                        tag=1;
                    }
                    else
                    {
                        if(i==strs.length-1)
                        {
                            newurl+=strs[i].split("=")[0]+"="+strs[i].split("=")[1];
                        }
                        else
                        {
                            newurl+=strs[i].split("=")[0]+"="+strs[i].split("=")[1]+"&";
                        }
                    }
               }
           
           if(tag==0)
           {
                newurl+="&"+type+"="+para;
           }
         
           window.location.href="List.aspx?"+newurl;
        }
    }
    else
    {

        window.location.href="List.aspx?"+type+"="+para;
    }
}

//计算折扣信息
function countzk(num_zk,buy_num)
{
    var zk="1";
    if(num_zk!="")
    {
        var zks=num_zk.split(",");
        for(var i=0;i<zks.length;i++)
        {
            if(zks[i]!="")
            {
               var zz=zks[i].split(":");
               if(parseInt(buy_num)>=parseInt(zz[0]))
               {
                  zk=zz[1];
               }
            }
        }
    }
    return zk;
}


 //计算总金额
function countallprice()
{
    var num_zk=document.getElementById("hid_num_zk").value;
    var pro_price=document.getElementById("hid_pro_price").value;
    var buy_num=document.getElementById("txt_buy_num").value;
    var mysl=document.getElementById("hid_no_post_num").value;
    var post_monery=document.getElementById("hid_post_monery").value;
    
    var gettype=document.getElementById("hid_get_type").value;
    
    var zk= countzk(num_zk,buy_num);
    var pay_all_monery=(parseFloat(pro_price)*parseInt(buy_num))*parseFloat(zk);
    
    if(gettype=="1")
    {
        if(parseInt(buy_num)<parseInt(mysl))
        {
            pay_all_monery+=parseFloat(post_monery);
        }
    }
    document.getElementById("hid_pay_monery").value=changeTwoDecimal_f(pay_all_monery);
    document.getElementById("sp_pay_monery").innerHTML=changeTwoDecimal_f(pay_all_monery)+"元";
}

//修改其他金额
function updateprice(price)
{
    document.getElementById("sp_pay_monery").innerHTML=price+"元";
    document.getElementById("hid_pro_price").value=price;
    countallprice();
}


//设置购买商品的描述信息
function setprodes()
{
    var sx_str="";
    var count=document.getElementById("hid_sx_count").value;
    if(count!="0")
    {
        for(var i=0;i<count;i++)
        {
            var type_name=document.getElementById("hid_ty_"+i).value;
            var ckb_s=document.getElementsByName("rdo"+i);
            for(var j=0;j<ckb_s.length;j++)
            {
                if(ckb_s[j].checked==true)
                {
                    var cs=ckb_s[j].value.split("|");
                    if(cs.length>1)
                    {
                        sx_str+="价格:"+parseFloat(cs[1]).toFixed(2)+";";
                        pro_price=cs[1];
                        sx_str+=type_name+":"+cs[0];
                    }
                    else
                    {
                        sx_str+=type_name+":"+ckb_s[j].value;
                    }
                }
            }
            sx_str+=";";
        }
    }
    else
    {
        pro_price=document.getElementById("sp_pro_price").value;
        sx_str+="价格:"+parseFloat(pro_price).toFixed(2)+";";
    }
    return sx_str;
}

//添加购物车
function innerbuyinfo()
{
    var pro_price=document.getElementById("hid_pro_price").value;
    var pro_id=document.getElementById("hid_pro_id").value;
    var user_id=document.getElementById("hid_user_id").value;
    var buy_num=document.getElementById("txt_buy_num").value;
    var pay_monery=document.getElementById("hid_pay_monery").value;
    var post_monery=document.getElementById("hid_post_monery").value;
    var no_post_num=document.getElementById("hid_no_post_num").value;
    
    if(user_id=="0")
    {
        alert("[提示]抱歉，您还没有登录，请登录后在进行操作");
        return;
    }
    
    var get_type=document.getElementById("hid_get_type").value;
    
    if(get_type=="1")
    {
        post_monery=document.getElementById("hid_post_monery").value;
        if(parseInt(buy_num)>=parseInt(no_post_num))
        {
            post_monery="0.00";
        }
    }
    else
    {
        post_monery="0.00";
    }
    
    var sx_str=setprodes();
    var result=Show.AddShopCar(pro_id, user_id, sx_str,buy_num,get_type, post_monery, pro_price,pay_monery).value;
    
    if(result=="1")
    {
        alert("[提示]该商品已成功添加到购物车");
        return;
    }
    else
    {
        alert("[提示]抱歉，系统异常，请联系管理员");
        return;
    }
}


//添加购物车
function addbuyinfo(id)
{
    var sx_str="";
    var pro_price=document.getElementById("hid_pro_price_"+id).value;
    var pro_id=document.getElementById("hid_pro_id_"+id).value;
    var user_id=document.getElementById("hid_user_id").value;
    var buy_num=1;
    var post_monery=document.getElementById("hid_post_monery_"+id).value;
    var get_type=document.getElementById("hid_get_type_"+id).value;
    
    if(user_id=="0")
    {
        alert("[提示]抱歉，您还没有登录，请登录后在进行操作");
        return;
    }
    sx_str+="价格:"+parseFloat(pro_price).toFixed(2)+";";
    
     $.ajax({ type: "POST", url: "ashx/AddProInCart.ashx", data: { pro_id: pro_id,user_id: user_id,op_des: sx_str,buy_num: buy_num,get_type: get_type,post_monery: post_monery,pro_price: pro_price, _: Math.random() },
        dataType: "html",
        success: function(msg) {
            if(msg=="1")
            {
                alert("[提示]该商品已成功添加到购物车");
                return;
            }
            else
            {
                alert("[提示]抱歉，系统异常，请联系管理员");
                return;
            }
        },
        error: function(msg) { alert("[提示]网络错误"+msg); return false; }
     });
}


//点击选项卡
function changetab(id)
{
    for(var i=1;i<=4;i++)
    {
        if(i==id)
        {
            document.getElementById("div_"+i).style.display="";;
            document.getElementById("li_"+i).className="img";
        }
        else
        {
            document.getElementById("div_"+i).style.display="none";
            document.getElementById("li_"+i).className="";
        }
    }
}


//修改购买数量
function changepronum(rec_no,type)
{
    var buynum=0;
    var post_str="";
    var all_str="";
    
    buynum=parseInt(document.getElementById("txt_bn_"+rec_no).value);
    
    if(type=="0")
    {
         buynum=parseInt(document.getElementById("txt_bn_"+rec_no).value)-1;
    }
    
    if(type=="1")
    {
         buynum=parseInt(document.getElementById("txt_bn_"+rec_no).value)+1;
    }
    
    if(parseInt(buynum)<1)
    {
        buynum="1";
    }
    
    document.getElementById("txt_bn_"+rec_no).value=buynum;
    var postmonery=document.getElementById("hid_pro_post_monery_"+rec_no).value;
    var porprice=document.getElementById("hid_pro_price_"+rec_no).value;
    var gettype=document.getElementById("hid_pro_gettype_"+rec_no).value;
    var nopostmoery=document.getElementById("hid_pro_nopost_"+rec_no).value;
    var num_zk=document.getElementById("hid_num_zk_"+rec_no).value;
    if(parseInt(gettype)==1&&parseInt(buynum)>=parseInt(nopostmoery))
    {
        postmonery="0.00";
        post_str="免邮";
    }
    else if(parseInt(gettype)==0)
    {
        postmonery="0.00";
        post_str="短信自提";
    }
    else
    {
        post_str="￥"+parseFloat(postmonery).toFixed(2)+"元";
    }
    var zk=countzk(num_zk,buynum);
    var summonery=((parseFloat(porprice)*parseInt(buynum))*parseFloat(zk))+parseFloat(postmonery);
    all_str="￥"+((parseFloat(porprice)*parseInt(buynum))*parseFloat(zk)).toFixed(2)+"元";
    
    var result= user_User_Cart.UpDateProNum(rec_no, buynum, postmonery, summonery).value;
    
    if(result=="0")
    {
        alert("[提示]系统异常");
        return;
    }
    
    document.getElementById("sp_allmonery_"+rec_no).innerHTML=all_str;
    document.getElementById("sp_postmonery_"+rec_no).innerHTML=post_str;
    
    countallmonery();
   
}

//删除购物车中商品
function delcart(rec_no)
{
   
    if(!confirm('[提示]您确定要从购物车中删除该商品吗?'))
    {
        return false;
    }
    var result=user_User_Cart.DelCartPro(rec_no).value;
    if(result=="1")
    {
        document.getElementById("tr_"+rec_no).style.display="none";
        document.getElementById("sp_pro_count").innerHTML=parseInt(document.getElementById("sp_pro_count").innerHTML)-1;
        countallmonery();
    }
    
}

//统计总价格和运费
function countallmonery()
{
    var userid=document.getElementById("hid_user_id").value;
    var sumpostmonery=user_User_Cart.CountAllPostMonery(userid).value;
    var sumpaymonery=user_User_Cart.CountAllPayMonery(userid).value;
    document.getElementById("sp_all_postmonery").innerHTML="￥"+parseFloat(sumpostmonery).toFixed(2)+"元";
    document.getElementById("sp_all_pro_monery").innerHTML="￥"+parseFloat(parseFloat(sumpaymonery)-parseFloat(sumpostmonery)).toFixed(2)+"元";;
    document.getElementById("sp_all_paymonery").innerHTML="￥"+parseFloat(sumpaymonery).toFixed(2)+"元";;
}


//获取市
function get_shi()
{
    var drp_sheng_list= document.getElementById("drp_sheng");
    var drp_sheng_listValue = drp_sheng_list.options[drp_sheng_list.selectedIndex].value;
    var shi_list= user_User_Cart.BindArea_Shi(drp_sheng_listValue).value
    $("#drp_shi").html(shi_list);
    get_qu();
  
}

//获取区
function get_qu()
{
    var drp_sheng_list= document.getElementById("drp_sheng");
    var drp_sheng_listValue = drp_sheng_list.options[drp_sheng_list.selectedIndex].value;
    var drp_shi_list= document.getElementById("drp_shi");
    var drp_shi_listValue = drp_shi_list.options[drp_shi_list.selectedIndex].value;
    var shi_list= user_User_Cart.BindArea_Qu(drp_sheng_listValue,drp_shi_listValue).value;
    $("#drp_qu").html(shi_list);
}

//验证支付
function validatepay()
{
    var password=document.getElementById("txt_pay_pass").value;
    if(password.Trim()=="")
    {
        alert("[提示]登录密码不可以为空");
        return false;
    }
    return true;
}

//保留两位小数
function changeTwoDecimal_f(x)
{
    var f_x = parseFloat(x);
    if (isNaN(f_x))
    {
        alert('function:changeTwoDecimal->parameter error：'+f_x);
        return false;
    }
    var f_x = Math.round(x*100)/100;
    var s_x = f_x.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0)
    {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2)
    {
         s_x += '0';
    }
    return s_x;
}

function fastbuy(id)
{
    var sx_str="";
    var pro_price=document.getElementById("hid_pro_price_"+id).value;
    var pro_id=document.getElementById("hid_pro_id_"+id).value;
    var user_id=document.getElementById("hid_user_id").value;
    var buy_num=1;
    var post_monery=document.getElementById("hid_post_monery_"+id).value;
    var get_type=document.getElementById("hid_get_type_"+id).value;
    
    if(user_id=="0")
    {
        alert("[提示]抱歉，您还没有登录，请登录后在进行操作");
        return;
    }
    sx_str+="价格:"+parseFloat(pro_price).toFixed(2)+";";
    
     $.ajax({ type: "POST", url: "ashx/AddProInCart.ashx", data: { pro_id: pro_id,user_id: user_id,op_des: sx_str,buy_num: buy_num,get_type: get_type,post_monery: post_monery,pro_price: pro_price, _: Math.random() },
        dataType: "html",
        success: function(msg) {
            if(msg=="1")
            {
                window.open("http://life.51qianyan.com/Cart.aspx");
                return;
            }
            else
            {
                alert("[提示]抱歉，系统异常，请联系管理员");
                return;
            }
        },
        error: function(msg) { alert("[提示]网络错误"+msg); return false; }
     });
}



