// JavaScript Document

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_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;
   if (isIE)
   {
	   x.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + x.oSrcAlph + ",sizingMethod='scale')";	  
   }
  }
}

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; 
	   if (isIE)
	   {
		   x.oSrcAlph = x.srcAlpha;	  
	   }
   }
   x.src=a[i+2];}
   if (isIE)
	   correctPNG();
}


var navigateur;
var ns4 = (document.layers)? true:false;
var ie4 = (document.all)? true:false;
var dom = (document.getElementById)? true:false;
if (ie4)
	navigateur	=	"ie";
else if (ns4)
	navigateur	=	"ns";
else if (dom)
	navigateur	=	"dom";

function getRandomNum(lbound, ubound)
{
	return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}

function getRandomChar(number, lower, upper, other, extra)
{
	var numberChars = "0123456789";
	var lowerChars = "abcdefghijklmnopqrstuvwxyz";
	var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
	var charSet = extra;
	if (number == true)
	charSet += numberChars;
	if (lower == true)
	charSet += lowerChars;
	if (upper == true)
	charSet += upperChars;
	if (other == true)
	charSet += otherChars;
	return charSet.charAt(getRandomNum(0, charSet.length));
}

function getPassword(length, extraChars, firstNumber, firstLower, firstUpper, firstOther, latterNumber, latterLower, latterUpper, latterOther)
{
	var rc = "";
	if (length > 0)
		rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars);
	for (var idx = 1; idx < length; ++idx)
	{
		rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars);
	}
	return rc;
}

function MM_openBrWindow(theURL,winName,features)
{
	if (features == "")
	{
		features	=	'center';
		features	+=	',width=500';
		features	+=	',height=500';
		features	+=	',scrollbars=yes';
	}
	
	if (features.substring(0, 7) == "center,")
	{
		features	=	features.substring(7);
		var index_width		=	features.indexOf("width=");
		var index_height	=	features.indexOf("height=");
		var win_width, win_height, win_left, win_top;
		
		if (index_width == -1)
		{
			win_width	=	500;
			alert("pas de 'width'");
		}
		else
		{
			var index_right		=	index_width + 6;
			
			if (features.indexOf(",", index_right) == -1)
				win_width	=	features.substring(index_right);
			else
				win_width	=	features.substring(index_right, features.indexOf(",", index_right));
		}
		
		if (index_height == -1)
		{
			win_height	=	500;
			alert("pas de 'height'");
		}
		else
		{
			var index_right		=	index_height + 7;
			if (features.indexOf(",", index_right) == -1)
				win_height	=	features.substring(index_right);
			else
				win_height	=	features.substring(index_right, features.indexOf(",", index_right));
		}
		
		win_left	=	(screen.width - win_width) / 2;
		win_top		=	(screen.height - win_height) / 2;
		features	+=	',left=' + win_left + ',top=' + win_top;
	}
	
	window.open(theURL,winName,features);
}

function thematiqueOn(nomCase)
{
	maCase = MM_findObj(nomCase);
	maCase.className = 'fondMenuThematiqueOn';
}

function thematiqueOff(nomCase)
{
	maCase = MM_findObj(nomCase);
	maCase.className = 'fondMenuThematique';
}

function MM_validateForm() { //v4.0
	var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments, amin=1500, amax=2050, separateur="/";
	
	if (args.length == 1)
		if (typeof(args[0]) == "object")
			args	=	args[0];

	for (i=0; i<(args.length-2); i+=3)
	{
		test	=	args[i+2];
		val		=	MM_findObj(args[i]);
		
		if (test == "RSup0")
		{
			if (val.value == 0)
				errors += '- '+args[i+1]+' est obligatoire.\n';
		}
		if (test == "REnum")
		{
			isSelected	=	false;
			for(j=0; j<100; j++) 
				if (val[j])
					if (val[j].checked == true)
						isSelected	=	true;
			if (!isSelected)
				errors += '- '+args[i+1]+' est obligatoire.\n';
		}
		else
		{
			if (val)
			{
				nm=val.name;
				if ((val=val.value)!="")
				{
					if (test.indexOf('isEmail')!=-1)
					{
						p=val.indexOf('@');
						if (p<1 || p==(val.length-1))
							errors+='- '+args[i+1]+' doit être une adresse EMail.\n';
					}
					else if (test.indexOf('isDate')!=-1)
					{
						//****************
						/*
						var d=val;
						var j=(d.substring(0,2));
						var m=(d.substring(3,5));
						var a=(d.substring(6));
						var ok=1;
						if ( ((isNaN(j))||(j<1)||(j>31)) && (ok==1) ) { 
						errors += '- '+args[i+1]+' (Le jour n\'est pas correct).\n'; ok=0; }
						if ( ((isNaN(m))||(m<1)||(m>12)) && (ok==1) ) { 
						errors += '- '+args[i+1]+' (Le mois n\'est pas correct).\n'; ok=0;  }
						if ( ((isNaN(a))||(a<amin)||(a>amax)) && (ok==1) ) { 
						errors += '- '+args[i+1]+' (Le année n\'est pas correct).'; ok=0;  }
						if ( ((d.substring(2,3)!=separateur)||(d.substring(5,6)!=separateur)) 
						&& (ok==1) ) { errors += '- '+args[i+1]+' (Les séparateurs doivent être des \''+separateur+'\').\n'; ok=0; }
						if (ok==1)
						{ 
							var d2=new Date(a,m-1,j); 
							j2=d2.getDate();
							m2=d2.getMonth()+1;
							a2=d2.getYear();
							
							//if (a2<=100) {a2=1900+a2;}
							a2=1900+a2;
							//alert("Jours : "+j+" - "+j2+"\nMois : "+m+" - "+m2+"\nAnnée : "+a+" - "+a2+"\n");
							if ( (j!=j2)||(m!=m2)||(a!=a2) )
							{
								//alert("nono");
								errors += '- '+args[i+1]+' (La date \''+d+'\' n\'existe pas !).\n';
							} 
						} 
						*/
						//****************
					}
					else if (test.indexOf('isHeure')!=-1)
					{
						/*
						var	heure_complet	=	val.explode(":");
						var heure			=	heure_complet[0];
						if ((isNaN(heure)) || (heure<0)||(heure>23))
							errors += '- '+args[i+1]+' (L\'heure n\'est pas correct).\n';
						if (heure_complet.length == 2)
						{
							var minute			=	heure_complet[1];
							if ((isNaN(minute)) || (minute<0)||(minute>59))
								errors += '- '+args[i+1]+' (Les minutes ne sont pas correct).\n';
						}
						if (heure_complet.length == 3)
						{
							var seconde			=	heure_complet[2];
							if ((isNaN(seconde)) || (seconde<0)||(seconde>59))
								errors += '- '+args[i+1]+' (Les secondes ne sont pas correct).\n';
						}
						*/
					}
					else if (test!='R')
					{
						num = parseFloat(val);
						if (isNaN(val))
							errors+='- '+args[i+1]+' doit être un nombre.\n';
						if (test.indexOf('inRange') != -1)
						{
							p=test.indexOf(':');
							min=test.substring(8,p);
							max=test.substring(p+1);
							if (num<min || max<num)
								errors+='- '+args[i+1]+' must contain a number between '+min+' and '+max+'.\n';
						}
					}
				}
				else if (test.charAt(0) == 'R')
					errors += '- '+args[i+1]+' est obligatoire.\n';
			}
		}
	}
  
  if (errors)
  	alert(errors);
  document.MM_returnValue = (errors == '');
}

String.prototype.explode = function(aStringSeparateur)
{
	var aString			=	this.trim();
	if (aString == "")
		return -1;
	var	lst_occurence	=	new Array();
	var old_index		=	0;
	var	current_index	=	0;
	
	for(i=0;i<aString.length;i++)
	{
		if (aString.substr(i, aStringSeparateur.length) == aStringSeparateur)
		{
			lst_occurence.push(aString.substring(old_index, i));
			old_index		=	i + aStringSeparateur.length;
		}
	}
	lst_occurence.push(aString.substr(old_index));
	/*
	if (lst_occurence.length == 1)
		return lst_occurence[0];
	else
		return lst_occurence;
	*/
	return lst_occurence;
}

String.prototype.sans_accents = function()
{
	var ch = this.replace(/é|è|ê|ë/g, "e");
	ch = ch.replace(/&agrave;|â|ä/g, "a");
	ch = ch.replace(/ç/g, "c");
	ch = ch.replace(/î|ï/g, "i");
	ch = ch.replace(/ô|ö/g, "o");
	ch = ch.replace(/ù|û|ü/g, "u");
	ch = ch.replace(/&agrave;|Â|Ä|Å/g, "A");
	ch = ch.replace(/Ç/g, "C");
	ch = ch.replace(/É|È|Ê|Ë/g, "E");
	ch = ch.replace(/Ô|Ö/g, "O");
	ch = ch.replace(/Ù|Û|Ü/g, "U");
	return ch;
}

String.prototype.trim = function()
{
	return this.replace(/ /g, "");
}

function show_div(aDivId)
{
	if (document.getElementById(aDivId).style.display == "none")
		document.getElementById(aDivId).style.display = "";
	else
		document.getElementById(aDivId).style.display = "none";
}

function show_div_etat(aDivId, aEtat)
{
	if (aEtat)
		document.getElementById(aDivId).style.display = "";
	else if (!aEtat)
		document.getElementById(aDivId).style.display = "none";
}

var old_rubrique	=	"div_salle_reception";
function show_rubrique(aRubriqueId)
{
	if (old_rubrique != null)
	{
		document.getElementById(old_rubrique).style.display	=	"none";
	}
	old_rubrique	=	aRubriqueId;
	document.getElementById(aRubriqueId).style.display	=	"";
}

// Upload ******
function update_div(aPage, aType, aId, aDivListName, aUploadPath, aUnique)
{
	xajax_update_div(aPage, aType, aId, aDivListName, aUploadPath, aUnique);
}
function supprimer_fichier(aPage, aType, aId, aDivListName, aUploadPath, aUnique, aFichierId)
{
	xajax_supprimer_fichier(aPage, aType, aId, aDivListName, aUploadPath, aUnique, aFichierId);
}
// Fin Upload **


