﻿$(function() {
    // Dialog
    $('#ctl00_ContentPlaceHolder1_panel_pop').dialog({
        autoOpen: false,
        resizable: false,
        width: 300,
        height: 300
    });
    $('#ctl00_ContentPlaceHolder1_panel_pop2').dialog({
        autoOpen: false,
        resizable: false,
        width: 300,
        height: 300
    });
    $('#ctl00_ContentPlaceHolder1_panel_erreur').dialog({
        autoOpen: false,
        resizable: false,
        width: 300,
        height: 140
    });

    $('#ctl00_ContentPlaceHolder1_Panel_tourisme').dialog({
        autoOpen: false,
        resizable: false,
        width: 800,
        height: 800
    });

    $('#ctl00_ContentPlaceHolder1_panel_choixvh').dialog({
        autoOpen: false,
        resizable: false,
        close: function(event, ui) {
        setTimeout('__doPostBack()', 0)
        },
        width: 800,
        height: 800
    });

    $('#ctl00_ContentPlaceHolder1_dl_choixvehicule').dialog({
        autoOpen: false,
        resizable: false,
        width: 350,
        height: 450
    });

    // Dialog Link
    $('#ctl00_ContentPlaceHolder1_Image2').click(function() {
        $('#ctl00_ContentPlaceHolder1_panel_pop2').dialog('open');
        return false;
    });

    //    $('#ctl00_ContentPlaceHolder1_ImageButton1').click(function() {
    //        $('#ctl00_ContentPlaceHolder1_panel_pop').dialog('open');
    //        return false;
    //    });

    $('#ctl00_ContentPlaceHolder1_Image1').click(function() {
        $('#ctl00_ContentPlaceHolder1_panel_pop').dialog('open');
        return false;
    });

    //    $('#ctl00_ContentPlaceHolder1_txt_vehicule').click(function() {
    //        var cat = document.forms[0].elements["ctl00_ContentPlaceHolder1_ddl_cat"].value
    //        if (cat == "VP") {
    //            $('#ctl00_ContentPlaceHolder1_dl_choixvehicule').dialog('open');
    //            if (Page_IsValid) {
    //                $('#ctl00_ContentPlaceHolder1_dl_choixvehicule').dialog({ modal: true, width: 450 });
    //                $('#ctl00_ContentPlaceHolder1_dl_choixvehicule').parent().appendTo($("form:first")); return false;
    //            } return false;
    //        }

    //        if (cat == "VU") {
    //            $('#ctl00_ContentPlaceHolder1_dl_utilitaire').dialog('open');
    //            if (Page_IsValid) {
    //                $('#ctl00_ContentPlaceHolder1_dl_utilitaire').dialog({ modal: true, width: 450 });
    //                $('#ctl00_ContentPlaceHolder1_dl_utilitaire').parent().appendTo($("form:first")); return false;
    //            } return false;
    //        }
    //    });
    $('#ctl00_ContentPlaceHolder1_hl_test').click(function() {
        
        $('#ctl00_ContentPlaceHolder1_panel_choixvh').dialog('open');
        if (Page_IsValid) {
            $('#ctl00_ContentPlaceHolder1_panel_choixvh').dialog({ modal: true, width: 650 });
            $('#ctl00_ContentPlaceHolder1_panel_choixvh').parent().appendTo($("form:first")); return false;
        } return false;
        doPostBack = false
    });




    $('#ctl00_ContentPlaceHolder1_ddl_cat').change(function() {
        var cat = document.forms[0].elements["ctl00_ContentPlaceHolder1_ddl_cat"].value
        if (cat == "VP") {
            $('#ctl00_ContentPlaceHolder1_Panel_tourisme').dialog('open');
            if (Page_IsValid) {
                $('#ctl00_ContentPlaceHolder1_Panel_tourisme').dialog({ modal: true, width: 800 });
                $('#ctl00_ContentPlaceHolder1_Panel_tourisme').parent().appendTo($("form:first")); return false;
            } return false;
        }

        if (cat == "VU") {
            $('#ctl00_ContentPlaceHolder1_panel_choixvh').dialog('open');
            if (Page_IsValid) {
                $('#ctl00_ContentPlaceHolder1_panel_choixvh').dialog({ modal: true, width: 650 });
                $('#ctl00_ContentPlaceHolder1_panel_choixvh').parent().appendTo($("form:first")); return false;
            } return false;
        }
    });

    $('#ctl00_ContentPlaceHolder1_txt_heuredep').timepicker({
        hourText: 'Heures',
        minuteText: 'Minutes',
        amPmText: ['AM', 'PM'],
        timeSeparator: ':',
        onHourShow: timepicker7OnHourShowCallback,
        showPeriodLabels: false
    });

    $('#ctl00_ContentPlaceHolder1_txt_heureret').timepicker({
        hourText: 'Heures',
        minuteText: 'Minutes',
        amPmText: ['AM', 'PM'],
        timeSeparator: ':',
        onHourShow: timepicker7OnHourShowCallback,
        showPeriodLabels: false
    });

    function timepicker7OnHourShowCallback(hour) {
        if ((hour > 23) || (hour < 8)) {
            return false;
        }
        return true;
    }
    //$( "#dialog:ui-dialog" ).dialog( "destroy" );

    //				$('#btn_reserv').click(function(){
    //				    $( "#panel_loader" ).dialog({
    //			        height: 140,
    //			        modal: true
    //		            });
    //				});

    // Datepicker
    $("#ctl00_ContentPlaceHolder1_txt_du").datepicker({
        minDate: +3,
        dateFormat: 'dd/mm/yy',
        dayNamesMin: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
        monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
        firstDay: 1,
        onSelect: function(dateText, inst) { updateDepart(); }
    });
    $("#ctl00_ContentPlaceHolder1_txt_au").datepicker({
        minDate: +3,
        dateFormat: 'dd/mm/yy',
        dayNamesMin: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
        monthNames: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'],
        firstDay: 1,
        onSelect: function(dateText, inst) { updateRetour(); }
    });
});
//function infos() {
//    var cat = document.forms[0].elements["ctl00_ContentPlaceHolder1_ddl_depart"].value
//    document.forms[0].elements["ctl00_ContentPlaceHolder1_HiddenField3"].value = cat
//    $('#ctl00_ContentPlaceHolder1_Image1').click(function() {
//        $('#ctl00_ContentPlaceHolder1_panel_pop').dialog('open');
//        return false;
//    });
//    var childWindowHandles = new Array();
//    refreshChildWindows();
//}

function choixvh() {
    $('#ctl00_ContentPlaceHolder1_panel_choixvh').dialog('open');
    if (Page_IsValid) {
        $('#ctl00_ContentPlaceHolder1_panel_choixvh').dialog({ modal: true, width: 650 });
        $('#ctl00_ContentPlaceHolder1_panel_choixvh').parent().appendTo($("form:first")); return false;
    } return false;
}

function refreshChildWindows() {
    for (var loop = 0; loop < childWindowHandles.length; loop++) if (!childWindowHandles[loop].closed)

        childWindowHandles[loop].reload(true);

}
            //Fonction qui coche automatiquement la checkbox 21 ans lorsque la 25 est coché
		    function onClick25ans()
	        {
	            if (document.forms[0].elements["ctl00_ContentPlaceHolder1_cb_25"].checked)
		    {
		        document.forms[0].elements["ctl00_ContentPlaceHolder1_cb_21"].checked = true;
		    }
	        }
	        
	        //Fonction qui change l'heure de retour en fonction de l'heure de depart
		    function onChangeHeure()
	        {
	            //var valeur = document.getElementById('ctl00_ContentPlaceHolder1_ddl_heureDep').options[document.getElementById('ctl00_ContentPlaceHolder1_ddl_heureDep').selectedIndex].index
	            var dtDepart = document.getElementById("ctl00_ContentPlaceHolder1_txt_heuredep").value
	            document.getElementById("ctl00_ContentPlaceHolder1_txt_heureret").value = dtDepart
	            //document.getElementById('ctl00_ContentPlaceHolder1_ddl_heureArr').selectedIndex = valeur
	        }
	        
            
            //Fonction qui masque ou affiche les champs en fonctions de la CheckBox Agence retour
//            function showHide(id1,id2){ 
//               el = document.getElementById(id1); 
//               e2 = document.getElementById(id2);
//               el.style.display = (el.style.display != 'block')? 'block' : 'none'; 
//               e2.style.display = (e2.style.display != 'block')? 'block' : 'none';
//            }


//	        function vehicules() {
//	            $('ctl00_ContentPlaceHolder1_dl_choixvehicule').dialog('open');
//	            return false;
//	        }
//            	        function vehicules() {
//            	            $('#ctl00_ContentPlaceHolder1_Panel_tourisme').dialog('open');
//	            return false;
//	        }
            function modifSelect(id, val)
                  {
	                     var elSel = document.forms[0].elements[id];
		                 var i;
		                 var idxVal = 0;
		                 for (i = 0; i < elSel.options.length ;  i++) 
		                 {
			                var v = elSel.options[i].value;
	      	                if (parseInt(v,10) == val)
	      	                {
	    	                  idxVal = i;
	    	                  //on arrete
	    	                  i = elSel.options.length;
	      	                }
		                 }
                		 
		                 elSel.selectedIndex = idxVal;
                  }
                  
            //Fonction qui regle l'heure de retour
            function updateRetour()
              {
                  var dtRetour = $("#ctl00_ContentPlaceHolder1_txt_au").datepicker('getDate');
                  //dtRetour.setHours(parseInt(document.forms[0].elements["ctl00_ContentPlaceHolder1_txt_heuredep"].value, 10))
                  //dtRetour.setMinutes(parseInt(document.forms[0].elements["ctl00_ContentPlaceHolder1_ddl_minuteArr"].value, 10))
                  $("#ctl00_ContentPlaceHolder1_txt_au").datepicker('disable');
                  $("#ctl00_ContentPlaceHolder1_txt_au").datepicker('setDate', dtRetour);
                  $("#ctl00_ContentPlaceHolder1_txt_au").datepicker('enable');

              }
              
              //Fonction qui met la date de retour à jour en fonction de la date de depart
              function updateDepart()
              {
                  var dtDepart = $("#ctl00_ContentPlaceHolder1_txt_du").datepicker('getDate');
		            var delaiMinDepartRetour = parseInt("48",10);
		            delaiMinDepartRetour =  delaiMinDepartRetour * 3600000;
		            dtDepart.setTime(dtDepart.getTime() + delaiMinDepartRetour);
		            
		            $("#ctl00_ContentPlaceHolder1_txt_au").datepicker('disable');
		            $("#ctl00_ContentPlaceHolder1_txt_au").datepicker('setDate', dtDepart);
		            $("#ctl00_ContentPlaceHolder1_txt_au").datepicker('enable');
              }
            function verifReserv()
            {
                alert($("#aspnetForm").validationEngine('validate'));
            }

            
            function parseDate(dt)
              {
	              day 	= dt.substring(0,2);
	              month = dt.substring(3,5);
	              year  = dt.substring(6,10);
	              d = new Date(parseInt(year,10),parseInt(month,10)-1, parseInt(day,10));
            	  
	              return d;  
              }
              
              //Fonction qui calcule la différence entre les deux dates pour connaitre le nombre de km autorisé
             function diffDate()
              {
                  var dtD = $("#ctl00_ContentPlaceHolder1_txt_du").datepicker('getDate').getTime();
                  var hD = parseInt(document.forms[0].elements["ctl00_ContentPlaceHolder1_txt_du"].value, 10);
                  var mD = parseInt(document.forms[0].elements["ctl00_ContentPlaceHolder1_txt_du"].value, 10);
		            var dateD = new Date();
		            dateD.setTime(dtD);
		            dateD.setHours  (hD);
		            dateD.setMinutes(mD);

		            var dtR = $("#ctl00_ContentPlaceHolder1_txt_au").datepicker('ctl00_ContentPlaceHolder1_getDate').getTime();
		            var hR = parseInt(document.forms[0].elements["ctl00_ContentPlaceHolder1_txt_au"].value, 10);
		            var mR = parseInt(document.forms[0].elements["ctl00_ContentPlaceHolder1_txt_au"].value, 10);
	              var dateR = new Date();
		            dateR.setTime(dtR);
		            dateR.setHours  (hR);
		            dateR.setMinutes(mR);
            	  
		            var diff = dateR.getTime()-dateD.getTime();
            	  
		            //Set 1 day in milliseconds
	              var one_day=1000.0*60.0*60.0*24.0;

	              //Calculate difference btw the two dates, and convert to days
	              var days = diff/(one_day);

	              if (days < 1)
		              days = 1;
            	  
	              return days;

              }
              
              //Fonction qui verifie si le nombre de km est valide
            function verifKmSup(evt)
              {
                  var value = document.forms[0].elements["ctl00_ContentPlaceHolder1_txt_km"].value.length + 1;
                  if (value > 5 && evt.keyCode != 8)
                    return false;
                  else
                    return true;
              }

              function popupFuncError(error) {

                  $("#dialogError").dialog({ closeOnEscape: false,
                      modal: true,
                      resizable: false,
                      autoOpen: false,
                      bgiframe: true,
                      buttons: { "Ok": function() { $(this).dialog("close"); } }
                  });
                  $("#dialogError").dialog('option', 'position', ['center', 'center']);

                  $('#dialogError').html("<html> <head></head><body>" + error + "</body></html>");
                  $("#dialogError").dialog('open');
              }

              function reserver() {
              
//                  if (!document.forms[0].elements["ctl00_ContentPlaceHolder1_cb_21"].checked) {
//                      document.getElementById("ctl00_ContentPlaceHolder1_lbl_confirm").innerHTML = "Vous avez indiquez ne pas avoir 21 ans, confirmez vous cette information ?"
//                      apprise('How about now?', { 'verify': true, 'textYes': 'Yes already!', 'textNo': 'No, not yet' }, function(r) {
//                          if (r) {
//                              document.forms[0].elements["ctl00_ContentPlaceHolder1_hf_confirm"].value = "oui"
//                              document.getElementById("ctl00_ContentPlaceHolder1_btn_reserv").click()
//                          }
//                          else {
//                              document.forms[0].elements["ctl00_ContentPlaceHolder1_hf_confirm"].value = "non"
//                          }
//                      });
//                      $.jqDialog.confirm("Are you sure want to click either of these buttons?",
//		                    function() { document.forms[0].elements["ctl00_ContentPlaceHolder1_hf_confirm"].value = "oui"; alert(document.forms[0].elements["ctl00_ContentPlaceHolder1_hf_confirm"].value) }, 	// callback function for 'YES' button
//		                    function() { document.forms[0].elements["ctl00_ContentPlaceHolder1_hf_confirm"].value = "non" } 	// callback function for 'NO' button
                      //	                    );
//                      if (confirm("test")) {
//                          document.forms[0].elements["ctl00_ContentPlaceHolder1_hf_confirm"].value = "oui"
//                      }
//                      else
//                      {
//                          document.forms[0].elements["ctl00_ContentPlaceHolder1_hf_confirm"].value = "non"
//                      }
//                      }
//                      else if (!document.forms[0].elements["ctl00_ContentPlaceHolder1_cb_1p"].checked) {
//                      $("#ctl00_ContentPlaceHolder1_panel_loader").dialog("destroy");
//                          popupFuncError("Le conducteur doit obligatoirement avoir plus de 1 an de permis le jour de début de location");
//                      }
//                      else {
                  //                          //showWaitMessage();
                        if (document.getElementById("ctl00_ContentPlaceHolder1_txt_km").value != "")
                        {
                            $("#ctl00_ContentPlaceHolder1_panel_loader").dialog({
                            height: 170,
                            width: 450,
                              modal: true
                          });
                          }
//                          //document.forms[0].submit();
//                      }
                  }
                  function erreurMvt() {
                      $("#ctl00_ContentPlaceHolder1_panel_erreur").dialog({
                          height: 140,
                          resizable: false,
                          modal: true
                      });
                      $("#ctl00_ContentPlaceHolder1_panel_loader").dialog("destroy");
                  }

                  function verifInfos() {
//                      $("#ctl00_ContentPlaceHolder1_confirmation").dialog({
//                          autoOpen: false,
//                          resizable: false,
//                          height: 140,
//                          modal: true
                      //                          });
                      $("#ctl00_ContentPlaceHolder1_confirmation").dialog({
                          height: 140,
                          resizable: false,
                          modal: true
                      });
                  }

                  function refus() {
                      $("#ctl00_ContentPlaceHolder1_refus").dialog({
                          height: 140,
                          resizable: false,
                          modal: true
                      });
                  }

                  function newsok() {
                      $("#ctl00_ContentPlaceHolder1_panel_confnews").dialog({
                          modal: true,
                          resizable: false,
                          buttons: {
                              Ok: function() {
                                  $(this).dialog("close");
                              }
                          }
                      });
                  }

                  function newspasok() {
                      $("#ctl00_ContentPlaceHolder1_panel_nonews").dialog({
                          modal: true,
                          resizable: false,
                          buttons: {
                              Ok: function() {
                                  $(this).dialog("close");
                              }
                          }
                      });
                  }
                  
                  function InfoDepart() {
                      $('#ctl00_ContentPlaceHolder1_panel_pop').dialog({
                          autoOpen: false,
                          width: 300,
                          height: 300
                      });
                  }
                  function affiche() {
                       if (document.forms[0].elements["ctl00_ContentPlaceHolder1_ddl_retour"].value == "00") {
                          var depart = document.forms[0].elements["ctl00_ContentPlaceHolder1_ddl_depart"].value
                          document.forms[0].elements["ctl00_ContentPlaceHolder1_ddl_retour"].value = depart
                      }
//                      $("#ctl00_ContentPlaceHolder1_ddl_retour").slideToggle("slow");
//                      $("#ctl00_ContentPlaceHolder1_Image2").slideToggle("slow");
//                      setTimeout(document.getElementById("teest").style.display = (document.getElementById("teest").style.display != '') ? '' : 'none', 500);

                  }
                  
                  function confirmation() {
                      document.forms[0].elements["ctl00_ContentPlaceHolder1_hf_confirm"].value = "oui"
                      $("#ctl00_ContentPlaceHolder1_confirmation").dialog("destroy");
                  }

                  function wait() {
                          $("#ctl00_ContentPlaceHolder1_panel_loader1").dialog({
                          height: 170,
                          width: 450,
                             resizable: true,
                             modal: true
                         });
                     }
