
//Kristo Vaher
//Mikare Baltic Ltd.
//www.mikare.net

//AJAX SETUP

var xmlHttp;
function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
	
function establishAjax(){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return 0;
	}
	else{
		return 1;
	}
}
	
//AJAX FUNCTIONS

function remProd(prodID){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp && prodID){
		var url = "/kv_ajax.php?act=remProd&prodID="+prodID;
		url=url+"&random="+Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4){
				window.location.href=window.location.href;
			}
		};
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

function opengallery(imageurl){
	document.getElementById('inactive').style.display='block';
	document.getElementById('popupbox').style.display='block';
	document.body.style.overflow='hidden';
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp){
		var url="/kv_ajax.php";
		url=url+"?act=displaypic";
		url=url+"&imageurl="+imageurl;
		url=url+"&random="+Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4){
				document.getElementById("popupbox").innerHTML=xmlHttp.responseText;
			}
		};
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

function closegallery(){
	document.getElementById('inactive').style.display='none';
	document.getElementById('popupbox').style.display='none';
	document.body.style.overflow='auto';
}



function changeAmount(prod_id,amount_nr){
	var establish = establishAjax();
	if(establish == 0){
		return;
	}
	var url = "/kv_ajax.php?act=changeAm&prodKogus="+amount_nr+"&prodID="+prod_id;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);	
}

function changeProd(prodID,prodKogus) {
	var explodedstring;
	var counterint=0;
	//var oldtimessake="<BEGIN ";
	while(prodKogus[counterint]){
		setTimeout("",300);
		explodedstring = prodKogus[counterint].split(":");
		//oldtimessake=oldtimessake+"counterint:"+counterint+"  prodID:"+explodedstring[0]+"  amount_nr:"+explodedstring[1]+"END>\n";
		changeAmount(explodedstring[0],explodedstring[1]);
		counterint++;
	}
	//alert(oldtimessake);
	return;
}

//checks if element input_id, when edited, has a numerical value
function newAmount(amountval,input_id) {
	var superid;
	var tempexplode;
	var old;
	if (amountval>99){amountval=99;}
	if (isNaN(amountval)){amountval=0;}
	document.getElementById(input_id).value = amountval;
	superid = input_id.split("_");
	tempexplode = productData[superid[1]].split(":");
	if(!amountval){
		newamountval=0;
	}
	else{
		newamountval=amountval;
	}
	old=productData[superid[1]];
	productData[superid[1]]=tempexplode[0]+":"+newamountval;
}

// function remProd(prodID) {
	// var xmlHttp = false;

	// try
	// {
		// xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	// }
	// catch  
	// {
		// try
		// {
			// xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		// }
		// catch (e)
		// {
			// xmlHttp = false;
		// }
	// }
	// var url = "ajaxScript.php?act=remProd&prodID="+prodID;
	// xmlHttp.open("GET", url, true);
	// xmlHttp.send(null);	
// }

//to change something
//	if (xmlHttp.readyState==4){
//		document.getElementById("list").innerHTML=xmlHttp.responseText;
//		}

