// verif format email : alphanum@alphanum.abc
//
function verif_email(email) {
	var ER = '^[0-9A-Za-z][0-9A-Za-z\-_\.]*@[0-9A-Za-z][0-9A-Za-z\-_\.]*\.[A-Za-z]{2,3}$';
	return email.match(ER);
}


// -------------------------------------------------------------------------------- Fonctions DOM ---------

// retourne un tableau des valeurs de chaque balise 'tag' dans un document 'docXML'
//
function GEBTN_values(docXML, tag) {
	var ret = new Array();
	var len = docXML.getElementsByTagName(tag).length;
	var it;
	
	for (i=0; i<len; i++) {
		it = docXML.getElementsByTagName(tag).item(i);
		if (it.hasChildNodes()) {
			if (it.firstChild.nodeType==3)
				ret[i] = it.firstChild.nodeValue; 
			else if (it.firstChild.nodeType==4)
				ret[i] = it.firstChild.data; 
		}
		else ret[i] = '';
	}

	return ret;
}

// retourne un tableau indexé des valeurs des filles d'une mère 'element'
// nombalise => valeur etc
function getChildNodesValues(element) {
	ret = new Array();
	
	len = element.childNodes.length;
	
	for (i=0; i<len; i++) {
		it = element.childNodes[i];
		if (it.nodeType == 1) {
			nomBalise = it.nodeName;
			if (it.hasChildNodes()) {
				if (it.firstChild.nodeType==3)
					retIt = it.firstChild.nodeValue; 
				else if (it.firstChild.nodeType==4)
					retIt = it.firstChild.data;
			}
			else retIt = '';
			ret[nomBalise] = retIt;
		}
	}

	return ret;
}

// retourne un tableau des elements enfants de "element"
//
function getHTMLChildNodes(element) {
	nodeList = element.ChildNodes;
	htmlNodeList = new Array();
	for (i=0; i<nodeList.length; i++) {
		if (nodeList[i].nodeType == 1) htmlNodeList.push(nodeList[i]);
	}
	return htmlNodeList;
}


// child est-il un enfant de parent ?
//
function mydom_isChildOf(child, parent) {
	for(var isChildOfTheLord = false, pere = child; pere!=null; pere = pere.parentNode)
		if (pere==parent) {
			isChildOfTheLord = true;
			break;
		}
	return isChildOfTheLord;
}

// selectionne l'option de value 'value' dans selectId
//
function selectOption(value, selectId) {
	if (optArg(arguments, 2)==true)
		selectN = selectId;
	else
		selectN = document.getElementById(selectId);
		
	optionsNL = selectN.getElementsByTagName('option');
	nb = optionsNL.length;
	
	//alert('1) '+optionsNL[2].getAttribute('value'));
	//alert('2) '+value);
	
	for (itsok = false, i=0; i<nb; i++) {
		if (optionsNL[i].getAttribute('selected') != null) {
			optionsNL[i].removeAttribute('selected');
		}
		if (!itsok && optionsNL[i].getAttribute('value')==value) {
			optionsNL[i].setAttribute('selected', 'selected');
			selectN.selectedIndex = i; 
			itsok = true;
		}
	}
}

// MAJ <select>
//

function MAJselect(cible, docXML, defaut) {
	defaut = optArg(arguments, 2, false); 

	nb = docXML.getElementsByTagName('item').length;
	
	deleteChildNodes(cible);
	
	if (defaut) {
		optionN = newElement('option', defaut[1], cible, document);
		optionN.setAttribute('value', defaut[0]);
	}
	
	for (i = 0; i < nb; i++) {
		itemN = docXML.getElementsByTagName('item')[i]; 
		nameN = itemN.getElementsByTagName('name')[0];
		idN = itemN.getElementsByTagName('value')[0];
	
		id = idN.hasChildNodes() ? idN.firstChild.nodeValue : '';
		name = nameN.hasChildNodes() ? nameN.firstChild.nodeValue : '';
		
		optionN = newElement('option', name, cible, document);
		optionN.setAttribute('value', id);
	}

}

// supprime le contenu d'un noeud
//
function deleteChildNodes(parentN) {
	while (parentN.hasChildNodes()) {
		parentN.removeChild(parentN.firstChild);
	}
}

// crée un noeud dans conteneurN
//
function newElement(balise, texte, conteneurN, docXML) {
	newel = docXML.createElement(balise);
	newtxt = docXML.createTextNode(texte);
	newel.appendChild(newtxt);
	conteneurN.appendChild(newel);
	return conteneurN.lastChild;
}


// retourne le prochain element balise
//
function getNextNode(n) {
	var nodeName = (arguments.length > 1) ? arguments[1].toLowerCase() : '';
	var x = n;
	
	if (!nodeName) {
		do {
			x = x.nextSibling;
		} while (x != null && x.nodeType != 1);
	}	
	else {
		do {
			x = x.nextSibling;
		} while ((x != null) && (x.nodeName) && (x.nodeName.toLowerCase() != nodeName));
	}
	
	return x;
}

// retourne un argument optionnel d'une fonction
//
function optArg(argtab, num) {
	var def = arguments[2] ? arguments[2] : '';
	if(argtab.length > num) return argtab[num];
	else return def;
}


// -------------------------------------------------------------------------------- Fonctions Window ---------



// Change l'ancre de la page, ENFIN BON ya window.hash
window.document.changeAnchor = function(anchor) {
/*	if (window.location.href.match(new RegExp('#.*$'))) { 
		window.location.href = window.location.href.replace(
			new RegExp('#.*$'),
			'#' + anchor
		);
	}
	else
		window.location.href = window.location.href + "#" + anchor;*/
	//var tt = window.document.title;
	//alert(window.document.title);
	window.location.href = '#'+anchor;
	//alert('uiy '+window.document.title);
	//window.document.title = tt;
	//return window.location;
}

window.document.getAnchor = function() {
	tab = window.location.href.split('#');
	return tab[1] ? tab[1] : '';
}

window.document.getURI = function() {
	tab = window.location.href.split('#');
	return tab[0];
}


// -- fonctions proprio

function pagination_go(id) {
	var n = parseInt($(id).redir_between.value);
	$(id).redir_between.value = !n ? 1 : n;
}
