/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
		if (i == 0){
			 if ( c != '+') return false;
		}
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}



// JavaScript Document
function ValidateDoc(form)
{

	
    var cr        = unescape("%0D");
    var ErrorMsg  = "Please check the following error(s):"+cr+cr;
    var InitLen   = ErrorMsg.length;
   
    
	if (form == 'step_1a'){
		 var TheForm   = document.step_1a;
		 if (TheForm.course.selectedIndex == '00')
        ErrorMsg =  ErrorMsg + "Course not selected" + cr;
	}
	
	if (form == 'accommodation'){
		 var TheForm   = document.accommodation;
		 if (TheForm.accommodation.selectedIndex == '00')
        ErrorMsg =  ErrorMsg + "Accommodation not selected" + cr;
	}
	
	if (form == 'add_accommodation'){
		 var TheForm   = document.accommodation;
		 if (TheForm.accommodation.selectedIndex == '00')
        ErrorMsg =  ErrorMsg + "Accommodation not selected" + cr;
		
		 if (TheForm.add_accommodation.selectedIndex == '00')
        ErrorMsg =  ErrorMsg + "Accommodation for additional corse not selected" + cr;
	}
	
	if (form == 'contact_details'){
		 var TheForm   = document.contact_details;
		 
	 
		 
		 if (TheForm.fname.value.length < 1)
        ErrorMsg =  ErrorMsg + "First name" + cr;
		
		 if (TheForm.surname.value.length < 1)
        ErrorMsg =  ErrorMsg + "Surname" + cr;
		
		  if (TheForm.sex.selectedIndex == '00')
        ErrorMsg =  ErrorMsg + "Sex" + cr;
		
		 if (TheForm.email.value.length < 1)
       ErrorMsg =  ErrorMsg + "Email" + cr;
    else if (TheForm.email.value.length < 4 || TheForm.email.value.indexOf ('@', 0) == -1 || TheForm.email.value.indexOf ('.', 0) == -1 || (TheForm.email.value.substring(1,0)=='@'))
        ErrorMsg =  ErrorMsg + "Your email is not invalid."+cr; 
		
	   if (TheForm.hphone.value.length < 1)  
	   ErrorMsg =  ErrorMsg + "Day phone" + cr;
		else if (checkInternationalPhone(TheForm.hphone.value)==false)
		ErrorMsg =  ErrorMsg + "Please enter a valid day phone number" + cr;
	   
	   if (TheForm.mphone.value != "")  {
			if (checkInternationalPhone(TheForm.mphone.value)==false)
		ErrorMsg =  ErrorMsg + "Please enter a valid mobile phone number" + cr;
	   }
	   
	    if (TheForm.address.value.length < 1)
        ErrorMsg =  ErrorMsg + "Address" + cr;
		
		 if (TheForm.state.value.length < 1)
        ErrorMsg =  ErrorMsg + "County/Sate" + cr;
		
		if (TheForm.country.selectedIndex == '00')
        ErrorMsg =  ErrorMsg + "Country" + cr;
		
		 if (TheForm.zip_post.value.length < 1)
        ErrorMsg =  ErrorMsg + "Zip/Post code" + cr;
		
		 if (TheForm.state_health.value.length < 1)
        ErrorMsg =  ErrorMsg + "State health" + cr;
		
		 if (TheForm.emc_name.value.length < 1)
        ErrorMsg =  ErrorMsg + "Emergency contact name" + cr;
		
		 if (TheForm.emc_relationship.value.length < 1)
        ErrorMsg =  ErrorMsg + "Relationship" + cr;
		
		 if (TheForm.emc_tel.value.length < 1){
        ErrorMsg =  ErrorMsg + "Emergency contact Tel." + cr;}
		else if (checkInternationalPhone(TheForm.emc_tel.value)==false)
		ErrorMsg =  ErrorMsg + "Please Enter a valid emergency day phone number" + cr;

	}
	
	if (form == 'step3'){

		 var TheForm   = document.step3;
		 
		 if (!(TheForm.payment[0].checked || TheForm.payment[1].checked ))
        ErrorMsg =  ErrorMsg + "Select Full or Deposit payment" + cr;
		
		 if (TheForm.payment_method.selectedIndex == '00')
        ErrorMsg =  ErrorMsg + "Payment method" + cr;
		
	  // if (!(TheForm.term[0].checked || TheForm.term[1].checked))
        //ErrorMsg =  ErrorMsg + "You must AgreeTerm and Condition" + cr;
		
	}
		
	
	if (form == 'free_articles'){

		 var TheForm   = document.free_articles;
		 
		  if (TheForm.name.value.length < 1)
        ErrorMsg =  ErrorMsg + "Name" + cr;
		 
		 if (TheForm.email.value.length < 1)
       ErrorMsg =  ErrorMsg + "Email" + cr;
    else if (TheForm.email.value.length < 4 || TheForm.email.value.indexOf ('@', 0) == -1 || TheForm.email.value.indexOf ('.', 0) == -1 || (TheForm.email.value.substring(1,0)=='@'))
        ErrorMsg =  ErrorMsg + "Your email is not invalid."+cr; 
		
		 if (TheForm.articles.selectedIndex == '00')
        ErrorMsg =  ErrorMsg + "Selected articles" + cr;
		
	  // if (!(TheForm.term[0].checked || TheForm.term[1].checked))
        //ErrorMsg =  ErrorMsg + "You must AgreeTerm and Condition" + cr;
		
	}
	if (form == 'tell_friend'){
    
		 var TheForm = document.tell_friend;
		 
		 if (TheForm.friend_email.value.length < 1)
       ErrorMsg =  ErrorMsg + "Friend's email" + cr;
    else if (TheForm.friend_email.value.length < 4 || TheForm.friend_email.value.indexOf ('@', 0) == -1 || TheForm.friend_email.value.indexOf ('.', 0) == -1 || (TheForm.friend_email.value.substring(1,0)=='@'))
        ErrorMsg =  ErrorMsg + "Frien's email is not invalid."+cr; 
		
		if (TheForm.sender_email.value.length < 1)
       ErrorMsg =  ErrorMsg + "Your email" + cr;
    else if (TheForm.sender_email.value.length < 4 || TheForm.sender_email.value.indexOf ('@', 0) == -1 || TheForm.sender_email.value.indexOf ('.', 0) == -1 || (TheForm.sender_email.value.substring(1,0)=='@'))
        ErrorMsg =  ErrorMsg + "Your email is not invalid."+cr; 
		
	}
		
		if (form == 'corporate'){

		 var TheForm   = document.corporate;
		 
		  if (TheForm.Name.value.length < 1)
        ErrorMsg =  ErrorMsg + "Name" + cr;
		
		if (TheForm.Company.value.length < 1)
        ErrorMsg =  ErrorMsg + "Company" + cr;
		
		if (TheForm.Position.value.length < 1)
        ErrorMsg =  ErrorMsg + "Position" + cr;
		
		if (TheForm.Telephone.value.length < 1)
        ErrorMsg =  ErrorMsg + "Telephone" + cr;
		
		 if (TheForm.email.value.length < 1)
       ErrorMsg =  ErrorMsg + "Email" + cr;
    else if (TheForm.email.value.length < 4 || TheForm.email.value.indexOf ('@', 0) == -1 || TheForm.email.value.indexOf ('.', 0) == -1 || (TheForm.email.value.substring(1,0)=='@'))
        ErrorMsg =  ErrorMsg + "Your email is not invalid."+cr; 

if (TheForm.Comments.value.length < 1)
        ErrorMsg =  ErrorMsg + "Comments" + cr;
		
	  // if (!(TheForm.term[0].checked || TheForm.term[1].checked))
        //ErrorMsg =  ErrorMsg + "You must AgreeTerm and Condition" + cr;
		
	}
	
		if (form == 'contact'){

		 var TheForm   = document.contact;
		 
		  if (TheForm.Name.value.length < 1)
        ErrorMsg =  ErrorMsg + " Name" + cr;
		
		
		
		 if (TheForm.Email.value.length < 1)
       ErrorMsg =  ErrorMsg + " Email" + cr;
    else if (TheForm.Email.value.length < 4 || TheForm.Email.value.indexOf ('@', 0) == -1 || TheForm.Email.value.indexOf ('.', 0) == -1 || (TheForm.Email.value.substring(1,0)=='@'))
        ErrorMsg =  ErrorMsg + "Your email is not invalid."+cr; 
		
		if (TheForm.Country.value.length < 1)
        ErrorMsg =  ErrorMsg + " Country" + cr;
		
		if (TheForm.Telephone.value.length < 1)
        ErrorMsg =  ErrorMsg + " Telephone" + cr;

		if (TheForm.Message.value.length < 1)
        ErrorMsg =  ErrorMsg + " Message" + cr;
		
		if (TheForm.Code.value.length < 5)
        ErrorMsg =  ErrorMsg + " Code" + cr;
	  // if (!(TheForm.term[0].checked || TheForm.term[1].checked))
        //ErrorMsg =  ErrorMsg + "You must AgreeTerm and Condition" + cr;
		
	}
	
    if ( ErrorMsg.length > InitLen )
   {	
		alert( ErrorMsg );
        return (false);
    }
    return(true);
}



