﻿var gHours;
var gMinutes;
var gSeconds;
var gMeridiem;
var gDayOfWeek;

function startClock( initHours, initMinutes, initSeconds, initMeridiem, initDayOfWeek )
{
   gHours = initHours;
   gMinutes = initMinutes;
   gSeconds = initSeconds;
   gMeridiem = initMeridiem;
   gDayOfWeek = initDayOfWeek;

   document.getElementById("clockLabel").innerHTML = 'ABR Local Time:';
   updateClock();
   intervalID = self.setInterval( "updateTime()", 1000);
}

function updateClock()
{
    clockString = gHours + ":" + padDigits(gMinutes);
    clockString = clockString + ":" + padDigits(gSeconds) + " " + gMeridiem;
    clockString = clockString + " " + gDayOfWeek;
    document.getElementById("clock").innerHTML = clockString;
}// end update Clock

function padDigits( digits )
{
   strDigits = digits.toString();
   if( strDigits.length < 2 )
   {
      strDigits = "0" + strDigits;
   }// end if

   return strDigits;

}// end padDigits

function incrementDay()
{
   switch(gDayOfWeek)
   {
      case "Sun":
         gDayOfWeek = "Mon";
         break;
      case "Mon":
         gDayOfWeek = "Tue";
         break;
      case "Tue":
         gDayOfWeek = "Wed";
         break;
      case "Wed":
         gDayOfWeek = "Thu";
         break;
      case "Thu":
         gDayOfWeek = "Fri";
         break;
      case "Fri":
         gDayOfWeek = "Sat";
         break;
      case "Sat":
         gDayOfWeek = "Sun";
         break;
   }// end switch
}// end incrementDay

function updateTime()
{
   var clockString;

   if( gSeconds > 59 )
   {
      gSeconds = 0;
      ++gMinutes;
   }// end if

   if( gMinutes > 59 )
   {
      gMinutes = 0;
      ++gHours;
   }// end if

   if( gHours == 12 && gMinutes == 0 && gSeconds == 0 )
   {
      if( gMeridiem == "AM" )
      {
         gMeridiem = "PM";
      }// end if
      else
      {
         gMeridiem = "AM";
         incrementDay();
      }// end else
   }// end if

   if( gHours > 12 )
   {
      gHours = 1;
   }// end if

   updateClock();

   ++gSeconds;

}// end updateClock

function selectSearchBar() {
    document.getElementById('q').select();
}// end clearSearchBar

function restoreSearchBar() {
    var searchValue = "";
    searchValue = document.getElementById('q').value;
    if (trim(searchValue) == "") {
        document.getElementById('q').value = "Search our site...";
    }// end if
}// end restoreSearchBar

function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}//end ltrim

function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}//end rtrim

function trim(str) {
	return ltrim(rtrim(str));
}

function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

