/*
*  FORM VALIDATION SCRIPTING
*
*  Purpose : To provide input validation for all forms in the Tuscan Dream site
*/


/*----------------------------------------------------
*  Check that a valid e-mail address has been provided
*/
function CheckEmail(checkStr)
{
	// test if valid email address, must have @ and .
	var checkEmail	= "@.";
	var EmailValid	= false;
	var EmailAt	= false;
	var EmailPeriod = false;
	var EmailSpace	= false;
	var error	= "";
	
	// Find if the e-mail address has a space
	if (checkStr.indexOf(" ") > -1)
	{
		EmailSpace = true;
	}
	for (i = 0;i < checkStr.length;i++)
	{
		// Get each character in the e-mail address
		ch = checkStr.charAt(i);
		for (j = 0;j < checkEmail.length;j++)
		{
			//  Find an @ sign
			if (ch == checkEmail.charAt(j) && ch == "@")
			EmailAt = true;
			
			// Find a .
			if (ch == checkEmail.charAt(j) && ch == ".")
			EmailPeriod = true;
			
			//  If both @ and . found exit for loop
			if (EmailAt && EmailPeriod)
			break;
		
			//  If end of e-mail address reached, exit for loop
			if (j == checkEmail.length)
			break;
		}
		// if both the @ and . were in the string
		if ((EmailAt) && (EmailPeriod) && (!EmailSpace))
		{
			// State that the e-mail address is correct
			EmailValid = true
			break;
			error = "";
		}
	}
	
	// If the e-mail address is not valid, set the error variable to say what the problem is
	if (!EmailValid)
	{
		error	= "";
		if ((!EmailAt) && (!EmailPeriod))
		{
			error	+= " - Email must contain an \"@\" and a \".\"\n";
		}
		if (!EmailAt)
		{
			error += " - Email must contain an \"@\"\n";	
		}
		if (!EmailPeriod)
		{
			error += " - Email must contain an \".\"\n";	
		}
		if (EmailSpace)
		{
			error += " - Email must not contain a space\n";	
		}
		else
		{
			error = " - Email is invalid\n";	
		}
	}
	return error;
}


/*----------------------------------------------------
*  Check that a valid phone number has been provided
*/
function CheckTel(checkStr)
{
	var CharFound= false;
	var error	= "";
	
	// Get each character in the telephone number
	for (i = 0;i < checkStr.length;i++)
	{
		ch = checkStr.charAt(i);
		// Find out if the character is not a number
		if(isNaN(ch))
		{
			// If 'not a number' exit the loop
			CharFound = true;
			break;
		}
	}
	// If a character was found, set the error variable accordingly
	if (CharFound)
	{
		error	= "- You must only enter numbers in the telephone field\n";
	}
	return error;
}


/*----------------------------------------------------
*  Print any errors found to an alert box
*/
function ShowError(Errors)
{
	// Show an alert box with all the errors found
	alert("The following error(s) occurred:\n" + Errors.substring(Errors,Errors.length-1) + "\n\nSorry can not Process the form");
}


/*----------------------------------------------------
*  Check to ensure the mandatory fields have been filled
*/
function CheckCompulsary(FormName)
{
	var alertsay	= ""; 
	// Check a value has been given for 'name'
	if (document.getElementById(FormName).Name.value == "")
	{
		alertsay	+= "- You must enter your Name\n";
	}
	// Check a value has been given for 'tel'
	if (document.getElementById(FormName).Tel.value == "")
	{
		alertsay	+= "- You must enter a Telephone Number\n";
	}
	else
	{
		// Check a valid telephone number has been supplied
		alertsay	+= CheckTel(document.getElementById(FormName).Tel.value);
	}
	// Check a value has been given for 'email'
	if (document.getElementById(FormName).Email.value == "")
	{
		alertsay	+= "- You must enter an Email Address\n";
	}
	else
	{
		// Check a valid email address has been supplied
		alertsay	+= CheckEmail(document.getElementById(FormName).Email.value);
	}
	return alertsay;
}


/*----------------------------------------------------
*  Check the data entered into the 'contact' form
*/
function CheckContact()
{
	// Make sure all mandatory fields have been completed
	var alertsay	= CheckCompulsary("ContactForm");
	if (alertsay)
	{
		ShowError(alertsay);
		return false;
	}
	else
	{
		return true;
	}
}


/*----------------------------------------------------
*  Check the data entered into the 'arrange a viewing' form
*/
function CheckViewing()
{
	var alertsay	= "";
	// Check whether a property ID has been supplied
	if (document.getElementById("ViewingForm").PropertyID.value == "")
	{
		alertsay	+= "- You Select the Property\n";
	}
	// Make sure all mandatory fields have been completed
	alertsay	+= CheckCompulsary("ViewingForm");
	if (alertsay)
	{
		ShowError(alertsay);
		return false;
	}
	else
	{
		return true;
	}
}


/*----------------------------------------------------
*  Check the data entered into the 'brochure request' form
*/
function CheckBrochure()
{
	// Make sure all mandatory fields have been completed
	var alertsay	= CheckCompulsary("BrochureForm");
	if (alertsay)
	{
		ShowError(alertsay);
		return false;
	}
	else
	{
		return true;
	}
}


/*----------------------------------------------------
*  Check the data entered into the 'services' form
*/
function CheckServices()
{
	// Make sure all mandatory fields have been completed
	var alertsay	= CheckCompulsary("ServicesForm");
	if (alertsay)
	{
		ShowError(alertsay);
		return false;
	}
	else
	{
		return true;
	}
}