/* ***************************************************************
	FONCTION getElement(psID)
	ADAPTER UN OBJET A TOUS LES BROWSERS
	Description: Adapter un objet à tous les browsers 
				 IE ou NS (toute version).
	Syntaxe:     OnKeyPress="getElement("Q12001");"
*************************************************************** */
function getElement(psID) { 
   if(document.all) { 
      return document.all[psID]; 
   } else if(document.getElementById) { 
      return document.getElementById(psID); 
   } else { 
      for (iLayer = 1; iLayer < document.layers.length; iLayer++) { 
         if(document.layers[iLayer].id == psID) 
            return document.layers[iLayer]; 
      }       
   } 
   return Null; 
} 
/* ***************************************************************
	FONCTION obligatoire2(tab, formulaire)
	VERIFICATEUR SI TOUS LES CHAMPS DU TABLEAU TAB SONT REMPLIS
	Description: Tous les champs text, textatea et radio du tableau Tab sont obligatoires
	Syntaxe:     obligatoire(tab, document.form1);"
*************************************************************** */
function obligatoire(tab, formulaire){
var nbr_obli = 0;
var ch = false;
var i;
	for(i=1; i<tab.length; i++){		
		if(typeof(tab[i])=='string'){
			champ = getElement(tab[i]);
		}
		else{
			
			if(tab[i].type=='radio'){
				champ = getElement(tab[i].name);
			}
			else{			
				champ = tab[i];
			}
		}
		if((champ.length>1)&&(champ.type=='radio')){			
			type = champ[1].type;
			name = champ[1].name;
			disabled = champ[1].disabled;
		}else{
			type = champ.type;
			name = champ.name;
			disabled = champ.disabled;
		}
		switch(type){
			case 'text' :
			case 'textarea' :
			case 'file' :
				if(champ.readOnly==0 && champ.disabled==0){
					if(champ.value=='') nbr_obli++; 
					classer(champ, champ.value=='');
				}
				break;

			case 'radio' :
				if(!disabled){
					if(!choix_fait(champ)){
						nbr_obli++;
					}
				}
				break;
			case 'select-one' :
				if(champ.disabled==0){
					if(champ.value==0) nbr_obli++; 
					classer(champ, champ.value==0||champ.value==-1);
				}
				break;
			default :
		}
	}
	return(nbr_obli);
}
/* ***************************************************************
	FONCTION choix_fait(nom, formulaire)
	VERIFICATEUR SI LE CHOIX A ETE FAIT SUR L'OPTION INTITULZ Nom
	Syntaxe:     choix_fait(nom, formulaire);"
*************************************************************** */
function choix_fait2(field){
var fait = false;
var i;
	for(i=0; i<champ.length; i++){
		field[i].className='';
		if(field[i].checked){
			fait = true;
		}
	}
	if(! fait) for(i=0; i<champ.length; i++) field[i].className='radiorouge';
	return fait;
}
/* ***************************************************************
	FONCTION classer(obj, ok)
	Modifier className de obj en fonction de ok
	Syntaxe:     "classer(obj, true);"
*************************************************************** */
function classer(obj, ok){
	if(ok){
		if(obj.className=='chp1_p') obj.className='chp1_p_rouge';
		if(obj.className=='chp2_p') obj.className='chp2_p_rouge';
		if(obj.className=='chp3_p') obj.className='chp3_p_rouge';
		if(obj.className=='list_p') obj.className='list_p_rouge';
		if(obj.className=='txtarea_p') obj.className='txtarea_p_rouge';
		if(obj.className=='text_standard') obj.className='text_standard_rouge';
	}else{
		if(obj.className=='chp1_p_rouge') obj.className='chp1_p';
		if(obj.className=='chp2_p_rouge') obj.className='chp2_p';
		if(obj.className=='chp3_p_rouge') obj.className='chp3_p';
		if(obj.className=='list_p_rouge') obj.className='list_p';
		if(obj.className=='txtarea_p_rouge') obj.className='txtarea_p';
		if(obj.className=='text_standard_rouge') obj.className='text_standard';
		
	}
}
/* ***************************************************************
	FONCTION VerifyNumericValue()
	VERIFICATEUR EN COURS DE FRAPPE
	Description: Verifie la valeur d'un champs numerique
				 que ce soit entier ou decimal.
	Syntaxe:     OnKeyPress="VerifyNumericValue(this);"
*************************************************************** */
function VerifyNumericValue(obj){
	// Pour les nombre entier
	if (navigator.appName.substr(0, 1) == 'M'){
		if (((event.keyCode < 48) || (event.keyCode > 57)) && (event.keyCode != 44) && (event.keyCode != 46)){
			event.returnValue = false; 
		}
		else{
		  if (((event.keyCode == 44) || (event.keyCode == 46)) && ((obj.value.indexOf('.') >= 0) || (obj.value.indexOf(',') >= 0))){
			event.returnValue = false; 
		  }
		}
	}
}
/* ***************************************************************
	FONCTION VerifyIntegerValue()
	VERIFICATEUR EN COURS DE FRAPPE
	Description: Verifie la valeur d'un champs numerique
				 (entier).
	Syntaxe:     OnKeyPress="VerifyIntegerValue(this);"
*************************************************************** */
function VerifyIntegerValue(obj){
	// Pour les nombre entier
	if (navigator.appName.substr(0, 1) == 'M'){
		if (((event.keyCode < 48) || (event.keyCode > 57)) && (event.keyCode != 44) && (event.keyCode != 46) && (event.keyCode != 40) && (event.keyCode != 41) && (event.keyCode != 43) && (event.keyCode != 32)){
			event.returnValue = false; 
		}
		else{		
		  if ((event.keyCode == 44)){
			event.returnValue = false; 
		  }
		}
	}
}

/*---------------------------------------------
Vide un champ de saisie
---------------------------------------------*/
function effacer_champ(form, champ){
	form.elements[champ].value="";
}
/* ---------------------------------------------
 Teste si c'est un chiffre ou non (caractère)
 ----------------------------------------------*/
function est_chiffre (c) 
{
   var d = c ;
   return ((0 <= d) && (d<=9) && (d!="") ) ;
}
/* -------------------------------------------
 Teste si c'est un slash ou non
 --------------------------------------------*/
function est_slash (c)
{
   return (c=='/') ;
}
/*----------------------------------
 Teste la validité d'une date
------------------------------------*/
function final_ver (s) {
var d, t,an_t,an_d ;
       t = s.split("/") ;
       d = new Date (t[2], t[1]-1, t[0]) ;
       if  (eval(t[2])>1900)
                an_t=eval(t[2])-1900
       else
                an_t=eval(t[2]);

       if  (d.getYear ()>1900)
                an_d=d.getYear ()-1900
       else
                an_d=d.getYear ();


       if ((d.getDate() == eval (t[0])) && (d.getMonth()+1 == eval(t[1])) && (an_d == an_t))
       {
           return true ;
       }
       else
       {
           return false ;
       } ;
}
/*---------------------------------
 Teste la syntaxe d'une date
-----------------------------------*/
function controler_date (d) {
    if (d.length != 10)
    {
        return false ;
    }
    else
    {
        var test0 = est_chiffre (d.charAt(0)) ;
        var test1 = est_chiffre (d.charAt(1)) ;
        var test2 = est_slash (d.charAt(2)) ;
        var test3 = est_chiffre (d.charAt(3)) ;
        var test4 = est_chiffre (d.charAt(4)) ;
        var test5 = est_slash (d.charAt(5)) ;
        var test6 = est_chiffre (d.charAt(6)) ;
        var test7 = est_chiffre (d.charAt(7)) ;
        var test8 = est_chiffre (d.charAt(8)) ;
        var test9 = est_chiffre (d.charAt(9)) ;
        var res= (test0 && test1 && test2 && test3 &&
                  test4 && test5 && test6 && test7
                  && test8 && test9) ;
       if (res)
       {
           return (final_ver (d)) ;
       }
       else
       {
           return (false) ;
       } ;
    }
}
/*-------------------------------------------
convertir en yyyymmdd d'une date dd/mm/yyyy
--------------------------------------------*/
function convertir_date(strDateFr) {
	var res = "";
	var d = strDateFr;
	if(d.length!=10) {
		return "";
	}
	res = d.substring(6,10);
	res += d.substring(3,5);
	res += d.substring(0,2);
	return res;
}
/*---------------------------------------------
comparer deux date ,operateur = opérateur de comparaison : "<", ">" , "==", "<=", ">=" , "!="
----------------------------------------------*/
function comparer_date(date1 , date2 , operateur){
	var bRes= false;
	if ((date1!="")&&(date2!=""))
	{
		bRes = eval(date1 + operateur + date2); 
	}	
	return bRes;
}

//-------------- MAIL -------------------------------------------------------------
/*----------------------------------------------------------
 Function qui teste la syntaxe d'une adresse e-mail (APPEL) 
------------------------------------------------------------*/
function isEmail(str) {
    var supported = 0;
    if (window.RegExp) {
      var tempStr = "a";
      var tempReg = new RegExp(tempStr);
      if (tempReg.test(tempStr)) supported = 1;
    }
    if (!supported)
      return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
    return (!r1.test(str) && r2.test(str));
}

/*----------------------------------------------------------
 Function qui teste la chaine est un chiffre
------------------------------------------------------------*/
function checkNum(str) {
    if (str == "") {
        return false;
    }
    for (var i = 0; i < str.length; i++) {
        var ch = str.substring(i, i + 1);
        if ((ch < "0" || ch > "9") && ch!="") {
            return false;
        }
    }
	return true;
}
/*----------------------------------------------------------
 Function qui teste la chaine est un décimal
------------------------------------------------------------*/
function CheckDecimal(str){
    if (str == "") {
        return false;
    }
    for (var i = 0; i < str.length; i++) {
        var ch = str.substring(i, i + 1);
        if ((ch < "0" || ch > "9") && (ch!="") && (ch!=".") && (ch!=",")) {
            return false;
        }
    }
	return true;
}
/*------------------------------------------------------------
Function pour créer un popup
-------------------------------------------------------------*/
function showPopUp(w, h, link) {
	var awjspuwbwx = (screen.width-w)/2 ;
	var awjspuwbwy = (screen.height-h)/2 ;
	var awjspuwbwpos = "left="+awjspuwbwx+",top="+awjspuwbwy ;
	awPopUpWin = window.open(link, "AWPopWin", "width="+w+",height="+h+",fullscreen=0,directories=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,maximize=0,toolbar=0,alwaysRaised=yes,"+awjspuwbwpos);
}
function showPopUp2(w, h, link) {
	var awjspuwbwx = (screen.width-w)/2 ;
	var awjspuwbwy = (screen.height-h)/2 ;
	var awjspuwbwpos = "left="+awjspuwbwx+",top="+awjspuwbwy ;
	awPopUpWin = window.open(link, "AWPopWin", "width="+w+",height="+h+",fullscreen=0,directories=0,location=0,menubar=0,resizable=0,scrollbars=1,status=0,maximize=0,toolbar=0,alwaysRaised=yes,"+awjspuwbwpos);
}
function showPopUp3(name, w, h, link) {
	var awjspuwbwx = (screen.width-w)/2 ;
	var awjspuwbwy = (screen.height-h)/2 ;
	var awjspuwbwpos = "left="+awjspuwbwx+",top="+awjspuwbwy ;
	awPopUpWin = window.open(link, name, "width="+w+",height="+h+",fullscreen=0,directories=0,location=0,menubar=0,resizable=0,scrollbars=1,status=0,maximize=0,toolbar=0,alwaysRaised=yes,"+awjspuwbwpos);
}

function openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function Exporter(w, h, link) {
	var awjspuwbwx = (screen.width-w)/2 ;
	var awjspuwbwy = (screen.height-h)/2 ;
	var awjspuwbwpos = "left="+awjspuwbwx+",top="+awjspuwbwy ;
	awPopUpWin = window.open(link, "AWPopWin", "width="+w+",height="+h+",fullscreen=0,directories=0,location=0,menubar=1,resizable=1,scrollbars=1,status=0,maximize=1,toolbar=0,alwaysRaised=yes,"+awjspuwbwpos);
}
/*----------------------------------------------------------
Function pour aller à un URL
----------------------------------------------------------*/
function locationPage(page){
	document.location=page;
}

/*----------------------------------------------------------
Function pour valider une entrée de fichier CSV
----------------------------------------------------------*/
function valideFile() {
	var erreur= Array(true,"");
	if (document.MyForm.fichier.value != "") {	
	if (document.MyForm.sary.src.length > 0) {
		ext = document.MyForm.sary.src.substr(document.MyForm.sary.src.length-3,document.MyForm.sary.src.length).toLowerCase();
		poidsImg = Math.round(parseInt(document.MyForm.sary.fileSize*1)/1024);
		if (poidsImg > 250) {
			erreur[1]='Le poids de l\'image ne doit pas dépasser 250Ko\n';	
			erreur[0]=false;				
		}		
		if (ext != "jpg" && ext!="JPG" && ext!="peg" && ext!="PEG") {
			erreur[1]='L\'image doit être du type .jpg\n';
			erreur[0]=false;
		}
	}
	}	
	return erreur;	
}


/*----------------------------------------------------------
Function pour afficher une image
----------------------------------------------------------*/
function afficheImg(Picture) {
	if (Picture.value != "") { 
		var NewImg = new Image();
		NewImg.src = Picture.value;
		document.MyForm.sary.src = NewImg.src;
		//document.MyForm.sary.width = 10 
		document.MyForm.sary.height = 104;			
	}
	ImgCalculs();
}



/*----------------------------------------------------------
Function pour calculer la taille d'une image
----------------------------------------------------------*/
function ImgCalculs() {	
	var erreur= Array(true,"");
	if (document.MyForm.photo.value != "") {	
	if (document.MyForm.sary.src.length > 0) {
		ext = document.MyForm.sary.src.substr(document.MyForm.sary.src.length-3,document.MyForm.sary.src.length).toLowerCase();
		poidsImg = Math.round(parseInt(document.MyForm.sary.fileSize*1)/1024);
		if (poidsImg > 250) {
			erreur[1]='Le poids de l\'image ne doit pas dépasser 250Ko\n';	
			erreur[0]=false;				
		}		
		if (ext != "jpg" && ext!="JPG" && ext!="peg" && ext!="PEG") {
			erreur[1]='L\'image doit être du type .jpg\n';
			erreur[0]=false;
		}
	}
	}	
	return erreur;	
}

function aller_a_url(listbox){
	//listbox = document.all[listbox];
	var link,target,chaine,tab;
	tab = new Array();
	chaine = new String(listbox);
	tab = chaine.split(",");
	link = tab[0];
	target = tab[1];
	if(target=="_blank"){		
//		_target = window.open(_url,_target);
		_target = window.open(_url);
	}
	document.location.href = link;
}
function buildURL(URL, params){
	if(params.indexOf('?',0)==0){
		alert ("buildURL Erreur, le 1er paramètre de params ne doit pas avoir de '&'");
	}
	if(URL.indexOf('?',0)!=-1){
		return URL + "&" + params;
	}else{
		return URL + "?" + params;
	}
}
/* ***************************************************************
	FONCTION VerifyPhoneValue(e)
	VERIFICATEUR EN COURS DE FRAPPE
	Description: Verifie la valeur d'un champs de téléphone
	Syntaxe:     onkeypress="keypress(event)"
*************************************************************** */
function VerifyPhoneValue(e) {   
	
	/*var code;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	alert("Code >>" + code);*/
	

	if(!({ 40:1,41:1,8:1,9:1,27:1,37:1,48:1,49:1,50:1,51:1,52:1,53:1,54:1,55:1,56:1,57:1,3:0,22:0,32:1}[e.which||e.keyCode]))         
	e.preventDefault? e.preventDefault() : e.returnValue = false; 
}
