//##################################
//######## Fonctions MsgBox ########
//##################################
/*Simulation d'une Msgbox VB en Html sans Popup*/
/*Mettre ce script et l'image MsgBox.gif dans la racine du site*/
/*passage du minimum d'arguments, texte centré et fenêtre centrée*/
/*Appeler depuis la page html la fonction MsgBox('Texte','Titre')*/
/*Titre est optionnel, par défaut c'est : Attention ! */
/*<script language="javascript">*/
/*Msgbox('Texte','Titre');*/
/*</script>*/
/*Adaptation du script téléchargé sur www.easyscript.fr.st)*/

var IE5=(document.getElementById && document.all)? true : false;
var W3C=(document.getElementById)? true: false;
var currIDb=null, currIDs=null, xoff=0, yoff=0; zctr=0; totz=0;
var idIndex=1;/* pour ouverture plusieurs boites*/

//*-----------------------------------*
//*------- Fonction TrackMouse -------*
//*-----------------------------------*
function trackmouse(evt){
  if((currIDb!=null) && (currIDs!=null)){
    var x=(IE5)? event.clientX+document.body.scrollLeft : evt.pageX;
    var y=(IE5)? event.clientY+document.body.scrollTop : evt.pageY;
    currIDb.style.left=x+xoff+'px';
    currIDs.style.left=x+xoff+10+'px';
	currIDb.style.top=y+yoff+'px';
	currIDs.style.top=y+yoff+10+'px';
	return false;
  }
}
//*---------------------------------*
//*------- Fonction StopDrag -------*
//*---------------------------------*
function stopdrag(){
  currIDb=null;
  currIDs=null;
  NS6bugfix();
}
//*--------------------------------*
//*------- Fonction Grab_ID -------*
//*--------------------------------*
function grab_id(evt){
  xoff=parseInt(this.IDb.style.left)-((IE5)? event.clientX+document.body.scrollLeft : evt.pageX);
  yoff=parseInt(this.IDb.style.top)-((IE5)? event.clientY+document.body.scrollTop : evt.pageY);
  currIDb=this.IDb;
  currIDs=this.IDs;
}
//*----------------------------------*
//*------- Fonction NS6BugFix -------*
//*----------------------------------*
function NS6bugfix(){
  if(!IE5){
    self.resizeBy(0,1);
    self.resizeBy(0,-1);
  }
}
//*-----------------------------------*
//*------- Fonction IncrZIndex -------*
//*-----------------------------------*
function incrzindex(){
  zctr=zctr+2;
  this.subb.style.zIndex=zctr;
  this.subs.style.zIndex=zctr-1;
}
//*-------------------------------*
//*------- Fonction MsgBox -------*
//*-------------------------------*
function MsgBox(text, title){
  /*paramètres possibles*/
  idIndex +=1;
  id = "msgbox"+idIndex;
  width=200
  height=100
  x=0
  y=0
  boxcolor="#B8C8FE"
  barcolor="#7B849C"
  shadowcolor="#404040"
  textcolor= "#0069B3"  
  textptsize="9"
  textfamily="Verdana"
  titlecolor ="#00FFFF"
  isdraggable=true
  text="<center><br>"+text+"</center>"
  if (title== null){
	title="Attention !";
  }
  if(!IE5){
    x=(innerWidth/2)-width/2;
    y=(innerHeight/2)-height/2;
  }
  else{
    x=(document.body.offsetWidth/2)-width/2;
    y=(document.body.offsetHeight/2)-height/2;
  }
  if(W3C){
    zctr+=2;
    totz=zctr;
    var txt='';
    txt+='<div id="'+id+'_s" style="position:absolute; left:'+(x+10)+'px; top:'+(y+10)+'px; width:'+width+'px; height:'+height+'px; background-color:'+shadowcolor+'; filter:alpha(opacity=50); visibility:visible"> </div>';
    txt+='<div id="'+id+'_b" style="border:outset '+barcolor+' 2px; position:absolute; left:'+x+'px; top:'+y+'px; width:'+width+'px; overflow:hidden; height:'+height+'px; background-color:'+boxcolor+'; visibility:visible">';
	txt+='<div style="width:'+width+'px; height:16px; background-color:'+barcolor+'; padding:0px; border:1px"><table cellpadding="0" cellspacing="0" border="0" width="'+(IE5? width-4 : width)+'"><tr><td width="'+(width-20)+'"><div id="'+id+'_h" style="width:'+(width-20)+'px; height:14px; font: bold 12px Arial; color:'+titlecolor+'"> '+title+'</div></td><td align="right"><a onmousedown="document.getElementById(\''+id+'_s\').style.display=\'none\'; document.getElementById(\''+id+'_b\').style.display=\'none\';return false"><img src="images/MsgBox.gif" border="0" height="15" width="15"></a></td></tr></table></div>';
	txt+='<div id="'+id+'_ov" width:'+width+'px; style="margin:2px; color:'+textcolor+'; font:'+textptsize+'pt '+textfamily+';">'+text+'</div></div>';
	document.write(txt);
	this.IDh=document.getElementById(id+'_h');
	this.IDh.IDb=document.getElementById(id+'_b');
	this.IDh.IDs=document.getElementById(id+'_s');
	this.IDh.IDb.subs=this.IDh.IDs;
	this.IDh.IDb.subb=this.IDh.IDb;
	this.IDh.IDb.IDov=document.getElementById(id+'_ov');
	if(IE5){
	  this.IDh.IDb.IDov.style.width=width-6;
	  this.IDh.IDb.IDov.style.height=height-22;
	  this.IDh.IDb.IDov.style.scrollbarBaseColor=boxcolor;
	  this.IDh.IDb.IDov.style.overflow="auto";
	}
	else{
	  this.IDh.IDs.style.MozOpacity=.5;
	}
	this.IDh.IDb.onmousedown=incrzindex;
	if(isdraggable){
	  this.IDh.onmousedown=grab_id;
	  this.IDh.onmouseup=stopdrag;
	}
  }
}

if(W3C)document.onmousemove=trackmouse;
if(!IE5 && W3C)window.onload=NS6bugfix;

//##################################
//######## Fonctions Cookies #######
//##################################

//*----------------------------------*
//*------- Fonction SetCookie -------*
//*----------------------------------*
function SetCookie (name, value) {
	var argv=SetCookie.arguments;
	var argc=SetCookie.arguments.length;
	var bEscape=(argc > 2) ? argv[2] : null;
	var expires=(argc > 3) ? argv[3] : null;
	var path=(argc > 4) ? argv[4] : null;
	var domain=(argc > 5) ? argv[5] : null;
	var secure=(argc > 6) ? argv[6] : false;
	var vValue=value;
	if (bEscape == null)
	{
		vValue=escape(value);
	}
	document.cookie=name+"="+vValue+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");
}
//*-------------------------------------*
//*------- Fonction GetCookieVal -------*
//*-------------------------------------*
function getCookieVal (offset) {
  var argv=getCookieVal.arguments;
  var argc=getCookieVal.arguments.length;
  var bEscape=(argc > 1) ? argv[1] : null;
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  if (bEscape == null)
  	{
  	  return  unescape(document.cookie.substring(offset, endstr));        //unescape(document.cookie.substring(offset, endstr));
  	}
  else
  	{
  	  return  document.cookie.substring(offset, endstr);        //unescape(document.cookie.substring(offset, endstr)); 	
  	}
}
//*----------------------------------*
//*------- Fonction GetCookie -------*
//*----------------------------------*
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var argv=GetCookie.arguments;
  var argc=GetCookie.arguments.length;
  var bEscape=(argc > 1) ? argv[1] : null;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
	   if (document.cookie.substring(i, j) == arg)
     return getCookieVal (j, bEscape);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}
//*-------------------------------------*
//*------- Fonction SetSubCookie -------*
//*-------------------------------------*
function SetSubCookie( uName, name, value )
{
   uValue = GetCookie( uName, false )
   nValue = escape(value);
   if( uValue ) {
      uCookies = uValue.split( '&' )
      var found = -1
      for( c = 0; c < uCookies.length; c++ )
      {
         t = uCookies[c].split( '=' )
         tName = t[0]
         tValue = t[1]
         if( tName == name ) {
            found = 1
            tValue = nValue
            t[1] = tValue
            uCookies[c] = t.join( '=' )
         }
      }
      if( found == -1 )
         uCookies[uCookies.length] = name + '=' + nValue
      uValue = uCookies.join( '&' )
   } else {
      uValue = name + '=' + nValue
   }
   // update real cookie
   SetCookie( uName, uValue, false)
}
//*-------------------------------------*
//*------- Fonction GetSubCookie -------*
//*-------------------------------------*
function GetSubCookie( uName, name )
{
   uValue = GetCookie( uName )
   if (uValue != null)
     {
     var flag = uValue.indexOf( name+'=' )
     if( flag != -1 ){
       flag += name.length + 1
       end = uValue.indexOf( '&', flag )
       if( end == -1 ) end = uValue.length
       return uValue.substring( flag, end )
     }
   }
}
//*------------------------------------*
//*------- Fonction ValideCombo -------*
//*------------------------------------*
function ValideCombo(theForm, theField, sL, sTitre)
{
  var argc=ValideCombo.arguments.length;
  if (argc == 3)
  {
  	var sTitre = "Champ Liste Deroulante";
  }
  if (theForm(theField).selectedIndex < 0)
  {
    alert("Sélectionnez une des options du "+sTitre+".");
    theForm(theField).focus();
    return (false);
  }
  return (true);
}

//################################
//######## Fonctions Dates #######
//################################

//*-----------------------------------*
//*------- Fonction ValideDate -------*
//*-----------------------------------*
function ValideDate(theForm, theField, sL, sTitre)
{
  var argc=ValideDate.arguments.length;
  if (argc == 3)
  {
  	var sTitre = "Date";
  }
  if (theForm(theField).value == "")
  {
    alert("Tapez une valeur (jj/mm/aaaa) dans le champ "+sTitre+".");
    theForm(theField).focus();
    return (false);
  }

  if (theForm(theField).value.length < 8)
  {
    alert("Tapez au moins 8 caractères dans le champ "+sTitre+".");
    theForm(theField).focus();
    return (false);
  }

  if (theForm(theField).value.length > 10)
  {
    alert("Tapez au plus 10 caractères dans le champ "+sTitre+".");
    theForm(theField).focus();
    return (false);
  }
  var checkOK = "0123456789-/";
  var checkStr = theForm(theField).value;
  var bCarDateD = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      bCarDateD = false;
      break;
    }
  }
  if (!bCarDateD)
  {
    alert("Ne tapez que  chiffre et \"/\" dans le champ "+sTitre+".");
    theForm(theField).focus();
    return (false);
  }

  if (ErrDate(theForm(theField)) == true)
  {
    return (false);
  }
  return (true);
}
//*--------------------------------*
//*------- Fonction ErrDate -------*
//*--------------------------------*
function ErrDate(champ)
{
  valcp=champ.value;
  if (valcp != "")
	{
  	msgerr=" Format des dates de type jj/mm/aaaa. Exemple 25/01/2007"
  	pos=valcp.indexOf("/")
  	if (pos==-1)
  	{
    	alert("Il manque un séparateur."+msgerr);
    	champ.focus();
    	return (true);
  	}
  	pos1=valcp.indexOf("/", pos+1)
  	if (pos1==-1)
  	{
    	alert("Il manque un séparateur."+msgerr);
    	champ.focus();
    	return (true);
  	}
	jour=valcp.substring(0,pos)
	mois=valcp.substring(pos+1,pos1)
	annee=valcp.substring(pos1+1,valcp.length)
	if (jour > 31){
		alert("Le jour n'est pas correct."+msgerr);
		champ.focus();
		return (true);
	}
	if (mois > 12){
		alert("Le mois n'est pas correct."+msgerr);
		champ.focus();
		return (true);
	}
	jour=parseInt(jour,10)
	mois=parseInt(mois,10)
	annee=parseInt(annee,10)
	if (isNaN(jour)){
		alert("Le jour n'est pas correct."+msgerr);
		champ.focus();
		return (true);
	}
	if (isNaN(mois)){
		alert("Le mois n'est pas correct."+msgerr);
		champ.focus();
		return (true);
	}
	if (isNaN(annee)){
		alert("L'année n'est pas correcte."+msgerr);
		champ.focus();
		return (true);
	}
    	tDate=new Date(annee,mois-1,jour)
    	annee=tDate.getFullYear()
    	mois=tDate.getMonth()+1
	if (mois < 10) {
		mois="0"+mois
	}
    	jour=tDate.getDate()
	if (jour < 10) {
		jour="0"+jour
	}
    	champ.value=jour+"/"+mois+"/"+annee
	}
return(false);
}
//*---------------------------------*
//*------- Fonction DateComp -------*
//*---------------------------------*
function DateComp(s1,s2)
{
   var dDat1 = new Date(s1.substring(6,10),s1.substring(3,5),s1.substring(0,2));
   if (s2 == 1)
   {
     var dDat2 = new Date();
     dDat2.setMonth(dDat2.getMonth()+1);
   }
   else
   {
     var dDat2 = new Date(s2.substring(6,10),s2.substring(3,5),s2.substring(0,2));
   }
   var v1 = dDat1.valueOf();
   var v2 = dDat2.valueOf();
   //Comparaison des dates 
   if (v1 < v2)
   {
      return (1);	// d1 < d2
   }
   else
   {
      if (v1 == v2)
      {
        return (2);	// d1 = d2
      }
      else
      {
      	return (3); // d1 > d2
      }
   }
}

//*-----------------------------*
//*------- Fonction fAge -------*
//*-----------------------------*
function fAge(dN, dJ, iType)
// Calcule l'age révolu d'une personne à partir de sa date de naissance (dN)
// par rapport à une date de référence (dJ)
{
	if (iType == 1) 
		// Date au format francais (jj/mm/aaaa)
		{
		var tD = new Array(2);
		tD = dN.split("/");
		dN = tD[1]+"/"+tD[0]+"/"+tD[2]; 
		tD = dJ.split("/");
		dJ = tD[1]+"/"+tD[0]+"/"+tD[2];
		}
	var iAge = (Date.parse(dJ)-Date.parse(dN))/(3600000*24*365);
	if (iAge > -1)
		{
		return(parseInt(iAge.toString(10).split(".")[0]));
		}
	else
		{
			return (0);
		}
}
//*-------------------------------------*
//*------- Fonction fValideChamp -------*
//*-------------------------------------*

function fValideChamp(Fld, sTitre, iMin, iMax, iType, sL, sCarSuppl)
{
  //iType = 0 : ALPHABETIQUE
  //iType = 1 : ALPHANUMERIQUE
  //iType = 2 : NUMERIQUE
  //iType = 3 : MONTANT
  //iType = 4 : DATE
  //iType = 5 : ADRESSE E-MAIL
  var argc = fValideChamp.arguments.length;
  if (argc == 6)
  {
  	var sCarSuppl = "";
  }
  if (Fld.value.length < iMin)
  {
  	alert("Vous devez saisir au moins "+iMin+" car. dans "+sTitre+"...");
  	Fld.focus();
  	return (false);
  }
  if (Fld.value.length > iMax)
  {
  	alert("Maxi "+iMax+" caractères pour "+sTitre+"...");
  	Fld.focus();
  	return (false);
  }
  if (iType == 0)
  {
    // ### TYPE ALPHABETIQUE
    var sOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ- \t\r\n\f"+sCarSuppl;
	var sMsg = "Ne tapez que des lettres, espaces et \"-\" dans "+sTitre+"."
  }
  if (iType == 1)
  {
    // ### TYPE ALPHANUMERIQUE
    var sOK = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ- \t\r\n\f"+sCarSuppl;
	var sMsg = "Ne tapez que des lettres, chiffres, espaces et \"-\" dans "+sTitre+"."
  }
  if (iType == 2)
  {
    // ### TYPE NUMERIQUE PUR
    var sOK = "0123456789"+sCarSuppl;
	var sMsg = "Ne tapez que des chiffres dans "+sTitre+"."
  }
  if (iType == 3)
  {
    // ### TYPE MONTANT
    var sOK = "0123456789,-+"+sCarSuppl;
	var sMsg = "Ne tapez que des chiffres, virgules et signes dans "+sTitre+"."
  }
  if (iType == 4)
  {
    // ### TYPE DATE
    var sOK = "0123456789/"+sCarSuppl;
	var sMsg = "Ne tapez que des chiffres et \"/\" dans "+sTitre+"."
  }
  if (iType == 5)
  {
    // ### TYPE E-MAIL
    var sOK = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@-_."+sCarSuppl;
	var sMsg = "Ne tapez que des lettres, chiffres, \"-\", \"_\", \"@\" et \".\" dans "+sTitre+"."
  }
  // Controle des caractères contenus dans le champ pour tous types
  var vVal = Fld.value;
  var bValid = true;
  for (I = 0;  I < vVal.length;  I++)
  {
    sC = vVal.charAt(I);
    for (J = 0;  J < sOK.length;  J++)
      if (sC == sOK.charAt(J))
        break;
    if (J == sOK.length)
    {
      bValid = false;
      break;
    }
  }
  if (!bValid)
  {
    alert(sMsg);
    Fld.focus();
    return (false);
  }
  if (iType == 4)
  {
    // ### TYPE DATE (Focus et alert effectués par ValideDate)
    if (ValideDate(Fld.form, Fld.name, sL, sTitre) == false)
      return (false);
  }
  if (iType == 5)
  {
  	// ### TYPE E-MAIL
	var bTest = false;
	for (J = 1; J < Fld.value.length; J++)
	{
	  if (Fld.value.charAt(J) == '@')
	  { 
	    if (J < (Fld.value.length - 4))
	    { 
	      for (K = J; K < Fld.value.length - 2; K++)
	      { 
	        if (Fld.value.charAt(K) == '.')
	        {
	          bTest = true; 
	          break;
	        }
	      } 
	    } 
	  } 
	} 
	if (bTest == false)
	{
	alert("Le format de "+sTitre+" est incorrect.");
	Fld.focus();
	return (false); 
	}
  }
  return (true);
}
//##################################
//######## Fonctions Session #######
//##################################

//*--------------------------------------*
//*------- Fonction lBeforeUnLoad -------*
//*--------------------------------------*

function lBeforeUnLoad(sChemin)
{
  //######## TEST SI UNLOAD PAR PROGRAMME SINON ABANDON SESSION #######
  if (sChemin != null && sChemin != "")
  {
    if (GetSubCookie("Global","Chainage") != "Oui" && GetSubCookie("Global","Chainage") != "XXX")
  	{
  	  return ("Cette action va annuler la réservation en cours...");
    }
  }
}

//*--------------------------------*
//*------- Fonction lUnLoad -------*
//*--------------------------------*

function lUnLoad(sChemin)
{
  //######## TEST SI UNLOAD PAR PROGRAMME SINON ABANDON SESSION #######
  if (sChemin != null && sChemin != "")
  {
    if (GetSubCookie("Global","Chainage") != "Oui")
  	{
      SetSubCookie("Global","Chainage","XXX");
      window.navigate(sChemin);
    }
  }
}
