function validateFormOnSubmit(theForm) {
var reason = "";
  if (theForm.name == "log") {
  	reason += validateLogin(theForm.login);
  	reason += validatePassword(theForm.pwd);
  }
  else if (theForm.name == "register") {
  	reason += validateLogin(theForm.login);
  	reason += validatePassword(theForm.pwd);
	reason += validatePasswordEquals(theForm.pwd,theForm.pwd2);
	reason += validateFirstname(theForm.firstname);
	reason += validateLastname(theForm.lastname);
	reason += validateEmail(theForm.email);
	reason += validateCaptcha(theForm.captcha);
  }
  else if (theForm.name == "mail") {
	reason += validateEmail(theForm.email);
  }
  else if (theForm.name == "project") {
  	reason += validateEmpty(theForm.project_name);
	reason += validateAllEmpty(theForm.list_locus,theForm.list_ko,theForm.list_ec,theForm.list_go,theForm.list_germplasm,theForm.start,theForm.end,theForm.ipr);
	if (theForm.ipr.value.length != 0) {
		 reason += validateIPR(theForm.ipr);
	}if (theForm.list_go.value.length != 0) {
		 reason += validateGO(theForm.list_go);
	}
  }
  else if (theForm.name == "keyword") {
	reason += validateEmpty(theForm.comment);
  }   
  else if (theForm.name == "sequence") {
	reason += validateInt(theForm.from);
	reason += validateInt(theForm.to);
  }
  else if (theForm.name == "form_locus_os" || theForm.name == "form_locus_at" || theForm.name == "form_locus_sb" || theForm.name == "form_locus_osi"  || theForm.name == "form_blast_at"  || theForm.name == "form_blast_os"){
	reason += validateLoc(theForm.clipboard, theForm.file);
  }
  else if (theForm.name == "form_blast_sb"  || theForm.name == "form_blast_osi"){
	reason += validateEmpty(theForm.clipboard);
  }
  
  else if (theForm.name == "form_ipr_os" || theForm.name == "form_ipr_at" || theForm.name == "form_ipr_sb" || theForm.name == "form_ipr_osi") {
	reason += validateIPR(theForm.comment);
  }   
  else if (theForm.name == "primer_designer") {
	reason += validateEmpty(theForm.fst);
  } 
  else {
  	reason += validateLogin(theForm.login);
  	reason += validatePassword(theForm.pwd);
	reason += validateFirstname(theForm.firstname);
	reason += validateLastname(theForm.lastname);
	reason += validateEmail(theForm.email);
	reason += validateCaptcha(theForm.captcha);
  }
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#F3C200'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateLoc(fld,fld1) {
    var error = "";
 
    if (fld.value.length == 0 && fld1.value.length == 0 ) {
        fld.style.background = '#F3C200'; 
        fld1.style.background = '#F3C200'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateLogin(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#F3C200'; 
        error = "You didn't enter a login.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#F3C200'; 
        error = "The login contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateFirstname(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#F3C200'; 
        error = "You didn't enter a first name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#F3C200'; 
        error = "The first name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateLastname(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#F3C200'; 
        error = "You didn't enter a last name.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#F3C200'; 
        error = "The last name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateInt(fld) {
    var error = "";
    var illegalChars = /[0-9]/; // allow letters, numbers, and underscores
 
    if (isNaN(fld.value)) {
        fld.style.background = '#F3C200'; 
        error = "Must be a number.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateProject(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#F3C200'; 
        error = "You didn't enter a name for your project.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#F3C200'; 
        error = "The name of project contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#F3C200';
        error = "You didn't enter a password.\n";
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = '#F3C200';
    } 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 = '#F3C200';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#F3C200';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#F3C200';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateCaptcha(fld) {
 var res=jcap();
var stp="";
if(jcap()){
return stp;
} else {
return res;
}

}
function validateIPR(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var filter = /^IPR\d{6}$/i;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (!filter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#F3C200';
        error = "Please entry an Interpro identifier like IPRnnnnnn.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateGO(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var filter = /^GO\:\d{7}$/i;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (!filter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#F3C200';
        error = "Please enter a GO identifier like GO:nnnnnnn.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = '#F3C200';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#F3C200';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#F3C200';
    }
    return error;
}
function validatePasswordEquals(fld,fld2) {
    var error = "";
    if(fld.value != fld2.value) {
	error = "Your passwords do not match. Please type more carefully.\n";
        fld.style.background = '#F3C200'; 
    }
    return error;
}

function validateAllEmpty(fld1,fld2,fld3,fld4,fld5,fld6,fld7,fld8) {
    var error = "";
 
    if ((fld1.value.length == 0) && (fld2.value.length == 0) && (fld3.value.length == 0) && (fld4.value.length == 0) && (fld5.value.length == 0) && (fld6.value.length == 0) && (fld7.value.length == 0) && (fld8.value == 0) ) {
        fld1.style.background = '#F3C200'; 
        fld2.style.background = '#F3C200'; 
        fld3.style.background = '#F3C200'; 
        fld4.style.background = '#F3C200'; 
        fld5.style.background = '#F3C200'; 
        fld6.style.background = '#F3C200'; 
        fld7.style.background = '#F3C200'; 
        fld8.style.background = '#F3C200'; 
        error = "All the field are empty.Please specify at least one entry\n"
    } else {
        fld1.style.background = 'White'; 
        fld2.style.background = 'White'; 
        fld3.style.background = 'White'; 
        fld4.style.background = 'White'; 
        fld5.style.background = 'White'; 
        fld6.style.background = 'White'; 
        fld7.style.background = 'White'; 
        fld8.style.background = 'White'; 
    }
    return error;  
}

function deleteRow(r) {
    var i=r.parentNode.parentNode.rowIndex;
    document.getElementById('project').deleteRow(i);
}
