// DISPLAY
function show(id, style) {
	var obj = document.getElementById(id);
	
	if(!style)
		style = "";
	
	if(obj)
		obj.style.display=style;
}
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")
			show(id);
		else
			hide(id);
	}
}

// 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 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);
}

// AJAX
var oXmlHttp = null;

function ajaxGet(id, uri, callback) {
	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 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 ajaxPost(id, uri, values, callback) {
	if(!oXmlHttp)		
		oXmlHttp = zXmlHttp.createRequest();
	else if(oXmlHttp.readyState != 0)
		oXmlHttp.abort();
		
	oXmlHttp.open("post", uri, true);		
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	
	oXmlHttp.onreadystatechange = function() {			
		if(oXmlHttp.readyState == 4) {
			if(oXmlHttp.status == 200) {
				callback(id, oXmlHttp.responseText);					
			} else {
				alert("impossibile contattare il server");
			}
		}
	}	
	
	var query = new Array ();
	for (var name in values) {
		query.push(name+"="+encodeURIComponent(values[name]));
	};
		
	oXmlHttp.send(query.join("&"));		
}