function urlencode (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
 
    var hexStr = function (dec) {
        return '%' + dec.toString(16).toUpperCase();
    };
 
    var ret = '',
            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();
 
    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}

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 n3_swapClass(theID,cssStyle,cssStyleOther) {
	if(!document.getElementById){return};
	var theElement = document.getElementById(theID);
	if (theElement.className == cssStyle) {
		theElement.className = cssStyleOther;
	} else {
		theElement.className = cssStyle;
	}
}

function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable,scrollbars'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function


function getSelectedButton(buttonGroup){
	for (var i = 0; i < buttonGroup.length; i++) {
		if (buttonGroup[i].checked) {
			return i
		}
	}
	return 0
}

function getSelectedButton1(buttonGroup){
	alert(buttonGroup.length);
	for (var i = 0; i < buttonGroup.length; i++) {
		if (buttonGroup[i].checked) {
			return i
		}
	}
	return 99
}


function checkMail(str) {
	var verif = /^[a-zA-Z0-9\-\_]{1,}[a-zA-Z0-9\.\-\_]*[a-zA-Z0-9\-\_]{1,}@[a-zA-Z0-9]{1,}[a-zA-Z0-9\.\-\_]*[a-zA-Z0-9]{1,}[.][a-zA-Z]{2,4}$/;
	if(verif.test(str)) return true;
	else return false;
}

function emptyField(textObj)
{
	if (textObj.value.length == 0) return true;
	for (var i=0; i<textObj.value.length; ++i) {
		var ch = textObj.value.charAt(i);
		if (ch != ' ' && ch != '\t') return false;
	}
	return true;
}


function validateDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy


PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()

   method such as the date separator character.
*************************************************/
  var objRegExp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/

  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return 1; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(4,5) //find date separator
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[2]);

    //check if month value and day value agree
    if(arrayLookup[arrayDate[1]] != null) {
      if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0)

        return 99; //found in lookup table, good date
    }

    //check for February
    var intYear = parseInt(arrayDate[0]);
    var intMonth = parseInt(arrayDate[1]);
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      return 99; //Feb. had valid number of days
  }
  return 2; //any other values, bad date
}

function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function isPosInteger (inputVal) {
	inputStr = inputVal.toString()
	for (var i=0; i<inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") {
			return false;
		}
	}
	return true;
}

function GeneratePassword(length) {

    if (parseInt(navigator.appVersion) <= 3) {
        alert("Sorry this only works in 4.0+ browsers");
        return true;
    }

    var sPassword = "";

    var noPunction = true;
    for (i=0; i < length; i++) {

        numI = getRandomNum();
        if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }

        sPassword = sPassword + String.fromCharCode(numI);
    }


    return sPassword;
}

function getRandomNum() {

    // between 0 - 1
    var rndNum = Math.random()

    // rndNum from 0 - 1000
    rndNum = parseInt(rndNum * 1000);

    // rndNum from 33 - 127
    rndNum = (rndNum % 94) + 33;

    return rndNum;
}

function checkPunc(num) {

    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }

    return false;
}

function bio(employe_id) {
	var str = '';
	str += '&employe_id=' + employe_id;
	var options = {};
	options.postBody = str;
	var ar = new Ajax.Request('php/bio.php?from_ajax=1&no_cache=' + Math.random(), options);
	ar.options.onSuccess = function (originalRequest){
		var msg=originalRequest.responseText;
		//console.debug(msg);
		$('biographie').innerHTML=msg;
		Element.show('biographie');
			
		}.bind(this);
		
		ar.options.onFailure = function(){ alert("Erreur, le serveur n'a pas rï¿½pondu") };
	//alert (str);
}

function suggestion(secteur, livre, champ) {
	var str = '';
	str += '&sid=' + secteur + '&lid=' + livre + '&champ=' + champ;
	var options = {};
	options.postBody = str;
	var ar = new Ajax.Request('php/suggestion.php?from_ajax=1&no_cache=' + Math.random(), options);
	ar.options.onSuccess = function (originalRequest){
		var msg=originalRequest.responseText;
		//alert(msg);
		//console.debug(msg);
		/*$('biographie').update(msg);
		Element.show('biographie');*/
		$(champ).innerHTML=msg;
		}.bind(this);
		
		ar.options.onFailure = function(){ alert("Erreur, le serveur n'a pas r?pondu") };
	//alert (str);
}


var statusmsg=""

function hidestatus(){
window.status=statusmsg
return true
}

function chg_cal_news(str_date) {
	var str = '';
	str += '&date=' + str_date;
	var options = {};
	options.postBody = str;
	var au = new Ajax.Updater({ success: 'calendar' }, 'php/chg_cal_news.php?from_ajax=1&no_cache=' + Math.random(), options);
	//var ar = new Ajax.Request('php/chg_cal.php?from_ajax=1&no_cache=' + Math.random(), options);
	//alert (str);
}

function chg_cal(str_date) {
	var str = '';
	str += '&date=' + str_date;
	var options = {};
	options.postBody = str;
	var au = new Ajax.Updater({ success: 'calendar' }, 'php/chg_cal.php?from_ajax=1&no_cache=' + Math.random(), options);
	//var ar = new Ajax.Request('php/chg_cal.php?from_ajax=1&no_cache=' + Math.random(), options);
	//alert (str);
}

function chg_news(str_date) {
	var str = '';
	str += '&date=' + str_date;
	var options = {};
	options.postBody = str;
	var au = new Ajax.Updater({ success: 'news' }, 'php/chg_news.php?from_ajax=1&no_cache=' + Math.random(), options);
	var lau = new Ajax.Updater({ success: 'contenu_liens' }, 'php/chg_lnk.php?from_ajax=1&no_cache=' + Math.random(), options);
	//var ar = new Ajax.Request('php/chg_cal.php?from_ajax=1&no_cache=' + Math.random(), options);
	//alert (str);
}

function filtre_evenement(filtre) {
	var str = '';
	str += '&categorie=' + filtre;
	var options = {};
	options.postBody = str;
	var au = new Ajax.Updater({ success: 'filtre' }, 'php/chg_filtre.php?from_ajax=1&no_cache=' + Math.random(), options);
}

function recherche() {
	var str = document.search.kw.value;
	if (str.length <= 3) {
		alert("Vous devez entrer un mot-clé de 4 caractères ou plus !\n");
	} else {
		document.search.submit();
	}
}

function swapcolor1(a) { 
  a.bgColor='#FFFFFF';
  a.style.background='#FFFFFF';
  a.style.color='#000000';
  a.color="#000000";
}

function swapcolorback1(a) { 
  a.bgColor='#FFFFFF';
  a.style.cursor='default';
  a.style.color='#838383';
  a.color="#838383";
}
