﻿function SetImageSrc(str)
{
    document.getElementById("ProductImageBig").src = str;
}

function SetImageSrcID(str,did)
{
    var imgs = document.getElementById(did).getElementsByTagName("img");
    imgs[1].src = imgs[2].src = str;
}

 function CurrencyEventChange(val,url){
     $.get(url,{Action:"get",id:val.value,time:Math.random()},
                                    function (data, textStatus)
                                    {
                                        location.href=location.href;
                                    });
                                }

function CurrencyLoad(url)
{
    $.get(url,{Action:"get",time:Math.random()},
                                    function (data, textStatus)
                                    {
                                        if(document.getElementById("SelectCurrency"))
                                            document.getElementById("SelectCurrency").value = data;
                                    }); 
}

        function FavoritenEventAdd(val,url){
            $.get(url, 
                {Action:"get",pid:val,time:Math.random()},
                function (data, textStatus){alert(data); });
        }
        
        function ShopCartAdd(val,url,url1){
            $.get(url, 
                {Action:"get",pid:val,time:Math.random()},
                function (data, textStatus){ location.href=url1; });
        }
        
        function ShopCartAddAll(url,url1){
            var pids = document.getElementsByName("HiddenFavoritenPID");
            var len = pids.length;
            
            if(len > 0)
            {
                var pp = "";
                for(var i=0;i<len;i++)
                {
                    pp += pids[i].value + ",";
                }

                $.get(url, 
                {Action:"get",pid:escape(pp),time:Math.random()},
                function (data, textStatus){                 
                 location.href=url1; });
            }
        }
        
         function PSizeJia(k,max)
        {
            var input = document.getElementById("ProductSizeInput" + k);
            input.value = PSizeNumberRegex(Number(input.value) + 1,max);
        }
        
        function PSizeJian(k,max)
        {
            var input = document.getElementById("ProductSizeInput" + k);
            input.value = PSizeNumberRegex(Number(input.value) - 1,max);
        }
        
        function PSizeNumberRegex(val,max)
        {
            var v = Number(val);
            
            if(v < 0)
                v = 0;
            else if(v > max)
                v = max;
                
            return v;
        }
        
        function ShopCartAddProductSize(){                       
            if(document.getElementById("TableProductSize")){                
                var list = document.getElementById("TableProductSize").getElementsByTagName("div");
                var spanlist = document.getElementById("TableProductSize").getElementsByTagName("span");
                var num = 0;
                var size = "";
                var b = false;
                document.getElementById("PNumber").value = "";
                document.getElementById("PSize").value = "";
                
                for(var i=0;i<list.length;i++)
                {
                    num = Number(list[i].getElementsByTagName("input")[0].value);
                   
                    if(isNaN(num))
                    {
                        alert(alertproductnumber);
                        list[i].getElementsByTagName("input")[0].focus();
                        return;
                    }
                    else
                    {
                        if(num > 0)
                        {                            
                            b = true;
                            size = spanlist[i].innerHTML;
                            document.getElementById("PNumber").value += num + ",";
                            document.getElementById("PSize").value += size + ",";                            
                        }   
                    }
                }                
            }
            
            return true;
        }

function deletefavoriten(sid,url,urlpage){
	$.get(url, 
	{Action:"get",id:sid,time:Math.random()},    
	function (data, textStatus){refreshpagefavoriten(urlpage);});
}

function refreshpagefavoriten(urlpage){
	$.get(urlpage, 
	{Action:"get",time:Math.random()},    
	function (data, textStatus){$("#favtolitenemplate").html(data); });
}

function CartAddressLoad()
{
    if(document.getElementById("isshow") && document.getElementById("bc"))
    {        
        if(document.getElementById("isshow").checked==false)
	        document.getElementById("bc").style.display='';
	    else
	        document.getElementById("bc").style.display='none';
    }
    
	var productamount = parseFloat($("#HiddenAmount").val());    
	    var cost = 0;    
	    var poundage = 0;    
	    var discount = 0;                               

        if(document.getElementById("CostCountry")){
	    if(document.getElementById("CostCountry").selectedIndex == 0)
		    $("#MemberCountry").val("");
	    else{
		    $("#MemberCountry").val($("#CostCountry").find("option:selected").text()); 
		    cost = parseFloat(document.getElementById("CostCountry").value);   
		}}
		
		if(document.getElementById("isshow"))
        {            
            if(document.getElementById("isshow").checked==false)
		    {
		       if(document.getElementById("CostCountry1")){
		            if(document.getElementById("CostCountry1").selectedIndex == 0)
		                $("#MemberCountry1").val("");
	                else{
		                $("#MemberCountry1").val($("#CostCountry1").find("option:selected").text()); 
		                cost = parseFloat(document.getElementById("CostCountry1").value); 		          
		            }  
    	        } 
		    }
		}
		
		$("#HiddenCostCountry").val(cost); 
		
	    var boolLogistics = true;
	    var boolPayment = true;
		
		$("input[name=LogisticsName]").each(function(i){
		    if(this.checked == true){
		        boolLogistics = false;
		    }
	    });
    								
	    $("input[name=PaymentName]").each(function(i){
		    if(this.checked == true){                                
			    boolPayment = false;                        
		    }                                                                 
	    });
    		                       	 
	    $("input[name=LogisticsName]").each(function(i){
	        if(boolLogistics == true && i == 0)
	            this.checked = true;	    
		    if(this.checked == true){
			    cost = (parseFloat(cost) + parseFloat($("#HiddenLogistics"+(i+1)).val())).toFixed(2);  
			    $("#HiddenCost").val(cost); 
			    $("#HiddenCostLogistics").val(parseFloat($("#HiddenLogistics"+(i+1)).val())); 
		    }
	    });
							
	    $("input[name=PaymentName]").each(function(i){
	        if(boolPayment == true && i == 0)
	            this.checked = true
	            
		    if(this.checked == true){                                
			    poundage = parseFloat($("#HiddenPoundage"+(i+1)).val()).toFixed(2);  
			    discount = parseFloat($("#HiddenDiscount"+(i+1)).val()).toFixed(2);  
			    $("#HiddenPoundage").val(poundage); 
			    $("#HiddenDiscount").val(discount);                                   
		    }                                                                 
	    });   
	    
	    
    	
	    $("#spanCost").html(cost);
	    $("#spanDiscount").html(parseFloat(100-discount).toFixed(2));  
	    $("#spanPoundage").html(poundage);  
	    $("#HiddenAmountOrder").val(parseFloat((productamount + parseFloat(cost)) 
		    * (100 + parseFloat(poundage)) / 100 * parseFloat(discount) / 100).toFixed(2));    	 
	     $("#spanAmount").html($("#HiddenAmountOrder").val());
}

function ShoppingLoad()
{
	    var productamount = parseFloat($("#HiddenAmount").val());    
	    var cost = parseFloat($("#HiddenCost").val()).toFixed(2);    
	    var poundage = parseFloat($("#HiddenPoundage").val()).toFixed(2);    
	    var discount = parseFloat($("#HiddenDiscount").val()).toFixed(2);
	    
        $("#spanCost").html(cost);
	    $("#spanPoundage").html(poundage);  
	    $("#spanDiscount").html(parseFloat(100-discount).toFixed(2));  
	    $("#HiddenAmountOrder").val(parseFloat((productamount + parseFloat(cost)) 
		        * (100 + parseFloat(poundage)) / 100 * parseFloat(discount) / 100).toFixed(2));  
        
        if(document.getElementById("IndentCoupon") && document.getElementById("IndentCouponUrl") && document.getElementById("IndentCoupon").value != "")
		{	   
		    $.get($("#IndentCouponUrl").val(), 
                {Action:"get",price:$("#HiddenAmountOrder").val(),number:$("#IndentCoupon").val(),time:Math.random()},
                function (data, textStatus){                 
                    $("#HiddenAmountOrder").val(data);
                    $("#SpanAmountOrder").html(data);
                });
		}else
		{
		    $("#SpanAmountOrder").html($("#HiddenAmountOrder").val());
		}
}

function addemailrss(url){    
    if(IsRegExp("EmailRssText",regexemail))
    {
        alert(alertEmail);
        $("#EmailRssText").select();
        return;        
    }else
    {
        $.get(url, 
	    {Action:"get",email:$("#EmailRssText").val(),time:Math.random()},    
	    function (data, textStatus){alert(data)}); 
    }
}

function addemailrss(url,id){    
    if(IsRegExp(id,regexemail))
    {
        alert(alertEmail);
        $("#" + id).select();
        return;        
    }else
    {
        $.get(url, 
	    {Action:"get",email:$("#"+id).val(),time:Math.random()},    
	    function (data, textStatus){alert(data)}); 
    }
}

function deleteshopcart(sid,url,urlpage){
	$.get(url, 
	{Action:"get",id:sid,time:Math.random()},    
	function (data, textStatus){refreshpageshopcart(urlpage);});
}

function refreshpageshopcart(urlpage){
	$.get(urlpage, 
	{Action:"get",time:Math.random()},    
	function (data, textStatus){$("#carttemplate").html(data); });
}

function updateshopcart(i,url,urlpage){
	var id = $("#InputShopCartListID" + i).val();
	var num = $("#InputShopCartListNumber"+i).val();     
	var color = "";
	if(document.getElementById("InputShopCartListColor" + i))       
	    color = $("#InputShopCartListColor" + i).val();
	
	var size = "";
	if(document.getElementById("InputShopCartListSize" + i))     
	    size = $("#InputShopCartListSize" + i).val();

	$.get(url,{Action:"get",id:id,num:num,color:color,size:size,time:Math.random()},    
	    function (data, textStatus){refreshpageshopcart(urlpage);});
}

function updateallshopcart(url,urlpage){
	var b = true;
	var id = "";
	var num = "";
	var color = "";
	var size = "";
	var temp = 0;
	
	$(".InputShopCartList").each(function(i){
	   i++;
	   temp = $("#InputShopCartListNumber"+i).val(); 
	   
	   if(isNaN(temp))
	   {
			$("#InputShopCartListNumber"+i).select();
			b = false;
	   }
	   else if(Number(temp) <= 0)
	   {
			$("#InputShopCartListNumber"+i).select();
			b = false;
	   }  
	   else
	   {
		   id += $("#InputShopCartListID" + i).val() + ",";
		   num += $("#InputShopCartListNumber"+i).val() + ",";            
		   color += $("#InputShopCartListColor" + i).val() + ",";
		   size += $("#InputShopCartListSize" + i).val() + ",";
	   }              
	});
	
	if(b == true)
	{
		if(id != "")
		{
			id = id.substring(0,id.lastIndexOf(','));
			num = num.substring(0,num.lastIndexOf(','));
			color = color.substring(0,color.lastIndexOf(','));
			size = size.substring(0,size.lastIndexOf(','));
			
			id = id.replace(/undefined/g,'0');
			num = num.replace(/undefined/g,'0');
			color = color.replace(/undefined/g,'');
			size = size.replace(/undefined/g,'');			
		}
		
		$.get(url, 
				{Action:"get",id:id,num:num,color:color,size:size,time:Math.random()},    
				function (data, textStatus){refreshpageshopcart(urlpage);});
	}
}

 function CategoryLD(sel,k,url){
            $.get(url, 
                {Action:"get",categoryid:sel.value,time:Math.random()},
                function (data, textStatus){
                    var model = data.split('|');                    
                    var drop = document.getElementById("SearchCategory" + k);
                    var str;
                    drop.length = 1;
                    drop.selectedIndex = 0;
                    
                    if(k == 2)
                    {
                       document.getElementById("SearchCategory3").length = 1;  
                       document.getElementById("SearchCategory3").selectedIndex = 0;  
                    }

                    for(var i=0; i<model.length; i++)
　　　　            {
　　　　                if(model[i] != ""){
　　　　                    str = model[i].split(',');
　　　　　　                drop.options.add(new Option(str[1],str[0]));
　　　　　　            }
　　　　            }　
                });
        }
