if (navigator&&navigator.cookieEnabled==false){
    document.write(no_cookies_message);
}

function addListToCart(shop_id){
      
    if (!isNaN(shop_id)&&document.f.elements['product_ids[]']) {
        var inp = document.f.elements['wholesale_limit'];

        // Wholesale limit
        wholesale_limit = (inp ? inp.value : false);

        // Submit button
        var but = document.getElementById('submit_button');
        if (but) but.disabled = true;
      	  	
        var total = readCookie('CART_TOTAL_'+shop_id);
        var totalWS = readCookie('CART_TOTAL_WS_'+shop_id);
        var cart = unescape(readCookie('CART_'+shop_id));
        var cart_split = cart.split(';');
        var cart_item_split;
        var cart_hash = {};
        var found = false, i, product_id, price, amount, priceWS;

        // Assign cart hash
        for (i=0; i < cart_split.length; ++i){
            cart_item_split = cart_split[i].split("=");
            cart_hash[cart_item_split[0]] = cart_item_split[1];
        }
					          
        cart='';
                             
        if (document.f.elements['product_ids[]']) {
        
            if (document.f.elements['product_ids[]'].nodeName!="INPUT")  {
                	
                for (i=0;i<document.f.elements['product_ids[]'].length;i++){

                    if (document.f.elements['product_ids[]'][i]) {
                                           
                        product_id = document.f.elements['product_ids[]'][i].value;
                			
                        price = document.f.elements['prices[]'][i].value;

                        inp = document.f.elements['prices_wholesale[]'];
                        priceWS = (inp ? inp[i].value : price);

                        amount = document.f.elements['amounts[]'][i].value;
						
                        if (amount!="" && amount > 0){

                            total = Number(total) + Number(price*amount);
                            totalWS = Number(totalWS) + Number(priceWS*amount);
							
                            if (cart_hash[product_id]){
                     				
                                cart_hash[product_id]= Number(cart_hash[product_id]) + Number(amount);
                     					                     				
                            } else {
                     				
                                cart_hash[product_id]=Number(amount);
                     			
                            }
 
                        }
                  
                    }

                }

            } else {         
                		
                product_id = document.f.elements['product_ids[]'].value;
                
                price = document.f.elements['prices[]'].value;
                
                inp = document.f.elements['prices_wholesale[]'];
                priceWS = (inp ? inp.value : price);
                  
                amount = document.f.elements['amounts[]'].value;

                if (amount!="" && amount > 0){
				
                    total = Number(total) + Number(price * amount);
                    totalWS = Number(totalWS) + Number(priceWS * amount);
					
                    if (cart_hash[product_id]){
                     				
                        cart_hash[product_id]= Number(cart_hash[product_id]) + Number(amount);
                     					                     				
                    } else {
                     				
                        cart_hash[product_id]=Number(amount);
                     			
                    }
 
                }
				                	                                 
            }
			
        }

        cart="";
		
        for (i in cart_hash){
				
            if (cart_hash[i]){
				
                if (cart!="") cart = cart + ";";
                cart = cart + i+ "=" + cart_hash[i];
							
            }
        }
		
        total = Math.round(total*100)/100;
        totalWS = Math.round(totalWS*100)/100;
 		 
        createCookie('CART_'+shop_id,cart,10); 
        createCookie('CART_TOTAL_'+shop_id,total,10);
        createCookie('CART_TOTAL_WS_'+shop_id,totalWS,10);
                                       
        //if (document.getElementById('cart_total')) document.getElementById('cart_total').innerHTML = total;
        var totalCont = document.getElementById('cart_total');
        var totalStrikeoutCont = document.getElementById('cart_total_strikeout');

        if (totalCont){
            if(wholesale_limit && total > wholesale_limit){
                totalCont.innerHTML = totalWS;
                if(totalStrikeoutCont) totalStrikeoutCont.innerHTML = total;
            }else{
                totalCont.innerHTML = total;
                if(totalStrikeoutCont) totalStrikeoutCont.innerHTML = '';   //totalWS
            }
        }
        
        resetOrderList();
     
        return true;
     
    }
                   
    return false;
          
}




function addToCart(shop_id,product_id,price,amount, price_wholesale, wholesale_limit){

    product_id = Number(product_id);
    price_wholesale = Number(price_wholesale);

    if (!isNaN(shop_id)&&(shop_id>0)&&!isNaN(product_id)&&(product_id>0)&&!isNaN(price)&&!isNaN(amount)&&(amount>0)) {

        // Default wholesale value
        if(!price_wholesale && price) price_wholesale = price;

        var total = readCookie('CART_TOTAL_'+shop_id);
        var totalWholesale = readCookie('CART_TOTAL_WS_'+shop_id);
        var cart = unescape(readCookie('CART_'+shop_id));
        var cart_split = cart.split(';'), cart_new = "", cart_item_split, found = false, total_amount=0, ta;
        

        if (total=="") total = 0;
        if(totalWholesale=="") totalWholesale = 0;

        for (var i=0; i < cart_split.length; i++){

            cart_item_split = cart_split[i].split("=");

            if (cart_item_split.length == 2){
                 
                if (!found&&cart_item_split[0] == product_id) {

                    total = Number(total) + Number(price*amount);
                    totalWholesale = Number(totalWholesale) + Number(price_wholesale*amount);
                    ta = Number(amount)+Number(cart_item_split[1]);
                    found = true;

                    if (cart_new!="") cart_new = cart_new + ";";
                    cart_new = cart_new + product_id + "=" + ta;
                    
                    // Update total amount counter
                    total_amount += ta;

                } else {
                     
                    if (cart_new!="") cart_new = cart_new + ";";
                    cart_new = cart_new + cart_item_split[0] + "=" + cart_item_split[1];
                    
                    // Update total amount counter
                    total_amount += cart_item_split[1] * 1;
                    
                }
               
            }

        } // for

        if (!found) {

            if (cart_new!="") cart_new = cart_new + ";";
            cart_new = cart_new + product_id + "=" + amount;
            total = Number(total) + Number(price*amount);
            totalWholesale = Number(totalWholesale) + Number(price_wholesale*amount);
            
            // Total amount counter
            total_amount += amount * 1;

        }
                    
        total = Math.round(total*100)/100;
        totalWholesale = Math.round(totalWholesale*100)/100;
                    
        createCookie('CART_'+shop_id,cart_new,10);
        createCookie('CART_TOTAL_'+shop_id,total,10);
        createCookie('CART_TOTAL_WS_'+shop_id,totalWholesale,10);
          
        var totalCont = document.getElementById('cart_total');
        var totalStrikeoutCont = document.getElementById('cart_total_strikeout');
        var totalAmountCont = document.getElementById('cart_total_amount');
            
        if (totalCont){
            if(wholesale_limit && total > wholesale_limit){
                totalCont.innerHTML = totalWholesale;
                if(totalStrikeoutCont) totalStrikeoutCont.innerHTML = total;
            }else{
                totalCont.innerHTML = total;
                if(totalStrikeoutCont) totalStrikeoutCont.innerHTML = '';   //totalWholesale
            }
        }
        
        if(totalAmountCont) totalAmountCont.innerHTML = total_amount;
         
        return true;
    }

    return false;
}

function getParent(obj,tagName) {
	
    if (obj){
	
        var par = obj.parentNode;
								
        while (par&&(par.nodeName!=tagName)){
            par = par.parentNode;
        }

        return par;

    }

    return null;
		
}

function deleteRaw(o){
    v = getParent(o,"TR");
    if (v)
        v.parentNode.removeChild(v);
}

var dot = true;
var ttt = "2.23";
if (isNaN(ttt))
    dot = false;		

function getEventTarget(e){
    if (!e) e = window.event;

    if (e.target) {
        if (e.target.nodeType == 3) e.target = e.target.parentNode;
        return e.target;

    }
    else if (e.srcElement)
        return e.srcElement;
}

function inputOnlyRealNumber(obj,e){

    var target = getEventTarget(e);
    
    if (target&&target.nodeName=="INPUT"&&target.type=="text"){

        var valueBefore = target.value;
        var value="";
         
        if (dot){

            value = valueBefore.replace(",",".");

        } else {

            value = valueBefore.replace(".",",");

        }
                   
        value = value.replace(/[^\d\.,]+/,"");
         
        if (value.length>1)
            value = value.replace(/[0]*(\d*[\.,]?\d*).*/,"$1");
                                    
        if (value!=valueBefore){
         
            target.value =  value;
				         
        }
		 		                                           
        if (value != ""&&valueBefore==value) {
                                                   
            return true;

        }
                                                          
    }
   
    return false;

}

function recountCart(shop_id){
    var total = 0, totalWholesale = 0, cart="", product_id, price, amount, total_amount=0, priceWS, wholesale_limit=false, inp, resCont, i;
    
    // Get wholesale limit, if any 
    inp = document.f.elements['wholesale_limit'];
    wholesale_limit = (inp?inp.value:false);

    if (document.f.elements['product_ids[]']) {
        if (document.f.elements['product_ids[]'].nodeName!="INPUT")  {
            for (i=0;i<document.f.elements['product_ids[]'].length;i++){
                if (document.f.elements['product_ids[]'][i]) {

                    // Product ID and price
                    product_id = document.f.elements['product_ids[]'][i].value;
                    price = document.f.elements['prices[]'][i].value;

                    // Wholesale price
                    inp = document.f.elements['prices_wholesale[]'];
                    priceWS = (inp && inp[i] ? inp[i].value : price);

                    // Amount
                    amount = document.f.elements['amounts[]'][i].value;

                    // Totals
                    total = total + price*amount;
                    totalWholesale = totalWholesale + priceWS*amount;

                    // Cart product row 
                    if (amount > 0){
                        if (cart!="") cart = cart + ";";
                        cart = cart + product_id + "=" + amount;
                        
                        // Total amount counter
                        total_amount += amount*1;
                    }

                    // Write totals
                    resCont = document.getElementById('res_'+product_id);
                    if(resCont)
                        resCont.innerHTML = formatPrice((Math.round((price*amount)*100)/100).toFixed(2));

                    resCont = document.getElementById('res_ws'+product_id);
                    if(resCont)
                        resCont.innerHTML = formatPrice((Math.round((priceWS*amount)*100)/100).toFixed(2));
                }

            }

        } else {    // product IDs ate in INPUT tags
            // Product ID
            product_id = document.f.elements['product_ids[]'].value;

            // Prices
            price = document.f.elements['prices[]'].value;
            priceWS = document.f.elements['prices_wholesale[]'].value;

            // Amount
            amount = document.f.elements['amounts[]'].value;
            
            // Total amount counter	
            total_amount += amount * 1;
            
            // Subtotals
            total = total + price*amount;
            totalWholesale  = totalWholesale + priceWS*amount;

            // Cart row
            cart = product_id + "=" + amount;

            // Write subtotals
            resCont = document.getElementById('res_'+product_id);
            if(resCont)
                resCont.innerHTML = formatPrice((Math.round((price*amount)*100)/100).toFixed(2));
            resCont = document.getElementById('res_ws'+product_id);
            if(resCont)
                resCont.innerHTML = formatPrice((Math.round((priceWS*amount)*100)/100).toFixed(2));
        }

    } else {
        if (document.getElementById('cart_div')) document.getElementById('cart_div').innerHTML = "";
    }

    // Total sums
    total = Math.round(total*100)/100;
    totalWholesale = Math.round(totalWholesale*100)/100;

    // Cookies
    createCookie('CART_'+shop_id,cart,10); 
    createCookie('CART_TOTAL_'+shop_id,total,10);
    createCookie('CART_TOTAL_WS_'+shop_id,totalWholesale,10);

    resCont = document.getElementById('total');
    
    // Container for wholesale total
    var resContWS = document.getElementById('total_ws');

    // Cart total div
    var resContPreview = document.getElementById('cart_total');
    var resContStrikeoutPreview = document.getElementById('cart_total_strikeout');
    
    // Have wholesale limit?
    // Have container for wholesale total?
    if(wholesale_limit>0 && (resContWS || resContStrikeoutPreview)){
        // Write wholesale total 
        if(resContWS) resContWS.innerHTML = formatPrice(totalWholesale.toFixed(2));
        // if(resContStrikeoutPreview) resContStrikeoutPreview.innerHTML = totalWholesale;

        // Strikeout total, if the wholesale limit reached
        if(total > wholesale_limit && totalWholesale){
            var s = "<s>" + formatPrice(total.toFixed(2))  + "</s>"; 
            resCont.innerHTML = s + '&nbsp;'+ formatPrice(totalWholesale.toFixed(2));

            if(resContStrikeoutPreview) resContStrikeoutPreview.innerHTML = s;
            if(resContPreview) resContPreview.innerHTML = formatPrice(totalWholesale.toFixed(2));
        }else{      // wholesale limit is not reached
            resCont.innerHTML = formatPrice(total.toFixed(2));

            if(resContStrikeoutPreview) resContStrikeoutPreview.innerHTML = '';
            if(resContPreview) resContPreview.innerHTML = formatPrice(total.toFixed(2));
        }
    }else{
        resCont.innerHTML = formatPrice(total.toFixed(2));
    }
    
    // Total amount container 
    var totalAmountCont = document.getElementById('cart_total_amount');
    if(totalAmountCont) totalAmountCont.innerHTML = total_amount;
}

function resetOrderList(){
    
    var i, e;
    
    // Disable button
    var but = document.getElementById('submit_button');
    if (but)
	    but.disabled = true;
    
    // Reset amounts			
    if (document.f.elements['amounts[]']) {
        if (document.f.elements['amounts[]'].nodeName!="INPUT")  {
            for (i=0;i<document.f.elements['amounts[]'].length;i++){
                e = document.f.elements['amounts[]'][i];
                if (e)
                    e.value = 0;
            }
        } else {
            document.f.elements['amounts[]'].value = 0;
        }
    }
	
	// Clear total span value	
    var total_span = document.getElementById('total');
    if (total_span) total_span.innerHTML = 0;
		
}

function recountOrderList(){
    
    var total = 0; 
    
    // Wholesale limit
    var inp = document.f.elements['wholesale_limit'];
    var wholesale_limit = (inp ? inp.value : false);
    
    // Misc
    var product_id, price, amount, priceWS, totalWS = 0, totalWSA=[];
    var resCont; 
    var N, i, t;
    var is_wholesale = false;
    
    // Nodes
    var total_span = document.getElementById('total');
    var but = document.getElementById('submit_button');
    
    // Totals already in the cart
    inp = document.f.elements['shop_id'];
    var shop_id=(inp ? inp.value : 0);
    var inCartTotal = readCookie('CART_TOTAL_'+shop_id);
    var inCartTotalWS = readCookie('CART_TOTAL_WS_'+shop_id);   
    if(inCartTotal=="")inCartTotal = 0;
    if(inCartTotalWS=="")inCartTotalWS = 0;
    
    // Anything to do?                                     
    if (document.f.elements['product_ids[]']) {
        if (document.f.elements['product_ids[]'].nodeName!="INPUT")  {
            N = document.f.elements['product_ids[]'].length;    	
            
            for (i=0; i<N; ++i){
                inp = document.f.elements['product_ids[]'][i];
                if (inp) {
                    // Product ID                       
                    product_id = document.f.elements['product_ids[]'][i].value;
                    
                    // Prices
                    price = document.f.elements['prices[]'][i].value;
                    inp = document.f.elements['prices_wholesale[]'][i];
                    priceWS = (inp ? inp.value : false);
                    
                    // Amount
                    amount = document.f.elements['amounts[]'][i].value;
                    
                    // Subtotals
                    total += price * amount;
                    t = priceWS * amount;
                    totalWS += t;
                    totalWSA.push(t);
                	
                	// Temporarily write price here. Then we'll update it 
                	resCont = document.getElementById('res_'+product_id);
                	if(resCont)
                        resCont.innerHTML = Math.round((price*amount)*100)/100;
                }
            } // ## for
            
            var overallTotal = inCartTotal + total;
            var overallTotalWS = inCartTotalWS + totalWS;
            var limit = Math.max(overallTotal, overallTotalWS);
            
            // Wholesale?
            if(wholesale_limit && limit > wholesale_limit)
                is_wholesale = true;
            
            // Update subtotals & the total with wholesale checkup
            if(is_wholesale){
                for(i=0; i<N; ++i){
                    inp = document.f.elements['product_ids[]'][i];
                    if (inp) {
                        // Product ID                       
                        product_id = document.f.elements['product_ids[]'][i].value;
                        
                    	// Temporarily write price here. Then we'll update it 
                    	resCont = document.getElementById('res_'+product_id);
                    	if(resCont && i<totalWSA.length){
                    	    resCont.innerHTML = Math.round(totalWSA[i]*100)/100;
                        }
                    }
                } // ## for
            }
 
        } else {        // Single input
            // Product ID    		
            product_id = document.f.elements['product_ids[]'].value;
            
            // Prices    
            price = document.f.elements['prices[]'].value;
            inp = document.f.elements['prices_wholesale[]'][i];
            priceWS = (inp ? inp.value : false);
            
            // Amount       
            amount = document.f.elements['amounts[]'].value;
            
            // Totals             
            total += price * amount;
            totalWS += priceWS * amount;
            
            if(wholesale_limit && total > wholesale_limit)
                is_wholesale = true;
                                         
            document.getElementById('res_'+product_id).innerHTML = Math.round((is_wholesale ? totalWS : total) *100)/100;
        }
    }
    
    // Wholesale total? 
    if(is_wholesale)
        total = totalWS;
    
    // Assign total 	
    if (isNaN(total)||total==0||total=='') 
        total = 0;
    else 
        total = Math.round(total*100)/100;
	
	// Write total 
    if (total_span) 
        total_span.innerHTML = total;
  			                                  
    if (but)
    	but.disabled = (total==0?true:false);
}

function findPos(obj){

    var result = {};

    result.x = 0;
    result.y = 0;

    if (obj.offsetParent) {
		
        while (obj.offsetParent) {
            result.y += obj.offsetTop;
            result.x += obj.offsetLeft;
            obj = obj.offsetParent;
        }
			
    } else {
        if (obj.x) result.x += obj.x;
        if (obj.y) result.y += obj.y;
    }

    return result;

} 

function emptyInputBlur(obj,e){

    var target = getEventTarget(e);
   
    if (target&&target.nodeName=="INPUT"&&target.type=="text"){
   
        if (target.value=="") target.value = 0;
        return true;
   
    }
   
    return false;
   
}

function showAddMessage(obj) {

    var pos = findPos(obj);
		
    var d = document.getElementById("shop-added");

    if (d) {
	
        d = d.cloneNode(true);
        d.style.display = 'block';
        d.style.left = (pos.x+10)+ 'px';
        d.style.top = (pos.y + obj.offsetHeight - d.offsetHeight) + 'px';
        document.body.appendChild(d);
        d.style.top = (parseInt(d.style.top)- d.offsetHeight-10) + 'px';
		
	
        window.setTimeout(function(){
            if (d&&d.parentNode)d.parentNode.removeChild(d);
            delete d;
        },500);
		
    }
		
}

function addList(f,shop_id,func){

    if (addListToCart(shop_id)){

        if (func) func(f);
        else showAddMessage(f);
		
    }
	
    return false;
}

function addProductForm(shop_id,product_id,product_price,f,func,wholesale_limit){
   
    var inp = f.product_price_wholesale;
   
    if (addToCart(shop_id, product_id, product_price, f.product_amount.value,
       (inp ? inp.value : false), wholesale_limit)){

        if (func) func(f);
        else showAddMessage(f);
		
    }

    f.product_amount.value = "1";
        
    return false;
   
}
function formatPrice(str){
    if (str.length >= 4) {
        var parts = str.split("\."), res = [];
        for (var i = (parts[0].length - 1), j = 1; i >= 0; i--, j++) {
            res.unshift(parts[0].charAt(i));
            if (j % 3 == 0) 
                res.unshift(' ');
        }
        
        return res.join('') + (parts[1] ? '\.' + parts[1] : '');
    }
    
    return str;
}

