var actSite, logoutLink, loginForm;
var loadingImg = "<div style=\"text-align:center;margin-top:15px;margin-bottom:15px;\"><img src=\"/eltester/images/loadAn.gif\" style=\"width:50px;height:50px;\" /></div>";

function init() {
	if (document.getElementsByTagName("form") && document.getElementsByTagName("form")[0]) {
		loginForm = document.getElementsByTagName("form")[0];
		loginForm.addEventListener("submit", login, false);
		loginForm.action = "javascript:void(0)";
	} else if (document.getElementById("logoutLink")) {
		logoutLink = document.getElementById("logoutLink");
		if (actSite && actSite.length > 0) {
		} else {
			/lastsite=(.*)$/.exec(logoutLink.href)
			actSite = RegExp.$1;
		}
		logoutLink.href = "javascript:void(0)";
		logoutLink.addEventListener("click", logout, false);
	}
}

function login() {
	var loginname, password, site, inputs, getString, postString, ajax;
	inputs = loginForm.getElementsByTagName("input");
	loginname = inputs[0].value;
	password = inputs[1].value;
	actSite = inputs[2].value;
	
	var getString = "/eltester/login_ajax.php";
	var postString = "loginname=" + encodeURIComponent(loginname) + "&password=" + encodeURIComponent(password);
	
	ajax = new AjaxRequest();
	ajax.set_callback_function(getMenu);
	ajax.POST_request(getString, postString);
	document.getElementById("login").innerHTML = loadingImg;
}

function getMenu(response) {
	var getString = "/eltester/menu_ajax.php";
	var postString = "site=" + encodeURIComponent(actSite) + "&error=" + encodeURIComponent(response);
	
	ajax = new AjaxRequest();
	ajax.set_callback_function(changeMenu);
	ajax.POST_request(getString, postString);
}

function changeMenu(response) {
	document.getElementById("navi").innerHTML = response;
	init();
}

function logout() {
	var getString = "/eltester/logout_ajax.php";
	
	var ajax = new AjaxRequest();
	ajax.set_callback_function(getMenuAfterLogout);
	ajax.GET_request(getString);
	document.getElementById("internMenu").innerHTML = loadingImg;
}

function getMenuAfterLogout(response) {
	response = parseInt(response);
	if (response == 0) {
		var getString = "/eltester/menu_ajax.php";
		var postString = "site=" + encodeURIComponent(actSite);
		
		ajax = new AjaxRequest();
		ajax.set_callback_function(changeMenu);
		ajax.POST_request(getString, postString);
	} else {
		window.location.reload();
	}
}

//########################################
//########################################

function AjaxRequest() {
	this.ajax = false;
	this.ajax_response = "";
	this.write_to_element = false;
	this.output_element = null;
	this.callback = false;
	this.callback_function = null;
	
	if (window.ActiveXObject) {
		try {
			this.ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { }
		}
	} else {
		this.ajax = new XMLHttpRequest();
	}
	
	
	this.set_output_element = function (element) {
		this.write_to_element = true;
		this.output_element = element;
	}
	
	this.set_callback_function = function (callback_function) {
		this.callback = true;
		this.callback_function = callback_function;
	}
	
	this.GET_request = function(url) {
		if(!this.ajax)
			return false;
		this.ajax.ajaxreq = this;
		this.ajax.onreadystatechange = function () {
			if(this.readyState == 4) {
				if(this.status == 200) {
					this.ajaxreq.handle_response(this.responseText);
					return true;
				} else {
					return false;
				}
			} else {
			return false;
			}
		};
		this.ajax.open('GET', url, true);
		this.ajax.send(null);
	}
	
	this.POST_request = function(url, postdata) {
		if(!this.ajax)
			return false;
		this.ajax.ajaxreq = this;
		
		this.ajax.onreadystatechange = function () {
			if(this.readyState == 4) {
				if(this.status == 200) {
					this.ajaxreq.handle_response(this.responseText);
					return true;
				} else {
					return false;
				}
			} else {
			return false;
			}
		};
		
		this.ajax.open('POST', url, true);
		this.ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this.ajax.send(postdata);
	}	
	
	this.handle_response = function(response) {
		if (this.write_to_element)
			this.output_element.innerHTML = response;
		else if (this.callback)
			this.callback_function(response);
		else
 			this.ajax_response = response;
	}
	
	
	this.get_response = function() {
		ajax_response = this.ajax_response;
		return ajax_response;
	}
}

init();
