/*
 *   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');");
}

/*
     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";
     }

     // Display alert if the "liens" section is clicked on - If javascript is disabled the user is redirected to liens.html, which
     // displays the right message
     /*
     if( document.addEventListener ) {
          getElement("liens").addEventListener('click',function(){ this.href = "#"; alert('Cette section sera disponible bientôt.'); },false);
     } else if (document.attachEvent){
          getElement("liens").attachEvent('onclick',function(){ getElement("liens").href = "#"; alert('Cette section sera disponible bientôt.'); });
     }
     */

}
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){}; })();


