function setdate() {
document.getElementById("start").valueAsDate = new Date();
}


function putnumber() {
	document.getElementById("number").value = 1;
}

var iddata = 0;
function setId(new_id) {
    iddata = new_id;
}

function checknumber() {
	var number = document.getElementById("number").value
	if (number < 1 ) {
		alert("minimum 1 person");
		document.getElementById("number").value = 1;
	}
}



function spliter() {
    var place = sessionStorage.getItem("place123");
    if (place == "Nabeul") {
    	var mydata = JSON.parse(hammamet)
    }
    if (place == "Sousse") {
    	var mydata = JSON.parse(sousse)
    }
    if (place == "Mednin") {
    	var mydata = JSON.parse(djerba)
    }
    if (place == "Tatouine") {
        var mydata = JSON.parse(tataouine)
    }
    if (place == "Gabes") {
        var mydata = JSON.parse(gabes)
    }
    if (place == "Tunis") {
        var mydata = JSON.parse(tunis)
    }
    if (place == "Jandouba") {
        var mydata = JSON.parse(jandouba)
    }
    if (place == "Tozeur") {
        var mydata = JSON.parse(Tozeur)
    }
    if (place == "Mahdia") {
        var mydata = JSON.parse(Mahdia)
    }

    
    var reorderedHotels = mydata.sort(comparePrice);
    displayHotels(reorderedHotels);
    
}

    function displayHotels(hotels) {
        var duration = sessionStorage.getItem("days123");
        var h        = document.getElementById("here");
        var minprice = sessionStorage.getItem("min123");
        var maxprice = sessionStorage.getItem("max123");

        for (var i = 0; i < (hotels.length); ++i) { 
            var name  = hotels[i].name;
            var img   = hotels[i].img;
            var price = hotels[i].price;
            var disc  = hotels[i].disc;
            var price = price * duration;
            if ((price > minprice) && (price < maxprice)) {
            h.insertAdjacentHTML("beforebegin", "<div class='product-card' id= '"+(i+1)+"'><div class='product-tumb'><img src='"+img+"' alt=''></div><div class='product-details'  ><h4><a href='book.html' onclick= 'setId("+(i+1)+")'>"+name+"</a></h4><p>"+disc+"</p><div class='product-bottom-details'><div class='product-price'> prix de votre period :"+price+" TND</div></div></div></div></div>");           
        }
    }
}

    function comparePrice(hotelA, hotelB) {
        if(parseInt(hotelA.price) > parseInt(hotelB.price)) {
            return 1;
        }

        if(hotelB.price > hotelA.price) {
            return -1;
        }

        return 0 ;
    }

function checkdate1() {

	var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1;
    var yyyy = today.getFullYear();
      if(dd<10){
        dd='0'+dd;
      } 
      if(mm<10){
        mm='0'+mm;
      } 

    var putteddate = document.getElementById("end").value;
    var year = putteddate.substring(0, 4);
    var month = putteddate.substring(5, 7);
    var day = putteddate.substring(8, 10); 
    if (year < yyyy) {
    	document.getElementById("end").style.background= "red"
    	alert("minimum est 1 jour ");
    	      var dd = parseInt(dd , 10);
    	      var dd = dd + 1;
    	document.getElementById("end").value = yyyy+"-"+mm+"-"+dd;
    }
    	else  {
    		if ((month < mm)&&(year == yyyy))  {
    			document.getElementById("end").style.background= "red"
    			alert("minimum est 1 jour");
    			      var dd = parseInt(dd , 10);
    			      var dd = dd + 1;
    			document.getElementById("end").value = yyyy+"-"+mm+"-"+dd;
    		}
    			else   {
    				if ((day < dd)&&(month == mm)&&(year == yyyy)) {
    					document.getElementById("end").style.background= "red"
    					alert("minimum est 1 jour");
    					      var dd = parseInt(dd , 10);
    					      var dd = dd + 1;
    					document.getElementById("end").value = yyyy+"-"+mm+"-"+dd;
    				}
    				else {
    					document.getElementById("end").style.background= "white"    
    									}
    			}

    		}
    	}
function checkdate() {

	var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1;
    var yyyy = today.getFullYear();
      if(dd<10){
        dd='0'+dd;
      } 
      if(mm<10){
        mm='0'+mm;
      } 
    var putteddate = document.getElementById("start").value
    var year = putteddate.substring(0, 4);
    var month = putteddate.substring(5, 7);
    var day = putteddate.substring(8, 10); 
    if (year < yyyy) {
    	alert("il est un problem dans la date de depart");
    	document.getElementById("start").style.background= "red"
    	document.getElementById("start").valueAsDate = new Date();
    }
    	else  {
    		if ((month < mm)&&(year == yyyy))  {
    			alert("il est un problem dans la date de depart");
    			document.getElementById("start").style.background= "red"
    			document.getElementById("start").valueAsDate = new Date();
    		}
    			else   {
    				if ((day < dd)&&(month == mm)&&(year == yyyy)) {
    					alert("il est un problem dans la date de depart");
    					document.getElementById("start").style.background= "red"
    					document.getElementById("start").valueAsDate = new Date();
    				}
    				else {
    					document.getElementById("start").style.background= "white";
    				}
    			}

    		}
    	}
    
    

function allhotels() {
	for (var n = 0; n < 9; ++n) {
		if (n == 0) {
			var mydata = JSON.parse(djerba)
		}
		if (n == 1) {
			var mydata = JSON.parse(sousse)
		}
		if (n == 2) {
			var mydata = JSON.parse(hammamet)
		}
        if (n == 3) {
            var mydata = JSON.parse(tataouine)
        }
        if (n == 4) {
            var mydata = JSON.parse(gabes)
        }
        if (n == 5) {
            var mydata = JSON.parse(tunis)
        }
        if (n == 6) {
            var mydata = JSON.parse(Tozeur)
        }
        if (n == 7) {
            var mydata = JSON.parse(Mahdia)
        }
        if (n == 8) {
            var mydata = JSON.parse(jandouba)
        }
	for (var i = 0; i < mydata.length; ++i) { 

var name = mydata[i].name;
var img = mydata[i].img;
var price = mydata[i].price;
var disc = mydata[i].disc;

  var h = document.getElementById("here");
  h.insertAdjacentHTML("afterend", "<div class='product-card'><div class='product-tumb'><img src='"+img+"' alt=''></div><div class='product-details'><h4><a href='book.html'>"+name+"</a></h4><p>"+disc+"</p><div class='product-bottom-details'><div class='product-price'> prix de votre period :"+price+" TND</div></div></div></div></div>");

}

}
}


function places() {
	if (document.getElementById("place").selectedIndex == "1") {
    document.getElementById("map").src = "https://www.openstreetmap.org/export/embed.html?bbox=10.247497558593752%2C36.44448503928196%2C11.291198730468752%2C37.017905231730914&amp;layer=mapnik"
		
	}
	if (document.getElementById("place").selectedIndex == "2") {
        document.getElementById("map").src =  "https://www.openstreetmap.org/export/embed.html?bbox=10.128021240234377%2C35.68351380631503%2C10.890197753906252%2C35.97356075349624&amp;layer=mapnik"
	}
	if (document.getElementById("place").selectedIndex == "3") {
        document.getElementById("map").src ="https://www.openstreetmap.org/export/embed.html?bbox=10.46928405761719%2C33.28289793826734%2C11.231460571289062%2C33.58144708224173&amp;layer=mapnik"

	}
    if (document.getElementById("place").selectedIndex == "4") {
        document.getElementById("map").src ="https://www.openstreetmap.org/export/embed.html?bbox=9.121398925781252%2C31.856564203952235%2C10.645751953125%2C32.462267257639176&amp;layer=mapnik"

    }
    if (document.getElementById("place").selectedIndex == "5") {
        document.getElementById("map").src ="https://www.openstreetmap.org/export/embed.html?bbox=9.330139160156252%2C33.763736215398566%2C10.092315673828127%2C34.0606236722589&amp;layer=mapnik"

    }
    if (document.getElementById("place").selectedIndex == "6") {
        document.getElementById("map").src ="https://www.openstreetmap.org/export/embed.html?bbox=9.63432312011719%2C36.649690741415036%2C10.396499633789064%2C36.936172034772895&amp;layer=mapnik"

    }
    if (document.getElementById("place").selectedIndex == "7") {
        document.getElementById("map").src ="https://www.openstreetmap.org/export/embed.html?bbox=8.23905944824219%2C36.410231151040215%2C9.001235961914064%2C36.697603754877754&amp;layer=mapnik"

    }

    if (document.getElementById("place").value == "none") {
        document.getElementById("map").src = "https://www.openstreetmap.org/export/embed.html?bbox=6.778564453125001%2C32.52828936482526%2C12.875976562500002%2C34.908457853981396&amp;layer=mapnik"
    }
    if (document.getElementById("place").value == "Tozeur") {
        document.getElementById("map").src = "https://www.openstreetmap.org/export/embed.html?bbox=7.544174194335938%2C33.786566509489155%2C8.306350708007814%2C34.0833745509365&amp;layer=mapnik"
    }
    if (document.getElementById("place").value == "Mahdia") {
        document.getElementById("map").src = "https://www.openstreetmap.org/export/embed.html?bbox=10.472717285156252%2C35.294952147406576%2C11.234893798828127%2C35.58641000195658&amp;layer=mapnik"
    }
    

}

function storage() {
		    var date2 = new Date(document.getElementById("start").value);
        var date1 = new Date(document.getElementById("end").value);
var Difference_In_Time = date1.getTime() - date2.getTime(); 
  
var days     = Difference_In_Time / (1000 * 3600 * 24); 
var days     = Math.abs(days);
var place    = document.getElementById("place").value;
var minprice = document.getElementById("min").value;
var maxprice = document.getElementById("max").value;
	sessionStorage.removeItem("days123");
	sessionStorage.removeItem("place123");
    sessionStorage.removeItem("min123");
    sessionStorage.removeItem("max123");
	sessionStorage.setItem("days123", days);
	sessionStorage.setItem("place123", place);
    sessionStorage.setItem("min123", minprice);
    sessionStorage.setItem("max123", maxprice);
}



