// QSearch form validation
function doFormQS()
{
	if(checkForm() && checkDate() && checkAge())
	{
		return true;
	} else {
		return false;
	}
}

function checkForm()
{
	// check a country has been selected
	if(document.details.area.value == "sel" || document.details.area.value == "none")
	{
		alert("Veuillez choisir une destination de la liste des pays.");
		document.details.area.focus();
		return false;

	// check for a pick up location
	} else if(document.details.tlocation.value == "") {
		alert("Veuillez choisir une station de prise en charge.");
		document.details.tlocation.focus();
		return false;

	// check for a drop off location
	} else if(document.details.tdropoff.value == "") {
		alert("Veuillez choisir une station de restitution.");
		document.details.tdropoff.focus();
		return false;
	}
	return true;
}

function checkDate()
{
	/* get selected values */	
	var pDate = document.details.slFromDay.value;
	var pMonth = document.details.slFromMonth.value;
	var pYear = document.details.slFromYear.value;
	
	var dDate = document.details.slToDay.value;
	var dMonth = document.details.slToMonth.value;
	var dYear = document.details.slToYear.value;

	/* check pick up date exists */
	if(((pMonth == "4" || pMonth == "6" || pMonth == "9" || pMonth == "11") && pDate == "31") || ((pMonth == "2") && (pDate == "30" || pDate == "31")))
	{
		alert("Vous avez choisi une date de prise en charge qui n’est pas valable. Veuillez choisir une autre date.");
		document.details.slToMonth.focus();
		return false;
	}

	/* check drop off date exists */
	if(((dMonth == "4" || dMonth == "6" || dMonth == "9" || dMonth == "11") && dDate == "31") || ((dMonth == "2") && (dDate == "30" || dDate == "31")))
	{
		alert("Vous avez choisi une date de restitution qui n’est pas valable. Veuillez choisir une autre date.");
		document.details.slToMonth.focus();
		return false;
	}

	/* build date objects */
	var pDateValid = new Date(pYear,pMonth - 1,pDate);
	var dDateValid = new Date(dYear,dMonth - 1,dDate);

	/* pDateValid in  unixtime */
	pDateValidunix=pDateValid.getTime();

	/* get today's date */
    var today = new Date(); // returns current date with timestamp
    var today_floor= new Date(today.getFullYear(),today.getMonth(),today.getDate()); // return current date with time=00:00:00
	var pDay = (today.getDay()); //get current day of the week
	
	var firstallowpickupdayunix= today_floor.getTime()+1*24*3600*1000; // returns the 00:00:00 time of tomorrow
    var secondallowpickupdayunix= today_floor.getTime()+2*24*3600*1000; // returns the 00:00:00 time of the day after tomorrow

    var firstallowedpickupday = new Date(firstallowpickupdayunix); // converts to date format
	var secondallowedpickupday = new Date(secondallowpickupdayunix); // converts to date format
    
    /* is pick up today? */
     if(pDateValid < today_floor) {
        alert("La date de prise en charge choisie est déjà passée. Veuillez choisir une autre date.");
        document.details.slFromMonth.focus();
        return false;
    }

	else if(pDay=="4" || pDay=="1" || pDay=="2" || pDay=="3")
	{
		if	(pDateValidunix < firstallowpickupdayunix)
		{
		alert("Nous ne pouvons pas accepter des réservations en ligne pour le même jour; appelez-nous du lundi au vendredi au 056 675 75 85, nous vous aiderons et renseignerons volontiers.");
		document.details.slFromMonth.focus();
		return false;
		}
	}

	else if(pDay=="0" || pDay=="5" || pDay=="6")
	{
	if	(pDateValid < secondallowedpickupday)
		{
		alert("Nous ne pouvons pas accepter des réservations en ligne pour le même jour; appelez-nous du lundi au vendredi au 056 675 75 85, nous vous aiderons et renseignerons volontiers.");
        document.details.slFromMonth.focus();
	    return false;
		}
	}	

	else if(dDateValid < pDateValid) {
        alert("La date de la restitution est avant de la prise en charge. Veuillez indiquer la date juste.");
        document.details.slFromMonth.focus();
        return false;
    }
    return true;
}



function checkAge()
{
	// check a driver age has been entered
	if(document.details.fiDriverAge.value == "") {
		alert("Vous avez oublié d’enregistrer l’âge.");
		document.details.fiDriverAge.focus();
		return false;

	// check driver age is valid
	} else if(document.details.fiDriverAge.value < 21) {
		alert("Si vous avez moins de 21 ans contactez notre call centre au 056 675 75 85, comme il y a des autres conditions ou même des suppléments.");
		document.details.fiDriverAge.focus();
		return false;
	}
	return true;
}


// get a variable from the querystring
function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++)
	{
		var pair = vars[i].split("=");
		if (pair[0] == variable)
		{
			return pair[1];
		}
	} 
}

// deeplink into carlist.w
function carlistLink(area,loc) {
	// get area & location
	var area = area;
	var location = loc;
	
	if(getQueryVariable("aff")) {
		var affcode = getQueryVariable("aff");
	} else {
		var affcode = "";
	}
	
	// get pick up dates
	var fDate = document.details.slFromDay.value;
	var fMonth = document.details.slFromMonth.value;
	if(fMonth < 10)
	{
		fMonth = "0" + fMonth;
	}
	var fYear = document.details.slFromYear.value;
	var fTime = document.details.slFromTime.value;
	fTimeNew = new String(fTime);
	fTimeNew.replace(":","%3A");
	
	// get drop off dates
	var tDate = document.details.slToDay.value;
	var tMonth = document.details.slToMonth.value;
	if(tMonth < 10)
	{
		tMonth = "0" + tMonth;
	}
	var tYear = document.details.slToYear.value;
	var tTime = document.details.slToTime.value;
	tTimeNew = new String(tTime);
	tTimeNew.replace(":","%3A");

	var baseURL = "/SWZ/SFR/qs/wait.html?UckUcZllaaIpkkac=&POlfigXkjkjkWbdq=&SblzdbFStskKccak=&ctryref=SWZ&lang=SFR";
	
	var action = baseURL + "&aff=" + affcode + 
						"&selFromTime=" + fTimeNew + 
						"&slFromDay=" + fDate + 
						"&slFromMonth=" + fMonth + 
						"&slFromYear=" + fYear + 
						"&selToTime=" + tTimeNew + 
						"&slToDay=" + tDate +
						"&slToMonth=" + tMonth + 
						"&slToYear=" + tYear + 
						"&area=" + area + 
						"&tlocation=" + location + 
						"&tdropoff=" + location + 
						"&fidriverage=26";

	window.location = action;
}


