var reEmail = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/

function $(obj){
	if(document.getElementById(obj)) return document.getElementById(obj);
	else return null;
}

function printContent(){
	popup = window.open('','printWindow','menubar=yes,scrollbars=yes,resizable=yes,width=800,height=650');
	$('print_form').submit();
}

function check_email_newsletter(error_message, ok_message){
	var email = $('id_email_newsletter').value;

	if( email== '' || !(reEmail.test(email)) ){
		alert(error_message);
		return false;
	}
	else{
		var xmlhttp = new XMLHTTP();
	    xmlhttp.open('GET', '/AJAX_newsletter_submit.php?LANG='+LANG+'&email='+escape(email), true);
    	xmlhttp.send(null);
    	xmlhttp.onreadystatechange = function() {
	    	if (xmlhttp.readyState == 4) {
		        if (xmlhttp.status == 200) {

		        	if(xmlhttp.responseText=='OK') {
		        		alert(ok_message);
		        		$('id_email_newsletter').value = '';
		        	}
		        	else {
	    				return false;
		        	}

		        } else {
		        	alert('Problème lors de la connexion au serveur.');
	    			return false;
		    	}
			}
	    };
	}
}

function send_to_friend(error_message){

	var dest_email = $('friend_email').value;
	var sender_email = $('self_email').value;
	if(!(reEmail.test(dest_email)) || !(reEmail.test(sender_email))){
		alert(error_message);
		return false;
	}
	else {
		$('mcg-sendtofriend_form').style.display = 'none';
		$('mcg-sendtofriend_wait').style.display = 'block';
		var url = 'http://'+$('self_url').value;
		var xmlhttp = new XMLHTTP();
	    xmlhttp.open('GET', '/AJAX_send_to_friend.php?LANG='+LANG+'&url='+escape(url)+'&dest_email='+escape(dest_email)+'&sender_email='+escape(sender_email), true);
    	xmlhttp.send(null);

    	xmlhttp.onreadystatechange = function() {
	    	if (xmlhttp.readyState == 4) {
		        if (xmlhttp.status == 200) {

					$('mcg-sendtofriend_wait').style.display = 'none';
		        	if(xmlhttp.responseText!='OK') {
		        		$('mcg-sendtofriend_form').style.display = 'block';
		        		alert(xmlhttp.responseText);
		        	}
		        	else {
						setTimeout('ami_display_off("mcg-sendtofriend")', 2000);
						$('mcg-sendtofriend_form').style.display = 'block';
	    				return false;
		        	}

		        } else {
		        	alert('Problème lors de la connexion au serveur.');
	    			return false;
		    	}
			}
	    };
	    return false;
	}

}

function XMLHTTP(){
	var http_request = false;
    if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    if (!http_request) {
        alert('Cannot create an XMLHTTP instance');
        return false;
    }
    return http_request;
}

// ///////////////  ///////////////
// ROLLOVER
// ///////////////
function addLoadEvent(func) {
	if ( typeof wpOnload != "function" ) {
		wpOnload = func;
	} else {
		var oldonload = wpOnload;
		wpOnload = function() {
			oldonload();
			func();
		}
	}
}

function hover(obj){
  	if(document.all){
    	UL = obj.getElementsByTagName('ul');
    	if(UL.length > 0){
      		sousMenu = UL[0].style;
      		if(sousMenu.display == 'none' || sousMenu.display == ''){
        		sousMenu.display = 'block';
      		}else{
        		sousMenu.display = 'none';
      		}
    	}
  	}
}

function setHover(){
  	// menu 1
  	LI1 = document.getElementById('ID_MenuColGauche_main1').getElementsByTagName('li');
  	nLI1 = LI1.length;
  	for(i=0; i < nLI1; i++){
    	LI1[i].onmouseover = function(){
      		hover(this);
    	}
    	LI1[i].onmouseout = function(){
      		hover(this);
    	}
  	}
  	// menu2
  	LI2 = document.getElementById('ID_MenuColGauche_main2').getElementsByTagName('li');
  	nLI2 = LI2.length;
  	for(i=0; i < nLI2; i++){
    	LI2[i].onmouseover = function(){
      		hover(this);
    	}
    	LI2[i].onmouseout = function(){
      		hover(this);
    	}
  	}
}

function montre_event(id){
	var d = document.getElementById(id);
	for (var i = 0; i<=500; i++) {
		if (document.getElementById('event_detail'+i)) {document.getElementById('event_detail'+i).style.visibility='hidden';}
	}
	if (d) {d.style.visibility='visible';}
}

function insertFlash(div_id, width, height, url, otherparam){
	document.getElementById(div_id).innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+width+'" height="'+height+'" '+otherparam+'>'+
		'<param name="allowScriptAccess" value="sameDomain" />'+
		'<param name="movie" value="'+url+'" />'+
		'<param name="quality" value="high" /><param name="wmode" value="transparent"><param name="bgcolor" value="#ffffff" />'+
		'<embed src="'+url+'" wmode="transparent" quality="high" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="compte_a_rebour" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" '+otherparam+'></embed>'+
		'</object>';
}

function auto_fill(o, i){
 	if(i){
  		if(o.refv==null) o.refv = o.value;
  		if(o.value==o.refv)	o.value='';
   		o.select();
 	}
 	else{
  		if(o.value=='') o.value=o.refv;
 	}
}

// ///////////////  ///////////////
// FORMULAIRES
// ///////////////
/*function recherche.submit(){}*/
/*function newsletter.submit(){}*/
/*function envoi_ami.submit()*/
function ami_display_off(id){
	AMI = document.getElementById(id);
	AMI.style.display = 'none';
}
function ami_display_on(id){
	AMI = document.getElementById(id);
	AMI.style.display = 'block';
/*	AMI.style.zIndex = '100';*/
}

function display_message(id){
	/*
	if(!$('error')) {
		var error_div = document.createElement("DIV");
		error_div.setAttribute('id', 'error');
		$('pagewidth').appendChild(error_div);
	}

	$('error').innerHTML = message;
	Effect.Appear('error', { duration: 0.6 });
	*/
	document.getElementById(id).style.display = 'block';
	setTimeout('dissapear("'+id+'")', 5000);
}

function dissapear(id){
	document.getElementById(id).style.display = 'none';
	//Effect.Fade('error', { duration: 1 });
}

//
// CSS Photo Shuffler v1.0 by
//   Carl Camera
//   http://iamacamera.org
//
// SetOpacity Function and inpiration from Photo Fade by
//   Richard Rutter
//   http://clagnut.com
//
// License: Creative Commons Attribution 2.5  License
//   http://creativecommons.org/licenses/by/2.5/
//

// Customize your photo shuffle settings
//
// * Surround the target <img /> with a <div>. specify id= in both
// * set background-repeat:no-repeat in CSS for the div
// * The first and final photo displayed is in the html <img> tag
// * The array contains paths to photos you want in the rotation.
//   If you want the first photo in the rotation, then it's best to
//   put it as the final array image.  All photos must be same dimension
// * The rotations variable specifies how many times to repeat array.
//   images. zero is a valid rotation value.

var gblPhotoShufflerDivId = "ID_Sponsors_def";
var gblPhotoShufflerImgId = "ID_Sponsors_img";
var gblImg = new Array(
  "/sponsors/damocles.jpg",
  "/sponsors/pnue.jpg",
  "/sponsors/agnesb.jpg"
  );
var gblPauseSeconds = 4.25;
var gblFadeSeconds = .85;
var gblRotations = 300;

// End Customization section

var gblDeckSize = gblImg.length;
var gblOpacity = 100;
var gblOnDeck = 0;
var gblStartImg;
var gblImageRotations = gblDeckSize * (gblRotations+1);

addLoadEvent(photoShufflerLaunch);

function photoShufflerLaunch()
{
	var theimg = document.getElementById(gblPhotoShufflerImgId);
	if(theimg){
		gblStartImg = theimg.src; // save away to show as final image

		document.getElementById(gblPhotoShufflerDivId).style.backgroundImage='url(' + gblImg[gblOnDeck] + ')';
		setTimeout("photoShufflerFade()",gblPauseSeconds*1000);
	}
}

function photoShufflerFade()
{
	var theimg = document.getElementById(gblPhotoShufflerImgId);

	// determine delta based on number of fade seconds
	// the slower the fade the more increments needed
      var fadeDelta = 100 / (30 * gblFadeSeconds);

	// fade top out to reveal bottom image
	if (gblOpacity < 2*fadeDelta )
	{
	  gblOpacity = 100;
	  // stop the rotation if we're done
	  if (gblImageRotations < 1) return;
	  photoShufflerShuffle();
	  // pause before next fade
        setTimeout("photoShufflerFade()",gblPauseSeconds*1000);
	}
	else
	{
	  gblOpacity -= fadeDelta;
	  setOpacity(theimg,gblOpacity);
	  setTimeout("photoShufflerFade()",30);  // 1/30th of a second
	}
}

function photoShufflerShuffle()
{
	var thediv = document.getElementById(gblPhotoShufflerDivId);
	var theimg = document.getElementById(gblPhotoShufflerImgId);

	// copy div background-image to img.src
	theimg.src = gblImg[gblOnDeck];
	// set img opacity to 100
	setOpacity(theimg,100);

      // shuffle the deck
	gblOnDeck = ++gblOnDeck % gblDeckSize;
	// decrement rotation counter
	if (--gblImageRotations < 1)
	{
	  // insert start/final image if we're done
	  gblImg[gblOnDeck] = gblStartImg;
	}

	// slide next image underneath
	thediv.style.backgroundImage='url(' + gblImg[gblOnDeck] + ')';
}

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;

  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";

  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;

  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;

  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}
