var allowedChars="0123456789abcdefghijklmnoprstquvwxyzABCDEFGHIJKLMNOPRSTQUVWXYZ_.,-@! ";
var allowedCharsLogin="0123456789abcdefghijklmnoprstquvwxyzABCDEFGHIJKLMNOPRSTQUVWXYZ_-!";

var phoneAllowedChars = "+-()0123456789"; 


function validateContactForm()
{
	var count = 0;
	var valid = false;
	//var strSql =  "SELECT, DELETE, UPDATE, ALTER, DROP, FROM, WHERE";
	var form = document.contactForm;
	//var sqlMsg = "Invalid Input";
	//var phoneAllowedChars = "0123456789"; 
	
	if(!checkInput(form, form.txtFName, 1, "*Name:", "You must enter a name"))
		count++;
	else
	{
		unhighlightLabel(form, "*Name:", 1);
	}
	//if(!checkInput(form, form.txtLName, 2, "*Last Name:", "You must enter a last name"))
		//count++;
	if(!checkEmailInput(form, form.txtEmail, 2))
		count++;
	else
	{
		unhighlightLabel(form, "*Email:", 2);
	}
	/*if(!checkInput(form, form.txtSecurityCode, 14, "*Security Code:", "You must enter the security code"))
		count++;
	if(!checkInput(form, form.txtNumOfPeople, 6, "*Number of People:", "You must enter the number of people wishing to stay"))
		count++;
	if(!checkInput(form, form.txtNumOfApartments, 8, "*Number of Apartments:", "You must enter the number of apartments you require"))
		count++;
	//if(!checkInput(form, form.txtSingleRooms, 7, "*Single Rooms:", "You must enter the number of single rooms you require"))
		//count++;*/
	/*if(count == 0)
	{
		if(checkSQLWords(form.txtFName.value, 1))
		{
			alert(sqlMsg);
			highlightLabel(form, "*First Name:", 1);
			count++;
		}
		if(checkSQLWords(form.txtFName.value, 1))
		{
			alert(sqlMsg);
			highlightLabel(form, "*Last Name:", 2);
			count++;
		}
			
	}*/
	
	/*if(!checkValidDate(form.selectDay.value, form.selectMonth.value, form.selectYear.value,
					form.selectDay1.value, form.selectMonth1.value, form.selectYear1.value))	
	{
		alert("Invalid dates selected, please ensure that your check in date is not less than the current date or your check out date is not before your check in date");
		highlightLabel(form, "*Date From:", 11);
		highlightLabel(form, "*Date To:", 12);
		count++;
	}*/
	
	if(!checkPhone(form.txtPhoneNo.value))
	{
			alert("Phone number can only be made up of the following characters " + phoneAllowedChars);
			highlightLabel(form, "*Phone Number:", 3);
			count++;
		
	}
	else
	{
		unhighlightLabel(form, "*Phone Number:", 3);
	}
	
	if(!checkInput(form, form.txtEnquiries, 4, "*Message:", "You must enter a message for the email"))
		count++;
	else
	{
		unhighlightLabel(form, "*Message:", 4);
	}
	
	if(count > 0)
		highlightLabel(form, "Fields highlighted in red have errors", 0);
	else if(count == 0)
	{
		unhighlightLabel(form, "Fields marked with an * are required fields", 0);
		valid = true;
	}
	
	return valid;	


}

function validateForm()
{
	
	var valid = false;
	
	var form = document.ChapsLogin;
	var count = 0;
	
	if(!checkInput(form, form.login_name, 1, "*Username:", "You must enter a username"))
		count++;
	if(!checkInput(form, form.login_pass, 2, "*Password:", "You must enter a password"))
		count++;
	if(count == 0)
	{
		unhighlightLabel(form, "Fields marked with an '*' are required fields", 0);
		valid = true;
	}
	else
		highlightLabel(form, "Fields highlighted in red have errors", 0);
	
	
	return valid;
	
	
}

function validateBookForm()
{
	var count = 0;
	var valid = false;
	//var strSql =  "SELECT, DELETE, UPDATE, ALTER, DROP, FROM, WHERE";
	var form = document.bookingForm;
	//var sqlMsg = "Invalid Input";
	//var phoneAllowedChars = "0123456789"; 
	
	if(!checkInput(form, form.txtFName, 1, "*First Name:", "You must enter a first name"))
		count++;
	if(!checkInput(form, form.txtLName, 2, "*Last Name:", "You must enter a last name"))
		count++;
	if(!checkEmailInput(form, form.txtEmail, 3))
		count++;
	if(!checkInput(form, form.txtSecurityCode, 14, "*Security Code:", "You must enter the security code"))
		count++;
	if(!checkInput(form, form.txtNumOfPeople, 6, "*Number of People:", "You must enter the number of people wishing to stay"))
		count++;
	if(!checkInput(form, form.txtNumOfApartments, 8, "*Number of Apartments:", "You must enter the number of apartments you require"))
		count++;
	//if(!checkInput(form, form.txtSingleRooms, 7, "*Single Rooms:", "You must enter the number of single rooms you require"))
		//count++;
	/*if(count == 0)
	{
		if(checkSQLWords(form.txtFName.value, 1))
		{
			alert(sqlMsg);
			highlightLabel(form, "*First Name:", 1);
			count++;
		}
		if(checkSQLWords(form.txtFName.value, 1))
		{
			alert(sqlMsg);
			highlightLabel(form, "*Last Name:", 2);
			count++;
		}
			
	}*/
	
	if(!checkValidDate(form.selectDay.value, form.selectMonth.value, form.selectYear.value,
					form.selectDay1.value, form.selectMonth1.value, form.selectYear1.value))	
	{
		alert("Invalid dates selected, please ensure that your check in date is not less than the current date or your check out date is not before your check in date");
		highlightLabel(form, "*Date From:", 11);
		highlightLabel(form, "*Date To:", 12);
		count++;
	}
	
	if(!checkPhone(form.txtPhoneNo.value))
	{
			alert("Phone number can only be made up of the following characters " + phoneAllowedChars);
			highlightLabel(form, "*Phone Number:", 4);
			count++;
		
	}
	
		
	if(count > 0)
		highlightLabel(form, "Fields highlighted in red have errors", 0);
	else if(count == 0)
	{
		unhighlightLabel(form, "Fields marked with an * are required fields", 0);
		valid = true;
	}
	
	return valid;	


}

function validateForm()
{
	
	var valid = false;
	
	var form = document.ChapsLogin;
	var count = 0;
	
	if(!checkInput(form, form.login_name, 1, "*Username:", "You must enter a username"))
		count++;
	if(!checkInput(form, form.login_pass, 2, "*Password:", "You must enter a password"))
		count++;
	if(count == 0)
	{
		unhighlightLabel(form, "Fields marked with an '*' are required fields", 0);
		valid = true;
	}
	else
		highlightLabel(form, "Fields highlighted in red have errors", 0);
	
	
	return valid;
	
	
}

function highlightLabel(form, title, index)
{
	//highlighting labels on for if field has an error
	//using the index of the label becuase am referecing labels
	//on the form based on abel indexes which start at 0 with the
	//first label
	var df = document.getElementsByTagName("label")[index];
	df.innerHTML = "<b><font color='#FF0000'>" +title+"</font>";
}
function unhighlightLabel(form, title, index)
{
	//unhighlighting labels on for if field has no error
	var df = document.getElementsByTagName("label")[index]
	df.innerHTML = "<font color='#333333'>" +title+"</font>";
}

function checkInput(form, field, labelIndex, title, errmsg)
{
	
	var allowedStr = "";
	allowedStr = allowedChars;
		
	if(field.value == "")
	{
		alert(errmsg);
		//getting firstName Label
		highlightLabel(form, title, labelIndex);
		//getting first label teling uer about the required fields
		field.focus();
		return false;
	}
	else if(!checkUserInput(field.value, allowedStr))
	{
		highlightLabel(form, title, labelIndex);
		alert("Characters entered must be made up of the following " + allowedChars);
		return false;
	}
	else
	{
		unhighlightLabel(form, title, labelIndex);
		return true;
	}
	
}


function checkEmailInput(form, emailFld, index)
{
	var email = emailFld.value;
	title = "*Email";
	if(email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)!=-1)
	{
		unhighlightLabel(form, title, index);
		return true;
	}
	else 
	{
		alert('Invalid email address! Please enter a valid address (i.e. joebloggs@blah.com)');
     		highlightLabel(form, title, index);
		//getting first label teling uer about the required fields
		emailFld.focus();
		return false
	}
}

function checkLeapYear(year)
{
	if(year %4 == 0 
		&&
		year %100 != 0
		||
		year %400 == 0
	)
		return true;
	else
		return false;
	
}

function checkValidDate(day, month, year, day1, month1, year1)
{
	/*var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var monthLength1 = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		
	if(checkLeapYear(year))
	{
		monthLength[1] = 29;
	}
	if(checkLeapYear(year1))
	{
		monthLength1[1] = 29;
	}	
	if(day <= monthLength[month] && day1 <= monthLength1[month1])
	{*/
		var checkInDate = new Date(year, month, day);
		var checkOutDate = new Date(year1, month1, day1);
	
		var date = new Date();
	
		if(checkInDate >= date)
		{
			if(checkOutDate > checkInDate)
				return true;
			else
				return false;
		}
		else
			return false;
	/*}
	else
		return false;
	*/
	
}

function checkUserInput(strVal, allowedStr)
{
	//checking to make sure user only used valid characters
	//checking to make sure user only used valid characters
	var i;
	var c;
	//var returnString="";
	var valid = true;
	
	if(strVal=="")
		valid = false;
	else
	{
		for(i=0;i<strVal.length;i++)
		{
			c=strVal.charAt(i);
			
			if(allowedStr.indexOf(c)!=-1)
				valid = true;
			else 
				return false;
		}
	}
	return valid;

}

function checkSQLWords(str, checkType)
{
	var disallowedWords = new Array(7);
	disallowedWords [0]= "SELECT";
	disallowedWords [1] = "DELETE";
	disallowedWords [2] = "UPDATE";
	disallowedWords [3] = "ALTER";
	disallowedWords [4] = "DROP";
	disallowedWords [5] = "FROM";
	disallowedWords [6] = "WHERE";
	
	str = str.toUpperCase();
	
	for(var i = 0; i < disallowedWords.length; i++)
	{
		if(checkType == 1)
		{
			if(disallowedWords[i] == str)	
				return true;
		}
		else
		{
			if(str.indexOf(disallowedWords[i]) != -1)	
			{
				return true;
				break;
			}
		}
	}
	return false;	
}

function checkPhone(strVal)
{
	//checking to make sure user only used valid characters
	var i;
	var c;
	//var returnString="";
	var valid = false;
	if(strVal=="")
	{
		return valid;
	}
	
	for(i=0;i<strVal.length;i++)
	{
		c=strVal.charAt(i);
		if(phoneAllowedChars.indexOf(c)!=-1)
			valid = true;
		else
			valid = false;
			
	}
	return valid;

}

	function displayYears(year)
	{
		var date = new Date();
		var curYear = date.getFullYear();
		var j = 0;
		for(var i = curYear; i < curYear + 5; i++)
		{
			document.write('<option value = ' + i + ' ');
			if(i == curYear || j == year || i == year)
				document.write('selected');
			document.write('>' + i + '</option>');
			j++;
		}
	}
	
	function updateDisplay(type)
	{
		var mnIndex;
		var yrIndex;
		
		if(type == 2)
		{
			mnIndex = document.getElementById("selectMonth").selectedIndex;
			yrIndex = document.getElementById("selectYear").options[document.getElementById("selectYear").selectedIndex].value;
			document.getElementById("selectMonth1").selectedIndex = mnIndex;
			document.getElementById("selectYear1").selectedIndex = document.getElementById("selectYear").selectedIndex;
		}
		else
		{
			mnIndex = document.getElementById("selectMonth1").selectedIndex;
			yrIndex = document.getElementById("selectYear1").selectedIndex;
		}
		displayMonthsDays(mnIndex, yrIndex, type);
		
	}

	function displayIstMonthDays(day, mn, yr)
	{
		var j= 1;

		var date = new Date();
		var month = 0;
		var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		//if a month index is passed  then use this as the index
		if(mn >= 0 && mn != "")
			month = mn;
		else
			month = date.getMonth();
		//if we are passing the index and not the full year we add the index to the current year to ensure the
		//correct year is displayed
		if(yr > 0 && yr < 10)
			yr += date.getFullYear();
		if(checkLeapYear(yr))
		{
			monthLength[1] = 29;
		}
		for(var i = 0; i < monthLength[month]; i++)
		{
			document.write('<option value = ' + j);
			if(j == date.getDate() || j == day)
				document.write(' selected');
			document.write('>' +j + '</option>');
			j++;
		}
	}
	function displayMonthsDays(mnIndex, year, type)
	{
		var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
		if(checkLeapYear(year))
		{
			monthLength[1] = 29;
		}
		var i, j = 1;

		for(i = 0; i < 31; i++)
		{
			if(type == 2)
			{
				document.getElementById("selectDay").options[i]=null;		
				document.getElementById("selectDay1").options[i]=null;
			}
			else //if(type == 3)
				document.getElementById("selectDay1").options[i]=null;
		}
		j = 1;
		var date = new Date();
		for(i = 0; i < monthLength[mnIndex]; i++)
		{
			
			if(type == 2)
			{
				if(i == date.getDate()-1)
				{
					document.getElementById("selectDay").options[i]=new Option(j,j,true);
					document.getElementById("selectDay1").options[i]=new Option(j,j,true);
				}
				else
				{
					document.getElementById("selectDay").options[i]=new Option(j,j);
					document.getElementById("selectDay1").options[i]=new Option(j,j);
				}
			}
			else
			{
				if(i == date.getDate()-1)
					document.getElementById("selectDay1").options[i]=new Option(j,j,true);
				else
					document.getElementById("selectDay1").options[i]=new Option(j,j);
			}
			j++;
			
		}
		
		
	}
	function displayMonths(month)
	{
		var months = new Array("January", "February", "March", 
								"April", "May", "June", "July", "August",
									"September", "October", "November",
										"December");
		var curMonth = new Date();
		for(var i = 0; i < months.length; i++)
		{
			document.write('<option value = ' + i);
			if(curMonth.getMonth() == i || month == i)
				document.write(' selected ');
			document.write('>' + months[i] + '</option>');
		}
	}
	
	

	
	function updateSelect(cal) 
	{
		var date = cal.date;
		var curYear = new Date();
		var selectMonth = document.getElementById("selectMonth");
		selectMonth.selectedIndex = date.getMonth();
		var selectDay = document.getElementById("selectDay");
		var selectYear = document.getElementById("selectYear");
		selectYear.selectedIndex = (date.getFullYear()-curYear.getFullYear());
		displayMonthsDays(selectMonth.selectedIndex, document.getElementById("selectYear").options[selectYear.selectedIndex].value, 2);
		selectDay.selectedIndex = (date.getDate() - 1);
		
		selectMonth = document.getElementById("selectMonth1");
		selectMonth.selectedIndex = date.getMonth();
		selectDay = document.getElementById("selectDay1");
		selectDay.selectedIndex = (date.getDate() - 1);
		selectYear = document.getElementById("selectYear1");
		selectYear.selectedIndex = (date.getFullYear()-curYear.getFullYear());
	}
	function updateSelect2(cal2) 
	{
		var date = cal2.date;
		var curYear = new Date();
		var selectMonth = document.getElementById("selectMonth1");
		selectMonth.selectedIndex = date.getMonth();
		var selectDay = document.getElementById("selectDay1");
		selectDay.selectedIndex = (date.getDate() - 1);
		var selectYear = document.getElementById("selectYear1");
		selectYear.selectedIndex = (date.getFullYear()-curYear.getFullYear());
	}


		<!--  to hide script contents from old browsers
		var startDate;
		var endDate;

		function resetDates() {
			startDate = endDate = null;
		}

		function filterDates1(cal) {
			startDate = new Date(cal.date)
			startDate.setHours(0,0,0,0)	// used for compares without TIME
			/* If they haven't chosen an 
			end date before we'll set it to the same date as the start date This
			way if the user scrolls in the start date 5 months forward, they don't
			need to do it again for the end date.
			*/

			if (endDate == null) { 
				Zapatec.Calendar.setup({
					inputField     :    "sel11",
					button         :    "buttonCal2",  // What will trigger the popup of the calendar
					ifFormat       :    "%b %d, %Y",
					date           :     cal.date,
					showsTime      :     false,          //no time
					dateStatusFunc		: disallowDateBefore, //the function to call
					onUpdate       :    filterDates2
				});
			}
			
		}

		function filterDates2(cal) {
			var date = cal.date;
			endDate = new Date(cal.date)
			endDate.setHours(0,0,0,0)	// used for compares without TIME
			
		}	

		/*
		* This functions return true to disallow a date
		* and false to allow it.
		*/


		/* 
		* Check-Out calendar allowed dates
		* Check-Out date can not be BEFORE Check-In date
		* Check-Out date can not be before today
		*/
		function disallowDateBefore(dateCheckOut) {
			dateCheckOut.setHours(0,0,0,0)
			if ((startDate != null) && startDate > dateCheckOut)
				// startDate is defined, make sure cal date is NOT before start date
				return true; 
			
			var now = new Date()
			now.setHours(0,0,0,0)
			if (dateCheckOut < now) 
				// check out date can not be befor today if startDate NOT defined
				return true;

			return false;
		}

		/* 
		* Check-In date checking
		* Check-In date can not be AFTER Check-Out date
		* Check-In date can not be before today
		*/
		function disallowDateAfter(dateCheckIn) {
			dateCheckIn.setHours(0,0,0,0)
			if ((endDate != null) && dateCheckIn > endDate)
				// endDate defined, calendar date can NOT be after endDate
				return true;

			var now = new Date()
			now.setHours(0,0,0,0)

			if (dateCheckIn < now)
				// endDate NOT defined, calendar date can not be before today
				return true;

			return false;
		}

		// end hiding contents from old browsers 



