/*
	katalog_tools.js
*/

//var xmlHttp;
var veryfied=null;
var tryReload=false;
setTimeout("tryReload=true", 1320000);
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 execRequest(url){ 
	if(url==''){
		alert("No URL");
		return null;
	}
	var xmlHttp=new GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Your browser does not support AJAX!");
		return null;
	}
	if(arguments[1]){
		var formation = arguments[1];
		var obj = arguments[2];
		xmlHttp.onreadystatechange=function(){ stateChangedXML(xmlHttp,formation, obj);};
		//xmlHttp.usefunction = true;
		xmlHttp.open("GET",url,true);
	}
	else{
		xmlHttp.open("GET",url,false);
	}
	xmlHttp.send(null);
	return xmlHttp;
}

function execPost(url, post){
	if(url==''){
		alert("No URL");
		return null;
	}
	var xmlHttp=new GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Your browser does not support AJAX!");
		return null;
	}
	var poststring=post.join('&');
	if(arguments[2]){
		var formation = arguments[2];
		var obj = arguments[3];
		xmlHttp.onreadystatechange=function(){ stateChangedXML(xmlHttp,formation, obj);};
		//xmlHttp.usefunction = true;
		xmlHttp.open("POST",url,true);
	}
	else{
		xmlHttp.open("POST",url,false);
	}
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
	xmlHttp.send(poststring);
	return xmlHttp;
}

function stateChangedXML(xmlHttp, formation, obj){
	if (xmlHttp.readyState==4){
		try{
			veryfied=xmlHttp.getResponseHeader('AJAX');
		}
		catch(err){//vadinas paklydom
			veryfied=null;
		}
		if(veryfied==1){
			tryReload=true;
			if(typeof(window[formation]) == 'function'){
				window[formation](xmlHttp, obj);//.documentElement);
			}
			else{
				//alert("No function for formating response ["+formation+"]");
			}
		}
		else{
			if(tryReload){
				if(window.top){
					window.top.location.reload();
				}
				else{
					location.reload();
				}
			}
			else{
				var hcode=200;
				try{
					hcode=xmlHttp.getResponseHeader('Status');
				}
				catch(err){//vadinas paklydom
					hcode=-1;
				}
				if(hcode > 400){
					//alert(hcode+" Access forbiden!");
					if(typeof(window[formation]) == 'function'){
						var res=new Object();
						res.responseText='<div id="error_blok"><p class="tit">'+hcode+" Access forbiden!</p></div>";
						window[formation](res, obj);
					}
				}
			}
		}
	}
}

function requestAbort(xmlHttp){
	if(xmlHttp){
		if(xmlHttp.readyState < 4 && typeof(xmlHttp.onreadystatechange) == 'function'){
			xmlHttp.onreadystatechange=function(){};
		}
		xmlHttp.abort();
	}
	xmlHttp = null;
}

function getValue(obj, tag){
	return ((obj.getElementsByTagName(tag).length == 1)? obj.getElementsByTagName(tag)[0].childNodes[0].nodeValue :null);
}

var nowchange=false;
var cho = null;
var loadingimg = 'images/cat_loader5.gif';
function changeCat(t){
	if(cho){
		if(cho.o){
			requestAbort(cho.o);
		}
	}
	cho = null;
	cho = new Object;
	if(document.getElementById('gamintojas')){
		cho.gamintojas = document.getElementById('gamintojas').parentNode;
		cho.gamintojas.innerHTML = loadingImage();
	}
	if(document.getElementById('aukstis')){
		cho.aukstis = document.getElementById('aukstis').parentNode;
		cho.aukstis.innerHTML = loadingImage();
	}
	if(document.getElementById('plotis')){
		cho.plotis = document.getElementById('plotis').parentNode;
		cho.plotis.innerHTML = loadingImage();
	}
	cho.o = execRequest('?get=param&change=kat&kat='+t.value, 'setElements', cho.gamintojas);
}

function setElements(response){
	if(cho){
		var xml = response.responseXML.documentElement;
		if(cho.gamintojas){
			cho.gamintojas.innerHTML = getValue(xml, 'gamintojai');
		}
		if(cho.aukstis){
			cho.aukstis.innerHTML = getValue(xml, 'auksciai');
		}
		if(cho.plotis){
			cho.plotis.innerHTML = getValue(xml, 'plociai');
		}
	}
	cho = null;
}

function changeGam(t){
	if(cho){
		if(cho.o){
			requestAbort(cho.o);
		}
	}
	cho = null;
	cho = new Object;
	if(document.getElementById('aukstis')){
		cho.aukstis = document.getElementById('aukstis').parentNode;
		cho.aukstis.innerHTML = loadingImage();
	}
	if(document.getElementById('plotis')){
		cho.plotis = document.getElementById('plotis').parentNode;
		cho.plotis.innerHTML = loadingImage();
	}
	var prod = document.getElementById('produktas').value;
	cho.o = execRequest('?get=param&change=gam&kat='+document.getElementById('produktas').value+'&gam='+t.value, 'setElements', cho);
}

function loadingImage(){
	return '<img src="'+root_url+loadingimg+'" border="0" alt=""/>';
}

