﻿// JavaScript Document

function verifCodePartenaire()
{
	var theForm = document.getElementById("form-abonnement");
	
	if( theForm.cdpart.value.length <= 0 )
	{
		alert("Veuillez indiquer le code partenaire.");
		theForm.cdpart.focus();
		return ;
	} else {
		var reg=new RegExp("﻿", "g");
			var response = "" ;
			new Ajax.Request('/usweb/',
				{
					method:'post',
					encoding:'UTF-8',
					parameters: {
						module: 'AJAX_CODE_PARTENAIRE',
						code_partenaire: theForm.cdpart.value											
					},
					onSuccess: function(transport)
					{
						response = transport.responseText.replace(reg, "") ; 					
						if(response.length == 0){
							
							alert("Le code partenaire '"+theForm.cdpart.value+"' n'est pas valide.");
							theForm.cdpart.value = "" ;
							theForm.validcdpart.value = "0" ;
							theForm.cdpart.focus();
							return false;
						} else {
							alert("Le code partenaire '"+theForm.cdpart.value+"' est valide.");
							theForm.validcdpart.value = "1" ;
							document.getElementById("cdpart").disabled = true ;
						}
					},
					onFailure: function(){
						alert('Problème de connexion au serveur, merci de renouveler votre action.') ;
						return false ;
					}
				}
			);
	}
}

function verifFormAbo(theForm){

	var num_formule = "";
	if( theForm.formule[0].checked )num_formule = 1 ; // Accès liberté			
	if( theForm.formule[1].checked )num_formule = 2 ; // Accès sérénité
	//if( theForm.formule[0].checked )num_formule = 3 ; // Accès découverte			

	//if( theForm.formule[1].checked && !theForm.paiement[0].checked )// Offre liberté et si 
		//theForm.paiement[1].checked = false ;
	
	if(theForm.societe.value == ""){
		alert("Veuillez indiquer le nom de la Société.");
		theForm.societe.focus();
		return false;
	} else if(theForm.nomMagasin.value == "") {
		alert("Veuillez indiquer le nom du magasin.");
		theForm.nomMagasin.focus();
		return false;
	} else if(theForm.typeCommerce.selectedIndex == 0) {
		alert("Veuillez indiquer le type de commerce.");
		theForm.typeCommerce.focus();
		return false;
	} else if(theForm.statut.selectedIndex == 0) {
		alert("Veuillez indiquer votre statut.");
		theForm.statut.focus();
		return false;
	} else if(theForm.nom.value == "") {
		alert("Veuillez indiquer votre nom.");
		theForm.nom.focus();
		return false;
	} else if(theForm.prenom.value == "") {
		alert("Veuillez indiquer votre prenom.");
		theForm.prenom.focus();
		return false;
	} else if(theForm.adresse.value == "") {
		alert("Veuillez indiquer votre adresse.");
		theForm.adresse.focus();
		return false;
	} else if(theForm.cp.value.length < 5) {
		alert("Veuillez indiquer votre code postal.");
		theForm.cp.focus();
		return false;
	} else if(theForm.localite.value == "") {
		alert("Veuillez indiquer la localité.");
		theForm.localite.focus();
		return false;
	} else if(theForm.email.value == "" || !verifMail(theForm.email.value) ) {
		alert("Veuillez indiquer votre email.");
		theForm.email.focus();
		return false;
	} else if(theForm.motDePasse.value.length < 6) {
		alert("Veuillez indiquer votre mot de passe.");
		theForm.motDePasse.focus();
		return false;
	} else if( theForm.motDePasse.value != theForm.motDePasse2.value ) {
		alert("Veuillez confirmer votre mot de passe.");
		theForm.motDePasse2.focus();
		return false;
	} else if( !theForm.formule[0].checked && !theForm.formule[1].checked ) {
		alert("Veuillez indiquer votre formule.");
		theForm.formule[0].focus();
		return false;
	} else if( ( theForm.formule[0].checked || theForm.formule[1].checked ) && !theForm.paiement[0].checked && !theForm.paiement[1].checked && !theForm.paiement[2].checked ) {
		if( document.getElementById('choixPaiementCheque').style.display == 'none' )
			alert("Veuillez confirmer le choix Paypal pour votre offre liberté.");
		else 
			alert("Veuillez indiquer votre mode de paiement.");
		theForm.paiement[0].focus();
		return false;
	} else if( theForm.paiement[2].checked && theForm.validcdpart.value == "0" ) { // Spécifique Code Partenaire
		alert("Veuillez indiquer ou valider le code partenaire.");
		theForm.cdpart.focus();
		return false;
	} else if( !theForm.cgv.checked ) {
		alert("Pour finaliser votre inscription,\nveuillez accepter les conditions générales de vente.");
		theForm.cgv.focus();
		return false;
	} else {
		confirmInscription(theForm);
		return false ;
	}
}

function confirmInscription(theForm)
{
	var num_formule = "" ;
	if( theForm.formule[0].checked )num_formule = 1 ; // Accès liberté			
	if( theForm.formule[1].checked )num_formule = 2 ; // Accès sérénité
	//if( theForm.formule[0].checked )num_formule = 3 ; // Accès découverte		
	
	if( confirm("Confirmez-vous l'inscription à l'abonnement ?") ){

		// Description de la formule
		theForm.description_formule.value = "" ;

		if( document.getElementById("formule-"+num_formule) != null ) 
			theForm.description_formule.value = document.getElementById("formule-"+num_formule).value ;

		//if( num_formule == 1){theForm.item_name.value =	"Fleurnews.net - Acces liberte" ;}
		//if( num_formule == 2){theForm.item_name.value =	"Fleurnews.net - Acces serenite" ;}

		if( theForm.paiement[0].checked )// Paypal
		{
			// Id BOUTON Paypal
			theForm.hosted_button_id.value = document.getElementById('hosted_button_id_'+num_formule).value ;
			document.getElementById('form-abonnement').action = theForm.link_paypal.value ;
			//theForm.invoice.value = "" ;
			document.getElementById("bouton").value = "Veuillez patienter..."
			
			//alert("Système en cours de réalisation.");
			var response = "" ;
	
			new Ajax.Request('/usweb/',
				{
					method:'post',
					encoding:'UTF-8',
					parameters: {
						module: 'PREINSCRIPTION_ABONNEMENT',
						id_auteur: (theForm.id_auteur != null) ? theForm.id_auteur.value : "" ,
						id_auteur_elargi: ( theForm.id_auteur_elargi != null ) ? theForm.id_auteur_elargi.value : "",

						email_verif: theForm.email.value,
						
						pre_commande: 'oui',
						invoice:theForm.invoice.value,
	
						link_paypal:theForm.link_paypal.value,
						hosted_button_id:theForm.hosted_button_id.value,
						/*item_name:theForm.item_name.value,*/
						
						email:theForm.email.value,
						motDePasse:theForm.motDePasse.value,
						
						paiement:"paypal",
						
						societe:theForm.societe.value,
						nomMagasin:theForm.nomMagasin.value,
						siret:theForm.siret.value,
						codeNA:theForm.codeNA.value,
						tvaIntracommunautaire:theForm.tvaIntracommunautaire.value,
						typeCommerce:theForm.typeCommerce.value,
						statut:theForm.statut.value,
						
						age:theForm.age.value,
						nom:theForm.nom.value,
						prenom:theForm.prenom.value,
						fonction:theForm.fonction.value,
						adresse:theForm.adresse.value,
						cp:theForm.cp.value,
						localite:theForm.localite.value,
						tel:theForm.tel.value,
						
						newsletter:( (theForm.newsletter.checked) ) ? 1 : 0 ,
		
						formule:num_formule,
						
						description_formule:theForm.description_formule.value							
					},
					onSuccess: function(transport)
					{
						var reg=new RegExp("﻿", "g");
						response = transport.responseText.replace(reg, "") ; 
						theForm.invoice.value = response ;							
						theForm.submit();
	
						return false;
					},
					onFailure: function(){
						alert('Problème de connexion au serveur, merci de renouveler votre action.') ;
						return false ;
					}
				}
			);
			return false ;
		} else { // End if paypal
			theForm.submit();
			return false ;
		}
	} else {
		return false ;
	}
}

function verifMotDePasseOublie(theForm)
{
	if(theForm.identifiant.value == "" || !verifMail(theForm.identifiant.value) ) {
		alert("Veuillez indiquer votre email.");
		theForm.identifiant.focus();
		return false;
	} else {
		var response = "" ;
		new Ajax.Request('/usweb/',
			{
				method:'post',
				encoding:'UTF-8',
				parameters: {
					module: 'RAPPEL_IDENTIFIANT_ABONNEMENT',
					identifiant: theForm.identifiant.value
				},
				onLoading: function(transport)
				{
					document.getElementById('bouton_rappel').value = "Veuillez patienter..." ;
				},
				onComplete: function(transport)
				{
					response = transport.responseText ; 		
					if( response.indexOf("-email-exist") != -1 )
					{
						alert("Vous allez recevoir par mail vos accès compte");
					} else {
						alert("L'identifiant indiqué ne fait référence à aucun compte");
					}
					document.getElementById('bouton_rappel').value = "Ok" ;
					return false ;
				},
				onFailure: function()
				{
					alert('Problème de connexion au serveur, merci de renouveler votre action.') ;
					return false ;
				}
			}
		);	
	}
	return false ;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

