// JavaScript Document
var correct1 = "not okay";
function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateName(theForm.name);
  reason += validateEmail(theForm.email);
  reason += validateQuestion(theForm.question);
  reason += validateCode(theForm.code);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateName(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a name.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 150)) {
        fld.style.background = 'Yellow'; 
        error = "The name is the wrong length.\n";
    } else if (fld.value == "Enter your name here") {
        fld.style.background = 'Yellow'; 
       	error = "You didn't enter a name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The name contains illegal characters.\n";
	} else {
        fld.style.background = 'White';
    } 
    return error;
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateQuestion(fld) {
    var error = "";
  
    if ((fld.value.length == 0)||(fld.value == "Please type your question in this area.")) {
        fld.style.background = 'Yellow'; 
        error = "The question field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateCode(fld) {
    var error = "";
  	var test = check_field(fld);
    if ((fld.value.length == 0) || (fld.value.length < 5)) {
        fld.style.background = 'Yellow'; 
        error = "The code field has not been filled in or is too short.\n";
	} else if(test == "False"){
		fld.style.background = 'Yellow'; 
        error = "The code field was entered incorrectly.\n";
	} else {
        fld.style.background = 'White';
    }
    return error;  
}
function again(){
	document.icaptcha.SetVariable("again","OK");
}
function checking(){
	document.icaptcha.SetVariable("code",document.coach.code.value);
}
function check_field(fld){
	return _call('valid.php?form='+fld.value);	
}
function _activeObject(){
	if(window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		return new XMLHttpRequest();
	else
		return null;
}
function _call(page){
	var activeObject = _activeObject();
	activeObject.open("GET", page, false);
	activeObject.send(null);
	return activeObject.responseText;
}
