/* Fonction globales */
resizeWindow = function (){
	if ( navigator.appName == "Netscape" ){
		window.resizeTo(1005, screen.height);
	}

	if ( navigator.appName == "Microsoft Internet Explorer" ){
		window.resizeTo(1019, screen.height);
	}
}

fAjouterFavoris = function (src, title){
	if ( navigator.appName != 'Microsoft Internet Explorer' ){
		alert("Pour ajouter cette page en favoris,\nVeuillez appuyer sur la touche control + D");
		//window.sidebar.addPanel(title,src,"");
	}else{
		window.external.AddFavorite(src,title);
	}
}


fChargement = function (){
	scroll(0,0);
	$('overlay').show();
	$('chargement').show();
}

fCloseChargement = function (){
	$('overlay').hide();
	$('chargement').hide();
}

fLien = function (iIdMenu){
	sUrl = "scripts/saveValue.php";
	sQuery = "iModeAction=3";
	sQuery += "&iIdMenu="+encodeURIComponent(iIdMenu);
	var myAjax = new Ajax.Request(sUrl,{method:'post',
									   	postBody:sQuery,
									    evalScripts:true,
										onCreate:fChargement,
										onComplete:fPage
	});
}

fMenu = function (iIdMenu, iMenuParent, iLangue){
	sUrl = "scripts/saveValue.php";
	sQuery = "iModeAction=1";
	sQuery += "&iMenu="+encodeURIComponent(iIdMenu)+"&iMenuParent="+encodeURIComponent(iMenuParent)+"&iLangue="+encodeURIComponent(iLangue);
	var myAjax = new Ajax.Request(sUrl,{method:'post',
									   	postBody:sQuery,
									    evalScripts:true,
										onCreate:fChargement,
										onComplete:function (xhr){
											if(xhr.responseText != ""){
												document.location.href=xhr.responseText;
											}else {
												fPage();
											}
										}
	});
}

fPage = function (){
	sUrl = "scripts/page.php";
	sQuery = "";
	var oRqt = new Ajax.Updater("page", sUrl, {method:'post',
											   postBody:sQuery,
											   evalScripts:true,
											   onComplete:fCloseChargement,
											   onSuccess:fCloseChargement
	});
}

fLogout = function (){
	sUrl = "scripts/saveValue.php";
	sQuery = "iModeAction=2&bLogout=1";
	var myAjax = new Ajax.Request(sUrl,{method:'post',
										postBody:sQuery,
										onComplete:function(xhr){
											document.location.href=xhr.responseText;
										}
	})
}

fMessage = function (iIdDiv, iIdMsg, sClass, iIdChamp, sCouleurBorder){
	if(isNaN(iIdMsg)){
		$(iIdDiv).innerHTML = iIdMsg;
	}else {
		sUrl = "scripts/libelle.php";
		sQuery = "iLibelle="+iIdMsg;
		var myAjax = new Ajax.Updater(iIdDiv, sUrl, {method: 'post',evalScripts:true,postBody:sQuery});
	}
	$(iIdDiv).className=sClass;
	$(iIdDiv).style.display='block';

	if(iIdChamp != ''){
		$(iIdChamp).style.border=sCouleurBorder;
		$(iIdChamp).activate();
	}
	setTimeout("fCacherMessage('"+iIdDiv+"')", 10000);

}

fRechercher = function (){
	if($F('sRecherche') == ""){
		return false;
	}
	return true;
}

fNumerique = function (sChaine){
	var exp = new RegExp("^[0-9]+$","g");
	return exp.test(sChaine);
}

fCheckDateValue = function (sDate, sSplit){
	if(sSplit == ""){
		sSplit = "/";
	}
	var sLaDate = sDate.split(sSplit);
	if ((sLaDate.length != 3) || isNaN(parseInt(sLaDate[0])) || isNaN(parseInt(sLaDate[1])) || isNaN(parseInt(sLaDate[2]))){
		return false;
	}else{
		var sUneDate = new Date(eval(sLaDate[2]),eval(sLaDate[1])-1,eval(sLaDate[0]));
		var iAnnee = sUneDate.getYear();
		if ((Math.abs(iAnnee)+"").length < 4){
			iAnnee = iAnnee + 1900;
		}
		if(sUneDate.getDate() == eval(sLaDate[0]) && (sUneDate.getMonth() == eval(sLaDate[1])-1) && (iAnnee == eval(sLaDate[2]))){
			return true;
		}else{
			return false;
		}
	}
}

String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

fValideChamps = function (sDiv){
	$(sDiv).style.border="1px solid #99ff99";
}

/* FAQ */
fFaq = function (sDivId){
	$(sDivId).toggle();
}

function fFaqToggle(oElement, sTag) {
	var oFAQ = document.getElementById('iIdFaq');
	var oQuestion = oFAQ.parentNode.getElementsByTagName('li');
	var iQuestion = 0;
	var bQuestion = false;
	while (oQuestion[iQuestion]) {
		if (oQuestion[iQuestion] == oElement) {
			bQuestion = true;
			oQuestion[iQuestion].parentNode.getElementsByTagName('div')[iQuestion].style.display='block';
		}else {
			oQuestion[iQuestion].parentNode.getElementsByTagName('div')[iQuestion].style.display='none';
		}
		iQuestion++;
	}
}


fAfficherFAQ = function (){
	sUrl = "modules/faq/index.php";
	sQuery = "";
	var oRqt = new Ajax.Updater("textePage", sUrl, {method:'post',
											   postBody:sQuery,
											   evalScripts:true
	});
}


fInscriptionNewsletter = function (){
	if($F('sEmailInscription') != ""){
		alert("Script d'inscription");
	}else {
		fMessage("iIdMsgNewsletter", "Veuillez saisir votre adresse e-mail !", "msgErreur", "sEmailInscription", "1px solid #ff4646");
	}
}

fCheckContact = function (oForm){
	var iErreur = 0;
	var sNomOld = "";
	var iNombreChamps = oForm.length;
	var sQuery = "bEnvoiForm=1";
	for (var iCompteur = 0; iCompteur < iNombreChamps; iCompteur++) {
		//Type d'ŽlŽments
		var sType = oForm.elements[iCompteur].type;
		var sNom = "";
		var sValeur = "";
		switch (sType){
			case "text" :
				var sNom = oForm.elements[iCompteur].name;
				var sValeur = oForm.elements[iCompteur].value;
				break;
			case "textarea" :
				var sNom = oForm.elements[iCompteur].name;
				var sValeur = oForm.elements[iCompteur].value;
				break;
			case "checkbox" :
				var sNom = oForm.elements[iCompteur].name;
				var sValeur = "";
				if(oForm.elements[iCompteur].checked){
					sValeur += oForm.elements[iCompteur].value;
				}
				break;
			case "radio" :
				if(oForm.elements[iCompteur].checked){
					if(oForm.elements[iCompteur].name != sNomOld){
						var sNom = oForm.elements[iCompteur].name;
						var sValeur = oForm.elements[iCompteur].value;
						sNomOld = sNom;
					}

				}
				break;
			case "select-one" :
				var sNom = oForm.elements[iCompteur].name;
				var sValeur = oForm.elements[iCompteur].options[oForm.elements[iCompteur].selectedIndex].value;
				break;
			case "hidden" :
				var sNom = oForm.elements[iCompteur].name;
				var sValeur = oForm.elements[iCompteur].value;
				break;
		}

		if(oForm.elements[iCompteur].className == "obligatoire" && sValeur == "" && sType != "radio"){
			fMessage("iIdMessageForm", "Veuillez renseigner les champs obligatoires !", "msgErreur", oForm.elements[iCompteur].id, "1px solid #ff0000");
			iErreur = iErreur + 1;
			return false;
		}else {
			if(sType != "undefined" && sType != "submit" && sType != "reset" && sNom != "" && sValeur != ""){
				sQuery += "&"+sNom+"="+encodeURIComponent(sValeur);
			}
			if(oForm.elements[iCompteur].className == "obligatoire"){
				oForm.elements[iCompteur].style.border = "1px solid #cccccc";
				$('iIdMessageForm').hide();
			}
		}
    }

    if(iErreur == 0){
    	var sUrl = "modules/formulaires/traitements.php";
    	var oRqt = new Ajax.Request(sUrl, {method:'post',
	    								   postBody:sQuery,
	    								   evalScripts:true,
	    								   onComplete : function (xhr){
	    								   		oForm.reset();
	    								   		var aMsg = xhr.responseText.split("|");
	    								   		fMessage("iIdMessageForm", aMsg[0], aMsg[1], "", "");
	    								   		scroll(0,0);
	    								   }
		});
    }else {
    	fMessage("iIdMessageForm", 84, "msgErreur", "", "");
    	return false;
    }
}

fCacherMessage = function (iId){
	$(iId).hide();
}


fInscriptionNewsletter = function (){
	if($F('sEmailInscription') != ""){
		var sUrl = "modules/newsletters/inscription.php";
		var sQuery = "sEmail="+encodeURIComponent($F('sEmailInscription'));
		var oRqt = new Ajax.Request(sUrl, {method:'post',
	    								   postBody:sQuery,
	    								   evalScripts:true,
	    								   onComplete : function (xhr){
	    								   		var aMsg = xhr.responseText.split("|");
	    								   		if(aMsg[1] != "msgErreur"){
	    								   			$('sEmailInscription').value = "";
	    								   		}
	    								   		fMessage("iIdMsgNewsletter", aMsg[0], aMsg[1], "", "");
	    								   		scroll(0,0);
	    								   }
		});
	}else {
		fMessage("iIdMsgNewsletter", 26, "msgErreur", "", "");
    	return false;
	}
}

openElement = function (sElement){
	var oElement = $$(".elementToggle");
	for ( var i = 0; i < oElement.length; i++ ){
		oElement[i].hide();
	}
	$(sElement).show();
}


/* Authentification */
fAuthentification = function (){
	if($F('iIdUser') == ""){
		fMessage("message", 1, "msgErreur", "iIdUser", "1px solid #ff0000");
		return false;
	}

	if($F('iIdPass') == ""){
		fMessage("message", 2, "msgErreur", "iIdPass", "1px solid #ff0000");
		return false;
	}
	fPrepareUrlLogin("scripts/auth.php");
}

fPrepareUrlLogin = function (sUrl){
	sQuery = "";
	pass = $("iIdPass").value
	buf=MD5(pass);
	sQuery += $("md5").name+"="+
	encodeURIComponent(buf)+"&";
	sQuery += $("iIdUser").name+"="+
	encodeURIComponent($("iIdUser").value);
	var myAjax = new Ajax.Request(sUrl,{method:'post',postBody:sQuery,onComplete:fAuthReponse})
}

fAuthReponse = function (xhr){
	if(xhr.readyState == 4){
		if(xhr.status == 200){
			var sReponse = xhr.responseText;
			if(sReponse == "false"){
				fMessage("message", 3, "msgErreur", "iIdUser", "1px solid #ff0000");
				$('iIdUser').focus();
			}else{
				if(sReponse == "Loginfalse"){
					fMessage("message", 3, "msgErreur", "iIdUser", "1px solid #ff0000");
				}else{
					document.location.href=sReponse;
				}
			}
		}
	}
}


var hex_chr = "0123456789abcdef";
function rhex(num){
  str = "";
  for(j = 0; j <= 3; j++)
    str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) +
           hex_chr.charAt((num >> (j * 8)) & 0x0F);
  return str;
}

function str2blks_MD5(str){
  nblk = ((str.length + 8) >> 6) + 1;
  blks = new Array(nblk * 16);
  for(i = 0; i < nblk * 16; i++) blks[i] = 0;
  for(i = 0; i < str.length; i++)
    blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
  blks[i >> 2] |= 0x80 << ((i % 4) * 8);
  blks[nblk * 16 - 2] = str.length * 8;
  return blks;
}

function add(x, y){
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

function rol(num, cnt){
  return (num << cnt) | (num >>> (32 - cnt));
}

function cmn(q, a, b, x, s, t){
  return add(rol(add(add(a, q), add(x, t)), s), b);
}

function ff(a, b, c, d, x, s, t){
  return cmn((b & c) | ((~b) & d), a, b, x, s, t);
}

function gg(a, b, c, d, x, s, t){
  return cmn((b & d) | (c & (~d)), a, b, x, s, t);
}

function hh(a, b, c, d, x, s, t){
  return cmn(b ^ c ^ d, a, b, x, s, t);
}

function ii(a, b, c, d, x, s, t){
  return cmn(c ^ (b | (~d)), a, b, x, s, t);
}

function MD5(str){
  x = str2blks_MD5(str);
  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  for(i = 0; i < x.length; i += 16){
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = ff(c, d, a, b, x[i+10], 17, -42063);
    b = ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = ff(d, a, b, c, x[i+13], 12, -40341101);
    c = ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = ff(b, c, d, a, x[i+15], 22,  1236535329);

    a = gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = gg(c, d, a, b, x[i+11], 14,  643717713);
    b = gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = gg(c, d, a, b, x[i+15], 14, -660478335);
    b = gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = gg(b, c, d, a, x[i+12], 20, -1926607734);

    a = hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = hh(b, c, d, a, x[i+14], 23, -35309556);
    a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = hh(d, a, b, c, x[i+12], 11, -421815835);
    c = hh(c, d, a, b, x[i+15], 16,  530742520);
    b = hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = ii(c, d, a, b, x[i+10], 15, -1051523);
    b = ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = ii(d, a, b, c, x[i+15], 10, -30611744);
    c = ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = add(a, olda);
    b = add(b, oldb);
    c = add(c, oldc);
    d = add(d, oldd);
  }
  return rhex(a) + rhex(b) + rhex(c) + rhex(d);
}

//MODULES EXTERNES => CALENDRIER
var oldLink = null;
// code to change the active stylesheet
function setActiveStyleSheet(link, title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (oldLink) oldLink.style.fontWeight = 'normal';
  oldLink = link;
  link.style.fontWeight = 'bold';
  return false;
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    // if we add this call we close the calendar on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    cal.callCloseHandler();
}

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

  	if($('iIdMessageFond')){
  		$('iIdMessageFond').hide();
	}
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, format, showsTime, showsOtherMonths) {
	if($('iIdMessageFond')){
  		$('iIdMessageFond').show();
	}

  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

// If this handler returns true then the "date" given as
// parameter will be disabled.  In this example we enable
// only days within a range of 10 days from the current
// date.
// You can use the functions date.getFullYear() -- returns the year
// as 4 digit number, date.getMonth() -- returns the month as 0..11,
// and date.getDate() -- returns the date of the month as 1..31, to
// make heavy calculations here.  However, beware that this function
// should be very fast, as it is called for each day in a month when
// the calendar is (re)constructed.
function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");

  // construct a calendar giving only the "selected" handler.
  var cal = new Calendar(0, null, flatSelected);

  // hide week numbers
  cal.weekNumbers = false;

  // We want some dates to be disabled; see function isDisabled above
  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%A, %B %e");

  // this call must be the last as it might use data initialized above; if
  // we specify a parent, as opposite to the "showCalendar" function above,
  // then we create a flat calendar -- not popup.  Hidden, though, but...
  cal.create(parent);

  // ... we can show it here.
  cal.show();
}
