﻿// JScript File
//==============================================================================================
//Update By: Ruby Gao(Gao Xiang)
//Update Date: 2009-11-19
//Description: Modify "ChangeCount" method to enable multiple order items.
//==============================================================================================
//Update By Ruby Gao on 2009-11-19 to enable multiple order items

//========================Clear shopping cart ========================
function ClearShopCart() {
    SetCookie("ShoppingCart","",168);
    return true;
}

//Delete by Ruby Gao on 2009-11-26 
//function Compare_CheckSetShopCartCookieValue(name,value,expires)
//{
//	var nameCookieValue = GetCookie(name);
//	if ((nameCookieValue == "") || (nameCookieValue == null))
//	{
//		var exp = new Date();
//		exp.setTime(exp.getTime()+expires*60*60*1000);
//		document.cookie=name+"="+escape(EncodeCookie(value))+";path=/;expires="+exp.toGMTString();
//	}
//	else
//	{
//		 	var arrCookies = nameCookieValue.split(",");
//			var ValueNum = arrCookies.length;
//	 	
//			nameCookieValue = "";
//			for (loop=0; loop < ValueNum; loop++)
//			{
//				nameCookieValue +=  arrCookies[loop] + ",";
//		    }
//			var exp = new Date();
//			exp.setTime(exp.getTime()+expires*60*60*1000);
//			document.cookie=name+"="+escape(EncodeCookie(value+","+nameCookieValue))+";path=/;expires="+exp.toGMTString();
//		 
//	}
//}

//Update By Ruby Gao on 2009-11-26
function RefreshBind(sender,imgSrcId, imgId,hidShopCartItemID,hidDefaultCMPPID,selectedCMPPID,hidPrice,hidPromotionPrice,hideDiscount,lblPrice,lblPromotionPrice,txtNum,lblTotal) {

    if(sender.checked) {
        document.getElementById(imgId).src = document.getElementById(imgSrcId).innerText;
    }
    var shopCartItemID = document.getElementById(hidShopCartItemID).value.replace(/ /g,"");
    var newPrice = document.getElementById(hidPrice).value.replace(/ /g,"");
    var newPromotionPrice = document.getElementById(hidPromotionPrice).value.replace(/ /g,"");
    var newDiscount = document.getElementById(hideDiscount).value.replace(/ /g,"");
    
    var lblPrice = document.getElementById(lblPrice);
    var lblPromotionPrice = document.getElementById(lblPromotionPrice);
    
    var txtNum = document.getElementById(txtNum);
    var total = document.getElementById(lblTotal);
    
    var truePrice;
    if(txtNum.value.replace(/ /g,"").length == 0 ) {
        txtNum.value = 1;
    }
    
    if(newPrice.length > 0) {
        lblPrice.innerText = formatCurrency(newPrice)
    }
    
    if(newPromotionPrice > 0) {
        if(newDiscount > 0) {
            truePrice = newPromotionPrice - newDiscount;
        }else {
            truePrice = newPromotionPrice
        }
        lblPromotionPrice.innerText = formatCurrency(truePrice)
        total.innerText = formatCurrency(truePrice * txtNum.value)
    }else {
        if(newDiscount > 0) {
            truePrice = newPrice-newDiscount;
        }else {
            truePrice = newPrice
        }
        total.innerText = formatCurrency(truePrice * txtNum.value)
    }
    
    var shopCarts = new Array();
    
    var shopCart = GetCookie("ShoppingCart");
    if (shopCart != null && shopCart.length > 0) {
    
        var items = shopCart.split(",");
        for(i = 0; i< items.length; i++) {
            var orderItem = items[i];
            if (orderItem != null && orderItem.length > 0) {
                var keys = orderItem.split("|");
                if(keys[0] == shopCartItemID) {
                    var newItem = keys[0] + "|" + keys[1] + "|" + keys[2] + "|" +  selectedCMPPID + "|" + keys[4] + "|" +  keys[5] + "|" +  keys[6];
                    shopCarts.push(newItem);
                }else {
                    shopCarts.push(orderItem);
                }
            }
        }
    }
    
    var orders = shopCarts.join();
    SetCookie("ShoppingCart",orders,1);  
    GetSum();
}

//Update By Ruby Gao on 2009-11-19 to enable multiple order items.
function ChangeCount(obj,shopCartItemID,lblPriceID,lblPromotionPriceID,lblTotalID) {
    
    var count = obj.value;
    var price = document.getElementById(lblPriceID).innerText.replace(/\$|\,/g,"");
    var promotionPrice = document.getElementById(lblPromotionPriceID).innerText.replace(/\$|\,/g,"");
    var lblTotal = document.getElementById(lblTotalID);
    if(count ==0) {
        obj.value = 1;
        count = 1;
    }
    if(promotionPrice.length > 0) {
        lblTotal.innerText = formatCurrency(promotionPrice*count);
    }else {
        if(price.length > 0) {
            lblTotal.innerText = formatCurrency(price*count);
        }
    }
    
    var shopCarts = new Array();
    
    var shopCart = GetCookie("ShoppingCart");
    if (shopCart != null && shopCart.length > 0) {
    
        var items = shopCart.split(",");
        for(i=0 ; i< items.length ; i++) {
        
            var orderItem = items[i];
            if (orderItem != null && orderItem.length > 0) {
                var keys = orderItem.split("|");
                if(keys[0] == shopCartItemID) {
                    var newItem = keys[0] + "|" + keys[1] + "|" + keys[2] + "|" +  keys[3] + "|" + count.toString() + "|" +  keys[5] + "|" +  keys[6];
                    shopCarts.push(newItem);
                }else {
                    shopCarts.push(orderItem);
                }
            }
        }
    }
    
    var orders = shopCarts.join();
    SetCookie("ShoppingCart",orders,1);    
    GetSum();
}
 
function PastProductNum() {

    var trimNum = clipboardData.getData('text').replace(/[^\d]/g,'');
    clipboardData.setData('text',trimNum);
    //if(trimNum.length = 0) {
    if(trimNum.length == 0) {
        clipboardData.setData('text',"1");
    }
}

function formatCurrency(num) {

    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0, num.length - (4*i+3)) + ','+
    num.substring(num.length-(4*i+3));
    //return (((sign)?'':'-') + '$' + num + '.' + cents); //$##,###,###.00
    return (((sign)?'':'-') + '$' + num );   //$##,###,###
}
    
function DelShopCartItem(shopCartItemId, sender, totalID) {
    
    var subTotal = document.getElementById(totalID).innerText.replace(/\$|\,/g,"");
    var total = document.getElementById("ctl00_cph_ucShoppingCartList_lblSum").innerText.replace(/\$|\,/g,"");

    var shopCarts = new Array();
    
    var shopCart = GetCookie("ShoppingCart");
    if (shopCart != null && shopCart.length > 0) {
        var items = shopCart.split(",");   
        //var tempStr = "";      
        for(i = 0; i < items.length; i++) {
           
            if(items[i].replace(/ /g,"").length > 0) {
                var keys = items[i].split("|");
                if(keys[0] != shopCartItemId) {
                    //tempStr = tempStr + items[i] + ",";
                    shopCarts.push(items[i]);
                }             
            } 
        }
    }
    
    var orders = shopCarts.join();
    SetCookie("ShoppingCart",orders,168);
    GetSum();
    //=========================================
    var prodItem = sender.parentNode.parentNode;
    prodItem.removeNode(true);
    document.getElementById("ctl00_cph_ucShoppingCartList_lblSum").innerText = formatCurrency(total -subTotal)
}
    
    
function GetSum() {
    
    var sum = 0;
    var spans  = document.getElementsByTagName("span");
    for(i=0; i < spans.length; i++) {
     
        if(spans[i].id.indexOf("lblTotal") != -1) {
            var total = document.getElementById(spans[i].id).innerText.replace(/\$|\,/g,"");
            if(total.length > 0) {
                sum = sum + new Number(total);                
            }                
        }        
    }
    document.getElementById('ctl00_cph_ucShoppingCartList_lblSum').innerText = formatCurrency(sum);
}
    
function CheckShopCart(prodCount,language) {
    if(prodCount > 0) {
        return true;
    }
    else {
        if (language == "CHI") {
            alert("您還未選購任何產品！");
        }
        else {
            alert("There is no product in your shopping cart!");
        }
        return false;
    }
}
    
//====================================================================================================================
    
//========================================shopping Cart Form====================
function IsMoreTime() {
    var ddlReservationTime = document.getElementById('<%= ddlReservationTime.ClientID %>');
    if(ddlReservationTime.value == "moreTimeSlot"){
	   
	    return true;
    }
    return false;
}

 
