// Includes many functions for checking various form elements
// Andy Weeks
// 26/01/04
//
//
// Includes the following:
// checkContactInfo(usesFullname) 			checks contact info on the form has been supplied
// checkDates()								checks if a start date is equal to or after an end date
// checkPriceDetails()						checks if required rental price details were supplied
// checkPropertyDetails()					checks if required property details were supplied
// checkNumericKeyStroke(includePoint)		checks if a numeric key was pressed




// This function makes sure all fields of a form has been filled in
function checkForm(f) {
	//var f = document.forms[0];
	var req = false;
	var valid = false;
	var reqFields = new Array(
							  "AgentRef",
							  "OwnerRef",
							  "TenantRef",
							  "Fullname",
							  "Forename",
							  "Surname",
							  "TelHomeEvening",
							  "TelWorkDay",
							  "email"
							 );
	var el = f.elements;
	for(a = 0; a < el.length; a++) {
		if(el[a].type != "button") {
			req = false;
			for(i = 0; i < reqFields.length; i++) {
				if(reqFields[i] == el[a].name) {
						req = true;
				}
			}

			if((req == true) && (el[a].value != "")) {
				valid = true;
			}
		}
	}

	if(valid == false) {
		if(!confirm("You have not entered any information, do you wish to continue?")) {
			return false;
		}
	}

	f.submit();
}


// This script checks if a telephone number or email address have been supplied. If an email address
// has been supplied then it validates it. The switch parameter determines whether two separate forename
// and surname boxes were used or just one
function checkContactInfo(usesFullname) 
{
    if(usesFullname) 
	{
		if(document.emailForm.Fullname.value == "")
		{
	        alert("Please enter your name");
	        document.emailForm.Fullname.focus();
			
			return false;
	    }	
		else if(document.emailForm.Email.value == "")
		{
			alert("Please enter an email address");
	        document.emailForm.Email.focus();
			
			return false;
	    }
		else if(document.emailForm.Email.value != "")
		{
			addr = document.emailForm.Email.value;
	
	        // Split on at and dot sign
	        bits = addr.split("@");
	    	bits2 = addr.split(".");
	
	        // Check if length is less than 2
	        if(bits.length < 2)	
			{
	            alert("Please enter a valid email address");
	            return false;
	        } 
			else if (bits2.length < 2) 
			{
	          	alert("Please enter a valid email address");
	            document.emailForm.Email.focus();
				
				return false;
	    	} 
			else 
			{
	        	return true;
	        }
	    } 
		else
	        return true;
	} 
	else if(!usesFullname) 
	{
		if(document.emailForm.Forename.value == "")
		{
	        alert("Please enter your forename");
	        document.emailForm.Forename.focus();
			
			return false;
	    }
		else if(document.emailForm.Surname.value == "")
		{
	        alert("Please enter your surname");
	        document.emailForm.Surname.focus();
			
			return false;
	    }
		else if(document.emailForm.Email.value == "")
		{
			alert("Please enter an email address");
	        document.emailForm.Email.focus();
			
			return false;
	    }
		else if(document.emailForm.Email.value != "")
		{
			addr = document.emailForm.Email.value;
	
	        // Split on at and dot sign
	        bits = addr.split("@");
	    	bits2 = addr.split(".");
	
	        // Check if length is less than 2
	        if(bits.length < 2)	
			{
	            alert("Please enter a valid email address");
	            return false;
	        } 
			else if (bits2.length < 2) 
			{
	          	alert("Please enter a valid email address");
	            document.emailForm.Email.focus();
				
				return false;
	    	} 
			else 
			{
	        	return true;
	        }
	    } 
		else
	        return true;
	}
	
}




// This function checks if a start date is after or equal to the end date. If so then it returns false
function checkDates()
{
	startDate = document.inputForm.startDateYear.options[document.inputForm.startDateYear.options.selectedIndex].value +
				document.inputForm.startDateMonth.options[document.inputForm.startDateMonth.options.selectedIndex].value +
				document.inputForm.startDateDay.options[document.inputForm.startDateDay.options.selectedIndex].value;
	
	endDate =  	document.inputForm.endDateYear.options[document.inputForm.endDateYear.options.selectedIndex].value +
				document.inputForm.endDateMonth.options[document.inputForm.endDateMonth.options.selectedIndex].value +
				document.inputForm.endDateDay.options[document.inputForm.endDateDay.options.selectedIndex].value;
	
	//alert(startDate);
	//alert(endDate);
	
	if (startDate >= endDate) 
	{
		alert("The arrival date must must be before the leaving date")
		return false;
	}
	else
		return true;
}




// This function checks if a start date is after or equal to the end date. If so then it returns false
function checkPriceDetails()
{
	if(document.inputForm.price.value == "") 
	{
		alert("Please provide a rental price");
		
		document.inputForm.price.focus();
		
		return false;
	}
	else
	{
		startDate = document.inputForm.startDateYear.options[document.inputForm.startDateYear.options.selectedIndex].value +
					document.inputForm.startDateMonth.options[document.inputForm.startDateMonth.options.selectedIndex].value +
					document.inputForm.startDateDay.options[document.inputForm.startDateDay.options.selectedIndex].value;
	
		endDate =  	document.inputForm.endDateYear.options[document.inputForm.endDateYear.options.selectedIndex].value +
					document.inputForm.endDateMonth.options[document.inputForm.endDateMonth.options.selectedIndex].value +
					document.inputForm.endDateDay.options[document.inputForm.endDateDay.options.selectedIndex].value;
	
		//alert(startDate);
		//alert(endDate);
	
		if (startDate >= endDate) 
		{
			alert("The arrival date must must be before the leaving date")
			return false;
		}
		else
			return true;
	}
}




// Function which checks if a property reference number and price was supplied
function checkPropertyDetails() 
{
	
	isok = true;
	
	if(document.inputForm.locality.value == "")
	{
		alert("You must input your town/urbanisation.");
		
		document.inputForm.locality.focus();
		
		isok = false;
	}
	if((document.inputForm.contacttelephone.value == "") || (document.inputForm.contacttelephone.value.length < 9))
	{
		alert("You must provide a valid telephone number.");
		
		document.inputForm.contacttelephone.focus();
		
		return false;		
	}
	if(document.inputForm.propertyref.value == "") 
	{
		if(!confirm("You have not provided a property reference number. Without a reference number, " +
				"it will be more difficult for users to locate this property. Do you wish to continue " +
				"without providing a reference number now?")) 
		{
			document.inputForm.propertyref.focus();
			
			isok = false;
		}
	}
	if (isok == true) {
		
		return true;
		
	} else {
			
		return false;
			
	}
}

// checks if a numeric key was pressed. Parameter determines if the decimal point is allowed also
function checkNumericKeyStroke(includePoint)
{
	if(includePoint)
	{
		if (event.keyCode < 46 || event.keyCode > 57) 
			event.returnValue = false;
		else
			return true;
	}
	else
	{	
		if (event.keyCode < 47 || event.keyCode > 57) 
			event.returnValue = false;
		else
			return true;
	}
}
