// JavaScript Document

//----------------------------------------------------------
//    	 ----     EMAIL VALIDATION FUNCTION     ----
//----------------------------------------------------------

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("Please enter your email address CORRECTLY")
		   return false
		}
		
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter your email address CORRECTLY")
		   return false
		}
		
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please enter your email address CORRECTLY")
		    return false
		}

		if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please enter your email address CORRECTLY")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please enter your email address CORRECTLY")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please enter your email address CORRECTLY")
		    return false
		 }

		 if (str.indexOf(" ")!=-1){
		    alert("Please enter your email address CORRECTLY")
		    return false
		 }
 		 return true					
	}

//----------------------------------------------------------

//    	       END EMAIL VALIDATION FUNCTION

//----------------------------------------------------------



//----------------------------------------------------------

//	BELOW DEALS WITH PHONE VALIDATION AND POSTCODE VALIDATION

//----------------------------------------------------------

// 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 = 8;
// Minimum no of digits for post code.

var maxDigitsInIphoneNumber = 11;
// Maximum no of digits for post code.

var minDigitsInPostCode = 4;


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 (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strphone){
s=stripCharsInBag(strphone,validWorldphoneChars);
return (isInteger(s) && s.length >= minDigitsInIphoneNumber);
}

function checkInternationalPhoneMax(strphone){
s=stripCharsInBag(strphone,validWorldphoneChars);
return (isInteger(s) && s.length <= maxDigitsInIphoneNumber);
}

function checkPostCode(strpostcode){
s=stripCharsInBag(strpostcode,validWorldphoneChars);
return (isInteger(s) && s.length >= minDigitsInPostCode);
}

function ValidateForm(){
	var phone=document.contact_form.phone_field

	if ((phone.value==null)||(phone.value=="")){
		alert("Please enter your phone Number")
		phone.focus()
		return false
	}

	if (checkInternationalphone(phone.value)==false){
		alert("Please enter a Valid phone Number")
		phone.value=""
		phone.focus()
		return false
	}

	return true

 }

//----------------------------------------------------------

//    	       END PHONE/POSTCODE VALIDATION

//----------------------------------------------------------





//----------------------------------------------------------

//           VALIDATE FORM FUNCTIONS COMPLETE

//----------------------------------------------------------



function validate_form ( )
{
	valid = true;
		//Checks for valid first name
        if ( document.contact_form.name_field.value == "" )
        {
                alert ( "Please fill in your Name." );
				document.contact_form.name_field.focus();
                return valid = false;
        }

		//Checks to see if valid phone number exists
		var phone=document.contact_form.phone_field
		
		if ((phone.value!=""))
		{
		  if (checkInternationalPhone(phone.value)==false)
		  {
			alert("Please enter a valid 8 digit phone Number")
			phone.value=""
			phone.focus()
			return false
		  }
		  if (checkInternationalPhoneMax(phone.value)==false)
		  {
			alert("Phone Number cannot exceed 10 digits")
			phone.value=""
			phone.focus()
			return false
		  }
		}

		//Checks to see if email is correct and exist
		var emailID=document.contact_form.email_field
		if ((emailID.value==null)||(emailID.value=="")){
			alert("Please enter your email address.")
			emailID.focus()
			return false
		}
		if (echeck(emailID.value)==false){
			emailID.value=""
			emailID.focus()
			return false
		}

		//Checks to see if message exists
		if ( document.contact_form.message_field.value == "" )
        {
                alert ( "You haven't typed a message!" );
				document.contact_form.message.focus();
                return valid = false;
        }
		
        return valid;
		
}

//----------------------------------------------------------

//                   END VALIDATE FORM

//----------------------------------------------------------
