var preload=new Image();
preload.src="img/b_est_login_2.gif";
preload.src="img/b_ok_2.gif";
preload.src="img/b_est_proceed_2.gif";
preload.src="img/b_est_continue_2.gif";


var agt = navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();

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));

function printWindow() {
	browserVersion = parseInt(navigator.appVersion)
	if (browserVersion >= 4) window.print()
}

var Idx = 0;
function showPopUp(url){
	requestData (url+ "&XMLHttpRequest=1", "", "showPopUpCallBack", "TEXT");
}
function showPopUpCallBack (res){
	$("popup").style.display='block';
	$("popupover").style.display='block';
	$("popUpContent").innerHTML = res;
}

function hidePopUp(){
	getElement("popup").style.display='none';
	getElement("popupover").style.display='none';
}


function showPopUp2(url){
	requestData (url+ "&XMLHttpRequest=1", "", "showPopUpCallBack2", "TEXT");
}
function showPopUpCallBack2 (res){
	$("popup2").style.display='block';
	$("popupover").style.display='block';
	$("popUpContent").innerHTML = res;
}

function hidePopUp2(){
	getElement("popup2").style.display='none';
	getElement("popupover").style.display='none';
}


var Idx = 0;
var timeup = 0;
var Idx = 0;
var Idx2 = 0;

function chgCartAmount(id, amt)
{
	getElement("infoStr").innerHTML = msg;
	elm = $('toode_'+id);
	curVal = parseInt(elm.value);
	if (isNaN(curVal)) {
		curVal = 0;
	}
	if(curVal){
		if(amt=='+1' || amt=='-1'){
			newVal = parseInt(elm.value) + amt;
		}else{
			newVal = amt;
		}
	}else if(amt > 0){
		newVal = amt;
	}else{
		newVal = 0;
	}
	elm.value = (newVal > 0) ? newVal : '';
	
	if (Idx) {
		window.clearTimeout(Idx);
		Idx = 0;
	}
	if (Idx2) {
		window.clearTimeout(Idx2);
		Idx2 = 0;
	}
	
	var Url ="?teg=lisa_toode";
	data = "prodID=" + id;
	data = data + "&toode_" + id + "=" + newVal;
	data = data + "&XMLHttpRequest=1";
	Idx = window.setTimeout('requestData ("'+Url+'", "'+data+'", "cartItemCallback", "TEXT");',500);
	Idx2 = window.setTimeout('initLayer ("infoStr", 100, 40);',500);
	if(newVal > 0){
		$("emtybtn_"+id).style.display='block';
		if($("prodMessage_"+id)){
			$("prodMessage_"+id).innerHTML = msgIncart;
		}
	}else{
		$("emtybtn_"+id).style.display='none';
		if($("prodMessage_"+id)){
			$("prodMessage_"+id).innerHTML = msgAddcart;
		}
	}

	
}

function cartItemCallback (response) {
	window.status = "Done";
	
	//alert("callBackRecved :)");
	
	updateCartBox();
	if($("mainCartTable")){
		updateCartView();
	}
	if (response!="ok") {
		$("infoStr").innerHTML = msgError + response;
	} else {
		$("infoStr").innerHTML = msgDone;	
	}
	
}
var Idx = 0;
function updateCartView(){
	var Url ="?teg=uuenda_korvi_vaade";
	data = "XMLHttpRequest=1";
	Idx = window.setTimeout('requestData ("'+Url+'", "'+data+'", "updateCartViewResponse", "TEXT");',500);
}
function updateCartViewResponse(response){
	getElement("mainCartTable").innerHTML = response;	
}

var Idx = 0;

function updateCartBox(){
	var Url ="?teg=uuenda_korv";
	data = "XMLHttpRequest=1";
	Idx = window.setTimeout('requestData ("'+Url+'", "'+data+'", "updateCartBoxResponse", "TEXT");',500);
}
function updateCartBoxResponse(response){
	getElement("shoppingCartData").innerHTML = response;	
}



function updateCheckOut(step, link){
	if($("checkOutForm")){
		var postdata=$("checkOutForm").serialize();
	}else{
		var postdata = '';
	}
	new Ajax.Request("?teg=CheckOut&Step="+step+"&XMLHttpRequest=1",
	{
		method:"post",
		parameters: postdata,
		onSuccess: function(response) {
			updateCheckOutResponse(response.responseText, link)
		}
	}); 

}
var Idx = 0;

function updateCheckOutResponse(response, link){
	if(response == 'TTMAP'){
		window.location="http://alcostore.1kdigital.com/tt.test.html";
	}else{
		getElement("CheckOutDiv").innerHTML = response;
		if(link){
			Idx = window.setTimeout('window.location="'+link+'"',2000);
		}
	}

}

function enableDisableTTTime(today, obj){
	if(today==obj.value){
		for (var i=0; i<$("ttAddrTime").length; i++) {
			if($("disabled_"+i)){
				$("disabled_"+i).disabled=true;
				$("ttAddrTime").selectedIndex = 0;
			}

		}	
	}else{
		for (var i=0; i<$("ttAddrTime").length; i++) {
			$("ttAddrTime").options[i].disabled=false;
		}
	}
}

function enableDisableTTLime(today, obj){
	if(today==obj.value){
		for (var i=0; i<$("ttTouristTime").length; i++) {
			if($("disabled_"+i)){
				$("disabled_"+i).disabled=true;
				$("ttAddrTime").selectedIndex = 0;
			}
		}	
	}else{
		for (var i=0; i<$("ttTouristTime").length; i++) {
			$("ttTouristTime").options[i].disabled=false;
		}
	}
}

function submitPopUpForm(obj, url){

	new Ajax.Request(url+"&XMLHttpRequest=1",
	{
		method:"post",
		parameters: $(obj).serialize(),
		onSuccess: function(response) {
			$('popUpContent').innerHTML=response.responseText;
		}
	}); 
	return false;
}
			

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 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();
	
    if (!ajaxRequest) {
        return false;
    } else {
        ajaxRequest.onreadystatechange = ajaxBindCallback;
        try {
            ajaxRequest.open("POST", url, true);
        } catch (e) {
        	alert(e);
            return false;
        }
        ajaxRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        ajaxRequest.send(data);
        return true;
    }
    return false;
}

function getHTTPObject () {
	var xO = false;
	if (typeof XMLHttpRequest != "undefined") {
		try {
			xO = new XMLHttpRequest();
			xO.overrideMimeType("text/xml");
			return xO;
		} catch (e) {
			try {
				xO = new XMLHttpRequest();
				return xO;
			} catch (e) {
				xO = false;
			}
		}
	} else {
		try {
			xO = new ActiveXObject("Msxml2.XMLHTTP");
			return xO;
		} catch (e) {
			try {
				xO = new ActiveXObject("Microsoft.XMLHTTP");
				return xO;
			} catch (e) {
				xO = false;
			}
		}
	}
	return xO;
}

function stdAction(frm) {
	for (var i=1; i<arguments.length; i=i+2) {
		frm.elements[arguments[i]].value = arguments[i+1]
	}
	frm.submit();
}

function chgCartAmount1(id, amt) {
	elm = document.getElementById(id);
	curVal = parseInt(elm.value);
	newVal = parseInt(elm.value) + amt;
	elm.value = (newVal > 0) ? newVal : curVal;
}

function salvesta_valuuta(nevVal) {
	window.location = "?teg=valuuta&currency="+nevVal+"&oldLoc="+encodeURIComponent(location.href);
}
/*function count5() {
	alert("test1");
}*/
function count5(v2li, number, hind, blokisoodus, blokikogus, kriitiline, pant)
         {
			 //alert("test");
			 var tulemus = eval(document.ostukorv['kogus_' + v2li].value) + eval(number);
			 if (tulemus > 0)
			 {
					
					document.ostukorv['kogus_' + v2li].value = tulemus;
					
					/*var toote_hind = tulemus * hind;
					if (km > 0) toote_hind = toote_hind + (toote_hind * km);
					if (tulemus >= blokikogus) toote_hind = toote_hind - (toote_hind * blokisoodus);
					if (taara > 0) toote_hind = toote_hind + (taara * tulemus);
					if (pant > 0) toote_hind = toote_hind + (pant * tulemus);
					if (tase > 0) toote_hind = toote_hind - (toote_hind * tase);
					if (synd > 0) toote_hind = toote_hind - (toote_hind * synd);*/
					//if (ettemaks > 0) toote_hind = toote_hind - (toote_hind * ettemaks);
					
					var vahe = Math.floor(tulemus / blokikogus);
					
					if (vahe > 0) { 
						var j22k = tulemus - (vahe * blokikogus);
						var toote_hind = (vahe * hind * blokikogus - (vahe * hind * blokikogus * (blokisoodus / 100))) + (j22k * hind);
						//alert(j22k);
					}
					else {
						var toote_hind = tulemus * hind;
					}
					
					hind = toote_hind / tulemus;

					
					

					document.ostukorv['hind_' + v2li].value = makeNice(toote_hind);

					var summa = 0;
					//alert(document.getElementById('v2li_1').value);
					for (var ii = 1; ii <= PriceAllCount; ii++)
					{
						summa = (summa + parseFloat(document.getElementById('v2li_' + ii).value));

					}
					//document.ostukorv['hind_km'].value = makeNice(PriceAllPages * 0.18);
					//document.ostukorv['hind_kokku'].value = makeNice((PriceAllPages * 0.18) + PriceAllPages);
					
					
					if (number == -1) PantKokku = PantKokku - pant;
					else PantKokku = PantKokku + pant;


					//document.ostukorv['PantKokku'].value = PantKokku;

					var hind = makeNice(PantKokku + summa);
					
					//var hind_r = hind.substring(5,4);
					//alert(hind_r);
					/*if (hind_r < 5 && hind_r > 0) {
						var hind_r2 = 5 - hind_r;
						hind = (hind + hind_r2 / 100);
						//alert(hind_r2);
					}
					else if (hind_r > 5) {
						hind_r2 = 10 - hind_r;
						hind = (hind + hind_r2 / 100);
						//alert(hind_r2);
					}*/

					document.ostukorv['hind_kokku'].value = makeNice(hind);
					document.ostukorv['hind_eur'].value = makeNice(hind / 15,64);
			 }
         }

function ettemaks1() {
	if (document.ostukorv.ettemaks.checked == true) { 
		document.ostukorv['hind_kokku'].value = makeNice(PriceAllPages - (PriceAllPages * 0.005));
		var kala = document.ostukorv['hind_kokku'].value;
		document.ostukorv['hind_eur'].value = makeNice(kala / 15,64);
	}
	else {
		document.ostukorv['hind_kokku'].value = makeNice(PriceAllPages);
		var kala = document.ostukorv['hind_kokku'].value;
		document.ostukorv['hind_eur'].value = makeNice(kala / 15,64);
	}
}
function makeNice(sum) {
	out = Math.round(sum * 100) / 100;
	if (out == Math.round(out)) {
		return out + '.00';
	}
	else if (out == Math.round(out * 10) / 10) {
		return out + '0';
	}
	else {
		return out;
	}
}

function submit_form() {
	if (document.ostukorv['hind_kokku'].value < 500)
	{
		alert("Ostusumma peab olema suurem kui 500.00.");
	}
	else
	{
		document.ostukorv.submit();
	}
}


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;
}
