﻿
var inputtitle='Invalid input';
var inputmsg1="Please input a positive number.";
var inputmsg2="Input quantity does not meet the min order quantity requirement, please retry.";
var inputmsg3="The online system limits the order's max. amount to $100,000,000 . You can contact us for more discounts for big order.";
var inputmsg4="The online system limits the max. quantity to 999,999 . You can contact us for more discounts for big order.";
var inputmsg5="Your order quantity needs to be a multiple of the quantity of per box."
var maxOrderAmount=99999999.99;

function setDefaultOptions()
{
     var newOptions = {
//        domain: 'www.builderelements.com',
        path: '/',
        hoursToLive: 7*24
      }
      
      jQuery.cookies.setOptions(newOptions);
      
      return;
}

//ShoppingCart:  pid1,qty1,issample1,unionid1,cat1.1,cat1.2;pid2,qty2,issample2,unionid2,cat2.1,cat2.2;pid3,qty3,issample13,unionid3,cat3.1,cat3.2
//add_or_delete: 1 : add , 0: delete
//param: integer
function add_or_delete_ShoppingCartItem(int_pid,int_qty,int_issample,int_unionid,int_catid1,int_catid2,add_or_delete)
{
    var pid=int_pid.toString();
    var qty=int_qty.toString();
    var issample=int_issample.toString();
    
    var unionid=int_unionid.toString();
    var catid1=int_catid1.toString();
    var catid2=int_catid2.toString();
    
    setDefaultOptions();        
      
    var str_shoppingCartCount=jQuery.cookies.get("shoppingCartCount"); 
    var shoppingCartItems=jQuery.cookies.get("shoppingCartItems"); 
    
    //alert('shoppingCartCount='+shoppingCartCount+'        shoppingCartItems='+shoppingCartItems);
    
    if (!str_shoppingCartCount)
        str_shoppingCartCount='0';
    if (!shoppingCartItems)
        shoppingCartItems="";
    
    var shoppingCartCount=parseInt(str_shoppingCartCount);
    
    if (shoppingCartCount==0)
    {
        if (add_or_delete==1) //add
        {
            var newShoppingCartItems=pid+","+issample+","+qty+","+unionid+","+catid1+","+catid2+";"
            jQuery.cookies.set("shoppingCartItems",newShoppingCartItems); 
            shoppingCartCount=1;
            jQuery.cookies.set("shoppingCartCount",shoppingCartCount.toString()); 
            
            jQuery.get('/caches/orderrequest.ashx?CartItems='+newShoppingCartItems);
        }
                
        // update 
        updateCartWishListCount();  /* in header.ascx*/

        return;
    }
    
    var items=shoppingCartItems.split(";");
    
    //alert('items.length='+items.length+'        shoppingCartCount='+shoppingCartCount);
    
    var newshoppingCartItems="";
    
    if (items.length - 1 != shoppingCartCount)
    {
        if (add_or_delete==1) //add
        {
            newshoppingCartItems=pid+","+issample+","+qty+","+unionid+","+catid1+","+catid2+";"
            jQuery.cookies.set("shoppingCartItems",newshoppingCartItems); 
            shoppingCartCount=1;
            jQuery.cookies.set("shoppingCartCount",shoppingCartCount.toString()); 
            
            jQuery.get('/caches/orderrequest.ashx?CartItems='+newShoppingCartItems);
        }
        else //delete
        {
            jQuery.cookies.set("shoppingCartItems",""); 
            jQuery.cookies.set("shoppingCartCount",'0');
             
            jQuery.get('/caches/orderrequest.ashx?CartItems=');
        }
        
        // update 
        updateCartWishListCount();  /* in header.ascx*/

        return;
    }

    var addflag=false;
    
    var newshoppingCartCount=0;
    for (var ii=0;ii<items.length - 1;ii++)
    {
        var subitems=items[ii].split(",");
        
        //alert('subitems.length='+subitems.length);
        
        if (subitems.length == 6)
        {
            
            if ((subitems[0]==pid) && (subitems[1]==issample))
            {
                if (add_or_delete==1) //add
                {
                    // dont change the seq.
                    newshoppingCartItems=newshoppingCartItems+pid+","+issample+","+qty+","+subitems[3]+","+catid1+","+catid2+";"
                    newshoppingCartCount++;
                 }
                 
                 // if delete , throw it   
                 addflag=true;
            }
            else
            {
                newshoppingCartItems=newshoppingCartItems+subitems[0]+","+subitems[1]+","+subitems[2]+","+subitems[3]+","+subitems[4]+","+subitems[5]+";"
                newshoppingCartCount++;
            }
        }
        
    }    
            
    if ((add_or_delete==1) && (addflag==false))
    {
        newshoppingCartItems=newshoppingCartItems+pid+","+issample+","+qty+","+unionid+","+catid1+","+catid2+";"
        newshoppingCartCount++;
    }

    //alert('newshoppingCartCount='+newshoppingCartCount.toString()+'        newshoppingCartItems='+newshoppingCartItems);

    jQuery.cookies.set("shoppingCartItems",newshoppingCartItems); 
    jQuery.cookies.set("shoppingCartCount",newshoppingCartCount.toString()); 
    
    jQuery.get('/caches/orderrequest.ashx?CartItems='+newshoppingCartItems);
    
    // update 
    updateCartWishListCount();  /* in header.ascx*/
}


//wishList:  pid1;pid2;pid3
//add_or_delete: 1 : add , 0: delete
function add_or_delete_WishListItem(int_pid,add_or_delete)
{
    var pid=int_pid.toString();
    
    setDefaultOptions();        
      
    var str_wishListCount=jQuery.cookies.get("wishListCount"); 
    var wishListItems=jQuery.cookies.get("wishListItems"); 
    
    //alert('wishListCount='+wishListCount+'        wishListItems='+wishListItems);
    
    if (!str_wishListCount)
        str_wishListCount='0';
    if (!wishListItems)
        wishListItems="";
    
    
    var wishListCount=parseInt(str_wishListCount);
    
    if (wishListCount==0)
    {
        if (add_or_delete==1) //add
        {
            var newShoppingCartItems=pid+";"
            jQuery.cookies.set("wishListItems",newShoppingCartItems); 
            wishListCount=1;
            jQuery.cookies.set("wishListCount",wishListCount.toString()); 
        }
                
        // update 
        updateCartWishListCount();  /* in header.ascx*/

        return;
    }
    
    var items=wishListItems.split(";");
    
    //alert('items.length='+items.length+'        wishListCount='+wishListCount);
    
    var newwishListItems="";
    
    if (items.length - 1 != wishListCount)
    {
        if (add_or_delete==1) //add
        {
            newwishListItems=pid+";"
            jQuery.cookies.set("wishListItems",newwishListItems); 
            wishListCount=1;
            jQuery.cookies.set("wishListCount",wishListCount.toString()); 
        }
        else //delete
        {
            jQuery.cookies.set("wishListItems",""); 
            jQuery.cookies.set("wishListCount",'0'); 
        }        

        // update 
        updateCartWishListCount();  /* in header.ascx*/

        return;
    }

    var newwishListCount=0;
    for (var ii=0;ii<items.length - 1;ii++)
    {
        if (items[ii]==pid)
        {
                ; // add : put in the back,  delete: throw it
        }
        else
        {
            newwishListItems=newwishListItems+items[ii]+";";
            newwishListCount++;
        }
    }    
            
    if (add_or_delete==1)
    {
        newwishListItems=newwishListItems+pid+";";
        newwishListCount++;
    }

    //alert('newwishListCount='+newwishListCount+'        newwishListItems='+newwishListItems);

    jQuery.cookies.set("wishListItems",newwishListItems); 
    jQuery.cookies.set("wishListCount",newwishListCount.toString()); 

    // update 
    updateCartWishListCount();  /* in header.ascx*/
}

//param: string
function resetCookie(cookiename,resetvalue)
{
    setDefaultOptions();        
      
    jQuery.cookies.set(cookiename,resetvalue); 
}

//param: string
function getCookie(cookiename,defaultValue)
{

    setDefaultOptions();        
     
    var getValue=jQuery.cookies.get(cookiename); 
    if (!getValue)
        return defaultValue;
        
   return getValue;

}

//
function getShoppingCartItemCount()
{
    var count_str=getCookie('shoppingCartCount','0');
    return parseInt(count_str);
}

function resetShoppingCartItem()
{
    resetCookie('shoppingCartItems','');
    resetCookie('shoppingCartCount','0');
}

//
function getWishListItemCount()
{
    var count_str=getCookie('wishListCount','0');
    return parseInt(count_str);
}

function resetWishListItem()
{
    resetCookie('wishListItems','');
    resetCookie('wishListCount','0');
}


// true: has exist   false: not exist
// param: string
function check_ShoppingCartItem(pid,issample)
{
    var unionid=0;
    var catid1=0;
    var catid2=0;
    
    setDefaultOptions();        
      
    var str_shoppingCartCount=jQuery.cookies.get("shoppingCartCount"); 
    var shoppingCartItems=jQuery.cookies.get("shoppingCartItems"); 
    
    //alert('shoppingCartCount='+shoppingCartCount+'        shoppingCartItems='+shoppingCartItems);
    
    if (!str_shoppingCartCount)
        str_shoppingCartCount='0';
    if (!shoppingCartItems)
        shoppingCartItems="";
    
    var shoppingCartCount=parseInt(str_shoppingCartCount);
    
    if (shoppingCartCount==0)
    {
        return false;
    }
    
    var items=shoppingCartItems.split(";");
    
    if (items.length - 1 != shoppingCartCount)
    {
        jQuery.cookies.set("shoppingCartItems",""); 
        jQuery.cookies.set("shoppingCartCount",'0'); 
        
        return false;
    }

    for (var ii=0;ii<items.length - 1;ii++)
    {
        var subitems=items[ii].split(",");
        
        if (subitems.length == 6)
        {
            
            if ((subitems[0]==pid) && (subitems[1]==issample))
            {
                return true;
            }
        }
        
    }    

    return false;            
}

// true: has exist   false: not exist
// pid: string
function check_WishListItem(pid)
{
    setDefaultOptions();        
      
    var str_wishListCount=jQuery.cookies.get("wishListCount"); 
    var wishListItems=jQuery.cookies.get("wishListItems"); 
    
    //alert('wishListCount='+wishListCount+'        wishListItems='+wishListItems);
    
    if (!str_wishListCount)
        str_wishListCount='0';
    if (!wishListItems)
        wishListItems="";
    
    var wishListCount=parseInt(str_wishListCount);
    
    if (wishListCount==0)
    {
        return false;
    }
    
    var items=wishListItems.split(";");
    
    //alert('items.length='+items.length+'        wishListCount='+wishListCount);
    
    var newwishListItems="";
    
    if (items.length - 1 != wishListCount)
    {
        jQuery.cookies.set("wishListItems",""); 
        jQuery.cookies.set("wishListCount",'0'); 

        return false;
    }

    for (var ii=0;ii<items.length - 1;ii++)
    {
        if (items[ii]==pid)
        {
            return true;
        }
    }    

    return false;            
}

//first, qty should by a string
// 0: less than minQty
// 1: A qty
// 2: B qty
// 3: (hasa==0) && (hasb==0)
// 4: not a positive integer or 0
function checkQty(qtystring)
{
    if (!checkPositiveInteger(qtystring))
        return 4;
    
    /*v2.0*/    
    var qty=parseFloat(qtystring);
        
    var hasa=parseInt(jQuery('#hasa').val());
    var aqty=parseFloat(jQuery('#aqty').val());
    var hasb=parseInt(jQuery('#hasb').val());
    var bqty=parseFloat(jQuery('#bqty').val());
    
    if ((hasa==0) && (hasb==0))
        return 3;
        
    if ((hasa==1) && (hasb==0))
    {
        if (qty<aqty)
            return 0;
            
        return 1;
    }
    if ((hasa==0) && (hasb==1))
    {
        if (qty<bqty)
            return 0;
            
        return 2;
    }
    
    if (aqty <= bqty)
    {
        if (qty < aqty)
            return 0;
        if (qty < bqty)
            return 1;
        return 2;       
    }
    
    //else
    
    if (qty < bqty)
        return 0;
    if (qty < aqty)
        return 2;
    return 1;       
    
}


/*  dahe@homeelements.us
    2011-06-22      */ 
function minQtyValid(qty){
    var hasa=jQuery('#hasa').val()=='1',
        aqty=parseFloat(jQuery('#aqty').val()),
        hasb=jQuery('#hasb').val()=='1',
        bqty=parseFloat(jQuery('#bqty').val());

    if(hasa&&hasb){
        if(qty<aqty&&aqty<bqty)
            return false;
        if(qty<bqty&&bqty<aqty)
            return false;
            
        return true;     
    }
    
    if(hasa){
        if(qty<aqty)
            return false;
            
        return true;
    }
    
    if(hasb){
        if(qty<bqty)
            return false;
            
        return true;
    }
    
    return false;
}


function formatQty(str_isInteger,str_Qty)
{
    
    if ((str_isInteger=='1') || (str_isInteger=='3'))
    {
        var qty_1=parseFloat(str_Qty);
        var qty_2=Math.ceil(qty_1);
       return qty_2.toString();
    }
    else
        return str_Qty;
}

function setMinQty(qtyobj)
{
    var ret=checkQty(qtyobj.value);
    if (ret==1)
        qtyobj.value=formatQty(jQuery("#isInteger").val(),jQuery('#aqty').val());
    if (ret==2)
        qtyobj.value=formatQty(jQuery("#isInteger").val(),jQuery('#bqty').val());
    if ((ret==0)||(ret==4))
    {
        var hasa=parseInt(jQuery('#hasa').val());
        var aqty=parseFloat(jQuery('#aqty').val());
        var hasb=parseInt(jQuery('#hasb').val());
        var bqty=parseFloat(jQuery('#bqty').val());

        if ((hasa==0) && (hasb==0))
            qtyobj.value="";
            
        if ((hasa==1) && (hasb==0))
        {
            qtyobj.value=formatQty(jQuery("#isInteger").val(),jQuery('#aqty').val());
            return;
        }
        if ((hasa==0) && (hasb==1))
        {
            qtyobj.value=formatQty(jQuery("#isInteger").val(),jQuery('#bqty').val());
            return;
        }
        
        if (aqty <= bqty)
        {
            qtyobj.value=formatQty(jQuery("#isInteger").val(),jQuery('#aqty').val());
            return;
        }
        
        //else
        qtyobj.value=formatQty(jQuery("#isInteger").val(),jQuery('#bqty').val());
        return;
    }
}

function onFocusQty_Buy(qtyobj)
{
    nextNode(qtyobj).style.display='block';
    jQuery('#errorInput_div_buy').css('display','none');
}

function changeQty(qtyobj)
{
    
    var qty=qtyobj.value;
    
    //format xxx.yy

    if (checkPositiveInteger(qty)==false)
    {
        jQuery('#errorInput_div_buy').html(inputmsg1);
        jQuery('#errorInput_div_buy').css('display','block');
        
        setMinQty(qtyobj);
        return false;
    }
    
    var qty_2=parseFloat(qty);
    
    if (jQuery("#isInteger").val()=='1')
    {
       qty_2=Math.ceil(qty_2);
    }
    else
    if (jQuery("#isInteger").val()=='2')
    {
       var maxbox=parseFloat(jQuery('#maxboxcapacity').val());
       var qty_3=(Math.ceil(qty_2/maxbox)*maxbox).toFixed(2);
       if(qty_3>qty_2+0.1)
       {
            jQuery('#errorInput_div_buy').html(inputmsg5 + '( ' + jQuery("#boxSpec").text() +' )');
            jQuery('#errorInput_div_buy').css('display','block');
       }
       
       qty_2=qty_3;
    }
    else
    if (jQuery("#isInteger").val()=='3')
    {
       var maxbox=parseFloat(jQuery('#maxboxcapacity').val());
       var qty_3=Math.ceil(qty_2/maxbox)*maxbox;
       
       if(qty_3>qty_2+0.1)
       {
            jQuery('#errorInput_div_buy').html(inputmsg5 + '( ' + jQuery("#boxSpec").text() +' )');
            jQuery('#errorInput_div_buy').css('display','block');
       }
       
       qty_2=qty_3;
    }
    
    if (qty_2 >= 999999)
    {
        jQuery('#errorInput_div_buy').html(inputmsg4);
        jQuery('#errorInput_div_buy').css('display','block');

        
        setMinQty(qtyobj);
        return false;
    }
    
    qtyobj.value=qty_2.toString();
    var qty=qtyobj.value;

    var ret=checkQty(qty),price,amount;
    
    if (ret==0)
    {
        jQuery('#errorInput_div_buy').html(inputmsg2);
        jQuery('#errorInput_div_buy').css('display','block');
        
        setMinQty(qtyobj);
        return false;
    }
    if (ret==4)
    {
        jQuery('#errorInput_div_buy').html(inputmsg1);
        jQuery('#errorInput_div_buy').css('display','block');
        
        setMinQty(qtyobj);
        return false;
    }
        
    if(ret==1){
        price=parseFloat(jQuery('#aprice').val());
        amount=price*parseFloat(qty);
        jQuery('#currentprice').text(fomatMoney(jQuery('#aprice').val()));
        jQuery('#currentamount').text(fomatMoney(amount.toString()));
//        jQuery('#currentmin').text(jQuery('#aqty').val());
        
        jQuery('#div_aprice').addClass('selectedprice')
        jQuery('#div_bprice').removeClass('selectedprice')
        
        if (jQuery('#imga').length>0)
        {
            jQuery('#imga').show();
            jQuery('#imgb').hide();
        }
    }
    if(ret==2){
        price=parseFloat(jQuery('#bprice').val());
        amount=price*parseFloat(qty);
        jQuery('#currentprice').text(fomatMoney(jQuery('#bprice').val()));
        jQuery('#currentamount').text(fomatMoney(amount.toString()));
//        jQuery('#currentmin').text(jQuery('#bqty').val());
        
        jQuery('#div_bprice').addClass('selectedprice')
        jQuery('#div_aprice').removeClass('selectedprice')

        if (jQuery('#imgb').length>0)
        {
            jQuery('#imgb').show();
            jQuery('#imga').hide();
        }
    }
    
    
}

//pid: integer
//qty: string
function addToShoppingCart(pid,qty)
{

    var ret=checkQty(qty);
    if (ret==0)
    {
        setMinQty(qtyobj);
        return false;
    }
    if (ret==4)
    {
        setMinQty(qtyobj);
        return false;
    }
    
    /*v2.0*/
    add_or_delete_ShoppingCartItem(pid,parseFloat(qty),0,0,0,0,1);
    
    var myDate = new Date();
    location.href='/cart.aspx?r='+myDate.getTime().toString();
}

//pid: integer
//qty: integer ---**
function addToShoppingCart2(pid,qty)
{
    jQuery("#cart_"+pid.toString())[0].src= '/images/addedtocart.gif';
    jQuery("#cart_"+pid.toString()).css("cursor","default");
    add_or_delete_ShoppingCartItem(pid,qty,0,0,0,0,1);

    
    top.location.href='/cart.aspx'; 
}


//pid: integer
function addToShoppingSample(pid){
    if (jQuery('#li_add_to_sample > img')[0].src.indexOf("sampleadded")<0)
    {
        add_or_delete_ShoppingCartItem(pid,1,1,0,0,0,1);
        jQuery('#li_add_to_sample > img')[0].src='/images/sampleadded.gif';
        jQuery('#li_add_to_sample > img').css("cursor","default");
        top.location.href='/cart.aspx';
    }
}

//pid: integer
function addToShoppingSample2(pid){
    add_or_delete_ShoppingCartItem(pid,1,1,0,0,0,1);
    jQuery("#sample_"+pid.toString())[0].src= '/images/sampleadded.gif';
    jQuery("#sample_"+pid.toString()).css("cursor","default");
    top.location.href='/cart.aspx';
}

//pid: integer
function addToWishList(pid){

    if (jQuery('#li_add_to_wish_list > img')[0].src.indexOf("wishlistadded")<0)
    {
        add_or_delete_WishListItem(pid,1);
        jQuery('#li_add_to_wish_list > img')[0].src='/images/wishlistadded.gif';
        jQuery('#li_add_to_wish_list > img').css("cursor","default");
        top.location.href='/wish-list.aspx';
    }
}
//pid: integer
function addToWishList2(pid){
    add_or_delete_WishListItem(pid,1);
    jQuery("#wishlist_"+pid.toString())[0].src= '/images/wishlistadded.gif';
    jQuery("#wishlist_"+pid.toString()).css("cursor","default");
    top.location.href='/wish-list.aspx';
}


//--mycart
function checkPositiveInteger(qtystring)
{
    if (!qtystring)
    {
        return false;
    }
    if (qtystring.length==0)
    {
        return false;
    }
    
    //var ii=/^[1-9]\d*$/.test(qtystring);
    /*v2.0*/
    var ii=/^((\d+\.\d*[1-9]\d*)|(\d*[1-9]\d*\.\d+)|(\d*[1-9]\d*))$/.test(qtystring);
    if (!ii)
    {
        return false;
    }
        
    if (parseFloat(qtystring)<=0.01)
    {
        return false;
    }
        
    return true;
}

// param: string
function getQtyFromCookie(pid,issample)
{
    setDefaultOptions();        
      
    var str_shoppingCartCount=jQuery.cookies.get("shoppingCartCount"); 
    var shoppingCartItems=jQuery.cookies.get("shoppingCartItems"); 
    
    if (!str_shoppingCartCount)
        str_shoppingCartCount='0';
    if (!shoppingCartItems)
        shoppingCartItems="";
    
    var shoppingCartCount=parseInt(str_shoppingCartCount);
    
    if (shoppingCartCount==0)
    {
        return 0;
    }
    
    var items=shoppingCartItems.split(";");
    
    if (items.length - 1 != shoppingCartCount)
    {
        jQuery.cookies.set("shoppingCartItems",""); 
        jQuery.cookies.set("shoppingCartCount",'0'); 
        
        return 0;
    }

    for (var ii=0;ii<items.length - 1;ii++)
    {
        var subitems=items[ii].split(",");
        
        if (subitems.length == 6)
        {
            
            if ((subitems[0]==pid) && (subitems[1]==issample))
            {
                /*v2.0*/
                return parseFloat(subitems[2]);
            }
        }
        
    }    

    return 0;            

}

//first, qty should by a string
// 0: less than minQty
// 1: A qty
// 2: B qty
// 3: (hasa==0) && (hasb==0)
function checkQty_pid_issample(pid,issample,qtystring)
{

    //var qty=parseInt(qtystring);
    /*v2.0*/
    var qty=parseFloat(qtystring);
    
    var hasa=parseInt(jQuery('#hasa_'+pid.toString()+'_'+issample.toString()).val());
    var aqty=parseFloat(jQuery('#aqty_'+pid.toString()+'_'+issample.toString()).val());
    var hasb=parseInt(jQuery('#hasb_'+pid.toString()+'_'+issample.toString()).val());
    var bqty=parseFloat(jQuery('#bqty_'+pid.toString()+'_'+issample.toString()).val());
    
    if ((hasa==0) && (hasb==0))
        return 3;
        
    if ((hasa==1) && (hasb==0))
    {
        if (qty<aqty)
            return 0;
            
        return 1;
    }
    if ((hasa==0) && (hasb==1))
    {
        if (qty<bqty)
            return 0;
            
        return 2;
    }
    
    if (aqty <= bqty)
    {
        if (qty < aqty)
            return 0;
        if (qty < bqty)
            return 1;
        return 2;       
    }
    
    //else
    
    if (qty < bqty)
        return 0;
    if (qty < aqty)
        return 2;
    return 1;       
    
}

function getSubTotal()
{
    setDefaultOptions();        
      
    var str_shoppingCartCount=jQuery.cookies.get("shoppingCartCount"); 
    var shoppingCartItems=jQuery.cookies.get("shoppingCartItems"); 
    
    if (!str_shoppingCartCount)
        str_shoppingCartCount='0';
    if (!shoppingCartItems)
        shoppingCartItems="";
    
    var shoppingCartCount=parseInt(str_shoppingCartCount);
    
    if (shoppingCartCount==0)
    {
        return 0;
    }
    
    var items=shoppingCartItems.split(";");
    
    if (items.length - 1 != shoppingCartCount)
    {
        jQuery.cookies.set("shoppingCartItems",""); 
        jQuery.cookies.set("shoppingCartCount",'0'); 
        
        return 0;
    }

    var subtotal=0*1.0;

    for (var ii=0;ii<items.length - 1;ii++)
    {
        var subitems=items[ii].split(",");
        
        if (subitems.length == 6)
        {
            if (jQuery("#total_"+subitems[0]+"_"+subitems[1]).length==0)
                continue;
                
            // remove $ and ,
            var totalstr1=jQuery("#total_"+subitems[0]+"_"+subitems[1]).text();
            var totalstr=totalstr1.substr(1,totalstr1.length-1).replace(/,/g,"");
            
            
            if (totalstr.length==0)
                continue;
                
            //alert("pid="+subitems[0]+" issample="+subitems[1]+"  totalstr="+totalstr+"  old="+subtotal.toString());
            
            subtotal=subtotal+parseFloat(totalstr);
        }
    }    

    return subtotal;            
}

// param: integer
function deleteCartItem(int_pid, int_issample)
{
    var pid=int_pid.toString();
    var issample=int_issample.toString();
    
    // delte table
    jQuery("#cart_item_"+pid+"_"+issample).remove();

    // delete cookie
    add_or_delete_ShoppingCartItem(int_pid,0,int_issample,0,0,0,0);
    
    //getShoppingCartItem();
    
    //update subtotal
    jQuery("#subtotal").text(fomatMoney(getSubTotal().toString()));
    
    if (getShoppingCartItemCount()==0)
        window.location.href='/cart.aspx';

}

// param: integer
function onFocusQty(int_pid,int_issample,qtyObj)
{
//    nextNode(qtyObj).style.display='block';

    var pid=int_pid.toString();
    var issample=int_issample.toString();
    
    //var ctrl_errorInput_div_cart=jQuery('#errorInput_div_cart_'+pid+'_'+issample);
    var ctrl_errorInput_div_cart=jQuery('.cls_errorInput_div_cart');
    
    ctrl_errorInput_div_cart.css('display','none');
}

// param: integer
function changeCartItem(int_pid, int_issample,int_subtotal_action,int_update_cookie)
{
    var pid=int_pid.toString();
    var issample=int_issample.toString();
    
    var ctrl_errorInput_div_cart=jQuery('#errorInput_div_cart_'+pid+'_'+issample);

    var str_inputQty=jQuery("#qty_"+pid+"_"+issample).val();
    
    if (checkPositiveInteger(str_inputQty)==false)
    {
        ctrl_errorInput_div_cart.html(inputmsg1);
        ctrl_errorInput_div_cart.css('display','block');
        
        jQuery("#qty_"+pid+"_"+issample).val(getQtyFromCookie(pid,issample).toString());
        return false;
    }

    var qty_2=parseFloat(str_inputQty);
    
    if (int_issample==1) // must be an integer
    {
       qty_2=Math.ceil(qty_2);
    }
    else
    if (jQuery('#isInteger_'+pid+'_'+issample).val()=='1')
    {
       qty_2=Math.ceil(qty_2);
    }
    else
    if (jQuery('#isInteger_'+pid+'_'+issample).val()=='2')
    {
       var maxbox=parseFloat(jQuery("#maxbox_"+pid+"_"+issample).val());
       var qty_3=(Math.ceil(qty_2/maxbox)*maxbox).toFixed(2);
       if(qty_3>qty_2+0.1)
       {
            ctrl_errorInput_div_cart.html( inputmsg5 + '( ' + jQuery("#boxSpec_"+pid+"_"+issample).text() + ' )');
            ctrl_errorInput_div_cart.css('display','block');
       }
       qty_2=qty_3;
    }
    else
    if (jQuery('#isInteger_'+pid+'_'+issample).val()=='3')
    {
       var maxbox=parseFloat(jQuery("#maxbox_"+pid+"_"+issample).val());
       var qty_3=(Math.ceil(qty_2/maxbox)*maxbox);
       if(qty_3>qty_2+0.1)
       {
            ctrl_errorInput_div_cart.html( inputmsg5 + '( ' + jQuery("#boxSpec_"+pid+"_"+issample).text() + ' )');
            ctrl_errorInput_div_cart.css('display','block');
       }
       qty_2=qty_3;
    }
    
    if (qty_2 >= 999999)
    {
        ctrl_errorInput_div_cart.html(inputmsg4);
        ctrl_errorInput_div_cart.css('display','block');
        
        jQuery("#qty_"+pid+"_"+issample).val(getQtyFromCookie(pid,issample).toString());
        return false;
    }
    
    jQuery("#qty_"+pid+"_"+issample).val(qty_2.toString());
    str_inputQty=jQuery("#qty_"+pid+"_"+issample).val();
    
    /*v2.0*/
    var qty=qty_2;

    if (qty<=0.01)
    {
        ctrl_errorInput_div_cart.html(inputmsg1);
        ctrl_errorInput_div_cart.css('display','block');
        
        jQuery("#qty_"+pid+"_"+issample).val(getQtyFromCookie(pid,issample).toString());
        
        return false;
    }
    var price;
    var total;
    if (int_issample==1) //sample
    {
        //show which price
        jQuery("#sp_"+pid+"_"+issample).removeClass("hidePrice");
        jQuery("#sp_"+pid+"_"+issample).addClass("showPrice");
        
        //update total
        price=parseFloat(jQuery("#sprice_"+pid+"_"+issample).val());
        total=qty * price;
    
        jQuery("#total_"+pid+"_"+issample).text(fomatMoney(total.toString()));
            
        
        //update subtotal
        if (int_subtotal_action==1)
            jQuery("#subtotal").text(fomatMoney(getSubTotal().toString()));
            
        //change to Cookie
        if (int_update_cookie == 1)
            /*v2.0*/
            add_or_delete_ShoppingCartItem(pid,parseFloat(jQuery("#qty_"+pid+"_"+issample).val()),issample,0,0,0,1)
            
        return true;
    }
    
    if (int_issample==0) //product
    {
        var ret=checkQty_pid_issample(pid,'0',jQuery("#qty_"+pid+"_"+issample).val());
        
        if (ret==0)
        {
            ctrl_errorInput_div_cart.html(inputmsg2);
            ctrl_errorInput_div_cart.css('display','block');

            jQuery("#qty_"+pid+"_"+issample).val(getQtyFromCookie(pid,issample).toString());
            return false;
        }
        
        if (ret==1) // a price
        {
            //show which price
            jQuery("#ap_"+pid+"_"+issample).removeClass("hidePrice");
            jQuery("#ap_"+pid+"_"+issample).removeClass("nonselectPrice");
            jQuery("#ap_"+pid+"_"+issample).addClass("selectPrice");
            jQuery("#ap_"+pid+"_"+issample+" img").removeClass("hidePrice");
            jQuery("#ap_"+pid+"_"+issample+" img").addClass("showPrice");
            
            var bbp=jQuery("#bp_"+pid+"_"+issample);
            if (bbp.length>0)
            {
                bbp.removeClass("selectPrice");
                bbp.addClass("nonselectPrice");
                jQuery("#bp_"+pid+"_"+issample+" img").removeClass("showPrice");
                jQuery("#bp_"+pid+"_"+issample+" img").addClass("hidePrice");
            }
            else
            {
                // no B, so delete A.img
                jQuery("#ap_"+pid+"_"+issample+" img").removeClass("showPrice");
                jQuery("#ap_"+pid+"_"+issample+" img").addClass("hidePrice");
            }
            
            //update total
            price=parseFloat(jQuery("#aprice_"+pid+"_"+issample).val());
            total=qty * price;
            
            jQuery("#total_"+pid+"_"+issample).text(fomatMoney(total.toString()));
            
            //update subtotal
            if (int_subtotal_action==1)
                jQuery("#subtotal").text(fomatMoney(getSubTotal().toString()));

            //change to Cookie
            if (int_update_cookie == 1)
                /*v2.0*/
                add_or_delete_ShoppingCartItem(pid,parseFloat(jQuery("#qty_"+pid+"_"+issample).val()),issample,0,0,0,1)
            
            return true;
        }
        if (ret==2) // b price
        {
            //show which price
            jQuery("#bp_"+pid+"_"+issample).removeClass("hidePrice");
            jQuery("#bp_"+pid+"_"+issample).removeClass("nonselectPrice");
            jQuery("#bp_"+pid+"_"+issample).addClass("selectPrice");
            jQuery("#bp_"+pid+"_"+issample+" img").removeClass("hidePrice");
            jQuery("#bp_"+pid+"_"+issample+" img").addClass("showPrice");

            var aap=jQuery("#ap_"+pid+"_"+issample);
            if (aap.length>0)
            {
                aap.removeClass("selectPrice");
                aap.addClass("nonselectPrice");
                jQuery("#ap_"+pid+"_"+issample+" img").removeClass("showPrice");
                jQuery("#ap_"+pid+"_"+issample+" img").addClass("hidePrice");
            }
            else
            {
                // no A, so delete B.img
                jQuery("#bp_"+pid+"_"+issample+" img").removeClass("showPrice");
                jQuery("#bp_"+pid+"_"+issample+" img").addClass("hidePrice");
            }
            
            //update total
            price=parseFloat(jQuery("#bprice_"+pid+"_"+issample).val());
            total=qty * price;
            
            jQuery("#total_"+pid+"_"+issample).text(fomatMoney(total.toString()));
            
            //update subtotal
            if (int_subtotal_action==1)
                jQuery("#subtotal").text(fomatMoney(getSubTotal().toString()));

            //change to Cookie
            if (int_update_cookie == 1)
                /*v2.0*/
                add_or_delete_ShoppingCartItem(pid,parseFloat(jQuery("#qty_"+pid+"_"+issample).val()),issample,0,0,0,1)
            
            return true;
        }
    }
}


function initCartItems()
{
    setDefaultOptions();        
      
    var str_shoppingCartCount=jQuery.cookies.get("shoppingCartCount"); 
    var shoppingCartItems=jQuery.cookies.get("shoppingCartItems"); 
    
    if (!str_shoppingCartCount)
        str_shoppingCartCount='0';
    if (!shoppingCartItems)
        shoppingCartItems="";
    
    var shoppingCartCount=parseInt(str_shoppingCartCount);
    
    if (shoppingCartCount==0)
    {
        return 0;
    }
    
    var items=shoppingCartItems.split(";");
    
    if (items.length - 1 != shoppingCartCount)
    {
        jQuery.cookies.set("shoppingCartItems",""); 
        jQuery.cookies.set("shoppingCartCount",'0'); 
        
        return 0;
    }

    var subtotal=0*1.0;

    for (var ii=0;ii<items.length - 1;ii++)
    {
        var subitems=items[ii].split(",");
        
        if (subitems.length == 6)
        {
            var s1=null;
            var s2=null;
            // is a sample
            if (subitems[1]=="1")
            {
                s1=jQuery("#sunit_"+subitems[0]+"_"+subitems[1]);
                if (s1)
                    s1.css("display","inline")
                s2=jQuery("#issample_"+subitems[0]+"_"+subitems[1]);
                if (s2)
                    s2.css("display","block")

            }
            else
            {
                s1=jQuery("#punit_"+subitems[0]+"_"+subitems[1]);
                if (s1)
                    s1.css("display","inline")
                s2=jQuery("#notsample_"+subitems[0]+"_"+subitems[1]);
                if (s2)
                    s2.css("display","block")
            }
            
            jQuery("#qty_"+subitems[0]+"_"+subitems[1]).val(subitems[2]);
            
            //alert(items[ii]);
            
            changeCartItem(parseInt(subitems[0]), parseInt(subitems[1]),0,0); // action=0, not count subtotal now
        }
    }    

    //update subtotal
    jQuery("#subtotal").text(fomatMoney(getSubTotal().toString()));
        
    return 0;
}

// param: integer
function moveWishListItemToCart(int_pid,int_qty)
{
    // add to cart
    add_or_delete_ShoppingCartItem(int_pid,int_qty,0,0,0,0,1);
    // delete in wishlist
    add_or_delete_WishListItem(int_pid,0);
    // remove table - tr
    jQuery("#wishlist_item_"+int_pid.toString()).remove();
    
    var newcount=getWishListItemCount();
    
    jQuery("#totalWishListCount").text(newcount.toString());
    
    if (newcount==0)
        window.location.href='/wish-list.aspx';
}

// param: integer
function deleteWishListItem(int_pid)
{
    // delete in wishlist
    add_or_delete_WishListItem(int_pid,0);
    // remove table - tr
    jQuery("#wishlist_item_"+int_pid.toString()).remove();
    
    var newcount=getWishListItemCount();
    
    jQuery("#totalWishListCount").text(newcount.toString());

    
    if (newcount==0)
        window.location.href='/wish-list.aspx';
    
}

// param: integer
function showYesNo_wishList(int_pid)
{
    var pid=int_pid.toString();
    jQuery("#del_"+pid).css("display","none");
    jQuery("#yesno_"+pid).css("display","block");
}

// param: integer
function showDelete_wishList(int_pid)
{
    var pid=int_pid.toString();
    jQuery("#del_"+pid).css("display","block");
    jQuery("#yesno_"+pid).css("display","none");
}

// param: integer
function showYesNo_Cart(int_pid, int_issample)
{
    var pid=int_pid.toString();
    var issample=int_issample.toString();
    jQuery("#del_"+pid+"_"+issample).css("display","none");
    jQuery("#yesno_"+pid+"_"+issample).css("display","block");
}

// param: integer
function showDelete_Cart(int_pid, int_issample)
{
    var pid=int_pid.toString();
    var issample=int_issample.toString();
    jQuery("#del_"+pid+"_"+issample).css("display","block");
    jQuery("#yesno_"+pid+"_"+issample).css("display","none");
}

function checkTotalAmountLimit()
{
    if (getSubTotal()>=maxOrderAmount)
    {
        jQuery('.cls_errorContinue_div_cart').text(inputmsg3);
        jQuery('.cls_errorContinue_div_cart').css("display","block");
        
        return false;
    }  
    
    jQuery('.cls_errorContinue_div_cart').css("display","none");
    
    return true;
}

function initListItems()
{
    setDefaultOptions();        
      
    initShoppingCart_ListPage();
    
    initWishList_ListPage();

    return 0;
}


function initShoppingCart_ListPage()
{
    var str_shoppingCartCount=jQuery.cookies.get("shoppingCartCount"); 
    var shoppingCartItems=jQuery.cookies.get("shoppingCartItems"); 
    
    if (!str_shoppingCartCount)
        str_shoppingCartCount='0';
    if (!shoppingCartItems)
        shoppingCartItems="";
    
    var shoppingCartCount=parseInt(str_shoppingCartCount);
    
    if (shoppingCartCount==0)
    {
        return 0;
    }
    
    var items=shoppingCartItems.split(";");
    
    if (items.length - 1 != shoppingCartCount)
    {
        jQuery.cookies.set("shoppingCartItems",""); 
        jQuery.cookies.set("shoppingCartCount",'0'); 
        
        return 0;
    }

    for (var ii=0;ii<items.length - 1;ii++)
    {
        var subitems=items[ii].split(",");
        
        if (subitems.length == 6)
        {
            if (subitems[1]=='0')
            {
                if (jQuery("#cart_"+subitems[0]).length>0)
                {
                    jQuery("#cart_"+subitems[0])[0].src= '/images/addedtocart.gif';
                    jQuery("#cart_"+subitems[0]).css("cursor","default");
                }
            }

            if (subitems[1]=='1')
            {
                if (jQuery("#sample_"+subitems[0]).length>0)
                {
                    jQuery("#sample_"+subitems[0])[0].src= '/images/sampleadded.gif';
                    jQuery("#sample_"+subitems[0]).css("cursor","default");
                }
            }
        }
    }
}
function initWishList_ListPage()
{
    var str_wishListCount=jQuery.cookies.get("wishListCount"); 
    var wishListItems=jQuery.cookies.get("wishListItems"); 
    
    if (!str_wishListCount)
        str_wishListCount='0';
    if (!wishListItems)
        wishListItems="";
    
    var wishListCount=parseInt(str_wishListCount);
    
    if (wishListCount==0)
    {
        return 0;
    }
    
    var items=wishListItems.split(";");
    
    if (items.length - 1 != wishListCount)
    {
        jQuery.cookies.set("wishListItems",""); 
        jQuery.cookies.set("wishListCount",'0'); 
        
        return 0;
    }

    for (var ii=0;ii<items.length - 1;ii++)
    {
        if (jQuery("#wishlist_"+items[ii]).length>0)
        {
            jQuery("#wishlist_"+items[ii])[0].src= '/images/wishlistadded.gif';
            jQuery("#wishlist_"+items[ii]).css("cursor","default");
        }
    }
}



