function Calculateur() {
   this.alert_Vehicule = "";
   this.alert_agenceAller = "";
   this.alert_agenceRetour = "";
   this.alert_KmEnvisages = "";
	this.alert_GammeInvalid = "";
	this.isValidKmsSettings = true;
   this.alert_DateDepartInvalid = "";
   this.alert_DateRetourInvalid = "";

	this.checkParamsBeforeGoToDevisPage=function(idVehicule, idAgenceDepart, idAgenceArrivee, nbKms, dateDepart, dateArrivee, formResa) {
		var errors=[];

		if(idVehicule == '') errors.push(this.alert_Vehicule);
		if(idAgenceDepart == '') errors.push(this.alert_agenceAller);
		if(idAgenceArrivee == '') errors.push(this.alert_agenceRetour);
		if(this.isValidKmsSettings) {
			nbKms=nbKms.replace('Km', '').replace('km', '').replace(' ', '');
			if((nbKms == '') || (nbKms == '0')) errors.push(this.alert_KmEnvisages);
		}

		if(errors.length > 0) {
			$('#etapes_reservation_error').text('');
			for (var item in errors) {
				$('#etapes_reservation_error').add('<ul/>').append($('<li/>').text(errors[item]));
			}
			$('#etapes_reservation_error').show();
			$("#btnResa").css("background", "url(/Content/medias/resa_bt_calculer.gif) no-repeat scroll center center transparent");
			$("#textResaWait").hide();
		} else {
			$('#etapes_reservation_error').hide();
            $('#resa_rapide_login_error').hide();

            var login = $('#resa_rapide_identifiant').val();
            var password = $('#resa_rapide_mdp').val();
            if ($('.etape_5').is(':visible') && ((login != '') || (password != ''))) {
                $.post(
                    '/Ajax/ExpressLogin',
                    { l: login, p: password },
                    function(data, textStatus) {
                        if(data != 'OK') {
                            $('#resa_rapide_login_error').text(data);
                            $('#resa_rapide_login_error').show();
                            $("#btnResa").css("background", "url(/Content/medias/resa_bt_calculer.gif) no-repeat scroll center center transparent");
                            $("#textResaWait").hide();
						} else {
							next_step(formResa, idVehicule, dateDepart, dateArrivee);
						}
                    },
                    'text'
                );
            } else {
                next_step(formResa, idVehicule, dateDepart, dateArrivee);
            }
        }

        return false;
    };
}
function next_step(formResa, idVehicule, dateDepart, dateArrivee) {
    $.post(
		mazedia.url.rRequestGetSelectedAndValidGamme,
		{
		    gammeUrl: idVehicule,
		    dateDepart: dateDepart,
		    dateArrivee: dateArrivee
		},
		function(results) {
		    var message = "";
		    if (results.Retour == "OK") {
		        formResa.submit();
		        return;
		    }

	        if (results.Retour == "DF") {
		        if (results.dateValidDebut != "") {
		            message = mazedia.calculateur.alert_DateDepartInvalid;
		        } else if (results.dateValidFin != "") {
		            message = mazedia.calculateur.alert_DateRetourInvalid;
		        }
		    } else {
		        message = mazedia.calculateur.alert_GammeInvalid.replace('{0}', results.dateValidDebut).replace('{1}', results.dateValidFin)
		    }
		    $('#etapes_reservation_error').text(message);
		    $('#etapes_reservation_error').show();
		    $("#btnResa").css("background", "url(/Content/medias/resa_bt_calculer.gif) no-repeat scroll center center transparent");
		    $("#textResaWait").hide();
		},
		"json"
	);
}
Calculateur.prototype.accueil = new Accueil();
Calculateur.prototype.gamme = new Gamme();

