var tab = 'tab1';

function changeTab(x) {
	document.getElementById(tab).className = 'tab_not';
	document.getElementById(x).className = 'tab_select';
	document.getElementById(tab + "_form").style.display = 'none';
	document.getElementById(x + "_form").style.display = 'block';
	tab = x;	
}

function openNav(postFunction) {
	thisnav = document.getElementById('scheda');
	thisnav.style.display = 'block';
	get_to = thisnav.offsetWidth;
		if (get_to > 0) {
		thisnav.style.width = "1px";
		// This is going to be a setInterval but for now it's not: 
	document.getElementById(tab).className = 'tab_select';
	document.getElementById(tab + '_form').style.display = 'block';
	expandNav(postFunction);
		}
}

function expandNav(postFunction) { 
	thisnav = document.getElementById('scheda');
	if (thisnav.offsetWidth < get_to) { 
		thisnav.style.width = (thisnav.offsetWidth + 30) + "px";
		scrollTimeoutID = setTimeout("expandNav(\"" + postFunction + "\")",10);	
	} else {
		setTimeout(postFunction, 10);	
	}
}

function closeNav() { 
	thisnav = document.getElementById('scheda');	
	if (thisnav.offsetWidth > 30) { 
		thisnav.style.width = (thisnav.offsetWidth - 30) + "px";
		scrollTimeoutID = setTimeout("closeNav()",10);	
	} else if (thisnav.offsetWidth <= 30) {
	thisnav.style.display = 'none';
	thisnav.style.width = 520 + "px";
	}
}

function strReplace(str,from,to) {
	while(str.indexOf(from)!=-1) {
		str = str.replace(from, to);
	}
	return str;
}

function showStatusMessage(message, type, autoclose) {
	thisnav = document.getElementById("statusMessage");
	switch(type) {
		case "INFO":
			background = "#FFF8BC";
			textcolor = "#000000";
			break;
		case "ERROR":
			background = "#D00000";
			textcolor = "#FFFFFF";
			break;
	}
	thisnav.style.background = background;
	thisnav.style.color = textcolor;
	
	thisnav.innerHTML = "<div style='padding: 5px 10px'>" + message + "</div>";
	openStatusMessage();
}

function openStatusMessage() {
	thisnav = document.getElementById("statusMessage");
	if (thisnav.offsetHeight < 30) { 
		thisnav.style.height = (thisnav.offsetHeight + 5) + "px";
		scrollTimeoutID = setTimeout("openStatusMessage()",10);
	} else {
		scrollTimeoutID = setTimeout("closeStatusMessage()",3000);
	}
}

function closeStatusMessage(value) { 
	thisnav = document.getElementById("statusMessage");
	if (thisnav.offsetHeight > 0) { 
		thisnav.style.height = (thisnav.offsetHeight - 5) + "px";
		scrollTimeoutID = setTimeout("closeStatusMessage()",10);	
	}
}

var loginType;
var messageLogin;
var loginRedir;

function loginWindowOn(type, redir){
	loginType = type;
	loginRedir = redir;
	switch (type) {
		case "LOCK":
			clearSession();
			messageLogin = "<b style=\"line-height:15px;font-size:10px;color:#000000\">Applicativo bloccato:</b>";
			break;

		case "REFRESHSESSION":
			messageLogin = "<b style=\"line-height:15px;font-size:10px;color:#ff0000\">La sessione �scaduta:</b>";
			break;

		case "LOGIN":
			messageLogin = "<b style=\"line-height:15px;font-size:10px;color:#009348\">Area amministrazione Virtualgeo</b>";
			break;
	}

	
	if (type == "LOCK") {
		document.getElementById("pellikola").innerHTML = "";
		document.getElementById("pellikola").style.background = "#009348";
	} else {

		if(!document.all && document.getElementById){
			document.getElementById("pellikola").innerHTML = "";
			document.getElementById("pellikola").style.background = "url('/img/pellicola_old.png')";
		}
	}
	
	document.getElementById("pellikola").style.display = "block";
	document.getElementById("pellikola_box").style.display = "block";
	document.getElementById("messageLogin").innerHTML = messageLogin;
	document.getElementById("infame").style.display = "block";
	document.getElementById("infame").style.filter= "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";

}

function loginWindowOff(){
	document.getElementById("pellikola").style.display = "";
	document.getElementById("pellikola_box").style.display = "";
	document.getElementById("infame").style.display = "";
}
	
function processQueue(queueStr, alternateFunction) {
	var jobs = queueStr.split("\n");
	var c = 0;
	for (c=0; c<=jobs.length-1; c++) {
		var thisJob = jobs[c].replace("\r", "").split("|");
		switch(thisJob[0]) {
			case "ALERT":
  				alert(thisJob[1]);
  				break;
			case "EXECUTE":
  				eval(thisJob[1]);
  				break;	
  			case "SET":
  				var tmpValue = strReplace(thisJob[2], "<newline>", "\n");
  				DWRUtil.setValue(thisJob[1], tmpValue);
  				break;
  			case "SETHTML":
  				var thisId = document.getElementById(thisJob[1]);
  				thisId.innerHTML = thisJob[2];
  				break;
			default:
				if (alternateFunction != "") {
					eval(alternateFunction.replace("()", "(jobs[c])"));
				}
		}
	}
}

function loginOK() {
	if (loginRedir != '') {
		location.href = loginRedir;
		document.getElementById('login_username').value = '';
		document.getElementById('login_password').value = '';
	}
	if (loginType == "LOGIN") {
		location.href = "works.asp";
		document.getElementById('login_username').value = '';
		document.getElementById('login_password').value = '';
	} else {
		loginWindowOff();
		document.getElementById('login_username').value = '';
		document.getElementById('login_password').value = '';
	}
}

function loginKO() {
	showStatusMessage("Username/Password errati! La preghiamo di riprovare.", "ERROR");
	document.getElementById("login_username").focus();
	document.getElementById("login_username").select();
}

function verifyLogin(){
	var ajax = new sack();
	function whenCompleted(){
		processQueue(ajax.response, "workAlternate()");
	}
	user = DWRUtil.getValue("login_username");
	pswd = DWRUtil.getValue("login_password");
	ajax.setVar("action", "LOGIN");
	ajax.setVar("u", user);
	ajax.setVar("p", pswd);
	ajax.requestFile = "/admin/ws-authenticate.asp";
	ajax.method = "POST";
	ajax.onLoading = whenLoading;
	ajax.onLoaded = whenLoaded; 
	ajax.onInteractive = whenInteractive;
	ajax.onCompletion = whenCompleted;
	ajax.runAJAX();
}

function clearSession(){
	var ajax = new sack();
	function whenCompleted(){
		processQueue(ajax.response, "workAlternate()");
	}
	ajax.setVar("action", "CLEARSESSION");
	ajax.requestFile = "/admin/ws-authenticate.asp";
	ajax.method = "POST";
	ajax.onLoading = whenLoading;
	ajax.onLoaded = whenLoaded; 
	ajax.onInteractive = whenInteractive;
	ajax.onCompletion = whenCompleted;
	ajax.runAJAX();
}

function clearField(f) {
	document.getElementById(f).value = '';
}