// ********************************************************************************************
// *********  common.js
// *********
// *********  Common javascript functions that are used by most of the web pages
// *********
// *********
// ********************************************************************************************

function formatDate()
//*************************************************************************************************
//**********                                                                           ************
//**********       Format a date in day, month, year format e.g. 27th September, 2009  ************
//**********                                                                           ************
//*************************************************************************************************
{
    var mydate=new Date() ;                 // Create a current date object
    var wkday = mydate.getDay() + 1 ;       // The day of the week.  The count starts at zero, so I am adding 1 to get it to the right number (Sunday = 1 etc..)
    var day   = mydate.getDate() ;          // The day of the month. ( Does not need 1 adding to it. )
    var month = mydate.getMonth() + 1 ;     // The mth count starts at zero, so I am adding 1 to get it to the right number (January = 1 etc.)
    var year  = mydate.getYear() ;          // Extract the year
    if (year < 1000) year+=1900 ;           // Correct the year for the right century
    var daym ;                              //  Variable in which we will add a suffix to the day of the month e.g. 31st, 4th, 2nd, 23rd etc.
    switch(day)
    {
      case 1:
      case 21:
      case 31:
        daym = day + "st"
        break;
      case 2:
      case 22:
        daym = day + "nd"
        break;
      case 3:
      case 23:
        daym = day + "rd"
        break;
      default:
        daym = day + "th"
        break;
    }
//  Create an array of week day names
    var dayarray   = new Array("No Day","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday") ;
//  Create an array of month names
    var montharray = new Array("No Month","January","February","March","April","May","June","July","August","September","October","November","December") ;
//  Format the date string for use on the web site banner
    var dateStr = dayarray[wkday] + ", " + montharray[month] + " " + daym + ", " + year  ;
//*************************************************************************************************
//  The code below overrides the normal date output with special dates in the calendar
//*************************************************************************************************
//    alert("Day : " + day + " Month : " + month) ;
//    if((day >= 4 && day <= 6)  && month==11 ) dateStr = "the Open Championship weekend, "  + year ;   // Just used for testing
//  Patron Saints Days
    if(day==1  && month==3 ) dateStr = "St David's Day, "    + year ;
    if(day==17 && month==3 ) dateStr = "St Patrick's Day, "  + year ;
    if(day==23 && month==4 ) dateStr = "St George's Day, "   + year ;
    if(day==30 && month==11) dateStr = "St Andrew's Day, "   + year ;
//  Fixed Yearly Events
    if(day==1  && month==1 ) dateStr = "New Years Day, " + year ;
    if(day==5  && month==1 ) dateStr = "the 12th night of Christmas, " + year ;
    if(day==14 && month==2)  dateStr = "St Valentines Day, " + year ;
    if(day==1  && month==4)  dateStr = "April Fools Day, " + year ;
    if(day==15 && month==9)  dateStr = "the Battle of Britain Day, " + year ;
    if(day==21 && month==10) dateStr = "Trafalgar Day, " + year ;
    if(day==31 && month==10) dateStr = "Halloween, " + year ;
    if(day==5  && month==11) dateStr = "Bonfire Night, " + year ;
    if(day==11 && month==11) dateStr = "Armistice Day, " + year ;
    if(day==24 && month==12) dateStr = "Christmas Eve, " + year ;
    if(day==25 && month==12) dateStr = "Christmas Day, " + year ;
    if(day==25 && month==12) dateStr = "Boxing Day, " + year ;
    if(day==31 && month==12) dateStr = "New Years Eve, " + year ;
//  Variable Major Lytham Green Drive Golf Club Events
//  Variable Yearly Events
    if(day==16 && month==2 && year==2010) dateStr = "Pancake Tuesday, " + year ;
    if(day==17 && month==2 && year==2010) dateStr = "Ash Wednesday, " + year ;
    if(day==2 && month==4 && year==2010) dateStr = "Good Friday, " + year ;
    if((day >=3  && day <=4) && month==4  && year==2010) dateStr = "Easter weekend, " + year ;
    if(day==5 && month==4 && year==2010) dateStr = "Easter Monday, " + year ;
//  Major Golf Event dates
    if((day >=8  && day <=11) && month==4  && year==2010) dateStr = "the Masters weekend, " + year ;
    if((day >=17 && day <=20) && month==6  && year==2010) dateStr = "the US Open weekend, " + year ;
    if((day >=15 && day <=18) && month==7  && year==2010) dateStr = "the Open Championship weekend, " + year ;
    if((day >=12 && day <=15) && month==8  && year==2010) dateStr = "the PGA Championship wekend, " + year ;
    if((day >=1  && day <=3 ) && month==10 && year==2010) dateStr = "the Ryder Cup weekend, " + year ;
    return dateStr ;
}
function checkKeyHitIsEnter(e)
//*************************************************************************************************
//**********                                                                           ************
//**********         Check it was the enter key that was pressed                       ************
//**********                                                                           ************
//*************************************************************************************************
{
    var keyPressed;
    //Browser compatibility check
    if (document.all)
    {
        //  Browser used: Internet Explorer 6
        keyPressed = e.keyCode;
//        alert('handleKeystroke: IE property: keyCode');
    }
    else
    {
        //Browser used: Firefox
        keyPressed = e.which;
//        alert('handleKeystroke: FF property: which');
    }
    // 13 = ASCII code for Enter key
    if(keyPressed == 13)
    {
      return true ;
    }
    return ;
}

// #########################################################################################################################
var SponsorPic = new Array() ; // don't touch this
// to add more images, just continue
// the pattern, adding to the array below
SponsorPic[0] = 'img/sponsors/logocouldbehere.gif' ;
SponsorPic[1] = 'img/sponsors/GlynJones.gif' ;
SponsorPic[2] = 'img/sponsors/CliftonAcademy.jpg';
SponsorPic[3] = 'img/sponsors/Alliance.gif' ;
SponsorPic[4] = 'img/sponsors/StJamesPlace.gif' ;
SponsorPic[5] = 'img/sponsors/logocouldbehere.gif' ;
SponsorPic[6] = 'img/sponsors/Rainbow.gif' ;
SponsorPic[7] = 'img/sponsors/logocouldbehere.gif' ;
SponsorPic[8] = 'img/sponsors/WRT.gif' ;
SponsorPic[9] = 'img/sponsors/RibblesdaleAutos.gif' ;
SponsorPic[10] = 'img/sponsors/PCMG.gif' ;
SponsorPic[11] = 'img/sponsors/Glaric.gif' ;
SponsorPic[12] = 'img/sponsors/TransContinental.gif' ;
SponsorPic[13] = 'img/sponsors/Classic.gif' ;
SponsorPic[14] = 'img/sponsors/StAnnesSports.gif' ;
SponsorPic[15] = 'img/sponsors/logocouldbehere.gif' ;
SponsorPic[16] = 'img/sponsors/logocouldbehere.gif' ;
SponsorPic[17] = 'img/sponsors/Infinet.gif' ;
SponsorPic[18] = 'img/sponsors/Sigma3.gif' ;
// =======================================
// do not edit anything below this line
// =======================================
var tm ;
var j1 = 0 ;
var p1 = SponsorPic.length ;
var preLoadedSponsors = new Array();
for (i1 = 0; i1 < p1; i1++)
{
   preLoadedSponsors[i1] = new Image() ;
   preLoadedSponsors[i1].src = SponsorPic[i1]  ;
}
function runSponsorShow()
//*************************************************************************************************
//**********                                                                           ************
//**********         Cycle through the images above                                    ************
//**********                                                                           ************
//*************************************************************************************************
{
   if (document.all)
   {
      document.images.SponsorShow.style.filter="blendTrans(duration=2)" ;
      document.images.SponsorShow.style.filter="blendTrans(duration=crossFadeDuration)" ;
      document.images.SponsorShow.filters.blendTrans.Apply() ;
   }
   document.images.SponsorShow.src = preLoadedSponsors[j1].src ;
   if (document.all)
   {
      document.images.SponsorShow.filters.blendTrans.Play() ;
   }
   j1++ ;
   if (j1 > (p1-1)) j1=0 ;
   tm = setTimeout('runSponsorShow()', 3000 ) ;
}
function hide(elmt)
//*************************************************************************************************
//**********                                                                           ************
//**********         Hide an element on the page                                       ************
//**********                                                                           ************
//*************************************************************************************************
{
      elmt.style.display="none" ;
//       document.getElementById(elmt).style.display="none" ;
}
function show(elmt)
//*************************************************************************************************
//**********                                                                           ************
//**********         Show an element on a page                                         ************
//**********                                                                           ************
//*************************************************************************************************
{
      elmt.style.display="block" ;
//      document.getElementById(elmt).style.display="block" ;
}

function toggle(elmt)
//*************************************************************************************************
//**********                                                                           ************
//**********         Toggle the display of an element on a page                        ************
//**********                                                                           ************
//*************************************************************************************************
{
  if (!document.getElementById) return ;

  if (document.getElementById(elmt).style.display=="block")
      document.getElementById(elmt).style.display="none" ;
  else
      document.getElementById(elmt).style.display="block" ;
}
var currElmt = "" ;
var currText = "" ;

function doMoreLess(linkObj, textObj)
//*************************************************************************************************
//**********                                                                           ************
//**********         Have a more/less link and toggle the display of a text element    ************
//**********         (The text elelement is normally a div)                            ************
//**********                                                                           ************
//*************************************************************************************************
{
  // Get the text object inbto a variable
  var textElmt = document.getElementById(textObj) ;

  // Check to see if you are clicking on a different expandable element
  if(currElmt != textElmt.id)
  {
    // If so set this up as the current expandable element
    currElmt = textElmt.id ;
    // Get the inital text that it has been given in the html (so that it can be re-applied when the expanded element is closed)
    currText = linkObj.innerHTML ;
  //  alert("Changing current item to :- " + textElmt.id + "\n\nChanging link text to :- " + currText)
  }

 // Check to see if the texElmt is currently not on display, and do the appropriate action
  if(textElmt.style.display == "none")
  {
    // Show the additional text item
    textElmt.style.display = "block";
    // Change the text on the link and the tooltip
    linkObj.innerHTML = "close [x]" ;
  	linkObj.title = "Click here to close the item" ;
  }
  else
  {
    // Hide the additional text item
  	textElmt.style.display = "none";
    // Restore the initial text on the link and the tooltip
  	linkObj.innerHTML = currText ;
    linkObj.title="Click here to expand the item" ;
  }
}

var currElmtId = "" ;

function toggleTextElmt(linkObj, textObj)
//*************************************************************************************************
//**********                                                                           ************
//**********         Have a more/less link and toggle the display of a text element    ************
//**********         (The text elelement is normally a div)                            ************
//**********                                                                           ************
//*************************************************************************************************
{
  // Check to see if you are clicking on a different expandable text element
  if(currElmtId != textObj.id)
  {
    // If so set this up as the current expandable element
    currElmtId = textObj.id ;
  }

 // Check to see if the texObj is currently not on display, and do the appropriate action
  if(textObj.style.display == "none")
  {
    // Show the additional text item
    textObj.style.display = "block";
    // Change the text/img on the link and the tooltip
    linkObj.innerHTML = "close [x]" ;
    linkObj.title = "Click here to close the item" ;
  }
  else
  {
    // Hide the additional text item
    textObj.style.display = "none";
    // Restore the initial text on the link and the tooltip
    linkObj.innerHTML = "see more [+]" ;
    linkObj.title="Click for more detail" ;
  }
}