var startRGB = new Array(253, 212, 117);
var targetRGB = new Array(255, 255, 255);
var startmotRGB = new Array(0, 0, 0);
var targetmotRGB = new Array(255, 153, 0);
var startdateRGB = new Array(0, 0, 0);
var targetdateRGB = new Array(153, 153, 153);

function doTrans(id, couleur_debut, couleur_fin, styleattribute)
{
    var link = document.getElementById(id);
	 link.curRGB = new Array(couleur_debut[0], couleur_debut[1], couleur_debut[2]);
	 if(link.t_out)
    {
        clearInterval(link.t_out);
        link.t_out = false;
    }
    if(link.t_over) return null;
    link.t_over = setInterval('rgbTrans(\'' + link.id + '\',\'' + couleur_fin[0] + '\',\'' + couleur_fin[1] + '\',\'' + couleur_fin[2] + '\',\'' + styleattribute + '\')', 100);
	link.t_over = false;
}

function rgbTrans(id, r, g, b, styleattribute)
{
    var link = document.getElementById(id);
    var i = 25;
    var curRGB = new Array(link.curRGB[0],link.curRGB[1],link.curRGB[2]);
    var destRGB = new Array(parseInt(r),parseInt(g),parseInt(b));
    if(curRGB[0] == destRGB[0] && curRGB[1] == destRGB[1] && curRGB[2] == destRGB[2])
    {
        if(link.t_over)
        {
            clearInterval(link.t_over);
            link.t_over = false;
        }
        return null;
    }
    for(var j = 0; j < 3; j++)
    {
        if(curRGB[j] > destRGB[j])
        {
            curRGB[j] = curRGB[j]-i > destRGB[j] ? curRGB[j]-i : destRGB[j];
        }
        else if(curRGB[j] < destRGB[j])
        {
            curRGB[j] = curRGB[j]+i < destRGB[j] ? curRGB[j]+i : destRGB[j];
        }
    }
    link.curRGB = curRGB;
	if(styleattribute=='backgroundColor')
	{
   		link.style.backgroundColor = 'rgb('+curRGB[0]+','+curRGB[1]+','+curRGB[2]+')';
	}
	if(styleattribute=='color')
	{
   		link.style.color = 'rgb('+curRGB[0]+','+curRGB[1]+','+curRGB[2]+')';
	}
}

function monCompteListeMariageDisplayOlmo(element){
	// Récupération de la valeur du select => id|tarif
	var valSelect = $j(element).val();
	// Récupération de l'identifiant
	var valSelectExplode = valSelect.split('|');
	var idOlmo = valSelectExplode[0];
	// On cache tous les descriptif
	$j('.listeMariageDescriptifOlomo').hide();
	// On affiche celui qui nous interresse
	$j('#listeMariageDescriptionOlmo_' + idOlmo).show();
	
}

// Crée les onglets
function monCompteListesMariageTabs(){
	$j("#tabsMonCompteListesMariage").tabs();
}


// Ouvre la popup des messages
function openDialogueMessages(){
	// Ouverture de la boite de dialogues
	$j("#dialogueListeMariageMessages").dialog('open');
}

// Ouvre la popup de paiement
function openDialogueInvitePaiement(){
	// Ouverture de la boite de dialogues
	$j("#dialogueListeMariageInvitePaiement").dialog('open');
}

// Ouvre la popup de création d'un invité
function openDialogueInvite(){
	// Ouverture de la boite de dialogues
	$j("#dialogueListeMariageInviteCreer").dialog('open');
}

// Ouvre la popup pour envoyer une invitation
function openDialogueInviteEnvoyer(){
	// Ouverture de la boite de dialogues
	$j("#dialogueListeMariageInviteEnvoyer").dialog('open');
}
	
function openDialgueParticiper(){
	$j("#dialogueListeMariageInviteParticiper").dialog('open');
}

function monCompteListesMariageTabsGoTo(index){
	var $tabs = $j("#tabsMonCompteListesMariage").tabs();
	$tabs.tabs('select', index);
    return false;
}

function changeTypePaiement(){
	$j("#monCompteListeMariageTypePaiement").change(function(){
		$j("#monCompteListeMariageTypesPaiements div.monCompteListeMariageTypePaiement").hide();
		
		if($j(this).val() == 1){
			$j("#monCompteListeMariageTypePaiementNormal").show();
			$j("#ogone_submit").val('Payer en ligne');
		}
		
		if($j(this).val() == 2){
			$j("#monCompteListeMariageTypePaiementOlmo").show();
			$j("#ogone_submit").val('Payer en ligne');
		}
		
		if($j(this).val() == 3){
			$j("#monCompteListeMariageTypePaiementLibre").show();
			$j("#ogone_submit").val('Payer en ligne');
		}
		if($j(this).val() == 4){
			$j("#monCompteListeMariageTypePaiementParticulier").show();
			$j("#ogone_submit").val('Faire une demande');
		}
		
		
	});
}

function monCompteListeMariagePaiementSubmit(){
	var typePaiement = $j("#monCompteListeMariageTypePaiement").val();
	var valMontant = null;
		
	if(typePaiement == 1){
		valMontant = $j("#monCompteListeMariagePaiementNormalMontant").val();
	}
		
	if(typePaiement == 2){
		var valSelect = $j("#monCompteListeMariagePaiementOlmoMontant").val();
		var valSelectExplode = valSelect.split('|');
		valMontant = valSelectExplode[1];
	}
		
	if(typePaiement == 3){
		valMontant = $j("#monCompteListeMariagePaiementLibreMontant").val();
	}
	
	$j("#amount").val(valMontant + '00');
	$j("#form_ogone").submit();
}

// Fonction appelé lorsque une liste de mariage est créée.
function monCompteListeMariageValide()
{
	// Suppression des valeurs des champs
	$j("#fmcCreerListeMariageIdDossier").val('');
	$j("#fmcCreerListeMariageIdClient").val('');
	$j("#fmcCreerListeMariageNom").val('');
	$j("#fmcCreerListeMariageLogin").val('');
	$j("#fmcCreerListeMariagePass1").val('');
	$j("#fmcCreerListeMariagePass2").val('');
	// Fermeture des popups
	$j("#dialogueListeMariage").dialog('close');
	$j("#dialogueListeMariageCreer").dialog('close');
	// Mise à jour de la liste des listes de mariage
	xajax_monCompteListesMariageCharger('monCompteDivListesMariage');
	// Mise à jour de la liste des dossiers pouvant avoir une liste de mariage
	xajax_monCompteListeMariageChargerDossiers('monCompteDivDossiersListesMariage');
}

// Fonction appelé lorsque une liste de mariage est créée.
function monCompteListeMariageInviteValide()
{
	// Suppression des valeurs des champs
	$j("#fmcCreerListeMariageInviteCreerPrenom").val('');
	$j("#fmcCreerListeMariageInviteCreerNom").val('');
	$j("#fmcCreerListeMariageInviteCreerEmail").val('');
	// Fermeture des popups
	$j("#dialogueListeMariageInviteCreer").dialog('close');
	// Mise à jour de la page des paramètres
	xajax_monCompteListeMariageParametres();
}

function monCompteListeMariageInviteParticiperValide()
{
	// Suppression des valeurs des champs
	// Fermeture des popups
	$j("#dialogueListeMariageInviteParticiper").dialog('close');
	xajax_monCompteListeMariageChargerPage();
}

// Fonction appelé lorsque une invitation a été envoyée.
function monCompteListeMariageInviteEnvoyerValide()
{
	// Suppression des valeurs des champs
	$j("#fmcCreerListeMariageInviteEnvoyerLogin").val('');
	$j("#fmcCreerListeMariageInviteEnvoyerPass").val('');
	// Fermeture des popups
	$j("#dialogueListeMariageInviteEnvoyer").dialog('close');
}

function monCompteOpenPaiementDialogue(div, montant, dossierNumero)
{
	var openDialogue = true;
	var dialogueName;
	var type	= $j(div).parent().prev().prev('.paiementType').children().val();
	
	$j('._dDossierMontant').text(montant + ' €');
	$j('._dDossierNumero').text(dossierNumero);
	
	switch (type)
	{
		case '3':
			dialogueName = 'dialogueDossierInformationsCheque';
			break;
		case '4':
			dialogueName = 'dialogueDossierInformationsMandat';
			break;
		default:
			openDialogue = false;
			break;
	}
	
	if(openDialogue){
		$j("#" + dialogueName).dialog('open');
	} else {
		alert("Il n'y a pas d'informations sur ce type de paiement.");
	}
}

function monCompteOpenPaiementChange(div)
{
	var selectVal = $j(div).val();
	var button = '<input id="buttonOgoneSubmit" type="submit" value="Payer" />';
	
	if(selectVal == '1'){
		$j(div).parent().next('.paiementForm').children().append(button);
	} else {
		$j("#buttonOgoneSubmit").remove();
	}
}

$j(document).ready(function(){
								
	$j(".uiButtonLink").hover(
		function(){ $j(this).addClass("ui-state-hover"); },
		function(){ $j(this).removeClass("ui-state-hover"); }
	);
						   
	$j("#dialogueListeMariage").dialog({
		bgiframe: true,
		resizable: false,
		modal: true,
		width:500,
		autoOpen: false,
		title: "Liste des dossiers disponibles",
		buttons: { 
			"ok": function(){
				var valIdDossier	= $j("input:radio[name=fmcListeDossiersIdDossier]:checked").val();
				var valIdClient		= $j("#fmcListeDossiersIdClient").val();
				// Fermeture de la popup des liste des dossiers
				$j(this).dialog('close');
				// Ouverture de la popup contenant le formulaire de création de la liste de mariage
				$j("#dialogueListeMariageCreer").dialog('open');
				// Initialisation des valeurs du formulaire
				$j("#fmcCreerListeMariageIdDossier").val(valIdDossier);
				$j("#fmcCreerListeMariageIdClient").val(valIdClient);
				$j("#fmcSpanCreerListeMariageIdDossier").text(valIdDossier);
				$j("#fmcSpanCreerListeMariageIdClient").text(valIdClient);	
			},
			"Annuler": function(){ $j(this).dialog('close'); } 
		}
	});
	
	$j("#dialogueListeMariageCreer").dialog({
		bgiframe: true,
		resizable: false,
		modal: true,
		width:500,
		autoOpen: false,
		title: "Créer une liste de mariage",
		buttons: { 
			"Ok": function(){
				xajax_monCompteListesMariageCreer(xajax.getFormValues(document.fmcCreerListeMariage))
			},
			"Annuler": function(){
				$j(this).dialog('close');
				$j("#dialogueListeMariage").dialog('open');
			}
		}
	});
	
	$j("#dialogueListeMariageInviteParticiper").dialog({
		bgiframe: true,
		resizable: false,
		modal: true,
		width:500,
		autoOpen: false,
		title: "Participer à la liste de mariage",
		buttons: { 
			"Ok": function(){
				xajax_monCompteListesMariageParticiper(xajax.getFormValues(document.fmcCreerListeMariageInviteParticiper))
			},
			"Annuler": function(){$j(this).dialog('close')}
		}
	});
	
	$j("#dialogueListeMariageInviteCreer").dialog({
		bgiframe: true,
		resizable: false,
		modal: true,
		width:500,
		autoOpen: false,
		title: "Ajouter un invité",
		buttons: { 
			"Ok": function(){xajax_monCompteListesMariageInviteCreer(xajax.getFormValues(document.fmcCreerListeMariageInviteCreer));},
			"Annuler": function(){$j(this).dialog('close');}
		}
	});
	
	$j("#dialogueListeMariageInviteEnvoyer").dialog({
		bgiframe: true,
		resizable: false,
		modal: true,
		width:500,
		autoOpen: false,
		title: "Envoyer une invitation",
		buttons: { 
			"Envoyer l'invitation": function(){xajax_monCompteListesMariageInviteEnvoyer(xajax.getFormValues(document.fmcCreerListeMariageInviteEnvoyer));},
			"Annuler": function(){
				$j("#fmcCreerListeMariageInviteEnvoyerLogin").val('');
				$j("#fmcCreerListeMariageInviteEnvoyerPass").val('');
				$j(this).dialog('close');
			}
		}
	});
	
	$j("#dialogueListeMariageInvitePaiement").dialog({
		bgiframe: true,
		resizable: false,
		modal: true,
		width:500,
		autoOpen: false,
		title: "Informations",
		buttons: { 
			"Ok": function(){$j(this).dialog('close');}
		}
	});
	
	$j("#dialogueListeMariageMessages").dialog({
		bgiframe: true,
		resizable: false,
		modal: true,
		width:500,
		autoOpen: false,
		title: "Informations",
		buttons: { 
			"Ok": function(){$j(this).dialog('close');}
		}
	});
	
	$j('#openDialogueListeMariage').click(function(){
		$j("#dialogueListeMariage").dialog('open');
	});
	
	$j("#dialogueDossierInformationsCheque").dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,
		width:500,
		title: "Informations sur le paiement par chèque",
		buttons: {
			Ok: function() {$j(this).dialog('close');}
		}
	});
	$j("#dialogueDossierInformationsMandat").dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,
		width:500,
		title: "Informations sur le paiement par mandat cash",
		buttons: {
			Ok: function() {$j(this).dialog('close');}
		}
	});
});
// effect visuel : affichage des autres voyageurs
function fct_js_addCoordonneesVoyageurs()
{
	document.getElementById('chp_voyageur_autre').style.display='block';
}
// effect visuel : cacher less autres voyageurs
function fct_js_noAddCoordonneesVoyageurs()
{
	document.getElementById('chp_voyageur_autre').style.display='none';
}
// function ajout : ajout de voyageurs
// @numero_dossier
function fct_js_addVoyageur(numero_dossier)
{
	// recupération du nombre de voyageurs
	var nbr_voyageur = document.forms.mon_compte_formulaire_dossier.nbr_voyageurs.value;
	// récupération de l'id du client
	var client_id = document.forms.mon_compte_formulaire_dossier.client_id.value;
	// définition des variable
	var civilite='';
	var nom='';
	var prenom='';
	var date_naissance='';
	// s'il a des voyageurs
	if(nbr_voyageur>0)
	{
		// énumération des voyageurs
		for(i=1;i<=nbr_voyageur;i++)
		{
			// récupération de la civilité
			civilite = document.getElementsByName('civilite_' + i).item(0).value;
			// récupération du nom
			nom = document.getElementsByName('nom_voyageur_' + i).item(0).value;
			// récupération du prénom
			prenom = document.getElementsByName('prenom_voyageur_' + i).item(0).value;
			// récupération de la date de naissance
			date_naissance = document.getElementsByName('date_naissance_voyageur_' + i).item(0).value;
			// appel de fonction xajax pour l'ajout du voyageur
			result = xajax_mon_compte_ajoute_voyageur(client_id,numero_dossier,civilite,nom.toUpperCase(),prenom.toLowerCase(),date_naissance);
		}		
	}	
}
// fonction de convertion de date
function ConvNum(tabDeDate) 
{
	for (i=0; i<tabDeDate.length; i++)
	tabDeDate[i] = (tabDeDate[i].charAt(0)=='0')?tabDeDate[i].charAt(1):tabDeDate[i];
	return tabDeDate;
}

// Vérifie le format d une date saisie
function Verif_Date(valeur_date)
{ 
	var tabDate = valeur_date.split('/');
	tabDate = ConvNum(tabDate);
	var datTest_Date = new Date(parseInt(tabDate[2]), parseInt(tabDate[1])-1, parseInt(tabDate[0]));
	if (valeur_date.length>10)
	{ 
		msg = 'Ne dois pas dépasser 10 caractères.';
		return false;
	}
	for (i=0; i<valeur_date.length; i++)
	{ 
		if (valeur_date.charAt(i) == ' ')
		{ 
			msg ="La date ne doit pas contenir d\'espaces.";
			return false;
		}
	}
	if (valeur_date.length > 0)
	{ 
		if ((parseInt(tabDate[0]) != datTest_Date.getDate()) || (parseInt(tabDate[1]) != parseInt(datTest_Date.getMonth())+1))
		{ 
			msg = "Veuillez saisir la date au format 'J/M/SSAA' ou JJ/MM/SSAA.\n\nex : 6/6/2004 ou 06/06/2004";
			return false;
		}
		if ((tabDate[2].length != 4) || (parseInt(tabDate[2]) < 1800) || (parseInt(tabDate[2]) > 3000))
		{ 
		msg = "Veuillez saisir l'année sur 4 chiffres.";
		return false;
		}
	}
	return true;
}

// function verif : vérification et validation du formulaire d'ajout de voyageur
// @function : numero_dossier
function fct_js_verification_Form(numero_dossier)
{
	if (document.forms.mon_compte_formulaire_dossier.mon_compte_radio_dossier[0].checked==true)
	{
		// definition des variables local
		var nbr_voyageur = document.forms.mon_compte_formulaire_dossier.nbr_voyageurs.value;
		var civilite='';
		var nom='';
		var prenom='';
		var date_naissance='';
		var erreur = 0;
		var msg =''
		if(nbr_voyageur>0)
		{
			for(j=1;j<=nbr_voyageur;j++)
			{
				// on jnitialise les champs avec la couleur blanche
				document.getElementsByName('civilite_' + j).item(0).style.backgroundColor = '#ffffff'	;
				document.getElementsByName('nom_voyageur_' + j).item(0).style.backgroundColor = '#ffffff'	;
				document.getElementsByName('prenom_voyageur_' + j).item(0).style.backgroundColor = '#ffffff'	;
				document.getElementsByName('date_naissance_voyageur_' + j).item(0).style.backgroundColor = '#ffffff'	;
				civilite = document.getElementsByName('civilite_' + j).item(0).value;			
				nom = document.getElementsByName('nom_voyageur_' + j).item(0).value;
				prenom = document.getElementsByName('prenom_voyageur_' + j).item(0).value;
				date_naissance = document.getElementsByName('date_naissance_voyageur_' + j).item(0).value;
				// Vérification de la bonne validité des champs
				if(civilite=='')
				{
					document.getElementsByName('civilite_' + j).item(0).style.backgroundColor = '#cf3636'	;
					erreur = erreur +1 ;
				}
				if(nom=='')
				{
					document.getElementsByName('nom_voyageur_' + j).item(0).style.backgroundColor = '#cf3636'	;
					erreur = erreur +1 ;
				}
				if(prenom=='')
				{
					document.getElementsByName('prenom_voyageur_' + j).item(0).style.backgroundColor = '#cf3636'	;
					erreur = erreur +1 ;
				}
				// vérification du format de la date de naissance
				result=Verif_Date(date_naissance);
				if(date_naissance=='' || result == false)
				{
					document.getElementsByName('date_naissance_voyageur_' + j).item(0).style.backgroundColor = '#cf3636';
					erreur = erreur +1 ;
				}
			}
			// verification du nombre d'erreur
			if(erreur >=1)
			{
				msg_erreur ='Veuillez renseigner correctement tous les champs en rouges!'; 
				alert(msg_erreur);	
			}
			else
			{
				// ajout de la liste des voyageurs
				fct_js_addVoyageur(numero_dossier);
				// Changement du statut du dossier
				js_dossier_actions_checkbox(numero_dossier);				
			}			
		}	
	}
	else
	{	
		// Changement du statut du dossier
		js_dossier_actions_checkbox(numero_dossier);
	}
	
}