function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  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];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function callJS(jsStr) { //v2.0
  return eval(jsStr)
}
function ouvre_fenetre(Url,Nom,Carac){
  window.open(Url,Nom,Carac);
}
/////////////////////////////////
// fct pour tester si au moins
// une ligne est selectionnée
/////////////////////////////////
function testSelect( form ){
  var aumoinsun = 0;
  eval("formulaire = document."+form+".sel");
  for (i=0; i< formulaire.length ; i++){
    if( formulaire[i].checked == true ){
      aumoinsun++;
    }
  }
  if( aumoinsun == 0 ){
  	alert('Vous devez sélectionner au  moins un item');
  	return false;
  }
	return true;
}
/////////////////////////////////
// fct pour tester si tout les champs
// sont saisis
/////////////////////////////////
function verif_chp_vide(){
var verif=true;
 for(i=0; i<document.forms[0].elements.length; ++i)
  if(document.forms[0].elements[i].value == "")  {
    alert("Tous les champs doivent être remplis!\r All fields must be filled!");
        document.forms[0].elements[i].focus();
        return false;
   }
  return true;
}
/////////////////////////////////
// fct pour tester si tous les
// champs dans la saisie articles
// sont renseignés
/////////////////////////////////
function verif_chp_vide_part(){
var verif=true;
  if(document.gest_art.ref_art.value =="")  {
          alert("Tu as oublié de rentrer la référence de l'article");
          document.gest_art.ref_art.focus();
          return false;
	}
  if(document.gest_art.idmarq_art.value =="")  {
          alert("Tu as oublié de rentrer la marque de l'article");
          document.gest_art.idmarq_art.focus();
          return false;
	}
  if(document.gest_art.idnomcat_art.value =="")  {
          alert("Tu as oublié de rentrer le domaine de l'article");
          document.gest_art.idnomcat_art.focus();
          return false;
	}
  if(document.gest_art.idssnomcat_art.value =="")  {
          alert("Tu as oublié de rentrer la spécialité de l'article");
          document.gest_art.idssnomcat_art.focus();
          return false;
	}
  if(document.gest_art.idechcat_art.value =="")  {
          alert("Tu as oublié de rentrer l'échelle de l'article");
          document.gest_art.idechcat_art.focus();
          return false;
	}
  if(document.gest_art.nom_art_fr.value =="")  {
          alert("Tu as oublié de rentrer le nom de l'article");
          document.gest_art.nom_art_fr.focus();
          return false;
	}
  if(document.gest_art.stk_art.value =="")  {
          alert("Tu as oublié de rentrer la quantité");
          document.gest_art.stk_art.focus();
          return false;
	}	
}
//soumission de formulaire
function soumettre(form){
  var test = testSelect( form );
  if( test == true ){
    soum="document."+form+".submit()";
    eval(soum);
  }
}
//fonction pour faire la selection de tout les articles de la page
//name checkbox du type nomcheckbox[0], nomcheckbox[1], nomcheckbox[X]
function selectous(coche, champ){
  i = 0;
  if( coche == false ){ while ( caseCochee = document.getElementsByName(champ+"["+i+"]")[0] ){ caseCochee.checked = false; i++; } }
  else { while ( caseCochee = document.getElementsByName(champ+"["+i+"]")[0] ){ caseCochee.checked = true; i++; } }
}

//////////////////////
// JS Calendar
//
//
/////////////////////
var calendar = null; // remember the calendar object so that we reuse
// it and avoid creating another

// This function gets called when an end-user clicks on some date
function selected(cal, date) {
	cal.sel.value = date; // just update the value of the input field
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
	cal.hide();			// hide the calendar

	// don't check mousedown on document anymore (used to be able to hide the
	// calendar when someone clicks outside it, see the showCalendar function).
	Calendar.removeEvent(document, "mousedown", checkCalendar);
}

// This gets called when the user presses a mouse button anywhere in the
// document, if the calendar is shown.  If the click was outside the open
// calendar this function closes it.
function checkCalendar(ev) {
	var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
	for (; el != null; el = el.parentNode)
	// FIXME: allow end-user to click some link without closing the
	// calendar.  Good to see real-time stylesheet change :)
	if (el == calendar.element || el.tagName == "A") break;
	if (el == null) {
		// calls closeHandler which should hide the calendar.
		calendar.callCloseHandler(); Calendar.stopEvent(ev);
	}
}

//fct pour affichage du calendrier
function showCalendar(id) {
	var el = document.getElementById(id);
	if (calendar != null) {
		// we already have one created, so just update it.
		calendar.hide();		// hide the existing calendar
		calendar.parseDate(el.value); // set it to a new date
	} else {
		// first-time call, create the calendar
		var cal = new Calendar(true, null, selected, closeHandler);
		calendar = cal;		// remember the calendar in the global
		cal.setRange(1900, 2070);	// min/max year allowed
		calendar.create();		// create a popup calendar
	}
	calendar.sel = el;		// inform it about the input field in use
	calendar.showAtElement(el);	// show the calendar next to the input field

	// catch mousedown on the document
	Calendar.addEvent(document, "mousedown", checkCalendar);
	return false;
}
/////////////////////////////////
// fcts pour XMLHttpRequest
/////////////////////////////////
/*creation de l'objet XMLHttpRequest*/
function getHTTPObject( action )
{
  var xmlhttp = null;
  if (window.XMLHttpRequest) {
	  // If IE7, Mozilla, Safari, and so on: Use native object
	  xmlhttp = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
		// ...otherwise, use the ActiveX control for IE5.x and IE6
			try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");//xmlHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0');
			 }
			catch (e){
				try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
				catch (E){
		           xmlhttp = false;
		        }
			}
		}
	}
  
  this.open = function ( method, url, async){ xmlhttp.open( method, url, async); }
  this.send = function ( data ){ xmlhttp.send( data ); }
  this.setRequestHeader = function (type, head ){ xmlhttp.setRequestHeader(type, head ); }
  if (xmlhttp){
     /* on définit ce qui doit se passer quand la page répondra */
     xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4) { /* 4 : état "complete" */
           if (xmlhttp.status == 200) { /* 200 : code HTTP pour OK */
               eval( action );
               //prompt("",xmlhttp.responseText);
               //alert(xmlhttp.responseText);
           }
        }
     }
  }
}


/**
  * Envoie des donnees a l'aide d'XmlHttpRequest?
  * @param string methode d'envoi ['GET'|'POST']
  * @param string url
  * @param string donnees a envoyer sous la forme var1=value1&var2=value2...
  */
 function envajax(action, data, url, method )
 {
    var xhr = new getHTTPObject( action );
    //xhr = false;
    if (!xhr) {  return false; }
    if( !method ){ method ="POST"; }
    //alert(data);
    if(method == "GET"){
        if(data == 'null'){ xhr.open("GET", url, true); } //ouverture asynchrone
        else{ xhr.open("GET", url+"?"+data, true); }
        xhr.send(null);
        return true;
     }
     else {
        xhr.open("POST", url, true); //ouverture asynchrone
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.send(data);
        return true;
     }
 }