
if (navigator && navigator.cookieEnabled == false) {

    document.write(no_cookies_message);
    
}

function addListToCart(shop_id){

    if (!isNaN(shop_id) && document.f.elements['product_ids[]']) {
    
        var but = document.getElementById('submit_button');
        
        if (but) {
            but.disabled = true;
        }
        
        var total = readCookie('CART_TOTAL_' + shop_id);
        var cart = unescape(readCookie('CART_' + shop_id));
        var cart_split = cart.split(';');
        var cart_item_split;
        var cart_hash = {};
        var found = false;
        
        for (var 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 (var i = 0; i < document.f.elements['product_ids[]'].length; i++) {
                
                    if (document.f.elements['product_ids[]'][i]) {
                    
                        var product_id = document.f.elements['product_ids[]'][i].value;
                        
                        var price = document.f.elements['prices[]'][i].value;
                        
                        var amount = document.f.elements['amounts[]'][i].value;
                        
                        if (amount != "" && amount > 0) {
                        
                            total = Number(total) + Number(price * 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 {
            
                var product_id = document.f.elements['product_ids[]'].value;
                
                var price = document.f.elements['prices[]'].value;
                
                var amount = document.f.elements['amounts[]'].value;
                
                if (amount != "" && amount > 0) {
                
                    total = Number(total) + Number(price * 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 (var i in cart_hash) {
        
            if (cart_hash[i]) {
            
                if (cart != "") 
                    cart = cart + ";";
                cart = cart + i + "=" + cart_hash[i];
                
            }
            
        }
        
        total = Math.round(total * 100) / 100;
        
        createCookie('CART_' + shop_id, cart, 10);
        createCookie('CART_TOTAL_' + shop_id, total, 10);
        
        if (document.getElementById('cart_total')) 
            document.getElementById('cart_total').innerHTML = formatPrice(total.toFixed(0));
        
        resetOrderList();
        
        return true;
        
    }
    
    return false;
    
}

//
// @comment: CART_ - cookie format: product_id=amount_with_border@amount_without_border; ...
//

function addToCart(shop_id, product_id, price, amount, with_border){

    shop_id = parseInt(shop_id);
    product_id = Number(product_id);
    price = parseFloat(price);
    amount = parseInt(amount);
    
    with_border = (parseInt(with_border) > 0 ? 1 : 0);
    
    if (!isNaN(shop_id) && (shop_id > 0) && !isNaN(product_id) && (product_id > 0) && !isNaN(price) && !isNaN(amount) && (amount > 0)) {
        // get cookie	
        var total = readCookie('CART_TOTAL_' + shop_id);
        var cart = unescape(readCookie('CART_' + shop_id));
        
        // calc total	
        total = parseFloat(total);
        if (isNaN(total)) {
            total = 0;
        }
        total += parseFloat(amount * price);
        
        var bExists = (cart.indexOf(product_id + "=") != -1 ? true : false);
        
        if (bExists) {
            var r = new RegExp("(" + product_id + "\\=)(\\d+)\\@(\\d+)");
            var a = cart.match(r);
            
            if (a.length == 4) {
                a[(3 - with_border)] = parseFloat(a[(3 - with_border)]) + amount;
                cart = cart.replace(r, "$1" + a[2] + "@" + a[3]);
            }
        }
        else {
            if (cart.indexOf("=") == -1) {
                cart = "";
            }
            var a = (with_border ? [amount, 0] : [0, amount]);
            cart += (cart != "" ? ";" : "") + product_id + "=" + a[0] + "@" + a[1];
        }
        
        total = Math.round(total * 100) / 100;
        
        createCookie('CART_' + shop_id, cart, 10);
        createCookie('CART_TOTAL_' + shop_id, total, 10);
        
        if (document.getElementById('cart_total')) 
            document.getElementById('cart_total').innerHTML = formatPrice(total.toFixed(0));
        
        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;
    
    var cart = "";
    
    var productIds = document.f.elements["cart_product_ids"].value.split(",");
    
    if (productIds && productIds.length > 0) {
    
        for (var i = 0, price1=0,price2=0; i < productIds.length; ++i) {
        	
			amount1 = amount2 = 0;
		
            // without border
            
            price1 = document.f.elements['price_' + productIds[i]];
			var p=0;
			
			if(price1){
				price1 = price1.value;
				var amount1 = parseInt(document.f.elements['amount_' + productIds[i]].value);
            	p = price1 * amount1;
				
            	total = total + p;	
			}
			else{
				price1 = 0;
			}
            var res = document.getElementById('res_'+productIds[i]);
			if(res) res.innerHTML = p;
            
            // with border
            
            price2 = document.f.elements['b_price_' + productIds[i]];
            var p2=0;
			
			if(price2){
				
				price2 = price2.value;
				var amount2 = parseInt(document.f.elements['b_amount_' + productIds[i]].value);
            	p2 = price2 * amount2;
								
				total += p2;
					
			}
			else{
				
				price2 = 0;
				
			}
			var b_res = document.getElementById('b_res_'+productIds[i]);
			if(b_res)b_res.innerHTML = p2;
			
			
			if(amount1>0 || amount2>0){
				if (cart != "") {
	                cart = cart + ";";
	            }
	            cart +=  productIds[i] + "=" + amount2 + "@" + amount1;
			}
        }
    }
	else{
		if (document.getElementById('cart_div')) 
            document.getElementById('cart_div').innerHTML = "";
   }
   total = Math.round(total * 100) / 100;
    
   createCookie('CART_' + shop_id, cart, 10);
   createCookie('CART_TOTAL_' + shop_id, total, 10);
    
   if (document.getElementById('total')) 
        document.getElementById('total').innerHTML = formatPrice(total.toFixed(0));
   if (document.getElementById('cart_total')) 
        document.getElementById('cart_total').innerHTML = formatPrice(total.toFixed(0));
    
}
	
//	if (document.f.elements['product_ids[]']) {
//    
//        if (document.f.elements['product_ids[]'].nodeName != "INPUT") {
//        
//            for (var i = 0; i < document.f.elements['product_ids[]'].length; i++) {
//            
//                if (document.f.elements['product_ids[]'][i]) {
//                
//                    var product_id = document.f.elements['product_ids[]'][i].value;
//                    
//                    var price = document.f.elements['prices[]'][i].value;
//                    
//                    var amount = document.f.elements['amounts[]'][i].value;
//                    
//                    total = total + price * amount;
//                    
//					// search for item with border
//					var price2 = document.f.elements["b_price_"+product_id].value;
//					var amount2 = 0;
//										
//					if(price2){
//						
//						amount2 = document.f.elements["b_amount_"+product_id].value;
//						
//						total += price2 * amount2;
//						
//						document.getElementById('b_res_' + product_id).innerHTML = Math.round((price2 * amount2) * 100) / 100;
//						
//					}
//					
//					if (amount > 0) {
//                    
//                        if (cart != "") 
//                            cart = cart + ";";
//                        cart = cart + product_id + "=" + amount2 + "@" + amount;
//                        
//                    }
//                    
//                    document.getElementById('res_' + product_id).innerHTML = Math.round((price * amount) * 100) / 100;
//		            
//                }
//                
//            }
//            
//        }
//        else {
//        
//            var product_id = document.f.elements['product_ids[]'].value;
//            
//            var price = document.f.elements['prices[]'].value;
//            
//            var amount = document.f.elements['amounts[]'].value;
//            
//            total = total + price * amount;
//			
//			// search for item with border
//			
//			var price2 = document.f.elements["b_price_"+product_id].value;
//			var amount2 = 0;
//									
//			if(price2){
//					
//				amount2 = document.f.elements["b_amount_"+product_id].value;
//							
//				total += price2 * amount2;
//							
//				document.getElementById('b_res_' + product_id).innerHTML = Math.round((price2 * amount2) * 100) / 100;
//				
//			}
//            
//            cart = product_id + "=" + amount2 + "@" + amount;
//            
//            document.getElementById('res_' + product_id).innerHTML = Math.round((price * amount) * 100) / 100;
//            
//        }
//        
//    }
//    else {
//    
//        if (document.getElementById('cart_div')) 
//            document.getElementById('cart_div').innerHTML = "";
//        
//    }
//    
//    total = Math.round(total * 100) / 100;
//    
//    createCookie('CART_' + shop_id, cart, 10);
//    createCookie('CART_TOTAL_' + shop_id, total, 10);
//    
//    if (document.getElementById('total')) 
//        document.getElementById('total').innerHTML = total;
//    if (document.getElementById('cart_total')) 
//        document.getElementById('cart_total').innerHTML = total;
//    
//}

function resetOrderList(){

    var but = document.getElementById('submit_button');
    
    if (but) {
    
        but.disabled = true;
        
    }
    
    if (document.f.elements['amounts[]']) {
    
        if (document.f.elements['amounts[]'].nodeName != "INPUT") {
        
            for (var i = 0; i < document.f.elements['amounts[]'].length; i++) {
            
                if (document.f.elements['amounts[]'][i]) {
                
                    document.f.elements['amounts[]'][i].value = 0;
                    
                }
                
            }
            
        }
        else {
        
            document.f.elements['amounts[]'].value = 0;
            
        }
        
    }
    
    var total_span = document.getElementById('total');
    
    if (total_span) 
        total_span.innerHTML = 0;
    
}

function recountOrderList(){

    var total = 0;
    
    if (document.f.elements['product_ids[]']) {
    
        if (document.f.elements['product_ids[]'].nodeName != "INPUT") {
        
            for (var i = 0; i < document.f.elements['product_ids[]'].length; i++) {
            
                if (document.f.elements['product_ids[]'][i]) {
                
                    var product_id = document.f.elements['product_ids[]'][i].value;
                    
                    var price = document.f.elements['prices[]'][i].value;
                    
                    var amount = document.f.elements['amounts[]'][i].value;
                    
                    total = total + price * amount;
                    
                    document.getElementById('res_' + product_id).innerHTML = formatPrice((Math.round((price * amount) * 100) / 100).toFixed(0));
                    
                }
                
            }
            
        }
        else {
        
            var product_id = document.f.elements['product_ids[]'].value;
            
            var price = document.f.elements['prices[]'].value;
            
            var amount = document.f.elements['amounts[]'].value;
            
            total = total + price * amount;
            
            document.getElementById('res_' + product_id).innerHTML = formatPrice((Math.round((price * amount) * 100) / 100).toFixed(0));
            
        }
        
        
    }
    
    var total_span = document.getElementById('total');
    
    if (isNaN(total) || total == 0 || total == '') 
        total = 0;
    else 
        total = Math.round(total * 100) / 100;
    
    if (total_span) 
        total_span.innerHTML = total;
    
    var but = document.getElementById('submit_button');
    
    if (but) {
    
        if (total == 0) 
            but.disabled = true;
        else 
            but.disabled = 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_prices, f, func, with_border){

    if (product_prices.length == 2) {
    
        if (addToCart(shop_id, product_id, product_prices[(parseInt(with_border) > 0 ? 0 : 1)], f.product_amount.value, with_border)) {
        
            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("\.");
        var 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.length==2?('\.' + parts[1]):'');
    }
    else {
        return str;
    }
}
