//AJAX Javascript functions
//Kristo Vaher - kristo@waher.net

var xmlHttp;

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}


function ajaxtest(){
	xmlHttp=GetXmlHttpObject();
	var url="ajax.php";
	if (xmlHttp){
		url=url+"?act=ajaxtest";
		url=url+"&random="+Math.random();
		document.getElementById("ajaxtest").innerHTML="<img title='loading' src='loading.gif'/>";
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4){
				document.getElementById("ajaxtest").innerHTML=xmlHttp.responseText;
			}
		};
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

//change language
function changeLanguage(newlang){
	xmlHttp=GetXmlHttpObject();
	var url="ajax.php";
	if (xmlHttp){
		url=url+"?act=changeLanguage";
		url=url+"&lang="+newlang;
		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);
	}
}

//change language
function clearStatistics(qrid){
	xmlHttp=GetXmlHttpObject();
	var url="../../ajax.php";
	if (xmlHttp){
		url=url+"?act=clearStatistics";
		url=url+"&qrid="+qrid;
		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);
	}
}

//change tab
function activeTab(tabtype,value){
	document.getElementById('currenttab').value=value;
	xmlHttp=GetXmlHttpObject();
	var url="../../ajax.php";
	if (xmlHttp){
		url=url+"?act=activeTab";
		url=url+"&tabtype="+tabtype;
		url=url+"&value="+value;
		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);
	}
}

//reset tab
function resetTab(tabtype){
	xmlHttp=GetXmlHttpObject();
	var url="../../ajax.php";
	if (xmlHttp){
		url=url+"?act=resetTab";
		url=url+"&tabtype="+tabtype;
		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);
	}
}

//search filter
function filterList(field,module){
	xmlHttp=GetXmlHttpObject();
	var searchkey = document.getElementById('filter_'+field).value;
	if (xmlHttp){
		var url = "ajax.php?act=filterList&module="+module+"&field="+field+"&searchkey="+encodeURIComponent(searchkey);
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4){
				document.getElementById('datalist').innerHTML=xmlHttp.responseText;
			}
		};
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

//authentification
function checkAuth(lang) {
	xmlHttp=GetXmlHttpObject();
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	if ((username == null) || (username == "")){ 	
		document.getElementById('pp').style.background='#990000';
		document.getElementById("pp").style.display = 'block';
		document.getElementById("pp").innerHTML = lang['insertusername'];
		return false;
	}
	if ((password == null) || (password == "")) { 	
		document.getElementById('pp').style.background='#990000';
		document.getElementById("pp").style.display = 'block';
		document.getElementById("pp").innerHTML = lang['insertpassword'];
		return false;
	}
	var url = "ajax.php?act=login&username="+escape(username)+"&password="+escape(password)+"&lang="+lang;
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = getReply;
	xmlHttp.send(null);
}

function getReply() {
  if (xmlHttp.readyState == 4) {
    var response = xmlHttp.responseText;
	eval(response);
	upd1();
  }
}

function upd() {
	document.location.href = 'index.php';
}

function upd1() {
	document.getElementById('pp').style.display = 'block';
	if(document.getElementById('auth').value == 1) {
		document.getElementById('pp').style.background='#35d200';
		document.getElementById('pp').innerHTML = lang['successfulauthorizing'];
		id = setTimeout('upd()', 2000);
	} else {
		document.getElementById('pp').style.background='#990000';
	    document.getElementById('pp').innerHTML = lang['authorizingfailed'];
	}
}

function copyQuestion(qr,q){
	xmlHttp=GetXmlHttpObject();
	var url="../../ajax.php";
	if (xmlHttp){
		url=url+"?act=copyQuestion";
		url=url+"&qr="+qr;
		url=url+"&q="+q;
		url=url+"&random="+Math.random();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4){
				window.opener.location.href=window.opener.location.href;
				window.close();
			}
		};
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

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