// SHOW & HIDE \\
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")
			show(id);
		else
			hide(id);
	}
}

// BOX \\
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;
		return true;
	} else {
		return false;
	}
}

function closeBox(id) {	
	boxId = id+'Box';
	box = document.getElementById(boxId);	
	
	if(box) {
		box.parentNode.removeChild(box);
		return true;
	} else {
		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 getHtml(id) {
	var obj = document.getElementById(id);
	
	if(obj)
		return obj.innerHTML;
	else
		return false;
}

// 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 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("&"));		
}*/

function ajaxPost(id, uri, values, callback, xml) {
	var async = true;
	
	if(!callback || callback == 'undefined')
		async = false;
		
	
	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) {
					if(xml)											
						callback(id, oXmlHttp.responseXML);					
					else
						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) {
		if(xml)
			return oXmlHttp.responseXML;		
		else
			return oXmlHttp.responseText;
	}
}

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;
}
