// This function will validate a form
// Written by: Thi Thach
// Date: 16 May 2007 9:06am

function validateForm(theform, formname)
{
	pass = 1; //assume everything is ok
	msg = "The following problem was found in trying to submit this form:\n\n";
	
	
	//Check enquiry form or contact us form
	//======================================
	
	if (formname == "enquiry")
	{
		//Enquiry form
		
		//make sure required fields are not empty
		
		//first name
		if (isEmpty(theform.realname.value))
		{
			msg = msg + "- First Name cannot be empty\n";
			pass = 0;
		}
		
		//last name
		if (isEmpty(theform.LastName.value))
		{
			msg = msg + "- Last Name cannot be empty\n";
			pass = 0;
		}
		
		//email
		if (isEmpty(theform.email.value))
		{
			msg = msg + "- Email address cannot be empty\n";
			pass = 0;
		}
		else
		{		
			//validate the email address
			if (!(isEmail(theform.email.value)))
			{
				msg = msg + "- Please enter a valid email address\n";
				pass = 0;
			}
		}
		
		//pickup location
		if (isEmpty(theform.Pick_Up_Location.value))
		{
			msg = msg + "- Pick Up Location cannot be empty\n";
			pass = 0;
		}
		
		//drop off location
		if (isEmpty(theform.Drop_Off_Location.value))
		{
			msg = msg + "- Drop Off Location cannot be empty\n";
			pass = 0;
		}		
		
		//category
		if (theform.Rental_Category.selectedIndex == 0)
		{
			msg = msg + "- Category cannot be empty\n";
			pass = 0;		
		}
		
		//pick up date
		if (theform.Rental_Pickup_Month.selectedIndex == 0)
		{
			msg = msg + "- Rental Pick Up Month cannot be empty\n";
			pass = 0;		
		}
		
		if (theform.Rental_Pickup_Day.selectedIndex == 0)
		{
			msg = msg + "- Rental Pick Up Day cannot be empty\n";
			pass = 0;		
		}
		
		if (theform.Rental_Pickup_Year.selectedIndex == 0)
		{
			msg = msg + "- Rental Pick Up Year cannot be empty\n";
			pass = 0;		
		}
		
		//return date
		if (theform.Return_Month.selectedIndex == 0)
		{
			msg = msg + "- Return Month cannot be empty\n";
			pass = 0;		
		}
		
		if (theform.Return_Day.selectedIndex == 0)
		{
			msg = msg + "- Return Day cannot be empty\n";
			pass = 0;		
		}
		
		if (theform.Return_Year.selectedIndex == 0)
		{
			msg = msg + "- Return Year cannot be empty\n";
			pass = 0;		
		}
		
		
		
	}
	else
	{
		//Contact-us form


		//make sure required fields are not empty
		if (isEmpty(theform.email.value))
		{
			msg = msg + "- Contact email address cannot be empty\n";
			pass = 0;
		}
		else
		{
		
			//validate the email address
			if (!(isEmail(theform.email.value)))
			{
				msg = msg + "- Please enter a valid contact email address\n";
				pass = 0;
			}
		}
	
	}
	
	
	if (pass == 1)
	{
		return true;
	}
	else
	{
		alert(msg);
		return false;
	}
  
}

function isEmpty (s) {
	var p = /\S+/;
	return !p.test(s);
}

function isEmail(string) {
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
        return false;
}