function load_bloc_futur_voiture(element) {
	var bloc = document.getElementById("bloc_futur_voiture");

	if( element.checked == true ) {
		bloc.style.display = "block";
	}
	else {
		bloc.style.display = "none";
	}
}

function load_bloc_precisions() {
	if (document.getElementById("bloc_precisions").style.display=="none"){
		document.getElementById("bloc_precisions").style.display="";
	}else{
		document.getElementById("bloc_precisions").style.display="none";
	}
}

function load_bloc_coordonnees() {
	var bloc_coordonnees = document.getElementById("bloc_coordonnees");
	var bloc_connexion = document.getElementById("bloc_connexion");

	if( bloc_coordonnees.style.display == "none" ) {
		bloc_connexion.style.display = "none";
		bloc_coordonnees.style.display = "block";
	}
	else {
		bloc_connexion.style.display = "block";
		bloc_coordonnees.style.display = "none";
	}

	document.form_coordonnees.reset();
}

/*
* Vérifie si il y a une valeur dans un champ
* @param   string   Identifiant du champ de formulaire
*
* @return  boolean
*/
function isValueSelected(id) {
	if( document.getElementById(id).value != "" ) {
		return true;
	}
	else {
		return false;
	}
}

/*
* Insert une image de confirmation dans un conteneur
* @param   boolean   Identifiant du champ de formulaire
* @param   string   Identifiant du conteneur qui affichera l'image
*/
function SetConfirmationImage(bool, id_out) {
	document.getElementById(id_out).style.display = "inline";

	if(bool) {
		document.getElementById(id_out).innerHTML = '<img class="inline" src="../images/ico/ok.gif" alt="" />';
	}
	else {
		document.getElementById(id_out).innerHTML = '';
	}
}

function SetConfirmation(id, id_out) {
	SetConfirmationImage(isValueSelected(id), id_out);
}

/*
* Vérifie si le mois et l'année de mise en circulation ont des valeurs et affiche l'image de confirmation
*/
function isDateCirculationOk() {
	var id_out = "date_circulation_out";

	/* Si le mois et l'année ont été sélectionnés */
	if( isValueSelected("mois") && isValueSelected("annee") ) {
		/* On affiche l'image dans le bloc de confirmation */
		SetConfirmationImage(true, id_out)
	}
	/* Sinon */
	else {
		/* On vide le bloc de confirmation */
		SetConfirmationImage(false, id_out)
	}
}

function loadModeles(marqueid, fnc, options){
	var marque = $('#'+marqueid).val();
	$.getJSON('/ajax/getModeles.php', {
		marque : marque
	}, function(data){
		eval(fnc+'(data,options)');
	});
}

function setDataByFinition(confirmationImage) {
	var parameters = "?marque=" + document.getElementById('marque').value + "&modele=" + document.getElementById('champ_modele').value + "&version=" + encodeURIComponent( document.getElementById('version').value );

	// var myAjax = new Ajax.Request(
		// '../lib/modules/ajax/ajax.versions.php', 
		// {method: 'get', parameters: parameters, onFailure: reportError2, onComplete: gestionReponse}
	// );
	$.getJSON(
		'../lib/modules/ajax/ajax.versions.php'+parameters,
		{},
		function(data){
			$.each(data, function(index, value){
				$('#puissance_fiscale').val(value.puissance_fiscale);
				$('#puissance_reelle').val(value.puissance_reelle);

				setSelectValue($('#motorisation option'), value.motorisation);
				setSelectValue($('#categorie option'), value.categorie);
				setSelectValue($('#boite option'), value.boite);
				setSelectValue($('#portes option'), value.portes);

				SetConfirmation('puissance_fiscale', 'puissance_fiscale2_out');
				SetConfirmation('puissance_reelle', 'puissance_reelle2_out');
				SetConfirmation('motorisation', 'motorisation2_out');
				SetConfirmation('categorie', 'categorie2_out');
				SetConfirmation('boite', 'boite2_out');
				SetConfirmation('portes', 'portes2_out');
			});
		});

	/* Si la version a été sélectionnée */
	if( isValueSelected("version") ) {
		/* On désactive le champ "Autre version" */
		document.getElementById("version2").disabled = true;
	}
	else {
		/* On active le champ "Autre version" */
		document.getElementById("version2").disabled = false;
	}
}

function setSelectValue(select, value) {
	if( value ) {
		$.each(select, function(index, elem){
			if( $(elem).attr('value') == ''+value ) {
				$(elem).attr("selected",true);
				return false;
			}
			
		});
	}
	else {
		$.each(select, function(index, elem){
			$(elem).attr('selected',false);
		});
	}
}

function reportError2(request) {
	alert('Sorry. There was an error.');
}

function on_load1(marque, modele, version) {
	load_ajax('select_modele', 'modele_out', 'name=modele&marque=' + marque + '&modele=' + modele + '&getFinitionByModele=1&confirmationImage=1');
	load_ajax('getFinitionByModele', 'version_out', 'marque=' + marque + '&modele=' + modele + '&version=' + version + '&confirmationImage=1');

	SetConfirmation('marque', 'marque_out');
	SetConfirmation('champ_modele', 'modele2_out');
	SetConfirmation('version', 'version2_out');
	SetConfirmation('categorie', 'categorie2_out');
	SetConfirmation('motorisation', 'motorisation2_out');
	SetConfirmation('boite', 'boite2_out');
	SetConfirmation('millesime', 'millesime_out');
	SetConfirmation('puissance_fiscale', 'puissance_fiscale2_out');
	SetConfirmation('puissance_reelle', 'puissance_reelle2_out');
	SetConfirmation('portes', 'portes2_out');
	SetConfirmation('couleur_ext', 'couleur_ext2_out');
	SetConfirmation('roues', 'roues2_out');
	SetConfirmation('garantie', 'garantie2_out');
	load_ajax('verif_int', 'km_out', 'val=' + document.getElementById('km').value);
	load_ajax('verif_int', 'prix_out', 'val=' + document.getElementById('prix').value);
	isDateCirculationOk();
}

function on_load2(emailing_marque, emailing_modele) {
	load_ajax('verif_email', 'email_out', 'email=' + document.getElementById('email').value);
	load_ajax('verif_email2', 'email2_out', 'email=' + document.getElementById('email').value + '&email2=' + document.getElementById('email2').value);
	load_ajax('verif_pass', 'pass_out', 'pass=' + document.getElementById('pass').value);
	load_ajax('verif_pass2', 'pass2_out', 'pass=' + document.getElementById('pass').value + '&pass2=' + document.getElementById('pass2').value);
	load_ajax('verif_tel', 'tel_out', 'tel=' + document.getElementById('tel').value);
	load_ajax('verif_cp2', 'cp_out', 'cp=' + document.getElementById('cp').value);
	load_ajax('verif_string', 'prenom_out', 'val=' + document.getElementById('prenom').value);
	load_ajax('verif_string', 'nom_out', 'val=' + document.getElementById('nom').value);
	load_ajax('verif_string', 'ville_out', 'val=' + document.getElementById('ville').value);
	load_ajax('verif_immat', 'immatriculation_out', 'val=' + document.getElementById('immatriculation').value);
	load_ajax('select_modele', 'modele', 'name=emailing_modele&marque=' + emailing_marque + '&modele=' + emailing_modele + '&infobulle=0');
}
