// 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("Bitte wählen Sie eine Destination aus der Länderliste.");
		document.details.area.focus();
		return false;

	// check for a pick up location
	} else if(document.details.tlocation.value == "") {
		alert("Bitte wählen Sie eine Übernahmestation.");
		document.details.tlocation.focus();
		return false;

	// check for a drop off location
	} else if(document.details.tdropoff.value == "") {
		alert("Bitte wählen Sie eine Abgabestation.");
		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("Sie haben ein ungültiges Übernahmedatum ausgewählt. Bitte wählen Sie ein anderes Datum.");
		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("Sie haben ein ungültiges Abgabedatum ausgewählt. Bitte wählen Sie ein anderes Datum.");
		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("Das Übernahmedatum ist schon vorbei. Bitte wählen Sie ein anderes Datum.");
        document.details.slFromMonth.focus();
        return false;
    }
	
	else if(pDay=="4" || pDay=="1" || pDay=="2" || pDay=="3")
		{
		if	(pDateValidunix < firstallowpickupdayunix)
			{
			alert("Wir können keine online Reservationen am Übernahmetag akzeptieren; unsere Reservationszentrale steht Ihnen sehr gerne von Montag bis Freitag unter 056 675 75 85 zur Verfügung.");
        document.details.slFromMonth.focus();
        return false;
			}
		}

	else if(pDay=="0" || pDay=="5" || pDay=="6")
		{
		if	(pDateValid < secondallowedpickupday)
			{
			alert("Wir können keine online Reservationen am Übernahmetag akzeptieren; unsere Reservationszentrale steht Ihnen sehr gerne von Montag bis Freitag unter 056 675 75 85 zur Verfügung.");
        document.details.slFromMonth.focus();
        return false;
			}
		}	

   	else if(dDateValid < pDateValid) {
        alert("Das Abgabedatum, das Sie eingegeben haben, ist for dem Übernahmedatum.\nBitte geben Sie das Datum nochmals ein.");
        document.details.slFromMonth.focus();
        return false;
    }
    return true;
}

function checkAge()
{
	// check a driver age has been entered
	if(document.details.fiDriverAge.value == "") {
		alert("Sie haben Ihr Alter nicht angegeben.");
		document.details.fiDriverAge.focus();
		return false;

	// check driver age is valid
	} else if(document.details.fiDriverAge.value < 21) {
		alert("Falls Sie unter 21 Jahre alt sind, kontaktieren Sie bitte unser\nCall Center unter 056 675 75 85, da hierfür die Konditionen ändern können, und eventuell Zuschläge erhoben werden.");
		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/SGR/qs/wait.html?UckUcZllaaIpkkac=&POlfigXkjkjkWbdq=&SblzdbFStskKccak=&ctryref=SWZ&lang=SGR";
	
	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;
}

