// Global Variables
var isFormOk = true;
var isErrorState = false;
var today = new Date();
//var today = new Date(2008, 3, 30);
var yesterday = new Date();
yesterday.setTime( today.getTime() );
yesterday.setDate(today.getDate()-1);

function initForm(){
	var orForm = document.forms[0];
	initCheckinDate();
	initCheckoutDate();
}

function initCheckinDate(){
	var orForm = document.forms[0];
	// Set checkin date as today's date
	orForm.checkinDay.selectedIndex = today.getDate() - 1;
	orForm.checkinMonth.selectedIndex = today.getMonth();
	orForm.checkinYear.selectedIndex = today.getFullYear()-2007;
}

function initCheckoutDate(){
	var orForm = document.forms[0];
	// Set checkout date as checkin date+1
	var checkinDate = new Date(orForm.checkinYear.selectedIndex+2007,
								orForm.checkinMonth.selectedIndex,
								orForm.checkinDay.selectedIndex+1);
	var checkoutDate = new Date();
	checkoutDate.setTime( checkinDate.getTime() );
	// CheckoutDate is always at least the next day following checkinDate
	checkoutDate.setDate( checkinDate.getDate()+1 );
	// Update form
	orForm.checkoutDay.selectedIndex = checkoutDate.getDate() - 1;
	orForm.checkoutMonth.selectedIndex = checkoutDate.getMonth();
	orForm.checkoutYear.selectedIndex = checkoutDate.getFullYear()-2007;
}

// Called when user changes checkin date
function changeCheckinDate(type) {
	
	var orForm = document.forms[0];
	// 1. Assure that checkinDate is possible
	makeCheckinDatePossible();
	// 2.1 Assure that checkinDate is not equal to or less than yesterday
	// 2.2 If so, modify it and...
	var checkinDate = new Date(orForm.checkinYear.selectedIndex+2007,
								orForm.checkinMonth.selectedIndex,
								orForm.checkinDay.selectedIndex+1);
	if (type=="day") {
		if (checkinDate <= yesterday) {
			// if current month december than increase the year with the month
			if ((orForm.checkinMonth.selectedIndex+1)==12) orForm.checkinYear.selectedIndex++;
			orForm.checkinMonth.selectedIndex = ((orForm.checkinMonth.selectedIndex+1)%12);
		}
	}
	
	if (type=="month") {				
		if (checkinDate <= yesterday) {
			orForm.checkinYear.selectedIndex++;
		}
	}
	
	if (type=="year") {			
		if (checkinDate <= yesterday) {
			initForm();
		}
	}
	// 2.3. ...make that modified date the nearest possible date
	makeCheckinDatePossible();
	// 3. Assure that checkoutDate is at least the day after checkinDate
	synchronizeCheckoutDate();
}

// Called when user changes checkout date
function changeCheckoutDate(type) {
	
	var orForm = document.forms[0];
	// 1. Assure that checkoutDate is possible
	makeCheckoutDatePossible();
	// 2.1 Assure that checkoutDate is not less than dayAfterCheckinDate
	// 2.2 If so, modify it and...
	var checkinDate = new Date(orForm.checkinYear.selectedIndex+2007,
								orForm.checkinMonth.selectedIndex,
								orForm.checkinDay.selectedIndex+1);
	var checkoutDate = new Date(orForm.checkoutYear.selectedIndex+2007,
								orForm.checkoutMonth.selectedIndex,
								orForm.checkoutDay.selectedIndex+1);
	var dayAfterCheckinDate = new Date();
	// Calculate dayAfterCheckinDate: simply the next day
	dayAfterCheckinDate.setTime( checkinDate.getTime() );
	dayAfterCheckinDate.setDate( checkinDate.getDate() + 1 );
	
	if (type=="day") {		
		if (checkoutDate < dayAfterCheckinDate) {
			if ((orForm.checkoutMonth.selectedIndex+1)==12) orForm.checkoutYear.selectedIndex++;
			orForm.checkoutMonth.selectedIndex = ((orForm.checkoutMonth.selectedIndex+1)%12);
		}
	}
	
	if (type=="month") {					
		if (checkoutDate < dayAfterCheckinDate) {
			orForm.checkoutYear.selectedIndex++;
		}
	}
	
	if (type=="year") {			
		if (checkoutDate < dayAfterCheckinDate) {
			synchronizeCheckoutDate();
		}
	}
	makeCheckoutDatePossible();
}

function makeCheckinDatePossible(){
	var orForm = document.forms[0];
	checkinDay = orForm.checkinDay.selectedIndex+1;
	checkinMonth = orForm.checkinMonth.selectedIndex;
	checkinYear = orForm.checkinYear.selectedIndex+2007;
	
	// Feb 29
	if ( (checkinDay==29)&&(checkinMonth==1)&&(checkinYear%4!=0) ) orForm.checkinMonth.selectedIndex=2;
	// Feb 29+
	if ( (checkinDay>29)&&(checkinMonth==1) ) orForm.checkinMonth.selectedIndex=2;
	// Apr, June, Sept, Nov 31
	if ( (checkinDay==31)&&(checkinMonth==3||checkinMonth==5||checkinMonth==8||checkinMonth==10) ) orForm.checkinMonth.selectedIndex++;
	// 31 Dec 2009 is not possible; make 30 Dec 2009
	if ( (checkinDay==31)&&(checkinMonth==11)&&(checkinYear==2009) ) {
		orForm.checkinDay.selectedIndex = 29;
		orForm.checkinMonth.selectedIndex = 11;
		orForm.checkinYear.selectedIndex = 2;
	}
}

function makeCheckoutDatePossible(){
	var orForm = document.forms[0];
	checkoutDay = orForm.checkoutDay.selectedIndex+1;
	checkoutMonth = orForm.checkoutMonth.selectedIndex;
	checkoutYear = orForm.checkoutYear.selectedIndex+2007;
	
	// Feb 29
	if ( (checkoutDay==29)&&(checkoutMonth==1)&&(checkoutYear%4!=0) ) orForm.checkoutMonth.selectedIndex=2;
	// Feb 29+
	if ( (checkoutDay>29)&&(checkoutMonth==1) ) orForm.checkoutMonth.selectedIndex=2;
	// Apr, June, Sept, Nov 31
	if ( (checkoutDay==31)&&(checkoutMonth==3||checkoutMonth==5||checkoutMonth==8||checkoutMonth==10) ) orForm.checkoutMonth.selectedIndex++;
}

function synchronizeCheckoutDate(){
	var orForm = document.forms[0];
	var checkinDate = new Date(orForm.checkinYear.selectedIndex+2007,
								orForm.checkinMonth.selectedIndex,
								orForm.checkinDay.selectedIndex+1);
	var checkoutDate = new Date(orForm.checkoutYear.selectedIndex+2007,
								orForm.checkoutMonth.selectedIndex,
								orForm.checkoutDay.selectedIndex+1);
	var dayAfterCheckinDate = new Date();
	// Calculate dayAfterCheckinDate: simply the next day
	dayAfterCheckinDate.setTime( checkinDate.getTime() );
	dayAfterCheckinDate.setDate( checkinDate.getDate() + 1 );
	
	if (checkoutDate < dayAfterCheckinDate) {
		initCheckoutDate();
		//alert (checkinDate);
	}
}

function checkForm(){
	var orForm = document.forms[0];
	
	// No forename
	if (orForm.forename.value == "") {
		document.getElementById("forename-label").style.fontSize= "150%";
		document.getElementById("forename-label").style.color= "red";
		isFormOk = false;
		isErrorState = true;
	}
	// No surname
	if (orForm.surname.value == "") {
		document.getElementById("surname-label").style.fontSize= "150%";
		document.getElementById("surname-label").style.color= "red";
		isFormOk = false;
		isErrorState = true;
	}
	// No e-mail
	if (orForm.email.value == "") {
		document.getElementById("email-label").style.fontSize= "150%";
		document.getElementById("email-label").style.color= "red";
		isFormOk = false;
		isErrorState = true;
	}
	// No telephone
	if (orForm.tel.value == "") {
		document.getElementById("tel-label").style.fontSize= "150%";
		document.getElementById("tel-label").style.color= "red";
		isFormOk = false;
		isErrorState = true;
	}
	// No fax
	if (orForm.faxconfirmation.checked && orForm.fax.value == "") {
		document.getElementById("fax-label").style.fontSize= "150%";
		document.getElementById("fax-label").style.color= "red";
		isFormOk = false;
		isErrorState = true;
	}
	
	emailString = orForm.email.value;
	// Is e-mail in correct format?
	if ( (emailString != "") && (verifyEmail(emailString) == false) ) {
		alert("E-posta adresinizde hata var. Lütfen kontrol ediniz.");
		document.getElementById("email-label").style.fontSize= "150%";
		document.getElementById("email-label").style.color= "red";
		isFormOk = false;
		isErrorState = true;	
	}
	
	// If everything OK submit the form
	if (isFormOk==true) {
		// Set hidden field "stay-period"
		var checkinDate = new Date(orForm.checkinYear.selectedIndex+2007,
									orForm.checkinMonth.selectedIndex,
									orForm.checkinDay.selectedIndex+1);
		var checkoutDate = new Date(orForm.checkoutYear.selectedIndex+2007,
								orForm.checkoutMonth.selectedIndex,
								orForm.checkoutDay.selectedIndex+1);
		var stayPeriod = checkoutDate - checkinDate;
		stayPeriod = Math.ceil(stayPeriod/(1000*60*60*24));
		orForm.stayPeriod.value = stayPeriod;
		var today = new Date();
		var day = today.getUTCDate();
		if (day<10) day = "0" + day;
		orForm.requestDay.value = day;
		var mon = today.getUTCMonth();
		mon++;
		if (mon<9) mon = "0" + mon;
		orForm.requestMonth.value = mon;
		orForm.requestYear.value = today.getUTCFullYear();
		var hr = today.getUTCHours();
		var min = today.getUTCMinutes();
		if (hr<10) hr = "0" + hr;
		if (min<10) min = "0" + min;
		orForm.requestTime.value = hr + ":" + min;
		// Submit the form
		orForm.submit();
	}
	else {
		document.getElementById("error-message").innerHTML="Lütfen zorunlu alanlarý doldurunuz.";
	}
}

function fieldChanged(fieldName) {
	var orForm = document.forms[0];
	if ((fieldName=="fax-field")&&(orForm.fax.value != "") ) orForm.faxconfirmation.checked=true;
	if (isErrorState==false) return;	// If no error state we have no other business here
	
	switch (fieldName) {
		case "forename-field":
			if (orForm.forename.value != "") {
				var forenameLabel = document.getElementById("forename-label");
				if (forenameLabel.style.color=="red") {
					document.getElementById("forename-label").style.fontSize= "100%";
					document.getElementById("forename-label").style.color= "white";
				}
			} else if (orForm.forename.value == "") {
				var forenameLabel = document.getElementById("forename-label");
				if (isErrorState == true) {
					document.getElementById("forename-label").style.fontSize= "150%";
					document.getElementById("forename-label").style.color= "red";
				}
			}
			break;
		case "surname-field":
			if (orForm.surname.value != "") {
				var surnameLabel = document.getElementById("surname-label");
				if (surnameLabel.style.color=="red") {
					document.getElementById("surname-label").style.fontSize= "100%";
					document.getElementById("surname-label").style.color= "white";
				}
			} else if (orForm.surname.value == "") {
				var surnameLabel = document.getElementById("surname-label");
				if (isErrorState == true) {
					document.getElementById("surname-label").style.fontSize= "150%";
					document.getElementById("surname-label").style.color= "red";
				}
			}
			break;
		case "email-field":
			if (orForm.email.value != "") {
				var emailLabel = document.getElementById("email-label");
				if (emailLabel.style.color=="red") {
					document.getElementById("email-label").style.fontSize= "100%";
					document.getElementById("email-label").style.color= "white";
				}
			} else if (orForm.email.value == "") {
				var emailLabel = document.getElementById("email-label");
				if (isErrorState == true) {
					document.getElementById("email-label").style.fontSize= "150%";
					document.getElementById("email-label").style.color= "red";
				}
			}
			break;
		case "tel-field":
			if (orForm.tel.value != "") {
				var telLabel = document.getElementById("tel-label");
				if (telLabel.style.color=="red") {
					document.getElementById("tel-label").style.fontSize= "100%";
					document.getElementById("tel-label").style.color= "white";
				}
			} else if (orForm.tel.value == "") {
				var telLabel = document.getElementById("tel-label");
				if (isErrorState == true) {
					document.getElementById("tel-label").style.fontSize= "150%";
					document.getElementById("tel-label").style.color= "red";
				}
			}
			break;
		case "fax-field":
		case "fax-confirmation-checkbox":
			if (orForm.faxconfirmation.checked==false || (orForm.faxconfirmation.checked && orForm.fax.value != "") ) {
				var faxLabel = document.getElementById("fax-label");
				if (faxLabel.style.color=="red") {
					document.getElementById("fax-label").style.fontSize= "100%";
					document.getElementById("fax-label").style.color= "white";
				}
			} else if (orForm.faxconfirmation.checked && orForm.fax.value == "") {
				var faxLabel = document.getElementById("fax-label");
				if (isErrorState == true) {
					document.getElementById("fax-label").style.fontSize= "150%";
					document.getElementById("fax-label").style.color= "red";
				}
			}
			break;
	}
	// A global check
	if (isErrorState == true) {	// We are in error state; so let's check
		if ( (orForm.forename.value != "")&&(orForm.surname.value != "")
					&&(orForm.email.value != "")
					&&(orForm.tel.value != "") 
					&&( (orForm.faxconfirmation.checked ==false)||(orForm.faxconfirmation.checked && orForm.fax.value != "") ) ) {
			// Remove error message or Display positive feedback
			isFormOk=true;
			document.getElementById("error-message").innerHTML="";
		} else {
			isFormOk=false;
			// Display error message
			document.getElementById("error-message").innerHTML="Lütfen zorunlu alanlarý doldurunuz.";
		}
	}
}
