var i=0, j=0, lastSelect='', text_quote='', navVersion, error = 0, XMLHTTP_out;
var getValue = new Array();
var speedMenu = 80;
var OP, DHTML, DOM, MS, NS;
var Style_markField = '1px solid red 18px';
var posY=0, posX=0;

/* Bibliothèque DHTML */
function DHTML_init() {

	if (window.opera) {
	   OP = 1;
	}
 
	if(document.getElementById) {
	   DHTML = 1;
	   DOM = 1;
	}
 
	if(document.all && ( !OP || OP==0 ) ) {
	   DHTML = 1;
	   MS = 1;
	}
 
	if(document.layers && ( !OP || OP==0 ) ) {
	   DHTML = 1;
	   NS = 1;
	}
	
	if( MS == 1 ) {
	
		navVersion = navigator.appVersion.search(/MSIE/);
		navVersion = navigator.appVersion.substring( navVersion+5, navVersion+6 );
	}
	
	ControlVersion();
	
	// Correction BUG Safari
	//if( window.location.href.indexOf(".php") == -1 && window.location.href.indexOf(".html") == -1 )
	//	window.location.href=baseUrl+"/accueil.html";
 
}

function ControlVersion() {

	if( MS == 1 && navVersion < 5 ) {
					
		alert("Your Navigator is too old, update your navigator www.microsoft.com");
	}
}

function PopUp(url, name, width,height,center,resize,scroll,posleft,postop) {

	if (posleft != 0) { X = posleft }
	if (postop  != 0) { Y = postop  }

	if (!scroll || scroll==0) { scroll = 'no' }
	if (!resize || resize==0) { resize = 'no' }

	if ((parseInt (navigator.appVersion) >= 4 ) && (center)) {
	  X = (screen.width  - width ) / 2;
	  Y = (screen.height - height) / 2;
	}
	
	if (scroll != 0) { scroll = 'yes' }
	
	if( MS != 1 ) {
		
		height = height;
		width = width+16;
		
	}
	
	var Win = window.open( url, name, 'width='+width+',height='+height+',top='+Y+',left='+X+',resizable='+resize+',scrollbars='+scroll+',location=no,status=no,menubar=no,toolbar=no,copyhistory=0');

	if( Win ) {
		
		Win.focus();
	}
	else {
		
		alert("Desactivate your anti-popup system !");
		return;
		
	}
}

function anim_quote() {
	
	if( typeof quote != 'undefined' && quote.length > i ) {
		
		text_quote += quote.charAt(i++);
		
		if( quote.charAt((i-1)) != " " ) {
		
			if( quote.charAt((i-1)) == "<" ) j=1;
				else if( quote.charAt((i-1)) == ">" ) j=0;
			
			if( j != 1 ) {
				
				getElement_byId("Quote").innerHTML = text_quote;
				setTimeout( "anim_quote()", speedMenu );
				return;
			}
			
		}
			
		anim_quote();
		
	}
	
}

function getImage( name ) {
	
	return document.images[ name ];
	
}

function switchPics( img, f, act ) {

	if( act != 1 && lastSelect != '' ) {
		
		getImage( img.name ).src = lastSelect;
		return;
		
	}
	
	lastSelect = getImage( img.name ).src;
	
	if( getImage( img.name ).src.indexOf( 'Active' ) == -1 )
		getImage( img.name ).src = pathIMG+'/'+ f +'/'+ img.name +'Over.gif';
	
	
}

function loadPics_menu() {
	
	var url = window.location.href;
	var folder = '';
	var array_url = url.split( "/" );
	var file = array_url[ array_url.length-1 ].replace( /\?(.+)/, '' );
		file = file.replace( /\.html(.*)$/, '' );
	
	preload_pictOver();
	
	if( file.indexOf("_") != -1 ) {
		
		var categories = file.split("_");
			file = categories[1];
			folder = categories[0];
		
	}
	
	var img = getImage( file );
	
	if( typeof img != 'undefined' ) {
		
		img.src = pathIMG+'/Navigation/'+ folder +'/'+ file +'Active.gif'
		
	} else {
		
		var firstPic = getImage( 3 );
		
		if( firstPic.src.toLowerCase().indexOf("retour") != -1 )
			firstPic = getImage( 4 );
		
		var expression = /^(.+)\.(\w+)$/; expression.exec( firstPic.src );
		
		firstPic.src = RegExp.$1 +"Active."+ RegExp.$2;
		
	}
		
}

function getElement_byId( x ) {
	
	if( DOM == 1 ) var element = document.getElementById(x);
		else if( MS == 1 ) var element = document.all[x];
			else if( NS == 1 ) var element = document.layers[x];
				else return false;
	
	if( typeof element == 'undefined' || element == null ) return false;
		else return element;
}

function GET() {

    var url = window.location.search;
	var temp, liste;
	
    if (url != "") {
		
      url = url.substring(1,url.length);                    
                                                              
      liste = url.split("&");                           	 
	  
      for (i=0;i<=liste.length-1;i++) {
		  
		   temp = liste[i].split("=");          				
		   getValue[ temp[0] ] = temp[1];   					
		
       }
	   
      for (i=0;i<=getValue.length-1;i++) {
		  
		 getValue[i] = getValue[i].replace(/\+/g," ");
			  // remplacement du + par un espace
		 getValue[i] = unescape(getValue[i]);          			
		 
      }
	  
     }
}

function preload_pictOver() {
	
	var elems = getElement_byId( "menu" );
	var to_preload = '';
	
	if( elems ) {
		
		for( var i=0; i < elems.getElementsByTagName("a").length; i++ ) {
			
			var expression = /^(.+)\.(\w+)$/; expression.exec( (elems.getElementsByTagName("a")[i]).getElementsByTagName("img")[0].src );
			
			to_preload += "'"+ RegExp.$1 +"Over."+ RegExp.$2 +"',";
			
		}
		
		to_preload = to_preload.substr( 0, (to_preload.length)-1 );
		
		MM_preloadImages( to_preload );
		
	}
	
}

function MM_preloadImages() {
  
  if(document.images) { 
  
  	if(!document.MM_p) document.MM_p=new Array();
	
    var i,j=document.MM_p.length,a=MM_preloadImages.arguments;
	
		for(i=0; i<a.length; i++) {
			
			if (a[i].indexOf("#")!=0){ 
			
				document.MM_p[j]=new Image; document.MM_p[j++].src=a[i];
			}
		}	
  	}
}

function controlMail( mail ) {
	
	var control = 0;
	
	for( var i=0; i<mail.length; i++ ) {
		
		if( mail.charAt(i) == "@" ) control++;
		if( mail.charAt(i) == "." ) control++;
		
	}
	
	if( control > 1 ) return true;
	
	return false;
	
}

function mark_fields( f, act ) {
	
	if( act == 1 ) {
	
		if( MS == 1 ) f.className='borderRed';
			else {
				
				var custom_style = Style_markField.split(" ");
								
				f.style.borderWidth=custom_style[0];
				f.style.borderColor=custom_style[2];
				f.style.borderStyle=custom_style[1];
				
				if( f.type == "text" )
					f.style.height=custom_style[3];
				
			}
			
	} else {
		
		if( MS == 1 ) f.className='';
			else {
				
				f.style.borderWidth='';
				f.style.borderColor='';
				f.style.borderStyle='';
				
				if( f.type == "text" )
					f.style.height='';
				
			}
		
	}

}

function controlBenevoles( f ) {
	
	var form 		= document.forms[ f.name ];
	var fields 		= ['nom', 'prenom', 'address_1', 'npa_city', 'pays', 'telephone', 'mail', 'job', 'assurance', 'membreOf', 'membreOf2'];
	var nameFields 	= ['Nom', 'Prénom', 'Adresse', 'N° postal et ville', 'Pays', 'Téléphone', 'E-mail', 'Profession', 'Assurance', 'Qui est membre de', 'Je suis membre de'];
	var error		= 'Les champs suivants doivent être rempli et complété correctement :\n\n';
	var msg_error	= error;
	
	for( var i=0; i<fields.length; i++ ) {
		
		if( form.elements[ fields[i] ].value == "" ) { msg_error += "- "+ nameFields[i] +"\n"; mark_fields( form.elements[ fields[i] ], 1 ) }
			else if( fields[i] == 'mail' ) {
				
				if( controlMail( form.elements[ fields[i] ].value ) == false ) { msg_error += "- "+ nameFields[i] +"\n"; mark_fields( form.elements[ fields[i] ], 1 ) }
			
			}
		
	}
	
	if( error != msg_error ) {
		
		alert( msg_error );
		window.location.href="#start";
		return false;
		
	}
	
	//alert("Pour des raisons techniques le formulaire d'inscription est momentanément interrompu.\nVotre participation étant très importante pour nous, nous vous remercions de revenir vous inscrire ultérieurement."); return false;
	
	if( confirm( "Avez-vous bien indiqué vos disponibillités ?") ) {
		
		alert("Formulaire envoyé avec succès !\n\nMerci de votre engagement");
		return true;
		
	} else {
		
		window.location.href="#dispo";
		return false;
	}

}

function do_print() {
	
	var elem = getElement_byId( 'central' );
		
	elem.style.overflow = 'visible';
	elem.style.height = 'auto';
	
	setTimeout( "window.print()", 500 );
	
	window.captureEvents(Event.FOCUS);
	window.onfocus = restore_central;
		
}

function restore_central(evenement) {
	
	var elem = getElement_byId( 'central' );
	
	elem.style.overflow = 'auto';
	elem.style.height = '400px';
	
}

function getContent( filename, id) {
	
	var req = null;
	var obj = getElement_byId( id );
	var method = "GET";
	var out;
	
	obj.innerHTML = '<div id="loading">Chargement en cours ...</div>';
	
	if( window.XMLHttpRequest ) {
		
		req = new XMLHttpRequest();
		
		if( req.overrideMimeType )
			req.overrideMimeType('text/xml');
		
	} else if ( window.ActiveXObject ) {
		
		try {
			
			req = new ActiveXObject("Msxml2.XMLHTTP");
			
		} catch(e) {
			
			try {
				
				req = new ActiveXObject("Microsoft.XMLHTTP");
				
			} catch(e) {
				
				if( error == 0 ){
					
					error = 1;
					alert("Error ! XMLHTTP isn't valid !, error returned: "+ e);
				
				}
			}
			
		}
		
	} else {
		
		if( error == 0 ) {
			
			error = 1;
			alert("Your browser don't support XMLHTTP Request !");
			
		}
		
	}
	
	req.open( method, filename, true );
	
	req.onreadystatechange = function() {
		
		if( req.readyState == 4 ) {
			
			if( req.status == 200 )
				obj.innerHTML = req.responseText;
			else
				obj.innerHTML = "Error ! Returned status code "+ req.status +" "+ req.statusText;
			
		}
		
	}
	
	req.send(null);
	return;	
}

function getMousePos(e) {
	
	if( MS == 1 ) {
		
		posX = event.x+document.body.scrollLeft;
		posY = event.y+document.body.scrollTop;
		
	} else {
		
		posX = e.pageX;
		posY = e.pageY;
		
	}
	
}

function InitBulle() {
	
	document.onmousemove = getMousePos;

}

function hideBulle() {
	
	var elem = getElement_byId( "infoBulle" );
	
	elem.style.visibility = "hidden";
	elem.style.top = "0px"; ;
	elem.style.left = "0px";
	
}

function showBulle( text ) {
	
	var elem = getElement_byId( "infoBulle" );
	var out, xOffset=20, yOffset=20;
	
	if( elem != false ) {
		
		out = '<table border="0" cellpadding="0" cellspacing="0">';
		out += '<tr><td>'+ text +'</td></tr>';
		out += '</table>';
		
		elem.innerHTML = out;
		elem.style.top = (posY+yOffset) +"px"; ;
		elem.style.left = (posX+xOffset) +"px";
		elem.style.visibility = "visible";
		
	}
	
}

function controlForm( f, exception ) {

	var form = document.forms[f.name];
	var error = "Les champs suivant n'ont pas été rempli correctement :\n\n";
	var msg_error = error;
	var exception_array = exception.split(",");
	
	if( DOM == 1 ) {
	
		for( var i=0; i<form.elements.length; i++ ) {
			
			var obj = form.elements[i];
			var control = true;
			
			for( var a=0; a<exception_array.length; a++ ) {
				
				if( exception_array[a] == obj.name ) control = false;
				
			}
			
			if( control ) {
			
				if( obj.type == "text" || obj.type == "textarea" || obj.type == "select" ) {
				
					if( obj.value == "" ) { msg_error += "- "+ obj.name +"\n"; mark_fields( obj, 1 ); }
						else if( obj.name.toLowerCase() == "email" && !controlMail( obj.value ) ) { msg_error += "- "+ obj.name +"\n"; mark_fields( obj, 1 ); }
					
				} else if( obj.type == "checkbox" ) {
				
					if( obj.checked == false )
						msg_error += "- "+ obj.name +"\n";
					
				} else if( obj.type == "radio" ) {
					
					var check = false;
					
					for( var b=0; b<document.getElementsByName( obj.name ).length; b++ ) {
						
						//alert( document.getElementsByName( obj.name )[b].value );
						if( document.getElementsByName( obj.name )[b].checked ) check = true;
						
					}
					
					i += (b-1);
					
					if( !check ) msg_error += "- "+ obj.name +"\n"; 
					
				}
			}
		
		}
		
		if( error != msg_error ) {
		
			alert( msg_error );
			window.location.href="#start";
			return false;
		
		}
	
	} else
		return false;
	
	alert("Formulaire envoyé avec succès");
	return true;
	
}

function newWindow( url ) {
	
	var Win = window.open( url, 'Secutix','resizable=yes,scrollbars=yes,location=yes,status=yes,menubar=yes,toolbar=yes,copyhistory=0');

	if( Win ) {
		
		Win.focus();
	}
	else {
		
		alert("Desactivate your anti-popup system !");
		return;
		
	}
	
}

function rand( n ) {
	
	return ( Math.floor( Math.random() * n ) );
}

	function randomLinks( links, cols, name ) {
		
		var out = new Array();
		var alreadyInsert = '';
		var to_out = '<table border="0" cellpadding="0" cellspacing="3" width="100%" id="'+ name +'">%CONTENT%</table>';
		
		var intRandom = rand( links.length );
		
		for( var i=intRandom; i<(intRandom+links.length); i++ ) {
			
			var n=0;
			
			if( i>(links.length-1) ) {
				
				n=i-links.length;
				
			} else
				n = i;
				
			out.push( links[ n ] );
			
		}
		
		/*for( var i=0; i<links.length; i++ ) {
			
			var ok = 0;
			
			while( ok != 1 ) {
			
				var intRandom = rand( links.length );
		
				if( links[ intRandom ] != "" && alreadyInsert.indexOf( intRandom ) == -1 ) {
					
					alreadyInsert += intRandom +",";
					out.push( links[ intRandom ] );
					
					ok = 1;
					
				}
			}
		}*/
		
		var content = '';
		var i=0;
		
		for( var a=0; a<Math.round(out.length/cols)+1; a++ ) {
			
			content += "<tr>";
			
			for( var b=0; b<cols; b++ ) {
				
				if( typeof out[i] != 'undefined' ) {
					
					var info = out[i].split(',');
					content += '<td><a href="'+ info[1] +'" target="_blank"><img src="'+ info[2] +'" border="0" alt="'+ info[0] +'" /></a><br />'+ info[0] +'</td>';
					
				} else
					content += '<td>&nbsp;</td>';
					
				i++;
				
			}
			
			content += "</tr><tr><td colspan='"+ cols +"'>&nbsp;</td>";
			
		}
		
		document.write( to_out.replace(/%CONTENT%/, content ) );
		
	}

function acceptConditions() {
	
	if( confirm( "Acceptez-vous les conditions ?") ) {
		
		newWindow( baseUrl +"/~airpic" );
		
	}
	
}

function confirmBillets() {
	
	alert( "Le système de billetterie proposé nécessite impérativement\nl'IMPRESSION DES BILLETS A DOMICILE !\n\nMerci de bien respecter la procédure de commande qui suit !");
	
	newWindow( "https://www.elca-services.ch/sales4/live/shop/BEX/SHOP_BEX07/index.php" );

}
	
// Decryption des courriels
var decryption_cache = new Array();

function decrypt_string(crypted_string,n,decryption_key,just_email_address) {
	var cache_index = "'"+crypted_string+","+just_email_address+"'";

	if(decryption_cache[cache_index])					// If this string has already been decrypted, just
		return decryption_cache[cache_index];				// return the cached version.

	if(addresses[crypted_string])						// Is crypted_string an index into the addresses array
		var crypted_string = addresses[crypted_string];			// or an actual string of numbers?

	if(!crypted_string.length)						// Make sure the string is actually a string
		return "Error, not a valid index.";

	if(n == 0 || decryption_key == 0) {					// If the decryption key and n are not passed to the
		var numbers = crypted_string.split(' ');			// function, assume they are stored as the first two
		n = numbers[0];	decryption_key = numbers[1];			// numbers in crypted string.
		numbers[0] = ""; numbers[1] = "";				// Remove them from the crypted string and continue
		crypted_string = numbers.join(" ").substr(2);
	}

	var decrypted_string = '';
	var crypted_characters = crypted_string.split(' ');

	for(var i in crypted_characters) {
		var current_character = crypted_characters[i];
		var decrypted_character = exponentialModulo(current_character,n,decryption_key);
		if(just_email_address && i < 7)				// Skip 'mailto:' part
			continue;
		if(just_email_address && decrypted_character == 63)	// Stop at '?subject=....'
			break;
		decrypted_string += String.fromCharCode(decrypted_character);
	}
	
	decryption_cache[cache_index] = decrypted_string;			// Cache this string for any future calls

	return decrypted_string;
}

function decrypt_and_email(crypted_string,n,decryption_key) {
	if(!n || !decryption_key) { n = 0; decryption_key = 0; }
	if(!crypted_string) crypted_string = 0;

	var decrypted_string = decrypt_string(crypted_string,n,decryption_key,false);
	parent.location = decrypted_string;
}

function decrypt_and_echo(crypted_string,n,decryption_key) {
	if(!n || !decryption_key) { n = 0; decryption_key = 0; }
	if(!crypted_string) crypted_string = 0;

	var decrypted_string = decrypt_string(crypted_string,n,decryption_key,true);
	document.write(decrypted_string);
	return true;
}

function exponentialModulo(base,exponent,y) {
	if (y % 2 == 0) {
		answer = 1;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	} else {
		answer = base;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	}
	return answer;
}

DHTML_init();
GET();

