// JScript source code
    var f;
	var theResult;
	var errorStyle = "1px solid #FF0000";
	var normStyle = "1px solid #A5ACB2";

    function validate()
	{
		f = document.forms.Form1
		theResult=true;
				
		testProduct();
		if (theResult != true) {
		    return false
		}
		
	    testFirstName();
	    testSurname();
	    testEmail();
	    testAddress();
	    testPostcode();
	    testPhone();
	    // check email matches confirmation email
	    testEmailMatch();
	    if (theResult == false){
	        alert("please fill out the fields highlighted in red");
	        return theResult;
	    }
		
	    if (theResult== false){
	        return theResult;
        }
        // setup the worldpay parameters
        formatData();
        
       	}
	
	function isBlank(formRef,highlightIfBlank){
	    if (formRef.value.length < 1)
	    {
	        if(highlightIfBlank){
	            formRef.style.border = errorStyle;	
	        }	
		    return true;
	    }
	    return false;
	}
    function testProduct() {

        var myOption = -1;
        var i = 0;
        for (i=f.product.length-1; i > -1; i--) {
            if (f.product[i].checked) {
                myOption = i; i = -1;
            }
        }
        
        if (myOption == -1) {
            alert("please select a course");
		    //f.productdiv.style.border = errorStyle;		
		    theResult = false;
        }

	    return theResult;
    }	
  
    function testFirstName() {

	    f.MC_firstname.style.border = normStyle;		
        	
	    if (f.MC_firstname.value.length < 1)
	    {
		    f.MC_firstname.style.border = errorStyle;		
		    theResult = false;
	    }
	    return theResult;
    }

    function testSurname(){
	    f.MC_surname.style.border = normStyle;		

	    if (f.MC_surname.value.length < 1)
	    {
		    f.MC_surname.style.border = errorStyle;		
		    theResult = false;
	    }
	    return theResult;
    }
    
    function testEmail(){
        f.MC_email.style.border = normStyle;
        f.MC_email2.style.border = normStyle;	
        
        f.MC_email.value = trimString(f.MC_email.value)
        f.MC_email2.value = trimString(f.MC_email2.value)
        
	    if (f.MC_email.value.length < 1)
	    {
		    f.MC_email.style.border = errorStyle;
		   	
		    theResult = false;
	    }else if(!eCheck(f.MC_email.value)){
	        f.MC_email.style.border = errorStyle;
	       
	        alert("The email address you entered doesn't appear to be valid");
	        theResult = false;
	    }
    	
	    return theResult;
    }
    
    function testEmailMatch(){
			
        if ((f.MC_email2.value.length < 1) && testEmail() ){
            alert("Please confirm your email address");
            f.MC_email2.style.border = errorStyle;
            theResult = false;
        }else{
	        if (f.MC_email.value != f.MC_email2.value )
	        {
		        alert("The email addresses you entered don't match");
		        f.MC_email.style.border = errorStyle;		
		        f.MC_email2.style.border = errorStyle;		
		        theResult = false;
	        }
	    }
	    return theResult;
    }

    function testAddress(){
        f.MC_address1.style.border = normStyle;
        f.MC_town.style.border = normStyle;		
        f.MC_county.style.border = normStyle;	 		
        
	    if (f.MC_address1.value.length < 1)
	    {
		    f.MC_address1.style.border = errorStyle;		
		    theResult = false;
	    }
	    if (f.MC_town.value.length < 1)
	    {
		    f.MC_town.style.border = errorStyle;		
		    theResult = false;
	    }
    	
	    if (f.MC_county.value.length < 1)
	    {
		    f.MC_county.style.border = errorStyle;		
		    theResult = false;
	    }
	    return theResult;
    }
    
    function testPostcode(){
        f.MC_postcode.style.border = normStyle;		
       
	    if (f.MC_postcode.value.length < 1)
	    {
		    f.MC_postcode.style.border = errorStyle;		
		    theResult = false;
	    }
	    return theResult;
    }
    
    function testPhone(){
        f.MC_tel.style.border = normStyle;		
         
	    if (f.MC_tel.value.length < 1)
	    {
		    f.MC_tel.style.border = errorStyle;	
		    f.tel.value = f.MC_tel.value	
		    theResult = false;
	    }
	    f.tel.value = f.MC_tel.value
	    return theResult;
    }

    function formatData() {
        f.name.value = f.MC_firstname.value + " " + f.MC_surname.value;
        f.address.value = f.MC_address1.value + "&#10;";
        f.address.value = f.address.value + f.MC_address2.value + "&#10;";
        f.address.value = f.address.value + f.MC_town.value + "&#10;";
        f.address.value = f.address.value + f.MC_county.value + "&#10;";
        f.postcode.value = f.MC_postcode.value;
        f.email.value = f.MC_email.value;
    }
    
  
    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){
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   return false
		}
		if (str.indexOf(at,(lat+1))!=-1){
		   return false
		}
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   return false
		}
		if (str.indexOf(dot,(lat+2))==-1){
		   return false
		}
		if (str.indexOf(" ")!=-1){
		   return false
		}
 		return true					
	}

function trimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

