/*****************************************************************************
Open pop-up & changeOpener
******************************************************************************/
function popUp(url,nom,w,h){
	if (w == null) { w=800; }
	if (h == null) { h=600; }
	self.window.open(url,nom,'toolbar=no,scrollbars=yes,resize=yes,status=yes,height='+h+',width='+w+',top=0,left=200');
}

/**
* Affichage d'un contenu dans une popup
*/
function popContent(id){
	w = 550;
	h = 680;
	url = "fiche.php?id="+id;
	popUp(url,"Contenu",w,h);
}

/**
* Fonction permettant d'afficher une page
*/
function goPage($url) {
	document.location.href = $url;
}
/**
* fonction permettant de fermer la fenetre
*/
function closeWindow() {
	try { fermer(); } catch (e) {}
	self.close();
}

/*****************************************************************************
Data Checks
******************************************************************************/
function emptyInputText(obj){
	obj.value = "";
}

function CheckMail(myMail){
	var re_email = /^([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+)*$/ ;
	t_email = re_email.test(myMail);
	if (t_email) { return true; }
	return false;
}
function IsSpaces(Str){
	var trim = 0;
	for(i=0;i<Str.length;i++){
		if (Str.charAt(i) != " ") { trim = 1; }
	}
	if (trim==0) { return true; }
	return false;
}



function requestUrl() {
    var url=location.search
    if (url.length > 0) {
    	url=url.substring(1,url.length)
    }
    return url
}

function sourceBoxName(zone) {
    var url=requestUrl();
    var iForm;
    var iElement;
    var boxName="";
    if (url.length > 0) {
    	request = url.split("=")
    	if (request.length >0) {
    	    if (request[0]=="boxName") {
    			boxNameElements = request[1].split(".");
                switch(zone) {
                    case "parent":
                    {
            			boxName=eval(parent.document.forms[boxNameElements[0]].elements[boxNameElements[1]])
                        break;
                    }
                    case "opener.parent":
                    {
            			boxName=eval(opener.parent.document.forms[boxNameElements[0]].elements[boxNameElements[1]])
                        break;
                    }
                    default:
                    {
            			boxName=eval(document.forms[boxNameElements[0]].elements[boxNameElements[1]])
                        break;
                    }
                }
    		}
    	}
    }
    return boxName;
}




/*****************************************************************************
Form Data Checks
******************************************************************************/
    /**
    * Fonction permettant de compter le nombre de selection d'un select multiple
    * passé en parametre
    */
    function getNbSelected(elem) {
        var nbSelected = 0;

        for (i=0; i<elem.length; i++) {
            if (elem.options[i].selected) nbSelected++;
        }

        return nbSelected;
    }
    
    /*
    * fonction permettant de verifier si une variable est un entier
    */
    function is_entier(num) {
        return (num == parseInt(num));
    }

    // fonction permettant de verifier si une variable est un reel
    function is_reel(num) {
        return (num == parseFloat(num));
    }

    /*
    * Fonction permettant de valider un e-mail : la chaine doit
    * comporter un texte de la forme suivante :
    *      texte@texte.texte
    */
    function is_email(email) {
        if (email != '') {
            reg_exp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[_a-z0-9-]+(\.[_a-z0-9-]+)*\.[_a-z0-9-]+$/gi;
            return email.match(reg_exp);
        } else {
            return true;
        }
    }

    /*
    * Fonction permettant de verifier la validité d'une date
    */
    function is_date(jour, mois, annee) {
        if (jour != '' || mois != '' || annee != '') {
            date_test = new Date(annee, mois-1, jour);
            date_jour = date_test.getDate();
            date_mois = date_test.getMonth() + 1;
            date_annee = date_test.getFullYear();

            return (date_jour == jour && date_mois == mois && date_annee == annee);
        }
        else
            return true;
    }

    /*
    * Fonction permettant de tester si une chaine est vide ou constituée d'espaces
    */
    function is_empty(texte) {
        return texte.match(/^[\s]*$/gi);
    }


/*****************************************************************************
Date Checks
******************************************************************************/

//----------- Fonction qui teste si la date est valide
function checkdate(objName,msg) {
	var datefield = objName;
	if (chkdate(objName) == false) {
		if(msg)	alert("La date est invalide.");
		datefield.select();
		return false;
	}
	else {
		return true;
	}
}
//----------- Fonction qui formate la date
function chkdate(objName) {
//var strDatestyle = "US"; //United States date style
var strDatestyle = "EU";  //European date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "/01/";
strMonthArray[1] = "/02/";
strMonthArray[2] = "/03/";
strMonthArray[3] = "/04/";
strMonthArray[4] = "/05/";
strMonthArray[5] = "/06/";
strMonthArray[6] = "/07/";
strMonthArray[7] = "/08/";
strMonthArray[8] = "/09/";
strMonthArray[9] = "/10/";
strMonthArray[10] = "/11/";
strMonthArray[11] = "/12/";
strDate = datefield.value;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
{
	if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
	strDateArray = strDate.split(strSeparatorArray[intElementNr]);
		if (strDateArray.length != 3) {
		err = 1;
		return false;
		}
		else {
		strDay = strDateArray[0];
		strMonth = strDateArray[1];
		strYear = strDateArray[2];
		}
		booFound = true;
	}	
	
}
if (booFound == false) {
	if (strDate.length>=5) {
	strDay = strDate.substr(0, 2);
	strMonth = strDate.substr(2, 2);
	strYear = strDate.substr(4);	
	}
}


// Test si une année a été tapée
if (strYear != null ) {

	// Test si l'année est un numerique
	if (isNaN(strYear))
	{
		//alert("Chaine interdite.")
		return false
	}
	else
	{
		// S'il n'y a qu'1 chiffre pour l'année, rajout du 200 pour le millenaire
		if (strYear.length == 1) {
		strYear = '200' + strYear;
		}	
		// S'il n'y a que 2 chiffres pour l'année, rajout du 20 pour le millenaire
		if (strYear.length == 2) {
		strYear = '20' + strYear;
		}
		// S'il n'y a que 2 chiffres pour l'année, rajout du 20 pour le millenaire
		if (strYear.length == 3) {
		strYear = '2' + strYear;
		}
	
		if (strYear.length > 4 ) {
		return false
		}
	}
}

// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
   }
}
if (isNaN(intMonth)) {
err = 3;
return false;
   }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}
if (strDatestyle == "US") {
datefield.value = strMonthArray[intMonth-1] + "" + intday+"" + strYear;
}
else {
	// Si la date n'un qu'un seul caractere donc inferieure à 9 alors ajout du 0 dans la chaine 
	if (intday>9)
	{
	jour = intday
	}
	else
	{
	jour = "0" + intday
	}
	// Si l'année est composé d'un seul caractere alors formatage pour l'an 2000 ( ex : 9 -> 2009 )
	if (strYear.length<2)
	{	
	strYear = "200" + strYear  
	}
	datefield.value = jour + "" + strMonthArray[intMonth-1] + "" + strYear;

}
return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}

function compte_char(name_editor)
{	
	// Calcul la longueur de la chaine 
	content = name_editor.document.body.innerText;
    var Nbr = content.length;
    alert("Nombre de caracteres : "+Nbr)
    name_editor.focus();
}

























/****************************************** DEPRECATED ***************************************************/

/*****************************************************************************
Default browsercheck
******************************************************************************/
function lib_bwcheck(){
	this.ver=navigator.appVersion; this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5 || this.dom)
	return this
}

/*****************************************************************************
Default CSS selection
******************************************************************************/
function selectCSS(root){
	var browser = new lib_bwcheck() //Making browsercheck object
	if(browser.ns4 || browser.ns6) {
		document.write('<LINK REL=\"stylesheet\" type=\"text/css\" href=\"'+root+'javascript/NS_mainStyle.css\">');
	} else {
		document.write('<LINK REL=\"stylesheet\" type=\"text/css\" href=\"'+root+'javascript/IE_mainStyle.css\">');
	}
}







