// validate that a field contains a date in the format 12 sep 1961
function vldDate(n,desc)
{ if (n.match(/^\s*$/))
  {  return true;
  };
  ex = desc + " - ";
  if (!n.toLowerCase().match(/^\s*\d{1,2}\s+(jan|january|feb|february|mar|march|apr|april|may|jun|june|jul|july|aug|august|sep|september|oct|october|nov|november|dec|december)\s+\d{4}\s*$/))
  { em+=ex+"wrong format, should be e.g. 14 Sep 1968"; return false;
  }
  re=/^\s*(\d{1,2})\s+(\w{3})\w*\s+(\d{4})/;
  dd=n.replace(re,"$1");
  mmm=n.toLowerCase().replace(re,"$2");
  yyyy=n.replace(re,"$3");
  if ((dd > 31) || (dd < 1))
  { em+=ex+"the day is incorrect"; return false;
  }
  switch (mmm)
  { case 'feb' :
      if ( (dd > 29) ||((dd > 28) && (yyyy%4!=0) || ((yyyy%100==0) && (yyyy%400!=0))) )
      { em+=ex+"the day is incorrect"; return false;
      }
      break;
    case 'apr':
    case 'jun':
    case 'sep':
    case 'nov':
      if (dd > 30)
      { em+=ex+"the day is incorrect"; return false;
      }
      break;
   };
  if ( (yyyy < 1900) || (yyyy > 2010))
  { em+=ex+"the year is incorrect"; return false;
  }
  return true;
}

// validate that a field is not empty
function vldNotEmpty(n,desc)
{ if(!n.match(/\S+/))
  { em+="You need to enter details in the "+desc+" field"; return false;
  }
  else return true;
}

// validate that the email address is valid
function vldEmail(n)
{ var emailFilter=/^.+@.+\..{2,3}$/;
  var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
  if (!(emailFilter.test(n)))
     { em+="You need to enter a valid email address."; return false;
     }
  else if (n.match(illegalChars))
     { em+="The email address contains illegal characters."; return false;
     }
  else return true;
}

// validate that field is not too large
function vldMaxLen(n,max,desc)
{ if (n.length > max)
  { em+="The "+desc+" field is too large (limit is "+max+" chars)"; return false;
  }
  else return true;
}

var em = "";
function verify(f)
{ result = true; em = "Errors in form\n\n";
  if (!vldNotEmpty(f.title.value,"Subject title")) { result=false; em += "\n";};
  if (!vldNotEmpty(f.sname.value,"Your name")) { result=false; em += "\n";};
  if (!vldEmail(f.semail.value)) { result=false; em += "\n";};
  if (!vldNotEmpty(f.ptext.value,"Message")) { result=false; em += "\n";};
  if (!vldMaxLen(f.ptext.value,4500,"Message")) { result=false; em += "\n";};
  if (!result) { alert(em);};
  return result;
}
