//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+		Scripts de base du sit unl-sas.fr										+
//+		Copyright I-Digitale 2007												+
//+		Tout droits de reproduction interdits									+
//+		http://www.idigitale.fr 												+
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


function ouvre_image(img,titre,ROOT_PATH){
	w = open('','image','status=no,toolbar=no,location=no,directories=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,top=50,left=50'); 
	var page = "<html><head>";
	page += "<title>"+titre+"</title>";
	if(!ROOT_PATH) {
		page += "<script language='JavaScript' src='../scripts/unl.js'></script></head>"; 		
	} else {
		page += "<script language='JavaScript' src='"+ROOT_PATH+"scripts/unl.js'></script></head>"; 
	}
	page += "<body onload='checksize();' onclick='window.close()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0  style='text-align:center;'>";
	page += "<img src='"+img+"' alt='Image non disp' title='Cliquez pour fermer' style='display:block;margin:10px auto;'>";
	page += "</body></html>"; 
	w.document.write(page);
	w.document.close();
	w.window.focus();
}

function checksize(){
	if (document.images[0].complete){
		window.resizeTo(document.images[0].width+30,document.images[0].height+70);
		window.focus();
	}else{
		setTimeout('checksize()',250);
	}
}


function ouvre_popup(page,width,height,titre) {
   	if(!width) {var width=600;}
   	if(!height) {var height=800;}
    if(!titre) {var titre='popup_unl';}else{var reg = /\s/g; titre=titre.replace(reg,'_');}
 	var w = window.open(page,titre,'status=no,toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',top=50,left=50');
   	w.window.focus();
}


function GereChkbox(form,statut){ 
   var taille = document.forms[form].elements.length; 
   var element = null; 
   for(i=0; i < taille; i++){ 
      element = document.forms[form].elements[i]; 
      if(element.type == "checkbox") 
         element.checked = statut; 
   }       
}

String.prototype.trim = function()
{
    return this.replace(/(?:^\s+|\s+$)/g, "");
}

var globalRegEx = new RegExp("([^0-9.,])+", "g");
var globalRegEx_entier = new RegExp("([^0-9])+", "g");
function verifier_num(input, entier)	{
	if(entier){
		input.value = input.value.replace(globalRegEx_entier,"");
	}else{
		input.value = input.value.replace(globalRegEx,"");
	}
}


Array.prototype.Find = function( value ) {
    for(count = 0; count<this.length; count++) {
        if( this[count] == value ) return count;
    }
    return -1;
};

Array.prototype.remove = function( index ) {
	var tmp = new Array();
	for( count = 0; count < this.length; count++ ) {
		if( index != count ) {
			tmp[tmp.length]=this[count];
		}
	}
	return tmp;
};


// Remplace toutes les occurences d'une chaine
function replaceAll(str, search, repl) {
	while (str.indexOf(search) != -1)
	str = str.replace(search, repl);
	return str;
}

// Remplace les caractères accentués
function NoAccent(str) {
	var norm = new Array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï', 'Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß', 'à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ', 'ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ý','þ','ÿ');
	var spec = new Array('A','A','A','A','A','A','A','C','E','E','E','E','I','I','I','I', 'D','N','O','O','O','0','O','O','U','U','U','U','Y','b','s', 'a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','d','n', 'o','o','o','o','o','o','u','u','u','u','y','y','b','y');
	for (var i = 0; i < spec.length; i++)
	str = replaceAll(str, norm[i], spec[i]);
	return str;
}

//-----	Script de pour mettre en avant un texte
function highlightWord(node,word) {
	
	nb_find=0;
		
	// Iterate into this nodes childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		//for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
		for(var hi_cn=0, len=node.childNodes.length; hi_cn<len; hi_cn++){
			nb_find +=highlightWord(node.childNodes[hi_cn],word);
		}
	}
	
	// And do this node itself
	if (node.nodeType == 3) { // text node
		tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		
		tempNodeVal=NoAccent(tempNodeVal);
		tempWordVal=NoAccent(tempWordVal);
		
		if (tempNodeVal.indexOf(tempWordVal) != -1) {
			pn = node.parentNode;
			// check if we're inside a "nosearchhi" zone
			checkn = pn;
			while (checkn.nodeType != 9 && checkn.nodeName.toLowerCase() != 'body') { 
			// 9 = top of doc
				if (checkn.className.match(/\bnosearchhi\b/)) { return; }
				checkn = checkn.parentNode;
			}
			if (pn.className != "searchword") {
				// word has not already been highlighted!
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
				// Create a load of replacement nodes
				tx_before=nv.substr(0,ni);
				before = document.createTextNode(tx_before);
				
				docWordVal = nv.substr(ni,word.length);
				
				tx_after=nv.substr(ni+word.length)
				after = document.createTextNode(tx_after);
				
				hiwordtext = document.createTextNode(docWordVal);
				hiword = document.createElement("span");
				hiword.className = "searchword";
				hiword.appendChild(hiwordtext);
				
				if(tx_before!=''){pn.insertBefore(before,node);}
				pn.insertBefore(hiword,node);
				if(tx_after!=''){pn.insertBefore(after,node);}
				
				pn.removeChild(node);
				//nb_find++;
			}
			nb_find++;
		}
	}
	return nb_find;
}

function unhighlight(node) {
	// Iterate into this nodes childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
		//for(var hi_cn=0, len=node.childNodes.length; hi_cn<len; hi_cn++){
			unhighlight(node.childNodes[hi_cn]);
		}
	}

	// And do this node itself
	if (node.nodeType == 3) { // text node
		pn = node.parentNode;
		if( pn.className == "searchword" ) {
			prevSib = pn.previousSibling;
			nextSib = pn.nextSibling;
			
			temp='';
			if(node.nodeValue){
				temp=node.nodeValue;
			}
			
			if(prevSib){
				if(prevSib.nodeValue){
					temp=prevSib.nodeValue+temp;
				}
				Element.remove(prevSib);
			}
			if(nextSib){
				if(nextSib.nodeValue){
					temp=temp+ nextSib.nodeValue;
				}
				Element.remove(nextSib);
			}
			
			new Insertion.Before(pn, temp);
			
			Element.remove(pn);
		}
		
	}
	return true;
}

