﻿// ------------------ COOKIES ------------------ //
function updatesum() {
	var maximum;
	if (document.frmpricing.c && document.frmpricing.p)
		maximum = (document.frmpricing.p.options[document.frmpricing.p.selectedIndex].text-0) + (document.frmpricing.c.options[document.frmpricing.c.selectedIndex].text-0); 
	return maximum;
}

function validate_form ( )
{
    valid = true;

    if ( updatesum() > document.frmpricing.maxvalue.value )
    {
        alert ( "Vous excedeed le nombre maximum de personne" );
        valid = false;
    }

    return valid;
}





function openCalendar(FormElement){
   var calendarwindow;

	   url = "/static/template/calendar.html?updateobj="+FormElement

   calendarwindow = window.open(url,"thewindow","toolbar=no,LEFT=300,TOP=250,WIDTH=170,HEIGHT=140,status=no,scrollbars=no,resize=no,menubar=no");
   calendarwindow.focus();
}


function openCalendarout(FormElement){
   var calendarwindow;

	   url = "/static/template/calendar.html?updateobj="+FormElement

   calendarwindow = window.open(url,"thewindow","toolbar=no,LEFT=300,TOP=250,WIDTH=170,HEIGHT=140,status=no,scrollbars=no,resize=no,menubar=no");
   calendarwindow.focus();
}




//leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}


//billing
function billing_validate (){
	var sErr = "";
	if (trim(document.forms.frmbilling.webRcpName.value) == '')
		sErr+="Spécifier un 'Prénom', s'il vous plaît\n";
	if (trim(document.forms.frmbilling.webRcpSurname.value) == '')
		sErr+="Spécifier un 'Nom', s'il vous plaît\n";
	if (trim(document.forms.frmbilling.QsWebRcpAddress.value) == '')
		sErr+="Spécifier une 'Adresse', s'il vous plaît\n";
	if (trim(document.forms.frmbilling.webRcpCity.value) == '')
		sErr+="Spécifier une 'Ville', s'il vous plaît\n";
	if (trim(document.forms.frmbilling.webRcpZipCode.value) == '')
		sErr+="Spécifier un 'Code postal', s'il vous plaît\n";
	if (document.forms.frmbilling.webRcpNatId[document.forms.frmbilling.webRcpNatId.selectedIndex].value == '')
		sErr+="Spécifier une 'Nation', s'il vous plaît\n";
	if (trim(document.forms.frmbilling.webRcpPhone.value) == '')
		sErr+="Spécifier un 'Téléphone', s'il vous plaît\n";

	if (sErr != ""){
		alert(sErr); 
		return false;
	}
	return true;
}











//purchase
function pay(){
var i=0;
for(i=0;i<document.forms.choosepayment.paymode.length;i++)
	if(document.forms.choosepayment.paymode[i].checked)
		eval("document.forms."+document.forms.choosepayment.paymode[i].value+".submit()");
}

//booking
function holdorbook(a){
if (a=='onhold'){
	document.forms.frmpricingres.action=document.forms.frmpricingres.onhold_to.value;
	document.forms.frmpricingres.s.value=2;
}else if (a=='request'){
	document.forms.frmpricingres.action=document.forms.frmpricingres.onhold_to.value;
	document.forms.frmpricingres.s.value=5;
}else{
	document.forms.frmpricingres.action=document.forms.frmpricingres.book_to.value;
	document.forms.frmpricingres.s.value=3;
}
return true;
}

//suitcase
function del(formname){
if(confirm('Etes-vous sûr de vouloir supprimer cet appartement de votre panier?')){
eval("document.forms."+formname+".action=document.URL;");
eval("document.forms."+formname+".mm.value=2;");
return true;
}else{
return false;
}
}


function validate_login (f) {
    if (isEmpty(document.forms.frmlogin.username.value)){
		alert("Précisez au moins une identifiant");
		return false;
	}
    return true;
}


function montre(id) {
var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
if (d) {d.style.display='block';}
}

function reservation(objlink, appId, bookrequest) {
    var one_day = 1000 * 60 * 60 * 24;
    var s = document.getElementById("s");
    if (s !== null) {
        if (s.value === "0") {
            alert("Re-click 'search' after changing your request");
            return false;
        }
    }

    var d1 = new Date(Number(document.getElementById('start').value.substring(6, 10)), Number(document.getElementById('start').value.substring(3, 5)) - 1, Number(document.getElementById('start').value.substring(0, 2)));
    var d2 = new Date(document.getElementById('end').value.substring(6, 10), Number(document.getElementById('end').value.substring(3, 5)) - 1, Number(document.getElementById('end').value.substring(0, 2)));

    var nights = (Math.ceil(d2.getTime() - d1.getTime()) / one_day);

    if (document.getElementById('d') != null) {
        document.getElementById('d').value = document.getElementById('start').value.substring(6, 10) + document.getElementById('start').value.substring(3, 5) + document.getElementById('start').value.substring(0, 2)
    }
    //La data di checkout date è da intendersi la data in cui si libera l'app.
    //pertanto ai fini del calcolo del prezzo occorre considerare il giorno prima 
    var end = document.getElementById('end');
    if (typeof (bookrequest) == "undefined") {
        if (document.getElementById('dt') != null) {
            var dateOut = new Date(end.value.substring(6, 10), Number(end.value.substring(3, 5)) - 1, end.value.substring(0, 2));
            dateOut = dateAdd('d', -1, dateOut);
            var dateOutString = "";
            var dateOutMonthString = new Number();
            var dateOutDayString = new Number();
            if (String(dateOut.getMonth()).length == 1) {
                dateOutMonthString = '0' + String(parseFloat(String(dateOut.getMonth())) + 1);
            }
            else {
                dateOutMonthString = parseFloat(dateOut.getMonth() + 1);
            }
            if (String(dateOut.getDate()).length == 1) {
                dateOutDayString = '0' + String(parseFloat(String(dateOut.getDate())));
            }
            else {
                dateOutDayString = dateOut.getDate();
            }
            dateOutString = String(dateOut.getFullYear()) + String(dateOutMonthString) + String(dateOutDayString);
            document.getElementById('dt').value = dateOutString;
        }
    }
    else {
        document.getElementById('dt').value = end.value.substring(6, 10) + end.value.substring(3, 5) + end.value.substring(0, 2)
    }
    if (document.getElementById('p') != '') {
        document.getElementById('p').value = document.getElementById('people').value;
    } else {
        document.getElementById('p').value = '';
    }
    if (document.getElementById('children') != '') {
        document.getElementById('c').value = document.getElementById('children').value;
    } else {
        document.getElementById('c').value = '';
    }
    if ((appId != null) && (appId != '')) {
        document.getElementById('a').value = appId;
    } else {
        document.getElementById('a').value = '';
    }

    if (document.getElementById('n') != null) {
        document.getElementById('n').value = nights;
    }
    if (typeof (bookrequest) != "undefined") {
        document.getElementById('inquiry').value = 1;
    }
    else {
        document.getElementById('inquiry').value = 0;
    }
    document.forms[0].action = objlink.getAttribute("href");
    document.forms[0].setAttribute("method", "post");
    document.forms[0].submit();
    return false;
}

function dateAdd(timeU, byMany, dateObj) {
    var millisecond = 1;
    var second = millisecond * 1000;
    var minute = second * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var year = day * 365;
    var newDate;
    var dVal = dateObj.valueOf();
    switch (timeU) {
        case "ms": newDate = new Date(dVal + millisecond * byMany); break;
        case "s": newDate = new Date(dVal + second * byMany); break;
        case "mi": newDate = new Date(dVal + minute * byMany); break;
        case "h": newDate = new Date(dVal + hour * byMany); break;
        case "d": newDate = new Date(dVal + day * byMany); break;
        case "y": newDate = new Date(dVal + year * byMany); break;
    }
    return newDate;
}

function search() {
    var one_day = 1000 * 60 * 60 * 24;
    var d1 = new Date();
    var d2 = new Date(document.getElementById('start').value.substring(6, 10), Number(document.getElementById('start').value.substring(3, 5)) - 1, Number(document.getElementById('start').value.substring(0, 2)));
    var d3 = new Date(document.getElementById('end').value.substring(6, 10), Number(document.getElementById('end').value.substring(3, 5)) - 1, Number(document.getElementById('end').value.substring(0, 2)));

    //Mentre nella reservation la checkout date viene decrementata di un giorno per il calcolo delle notte	all'interno della funzione js
    //nella ricerca questa operazione viene fatta all'interno della store procedure 
    var days = (Math.ceil(d2.getTime() - d1.getTime()) / one_day);
    var nights = (Math.ceil(d3.getTime() - d2.getTime()) / one_day);

    document.forms[0].action = "/fr/searchByDate/index.html";
    var dsStart = new Date();
    var dsEnd = new Date();

    if (!((document.getElementById('start').value != '') && (document.getElementById('end').value != ''))) {
        alert("Précisez vos dates d’arrivée et de départ");
        return false;
    }
    
    if (!(parseFloat(days) > 5)) {
        alert("Votre date d’arrivée doit être au plus tôt dans 5 jours. Si vous souhaitez arriver avant merci de nous contacter.");
        return false;
    }
    if (!(parseFloat(nights) > 0)) {
        alert("Votre date d’arrivée doit être avant votre date de départ");
        return false;

    }
    for (i = 0; i < seasons.length; i++) {
        var dsStart = new Date(seasons[i].ssnbegindate.substring(0, 4), Number(seasons[i].ssnbegindate.substring(5, 7)) - 1, seasons[i].ssnbegindate.substring(8, 10));
        var dsEnd = new Date(seasons[i].ssnenddate.substring(0, 4), Number(seasons[i].ssnenddate.substring(5, 7)) - 1, seasons[i].ssnenddate.substring(8, 10));
        if ((d2 >= dsStart && d2 <= dsEnd)) {
            if (!(parseInt(nights) >= parseInt(seasons[i].mindays))) {
                alert("La reservation pour cette période doit être au minimum de " + seasons[i].mindays + " jours");
                bMinNight = false;
                return false;
            }
            else {
                bMinNight = true;
                break;
            }
        }
        else {
            bMinNight = false;
        }
    }
}

function postdata(objlink) {
    var urlRedirect = new String();
    urlRedirect = objlink.getAttribute("href").substring(objlink.getAttribute("href").indexOf("?to=") + 4);
    document.forms[0].action = "/fr/inquiry/index.html?app=" + urlRedirect;
    document.forms[0].setAttribute("method", "post");
    document.forms[0].submit();
    return false;
}

function prepareSearch() {
    if (document.getElementById("s") != null) {
        document.getElementById("s").value = 0;
    }
}

function OnAttrModified(event) {
    var message = "";
    if ('attrChange' in event) {    // Firefox, Opera, Internet Explorer from version 9
        message += "The element: " + event.target;
        message += "\nproperty: " + event.attrName;
        message += "\noriginal value: " + event.prevValue;
        message += "\n changed to: " + event.newValue;
    }
    if ('propertyName' in event) {  // Internet Explorer
        message += "The element: " + event.srcElement.tagName;
        message += "\nproperty: " + event.propertyName;

        var elem = event.srcElement;
        message += "\nvalue changed to: " + elem.attributes[event.propertyName].value;
    }
    alert(message);
}



window.onload = function () {
    if ((document.addEventListener) && (document.getElementById('start') != null)) {
        document.getElementById('start').addEventListener('change', prepareSearch, false);
        document.getElementById('end').addEventListener('change', prepareSearch, false);
        document.getElementById('people').addEventListener('change', prepareSearch, false);
        document.getElementById('children').addEventListener('change', prepareSearch, false);
    }
    else if ((document.attachEvent) && (document.getElementById('start') != null)) {
        document.getElementById('start').attachEvent('onchange', prepareSearch);
        document.getElementById('end').attachEvent('onchange', prepareSearch);
        document.getElementById('people').attachEvent('onchange', prepareSearch);
        document.getElementById('children').attachEvent('onchange', prepareSearch);
    }
}
