//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//		Fonctions générales
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//==================divers====================
function getKey(e){
	var key;
	if(window.event)
		key = window.event.keyCode;     //IE
	else
		key = e.which;     		//firefox
	return key;
}

//internet explorer?
function is_ie(){
    return (navigator.appName.indexOf("Microsoft") != -1);
}

var fen = null;
function win_destroy(){
	if( (fen!=null) && (!fen.closed) ){
		fen.close();
	}
	fen=null;
}
function win_center(l,h,url,name,param){
	win_destroy();
	param = 'width='+l+',height='+h+',left='+((screen.width-l)/2)+',top='+((screen.height-h)/2)+','+param;
	fen= window.open(url,name,param);
	fen.focus();
}

function win_center_top(l,h,url,name,param){
	win_destroy();
	param = 'width='+l+',height='+h+',left='+((screen.width-l)/2)+',top=60,'+param;
	fen=window.open(url,name,param);
	fen.focus();
}

function aleatoire(N) {
  	return (Math.floor((N)*Math.random()+1));
}

//Detecte la pression sur entree
//utiliser getKey
function checkEnter(event)
{
	var key;
     	if(window.event)
          	key = window.event.keyCode;     //IE
    	else
          	key = event.which;     		//firefox

     	if(key == 13)
     		return true;
}

function isNumber(chaine)
{
	exp = new RegExp("[^0-9\.\ ]+");
        if(!exp.test(chaine))
                return true;
	return false;
}

function verif_phone(nbr)
{
	if (nbr.length >= 9 && nbr.length <= 14 && isNumber(nbr))
		return true;
	return false;
}

function verif_areacode(chaine)
{
	exp = new RegExp("^[+]?[0-9]*$");
	if(exp.test(chaine))
        	return true;
        return false;
}

function test(){
	alert('test');
}

//Donne la valeur d'un parametre de l'Url
function GetParametre(nom) 
{
  	urlvar = new Array();
  	urlvarnum = new Array();

  	if (window.location.search != "") 
  	{
		longueur = window.location.search.length - 1;
		data = window.location.search.substr(1,longueur);
		donnees = data.split("&");
		for (var i=0; i < donnees.length; i++) 
		{
			position = donnees[i].indexOf("=");
			variable = donnees[i].substr(0,position);
			pos = position + 1;
			valeur = decodeURI(donnees[i].substr(pos,donnees[i].length));
			while (valeur.search(/\+/) != -1)
			valeur = valeur.replace(/\+/," ");
			urlvar[variable] = valeur;
			urlvarnum[i] = valeur;
		}
  	}

	return (urlvar[nom]);
}

function in_array(array,value){
	for (var i = 0; i < array.length; i++) {
		if (array[i] == value) {
			return true;
		}
	}
	return false;
}

//permet de restrindre la valeur de saisie d'un input au entiers
//a adapter pour le reste
function SUC(champ)
{
 	this.champ=champ;
 	var Lui=this;
 	var ie = false; /*@cc_on ie = true; @*/
 	if ( ie ) 
     		this.champ.onkeypress = Lui.IE;
 	else  
	{ 
     		this.champ.onkeyup = function(e)
      		{
       			Lui.FF(this, e);
      		}
    	}

}


SUC.prototype.IE=function() 
{
 	if ( event.keyCode<0x30 || event.keyCode>0x39 )
 		event.returnValue= false;
}

SUC.prototype.FF=function(zone,evt)
{
 	if ( evt.which<0x30 || evt.which>0x39 )
 		zone.value=zone.value.replace(/[^0-9]/g,"");
}


//met le tableau de valeur tab dans une liste deroulante
function lst_setValue(tab,liste) {
	lst_del(liste);
	for (i=0;i<tab.length;i++)
	{
		var o=new Option(tab[i][0],tab[i][1]);
		liste.options[i]=o;
	}
}

//efface une liste deroulante
function lst_del(liste) {
  var max = liste.options.length;
  for (j=max-1;j>-1;j--) 
	liste.options[j]=null;
}


//==================chaine====================
//ecrit la chaine si elle ne depasse pas la taille nb
//sinon complette par 3 pt
function txt_limit(chaine,nb)
{
	var result = "";
	if(chaine.length > nb)
	{
		result = chaine.substring(0,nb);
		result = result + "...";	
	}
	else
		result = chaine;

	document.write(result);
}



//valide un e-mail
function verif_mail(email){
   var mail = /^[\w\-]+(\.[\w\-]+)*@[\w\-]+(\.[\w\-]+)*\.[\w\-]{2,}$/;
   return (mail.test(email));
}

function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}

function trim(myString) {
	//alert(myString);
	//alert(myString.replace(/^\s+/g,'').replace(/\s+$/g,''));
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'') ;
}

function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}


//----------------------------------routine permattant de tronquer une chaine (voir si il n'existe pas une fct qui le fait directement)
function right(str,nb){
	if (typeof(str)=='string'){
		var retStr ='';
		for (var i = str.length-1; i >= str.length-nb; i--)
			retStr=str.charAt(i)+retStr;
		return (retStr);
	}
	else return '';
}


//==================avoir les coordonnees d'un div====================
function FindPos(ID_Div)
{
	var obj = document.getElementById(ID_Div);
	var posX = 0, posY = 0;
	do
	{
		posX += obj.offsetLeft;
		posY += obj.offsetTop;
		obj = obj.offsetParent;
	}
	while( obj != null );
	var pos = [];
	pos['X'] = posX;
	pos['Y'] = posY;
	return pos;
}

function getWith(ID_Div)
{
	var obj = document.getElementById(ID_Div);
	return obj.offsetWidth;
}

function getHeight(ID_Div)
{
	var obj = document.getElementById(ID_Div);
	return obj.offsetHeight;
}

function exist(obj){
	if( (obj==null) || (obj==undefined) )
		return false;
		
	return true;
}
//==================afficher un div====================
function show(id){
	var obj = document.getElementById(id);
	if(exist(obj))
		obj.style.visibility= 'visible';
}
function hide(id){
	var obj = document.getElementById(id);
	if(exist(obj))
		obj.style.visibility= 'hidden';
}

//==================bouger un div====================
function get_offset(elem, pos){
    var offset = 0;
    while(elem) {
        offset += elem[pos];
        elem = elem.offsetParent
    }
    return offset
}
function get_full_position(id){
    var d = document.getElementById(id);
    var offsets = new Object;
    offsets.top = get_offset(d,"offsetTop");
    offsets.left = get_offset(d,"offsetLeft");
    offsets.width = d.clientWidth;
    offsets.height = d.clientHeight;
    return offsets;
}

//met deux bloc l'un a cote de l'autre
function a_cote(id1,id2){   
	var posi = FindPos(id2);
	var decal = get_decalage();
	document.getElementById(id1).style.left = posi['X'] -decal['X'] + document.getElementById(id2).offsetWidth +'px';
	document.getElementById(id1).style.top = posi['Y'] -decal['Y'] + 'px';
}

function a_cote_screen(ancre,to_place){   
	var posi_ancre 		= FindPos(ancre);
	var ancre_l			= document.getElementById(ancre).offsetWidth;
	var ancre_h			= document.getElementById(ancre).offsetHeight;
	var screen_h 		= screen.height;
	var screen_l 		= screen.width;
	var obj_l 			= document.getElementById(to_place).offsetWidth;
	var obj_h 			= document.getElementById(to_place).offsetHeight;
	var obj_x 			= posi_ancre['X']+ancre_l;
	var obj_y 			= posi_ancre['Y'];
	//if()
	document.getElementById(to_place).style.left	= obj_x + 'px';
	document.getElementById(to_place).style.top		= obj_y + 'px';
}

function a_cote_ssdecal(id1,id2){   
	var posi = FindPos(id2);
	document.getElementById(id1).style.left = posi['X'] + document.getElementById(id2).offsetWidth +'px';
	document.getElementById(id1).style.top = posi['Y'] + 'px';
}

function a_cote_g(id1,id2){   
	var posi = FindPos(id2);
	var decal = get_decalage();
	document.getElementById(id1).style.left = posi['X']-decal['X'] - document.getElementById(id1).offsetWidth +'px';
	document.getElementById(id1).style.top = posi['Y']-decal['Y'] + 'px';
}

function setTop(ID_Div,top)
{
	var obj = document.getElementById(ID_Div);
	obj.style.top = top+"px";
}

function setLeft(ID_Div,left)
{
	var obj = document.getElementById(ID_Div);
	obj.style.left = left+"px";
}

function setWith(ID_Div,width)
{
	var obj = document.getElementById(ID_Div);
	obj.style.width = width+"px";;
}

function setHeight(ID_Div,height)
{
	var obj = document.getElementById(ID_Div);
	obj.style.height = height+"px";
}

function center(ID_Div){
	var div = document.getElementById(ID_Div);
	var L=div.offsetWidth;
	var X=(screen.width/2)-(L/2);
	var Y=20;
	div.style.left=X+"px";
	div.style.top=Y+"px";
}

function center_ecran(ID_Div) 
{
	var width=document.getElementById(ID_Div).offsetWidth;
	var pWidth=document.body.clientWidth;
	var posX=(pWidth/2)-(width/2);
	document.getElementById(ID_Div).style.left=posX+"px";
}


//renvoie les coordonnée de la sourie
function MousePos(e){
	//permet de ne pas avoir de decalage si on joue de la molette ou de l'ascensseur dans ie
	var ietruebody=(document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
	var netscape=(navigator.appName.substring(0,3) == "Net");
	var x=(netscape)?e.pageX : event.clientX+ietruebody.scrollLeft;	
	var y=(netscape)?e.pageY : event.clientY+ietruebody.scrollTop;
	
	var pos = [];
	pos['X'] = x;
	pos['Y'] = y;
	return pos;
}

//input
function input_blur(txt,id_input){
	if(document.getElementById(id_input).value=="")
		document.getElementById(id_input).value=txt;
}

function input_focus(txt,id_input){
	if(document.getElementById(id_input).value==txt)
		document.getElementById(id_input).value="";
}
