// Javascript

// convert all characters to lowercase to simplify testing
var agt = navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();

// *** BROWSER VERSION ***

var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
var is_opera = (agt.indexOf("opera") != -1);
var iePos  = appVer.indexOf("msie");
var is_ie   = ((iePos!=-1) && (!is_opera));
var is_ie7up = (is_ie && (agt.indexOf("msie 7.") != -1));

var e = window.encodeURIComponent ? encodeURIComponent : escape;
var uE = window.decodeURIComponent ? decodeURIComponent : unescape;

function showPic (Name, OWidth, OHeight, Url) {
	if (myWin) {
		myWin.close();
	}
	
	Width = (OWidth + 20);
	Height = (OHeight + 20);
	
	var myHTML = "<html>" + 
"<head>" + 
"<title>Pilt</title>" + 
"<meta http-equiv=\"imagetoolbar\" content=\"no\" />" + 
"<meta http-equiv=\"Pragma\" content=\"no-cache\" />" + 
"<meta http-equiv=\"Expires\" content=\"-1\" />" + 
"</head>" + 
"<body onclick=\"self.close();\" onload=\"self.focus();\" style=\"margin: 0;padding: 5px;color: #408ED1;\" oncontextmenu=\"return false;\">" + 
"<div style=\"border: 1px solid #b3ddff; padding: 5px; background: url(" + Url + ") no-repeat; background-position: center center;\">" + 
"<img src=\"pix/pic.gif\" height=\"" + OHeight + "\" width=\"" + OWidth + "\" border=\"0\" galleryimg=\"no\" oncontextmenu=\"return false;\" />" + 
"</div>" + 
"</body>" + 
"</html>";
	
	var myWin = window.open("", "Image", "location=no,width=" + Width + ",height=" + Height); // a window object
	with (myWin.document) {
		open("text/html", "replace");
		write(myHTML);
		close();
	}
	return false;
}

function inc_x (pID) {
	return korv_muuda(pID, 1);
}

function dec_x (pID) {
	return korv_muuda(pID, -1);
}

function korv_muuda (pID, palju_muuta) {
	item_id = "changeQuantity[" + pID + "]";
	v22rtus = 0;
	if (document.editCart[item_id].value) {
		v22rtus = parseInt(document.editCart[item_id].value);
		if (!v22rtus) { v22rtus = 0; }
	}
	v22rtus += palju_muuta;
	if (v22rtus < 0) { v22rtus = 0; }
	if (v22rtus > 9999) { v22rtus = 9999; }
	document.editCart[item_id].value = v22rtus;
	document.editCart[item_id].focus();
	return false;
}

function dbl_click (inp) {
	if (navigator.appName == "Microsoft Internet Explorer") { inp.onclick(); }
}

function getElement(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	}
	else if (document.all) {
		return document.all[id];
	}
	else if (document.layers) {
		return document.layers[id];
	}
}

function cartItem (Url, ID, prodID, Verifier, Quantity, Price) {
	Quantity = parseInt(Quantity);
	if (Quantity) {
		msg = msgAdd;
	} else {
		msg = msgRemove;
	}
	window.status = msg;
	getElement("infoStr").innerHTML = msg;
	initLayer("infoStr", 120, 40);
	
	data = "id=" + ID;
	data = data + "&prodID=" + prodID;
	data = data + "&changeQuantity[" + (Verifier != "" ? e(Verifier) : prodID) + "]=" + Quantity;
	if (!Price) {
		selObj = getElement("price" + prodID);
		Price = selObj.options[selObj.selectedIndex].value;
	}
	data = data + "&itemPrice[" + (Verifier != "" ? e(Verifier) : prodID) + "]=" + Price;

	sendData = data + "&XMLHttpRequest=1";
	if (requestData(Url, sendData, "cartItemCallback", "TEXT") == true) {
//		alert("Andmed (cartItem) saadetud.");
	} else {
//		alert("Andmete (cartItem) saatmine ebaġnnestus!");
		document.location = Url + "?" + data;
	}
	return false;
}

function cartItemCallback (response) {
	window.status = "Done";
	
//	alert("callBackRecved :)");
	
	if (isNaN(response)) {
		getElement("infoStr").innerHTML = msgError + response;
	} else {
		getElement("infoStr").innerHTML = msgDone;
		
	}
	
	initLayer("infoStr", 120, 40);
	
	updateCartInfoMsg();
}

// threadsafe asynchronous XMLHTTPRequest code
function requestData(url, data, callback, dType) {
    resXML = (dType == "XML" ? true : false);
    // we use a javascript feature here called "inner functions"
    // using these means the local variables retain their values after the outer function
    // has returned. this is useful for thread safety, so
    // reassigning the onreadystatechange function doesn\'t stomp over earlier requests.
    function ajaxBindCallback() {
        if (ajaxRequest.readyState == 4) {
            if (ajaxRequest.status == 200) {
                if (ajaxCallback) {
                    if (resXML) {
                        eval(ajaxCallback + "(ajaxRequest.responseXML.documentElement)");
                    } else {
                        eval(ajaxCallback + "(ajaxRequest.responseText)");
                    }
                } else {
                    alert("no callback defined");
                }
            } else {
                alert("There was a problem retrieving the xml data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
            }
        }
    }
    
    // use a local variable to hold our request and callback until the inner function is called...
    var ajaxRequest = false;
    var ajaxCallback = callback;
    
    // bind our callback then hit the server...
    ajaxRequest = getHTTPObject(resXML);
	
    if (!ajaxRequest) {
        return false;
    } else {
        ajaxRequest.onreadystatechange = ajaxBindCallback;
        try {
            ajaxRequest.open("POST", url, true);
        } catch (e) {
            return false;
        }
        ajaxRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        ajaxRequest.send(data);
        return true;
    }
    return false;
}


function getHTTPObject (resXML) {
	var xO = false;

	if (typeof XMLHttpRequest != "undefined") {
		try {
			xO = new XMLHttpRequest();
			if (resXML) {
				xO.overrideMimeType("text/xml");
			} else {
				xO.overrideMimeType("text/html");
			}
			return xO;
		} catch (e) {
			if (is_ie7up) {
				xO = new XMLHttpRequest();
				return xO;
			} else {
				xO = false;
			}
		}
	}
	if(!xO) {
		var msxmls = new Array(
			"Msxml2.XMLHTTP",
			"Msxml2.XMLHTTP.5.0",
			"Msxml2.XMLHTTP.4.0",
			"Msxml2.XMLHTTP.3.0",
			"Microsoft.XMLHTTP"
		);
		for (var i = 0; i < msxmls.length; i++) {
			var msxml = msxmls[i]; 
			try {
				xO = new ActiveXObject(msxml);
				return xO;
			} catch(e) {
				xO = false;
			}
		}
	}
	return xO;
}

function updateCartInfoMsg () {
	bCa = 0;
	bTq = 0;
	bVq = 0;
	for (var i = 0; i<document.editCart.elements.length; i++) {
		if (document.editCart.elements[i].type == "text") {
			if (document.editCart.elements[i].name.indexOf("changeQuantity") > -1) {
				sbid = document.editCart.elements[i].name.substring(15, (document.editCart.elements[i].name.length - 1));
				
    			bQl = "changeQuantity[" + sbid + "]";
    			ql = 0;
    			if(document.getElementById(bQl) && document.getElementById(bQl).value) {
    				ql = parseInt(document.getElementById(bQl).value);
    				if(!ql) ql = 0;
    			}
				
				bPz = "prodPrice[" + sbid + "]";
    			price = 0;
    			if(document.getElementById(bPz) && document.getElementById(bPz).value) {
    				price = document.getElementById(bPz).value;
    			}
    			bTo = parseFloat(ql * price);
				
				bVt = "prodVAT[" + sbid + "]";
    			vaO = 0;
    			if(document.getElementById(bVt) && document.getElementById(bVt).value) {
    				vaO = document.getElementById(bVt).value;
    			}
				
                vat = (vaO / 100);
                vat = (1 + vat);
				bVo = bTo - (bTo / vat);

    			bTq += ql;
    			bCa += bTo;
				bVq += bVo;
			}
		}
	}
	bCa = bCa.toFixed(2);
	bVq = bVq.toFixed(2);
	woV = (bCa - bVq);
	woV = woV.toFixed(2);

/*

bQl: changeQuantity[74affa40d584f4beae7273395a0338cd0de2293d] & 
ql: 1 & 
bPz: prodPrice[74affa40d584f4beae7273395a0338cd0de2293d] & 
price: 21.97 & 
bVt: prodVAT[74affa40d584f4beae7273395a0338cd0de2293d] & 
vaO: 1.15 & 
bCa: 21.97 & 
bVq: 0.25 & 
woV: 21.72





Total: 18.62 EUR
VAT: 3.35 EUR
Grand total: 21.97 EUR

*/

//	var str = "bQl: " + bQl + " & ql: " + ql + " & bPz: " + bPz + " & price: " + price + " & bVt: " + bVt + " & vaO: " + vaO + " & bCa: " + bCa + " & bVq: " + bVq + " & woV: " + woV;
//	alert(str);
//	document.write(str);
	if(document.getElementById("cartTotSum")){
		document.getElementById("cartTotSum").innerHTML = woV;
		document.getElementById("cartTotVat").innerHTML = bVq;
		document.getElementById("cartTotAll").innerHTML = bCa;
	}
}

function updateTransportCost (selected) {
	bCa = 0;
	bTq = 0;
	bVq = 0;
	if(document.getElementById("getCartWovat").value) {
		bTq = parseFloat(document.getElementById("getCartWovat").value);
	}
	if(document.getElementById("getCartVat").value) {
		bVq = parseFloat(document.getElementById("getCartVat").value);
	}
	if(document.getElementById("getCartTotal").value) {
		bCa = parseFloat(document.getElementById("getCartTotal").value);
	}
	tSum = 0;
	tVat = 0;
	tTot = 0;
	if (selected) {
		if (transport[selected]) {
			tTot = parseFloat(transport[selected]);
			vat = 18;
            vat = (vat / 100);
            vat = (1 + vat);
			tVat = tTot - (tTot / vat);
			tSum = tTot - tVat
		}
	}
	tSum = (bTq + tSum);
	tVat = (bVq + tVat);
	tTot = (bCa + tTot);
	
	tSum = tSum.toFixed(2);
	tVat = tVat.toFixed(2);
	tTot = tTot.toFixed(2);
	document.getElementById("cartTotSum").innerHTML = tSum;
	document.getElementById("cartTotVat").innerHTML = tVat;
	document.getElementById("cartTotAll").innerHTML = tTot;
}

/****************************************************************/

var xz = new Object;

xz.Obj = 0;
xz.Width = 0;
xz.Height = 0;

xz.reSizeTimer = 0;
xz.hideLayerTimer = 0;

xz.ScreenWidth = 0;
xz.ScreenHeight = 0;

xz.scrollTop = 0;
xz.scrollLeft = 0;

function initLayer(Id, thisWidth, thisHeight) {
    if (initObject(Id)) {
		showLayer();
		
		setObjectDimensions(thisWidth, thisHeight);
				
		reScalSize();
		
		XPosition = xz.scrollLeft + ( (xz.ScreenWidth - xz.Width) / 2);
		YPosition = xz.scrollTop + ( (xz.ScreenHeight - xz.Height) / 2);
		
		moveLayerTo(XPosition, YPosition);
		
		if (xz.hideLayerTimer) {
			clearTimeout(xz.hideLayerTimer);
		}
		xz.hideLayerTimer = setTimeout("hideLayer()", 1500);
	}
}

function setObjectDimensions (thisWidth, thisHeight) {
	if (xz.Obj.scrollHeight) {
		xz.Height = parseInt(xz.Obj.scrollHeight);
	} else if (xz.Obj.offsetHeight) {
		xz.Height = parseInt(xz.Obj.offsetHeight);
	} else {
		xz.Height = parseInt(thisHeight);
	}
	xz.Width = parseInt(thisWidth);
}

function initObject (Id) {
	if (document.getElementById) {
		xz.Obj = document.getElementById(Id);
		if (xz.Obj) {
			return true;
		}
	}
	return false;
}

function hideLayer () {
	xz.Obj.style.display = "none";
}

function showLayer () {
	xz.Obj.style.display = "block";
}

function moveLayerTo(x, y) {
	xz.Obj.style.left = parseInt(x) + "px";	
	xz.Obj.style.top = parseInt(y) + "px";
}

function reScalSize () {
	if ( typeof( window.innerWidth ) == 'number' ) {
		frameWidth = window.innerWidth;
		frameHeight = window.innerHeight;
	} else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	} else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	} else {
		frameWidth = window.screen.width;
		frameHeight = window.screen.height;
	}
	
	xz.ScreenWidth = frameWidth;
	xz.ScreenHeight = frameHeight;
	
	setMyScrolling();
	
	xz.reSizeTimer = setTimeout("reScalSize()", 500);
}

function setMyScrolling () {
	var posY = 0, posX = 0;
	if ( typeof( window.pageYOffset ) == 'number' ) {
		posY = window.pageYOffset;
		posX = window.pageXOffset;
	} else if ( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		posY = document.documentElement.scrollTop;
		posX = document.documentElement.scrollLeft;
	} else if ( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		posY = document.body.scrollTop;
		posX = document.body.scrollLeft;
	}
	xz.scrollTop = posY;
	xz.scrollLeft = posX;
}