﻿function SendRequest(url, data) {
    if(url[0] = "/") {
        try {
            var root = document.URL;
            var i = root.indexOf("/", 9);
            if(i > 0) {
                url = root.substr(0, i) + url;
            }
        }
        catch(e) {
            //alert("Ваш браузер не поддерживает работу с корзиной.");
            return "";
        }
    }
    var req;
    try {
        req = new XMLHttpRequest();
    } catch (e) {
        var lib = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0',
            'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'MICROSOFT.XMLHTTP.1.0',
            'MICROSOFT.XMLHTTP.1', 'MICROSOFT.XMLHTTP'];

        for (var i = 0; i < lib.length; i++) {
            try {
                req = new ActiveXObject(lib[i]);
                break;
            } catch (e) {
            }
        }
    }
    if (req) {
        try {
            req.open("POST", url, false);
            req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            req.send(data);
            return req.responseText;
        } catch (e) {
        }
    }
    //alert("Ваш браузер не поддерживает работу с корзиной.");
    return "";
}

function AddToCart(url, product) {
    if (document.all("addtocart" + product) == null) return;

	var count = parseInt(document.all("addtocart" + product).value);
	if(isNaN(count)) {
		count = 1;
	}
	document.all("addtocart" + product).value = count.toString();

	var response = SendRequest(url, "op=add&product=" + product.toString() + "&count=" + count.toString());
	if ((response != "") && (document.all("cartlinkland") != null)) {
	    document.all("cartlinkland").innerHTML = response;
	}
}

function GetCart(url) {
    if (document.all("shoppingcartland") == null) return;

    var response = SendRequest(url, "op=get");
    if (response != "") {
        document.all("shoppingcartland").innerHTML = response;
    }
}

function RecalcCart(url, product) {
    if (document.all("editcart" + product) == null) return;
    
	var count = parseInt(document.all("editcart" + product).value);
	if(isNaN(count)) {
		count = 1;
	}
	document.all("editcart" + product).value = count.toString();
	
	var response = SendRequest(url, "op=rec&product=" + product.toString() + "&count=" + count.toString());
	if ((response != "") && (document.all("cartlinkland") != null)) {
	    document.all("cartlinkland").innerHTML = response;
	}
	GetCart(url);
}

function DeleteCart(url, product) {
	var response = SendRequest(url, "op=rem&product=" + product.toString());
	if ((response != "") && (document.all("cartlinkland") != null)) {
	    document.all("cartlinkland").innerHTML = response;
	}
	GetCart(url);
}

function GetCartLink(url) {
	var response = SendRequest(url, "op=link");
	if ((response != "") && (document.all("cartlinkland") != null)) {
	    document.all("cartlinkland").innerHTML = response;
	}
}

function RefreshCart(url) {
    GetCartLink(url);
    GetCart(url);
}

