//
//*Version Control
//****************************************************************************************************
//*                                                                                                  *
//*  Source: formcheck.js                                                                            *
//'*                                                                                                 *
//'*  Rev     Inits   Date    Description                                                            *
//'* -----    -----  -------  -----------                                                            *
//'*                                                                                                 *
//'*  0.0            	      Initial                                                                *
//'*                                                                                                 *
//'***************************************************************************************************
//
// 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 validates an email address
function checkEmail(f) {
	var str = f.value;
 	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
     	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	if (!(!reg1.test(str) && reg2.test(str))) { //   syntax is invalid
		alert('Please enter a valid email address');
 	   	f.focus();
 		f.select();
 	 	return (false);
	}
	return (true);
}

// 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.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;
			}
	}	
	if(document.emailForm.Email.value == "" && document.emailForm.TelHomeEvening.value == "" && document.emailForm.TelWorkDay.value == "")	{
		alert("Please enter either a valid telephone number or a valid email address");
	        document.emailForm.Email.focus();
			
		return false;
	} else {
		if(document.emailForm.Email.value != "")	{
			if (!checkEmail(document.emailForm.Email))
				return(false);
		}
	} 
	return true;
	
}

// This script checks if a telephone number or email address have been supplied for company contact info. If an email address
// has been supplied then it validates it. 
function checkCoContactInfo(theForm,id) 
{
	if (id) {
		if(theForm.CompanyId.value== "") {
			alert("Please enter a Company No");
			theForm.CompanyId.focus();
			return false;
		}
	}
	if(theForm.CompanyName.value == "") {
	        alert("Please enter your Company Name");
	        theForm.CompanyName.focus();
		return false;
	}	
	if(theForm.CompanyName.value == "") {
	        alert("Please enter your Company Name");
	        theForm.CompanyName.focus();
		return false;
	}	
	if(theForm.ContactName.value == "") {
	        alert("Please enter a Contact Name");
	        theForm.ContactName.focus();
		return false;
	}	
	if(theForm.Email.value == "" && theForm.TelWorkDay.value == "")	{
		alert("Please enter either a valid telephone number or a valid email address");
	        theForm.TelWorkDay.focus();
		return false;
	} else {
		if(theForm.Email.value != "")	{
			if (!checkEmail(theForm.Email))
				return(false);
		}
	} 
	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.arrivedate.value;
				
	endDate =  	document.inputForm.departdate.value;

	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 checkFormDates(theForm)
{
	startDate = theForm.arrivedate.value;
	
	endDate =  	theForm.departdate.value;
	
	alert(endDate);
	//alert(endDate);

	if (startDate >= endDate) 
	{
		alert("The arrival date must must be before the leaving date")
		return false;
	}
	else
		return true;
}



// Function which checks that appropriate start Days have been set
function checkStarts(theForm)
{
	if (theForm.RentalPeriod.selectedIndex==0) {
		theForm.RentalMonthStartDay.selectedIndex=0;
		theForm.RentalWeekStartDay.selectedIndex=0;
		return true;
	}
	if (theForm.RentalPeriod.selectedIndex==1) {
		if (theForm.RentalWeekStartDay.selectedIndex == 0) {
			theForm.RentalMonthStartDay.selectedIndex = 0;
			alert("Please select a Week Start Day");
			theForm.RentalWeekStartDay.focus();
			return false;
		}
	} else {
		if (theForm.RentalPeriod.selectedIndex == 2) {
			if (theForm.RentalMonthStartDay.selectedIndex == 0) {
					theForm.RentalWeekStartDay.selectedIndex = 0;
				alert("Please select a Month Start Day");
				theForm.RentalMonthStartDay.focus();
				return false;
			}
		}
	 }
		
	
	return true;
}


// Function which checks if a property reference number and price was supplied
function checkPropertyDetails(autoref) 
{
	if (!autoref) {
		if(document.inputForm.propertyref.value == "") 
		{
			alert("You must enter a Property Reference"); 
			document.inputForm.propertyref.focus();
			document.inputForm.propertyref.select();	
			return false;
		}
	}
	if(document.inputForm.country.selectedIndex == 0 ) 
	{ 
		alert("You must select a Country");
		document.inputForm.country.focus();
		return false;
	}
	if (document.inputForm.province.selectedIndex == 0 )
	{
		alert("You must select a Province");
		document.inputForm.province.focus();
		return false;
	}
	if (document.inputForm.location.selectedIndex == 0 )
	{
		alert("You must select a Location");
		document.inputForm.location.focus();
		return false;
	}	
	if(document.inputForm.price.value == "") 
	{
		alert("Please provide a rental price for this property");
		
		document.inputForm.price.focus();
		
		return false;
	}
//	return(checkStarts(document.inputForm));
	return true;
}




// 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;
	}
}
