var msgs = new Array();

msgs["fr"] = new Object();
msgs["fr"]["error_req"] = "Vous n'avez pas rempli tous les champs requis !";
msgs["fr"]["error_cond"] = "Vous n'avez pas accepté les cgv !";
msgs["fr"]["error_upload"] = "La photo n'a pas pu etre téléchargée (elle doit être au format .jpg et d'une taille maximale de 1Mo)";
msgs["fr"]["error_allready_pseudo"] = "Ce pseudo existe déjà ! Veuillez en choisir un autre svp.";
msgs["fr"]["error_allready_email"] = "Cette adresse email a déjà été enregistrée!";
msgs["fr"]["error_password"] = "Mot de passe incorrect! Il doit comporter au moins 5 caracteres et etre identique.";

msgs["de"] = new Object();
msgs["de"]["error_req"] = "Es sind noch leere Pflichtfelder auszufüllen !";
msgs["de"]["error_cond"] = "Sie haben die AGBs nicht akzeptiert !";
msgs["de"]["error_upload"] = "Ein Fehler ist aufgetretten (Das Bild soll in einem Format .jpg mit eine maximale Grösse von 1Mo sein)";
msgs["de"]["error_allready_pseudo"] = "Ce pseudo existe déjà ! Veuillez en choisir un autre svp.";
msgs["de"]["error_allready_email"] = "Cette adresse email a déjà été enregistrée!";
msgs["de"]["error_password"] = "Kenntwort Fehler! Es muss mindestens 5 Zeichen lang sein.";

msgs["en"] = new Object();
msgs["en"]["error_req"] = "Please fill in all required fields !";
msgs["en"]["error_cond"] = "You must agree our terms and conditions !";
msgs["en"]["error_upload"] = "Picture not uploaded (It should have the format .jpg and the maximum size of 1Mo)";
msgs["en"]["error_allready_pseudo"] = "Ce pseudo existe déjà ! Veuillez en choisir un autre svp.";
msgs["en"]["error_allready_email"] = "Cette adresse email a déjà été enregistrée!";
msgs["en"]["error_password"] = "Wrong password! It muss consist on at least 5 character.";

msgs["tr"] = new Object();
msgs["tr"]["error_req"] = "Vous n'avez pas rempli tous les champs requis !";
msgs["tr"]["error_cond"] = "Vous n'avez pas accepté les cgv !";
msgs["tr"]["error_upload"] = "Jpg dosyasi almali ve en fazla 1Mo boyutunda";
msgs["tr"]["error_allready_pseudo"] = "Ce pseudo existe déjà ! Veuillez en choisir un autre svp.";
msgs["tr"]["error_allready_email"] = "Cette adresse email a déjà été enregistrée!";
msgs["tr"]["error_password"] = "Mot de passe incorrect! Il doit comporter au moins 5 caracteres et etre identique.";

msgs["nl"] = new Object();
msgs["nl"]["error_req"] = "Vous n'avez pas rempli tous les champs requis !";
msgs["nl"]["error_cond"] = "Vous n'avez pas accepté les cgv !";
msgs["nl"]["error_upload"] = "La photo n'a pas pu etre téléchargée (elle doit être au format .jpg et d'une taille maximale de 1Mo)";
msgs["nl"]["error_allready_pseudo"] = "Ce pseudo existe déjà ! Veuillez en choisir un autre svp.";
msgs["nl"]["error_allready_email"] = "Cette adresse email a déjà été enregistrée!";
msgs["nl"]["error_password"] = "Mot de passe incorrect! Il doit comporter au moins 5 caracteres et etre identique.";


/* *** INIT START PAGE FUNCTIONS *** */
function onStartPageLoading(params){
		
	/** RESETING THE CONNECTED MEMBERS **/
	resetStatutConnectedMembers();
	
	/** FANCYBOXES **/
	$("a.iframe").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 500,
		'frameHeight': 400,
		'centerOnScroll': true
	});
	
	
	/** BLOC PHOTO FLASH **/
	$('.blocphoto').media({ 
		width:     364, 
		height:    364, 
		autoplay:  true, 
		src:       '/global/gfx/home.swf',
		flashvars: { rand: Math.random() },
		caption:   false // supress caption text 
	});
	
	/** LOGIN FORM **/
	initLoginForm();
	
	/** REGISTRATION FORM **/
	loadRegistrationFormContoler();

	/** CAROUSSEL **/
	$('#mycarousel').jcarousel({
		auto: 0.01,
		animation: 2000,
		scroll: 1,
		wrap: 'circular'
	});
}

/* *** ALL ABOUT THE REGISTRATION'S FORM *** */

function handlingDatesFields(){
	$("#jdate").keyup(function(event){ 
		var strVal = $("#jdate").val();
		//taille de saisie = 2 et touche differente de "retour arriere"
		if( strVal.length == 2 && event.keyCode != '8' ){ 
			$("#mdate").focus();
		}
	});
	$("#mdate").keyup(function(event){ 
		var strVal = $("#mdate").val();
		//taille de saisie = 2 et touche differente de "retour arriere"
		if( strVal.length == 2 && event.keyCode != '8' ){ 
			$("#adate").focus();
		}
	});
	$("#adate").keyup(function(event){ 
		var strVal = $("#adate").val();
		//taille de saisie = 4 et touche differente de "retour arriere"
		if( strVal.length == 4 && event.keyCode != '8' ){ 
			chekAndAlertOnDateError('jdate','mdate','adate');
		}
	});
	
	flushDefaultValueOnFocus("jdate");
	flushDefaultValueOnFocus("mdate");
	flushDefaultValueOnFocus("adate");
}

		
function loadRegistrationFormContoler(){
	
	handlingDatesFields();
	
	//charge les regions correspondantes au pays selectionné
	$("#pays").change(function(){ 
		getRegionsByLandOnRegistration( $("#pays").val() );
	});
	
	//choisi la ville en fonction du code postal
	$("#codepostal").blur(function(){ 
		var pays = $("#pays").val();
		var codepostal = $("#codepostal").val();
		getCitiesByLandPostalCode(pays, codepostal);
	});
	
	//teste le mail en quittant le champ email
	$("#email").blur(function(){ 
		if( ! checkEmail( $("#email").val()  ) ){
			highlightErrorOnField('email', true);
		}
	});
	
	$("#email").blur(function(){ 
		checkNeverRegistered("email", $("#email").val());
	});
	
	//teste le pseudo en quittant le champ pseudo
	$("#pseudo").blur(function(){ 
		checkNeverRegistered("pseudo", $("#pseudo").val());
	});
	
	//teste le mot de passe en quitant le champ de confirmation
	$("#passwdConfirm").blur(function(){ 
		if( ! checkPasswordConfirmation( $("#passwd").val(), $("#passwdConfirm").val() ) ){
			highlightErrorOnField('passwd', true);
			highlightErrorOnField('passwdConfirm', true);
			var lang = $("#msg_registration_error01").attr("lang");
			$("#msg_registration_error01").html(msgs[lang]["error_password"]);
			$("#msg_registration_error01").show();
			valid = false;
		}
	});
	
	//teste la validite de la date en quittant les champs de date
	$("#jdate").blur(function(){ 
		chekAndAlertOnDateError('jdate','mdate','adate');
	});
	$("#mdate").blur(function(){ 
		chekAndAlertOnDateError('jdate','mdate','adate');
	});
	$("#adate").blur(function(){ 
		chekAndAlertOnDateError('jdate','mdate','adate');
	});
	
	
	//vide le champ en se placant dessus
	$("#jdate").focus(function(){ $("#jdate").val(''); });
	$("#mdate").focus(function(){ $("#mdate").val(''); });
	$("#adate").focus(function(){ $("#adate").val(''); });
	
			
	/* Envoi formulaire 1ere etape inscription */
	$("#bt_inscription01").click(function(){ 
		if( checkRegistrationForm01() ){
			doRegistration01();
		}
	});
	
	/* Envoi formulaire 2nde etape inscription */
	$("#bt_inscription02").click(function(){ 
		if( checkRegistrationForm02() ){
			doRegistration02();
		}
	});
	
	/* Envoi formulaire 2nde etape inscription */
	$("#bt_upload03").click(function(){ 
		if( checkRegistrationForm03() ){
			$("#msg_registration_error03").hide();
			uploadAvatar();
		}
	});
	
	$("#deleteAvatar").click(function(){ 
		deleteAvatar();
	});
	
	/* Inscription termin�e */
	$("#bt_terminer03").click(function(){ 
		saveAvatar();
	});
}
		
/* Controle formulaire 1ere etape inscription */
function checkRegistrationForm01(){
	var valid = true;
	var lang = $("#msg_registration_error01").attr("lang");
	
	//remove error highlight
	removehighlightOnFields("req");
	
	//erreur champ requis
	valid = notemptyRequiredFields("req", lang, "msg_registration_error01");
	
	//erreur email format
	if( ! checkEmail( $("#email").val()  ) ){
		highlightErrorOnField('email', true);
		valid = false;
	}
		
	//erreur date format
	var dateString = $("#jdate").val() + $("#mdate").val() + $("#adate").val();
	if( ! chekAndAlertOnDateError('jdate','mdate','adate') ){
		valid = false;
	}
	
	
	//passwords differents
	if( ! checkPasswordConfirmation( $("#passwd").val(), $("#passwdConfirm").val() ) ){
		highlightErrorOnField('passwd', true);
		highlightErrorOnField('passwdConfirm', true);
		var lang = $("#msg_registration_error01").attr("lang");
		$("#msg_registration_error01").html(msgs[lang]["error_password"]);
		$("#msg_registration_error01").show();
		valid = false;
	}
		
	//erreur CGV
	if( ! $("#cgv").attr("checked") ){
		$("#msg_registration_error01").html(msgs[lang]["error_cond"]);
		$("#msg_registration_error01").show();
		valid = false;
	}
	return valid;
}

/* Appelle la fonction Ajax pour enregistrement en bdd */
function doRegistration01() {        
	var dataStr = $("#registrationForm01").serialize();
	$.ajax({
		type: "POST",
		url: "/global/inc/ajax/inscription_etape01.inc.php",
		data: dataStr,
		success: function(uid){
			
			if( uid == "pseudo_allready_registered" ){
				property = "pseudo";
				highlightErrorOnField(property, true);
				var lang = $("#msg_registration_error01").attr("lang");
				$("#msg_registration_error01").html(msgs[lang]["error_allready_"+property]);
				$("#msg_registration_error01").show();
			}
			
			else if( uid == "email_allready_registered"){
				property = "email";
				highlightErrorOnField(property, true);
				var lang = $("#msg_registration_error01").attr("lang");
				$("#msg_registration_error01").html(msgs[lang]["error_allready_"+property]);
				$("#msg_registration_error01").show();
			}
			
			else if( ! isNaN(uid) ) {
				//save the user id
				$("#newMember02").val(uid);
				$("#panelRegistration01").hide();
				$("#panelRegistration02").show();
			}
		}
	});

};       


/* Controle formulaire 2nde etape inscription */
function checkRegistrationForm02(){
	var valid = true;
	//remove error highlight
	removehighlightOnFields("req2");
	var lang = $("#msg_registration_error02").attr("lang");
	//erreur champ requis
	valid = notemptyRequiredFields("req2", lang, "msg_registration_error02");
	
	//verifie le champ ville rempli par l'utilisateur
	//	if( valid ){
	//		if( typeof( $("#villeFilled") != "undefined") ){
	//			valid = $("#villeFilled").val() == "1" ? true : false;
	//			highlightErrorOnField("ville", valid);
	//		}
	//	}
	
	return valid;
}

/* Appelle la fonction Ajax pour enregistrement en bdd */
function doRegistration02() {        
	var dataStr = $("#registrationForm02").serialize();
	$.ajax({
		type: "POST",
		url: "/global/inc/ajax/inscription_etape02.inc.php",
		data: dataStr,
		success: function(uid){
			//save the user id
			$("#newMember03").val(uid);
			
			$("#panelRegistration02").hide();
			$("#panelRegistration03").show();
			return true;
		}
	});
	return false;
};   

/* Controle formulaire 3eme etape inscription */
function checkRegistrationForm03(){
	var valid = true;
	var lang = $("#msg_registration_error01").attr("lang");
	//erreur champ requis
	valid = notemptyRequiredFields("req3", lang, "msg_registration_error03");
	return valid;
}

/* Appelle la fonction Ajax pour upload de l'avatar et enregistrement en bdd */
function uploadAvatar() {
	document.forms.form_envoi_avatar.submit();
	var dataStr = $("#registrationForm03").serialize();
	$.ajax({
		type: "POST",
		url: "/global/inc/ajax/inscription_etape03.inc.php",
		data: dataStr,
		success: function(res){
			if(res != 0 ){
				$("#appercuAvatarImg").attr("src",'/global/gfx/ic-nopicture-man-185.gif');
				$("#appercuAvatarImg").attr("src",res);
				$("#deleteAvatar").show();
				$("#avatar").val('');
				return true;
			}else{
				var lang = $("#msg_registration_error01").attr("lang");
				$("#msg_registration_error03").html(msgs[lang]["error_upload"]);
				$("#msg_registration_error03").show();
			}
		}
	});
	return false;
};

function saveAvatar(){
	$.ajax({
		type: "POST",
		url: "/global/inc/ajax/inscription_etape03.inc.php?action=confirm&newMember="+$("#newMember03").val(),
		success: function(html){
			$("#infoActivation").html(html);
			$("#panelRegistration03").hide();
			$("#panelRegistration04").show();
			return true;
		}
	});
	return false;
}

function deleteAvatar(){
	$.ajax({
		type: "POST",
		url: "/global/inc/ajax/inscription_etape03.inc.php?action=delete",
		success: function(res){
			$("#appercuAvatarImg").attr("src",'/global/gfx/ic-nopicture-man-185.gif');
			$("#deleteAvatar").hide();
			return true;
		}
	});
	return false;
}

function checkNeverRegistered(property, value){
	$.ajax({
		type: "GET",
		url: "/global/inc/ajax/inscription_check.inc.php?prop="+property+"&val="+value,
		success: function(allready){
			if( allready == 1 ){
				highlightErrorOnField(property, true);
				var lang = $("#msg_registration_error01").attr("lang");
				$("#msg_registration_error01").html(msgs[lang]["error_allready_"+property]);
				$("#msg_registration_error01").show();
			}else{
				highlightErrorOnField(property, false);
				$("#msg_registration_error01").hide();
			}
		}
	});
}

function getRegionsByLandOnRegistration(landCode){
	$.ajax({
		type: "POST",
		url: "/global/inc/ajax/inscription_getRegionsByLand.inc.php?landCode="+landCode,
		success: function(res){
			$("#liRegion").html(res);
		}
	});
}

function getCitiesByLandPostalCode(landCode, postalCode){
	var loading = '<img src="/global/gfx/ajax-loader-msgs.gif" width="280px" height="20px" />';
	$("#divVille").html(loading);
	
	$.ajax({
		type: "POST",
		url: "/global/inc/ajax/inscription_getCitiesByLandPostalCode.inc.php?landCode="+landCode+"&postalCode="+postalCode,
		success: function(res){
			$("#divVille").html( res );
		}
	});
}

function resendActivationOnRegistration(newMember){
	$.ajax({
		type: "GET",
		url: "/global/inc/ajax/account_resendActivationEmail.inc.php?newMember="+newMember,
		success: function(msg){
			var html = prepareFancyBox(msg);
			$("#fancyOnRegistration").html(html);
			$("#fancyOnRegistrationClick").click();
		}
	});
}
