var regValidInteger = /\D/;
// Array holding the days in the month
var daysInMonth = new Array();
daysInMonth[1] = 31;
daysInMonth[2] = 29;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function getBookingType() {
	
	// Determine type of control used to select booking type (select or option)
	var selectBookingType = true;
	
	if (document.getElementById("bookingtypeid1")) {
		selectBookingType = false;
	}
	
	if (selectBookingType)	// Get bookingtypeid from select
	{
		if (document.getElementById("bookingtypeid")) {
			bookingTypeId = document.getElementById('bookingtypeid').value;
		}
		else
		{
			return false;
		}
	}
	else			// Get bookingtypeid from option
	{
		for (i=0;i<document.step1.bookingtypeid.length;i++) {
			if (document.step1.bookingtypeid[i].checked) {
				bookingTypeId = document.step1.bookingtypeid[i].value;
			}
		}
	}
	
	return bookingTypeId;
}

// General purpose function that removes whitespace from within passed in string.
function stripWhitespace (str) { 
	var whitespace = " \t\n\r";
	var returnString = "";
	for (var i = 0; i < str.length; i++) { 
		var c = str.charAt(i);
		if (whitespace.indexOf(c) == -1) returnString += c;
	};
	return returnString;
};
// Function to check if input is numeric
function isValidInteger(strNumber) {	
	if ( stripWhitespace(strNumber).length == 0) {
		return true;
	};
	if (strNumber.search(/\D/) == -1) {
		return true;
	};
	return false;
};
// Function to check field value contains something
function isValidMandatory(str) {
	if (stripWhitespace(str).length == 0) {
		return false;
	};
	return true;
};
// Function that returns the days in february based on year
function isValidDaysInFebruary(year) {   
	if ( (year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0)) ) {
		return 29
	};
	return 28
};
// Function to validate dates
function isValidDate(day, month, year) { 
	if ( stripWhitespace(day + month + year).length == 0 ) { 
		return true;
	}; 
	if (! isValidInteger(day + month + year) ) {
		return false;
	}; 
	if ( year.length != 2 && year.length != 4) {
		return false;
	}; 
	var intYear = parseInt(year);
	var intMonth = parseInt(month);
	var intDay = parseInt(day); 
	if ( day.length == 2 && day.charAt(0) == 0 ) {
		intDay = day.charAt(1);
	};
	if ( month.length == 2 && month.charAt(0) == 0 ) {
		intMonth = month.charAt(1);
	}; 
	if ((intYear <= 0) || (intMonth <= 0) || (intYear <= 0)) {
		return false;
	}; 
	if (intDay > daysInMonth[intMonth] || intMonth > 12) { 
		return false; 
	}; 
	if ((intMonth == 2) && (intDay > isValidDaysInFebruary(intYear))) { 
		return false;
	};
	return true;
};

function QQ_validateQuicksearchForm(objForm) {
	
	var errMsg = quickQuoteErrorMessages['ERROR_MESSAGE'] + "\n\n";
	var isErr = false;
	
	// Default style has 2 date fields now - arrival and departure
	var defaultStyle = false;
	if (document.getElementsByName("today").length == 0) {	
		defaultStyle = true;
	}

	// Set available date to one day in the future
	var availableDate = new Date();
	availableDate.setDate(availableDate.getDate() + 1);
	
	if (defaultStyle)
	{ 
		
		var from= objForm.from.value;
		var to= objForm.to.value;
		if (from.length==0)
		{
			isErr= true;
			errMsg += quickQuoteErrorMessages['ARR_FROM_ERROR'] + "\n";
		}
		if (from==to)
		{ 
			isErr= true;
			errMsg += quickQuoteErrorMessages['ARR_FROMTO_ERROR'] + "\n";
		}

		var bookingTypeId = getBookingType();
		
		if ( (bookingTypeId == '1') || (bookingTypeId == '2') ) {
			
			var arrivalmonthyear = objForm.desde.value; 
			var arrivalday = objForm.arrivalday.value;   
			var arrivalmonth =objForm.desde.value;  arrivalmonth=arrivalmonth.substring(0,2); 
			var arrivalyear = objForm.desde.value;  arrivalyear=arrivalyear.substring(2,6); 
			var arrivalDateTime = new Date(arrivalyear, arrivalmonth-1, arrivalday);
			
			if ( arrivalDateTime < availableDate ) { 
				isErr = true; 
				errMsg += quickQuoteErrorMessages['ARR_DATE_ERROR'] + "\n"; 
			};
		
			if (! isValidDate(arrivalday, arrivalmonth, arrivalyear) ) {  
				errMsg += quickQuoteErrorMessages['VALID_DATE_ERROR'] + "\n";
				isErr = true;
			};
		};

		if ( (bookingTypeId == '1') || (bookingTypeId == '3') ) {
			
			var departuremonthyear = objForm.hasta.value;
			var departureday = objForm.returnday.value;
			var departuremonth =objForm.hasta.value;  departuremonth=departuremonth.substring(0,2); 
			var departureyear = objForm.hasta.value;  departureyear=departureyear.substring(2,6); 
			var departureDateTime = new Date(departureyear, departuremonth-1, departureday);
	
			if ( departureDateTime < availableDate ) {
				isErr = true;
				errMsg += quickQuoteErrorMessages['DEP_DATE_ERROR'] + "\n";
			};
		
			if (! isValidDate(departureday, departuremonth, departureyear) ) {
				errMsg += quickQuoteErrorMessages['VALID_DATE_ERROR'] + "\n";
				isErr = true;
			};
		};
		
		if (bookingTypeId == '1') {
			
			// Check departure date after arrival date
			if (arrivalDateTime >= departureDateTime)
			{  	
				errMsg += quickQuoteErrorMessages['DEP_DATE_FIRST_ERROR'] + "\n";
			};
		};

				
	} else {
		
		var today = objForm.today.value;
		var tomonthyear = objForm.tomonthyear.value;
		var tomonth = tomonthyear.substring(0,2);
		var toyear = tomonthyear.substring(2,6);
		var toDateTime = new Date(toyear, tomonth-1, today, 23, 59);
		
		if ( toDateTime < availableDate ) {
			isErr = true;
			errMsg += quickQuoteErrorMessages['ARR_DATE_ERROR'] + "\n";
		};
	
		if (! isValidDate(today, tomonth, toyear) ) {
			errMsg += quickQuoteErrorMessages['VALID_DATE_ERROR'] + "\n";
			isErr = true;
		};
	};
	
	if (isErr) {
		alert(errMsg); 
		return false;
	};

	return true;
	
}

		
function QQ_changeBookingType() {

	var bookingTypeId = getBookingType();
	
	// Enable/disable dates based on booking type
	switch (bookingTypeId)
	{
		case '1':
			document.getElementById('from').disabled = 'disabled';
			document.getElementById('from').value = 'Málaga Airport';
			document.getElementById('to').disabled = '';
			document.getElementById('arrivalday').disabled = '';
			document.getElementById('desde').disabled = '';
						
			document.getElementById('returnday').disabled = '';
			document.getElementById('hasta').disabled = '';
			break;
			
		case '2':
			document.getElementById('from').disabled = 'disabled';
			document.getElementById('from').value = 'Málaga Airport';
			document.getElementById('to').disabled = '';
			document.getElementById('arrivalday').disabled = '';
			document.getElementById('desde').disabled = '';
			
			document.getElementById('returnday').disabled = 'disabled';
			document.getElementById('hasta').disabled = 'disabled';
			break;
			
		case '3':
			document.getElementById('from').disabled = '';
			document.getElementById('from').value = '';
			document.getElementById('to').disabled = 'disabled';
			document.getElementById('to').value = 'Málaga Airport';
			document.getElementById('arrivalday').disabled = 'disabled';
			document.getElementById('desde').disabled = 'disabled';
			
			document.getElementById('returnday').disabled = '';
			document.getElementById('hasta').disabled = '';
			break;
	}
}
