// JavaScript Document


// This function controls all the others when the page Loads
//=====================================================================
function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
};
//function addEvent(elm, evType, fn, useCapture) {
//  if (elm.addEventListener) {
//    elm.addEventListener(evType, fn, useCapture);
//    return true;
//  } else if (elm.attachEvent) {
//    var r = elm.attachEvent('on' + evType, fn);
//    return r;
//  } else {
//    elm['on' + evType] = fn;
//  }
//}

function init() {
  if (!document.getElementById)
    return;
  var clock = document.getElementById('clock');
  //if(!clock.firstChild.nodeValue)
  if (!clock.innerHTML)
    return;
 
  setInterval(function() { update(clock); }, 1000);
  
 }

function update(clock) {
  var d = new Date();
  var digits, readout = '';
  var months = new Array("January","February","March","April","May","June","July",
                       "August","September","October","November","December");
  var yearNow = d.getFullYear();
  var monthNow = months[d.getMonth()];
  var dayNow = d.getDate();
  //var nowHour = d.getHours();
//  var nowMinute = d.getMinutes();
//  var nowSecond = d.getSeconds();
  var daySuffix;
  
  switch (dayNow)
{
case 1:
case 21:
case 31:
   daySuffix = "st";
   break;
case 2:
case 22:
   daySuffix = "nd";
   break;
case 3: 
case 23:
   daySuffix = "rd";
   break;
default:
   daySuffix = "th";
   break;
}
 
  readout += dayNow + daySuffix + ' ' + monthNow + ' ' + yearNow + ' ';
  
  digits = d.getHours();
  readout += (digits > 9 ? '' : '0') + digits + ':';

  digits = d.getMinutes();
  readout += (digits > 9 ? '' : '0') + digits + ':';

  digits = d.getSeconds();
  readout += (digits > 9 ? '' : '0') + digits;

  //clock.innerHTML = readout;
  clock.firstChild.nodeValue = readout;
}



// This function highlights the page for Positional Awareness 
//============================================================

function highlightPage() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("mainNav")) return false;
  var nav = document.getElementById("mainNav");
  var links = nav.getElementsByTagName("a");
  for (var i=0; i<links.length; i++) {
    var linkurl = links[i].getAttribute("href");
    var currenturl = window.location.href;
    if (currenturl.indexOf(linkurl) != -1) {
      links[i].className = "mark";
      var linktext = links[i].lastChild.nodeValue.toLowerCase();
      document.body.setAttribute("class",linktext);
    }
  }
}


function checkforms()
{
  var submit = document.getElementById("pack");
  submit.onclick = validateFields;
  return true;
  
}

function validateFields()  {

  var firstName = document.getElementById("author");
  var lastName = document.getElementById("lastname");
  var Email = document.getElementById("email");
  var Comments =document.getElementById("text");

  if (firstName.value == "" || /^\s+$/.test(firstName.value))
  {
    alert("Please enter a valid Firstname");
	firstName.focus();
	firstName.select();
	return false;
  }
  
  else if (lastName.value == "" || /^\s+$/.test(lastName.value))
  {
    alert("Please enter a valid Lastname");
	lastName.focus();
	lastName.select();
	return false;
  }
   
  else if (Email.value == "" || !/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/.test(Email.value))
  {
    alert("Please enter a valid email address");
	Email.focus();
	Email.select();
	return false;
  }
  
  else if (Comments.value == "" || /^\s+$/.test(Comments.value))
  {
    alert("Please enter Your Comments");
	Comments.focus();
	Comments.select();
	return false;
  }
  
  return true;

}

addLoadListener(init);
addLoadListener(highlightPage);
addLoadListener(checkforms);


