/*
  Author: Eoin O'Connor, Recreational Angling Ireland, 2004
*/

// This discovers what browser the user is viewing the page with.
var IE4 = (document.all && !document.getElementById) ? true : false;
var N4 = (document.layers) ? true : false;
var IE5 = (document.all && document.getElementById) ? true : false;
var N6 = (document.getElementById && !document.all) ? true : false;

/*
  This changes the font colour of a link with an id of a_name and the
  background colour of a cell with an id of td_name.
*/
function coloron(a_name,td_name)
{
  var link;

  font_color = '#FFFFFF';
  bg_color = '#3399FF';

  if(IE4)
  {
    document.all(a_name).style.color=font_color;
    document.all(td_name).style.background=bg_color;
  }
  else if(IE5)
  {
    link = window.event.srcElement;

    link.menu = document.getElementById(a_name);
    link.menu.style.color=font_color;

    link.menu = document.getElementById(td_name);
    link.menu.style.background=bg_color;
  }
  else if(N6)
  {
    document.getElementById(a_name).style.color=font_color;
    document.getElementById(td_name).style.background=bg_color;
  }
}

/*
  This changes the font colour of a link with an id of a_name and the
  background colour of a cell with an id of td_name.
*/
function coloroff(a_name,td_name)
{
  var link;

  font_color = '#000000';
  bg_color = '#FFFFFF';

  if(IE4)
  {
    document.all(a_name).style.color=font_color;
    document.all(td_name).style.background=bg_color;
  }
  else if(IE5)
  {
    link = window.event.srcElement;

    link.menu = document.getElementById(a_name);
    link.menu.style.color=font_color;

    link.menu = document.getElementById(td_name);
    link.menu.style.background=bg_color;
  }
  else if(N6)
  {
    document.getElementById(a_name).style.color=font_color;
    document.getElementById(td_name).style.background=bg_color;
  }
}

// This sends the browser to the location passed to the function as a parameter.
function gotoloc(loc)
{
  if(IE4)
    document.location=loc;
  else if(IE5)
    document.location=loc;
  else if(N6)
    document.location=loc;
}

// This constructs an e-mail with name@address in the To field.
function contact(name,address)
{
  mailto = 'mailto:' + name + '@' + address;
  document.location = mailto;
}

// This prints a nicely formatted version of the date on which the page was last modified.
function print_date()
{
  var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  var endings = new Array("st","nd","rd","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","st","th","rd","th","th","th","th","th","th","th","st");

  var d = new Date(document.lastModified);
  document.write(d.getDate());
  document.write(endings[d.getDate()-1]);
  document.write(" ");
  document.write(months[d.getMonth()]);
  document.write(" ");
  document.write(d.getFullYear());
}

// This prints the current year.
function print_year()
{
  var d = new Date();
  document.write(d.getFullYear());
}

// This returns true if the fields on the contact us form are valid
function validateContactUsForm()
{
  var frm = document.form;
  var messageStr = "";

  messageStr = messageStr + checkString(frm.name,"Name");
  messageStr = messageStr + checkEmail(frm.email,"Email Address",true);
  messageStr = messageStr + checkString(frm.subject,"Subject");
  messageStr = messageStr + checkString(frm.message,"Message");
  messageStr = messageStr + checkString(frm.code,"Security Code");

  if(messageStr.length>0)
  {
    alert(messageStr);
    return false;
  }
  else
    return true;
}

// Checks if a field has the right length of characters
function checkString(item,field,min,max)
{
  var length = trim(item.value).length;
  if(min == null) 
    min = 1;

  if(length < min && min == 1 && max == null)
    return (field + " is required.\n");
  else if(length < min && max == null)
    return (field + " must be at least " + min + " characters long.\n");
  else if(length < min || (max != null && length > max))
    return (field + " must be between " + min + " and " + max + " characters long.\n");
  return "";
}

// Trim a string and returns it
function trim(str)
{
  var i = 0, j = str.length - 1;
  while(i < j && str.charAt(i) == ' ')
    i++;
  while(j > i && str.charAt(j) == ' ')
    j--;
  return str.substring(i, j + 1);
}

// Checks for a valid email
function checkEmail(item,field,required)
{
  var email = trim(item.value);

  if(required && email == "")
    return (field + " is required.\n");
  else if(!required && email == "")
    return "";

  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if(!reg1.test(email) && reg2.test(email))  // if syntax is valid  
    return "";

  return (field + " requires a valid email address.\n");
}