// JavaScript Document


//Fonction de positionnement de la bulle en fonction des mouvements du curseur

//==================================
	function init(){
		btnfiles	=	document.getElementById('btnpopupopen');
		btnfiles.onclick=popUp;
		
		btnclose	=	document.getElementById('btnpopupclose');
		btnclose.onclick=popUp;
		
		
	}
	//==================================
	function popUp(){
		popupDiv	=	document.getElementById('pop-up');
		if(popupDiv.style.display=="block"){
			popupDiv.style.display="none";
		}else{
			popupDiv.style.display="block";
			
		}
		effect();
	}
	//==================================
	
	function chargement(f){
	//alert('ok');
		if(nBupload<6){
			effect();
			
			var btn		=	document.getElementById('upload');
			
			btn.className= "chargement";
			
			if(!verif(f)){
				btn.className= "btn_upload";
				messageSend("Type de fichier incorrect. Choisir fichier JPG ou JPEG");
				return false;
			}
			
			if(frame==true){
				var elementFrame = document.createElement('div');
				elementFrame.innerHTML = '<iframe name="frame_cache" id="frame_cache" style="border:none;width:0px;height:0px"></iframe>';
				document.body.appendChild(elementFrame);
				document.getElementById('frame_cache').onload=Charger;
				frame=false;
			}
			
			f.submit();
		}else{
			messageSend("Vous ne pouvez pas uploader plus de six images.");
		}
		
	}
	
	//==================================
	
	function verif(f)
    {   var fich=f.fichier.value.split(".");
	
        var ext=fich[fich.length-1].toLowerCase();
		
        if ( ext !='jpg' && ext != 'jpeg' && ext != 'JPG') {
			return false;
		}else{
			return true;
		}
    }
	
	//==================================
	function Charger(){
			
			var element = document.getElementById('frame_cache');
		
			if (element.contentDocument) {
				var doc = element.contentDocument;
			} else if (element.contentWindow) {
				var doc = element.contentWindow.document;
			} else {
				var doc = window.frames['frame_cache'].document;
			}
			
			if (doc.location.src == "about:blank") {
				return;
			}
			
			if(doc.body.innerHTML!=""){
				Ecrire(doc.body.innerHTML);
			}
	}
	//==================================
	function Ecrire(message){
		
		if(message=='ERROR'){
			messageSend("Type de fichier incorrect.");
			return false
		}
		
		var btn		=	document.getElementById('upload');
		btn.className= "btn_upload";
		
		nbimg++;
		nBupload++;
		
		document.getElementById('input_image').innerHTML +='<input type="hidden" name="image_'+nbimg+'" id="'+nbimg+'" value="'+message+'"/>';
		document.getElementById('liste').innerHTML +='<div class="image_pop-up" id="preview'+nbimg+'"><img src="./dossier_temp/'+message+'" class="preview_img" height="80px" /><div class="supprimer_image" onclick=\'deleted("'+nbimg+'");\'></div></div>';
		
			 
	}
	
	function deleted(divNum){
		nBupload--;
		var olddiv = document.getElementById(divNum);
  		document.getElementById('input_image').removeChild(olddiv);
		
		var olddivpreview = document.getElementById('preview'+divNum);
  		document.getElementById('liste').removeChild(olddivpreview);
	}// JavaScript Document

/*===============================QUE FAIRE EN FONCTION DU LIEN==============================================*/
function validForm(f){
	
	if(f.prenom.value == "" ||!isWord(f.prenom.value)||repWord(f.prenom.value)){//
		messageSend("Veuillez entrer votre prenom",f.prenom);
		return false;
	}
	
	if(f.nom.value == "" || !isWord(f.nom.value)|| repWord(f.nom.value)){
		messageSend("Veuillez entrer votre nom",f.nom);
		return false;
	}
	
	if(f.date_naissance.value == "jj/mm/aaaa"|| !isDate(f.date_naissance.value)){
		messageSend("Veuillez entrer votre date de naissance <em>(Ex : 02/04/1980)</em>",f.date_naissance);
		return false;
	}
	
	if(f.nationalite.value!=""){
		if(!isWord(f.nationalite.value)|| repWord(f.nationalite.value)){
			messageSend("Veuillez entrer votre nationnalité",f.nationalite);
			return false;
		}
	}
	
	if(f.code_postal.value!=""){
		if(!isCP(f.code_postal.value)){
			messageSend("Veuillez entrer votre Code Postal valide",f.code_postal);
			return false;
		}
	}
	
	if(f.ville.value!=""){
		if(!isWord(f.ville.value)|| repWord(f.ville.value)){
			messageSend("Veuillez entrer votre ville valide",f.ville);
			return false;
		}
	}
	
	/*if(f.taille.value == "" || !isTaille(f.taille.value)){
		messageSend("Veuillez entrer votre taille en cm",f.taille);
		return false;
	}*/
	
	if(!isMail(f.email.value)){
		messageSend("Veuillez entrer votre email",f.email);
		return false;
	}	
	
	if(!isTel(f.cellphone.value)){
		messageSend("Veuillez entrer votre numéro de téléphone",f.cellphone);
		return false;
	}
	
	if(f.phone.value!=""){
		if(!isTel(f.phone.value)){
			messageSend("Veuillez entrer votre numéro de téléphone",f.phone);
			return false;
		}
	}
	
	if(nBupload<2){
		messageSend("Veuillez uploader au moins deux photos");
		return false;
	}
	
	messageSend("Envoi de votre inscription en cours");
	
	f.submit();
}

function validInscription(f){
	
	if(f.prenom.value == "" ||!isWord(f.prenom.value)||repWord(f.prenom.value)){//
		messageSend("Veuillez entrer votre prenom",f.prenom);
		return false;
	}
	
	if(f.nom.value == "" || !isWord(f.nom.value)|| repWord(f.nom.value)){
		messageSend("Veuillez entrer votre nom",f.nom);
		return false;
	}
	
	if(!isMail(f.email.value)){
		messageSend("Veuillez entrer votre email",f.email);
		return false;
	}
	
	f.submit();
	
}
/*=========================================*/	
function isMail(txtMail) {
	if(txtMail == ""){
		return false;
	}else{
		var regMail=new RegExp("^[0-9a-z._-]+@{1}[0-9a-z._-]{2,}[.]{1}[a-z]{2,5}$","i");
		return regMail.test(txtMail);
	}
}
/*=========================================*/
function isTel(txtmobile) {
	var regMobile=new RegExp("^[0-9]*$","i");
	return regMobile.test(txtmobile);
}
/*=========================================*/
function isCP(txtcp) {
	var regMobile=new RegExp("^[0-9]*$","i");
	return regMobile.test(txtcp);
}
/*=========================================*/
function isTaille(txtcp) {
	var regCp=new RegExp("^1[0-9]{2}$","i");
	return regCp.test(txtcp);
}
/*=========================================*/
function repWord(txtcp){
	var regCp=new RegExp("([a-zA-Z])\\1{2,}","i");
	return regCp.test(txtcp);
}
/*=========================================*/
function isWord(txtcp) {
	var regCp=new RegExp("^[a-zA-Z\-äéèëêïîôüû \']{3,}$","i");
	return regCp.test(txtcp);
}

/*=========================================*/
function isDate(txtcp) {
	var regCp=new RegExp("(?:0[1-9]|[12][0-9]|3[01])/(?:0[1-9]|1[012])/[12][09][0-9]{2}","i");
	return regCp.test(txtcp);
}

/*========================================*/
function messageSend(message,e){
	
	element=document.getElementById('message');
	
	element.innerHTML = message;		
	element.style.display='block';
	
	if(e!=null){
		e.className= "input_erreur";
		e.onfocus=effect;
	}
}
/*========================================*/
function effect()
{
	element=document.getElementById('message');
	if(element.style.display!='none'){
		
		var tableau_erreur=document.getElementsByClassName("input_erreur");
			
		for(var i=0; i<tableau_erreur.length;i++){
			tableau_erreur[i].className= "input_text";
		}
			
		if(compteur != 50){
			element.style.Top= '-'+compteur+'px';
			
			compteur ++;
			setTimeout("effect()",20);
		}else{
			compteur=0;
			element.style.Top= '-'+compteur+'px';
			element.style.display='none';
			
		}
	}
}
/*========================================*/

function resetInput(e){
	if(e.value=="jj/mm/aaaa"){
		e.value="";
	}
}

function plainInput(e){
	if(e.value==""){
		e.value="jj/mm/aaaa";
		
	}
}
/*=======================================*/
function fermer_popup_mail(){
		element=document.getElementById('pop-up_mail');
		element.style.display='none';
		effect();
}
		
function ouvrir_popup_mail() {
		element=document.getElementById('pop-up_mail');
		element.style.display='block';
}
/*=====================================*/

function show(){
	if(video!=true){
	element=document.getElementById('videoConcour')	;
	element.innerHTML='<object width="350" height="196"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=9461575&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=9461575&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="350" height="196"></embed></object>';
	video =true;
	}
}function AutoSuggest(elem, suggestions)
{

	//The 'me' variable allow you to access the AutoSuggest object
	//from the elem's event handlers defined below.
	var me = this;

	//A reference to the element we're binding the list to.
	this.elem = elem;

	this.suggestions = suggestions;

	//Arrow to store a subset of eligible suggestions that match the user's input
	this.eligible = new Array();

	//The text input by the user.
	this.inputText = null;

	//A pointer to the index of the highlighted eligible item. -1 means nothing highlighted.
	this.highlighted = -1;

	//A div to use to create the dropdown.
	this.div = document.getElementById("autosuggest");


	//Do you want to remember what keycode means what? Me neither.
	var TAB = 13;
	var ESC = 27;
	var KEYUP = 38;
	var KEYDN = 40;
	

	//The browsers' own autocomplete feature can be problematic, since it will 
	//be making suggestions from the users' past input.
	//Setting this attribute should turn it off.
	elem.setAttribute("autocomplete","off");

	//We need to be able to reference the elem by id. If it doesn't have an id, set one.
	if(!elem.id)
	{
		var id = "autosuggest" + idCounter;
		idCounter++;

		elem.id = id;
	}


	/********************************************************
	onkeydown event handler for the input elem.
	Tab key = use the highlighted suggestion, if there is one.
	Esc key = get rid of the autosuggest dropdown
	Up/down arrows = Move the highlight up and down in the suggestions.
	********************************************************/
	elem.onkeydown = function(ev)
	{
		var key = me.getKeyCode(ev);

		switch(key)
		{
			case TAB:
			me.useSuggestion();
			break;

			case ESC:
			me.hideDiv();
			break;

			case KEYUP:
			if (me.highlighted > 0)
			{
				me.highlighted--;
			}
			me.changeHighlight(key);
			break;

			case KEYDN:
			if (me.highlighted < (me.eligible.length - 1))
			{
				me.highlighted++;
			}
			me.changeHighlight(key);
			break;
		}
	};

	/********************************************************
	onkeyup handler for the elem
	If the text is of sufficient length, and has been changed, 
	then display a list of eligible suggestions.
	********************************************************/
	elem.onkeyup = function(ev) 
	{
		var key = me.getKeyCode(ev);
		switch(key)
		{
		//The control keys were already handled by onkeydown, so do nothing.
		case TAB:
		case ESC:
		case KEYUP:
		case KEYDN:
			return;
		default:

			if (this.value != me.inputText && this.value.length > 0)
			{
				me.inputText = this.value;
				me.getEligible();
				me.createDiv();
				me.positionDiv();
				me.showDiv();
			}
			else
			{
				me.hideDiv();
			}
		}
	};


	/********************************************************
	Insert the highlighted suggestion into the input box, and 
	remove the suggestion dropdown.
	********************************************************/
	this.useSuggestion = function()
	{
		if (this.highlighted > -1)
		{
			this.elem.value = this.eligible[this.highlighted];
			this.hideDiv();
			//It's impossible to cancel the Tab key's default behavior. 
			//So this undoes it by moving the focus back to our field right after
			//the event completes.
			setTimeout("document.getElementById('" + this.elem.id + "').focus()",0);
		}
	};

	/********************************************************
	Display the dropdown. Pretty straightforward.
	********************************************************/
	this.showDiv = function()
	{
		this.div.style.display = 'block';
	};

	/********************************************************
	Hide the dropdown and clear any highlight.
	********************************************************/
	this.hideDiv = function()
	{
		this.div.style.display = 'none';
		this.highlighted = -1;
	};

	/********************************************************
	Modify the HTML in the dropdown to move the highlight.
	********************************************************/
	this.changeHighlight = function()
	{
		var lis = this.div.getElementsByTagName('LI');
		for (i in lis)
		{
			var li = lis[i];

			if (this.highlighted == i)
			{
				li.className = "selected";
			}
			else
			{
				li.className = "";
			}
		}
	};

	/********************************************************
	Position the dropdown div below the input text field.
	********************************************************/
	this.positionDiv = function()
	{
		var el = this.elem;
		var x = 0;
		var y = el.offsetHeight;
	
		//Walk up the DOM and add up all of the offset positions.
		while (el.offsetParent && el.tagName.toUpperCase() != 'BODY')
		{
			x += el.offsetLeft;
			y += el.offsetTop;
			el = el.offsetParent;
		}

		x += el.offsetLeft;
		y += el.offsetTop;

		this.div.style.left = x + 'px';
		this.div.style.top = y + 'px';
	};

	/********************************************************
	Build the HTML for the dropdown div
	********************************************************/
	this.createDiv = function()
	{
		var ul = document.createElement('ul');
	
		//Create an array of LI's for the words.
		for (i in this.eligible)
		{
			var word = this.eligible[i];
	
			var li = document.createElement('li');
			var a = document.createElement('a');
			a.href="javascript:false";
			a.innerHTML = word;
			li.appendChild(a);
	
			if (me.highlighted == i)
			{
				li.className = "selected";
			}
	
			ul.appendChild(li);
		}
	
		this.div.replaceChild(ul,this.div.childNodes[0]);
	

		/********************************************************
		mouseover handler for the dropdown ul
		move the highlighted suggestion with the mouse
		********************************************************/
		ul.onmouseover = function(ev)
		{
			//Walk up from target until you find the LI.
			var target = me.getEventSource(ev);
			while (target.parentNode && target.tagName.toUpperCase() != 'LI')
			{
				target = target.parentNode;
			}
		
			var lis = me.div.getElementsByTagName('LI');
			
	
			for (i in lis)
			{
				var li = lis[i];
				if(li == target)
				{
					me.highlighted = i;
					break;
				}
			}
			me.changeHighlight();
		};

		/********************************************************
		click handler for the dropdown ul
		insert the clicked suggestion into the input
		********************************************************/
		ul.onclick = function(ev)
		{
			me.useSuggestion();
			me.hideDiv();
			me.cancelEvent(ev);
			return false;
		};
	
		this.div.className="suggestion_list";
		this.div.style.position = 'absolute';

	};

	/********************************************************
	determine which of the suggestions matches the input
	********************************************************/
	this.getEligible = function()
	{
		this.eligible = new Array();
		for (i in this.suggestions) 
		{
			var suggestion = this.suggestions[i];
			
			if(suggestion.toLowerCase().indexOf(this.inputText.toLowerCase()) == "0")
			{
				this.eligible[this.eligible.length]=suggestion;
			}
		}
	};

	/********************************************************
	Helper function to determine the keycode pressed in a 
	browser-independent manner.
	********************************************************/
	this.getKeyCode = function(ev)
	{
		if(ev)			//Moz
		{
			return ev.keyCode;
		}
		if(window.event)	//IE
		{
			return window.event.keyCode;
		}
	};

	/********************************************************
	Helper function to determine the event source element in a 
	browser-independent manner.
	********************************************************/
	this.getEventSource = function(ev)
	{
		if(ev)			//Moz
		{
			return ev.target;
		}
	
		if(window.event)	//IE
		{
			return window.event.srcElement;
		}
	};

	/********************************************************
	Helper function to cancel an event in a 
	browser-independent manner.
	(Returning false helps too).
	********************************************************/
	this.cancelEvent = function(ev)
	{
		if(ev)			//Moz
		{
			ev.preventDefault();
			ev.stopPropagation();
		}
		if(window.event)	//IE
		{
			window.event.returnValue = false;
		}
	}
}

//counter to help create unique ID's
var idCounter = 0;
