
var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);
/*** ***** Utiliser ***** ***/

function getRef(id) {
	if (isDOM) return document.getElementById(id);
	if (isIE4) return document.all[id];
	if (isNS4) return document.layers[id];
	}
		  
		  function loadOne() {
        window.location.reload();
        }

function openwindow(url,name,width,height,ptop,pleft) {
	window.open(url,name,'toolbar=0,menubar=0,scrollbars=yes,resizable=0,width=' + width + ',height=' + height + ',top=' + ptop + ',left=' + pleft);
	}

/************************** MM_preloadImages 3.0
 *
 * Auteur : Macromédia
 *
 *
 */


function MM_preloadImages() {
	var d = document;
	if(d.images) {
		if(!d.MM_p) d.MM_p=new Array();
		var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
		for(i=0; i<a.length; i++) if(a[i].indexOf("#") != 0) {
			d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
			}
		}
	}


/************************** ajouter
 *
 */

function ajouter(id,url) {
	pleft = (window.screen.availWidth - 640) / 2;
	ptop = (window.screen.availHeight - 480) / 2;
	openwindow(url+'_modifier.php?id='+id,'modifier',640,480,ptop,pleft);
	}

/************************** repondre
 *
 */

function repondre(id,table) {
	pleft = (window.screen.availWidth - 640) / 2;
	ptop = (window.screen.availHeight - 480) / 2;
	openwindow('repondre.php?id='+id+'&table='+table,'repondre',640,480,ptop,pleft);
	}

/************************** supprimer
 *
 * Version : 1.2 2003-09-04
 * Auteur : Sebastien Poubelle
 *
 * Appelle la page supprimer.php qui permet de supprimer
 * l'enregistrement 'galerie_id' de la table 'table' de la base
 * de donnée courante.
 * 'url' est l'adresse de retour et 'arg' est la chaine
 * de caractčres correspondant aux argument ŕ retourner.
 *
 */

function supprimer(id,table,url,arg) {
	if(confirm("Confirmez-vous la suppression définitive ?\n")) {
		location = "supprimer.php?id="+ id +"&table="+table +"&url="+ url +"&arg=&"+ arg;
		}
	}

function traiteur_supprimer(id,type,url,arg) {
	if(confirm("Confirmez-vous la suppression définitive ?\n")) {
		if(type == 0) {
			if(confirm("Il est possible que le traiteur soit associé ŕ des lieux de réception.\nConfirmez-vous leurs suppressions définitives ?\n")) {
				location = "traiteur_supprimer.php?sup_id="+ id +"&url="+ url +"&arg=&"+ arg;
				}
			}
		else location = "traiteur_supprimer.php?sup_id="+ id +"&url="+ url +"&arg=&"+ arg;
		}
	}

function pub_supprimer(id,arg) {
	if(confirm("Confirmez-vous la suppression définitive ?\n")) {
		location = "pub_supprimer.php?id="+ id;
		}
	}

function fiche(id,table)
{
        pleft = (window.screen.availWidth - 640) / 2;
        ptop = (window.screen.availHeight - 480) / 2;
        openwindow('fiche.php?id='+id+'&table='+table,'fiche',640,480,ptop,pleft);
}

function traiter(id,table) {
        pleft = (window.screen.availWidth - 640) / 2;
        ptop = (window.screen.availHeight - 480) / 2;
        openwindow('traiter.php?id='+id+'&table='+table,'traiter',640,480,ptop,pleft);
        }

function valider_traitement(color,id,table) { // version 1.1
        if(confirm("Confirmez-vous le traitement ?")) {
                document.getElementById("nowLoading").innerHTML = "<font color='"+ color +"'>TRAITEMENT EN COURS</font>";
                document.location.href = "traiter_valid.php?id="+ id +"&table="+ table;
               }
        }

function loaded(color) { // version 1.2
        document.getElementById("nowLoading").innerHTML = "<font color='"+ color +"'>TRAITEMENT EN COURS</font>";
        }

function reloaded(color,id) { // version 1.2
        document.getElementById(id).innerHTML = "<font color='"+ color +"'>TRAITEMENT EN COURS</font>";
        }

function verif(color) { // version 3.1 (derničre modification éffectuée par Sébastien Poubelle)
        if(eval('document.form.objet')) {
                if( document.form.objet.value == "" ) {
                        alert("Vous n'avez pas saisi d'objet");
                        document.form.objet.focus();
                        return;
                        }
                }
        if(eval('document.form.corps')) {
                if(document.form.corps.value == "") {
                        alert("Vous n'avez pas saisi de message");
                        document.form.corps.focus();
                        return;
                        }
                }
        if(eval('document.form.email')) {
                var email = document.form.email.value;
                if(email.indexOf("@") == "-1" || email.indexOf(".") == "-1" || email == "") {
                        alert("Vous n'avez pas saisi d'email");
                        document.form.email.focus();
                        return;
                        }
                }
        document.getElementById("nowLoading").innerHTML = "<font color='"+ color +"'>TRAITEMENT EN COURS</font>";
        document.form.submit();
        }


/************************** dateString 1.0 2007-05-16
 *
 * Retourne une chaine de caractère de forme 'AAAA-MM-JJ'
 * à partir de l'objet arg_date.
 *
 * exemple d'utilisation : alert(dateString(new Date()));
 *
 */

function dateString (arg_date)
{
	var annee = arg_date.getFullYear();
	annee = ""+ annee;
	var mois = arg_date.getMonth()+1;
	mois = ""+ mois;
	if(mois.length < 2) mois = "0"+ mois;
	var jour = arg_date.getDate();
	jour = ""+ jour;
	if(jour.length < 2) jour = "0"+ jour;
	return(annee +"-"+ mois +"-"+ jour);
}



function IsNumeric(strString)
// check for valid numeric strings
{
        var strValidChars = "0123456789.- ";
        var strChar;
        var blnResult = true;

        if (strString.length == 0) return false;

        // test strString consists of valid characters listed above
        for (i = 0; i < strString.length && blnResult == true; i++)
        {
                strChar = strString.charAt(i);
                if (strValidChars.indexOf(strChar) == -1) blnResult = false;
        }
        return blnResult;
}

function popup(pop_url,pop_width,pop_height) {
        pop_name = pop_url.substring(0,pop_url.indexOf("."));
        pop_left = (window.screen.availWidth / 2) - (pop_width / 2);
        pop_top = (window.screen.availHeight / 2) - (pop_height / 2);
        window.open(pop_url,pop_name,"left=" + pop_left + ",top=" + pop_top + ",width=" + pop_width + ",height=" + pop_height + ",scrollbars=no,noresize");
        }

/******************** FONCTION PHPMYADMIN ********************/

/**
  * Checks/unchecks all options of a <select> element
  *
  * @param   string   the form name
  * @param   string   the element name
  * @param   boolean  whether to check or to uncheck the element
  *
  * @return  boolean  always true
  */

function setSelectOptions(the_form, the_select, do_check)
{
    var selectObject = document.forms[the_form].elements[the_select];
    var selectCount  = selectObject.length;

    for (var i = 0; i < selectCount; i++) {
        selectObject.options[i].selected = do_check;
    } // end for

//    return true;
} // end of the 'setSelectOptions()' function

function ejs_img_fx(img) {
     if(img && img.filters && img.filters[0]) {
          img.filters[0].apply();
          img.filters[0].play();
          }
     }

/************************** mailto 1.1 2007-06-22
 *
 * Auteur : Sébastien Poubelle
 *
 */


function mailto(user,domaine,style) {
	var at = "@"
	document.write("<a href=\"mailto:"+ user + at + domaine +"\" style=\""+ style +"\">");
	document.write(user + at + domaine +"</a>");
	}

/************************** getXhr 1.0 2007-03-13
 *
 * Retourne un objet XMLHttpRequest en déterminant le navigateur utilisé.
 *
 */

function getXhr()
{
	var xhr = null; 
	if(window.XMLHttpRequest) xhr = new XMLHttpRequest(); // Firefox et autres
	else if(window.ActiveXObject) // Internet Explorer
	{
		try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
	}
	else // XMLHttpRequest non supporté par le navigateur 
	{
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		xhr = false;
	}
	return xhr
}


/************************** getXhrById 1.0 2007-05-30
 *
 * Retourne le résultat du script arg_script dans l'id arg_id.
 *
 */

function getXhrById (arg_script, arg_id)
{
	var xhr = getXhr()
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			getRef(arg_id).innerHTML = xhr.responseText;
		}
	}
	xhr.open("GET",arg_script,true);
	xhr.send(null);
}


/************************** getXhrByScript 1.0 2007-05-30
 *
 * Exécute le résultat retourné par le script arg_script.
 *
 */

function getXhrByScript (arg_script)
{
	var xhr = getXhr()
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			eval(xhr.responseText);
		}
	}
	xhr.open("GET",arg_script,true);
	xhr.send(null);
}


/************************** getXhrFormById 1.0 2007-05-29
 *
 * Traite un formulaire arg_form par un objet XMLHttpRequest en utilisant
 * le script arg_script. Le résultat est retourné dans l'id arg_id.
 *
 */

function getXhrFormById (arg_form, arg_script, arg_id)
{
	var xhr = getXhr()
	var xhr_data = "";
	for(var i = 0; i < arg_form.elements.length; i++)
	{
		if(i != 0) xhr_data += "&";
		if(arg_form.elements[i].type == "file")
		{
			alert("file : "+arg_form.elements[i].name+"="+arg_form.elements[i].value+" ("+arg_form.elements[i].tmp_name+")");
			
		}
		else xhr_data += arg_form.elements[i].name +"="+ arg_form.elements[i].value;
	}
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			getRef(arg_id).innerHTML = xhr.responseText;
		}
	}
	xhr.open("POST",arg_script,true);
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.send(xhr_data);
}


/************************** getXhrFormByScript 1.0 2007-05-30
 *
 * Traite un formulaire arg_form par un objet XMLHttpRequest en utilisant
 * le script arg_script. Le résultat retourné est exécuté en tand que
 * script JS.
 *
 */

function getXhrFormByScript (arg_form, arg_script)
{
	var xhr = getXhr()
	var xhr_speed = 10;
	var xhr_data = "";
	for(var i = 0; i < arg_form.elements.length; i++)
	{
		if(i != 0) xhr_data += "&";
		xhr_data += arg_form.elements[i].name +"="+ arg_form.elements[i].value;
	}
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200)
		{
			eval(xhr.responseText);
		}
	}
	xhr.open("POST",arg_script,true);
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.send(xhr_data);
}


function isDate(dateStr)
{
//	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var datePat = /^(\d{4})(-)(\d{1,2})(-)(\d{1,2})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null)
	{
//		alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		return false;
	}

	year = dateStr.substr(0,4);
	month = dateStr.substr(5,2);
	day = dateStr.substr(8,2);
	
// 	month = matchArray[1]; // p@rse date into variables
// 	day = matchArray[3];
// 	year = matchArray[5];

	if (month < 1 || month > 12)
	{ // check month range
//		alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31)
	{
//		alert("Day must be between 1 and 31.");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31)
	{
//		alert("Month "+month+" doesn`t have 31 days!")
		return false;
	}

	if (month == 2)
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap))
		{
//			alert("February " + year + " doesn`t have " + day + " days!");
			return false;
		}
	}
	return true; // date is valid
}

