/**
 @public
 @brief restituisce un array associativo dei parametri in GET della pagina corrente
 @return l'array associativo dei parametri
*/
function ParseGET() {
	var arrPars = new Array();
	if (document.baseURI != undefined) {
		var queryString = document.baseURI.split('?').pop();
	} else if (document.URL != undefined) {
		var queryString = document.URL.split('?').pop();
	}
	if (queryString != undefined && queryString != '') {
		queryString = queryString.split('&');
		for (var i in queryString) {
			var par = queryString[i].split('=');
			arrPars[par[0]] = par[1];
		}
	}
	return arrPars;
}


/**
@public
@brief restituisce il nome del dominio
@return il nome del dominio di livello pił alto
*/
function GetTopLevelDomain() {
	var regexpDomain = /\w+\..{2,3}$/;
	return regexpDomain.exec(document.domain);
}


/**
@public
@brief La comune funzione Trim. Toglie spazi, tab, e invii
*/
function Trim(txt) {
	txt = txt.replace(/^[\s\t\n\r]+/g, '');
	txt = txt.replace(/[\s\t\n\r]+$/g, '');
	return txt;
}


var Onload = new Array();
/**
@public
@brief aggiunge una funzione nella coda OnLoad della pagina
*/
function AddOnload (func) {
	Onload.push(func);
}


/**
@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
*/
var debugSearchChilds = new Array();
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();
	debugSearchChilds[chiaveDebug]['time'] += parseInt(endTime-beginTime);

	var txt='';
	for(var ii in debugSearchChilds){
		var key = ii.split('-');
		if (key[0] != undefined && key[1] != undefined && key[2] != undefined &&  debugSearchChilds[ii]['time']!= undefined && debugSearchChilds[ii]['cnt'] != undefined ) {
			txt+='<'+key[0]+' '+key[1]+ '="' + key[2]+'"> Tempo ricerca: '+parseInt(debugSearchChilds[ii]['time'])+" #"+debugSearchChilds[ii]['cnt']+" elem\n";
		};

	};
	//addJSDebug(txt, 'timeSearchChilds');
	return r;
}


function performSearchChilds (tagFather, nomeTag, nomeAttributo, valoreAttributo, retrieveCollection) {
	var chiaveDebug = nomeTag + "-" + nomeAttributo + "-" + valoreAttributo;
	if (debugSearchChilds[chiaveDebug] != undefined) {
		debugSearchChilds[chiaveDebug]['cnt']++;
		// debugSearchChilds[chiaveDebug]['time'] += 2;
	} else {
		debugSearchChilds[chiaveDebug] = new Array();
		debugSearchChilds[chiaveDebug]['cnt'] = 1;
		debugSearchChilds[chiaveDebug]['time'] = 0;
	}
	var collection = Array();
	nomeTag = nomeTag.toLowerCase();
	for (var i=0; i < tagFather.childNodes.length; i++) {
		var elem = tagFather.childNodes[i];

		if (elem.tagName != undefined && (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' && elem.className == valoreAttributo)) {
					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) {
							collection.push(b[q1]);
						};
					}
				};
			};
		};
		a = undefined;
	}
	if (collection.length > 0) {
		//var qq = '';
		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;
}

