//http://www.w3schools.com/js/js_form_validation.asp
//Use the following in the FORM tag: onsubmit="return validate_form(this);"
//make sure form fields have a NAME parameter.
//In the head, create a validate_form function, e.g.:
/*
<script src="assets/validate.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
function validate_form(thisform)
{
	with (thisform)
	{
		if (validate_email(email,"Not a valid e-mail address!")==false)
		  {email.focus();return false}
		if (validate_required(email,"Not a valid e-mail address!")==false)
		  {email.focus();return false}
		if (validate_length(phone,10,"Please enter your full phone number")==false)
		  {phone.focus();return false}
		if (validate_dropdown(adults,"Please enter the number of adults and children in your booking.")==false)
		  {adults.focus();return false}
	}
}
</script>
*/

// text field must not be empty.
function validate_required(field,alerttxt)
{
	with (field)
	{
	if (value==null||value=="")
	  {alert(alerttxt);return false}
	else {return true}
	}
}

// text field must be longer than 'len'.
function validate_length(field,len,alerttxt)
{
	with (field)
	{
	if (value==null||value==""||value.length<len)
	  {alert(alerttxt);return false}
	else {return true}
	}
}

//text field must be email address
function validate_email(field,alerttxt)
{
	//alert("validating email...");
	with (field)
	{
	apos=value.indexOf("@")
	dotpos=value.lastIndexOf(".")
	if (apos<1||dotpos-apos<2) 
	  {alert(alerttxt);return false}
	else {return true}
	}
}

//validate checkbox
function validate_checkbox(field,alerttxt)
{
	with (field)
	{
	if (checked==false)
	  {alert(alerttxt);return false}
	else {return true}
	}
}

//validate radiobuttons
function validate_radio(field,alerttxt)
{
	var cnt = -1;
	for (var i=field.length-1; i > -1; i--) 
	{
		if (field[i].checked) {cnt = i; i = -1;}
	}
	if (cnt == -1) {alert(alerttxt);return false}
	else {return true}
}

//validate dropdown list
function validate_dropdown(field,alerttxt)
{
	with (field)
	{
	if (selectedIndex == 0)
	  {alert(alerttxt);return false}
	else {return true}
	}
}
