// DISPLAY
function getWidth(box){
	if(box.clientWidth != 0) var width = box.clientWidth;
	else var width = box.offsetWidth;
//alert("width: " + width);
	return width;
}

function getHeight(box){
	if(box.clientHeight != 0) var height = box.clientHeight;
	else var height = box.offsetHeight;
//alert("height: " + height);
	return height;
}
function calculateWindowY(index){
	var height = document.documentElement.clientHeight;
//alert("w height: " + height);
	height = height*index;
//alert("height: " + height);	
	//if(boxHeight) boxHeight = boxHeight/2; else boxHeight = 0;
//alert("boxHeight: " + boxHeight);	
	//return Math.round(height - boxHeight);
	return Math.round(height);
}

function calculateWindowX(boxWidth, index){
	var width = document.body.clientWidth;
//alert("w width: " + width);
	//width = width*index;
//alert("width: " + width);
	//if(boxWidth) boxWidth = boxWidth/2; else boxWidth = 0;
//alert("boxWidth: " + boxWidth);
	return Math.round((width - boxWidth)*index);
}

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 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.style.display == "block")
		hide(id);
	else
		show(id);
}
function showAndHide(showId, hideId) {
	show(showId);
	hide(hideId);	
}

// 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 getChecked(id) {
	var obj = document.getElementById(id);
	
	if(obj)
		return obj.checked;
	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, before) {
	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;
			
			if(before) {
				var obj2 = document.getElementById(before);
				obj.insertBefore(box, obj2);
			} else
				obj.appendChild(box);
		}
		
		box.className = boxClass;		
		box.innerHTML = content;		
	}
}

function openBox2(parent, content, boxClass, tag, before) {
	if(typeof(parent) != 'object') return false;	
	if(!tag) tag = 'div';
	
	box = document.createElement(tag);
	
	if(parent.id) box.id = parent.id+'#box';
	box.className = boxClass;		
	box.innerHTML = content;	
	
	if(parent.hasChildNodes())
		for(var i=0; i < parent.childNodes.length; i++) {
			if(parent.childNodes[i].className == boxClass) {
				parent.childNodes[i].innerHTML = content;
				return parent.childNodes[i];
			}
		}

	if(before) {
		before = document.getElementById(before);
		parent.insertBefore(box, before);
	} else
		parent.appendChild(box);	
		
	return box;
}

function closeBox(id) {
	box = document.getElementById(id+'#box');	
	if(!box) box = document.getElementById(id);
	if(box) box.parentNode.removeChild(box);
}

function closeBox2(box) {
	if(typeof(box)!= 'object') return false;	
	box.parentNode.removeChild(box);	
}

// AJAX
var oXmlHttp = null;

function ajaxGet(id, uri, callback, xml) {
	var obj = document.getElementById(id);
		
	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) {
				if(callback) {
					if(xml)	{
						callback(id, oXmlHttp.responseXML);
					} else
						callback(id, oXmlHttp.responseText);							
				}
			}
		}
	}	
	
	oXmlHttp.send(null);	
}

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 validateForm(form, script, errorClass, errorTag) {
	if(typeof(form) == 'string') form = document.getElementById(form);	
	if(!form) return false;
	if(!errorTag) errorTag = 'div';
	
	var values = new Array();
	var fields = new Array();

	for(var i = 0; i < form.elements.length; i++)
		if(form.elements[i].getAttribute('required')) {
			values[form.elements[i].name] = form.elements[i].value;			
			fields[form.elements[i].name] = i;
			
			box = document.getElementById("error-"+form.elements[i].name);			
			if(box) box.parentNode.removeChild(box);
		}
	
	var result = ajaxPost(form.id, script, values, false, true);
	if(!result) return false;
	
	var response = result.getElementsByTagName("validate");
	
	if(response.length) {
		var check = response[0].getElementsByTagName("passed");
		var passed = check[0].firstChild.nodeValue;

		if(passed == '1')
			return true;
		else {
			var errors = response[0].getElementsByTagName("errors");		
			
			for(var i = 0; i < errors.item(0).childNodes.length; i++) {
				error = errors.item(0).childNodes[i]
				
				if(error.nodeType == 1) {					
					j = fields[error.nodeName];
					
					if(j) {
						box = document.createElement(errorTag);
						box.id = "error-"+error.nodeName;
						box.innerHTML = error.firstChild.nodeValue;					
						if(errorClass) box.className = errorClass;
						
						label = form.elements[j].parentNode;
						label.parentNode.insertBefore(box, label);
					}
				}
			}			
			
			return false;			
		}		
	} else
		return false;		
}

function messageBox(owner, script, className, left, top) {
	if(typeof(owner) != 'object') return false;
	
	var callback = function(id, response) {
		var box = openBox2(owner, response, className);

		if(left) box.style.left = left+'px';
		if(top) box.style.top = top+'px';		
	}
	
	var id = false;

	ajaxGet(id, script, callback);
}

function messageBox2(id, script, className, left, top, 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;	
		box.style.position = "absolute";

		if(left) box.style.left = left+'px';
		if(top) box.style.top = top+'px';		
		
		if(postback) postback(id, response);
	}	

	ajaxGet(id, script, callback);
}

function mouseX(e) {
	if(document.all)e = event;
	var vDoc=(document.documentElement && document.documentElement.scrollTop)?document.documentElement:document.body;
	var mouseX=(e.pageX)?e.pageX:e.clientX+vDoc.scrollLeft;	
	
	return mouseX;
}

function mouseY(e) {
	if(document.all)e = event;
	var vDoc=(document.documentElement && document.documentElement.scrollTop)?document.documentElement:document.body;
	var mouseY=(e.pageY)?e.pageY:e.clientY+vDoc.scrollTop;
	
	return mouseY;
}