
function getStyle (elem, property) {
	if (elem.tagName == undefined)
		return false;
	var x = (elem);
	if (x.currentStyle)
		var y = x.currentStyle[property];
	else if (window.getComputedStyle)
		var y = window.getComputedStyle(x,null).getPropertyValue(property);
	else
		y = x.style[property]

	return y;
}

/**
@public
@brief Scambia lo stile display da none a block e viceversa. Se è presente un'immagine che ha come id l'id uguale all'elemento da mostrare con il prefisso "arrow_" cambia anche questa.
$param elem oggetto o la stringa dell'id dell'elemento
*/
function showHide (elem, forzaDisplay) {

	if (typeof elem == 'string' || typeof elem == 'number') {
		elem = document.getElementById(elem);
	};
	if (elem == null || typeof(elem) != 'object') return false;


	if (forzaDisplay != undefined && forzaDisplay == 'block' || forzaDisplay == 'none') {
		// forzatura dell'apertura
		elem.style.display = forzaDisplay;
	} else {
		// se non viene forzato, guarda lo stile applicato all'elemento da visualizzare / nascondere
		elem.style.display = getStyle(elem, 'display') == 'none' ? 'block' : 'none';
	};

	return true;
}


function checkInteger (elem) {
	elem.value = elem.value.replace(/\D/, '');
}


/**
@public
@brief Cerca un nodo in base ad un attributo del nodo stesso
@param tagFather nodo padre da cui partire
@param nomeTag nome del tag da cercare
@param nomeAttributo nome dell'attributo del nodo da cercare
@param valoreAttributo valore della proprietà nomeAttributo del nodo da cercare
@return il nodo oppure @c false se non lo trova

Funzione ricursiva che cerca in tutti i figli di tutti i nodi figli del padre passato come parametro. E' possibile non passare nomeAttributo e valoreAttributo: in questo caso non vengono fatti i confronti con l'attributo
*/
function searchChild (tagFather, nomeTag, nomeAttributo, valoreAttributo, retrieveCollection) {
	var chiaveDebug = nomeTag + "-" + nomeAttributo + "-" + valoreAttributo;
	var dataCorrente1 = new Date();
	var beginTime = dataCorrente1.getTime();
	var r = performSearchChilds (tagFather, nomeTag, nomeAttributo, valoreAttributo, retrieveCollection);
	var dataCorrente2 = new Date();
	var endTime = dataCorrente2.getTime();

	return r;
}
function performSearchChilds (tagFather, nomeTag, nomeAttributo, valoreAttributo, retrieveCollection) {
	var collection = Array();
	nomeTag = nomeTag.toLowerCase();
	for (var i=0; i < tagFather.childNodes.length; i++) {
		var elem = tagFather.childNodes[i];

		if (elem.tagName == undefined) {
			continue;
		};

		if (elem.tagName.toLowerCase() == nomeTag.toLowerCase() || nomeTag == '*') {
			if (nomeAttributo == undefined && valoreAttributo == undefined) {
				a = elem;
			} else {
				if (elem.tagName.toLowerCase() == 'label' && nomeAttributo.toLowerCase() == 'for' && (valoreAttributo == undefined || (elem.getAttribute('htmlFor') == valoreAttributo ^ elem.getAttribute('for') == valoreAttributo))) { // in InternetExplorer non esiste elem.getAttribute('for')
						var a = elem;
				} else if (valoreAttributo == undefined || nomeAttributo.toLowerCase() == 'class') {
					var re = '(^|\\s)'+valoreAttributo+'(\\s|$)';
					if (elem.className.match(new RegExp(re)))
						var a = elem;

				} else if (valoreAttributo == undefined || (nomeAttributo.toLowerCase() == 'checked' && elem.checked != undefined && elem.checked == valoreAttributo)) {
					var a = elem;
				} else if (valoreAttributo == undefined || (elem.getAttribute(nomeAttributo) == valoreAttributo ) ) {
					var a = elem;
				} else {
					if (elem.getAttribute(nomeAttributo) != undefined && isRegExpString(valoreAttributo)) {
						var re = valoreAttributo.replace(/\/$/, '').replace(/^\//,'');
						if (elem.getAttribute(nomeAttributo) != undefined && elem.getAttribute(nomeAttributo).search(new RegExp(re)) != -1) {
							var a = elem;
						}
					};
				};
			}
			if (retrieveCollection != true) {
				if (a != undefined) return a;
			} else {
				if (a != undefined) collection.push(a);
			};
		};

		if (elem.childNodes != undefined && elem.childNodes.length > 0)
			if (elem.childNodes.length > 0) {
				var b = performSearchChilds(elem, nomeTag, nomeAttributo, valoreAttributo, true);
				if (b != false)
					if (retrieveCollection != true)
						return b.pop();
					else if (b.length > 0)
						for (var q1 in b)
							if (typeof b[q1] == 'object')
								collection.push(b[q1]);
			};
		//
		a = undefined;
	}
	if (collection.length > 0) {
		var ww = Array();
		for (var q in collection)
			ww[q] = collection[q];
		return ww;
	}
	return false;
}

/**
@public
@brief restituisce una collection di nodi che hanno i parametri specificati
@see searchChild
@return un array con la collection dei nodi oppure @c false se non viene trovato nessun nodo
*/
function searchChildsCollection (tagFather, nomeTag, nomeAttributo, valoreAttributo) {
	var ee = searchChild(tagFather, nomeTag, nomeAttributo, valoreAttributo, true);
	return ee;
}


// questa funzione serve per selezionare una option di una select. Primo parametro: il tag <select>; Secondo parametro: il value dell'<option>
function selectOption (selectTag, value) {
	for (var i=0; i < selectTag.options.length; i++) {
		if (selectTag.options[i].value == value || selectTag.options[i].innerHTML == value) {
			selectTag.options[i].selected = true;
			return true;
		}
	}
	return false;
}

function selectRadio (radioTag, value) {
	for (var i=0; i < radioTag.length; i++) {
		if (radioTag[i].value == value) {
			radioTag[i].checked = true;
			return true;
		}
	}
	return false;
}

function getRadioChecked (radioElemCollection) {
	if (radioElemCollection == undefined)
		return false;
	var totElem = radioElemCollection.length;
	for (var i =0; i < totElem; i++)
		if (radioElemCollection[i].checked)
			return radioElemCollection[i];

	return false;
}



/**
@public
@brief Aggiunge la funzione passata come parametro a window.onload
@param func La funziona da aggiungere. Se viene passata una funzione la esegue senza parametri, altrimenti se è una stringa viene fatto l'eval
@param priority Priorità con le quali vengono eseguite le funzioni.
*/
var oldOnload = new Array(new Array(), new Array(), new Array());
function addOnload (func, priority) {
	switch (priority) {
		case 'high':
			priority = 2;
			break;
		case 'low':
			priority = 0;
			break;
		default:
			priority = 1;
	};


	oldOnload[priority].push(func);
}
window.onload = function () {
	for (var pri=2; pri >= 0; pri--) {
		var arrToPerform = oldOnload[pri];
		if (arrToPerform.length > 0)
			for (var i in arrToPerform) {
				switch (typeof arrToPerform[i]) {
					case 'function':
						arrToPerform[i]();
						break;
					case 'string':
						eval(arrToPerform[i]);
						break;
					default:
						alert('error onload: ' + arrToPerform[i]);
				}
			}
	}
}


// thanks to http://forums.devarticles.com/showpost.php?p=71368&postcount=2
function roundNumber(num, dec) {
	var result = Math.round(num * Math.pow(10,dec)) / Math.pow(10,dec);
	return result;
}

// {{{ FUNZIONI KABA !!
function getElemSchoolAsterisk() {
	return searchChild(document.getElementById('fieldScuola'), 'span', 'class', 'requiredField');
}

function requireSchool() {
	getElemSchoolAsterisk().innerHTML = '*';
	getElemSchoolAsterisk().isRequired = true;
}
function unrequireSchool() {
	getElemSchoolAsterisk().innerHTML = '';
	getElemSchoolAsterisk().isRequired = false;
}
// }}}


/* FUNZIONI DI SWAP DELLE IMMAGINI DI DREAMWEAVER */
 
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* FINE FUNZIONI DW */





