/*
 *   Scroll below for behavioral events attatched to all pages
 */

function popupViewer(theImage) {
     // The viewer resizes itself automaticalle
     day = new Date();
     id = day.getTime();
     eval("page"+id+" = window.open('viewer.php?image="+theImage+"' , '"+ id +"', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=200,height=200,left=200,top=150');");
}

function popupImprimer(url) {
     // The viewer resizes itself automaticalle
     day = new Date();
     id = day.getTime();
     eval("page"+id+" = window.open('"+url+"' , '"+ id +"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=690,height=550,left=20,top=200');");
}

function openWin(url,width,height) {
     // The viewer resizes itself automaticalle
     day = new Date();
     id = day.getTime();
     eval("page"+id+" = window.open('"+url+"' , '"+ id +"', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+",left=200,top=150');");
}

/*
     redirects the browser to the page send as a param
 */
function redirect(direction){
     window.location=direction;
}

/*
 *   Browser safe method to get an element by it's id
 */
function getElement(id) {
     return document.getElementById ? document.getElementById(id) :
     document.all ? document.all(id) : null;
}

/*
 *   Browser safe method to hide an element (display:none)
 */
function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

/*
 *   Browser safe method to display an element (block) that is currently hidden (display:block)
 */
function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

/*
 *   Adds a new bookmark
 */
function addBookmark(title,url) {
     if (window.sidebar) {
          window.sidebar.addPanel(title, url,"");
     } else if( document.all ) {
          window.external.AddFavorite( url, title);
     } else if( window.opera && window.print ) {
          return true;
     }
}



function validateEmail(entered) {
     apos=entered.indexOf("@");
     dotpos=entered.lastIndexOf(".");
     lastpos=entered.length-1;
     if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
          return false;
     } else {
          return true;
     }
}

function validateNumber(x) {
     var anum=/(^\d+$)|(^\d+\.\d+$)/;
     if (anum.test(x)){
          return true;
     } else {
          return false;
     }
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s) {   
     var i;
     for (i = 0; i < s.length; i++) {
          // Check that current character is number.
          var c = s.charAt(i);
          if (((c < "0") || (c > "9"))) return false; 
     }
     // All characters are numbers.
     return true;
}

function stripCharsInBag(s, bag) {
     var i;
     var returnString = "";
     // Search through string's characters one by one.
     // If character is not in bag, append to returnString.
     for (i = 0; i < s.length; i++) {
          // Check that current character isn't whitespace.
          var c = s.charAt(i);
          if (bag.indexOf(c) == -1) returnString += c;
     }
     return returnString;
}

function checkInternationalPhone(strPhone){
     s=stripCharsInBag(strPhone,validWorldPhoneChars);
     return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function resizeOuterTo(w,h) {
     if (parseInt(navigator.appVersion)>3) {
          if (navigator.appName=="Netscape") {
               top.outerWidth=w;
               top.outerHeight=h;
          } else top.resizeTo(w,h);
     }

		var x,y;
     if (self.innerHeight) // all except Explorer
     {
     	x = self.innerWidth;
     	y = self.innerHeight;
     }
     else if (document.documentElement && document.documentElement.clientHeight)
     	// Explorer 6 Strict Mode
     {
     	x = document.documentElement.clientWidth;
     	y = document.documentElement.clientHeight;
     }
     else if (document.body) // other Explorers
     {
     	x = document.body.clientWidth;
     	y = document.body.clientHeight;
     }

     w = w + (w-x);
     h = h + (h-y);

     if (parseInt(navigator.appVersion)>3) {
          if (navigator.appName=="Netscape") {
               top.outerWidth=w;
               top.outerHeight=h;
          } else top.resizeTo(w,h);
     }
}

/*
 * http://www.activsoftware.com/code_samples/code.cfm/CodeID/59/JavaScript/Get_Query_String_variables_in_JavaScript
 * Credit goes to Pete Freitag for this function
 */
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return false;
}

/*
 * Credits to Simon Willison for this function
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function getElementsByStyleClass (className) {
  var all = document.all ? document.all :
    document.getElementsByTagName('*');
  var elements = new Array();
  for (var e = 0; e < all.length; e++)
    if (all[e].className == className)
      elements[elements.length] = all[e];
  return elements;
}

function XBrowserAddHandler(target,eventName,handlerName) {
  if ( target.addEventListener ) { 
    target.addEventListener(eventName, function(e){target[handlerName](e);}, false);
  } else if ( target.attachEvent ) { 
    target.attachEvent("on" + eventName, function(e){target[handlerName](e);});
  } else { 
    var originalHandler = target["on" + eventName]; 
    if ( originalHandler ) { 
      target["on" + eventName] = function(e){originalHandler(e);target[handlerName](e);}; 
    } else { 
      target["on" + eventName] = target[handlerName]; 
    } 
  } 
}

function addBehavior(){
     
     /*
          Get targetBlank links and attribute the right target.
     */
     elements = getElementsByStyleClass("targetBlank");
     for( var i=0; i < elements.length; i++ ){
          elements[i].target = "_blank";
     }
     
     if( getElement("sousmenuAPropos") ){
          if( getElement("sousmenuAPropos").style.display != "block" ){

               if( document.addEventListener ) {
                    getElement("APropos").addEventListener('mouseover',function(){ getElement("sousmenuAPropos").style.display = "block"; },false);
                    getElement("APropos").addEventListener('mouseout',function(){ getElement("sousmenuAPropos").style.display = "none"; },false);
                    getElement("sousmenuAPropos").addEventListener('mouseover',function(){ getElement("sousmenuAPropos").style.display = "block"; },false);
                    getElement("sousmenuAPropos").addEventListener('mouseout',function(){ getElement("sousmenuAPropos").style.display = "none"; },false);
               } else if (document.attachEvent){
                    getElement("APropos").attachEvent('onmouseover',function(){ getElement("sousmenuAPropos").style.display = "block"; });
                    getElement("APropos").attachEvent('onmouseout',function(){ getElement("sousmenuAPropos").style.display = "none"; });
                    getElement("sousmenuAPropos").attachEvent('onmouseover',function(){ getElement("sousmenuAPropos").style.display = "block"; });
                    getElement("sousmenuAPropos").attachEvent('onmouseout',function(){ getElement("sousmenuAPropos").style.display = "none"; });
               }
          
          }
     }
     
     if( getElement("sousmenuConseilMunicipal") ){
          if( getElement("sousmenuConseilMunicipal").style.display != "block" ){

               if( document.addEventListener ) {
                    getElement("ConseilMunicipal").addEventListener('mouseover',function(){ getElement("sousmenuConseilMunicipal").style.display = "block"; },false);
                    getElement("ConseilMunicipal").addEventListener('mouseout',function(){ getElement("sousmenuConseilMunicipal").style.display = "none"; },false);
                    getElement("sousmenuConseilMunicipal").addEventListener('mouseover',function(){ getElement("sousmenuConseilMunicipal").style.display = "block"; },false);
                    getElement("sousmenuConseilMunicipal").addEventListener('mouseout',function(){ getElement("sousmenuConseilMunicipal").style.display = "none"; },false);
               } else if (document.attachEvent){
                    getElement("ConseilMunicipal").attachEvent('onmouseover',function(){ getElement("sousmenuConseilMunicipal").style.display = "block"; });
                    getElement("ConseilMunicipal").attachEvent('onmouseout',function(){ getElement("sousmenuConseilMunicipal").style.display = "none"; });
                    getElement("sousmenuConseilMunicipal").attachEvent('onmouseover',function(){ getElement("sousmenuConseilMunicipal").style.display = "block"; });
                    getElement("sousmenuConseilMunicipal").attachEvent('onmouseout',function(){ getElement("sousmenuConseilMunicipal").style.display = "none"; });
               }
          
          }
     }

     if( getElement("sousmenuBibliotheques") ){
          if( getElement("sousmenuBibliotheques").style.display != "block" ){

               if( document.addEventListener ) {
                    getElement("Bibliotheques").addEventListener('mouseover',function(){ getElement("sousmenuBibliotheques").style.display = "block"; },false);
                    getElement("Bibliotheques").addEventListener('mouseout',function(){ getElement("sousmenuBibliotheques").style.display = "none"; },false);
                    getElement("sousmenuBibliotheques").addEventListener('mouseover',function(){ getElement("sousmenuBibliotheques").style.display = "block"; },false);
                    getElement("sousmenuBibliotheques").addEventListener('mouseout',function(){ getElement("sousmenuBibliotheques").style.display = "none"; },false);
               } else if (document.attachEvent){
                    getElement("Bibliotheques").attachEvent('onmouseover',function(){ getElement("sousmenuBibliotheques").style.display = "block"; });
                    getElement("Bibliotheques").attachEvent('onmouseout',function(){ getElement("sousmenuBibliotheques").style.display = "none"; });
                    getElement("sousmenuBibliotheques").attachEvent('onmouseover',function(){ getElement("sousmenuBibliotheques").style.display = "block"; });
                    getElement("sousmenuBibliotheques").attachEvent('onmouseout',function(){ getElement("sousmenuBibliotheques").style.display = "none"; });
               }
          
          }
     }

}
addLoadEvent(addBehavior);

(function(){ /*Use Object Detection to detect IE6*/ var m = document.uniqueID /*IE*/ && document.compatMode /*>=IE6*/ && !window.XMLHttpRequest /*<=IE6*/ && document.execCommand ; try{ if(!!m){ m("BackgroundImageCache", false, true) /* = IE6 only */ } }catch(oh){}; })();


var currentPage = 1;
var currentMaxRows = 10;
function displayPage(page,maxRows){
     var table = getElement("listeAdmin");
     var startRow = page*maxRows - maxRows + 1;
     var endRow = startRow + maxRows;
     for (var i=1; i<table.rows.length; i++) {
          if( i >= startRow && i < endRow ){
               table.rows[i].style.display = "table-row";
          } else {
               table.rows[i].style.display = "none";
          }
     }
     currentPage = page;
     currentMaxRows = maxRows;
}

function showAllRows(){
     var table = getElement("listeAdmin");
     for (var i=1; i<table.rows.length; i++) {
          table.rows[i].style.display = "table-row";
     }
}

function hideMenus(){
     for(i=0;i<=500;i++){
          if(getElement("menu-"+i)) hidediv("menu-"+i);
     }
}
function displayMenu(id){
     hideMenus();
     showdiv("menu-"+id);
}
