
function checkform(of)
{ //testing if DOM is available and that there is a field with the ID requiredFields
  if(!document.getElementById || !document.createTextNode){return;}
  if(!document.getElementById('requiredFields')){return;}
//We continue by defining all the variables used in the error display and by splitting the required field IDs into an array.

  var errorID='errormsg';
  var errorClass='error'
  var errorMsg='Please enter or change the fields marked with a ';
  var errorImg='images/systemimages/alert.gif';
  var errorAlt='Error';
  var errorTitle='This field has an error!';
  var reqfields=document.getElementById('requiredFields').value.split(',');
	//// Cleanup old mess
  // if there is an old errormessage field, delete it
  if(document.getElementById(errorID))
  {
    var em=document.getElementById(errorID);
    em.parentNode.removeChild(em);
  }
  // remove old images and classes from the required fields
  for(var i=0;i<reqfields.length;i++)
  {
    var f=document.getElementById(reqfields[i]);
    if(!f){continue;}
    if(f.previousSibling && /img/i.test(f.previousSibling.nodeName))
    {
      f.parentNode.removeChild(f.previousSibling);
    }
    f.className='';
  }
  
  //now test to make sure the fields exist to prevent error messages
// loop over required fields
  for(var i=0;i<reqfields.length;i++)
  {
// check if required field is there
    var f=document.getElementById(reqfields[i]);
	var isEmailId = f.id
	var isEmail = isEmailId.indexOf("email") ;
	if (isEmail = -1){isEmail = isEmailId.indexOf("Email")}
    if(!f){continue;
	}
//We then check each field according to its type. 
//For textareas and text fields we need to check the value, 
//for checkboxes we need to check for the checked attribute and 
//for select boxes if there is a selectedIndex defined and that it is bigger than 0.
// test if the required field has an error, 
// according to its type
    switch(f.type.toLowerCase())
	
    { 
      case 'text':
        if(f.value=='' && isEmail == -1){cf_adderr(f)}  //needs to be modified to check for email in the name            
// email is a special field and needs checking
        if((isEmail != -1) && ////needs to be modified to check for email in the name
        !cf_isEmailAddr(f.value)){cf_adderr(f)}              
      break;
      case 'textarea':
        if(f.value==''){cf_adderr(f)}              
      break;
      case 'checkbox':
        if(!f.checked){cf_adderr(f)}              
      break;
      case 'select-one':
        if(!f.selectedIndex && f.selectedIndex==0){cf_adderr(f)}              
      break;
    }
  }
//If any of the tests above trigger an error report, the cf_adderr() generates the error message (a DIV with the errorid as ID). 
//Therefore we return to the sending process of the form only when this element is not existant.  	  
	return !document.getElementById(errorID);

/* Tool methods used above*/
function cf_adderr(o)
  {

//We create the image, set its alternative text and title and insert it before the element. 
//We apply the CSS-class stored in errorClass to the element to colour it.

    // create image, add to and colourise the error fields
    var errorIndicator=document.createElement('img');
    errorIndicator.alt=errorAlt;
    errorIndicator.src=errorImg;
    errorIndicator.title=errorTitle;
    o.className=errorClass;
    o.parentNode.insertBefore(errorIndicator,o);

//Then we check if there is already an error message and create it if necessary. 
//Once we have created this element, this condition will not be executed again.

  // Check if there is no error message
    if(!document.getElementById(errorID))
    {
    // create errormessage and insert before submit button
      var em=document.createElement('div');
      em.id=errorID;
      var newp=document.createElement('p');
      newp.appendChild(document.createTextNode(errorMsg))
      // clone and insert the error image
      newp.appendChild(errorIndicator.cloneNode(true));
      em.appendChild(newp);

//We find the submit button (by checking the type of each input element) 
//and insert the new message before its parent element (the paragraph the submit button resides in).

      // find the submit button 
      for(var i=0;i<of.getElementsByTagName('input').length;i++)
      {
        if(/submit/i.test(of.getElementsByTagName('input')[i].type))
        {
          var sb=of.getElementsByTagName('input')[i];
          break;
        }
      }
      if(sb)
      {
        sb.parentNode.insertBefore(em,sb);
      }  
    } 
  }

//Finally, we need the method to test if the submitted email is in a valid format:

  function cf_isEmailAddr(str) 
  {
      return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
  }
}

function CheckEmailAddress(emailvar){
   var emailparts = emailvar.split("@");
   var emailnameonly = "";
   var minimumextlength = 2;
   var maximumextlength = 4;
   var urltestchar = "";
   var charok = 0;
   var acceptable = new Array("A","B","C","D","E","F","G","H","I","J",
    "K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
    "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",
    "q","r","s","t","u","v","w","x","y","z","-",
    ".","1","2","3","4","5","6","7","8","9","0","@","_");
   
  
   // check to see if there are any @'s in the string
   
   if (emailparts.length > 2) {
    return false;
   }
   if (emailparts.length <= 1) {
    return false;
   }
   
   // There is one @ - now check for a dot
   // and data before and after the @
   
   var emailurl=(emailparts[emailparts.length - 1]);
   for (var i=0; i < emailparts.length-1; i++) {
    emailnameonly=emailnameonly+emailparts[i];
   }
   
   // Examine emailurl for a dot and data in front of it
   // and two or three characters after the last dot
   
   var urlparts = emailurl.split(".");
   
   // check to see if there are any dot's in the string
   // more than one is ok
   
   if (urlparts.length <= 1) {
    return false;
   }
   
   // Check to see how long the last urlpart is
   // Must be either two or three characters long
   
   if (urlparts[urlparts.length - 1].length < minimumextlength || urlparts[urlparts.length-1].length > maximumextlength) {
    return false;
   }
   
   // Check all characters of the address to make sure there are no
   // invalid characters - default allows the use of an underscore
   // although that is technically not allowed in a domain name
   // it's not that big a risk as it is allowed in the email name.
   
   for (i=0; i < emailvar.length-1; i++) {
    urltestchar = emailvar.substr(i,1);
    for (var ii=0; ii < acceptable.length; ii++) {
     if (urltestchar == acceptable[ii]) {
      charok = 1;
     }
    }
    if (charok == 0) {
     return false;
    }
    charok = 0;
   }
   
   // check to make sure the length of the string prior to the
   // extension is greater than minimumextlength
   
   if (emailurl.length-urlparts[urlparts.length - 1].length-1 < minimumextlength) {
    return false;
   }
   
   // check to make sure the length of the string prior to the
   // domain is greater than minimumextlength
   
   if (emailnameonly.length < minimumextlength) {
    return false;
   }
  
   return true;
  }
