// JavaScript Document

function highlight(field) {
       field.focus();
       field.select();
}

                
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}   
	
	function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Invalid Email Address");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid Email Address");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid Email Address");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid Email Address");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid Email Address");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid Email Address");
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid Email Address");
		    return false;
		 }

 		 return true					
	}



	
function validate_vip(vipForm)
{
   with (vipForm)
   {
      if(vipForm.name.value=="" || vipForm.email.value=="" || vipForm.valid_email.value=="")
	  {
	     alert("All fields are required.");
		 return false;
	  }
	  
	  var emailID=vipForm.email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email Address");
		return false;
		emailID.focus();
			}
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}
	  
      if(vipForm.email.value!=vipForm.valid_email.value)
	  {
	     alert("Emails do not match");
		 return false;
		 email.focus();
      }
	  if(vipForm.found.value=="Choose one")
	  {
	     alert("Please select how you found out about us.");
		 return false;
		 found.focus();
	  }
	  
	  if ( ( vipForm.found[0].checked == false )
    && ( vipForm.found[1].checked == false ) 
	&& ( vipForm.found[2].checked == false ) 
	&& ( vipForm.found[3].checked == false ) 
	&& ( vipForm.found[4].checked == false ) 
	&& ( vipForm.found[5].checked == false ) 
	&& ( vipForm.found[6].checked == false ) 
	&& ( vipForm.found[7].checked == false ) 
	&& ( vipForm.found[8].checked == false ) 
	&& ( vipForm.found[9].checked == false ) 
	&& ( vipForm.found[10].checked == false ) )
    {
        alert ( "Please select how you found us" );
        return false;
    }
	
		if ( (vipForm.found[10].checked == true )
	&& ( vipForm.other.value == "" ) )
	{
	alert ("Please specify how you found us");
	return false;
	other.focus();
	}
	
	}
}

function validate_competition(competitionForm)
{
   with (competitionForm)
   {
      if(competitionForm.fname.value=="" || competitionForm.sname.value=="" || competitionForm.pcode.value=="" || competitionForm.phone.value=="" || competitionForm.email.value=="" || competitionForm.valid_email.value=="")
	  {
	     alert("Fields marked with * are required.");
		 return false;
	  }
	  
	  var emailID=competitionForm.email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email Address");
		return false;
		emailID.focus();
			}
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}
	  
      if(competitionForm.email.value!=competitionForm.valid_email.value)
	  {
	     alert("Emails do not match");
		 return false;
		 email.focus();
      }
	  
	  if ( ( competitionForm.found[0].checked == false )
    && ( competitionForm.found[1].checked == false ) 
	&& ( competitionForm.found[2].checked == false ) 
	&& ( competitionForm.found[3].checked == false ) 
	&& ( competitionForm.found[4].checked == false ) 
	&& ( competitionForm.found[5].checked == false ) 
	&& ( competitionForm.found[6].checked == false ) 
	&& ( competitionForm.found[7].checked == false ) 
	&& ( competitionForm.found[8].checked == false ) 
	&& ( competitionForm.found[9].checked == false ) 
	&& ( competitionForm.found[10].checked == false ) )
    {
        alert ( "Please select how you found us" );
        return false;
    }
	
	if ( (competitionForm.found[10].checked == true )
	&& ( competitionForm.other.value == "" ) )
	{
	alert ("Please specify how you found us");
	return false;
	other.focus();
	}
	
	}
}

function validate_mail(emailForm) 
{
	with (emailForm)
	{
		if(emailForm.name.value=="")
		{
			alert("Enter your name");
			return false;
			name.focus();
		}
				if(emailForm.subject.value=="")
		{
			alert("Enter a subject");
			return false;
			subject.focus();
		}
				if(emailForm.email.value=="")
		{
			alert("Please enter your email address so I can get back to you!");
			return false;
			email.focus();
		}
		
		var email_values = /^[a-zA-Z0-9_\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/;
        if(!email_values.test(emailForm.email.value))
        { 
           alert("Invalid email address");
           email.focus();
           email.select();
           return false;
        }
				if(emailForm.message.value=="")
		{
			alert("Please enter a message.");
			return false;
			message.focus();
		}
	}   
}

function validate_custphoto(custphoto) 
{
	with (custphoto)
	{
		if(custphoto.fname.value=="")
		{
			alert("Enter your first name.");
			return false;
			fname.focus();
		}
		if(custphoto.lname.value=="")
		{
			alert("Enter your surname.");
			return false;
			lname.focus();
		}
	    if(custphoto.email.value=="")
		{
			alert("Please enter your email address.");
			return false;
			email.focus();
		}
		var email_values = /^[a-zA-Z0-9_\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/;
        if(!email_values.test(custphoto.email.value))
        { 
           alert("Invalid email address");
           email.focus();
           email.select();
           return false;
        }
		
		if(custphoto.email.value != custphoto.confirmemail.value)
		{
		   alert("Check your email address is correct");
		   confirmemail.focus();
		   confirmemail.select();
		   return false;
		}
		if(custphoto.phone.value=="")
		{
			alert("Enter your phone number.");
			return false;
			phone.focus();
		}
		if(custphoto.pcode.value=="")
		{
			alert("Enter your post code.");
			return false;
			pcode.focus();
		}
	}   
}

function required(field,atext)
{
with (field)
{
if (value==null||value=="")
  {alert(atext);return false;}
else {return true}
}
}

   function chooseone(searchform)
   {
   with (searchform)
   {
     if (searchform.title.value == ""&&searchform.author.value == "")
	 {alert("Must enter at LEAST one field");
	 title.focus();
	 return false;}
	 else {return true;}
   }
   }
   
   
