// Sourced from http://www.smartwebby.com/DHTML/email_validation.asp
function checkEmail(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;					
}

// Clean code to avoid getElementById
function gebid(field){ return document.getElementById(field); }

// Get the Selected value from a select box
function getSValue(id){
  var sel = gebid(id);
return sel.options[sel.selectedIndex].value;
}

// Global variables which are pulled in with variable variables into checkForm1
var commute_free = ["name","email","address","current","regular"];
var contact = ["name","email","message"];
var coach = ["name","addr1","town","county","tel","postcode","date","no","start","end","stime","etime","ferry","stay","finish"];
var brochure = ["name","addr1","town","county","postcode","tel"];
var callback = ["name","tel","trip","when","between"];
var special = ["name","addr1","town","county","postcode","mob"];
var schools = ["name","yschool","bus","mobile","email"];

// Actual generic form checking script
function checkForm1(arr_to_check){

  var field = window[arr_to_check]; // Reverse of what a site said about variable variables

  for (i=0; i < field.length; i++){
    // Make the type text just in case there's confusion!
    var itype = gebid(field[i]).type.toString();
    
    // Is the length 0?
    if (itype == "text" || itype == "textarea"){
      if ((gebid(field[i]).value.length)===0){
        alert('All fields marked with a * are required');
        return false;
      }  
    }

    // Is it a Select box? Needs selecting! - leave first Value blank
    if(itype == "select" || itype == "select-one"){      
      if (getSValue(field[i]).length == 0 || getSValue(field[i])=="Select"){
          alert('Please choose from the drop down boxes');
          return false;
      }
    }
    
    // Is the email valid?
    if (field[i]=="email"){  
      if (!(checkEmail(gebid(field[i]).value))){
        alert('Valid email required');
        return false;
      }
    }
  }
}