// DISPLAY
function show(id) {
	var obj = document.getElementById(id)	
	
	if(obj)
		obj.style.display="block";
}
function hide(id) {
	var obj = document.getElementById(id)	
	
	if(obj)
		obj.style.display="none";
}
function showOrHide(id) {
	var obj = document.getElementById(id);
	
	if(obj) {
		if(obj.style.display == "none" || obj.style.display == "") {
			show(id);
			return true;
		} else {
			hide(id);
			return false;
		}
	}
	
	return false;
}

// SET&GET
function setValue(id, value) {
	var obj = document.getElementById(id);
	
	if(obj)
		obj.value = value;
}
function getValue(id) {
	var obj = document.getElementById(id);
	
	if(obj)
		return obj.value;
	else
		return false;
}

function setHtml(id, html) {
	var obj = document.getElementById(id);
	
	if(obj)
		obj.innerHTML = html;
}

function getFormValues(formId) {
	var form = document.getElementById(formId);
	
	if(form) {
		var values = new Array(form.elements.length);
		
		for(var i=0; i < form.elements.length; i++){
			values[form.elements[i].name] = form.elements[i].value;
		}	
		
		return values;
	} else
		return false;
}

function openBox(id, content, boxClass, tag) {
	obj = document.getElementById(id);
	
	if(!tag)
		tag = 'div';
	
	if(obj) {
		var boxId = id+'Box';
		var box = document.getElementById(boxId);
		
		if(!box) {
			box = document.createElement(tag);
			box.id = boxId;
			box.className = boxClass;
			
			obj.appendChild(box);
		}
		
		box.innerHTML = content;
	}
}

function closeBox(id) {	
	box = document.getElementById(id);
	
	if(box) {
		box.parentNode.removeChild(box);
		return true;
	} else {
		return false;
	}
}

// AJAX
var oXmlHttp = null;

function ajaxGet(id, uri, callback) {
	var obj = document.getElementById(id);
	
/*	if(!obj)
		return false;*/
	
	if(!oXmlHttp)		
		oXmlHttp = zXmlHttp.createRequest();
	else if(oXmlHttp.readyState != 0)
		oXmlHttp.abort();

	oXmlHttp.open("get", uri, true);	
	
	oXmlHttp.onreadystatechange = function() {			
		if(oXmlHttp.readyState == 4) {
			if(oXmlHttp.status == 200) {
				var response = oXmlHttp.responseText;	

				if(callback)
					callback(id, response);
			}
		}
	}	
	
	oXmlHttp.send(null);	
}

function ajaxPost(id, uri, values, callback, async) {
	if(async == 'undefined')
		async = true;	
		
	if(!oXmlHttp)		
		oXmlHttp = zXmlHttp.createRequest();
	else if(oXmlHttp.readyState != 0)
		oXmlHttp.abort();				
		
	oXmlHttp.open("post", uri, async);		
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	
	if(callback) {
		oXmlHttp.onreadystatechange = function() {			
			if(oXmlHttp.readyState == 4) {
				if(oXmlHttp.status == 200) {
					callback(id, oXmlHttp.responseText);					
				} else {
					alert("impossibile contattare il server: "+uri);
				}
			}
		}	
	}
	
	var query = new Array ();
	for (var name in values) {
		query.push(name+"="+encodeURIComponent(values[name]));
	};
		
	oXmlHttp.send(query.join("&"));
	
	if(!async)
		return oXmlHttp.responseText;
}

function messageBox(id, uri, className, owner, postBack) {
	if(!owner)
		var owner = document.getElementsByTagName('body')[0];

	var callback = function(id, response) {
		var box = document.getElementById(id);

		if(!box) {
			box = document.createElement('div');
			box.id = id;
			owner.appendChild(box);					
		}

		box.className = className;		
		box.innerHTML = response;	

		setBoxPosition(box, "fixed", calculateWindowY(getHeight(box), 0.50), calculateWindowX(getWidth(box), 0.5));
		
		if(postBack) {
			postBack(id);
		}
	}	

	ajaxGet(id, uri, callback);
}

function calculateWindowY(boxHeight, index){
	var height = document.documentElement.clientHeight;

	height = height*index;
	if(boxHeight) boxHeight = boxHeight/2; else boxHeight = 0;
	
	return Math.round(height - boxHeight);
}

function calculateWindowX(boxWidth, index){
	var width = document.body.clientWidth;

	width = width*index;
	if(boxWidth) boxWidth = boxWidth/2; else boxWidth = 0;

	return Math.round(width - boxWidth);
}

function setBoxPosition(obj, position, top, left){
	if(top)	obj.style.top = top + "px";
	if(left)obj.style.left = left + "px";

	obj.style.position = position;
}

function getWidth(box){
	if(box.clientWidth != 0) var width = box.clientWidth;
	else var width = box.offsetWidth;

	return width;
}

function getHeight(box){
	if(box.clientHeight != 0) var height = box.clientHeight;
	else var height = box.offsetHeight;

	return height;
}