$(document).ready(
    function() {

    	// Pour les selects de date
    	$('.date_rdv').bind('change', function(e) {
            changeHour(e, $(this));
        });

    	// Pour le bouton valider
        $("#btn_valider_rdv").bind('click', function(e) {
        	validRdv();
            ajaxRepriseRdvVar('rdvValid');
        });
        
    	// Pour le bouton RDV
        $("#btn_display_rdv").bind('click', function(e) {
        	displayRdvForm();
        	ajaxRepriseRdvVar('rdv');
        });
        
        //Tapez ici vos commentaires
        $("#comments_estimate").live('focus', function() {
        	if($("#comments_estimate").val() == 'Tapez ici vos commentaires') {
        		$("#comments_estimate").val('');
        	}
        });
    }
);

function validRdv() {
	
	if ( $('#date_rdv_main').val() == '')  {
		alert('Merci de compléter votre date de préférence');
		return
	}
	else if ( $('#date_rdv_main').val() == $('#date_rdv_alt').val() ) {
		alert('Merci de sélectionner 2 jours différents');
		return
	}
	else if ( $('#tel_rdv').val() == '')  {
		alert('Merci de communiquer votre N°');
		return
	}
	else {
		$.ajax({
			type: "POST",
			url: BASE_URL + "eurotax/sendEmailRdv",
			dataType: 'json',
			data: ({
				id_client_rdv : $("#id_client_rdv").val(),
				id_vehicle_rdv : $("#id_vehicle_rdv").val(),
				vehicle_estimation_rdv : $("#vehicle_estimation_rdv").val(),
				tel_rdv : $("#tel_rdv").val(),
				date_rdv_main : $("#date_rdv_main").val(),
				hour_rdv_main : $("#hour_rdv_main").val(),
				date_rdv_alt : $("#date_rdv_alt").val(),
				hour_rdv_alt : $("#hour_rdv_alt").val()
			}),
			success: function(ajaxReturn){

				if (ajaxReturn['succes']) {
					msgRet = "<p>Votre demande de rendez-vous a bien été transmise.<br>Notre expert vous recontactera sous 24H afin de confirmer une date et un horaire définitifs.</p>";
				}
				else {
					msgRet = "<p>Votre demande de rendez-vous n'a pas pu être transmise.<br>Merci de contacter par téléphone une agence afin de convenir d'un rendez-vous.</p>";
				}
				
				$('#content_rdv').html(msgRet + "<p>A bientôt,<br>L'équipe Reprise Illico</p>");

  			}
 		});
	}
}

function changeHour(e, objJquery) {
	
	// Get if saturday is selected
	is_saturday = objJquery.children(" option:selected").attr('is_saturday')
	
	if (is_saturday == 'true') {
		// 10h00 to 12h30 (10,13)
		$('#hour_rdv_'+ objJquery.attr('imat')).html(strGenTime(10,13));
	}
	else {
		// 12h00 to 17h30 (12,18)
		$('#hour_rdv_'+ objJquery.attr('imat')).html(strGenTime(12,18));
	}
}

function strGenTime(start,end) {

	var mn = 0;
	var hour = start;
	var strHtmlOption = "";

	// Ex: for 17h30 => 17.5 = 18-0.5 [end=18]
	while (hour < end) {
		
		strHtmlOption += "<option value='" + Math.floor(hour) + "h" + mn +"0'>" +  Math.floor(hour) + "h" + mn + "0</option>";
		
		// Half Hour		
		hour += 0.5;
		mn += 3;
		if (mn >= 6) { mn = 0; }
	}
	
	return strHtmlOption;
}

function displayRdvForm() {
	$('#eurotax_valuation_step_7').hide();
	$('#eurotax_valuation_rdv').show();
}

