/*
   This file contains valid_form function which checks form.html when submitt is pressed
   Can also be used when function is called using ebent handlers such as onBlurr and onChange
*/

/*
   This is regular expression which check syntax of e-mail address. Expression from script 9.1 Negrino.
   Syntax: "\w" = any one characheter a-z or 0-9 or _  "+" = one or more of previous expression.
   "([\.-]?\w+)" = optional . or - followed by any numbe a-z or 0-9 as above. "*" any number of previous bracketed group.
   "@" = must have an comercial at. "\w" as above. ([\.]?\w+) as above. "(\.\w{2,3})+" one or more goups of . follwed by
   two or three "\w" as above.
   
*/

reEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

/*
   This is regular expression which checks phone number syntax.
   "(?" optional bracket "\d(3,6()" followed by three to six numbers 0-9 ")?" followed by an optional closing bracket
   [\.\-\/ ]? followed by optional . - or space expression then repeats.
   valid entries (012)-(345)-(679) and (123456) (222) (567890) and 01236 121212 and 000/555/999
*/

rePhone = /^\(?(\d{3,6})\)?[\.\-\/ ]?(\d{3,6})[\.\-\/ ]?(\d{3,6})$/

/*
   this is a regular expression to check for Visa cards. 
   Card must start 4 then have 3 digits with an optional a space or -, 
   and 3 sets of 4 digits seperated by an optional space or dash
*/

reCardVisa = /^4\d{3}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}$/;

/*
   this is a regular expression to check for MasterCards. 
   Card must start 51 to 55 then have 2 digits with an optional a space or -, 
   and 3 sets of 4 digits seperated by an optional space or dash
*/

reCardMaster = /^5[1-5]\d{2}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}$/;

/*  Start of function which contains if statements to test each line on form.html. inclusion o break means that first time 
    condition is false it returns user to form. focus() returns user to field which is in error "valid" is used
    as a flag to end condition at end of while loop. alert contaions the message displayed to user.
*/


/*
   This is regular expression which checks post code syntax.
*/







function validate_form ( )
{
	valid = true

	while( valid = true )
	
	{

        if ( document.form.Function1.value == "" )
        {
			alert ( "Function: Please tells us the type of function ie. Wedding, 21st Birthday, etc." );
             	form.Function1.focus();
			valid = false;
			return false;
			break;
			
        }
        
        
        if ( document.form.venue_name.value == "" )
        {
                	alert ( "Venue Name: Please tell us the venue for this function." );
                	form.venue_name.focus();
			valid = false;
			return false;
			break;
		    
        }
             


        if ( document.form.venue_town.value == "" )
        {
                	alert ( "Venue Name: Please tell us the City or Town which this venue is Located." );
                	form.venue_town.focus();
			valid = false;
			return false;
			break;
		    
        }


	if ( document.form.your_name.value == "" )
        {
                	alert ( "Your Name: Please tell us your name." );
                	form.your_name.focus();
		    	return false;
			break;
	  }     


    
	if ( document.form.Address.value == "" )
        {
                	alert ( "Address: Please tell us your house number & Street." );
                	form.Address.focus();
			valid = false;
			return false;
			break;
		    
        }

  


	if ( document.form.City.value == "" )
        {
                	alert ( "Your City: Please tell us which Town or City you stay in.");
                	form.City.focus();
			valid = false;
			return false;
			break;
		    
        }

        if ( document.form.Post_Code.value == "" )
        {
			alert ( "Your Post Code: Please tell us your Post Code." );
             	form.Post_Code.focus();
			valid = false;
			return false;
			break;
			
        }
        
/*        

        if ( rePcode.test(form.Post_Code.value )) 
        {
                	valid = true;
        }
        else 
	  {
                	alert ( "Your Post Code: You have not entered the correct syntax for a Post Code." );
                	form.Post_Code.focus();
			valid = false;
			return false;
			break;
        }

*/

        if ( document.form.Phone.value == "" )
        {
			alert ( "Your Contact Number: Please tell us your contact phone number." );
             	        form.Phone.focus();
			valid = false;
			return false;
			break;
			
        }
        



 

        if ( rePhone.test(form.Phone.value )) 
        {
                	valid = true;
        }
        else 
	  {
                	alert ( "Your Phone Number: You have not entered the correct syntax for a phone number." );
                	form.Phone.focus();
			valid = false;
			return false;
			break;
        }



                
                   
        if (reEmail.test(form.email.value)) 
        {
                	valid = true;
        }
        else
        {
        	    	alert ( "Your E-Mail: You have not entered an e-mail address, or is does not have the correct syntax.");
                	form.email.focus();
			valid = false;
			return false;
			break;
        }

//       
//   This tests credit card against regular expression which tests for Visa card ie. starts 4
//

        if (reCardVisa.test(form.billCardNumber.value)) 
        {
                	valid = true;

//       
//   This tests credit card against regular expression which tests for MasterCard ie. starts 51-55
//

	  }
        else if (reCardMaster.test(form.billCardNumber.value))
	  {		
			valid = true;
/*
      If neither Visa or master this condition returns user to form & field displaying alert message
      Additional card providers can be inserted above this farp of the function.
*/
	  }
	  else
	  {
	           	alert ( "Billing Information: Credit Card Number - blank, incorrect syntax, not Visa or MasterCard.");
             	form.billCardNumber.focus();
			valid = false;
			return false;
			break;
	  }


   	  return true;
	
	}		       
	
}


