function onfocus_ctrl(text,idCtrl){
    if(g(idCtrl).value==text){
        g(idCtrl).value="";
    }
}
function onblur_ctrl(text,idCtrl){
    if(g(idCtrl).value==""){
        g(idCtrl).value=text;
    }	
}

function egc_code_KeyPress(e){
    var key=e.keyCode || e.which;
    if(key==13)
        apply_egc_code();
}

function apply_egc_code(){
    var total=0;
    var coupon_code=0;
	
    total=g("total_amount_temp").value;
    //total=g('total_amount').value;
    coupon_code=g("coupon_code_egc").value;
	
    if(trim(coupon_code)==""){
        alert("Enter your Egift Card code");
        g("coupon_code_egc").focus();
    }else{
        $.post('controllers/egc_cartController.php', {
            coupon_code:coupon_code, 
            total:total,
            action:"discount_code"
        }, function(data){
	
            rpta=data.split("|");
			
            ok=rpta[0];
			
            if(ok=="OK"){
                totalamount=currencyConvert(rpta[1]);
                //g("totalAmount").innerHTML="$ "+totalamount;
                //g("total_amount").value=rpta[1];
				
                // Mostramos en el HTML el monto de descuento
                g('amountDiscount_egc_show').style.display="";
                g("amountDiscount_egc").innerHTML="$ "+currencyConvert(rpta[2]);
				
                g("egc").value="1";
                g("egc_id").value=rpta[4]; // idEGC
                g("egc_amount").value=rpta[2]; // Descuento
                g("egc_amount_used").value=rpta[3]; // nuevo monto usado
				
                g('checkCreditCard').value=rpta[5];
                if(rpta[5]=="-1"){					
                    g('paymentMethod').style.display="none";
                }else{
                    g('paymentMethod').style.display="";	
                }
            }else{
                alert(rpta[1]);
                g("egc").value="0";
				
                g('amountDiscount_egc_show').style.display="none";
                g("amountDiscount_egc").innerHTML="$0.00";
            }
			
            calculate_total_amount('');
        });	
    }
}

function checkstock_cart(indice,qty_temp,idProduct,idSize,idColor,flagColorSize,indice_cart){
    //alert(idProduct);
    var rpta;
    var ok;
	
    var qty=g("qty"+indice).value;
    
    if(parseInt(qty)>0){
        $.post('/controllers/cartController.php', {
            idProduct:idProduct, 
            flagColorSize:flagColorSize, 
            idSize:idSize, 
            idColor:idColor, 
            qty:qty, 
            action:"checkStock",
            indice_cart:indice_cart
        }, function(data){

            switch(data.ok){
                case "OK":
                    g("subtotal_first").value=currencyConvert(data.subtotal);
                    g("subtotal").value=currencyConvert(data.subtotal);
                    g("totalamount").value=currencyConvert(data.subtotal);

                    g("totalamountdisabled").innerHTML="$ "+currencyConvert(data.subtotal);
                    g("amount"+indice).innerHTML="$ "+currencyConvert(data.subtotalItem);
                    break;
                default:
                    alert(data.message);
                    g("qty"+indice).value=qty_temp;
                    return false;
            }        
        },"json");	
    }else{
        g("qty"+indice).value=qty_temp;
    }
}

function calculateShippingAndGetState(country_current,state_current,idDivState,objName_state){
    var subtotal_without_shipp=g("subtotal_without_shipp").value;
    var discountCodeAmount=g("discountCodeAmount").value;
	
    var subtotal=g("subtotal").value;
    var total_amount=g("total_amount").value;
	
    var country=country_current;
    var state=state_current
    var objState=objName_state;
    $.post('controllers/process.php', {
        state:state, 
        country_:country,
        action:"showStateForWholesaleAndShipping",
        model:"Countries",
        objState:objState,
        discountCodeAmount:discountCodeAmount,
        subtotal_without_shipp:subtotal_without_shipp
    }, function(data){
																																																												
        rpta=data.split("|");																																																											  		
        if(country_current=="United States"){
            g("disclamer").style.display="none";
            g("accept_terms").value=0;
        }else{
            g("disclamer").style.display="";
            g("accept_terms").value=1;
        }
        //g("total_amount").value=rpta[0];
        //g("subtotal").value=rpta[1];
        //g("valueShipp").value=rpta[2];
		
        g(idDivState).innerHTML=rpta[3];  
    });
}

function estimatedShipping(country){
    var subtotal=g("subtotal_first").value;
    $.post('controllers/process.php', {
        action:"estimateShipping", 
        model:"Countries", 
        country:country, 
        subtotal:subtotal
    }, function(data){																																													
        rpta=data.split("|");
		
        g("totalamount").value=rpta[0];
        g("subtotal").value=rpta[0];
        g("totalamountdisabled").innerHTML="$ "+rpta[0];
        g("valueShipp").value=rpta[1];
        g("costShipp").innerHTML="$ "+rpta[1];
        g("shippto").value=rpta[2];
		
    });
}

function showCountry(countryName){
    if(countryName=="United States"){
        g("ShowState").style.display="";
        g("showCity").style.display="";
        g("showBtnSearch").style.display="";
    }else{
        g("ShowState").style.display="none";
        g("showCity").style.display="none";
        g("showBtnSearch").style.display="none";
		
        $.post('controllers/process.php', {
            model:"StoreLocator",
            action:"showResultForCountry", 
            countryName:countryName
        }, function(data){																																													
            g("result").innerHTML=data;		
        });
    }
}

function showResult(){
    var idState;
    var city;
	
    idState=g('cboState').value;
    city=g('cboCity').value;
    $.post('controllers/process.php', {
        model:"StoreLocator",
        action:"showResult", 
        idState:idState, 
        city:city
    }, function(data){																																													
        g("result").innerHTML=data;
    });
}

function showCity(idState){	
    $.post('controllers/process.php', {
        model:"StoreLocator",
        action:"showCity", 
        idState:idState
    }, function(data){																																													
        g("showCity").innerHTML=data;
    });
}

/*Wish list de wholesale*/
function addCartFromWholesaleWishList(idProd,idColor,IdSize,Qty,price,i,controlStock,idWish){
	
    var flagColorSize=1;
    if(idColor=="0" && IdSize=="0")
        flagColorSize=0;
	
    $.post('controllers/WholesaleWishController.php', {
        action:"addCart", 
        idProduct:idProd, 
        color:idColor, 
        size:IdSize, 
        flagColorSize:flagColorSize,
        quantity:Qty,
        price:price,
        controlStock:controlStock,
        idWish:idWish
    }, function(data){
		
        /*rpta=data.split("|");
		
		g("msgAddCart"+i).style.display='';
		g("msgAddCart"+i).innerHTML=rpta[0];
		g("detail_my_cart").innerHTML=rpta[1];*/
        messageAddToCart();
		
        try {
            clearTimeout(to);
        }catch (e) {}					
        to = setTimeout("refresh()",4500);
    });
}

function addWholesaleWishList(idProd,contentObj){
	
    var flag=g("flagColorSize"+contentObj).value;
    var color=0;
    var size=0;
    var addwl=true;
	
    if(flag=='1'){
        color=g('color'+contentObj).value;
        size=g('size'+contentObj).value;
        if(trim(color)=="" && addwl){
            alert("You need to select a color");	
            addwl=false;
        }
        if(trim(size)=="" && addwl){
            alert("You need to select a size");	
            addwl=false;
        }
    }
	
    if(addwl){
        $.post('/controllers/WholesaleWishController.php', {
            idProduct:idProd, 
            action:"add", 
            color:color, 
            size:size
        }, function(data){
           /* g("wishList").innerHTML=data;
            messageAddWishList();*/
			
			if(contentObj==""){
            	messageAddWishList();
			}else{
				alert("This product has been added to your Wish List!");
			}
        });	
    }
}
/************************/

function clearDataWholeSaleShippingAddress(){
    g('name_shipp').value="";
    g('lastName_shipp').value="";
    g('company_shipp').value="";
    g('address1_shipp').value="";
    g('address2_shipp').value="";
    g('city_shipp').value="";
    g("stateprov_shipp").value="";
    g('zip_shipp').value="";
    g('country_shipp').value="";	
}

function copyDataWholeSaleShippingAddress(){
    g("name_shipp").value=g("name_bill").value;
    g("lastName_shipp").value=g("lastName_bill").value;
    g("company_shipp").value=g("company_bill").value;
    g("address1_shipp").value=g("address1_bill").value;
    g("address2_shipp").value=g("address2_bill").value;
    g("country_shipp").value=g("country_bill").value;
	
    if(g("country_shipp").value=="United States"){		
        selectedCountryFormWholesale("United States",g("stateprov_bill").value,"divStateShipp","stateprov_shipp");
    }else{
        g("divStateShipp").innerHTML='<input name="stateprov_shipp" type="text" class="input-text" id="stateprov_shipp" size="30" value="'+g("stateprov_bill").value+'" />';
    }
		
    g("city_shipp").value=g("city_bill").value;
    g("zip_shipp").value=g("zip_bill").value;
	
}

function selectedCountryFormWholesale(country_current,state_current,idDivState,objName_state){
    var country=country_current;
    var state=state_current
    var objState=objName_state;
    $.post('/controllers/process.php', {
        state:state, 
        country_:country,
        action:"showStateForWholesale",
        model:"Countries",
        objState:objState
    }, function(data){
        g(idDivState).innerHTML=data;  
    //copyShippingData(1)
    //calculate_shipping(); 
    });
}

function selectedTypeService(amount, costService){
    var amountTotal;
    amountTotal=(1)*(amount)+(1)*(costService);
	
    g("totalAmount").innerHTML="$"+currencyConvert(amountTotal);
    g("total_amount").value=currencyConvert(amountTotal);
	
    g('is_required_shipping').value=costService;
}

function addCartFromWishList(idProd,idColor,IdSize,Qty,price,i,controlStock,idWish){
	
    var flagColorSize=1;
    if(idColor=="0" && IdSize=="0")
        flagColorSize=0;
	
    $.post('controllers/wishController.php', {
        action:"addCart", 
        idProduct:idProd, 
        color:idColor, 
        size:IdSize, 
        flagColorSize:flagColorSize,
        quantity:Qty,
        price:price,
        controlStock:controlStock,
        idWish:idWish
    }, function(data){
		
        /*rpta=data.split("|");
		
		g("msgAddCart"+i).style.display='';
		g("msgAddCart"+i).innerHTML=rpta[0];
		g("detail_my_cart").innerHTML=rpta[1];*/
        messageAddToCart();
		
        try {
            clearTimeout(to);
        }catch (e) {}					
        to = setTimeout("refresh()",4500);
    });
}

function closeMessageAddCart(i){
    g("msgAddCart"+i).style.display='none';	
}

function deleteWishList(id,typeClient){
    var url_controller;
    if(typeClient=="1"){
        url_controller="wishController.php";
    }
	
    if(typeClient=="2"){
        url_controller="WholesaleWishController.php";
    }
	
    $.post('/controllers/'+url_controller, {
        id:id, 
        action:"delete"
    }, function(data){
        g("wishList").innerHTML=data;
    });	
}

function addWishList(idProd,contentObj){
	
    var flag=g("flagColorSize"+contentObj).value;
    var color=0;
    var size=0;
	
    addwl=true;
	
    if(flag=='1'){
        color=g('color'+contentObj).value;
        size=g('size'+contentObj).value;
		
        if(trim(color)=="" && addwl){
            alert("You need to select a color");	
            addwl=false;
        }
        if(trim(size)=="" && addwl){
            alert("You need to select a size");	
            addwl=false;
        }
    }
	
    if(addwl){
        $.post('/controllers/wishController.php', {
            idProduct:idProd, 
            action:"add", 
            color:color, 
            size:size
        }, function(data){
            //g("wishList").innerHTML=data;
			if(contentObj==""){
            	messageAddWishList();
			}else{
				alert("This product has been added to your Wish List!");
			}
        });	
    }
}

function cancelCheckout(){
    location.href="controllers/cartController.php?action=cancel_checkout";
}

function ChangeState(country){	
    var state=g("stateprov_bill").value
    $.post('controllers/process.php', {
        model:"Countries",
        action:"showstate",
        country_:country,
        state:state
    }, function(data){
        g("StateDiv").innerHTML=data;																									
    });
}



function calculate_shipping(typeService,amount,itemsShippingCalculate,decPriceMinShipp,decPriceMoreItems){
    var amountTotal;
    var amountTotal_a;
    var amountTax;
    var amountShipp;
    if(typeService=="default" || typeService=="empty"){
        if(itemsShippingCalculate=="1")	{ // Se jhace el calculo con priceMin
            amountTotal_a=(1)*(amount)+(1)*(decPriceMinShipp);
            amountShipp=decPriceMinShipp;
        }else{ // sino se hace el calculo con el precio para cantidades mayores al total de items del carrito
            var qprod;
            qprod=g('qty_prod').value;
            amountShipp=qprod*decPriceMoreItems;
            amountTotal_a=(1)*(amountShipp)+(1)*(amount);			
			
        }
    }
	
    if(typeService=="premium"){
        amountShipp=decPriceMinShipp;
        amountTotal_a=(1)*(amount)+(1)*(decPriceMinShipp);
    }
	
    if(typeService=="pickup"){
        amountShipp=0;
        amountTotal_a=amount;
    }
	
    amountTax=g("monto_tax").value;
    amountTotal=(1)*(amountTotal_a)+(1)*(amountTax);
	
    g("priceShipp").innerHTML="$"+currencyConvert(amountShipp);
    g("price_shipping").value=currencyConvert(amountShipp);	
    g("totalAmount").innerHTML="$"+currencyConvert(amountTotal);
    g("total_amount").value=currencyConvert(amountTotal);
	
    g('is_required_shipping').value=amountShipp;
}
function selectedCountry(country_current,state_current){
    var country=country_current;
    var state=state_current
	
    $.post('controllers/process.php', {
        state:state, 
        country_:country,
        action:"showstate",
        model:"Countries"
    }, function(data){
        g("divState").innerHTML=data;  
    //copyShippingData(1)
    //calculate_shipping(); 
    });
}

function clearFormBilling(){
    g('name_shipp').value="";
    g('lastName_shipp').value="";
    g('company_shipp').value="";
    g('address1_shipp').value="";
    g('address2_shipp').value="";
    g('city_shipp').value="";
    g("stateprov_shipp").value="";
    g('zip_shipp').value="";
    g('country_shipp').value="";
    g('phono_shipp').value="";
	
	
// Reseteamos los valores
/*g("priceShipp").innerHTML="$0.00";
	g("taxMonto").innerHTML="$0.00";
	g("totalAmount").innerHTML=g("subtotal").value;
	
	g("price_shipping").value=0;
	g("total_amount").value=g("subtotal").value;
		
	g('percent_tax').value=0;
	g('monto_tax').value=0;
	*/
}

function copyShippingData(state_copy){
    g("name_shipp").value=g("name_bill").value;
    g("lastName_shipp").value=g("lastName_bill").value;
    g("company_shipp").value=g("company_bill").value;
    g("address1_shipp").value=g("address1_bill").value;
    g("address2_shipp").value=g("address2_bill").value;
    g("city_shipp").value=g("city_bill").value;
    g("zip_shipp").value=g("zip_bill").value;
    g("country_shipp").value=g("country_bill").value;
    g("phono_shipp").value=g("phono_bill").value;	
	
    if(state_copy==0){
        g("stateprov_shipp").value=g("stateprov_bill").value;
    }else{
        g("stateprov_shipp").value=g("stateprov_bill").value;	
    }
}

function checkoutStepOne(){
	
    g("action_cart").value="send_checkout";
	
    frm=document.forms["frmCart"];
    frm.submit();
}

function coupon_code_KeyPress(e){
    var key=e.keyCode || e.which;
    if(key==13)
        apply_coupon_code();
}

function apply_coupon_code(){
    var subtotal=0;
    var coupon_code=0;
    var valueShipping=0;
    var valueWeight=0;
    var valuePieces=0;
    
    subtotal=g("subtotal").value;
    //subtotal=g("subtotal_first").value;
    coupon_code=g("coupon_code").value;
    valueShipping=g('shipping_options').value;
    valueWeight=g('weight').value;
    valuePieces=g('pieces').value;
    if(trim(coupon_code)==""){
        alert("Enter your discount code");
        g("coupon_code").focus();
    }else{
        tb_show('Discount Code', '?#TB_inline&height=60&width=200&inlineId=messageWait&modal=true', '');	
		
        $.post('/controllers/cartController.php', {
            coupon_code:coupon_code, 
            subtotal:subtotal,
            action:"discount_code",
            valueShipping:valueShipping,
            valueWeight:valueWeight,
            pieces:valuePieces
        }, function(data){
			
            if(data.next=="OK"){
                if(data.freeshipping=="NO"){
                    g("freeShippingcode").value="NO";
					
                    g("listServices").innerHTML=data.dropdown;	
					
                    g("lblValueCostShip").innerHTML="$"+data.defaultShippingCost;
                    g("price_shipping").value=data.defaultShippingCost;
                }else{					
                    if(data.freeshipping=="YES"){
                        g("freeShippingcode").value="YES";
                        g("listServices").innerHTML=data.dropdown;													
                    }
                }
                // hidden
                g("idCouponCode").value=data.couponID;
                g("discountCodePercent").value=data.discountPercent;
                g("amountCodeDiscount").value=data.discountAmount;
                //
				
                g("discountCode_div").style.display="";
                g("discountCodeAmount_div").innerHTML="-$"+data.discountAmount;
				
                calculate_total_amount('');
            }else{
				
                if(data.message=='session_expired'){
                    window.location.href="/cart.php?m";
                }else{
                    g("idCouponCode").value=0;
                    g("discountCodePercent").value=0;
                    g("amountCodeDiscount").value=0;
					
                    g("discountCode_div").style.display="none";
                    g("discountCodeAmount_div").innerHTML="$0.00";
					
                    g("freeShippingcode").value="NO";
					
                    alert(data.message);
                }
            }
			
            calculate_total_amount('');
            tb_remove();
						
            if(data.freeshipping=="YES" && data.isPOBox=="YES"){
                g("freeShippingcode").value="NO";
                //tb_show('Discount Code', '?#TB_inline&height=60&width=300&inlineId=messageInfo&modal=false', '');
                alert("This coupon cannot be applied to Fedex free shipping since you have a P.O. Box as your shipping address.");
            }
			
			
        },"json");	
    }
}

function loadColorCart(idProduct,indice,indiceCart){
    var idSize
    idSize=g('size'+indice).value;
    $("#div_color"+indice).load("controllers/attributesController.php?action=3&idProduct="+idProduct+'&idSize='+idSize+"&indice="+indice+"&indiceCart="+indiceCart);
}

function clearCart(){
    var ok=confirm("Are you sure you want to empty your shopping cart?");
    if(ok==true){
        window.location.href="controllers/cartController.php?action=clear";
    }
}

function updateCart(){
    var error;
    var total=g("total_item").value;
	
    for(i=0; i<total; i++){
        q=g("qty"+i).value;
        if(q>0){
            error=1;
        }
        else{
            alert("The quantity is required in your product");
            error=0;
            break;
        }
    }
	
    if(error){
        g("action_cart").value="update";
		
        frm=document.forms["frmCart"];
        frm.submit();
    }
}

function addCart_KeyPress(e,productID,flagColorSize){
    var key=e.keyCode || e.which;
    if(key==13)
        addCart(productID,flagColorSize);
}

function addCart(idProduct,flagColorSize,contentObj){
    //alert(idProduct);
    var rpta;
    var ok;
    var idSize=0;
    var idColor=0;
    var qty=g('producto-quantity'+contentObj).value;
	
    var send=true;
	
    var price=g("price"+contentObj).value;
    var controlStock=g("controlStock"+contentObj).value;
    var featured=g("featured"+contentObj).value;
    var parentCategoryID=g("parentCategoryID"+contentObj).value;
    
    if(flagColorSize==1){		
        idSize=g('size'+contentObj).value;
        idColor=g('color'+contentObj).value;
		
        if(idSize==""){
            alert("You need to select a size");
            return false;
            send=false;
        }
        if(idColor==""){
            alert("You need to select a color");
            return false;
            send=false;
        }
    }
	
    if(send){
        $.post('/controllers/cartController.php', {
            idProduct:idProduct, 
            flagColorSize:flagColorSize, 
            idSize:idSize, 
            idColor:idColor, 
            qty:qty, 
            action:"addCartSlide",
            price:price, 
            controlStock:controlStock, 
            featured:featured,
            parentCategoryID:parentCategoryID
        }, function(data){																																																																																																																		
            if(data.isOK=="OK"){
				
				if(contentObj!=""){
					$.modal.close();	
				}
				
                $('#top-cart-slide-cart').html(data.bag);
                $('#top-cart-slide').slideDown();
                setTimeout("$('#top-cart-slide').slideUp()", 5000);
                            
                $('#top-cart-amount').html(data.info_num_items);
                if(data.infoBlockMyCart!="none"){
                    $('#blockInfoMyCart').html(data.infoBlockMyCart);
                }
            }
            else if(data.isOK=="IS_GREATER"){
                alert(data.message);
                return false;
            //frm.submit.disabled=true;
            }
            else if(data.isOK=="SOLD_OUT"){
                alert(data.message);
                return false;
            //frm.submit.disabled=true;
            }
			
            closeBag.action_x();
            linkProduct.action_go();
			
        },"json");
    }
}

function loadColor(idProduct,idSize,contentObj){	
    $.post('/controllers/attributesController.php?action=2&idProduct='+idProduct+'&idSize='+idSize+'&contentObj='+contentObj,function(data){
        g("attrColor"+contentObj).innerHTML=data;
        g('color'+contentObj).value="";
        loadAttrSelector(contentObj);		
        loadNumberColors.checkLoad(contentObj);
    });
}

function attrSize(idProduct,contentObj){

	var divAttrSize="attrSize"+contentObj;
	var idSizeFirst="idSizeFirst"+contentObj;
	var attrColor="attrColor"+contentObj;
	var color='color'+contentObj;
		
    $.post('/controllers/attributesController.php?action=1&idProduct='+idProduct+'&contentObj='+contentObj,function(data){		
        var idSize;		
       // g(attrSize).innerHTML=data;		
		$("#"+divAttrSize).html(data);
        idSize=g(idSizeFirst).value;
        //g('attSizeSelected').innerHTML=g("z-"+idSize).innerHTML;
        //g('size').value=idSize;
		
        $.post('/controllers/attributesController.php?action=5&idProduct='+idProduct+'&idSize='+idSize+'&contentObj='+contentObj,function(data){
            //$("#attrColor").html(data);
		   	g(attrColor).innerHTML=data;
            g(color).value="";
            //g('color').value=g('idColorFirst').value;
            //iniciando click
            loadAttrSelector(contentObj);
            loadNumberColors.checkLoad(contentObj);
        });
		
        if(idSize=="-1"){
            g("control-button").style.display="none";
        }
		
    });
	
}


function currencyConvert(n){
    n = Math.round( (n*100) )/100;
    strN = new String( n );
    if( strN.indexOf( "." ) == -1 )
        strN += ".00";
    else if( strN.indexOf( "." ) == strN.length-2 )
        strN += "0";
    return strN;
}

function validCreditCart(ccNumb) {
    var valid = "0123456789" //Digitos validos
    var len = ccNumb.length;
    var iCCN = parseInt(ccNumb); 
    var sCCN = ccNumb.toString(); 
    sCCN = sCCN.replace (/^\s+|\s+$/g,'');
    var iTotal = 0;  
    var bNum = true; 
    var bResult = false; 
    var temp; 
    var calc; 
 
    for (var j=0; j<len; j++) {
        temp = "" + sCCN.substring(j, j+1);
        if (valid.indexOf(temp) == "-1"){
            bNum = false;
        }
    }
 
    if(!bNum){
        bResult = false;
    }
 
    if((len == 0)&&(bResult)){
        bResult = false;
    } else{
        if(len >= 12){  // 15 or 16 for Amex or V/MC
            for(var i=len;i>0;i--){  
                calc = parseInt(iCCN) % 10; 
                calc = parseInt(calc);  
                iTotal += calc;
                i--;  
                iCCN = iCCN / 10;
                calc = parseInt(iCCN) % 10 ;
                calc = calc *2; 
                switch(calc){
                    case 10:
                        calc = 1;
                        break;       //5*2=10 & 1+0 = 1
                    case 12:
                        calc = 3;
                        break;       //6*2=12 & 1+2 = 3
                    case 14:
                        calc = 5;
                        break;       //7*2=14 & 1+4 = 5
                    case 16:
                        calc = 7;
                        break;       //8*2=16 & 1+6 = 7
                    case 18:
                        calc = 9;
                        break;       //9*2=18 & 1+8 = 9
                    default:
                        calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
                }                                               
                iCCN = iCCN / 10;
                iTotal += calc;
            } //end for
            if ((iTotal%10)==0){
                bResult = true;
            } else {
                bResult = false; 
            }
        }
    }
    return bResult;
}

function loadAttrSelector(contentObj){
    $('#attrColorBlock'+contentObj+' div').unbind('click');
    $('#attrSizeBlock'+contentObj+' div').unbind('click');

    $('#attrColorBlock'+contentObj+' div').click(function(){
        $('#attrColorBlock'+contentObj+' div').removeClass('selected');
        $(this).addClass('selected');
        $('#color'+contentObj).val($(this).attr('colorId'+contentObj));
        $('#attColorSelected'+contentObj).html($(this).attr('title'));
		
        galleryImagesColor.loadGallery($(this).attr('colorId'+contentObj),contentObj);
    });
    $('#attrSizeBlock'+contentObj+' div').click(function(){
        $('#attrSizeBlock'+contentObj+' div').removeClass('selected');
        $(this).addClass('selected');
        $('#size'+contentObj).val($(this).attr('sizeId'+contentObj));
        $('#attSizeSelected'+contentObj).html($(this).html());	
		
        loadColor($('#idProduct'+contentObj).val(),$(this).attr('sizeId'+contentObj),contentObj);
    });
//galleryImagesColor.loadGallery($("#idColorFirst").val());
}

function loadProductLibs(contentObj){
    
	galleryProduct.controlNextPrev(contentObj);
    galleryProduct.loadImageBig(contentObj);

    $("#product-zoom"+contentObj).fancybox({
        'titleShow': false,
        'overlayColor':'#000000'
    });
	
    $('.cloud-zoom, .cloud-zoom-gallery').CloudZoom()
}

var galleryProduct={
	controlNextPrev:function(contentObj){
        if($('#gallery'+contentObj+' li').length>3){
			$('#gallery'+contentObj).jCarouselLite({
				btnNext: "#gallery-next"+contentObj,
				btnPrev: "#gallery-prev"+contentObj,
				visible: 3,
				circular: false
			});
			$('#gallery-control'+contentObj).show();
		}
	},
	loadImageBig:function(contentObj){
		$('#gallery'+contentObj+' a').click(function(){													
			$("#product-zoom"+contentObj).attr('href',$(this).attr('href'));
		});
	}	
}

$(document).ready(function(){
	
	quickviewProduct.show();
	 blockSlideHome.banners();
                 blockSlideHome.inforight();
    //png
    $('.imgPng').pngFix();
	
	$('#account-menu-v2 li').mouseenter(function(){
        var displayMenu=$(this).children('ul');
        if(displayMenu.length==1){
            displayMenu.slideDown('fast');
        }        
    }).mouseleave(function(){
        var displayMenu=$(this).children('ul');
        if(displayMenu.length==1){
            displayMenu.slideUp('fast');
        } 
    });
	
    $('#menu-top li').mouseenter(function(){
        var displayMenu=$(this).children('ul');
        if(displayMenu.length==1){
            //displayMenu.slideDown('fast');
            displayMenu.show();
        }        
    }).mouseleave(function(){
        var displayMenu=$(this).children('ul');
        if(displayMenu.length==1){
            //displayMenu.slideUp('fast');
            displayMenu.hide();
        } 
    });

    $('#menu-up li').mouseenter(function(){
        var displayMenu=$(this).children('ul');
        if(displayMenu.length==1){
            //displayMenu.slideDown('fast');
            displayMenu.show();
        } 
    }).mouseleave(function(){
        var displayMenu=$(this).children('ul');
        if(displayMenu.length==1){
            //displayMenu.slideUp('fast');
            displayMenu.hide();
        }
    });

    //product details effects
    if($('#product-detail-content').length==1){
        loadProductLibs('');  
        loadAttrSelector('');
    }
	
	if($('#product-detail-content-q').length==1){
        loadProductLibs('-q');  
        loadAttrSelector('-q');
    }

    $('div.product-tab-title').click(function(){
        $('div.product-tab-title').removeClass('selected');
        $(this).addClass('selected');
        
        $('.product-tab-content').hide();
        $('#'+$(this).attr('id')+'-cont').show();
    })

    ///cart top-cart-slide
    $('#top-cart-slide-open').click(function(){
											 
        $.post('/controllers/cartController.php', {
            action:"showBag"
        }, function(data){
            $('#top-cart-slide-cart').html(data.bag);																																																						 			
            closeBag.action_x();
            linkProduct.action_go();
			
        },"json");
											 
        $('#top-cart-slide').slideToggle();
		
        setTimeout("$('#top-cart-slide').slideUp()", 20000);
				
    });
    
    closeBag.action_x();

    if($.browser.msie && /msie 6\.0/i.test(navigator.userAgent)){
        $('body').addClass('ie6fix');
    }
	
});

var quickviewProduct={
	show:function(){
		$(".quickview").click(function(){
			var productID=$(this).attr("id");
			
			if($('#quickview-product').length=='1'){
				$('#quickview-product').modal('open');
			}else{
				
				$.post('/controllers/process.php',{model:'QuickviewProduct',action:'viewDetail',productID:productID},function(data){	
					
					$(data).modal({
						close:true,
						position: ["10%"],
						overlayId:'dialogOverlayQuickview',
						containerId:'dialogContainerQuickview',
						closeHTML: null,
						overlayClose: true,
						onShow: function (dialog) {
							
							
							dialog.data.find('#dialog_close').click(function () {
								$.modal.close();
							});
							
							attrSize(productID,'-q');
							
							/*galleryProduct.controlNextPrev('-q');
							galleryProduct.loadImageBig('-q');*/
							loadProductLibs('-q')
							$('#quickview-product .imgPng').ifixpng();$('#quickview-product').ifixpng();
							loadAttrSelector('-q');
							
						}
						
					});
					
					$('#quickview-product').ifixpng();
				});
			}
		});
	}
}


var linkProduct={
    action_go:function(){
        $(".top-cart-slide-block").click(function(){
            var $link=$(this).attr("href");
            window.location.href=$link;
        });
    }
};

var closeBag={
    action_x:function(){
        $('#top-cart-slide-close').click(function(){
            $('#top-cart-slide').slideUp();
        });	
    }
};

var galleryImagesColor={
    loadGallery:function(colorID,contentObj){
        var parameters_for_color={
            action:4,
            idProduct:$('#idProduct'+contentObj).val(),
            idColor:colorID,
			contentObj:contentObj
        }
        $.post('/controllers/attributesController.php',parameters_for_color,function(data){
            $('#product-detail-content'+contentObj+' div.product-img-content').html(data);
            loadProductLibs(contentObj);
            $('.cloud-zoom, .cloud-zoom-gallery').CloudZoom()
            });
    }
}

var loadNumberColors={
    checkLoad:function(contentObj){
        var totalColors=$("#numberColors"+contentObj).val();		
        if("1"==totalColors){
            $('#attrColorBlock'+contentObj+' div').removeClass('selected');
            $('#attrColorBlock'+contentObj+' div').addClass('selected');
            $('#color'+contentObj).val($('#attrColorBlock'+contentObj+' div').attr('colorId'+contentObj));
            $('#attColorSelected'+contentObj).html($('#attrColorBlock'+contentObj+' div').attr('title'));	
        }
    }
}


function updateColorCart(colorID,productID,indice,indiceCart){
    var idSize=g('size'+indice).value;
    var qty=g('qty'+indice).value;
    var flagColorSize=g('flagColorSize'+indice).value;
    
    //alert(colorID+' '+idSize+' '+productID+' '+indice+' '+indiceCart);
    
    var param={
        action:"updateRowCart",
        colorID:colorID,
        productID:productID,
        idSize:idSize,
        indiceCart:indiceCart,
        qty:qty,
        flagColorSize:flagColorSize
    }
    
    $.post('/controllers/cartController.php',param,function(data){
        
        if(data.message=="OK"){
            g('img_'+indice).innerHTML=data.image_pord;
        }else{
            alert(data.message);
        }
        
    },"json");    
    
}

var blockSlideHome={
	banners:function(){
		if($('#banners-content li').length>3){
			$('#banners-content').jCarouselLite({
					btnNext: "#block-banners-btn-next",
					btnPrev: "#block-banners-btn-prev",
					visible: 3,
					circular: true
			});
			$('#gallery-control').show();
		}	
	},
	
	inforight:function(){
		if($('#slide-content li').length>1){                
			$('#slide-content').jCarouselLite({
					btnNext: "#info-next",
					btnPrev: "#info-prev",
					visible: 1,
					circular: true
			});
			$('#slide-content').show();
		}	
	}
	/*inforight:function(){
		var opc={
			btnNext: "#info-next",
			btnPrev: "#info-prev",
			speed: 1000,
			auto: 8000,
			visible: 1
		}
		$('#slide-content').jCarouselLite(opc);	
	}*/
}

var checkBox={
	isChecked:function(class_action){
		var arrayID=new Array();
		$(class_action).each(function(){
			arrayID.push(this.value);
		});	
		if(arrayID!=""){
			return true;	
		}else{
			return false;	
		}
			
	}	
}

function clearTimeOut(func,time){
	try {
		 clearTimeout(to);
	}catch (e) {}							
	to = setTimeout(func,time);
}

function closeMsg(objID){
	document.getElementById(objID).style.display='none';
}
