//Ouverture d'une pop up
function ouvreFenetre(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
win=window.open(mypage,myname,settings);}

function confirmation(URL,MSG)
{
	if(confirm(MSG))
	{
		document.location.href=URL;
	}
}

function redirect(URL)
{
	document.location.href=URL;
}

function verif(email) {
   var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
   return (reg.exec(email)!=null)
}

function limite(textarea, max)
{
	var affichage_reste = '';
	var pluriel = '';
    if(textarea.value.length >= max)
    {
        textarea.value = textarea.value.substring(0,max);
    }
    var reste = max - textarea.value.length;    
	if(reste == 0) { affichage_reste = 'Vous avez atteint les '+max+' caractères !'; }
	else {
		if(reste > 1) { pluriel = 's'; } else { pluriel = ''; }
		affichage_reste =  reste +' caractère'+pluriel+' restant';
	}
    document.getElementById('max_desc').innerHTML = affichage_reste;
}

function ShowHide(LayerID)
{
	$(document).ready(function () {
		if($("#"+LayerID).is(':visible')) $("#"+LayerID).slideUp('fast');
		if($("#"+LayerID).is(':hidden')) $("#"+LayerID).slideDown('fast');
	});
}

function enSavoirPlus(nbID,myID)
{
	for(var i=0; i < nbID; i++)
	{
		var vImg = $("#cVignette_"+i).attr("rel");		
		var myPage = $("#cBox_"+i).attr("rel");
		enSavoirMoins(nbID,i,myPage);
	}
	
	var myImg = $("#cVignette_"+myID).attr("rel");
	var myPage = $("#cBox_"+myID).attr("rel");
	$("#cBox_"+myID).css({ 'background-color':'#1e1e1e' });
	$("#cInfos_"+myID).css({ 'width':'200px' });
	$("#cVignette_"+myID).css({ 'width':'200px' });
	if(myImg != "photo-indisponible.jpg") { $("#cVignette_"+myID).html( '<a class="lightbox" href="/data/'+myPage+'/images/'+myImg+'" title="Cliquez pour agrandir"><img src="/data/'+myPage+'/vignettes/'+myImg+'" /></a>');	}
	else { $("#cVignette_"+myID).html( '<img src="/data/'+myPage+'/vignettes/'+myImg+'" />'); }
	$("#cDetails_"+myID).css({ 'width':'500px' });
	$("#cDescription_"+myID).slideUp('fast');
	$("#cTexteDetaille_"+myID).slideDown('slow');	
	$("#cCoordonnees_"+myID).css({ 'width':'200px' });
	$("#cCoordonnees_"+myID).slideDown('slow');	
	$("#cESP_"+myID).slideUp('fast');
	$("#cESM_"+myID).slideDown('fast');	
	// $("#cBox_"+myID).focus(scrollGo);
	$('a.lightbox').lightBox();	
}

function enSavoirMoins(nbID,myID)
{
	var myImg = $("#cVignette_"+myID).attr("rel");
	var myPage = $("#cBox_"+myID).attr("rel");
	$("#cBox_"+myID).css({ 'background-color':'transparent' });
	$("#cInfos_"+myID).css({ 'width':'100px' });
	$("#cVignette_"+myID).css({ 'width':'100px' });
	$("#cVignette_"+myID).html('<a href="javascript:void(0);" onClick="javascript:enSavoirPlus('+nbID+','+myID+');" title="En savoir plus"><img src="/data/'+myPage+'/vignettes/v-'+myImg+'" /></a>');
	$("#cDetails_"+myID).css({ 'width':'520px' });
	$("#cDescription_"+myID).slideDown('fast');
	$("#cTexteDetaille_"+myID).slideUp('slow');	
	$("#cCoordonnees_"+myID).css({ 'width':'100px', 'display':'none' });
	$("#cESP_"+myID).slideDown('fast');
	$("#cESM_"+myID).slideUp('fast');
	// $("#cBox_"+myID).focus(scrollGo);
}

function scrollGo() {
	var targetOffset = $(this).offset().top -200;
	$('html,body').animate({ 'scrollTop':targetOffset }, 1000);
}

function disableAllFields(myForm) {
	var form = document.forms[0];	
	var el = form.elements;
	for(var i=0;i<el.length;i++)
	{
		if(el[i].name != "CO_type")
		{
			el[i].disabled=true;
		}
	}
}

function enableAllFields(myForm) {
	var form = document.forms[0];	
	var el = form.elements;
	for(var i=0;i<el.length;i++)
	{
		el[i].disabled=false;		
	}
}

function checkValue(formValue,formName) {	
	if(formValue != "")
	{
		for(var i=1; i <= 3; i++)
		{
			$("#COform_"+i).slideUp("fast");
		}
		switch(formValue)
		{
			case "Exposant":				
				$("#COform_1").slideDown("slow");
				break;
			case "Organisateur":	
				$("#COform_2").slideDown("slow");			
				break;
			case "Visiteur":				
				break;
			default:
		}
		enableAllFields();
	}
	else
	{
		for(var i=1; i <= 3; i++)
		{
			$("#COform_"+i).slideUp("fast");
		}
		disableAllFields();
	}
}

function openprompt(o){
	o = jQuery.extend({},{ state:0 },o);	
	var temp = {			
		state0: {
			html:
				'<h2 class="textRed">Identification requise</h2>'+
				'L´accès à la Galerie photos "haute définition" et autres éléments du dossier de presse des manifestations est réservé aux journalistes, médias et professionnels partenaires de Savoiexpo.'+
				'<div class="pointilles"></div>'+				
				'<form id="jqLogin" method="POST" action="point-presse_communiques-photos.html">'+
				'<div class="field"><label for="AP_login" class="popup"><b>Votre identifiant</b></label><input type="text" name="AP_login" id="AP_login" value="" style="width:200px;" /></div>'+
				'<div class="field"><label for="AP_password" class="popup"><b>Votre mot de passe</b></label><input type="password" name="AP_password" id="AP_password" value="" style="width:200px;" /></div>'+
				'<input type="hidden" name="MY_adresse" value="oui" />'+
				'<input type="hidden" name="AP_validation" value="oui" />'+
				'</form>',
			buttons: { 'Valider': 3, 'S´inscrire': 2, 'Mot de passe perdu ?': 1, 'Annuler': 0 },
			focus: 0,
			submit:function(v,m,f) { 
				var e = "";
				m.find('.errorBlock').hide('fast',function(){ jQuery(this).remove(); });
				if(v==3) {					
					if(f.AP_login=="") { e += "Veuillez entrer votre identifiant SVP<br />"; }
					if(f.AP_password=="") { e += "Veuillez entrer votre mot de passe SVP<br />"; }
					if(e=="") {
						$('#jqLogin').submit();
					}
					else {
						jQuery('<div class="errorBlock" style="display: none;">'+ e +'</div>').prependTo('#jqLogin').slideDown('fast');
					}
				}
				else if(v==2)	$.prompt.goToState('state2');
				else if(v==1)	$.prompt.goToState('state1');
				else if(v==0) $.prompt.close()
				return false; 
			}
		},
		state1: {			
			html:
				'<h2 class="textRed">Mot de passe perdu !</h2>'+
				'Veuillez saisir l’adresse e-mail utilisée lors de votre inscription pour le recevoir à nouveau sur cette adresse :'+
				'<form id="jqRecup" method="POST" action="point-presse_communiques-photos.html">'+
				'<div class="field"><label for="RE_email"><b>Votre adresse email</b></label><input type="text" name="RE_email" id="RE_email" value="" style="width:200px;" /></div>'+
				'<input type="hidden" name="RE_recuperation" value="oui" />'+
				'</form>',
			buttons: { 'Valider': 1, 'Retour': -1, 'Annuler': 0 },
			focus: 0,
			submit:function(v,m,f) { 
				var e = "";
				m.find('.errorBlock').hide('fast',function(){ jQuery(this).remove(); });
				if(v==1) {						
					if(f.RE_email=="") { e += "Veuillez entrer votre adresse email SVP<br />"; }
					else if(!verif(f.RE_email)) { e += "Veuillez entrer une adresse email valide SVP<br />"; }
					if(e=="") {
						$('#jqRecup').submit();
					}
					else {
						jQuery('<div class="errorBlock" style="display: none;">'+ e +'</div>').prependTo('#jqRecup').slideDown('fast');
						return false;
					}					
				}
				else if(v==-1) $.prompt.goToState('state0');
				else if(v==0) $.prompt.close()
				return false; 
			}
		},
		state2: {
			html:
				'<h2 class="textRed">Inscription</h2>'+
				'L´accès à la Galerie photos "haute définition" et autres éléments du dossier de presse des manifestations est réservé aux journalistes, médias et professionnels partenaires de Savoiexpo.'+				
				'<div class="pointilles"></div>'+
				'<div class="tCenter">(Les champs en gras sont obligatoires)</div>'+
				'<form id="jqRegister" method="POST" action="point-presse_communiques-photos.html">'+
				'<div class="field"><label for="AP_nom"><b>Nom</b></label><input type="text" name="AP_nom" id="AP_nom" value="" style="width:200px;" /></div>'+
				'<div class="field"><label for="AP_prenom">Prénom</label><input type="text" name="AP_prenom" id="AP_prenom" value="" style="width:200px;" /></div>'+
				'<div class="field"><label for="AP_societe"><b>Société/Titre média représenté</b></label><input type="text" name="AP_societe" id="AP_societe" value="" style="width:200px;" /></div>'+
				'<div class="field"><label for="AP_email"><b>Adresse email</b></label><input type="text" name="AP_email" id="AP_email" value="" style="width:200px;" /></div>'+
				'<input type="hidden" name="AP_register" value="oui" />'+
				'</form>',
			buttons: { 'Valider': 1, 'Retour': -1, 'Annuler': 0 },
			focus: 0,
			submit:function(v,m,f) {
				var e = "";
				var ajaxReturn = "";
				m.find('.errorBlock').hide('fast',function(){ jQuery(this).remove(); });
				if(v==1) {
					if(f.AP_nom=="") { e += "Veuillez entrer votre Nom SVP<br />"; }					
					if(f.AP_societe=="") { e += "Veuillez entrer votre Société/Titre média représenté SVP<br />"; }					
					if(f.AP_email=="") { e += "Veuillez entrer votre adresse email SVP<br />"; }
					else if(!verif(f.AP_email)) { e += "Veuillez entrer une adresse email valide SVP<br />"; }
					else {						
						var ajaxReturn = $.ajax({
							type: "POST",							
							dataType: "html",
							async : false,
							data: {"AP_email": f.AP_email},
							url: "ajax.req.php?id=emailUnique",
							success: function(dataReturn) {
								e += dataReturn;
							}
						});						
					}
					if(e=="") {
						$('#jqRegister').submit();
					}
					else {
						jQuery('<div class="errorBlock" style="display: none;">'+ e +'</div>').prependTo('#jqRegister').slideDown('fast');
					}
				}
				else if(v==-1) $.prompt.goToState('state0');
				else if(v==0) $.prompt.close();
				return false; 
			}
		},		
		state3: {
			html:
				'<h2 class="textRed">Identification requise</h2>'+
				'L’identifiant ou le mot de passe est incorrect ! Veuillez réessayer de vous connecter SVP',
			buttons: { 'Retour': -1, 'Annuler': 0 },
			focus: 0,
			submit:function(v,m,f){ 
				if(v==0) $.prompt.close();
				else if(v=-1) $.prompt.goToState('state0');
				return false; 
			}
		},
		state4: {
			html:
				'<h2 class="textRed">Point Presse - Photos</h2>'+
				'Merci de vous être identifier !',
			buttons: { 'Ok': true },
			focus: 0,
			submit:function(v,m,f){ 
				if(v)
					$.prompt.close();
				return false; 
			}
		},
		state5: {
			html:
				'<h2 class="textRed">Point Presse - Photos</h2>'+
				'Vos identifiants de connexion vous ont été envoyés par email.',
			buttons: { 'Ok': true },
			focus: 0,
			submit:function(v,m,f){ 
				if(v) $.prompt.close();				
				return false; 
			}
		},
		state6: {
			html:
				'<h2 class="textRed">Point Presse - Photos</h2>'+
				'L´adresse email renseignée est inconnue. Veuillez réessayer SVP!',
			buttons: { 'Retour': -1, 'Annuler': 0 },
			focus: 0,
			submit:function(v,m,f){ 
				if(v==0) $.prompt.close();
				else if(v==-1) $.prompt.goToState('state1');
				return false; 
			}
		}
	}
	$.prompt(temp,{ startFrom:'state'+o.state	});
}

