/**********************************************************************************
  File name:  tlat.js

  Version History:
      Author         Date          Version   Comment
      John Marshall  27/11/2003    1.0       Released
      John Marshall  05/12/2003    2.0       Added form validation functions
		John Marshall  05/12/2003    3.0       Added feedback form validation

**********************************************************************************/

function setFocus(){document.simplesearch.q.focus();}

function setAdvFocus(){document.advancedsearch.wo.focus();}

function setFeedbackFocus(){document.feedback.name.focus();}

function validNumber(val, min, max){
	if (isNaN(val)) return false;
	if (max && val > max) return false;
	if (min && val < min) return false;
	return true;
}

function trimSpaces(sSource) { 
  // Remove leading spaces 
  while (sSource.substring(0,1) == ' ') 
    sSource = sSource.substring(1, sSource.length);

    // Remove trailing spaces 
    while (sSource.substring(sSource.length-1,sSource.length) == ' ')
        sSource = sSource.substring(0, sSource.length-1);

   return sSource;
} 

function validateForm(frm) {
  var year = 0;
  var previousyear = 0;
  
  // Validate the simple search form.
  // If the q field does not exist it is assumed 
  // the form is the advanced search form.
  if (frm.q) {
    if (trimSpaces(frm.q.value) == "") {
      alert("Your search was empty.");
      frm.q.focus();
      return false;
	 } else {
		return true;
	 }
  }
  
  // Validate the advanced search form.
  if ((trimSpaces(frm.wo.value) == "") && (trimSpaces(frm.wa.value) == "") && (trimSpaces(frm.we.value) == "") && (trimSpaces(frm.ev.value) == "") && (trimSpaces(frm.yrs.value) == "") && (trimSpaces(frm.yre.value) == "")) {
    alert("Your search was empty. Please complete one or more of the form fields.");
    frm.wo.focus();
    return false;
  }
  
  var now = new Date();
	 
  if (frm.yrs.value != "") {		 
    year = Math.floor(frm.yrs.value - 0);	 
    if (!validNumber(year, 1600, now.getFullYear())) {
      alert('Invalid year entered. Year must be between 1600 and ' +  now.getFullYear());
      frm.yrs.focus();
	   return false;
	 }	 
  }
  
  if (frm.yre.value != "") {		 
    if (year == 0) {
	   previousyear = 1600;
	 } else {
	   previousyear = year;
	 }
    year = Math.floor(frm.yre.value - 0);	
    if (!validNumber(year, previousyear, now.getFullYear())) {
      alert('Invalid year entered. Year must be between ' + previousyear + ' and ' +  now.getFullYear());
      frm.yre.focus();
      return false;
	 }
  }
  
  // Only return true when we are satisified the form has valid data
  return true;
}

function validateFeedbackForm(frm) {
	return true;
}