function PostForm(strFormName)
{

   if( !document.forms[strFormName] ) {
      alert('Could not locate main page form.  Please try refreshing your browser.');
      return false;
   }
   document.forms[strFormName].submit();
   return false; 
}

function SetCurrentPageForPagination(strFormName, strCurrentPage)
{
   document.forms[strFormName].iCurrentPage.value = strCurrentPage;
   return PostForm(strFormName);
}


function SetOnField(strFormName, strFieldName, strFieldValue)
{
   if( !document.forms[strFormName] ) {
      alert('Could not locate main page form.  Please try refreshing your browser.');
      return false;
   }
   document.forms[strFormName].elements[strFieldName].value = strFieldValue;
}

function IsValidNumber(strDigit)
{
   if ( strDigit == "0" ) {
      return true;
   }
   if ( strDigit == "1" ) {
      return true;
   }
   if ( strDigit == "2" ) {
      return true;
   }
   if ( strDigit == "3" ) {
      return true;
   }
   if ( strDigit == "4" ) {
      return true;
   }
   if ( strDigit == "5" ) {
      return true;
   }
   if ( strDigit == "6" ) {
      return true;
   }
   if ( strDigit == "7" ) {
      return true;
   }
   if ( strDigit == "8" ) {
      return true;
   }
   if ( strDigit == "9" ) {
      return true;
   }
   return false;   
}

function IsFieldValidNumber(strFormName, strFieldValue)
{
   var bValidNumber = true;
   if ( strFieldValue.length > 0 ) {
     var i = 0;
     while ( i < strFieldValue.length && bValidNumber ) {
       bValidNumber = IsValidNumber(strFieldValue.substring(i, (i+1)));
       i++;
     }
   }
   if (!bValidNumber) {
      alert('ERROR! You entered an invalid #: ' + strFieldValue);
   } 
   return bValidNumber;
}


/******************************************************************
* FUNCTION:     IsFieldValidNumberForSomething
* PARAMETERS:   strFormName
*                 The name of the current form
*               strFieldName
*                 The name of the field to check
*               strSomething
*                 The something we are checking the # for, could 
*                 be a persons name
* PURPOSE:      Checks to see if the field name passed in is 
*               a valid number
*****************************************************************/
function IsFieldValidNumberForSomething(strFormName, strFieldName, strSomething)
{
   var bValidNumber  = true;
   var strFieldValue = document.forms[strFormName].elements[strFieldName].value;
   var bFoundDecimal = false;

   if ( strFieldValue.length > 0 ) {
     var i = 0;
     while ( i < strFieldValue.length && bValidNumber ) {
       if (strFieldValue.substring(i, (i+1)) != ".") {
          bValidNumber = IsValidNumber(strFieldValue.substring(i, (i+1)));
       } else if (!bFoundDecimal && ((i+1) != strFieldValue.length)) {
          bFoundDecimal = true;
       } else {
          //to many decimals
          bValidNumber = false;
       }
       i++;
     }
   }
   if (!bValidNumber) {
      alert('ERROR! You entered an invalid number for ' + strSomething + ': ' + strFieldValue);
   } 
   return bValidNumber;
}


function TextCounter( field, countfield, maxlimit ) 
{
  if ( field.value.length > maxlimit )
  {
  
    field.value = field.value.substring( 0, maxlimit );
    alert( 'Textarea value can only be ' + maxlimit + ' characters in length.' );
    return false;
  }
  else
  {
    countfield.value = maxlimit - field.value.length;
  }
}

function ShowColorPicker(strFormName, strCurrentField)
{
  var strOldColor = document.forms[strFormName].elements[strCurrentField].value;
  strOldColor = strOldColor.replace("#", '');
  var strNewColor = showModalDialog(_editor_url + "popups/select_color.html", strOldColor, "dialogHeight:218px; dialogWidth:240px; resizable: no; help: no; status: no; scroll: no;");
  if ( strNewColor.length == 6 ) {
      document.forms[strFormName].elements[strCurrentField].value = "#" + strNewColor;
  }
}


/******************************************************************
* FUNCTION:     EnableField
* PARAMETERS:   strFormName
*                 The name of the current form
*               strPassingFieldValue
*                 The name of the field to check
*               strFieldToEnable
*                 The name of the field to enable
*               strDefaultValue
*                 The value to set the enabled field too if we disable it
* PURPOSE:      This function will check to see if the value of the passing
*               field is null, if it is then we want to disable the enabled field
*               passed in and set it to the default value passed in. If the passing
*               field has a value, we want to make sure the enabled field is 
*               enabled for user use.
******************************************************************/
function EnableField(strFormName, strPassingFieldValue, strFieldToEnable, strDefaultValue)
{
   if( !document.forms[strFormName] ) {
      alert('Could not locate main page form.  Please try refreshing your browser.');
      return false;
   }
   if ( document.forms[strFormName].elements[strPassingFieldValue].value == '' ) {
      document.forms[strFormName].elements[strFieldToEnable].disabled = true;
      document.forms[strFormName].elements[strFieldToEnable].value    = strDefaultValue;
   } else {
      document.forms[strFormName].elements[strFieldToEnable].disabled = false;
   }
}

/******************************************************************
* FUNCTION:     RemoveDefaultValue
* PARAMETERS:   strFormName
*                 The name of the current form
*               strFieldName
*                 The name of the field to check
*               strDefaultValue
*                 The value to check to see if we need to wipe the field out
* PURPOSE:      This function will wipe out the default value if the 
*               strFieldName.value = the strDefaultValue passed in
******************************************************************/
function RemoveDefaultValue(strFormName, strFieldName, strDefaultValue) 
{
   if( !document.forms[strFormName] ) {
      alert('Could not locate main page form.  Please try refreshing your browser.');
      return false;
   }
   if ( document.forms[strFormName].elements[strFieldName].value == strDefaultValue ) {
      document.forms[strFormName].elements[strFieldName].value    = '';
   } 
}


/******************************************************************
* FUNCTION:     SetArrayOfCheckBoxes
* PARAMETERS:   strFormName
*                 The name of the current form
*               aCheckBoxNames
*                 An array of all check box names
*               bChecked
*                 Boolean - T or F, sets all cbs to this
* PURPOSE:      Will rip through the array of checkboxes and set 
*               the value checked to be bChecked passed in
******************************************************************/
function SetArrayOfCheckBoxes(strFormName, aCheckBoxNames, bChecked) 
{
   if( !document.forms[strFormName] ) {
      alert('Could not locate main page form.  Please try refreshing your browser.');
      return false;
   }
   for ( var i = 0; i < aCheckBoxNames.length; i++ ) {
      document.forms[strFormName].elements[aCheckBoxNames[i]].checked = bChecked;
   }
}


/******************************************************************
* FUNCTION:     SetDefaultCoachesCategories
* PARAMETERS:   strFormName
*                 The name of the current form
*               strCoachesValue
*                 Y or N
*               aSchoolArray
*                 The school array name
*               aCoachesCategoriesArray
*                 The array of default categories
*               strExtraMessage
*                 Extra message for the alert
* PURPOSE:      Will rip through the array of checkboxes and set 
*               the value checked to be bChecked passed in
******************************************************************/
function SetDefaultCoachesCategories(strFormName, strCoachesValue, aSchoolArray, aCoachesCategoriesArray, strExtraMessage)
{
   if(strCoachesValue == 'Y') { 
      SetArrayOfCheckBoxes(strFormName, aSchoolArray, false); 
      SetArrayOfCheckBoxes(strFormName, aCoachesCategoriesArray, true); 
      alert('The default School Categories have now been set and are ready to be saved. ' + strExtraMessage);
   }
}

/****************************************************************************************
* FUNCTION:     OpenPopupWindow
* PARAMETERS:   strLink
*                 The link of the popup window content
*               strWindowFrameName
*                 The name of the window frame
*               iFrameWidth
*                 Width of the popup windo frame
*               iFrameWidth
*                 Width of the popup windo frame
*               iLeft
*                 left and top set the position of the popup on the screen
*               iTop
*                 left and top set the position of the popup on the screen
*               strToolBar
*                 toolbar indicates if there should be toolbar in the popup
*               strLocationBar
*                 location bar is where the URL is displayed at the top of the window.
*               strStatusBar
*                 browser displays messages for the user. 
*                 display the URL of a link when the mouse passes over the link
*               strMenuBar
*                  menu bar is the menu for the popup window
*               strScrollBars
*                  This is a problem if the content of the popup takes up more space 
*                   than the first page
*               strResizable
*                 Resizes window
* PURPOSE:      This function will wipe out the default value if the 
*               strFieldName.value = the strDefaultValue passed in
****************************************************************************************/
function OpenPopupWindow(strLink, strWindowFrameName, iLeft, iTop, iFrameWidth, iFrameHeight, strToolBar, strLocationBar, strStatusBar, strMenuBar, strScrollBars, strResizable)
{
   if (!window.focus) {
      return true;
   }

   window.open(strLink, strWindowFrameName, 'width=' + iFrameWidth + ', height=' + iFrameHeight + ', left=' + iLeft + ', top=' + iTop + ', toolbar=' + strToolBar + ', location=' + strLocationBar + ', status=' + strStatusBar + ', menubar=' + strMenuBar + ', scrollbars=' + strScrollBars + ', resizable=' + strResizable + '');

   return false;
}


/******************************************************
FRAME ELEMENT FUNCTIONS
*******************************************************/

/**********************************************************************************************************************
* FUNCTION:     changeCurrentScoreboardDisplay
* PARAMETERS:   strVisitorLogo
*                 The vistor logo image
*               strHomeLogo
*                 The home team logo image
*               strVisitorRecord
*                 The record of the vistor team
*               strVisitorMascot
*                 Mascot name of the vistors
*               strHomeMascot
*                 Mascot name of the home team
*               strHomeRecord
*                 The record of the home team
*               iVisitorScore
*                 The vistor score
*               strGameStatus
*                 The status of the game - for now final - could lead to real time
*               iHomeScore
*                 The current home score
*               strEventDate
*                 The date of the event
*               $strScheduleLink
*                 The link to the schedule
* PURPOSE:      Replaces the values in the document with the values passed in
**********************************************************************************************************************/
function changeCurrentScoreboardDisplay(strVisitorLogo, strHomeLogo, strVisitorMascot, strHomeMascot, strVisitorRecord, strHomeRecord, strScheduleType, iVisitorScore, strGameStatus, iHomeScore, strEventDate, $strScheduleLink) 
{
   document.scoreboard_featured_visitor_logo.src = strVisitorLogo;
   document.scoreboard_featured_home_logo.src = strHomeLogo;
   document.getElementById('scoreboard_featured_vs').innerHTML = strVisitorMascot + " vs. " + strHomeMascot;
   document.getElementById('scoreboard_featured_visitor_record').innerHTML = strVisitorRecord;
   document.getElementById('scoreboard_featured_home_record').innerHTML = strHomeRecord;
   document.getElementById('scoreboard_featured_schedule_type').innerHTML = strScheduleType;
   document.getElementById('scoreboard_featured_visitor_score').innerHTML = iVisitorScore;
   document.getElementById('scoreboard_featured_game_status').innerHTML = strGameStatus;
   document.getElementById('scoreboard_featured_home_score').innerHTML = iHomeScore;
   document.getElementById('scoreboard_featured_event_date').innerHTML = strEventDate;
   document.getElementById('scoreboard_featured_link').innerHTML = $strScheduleLink;
}


/**********************************************************************************************************************
* FUNCTION:     PrepareToDisplayNewScoreBoardData
* PARAMETERS:   strAction
*                 The action the user is wanting to pursue
*               strNextStopImage
*                 The next stop image to be displayed
*               strNextPlayImage
*                 The next play image to be displayed
* PURPOSE:      This uses the global variables determined in code to call the changeScoreboard data. Values must be set
*               This function will call itself every 4 seconds.
* RETURNS:      Bool - True if the scoreboard was set, false if we are at a disabeld field
**********************************************************************************************************************/
function PrepareToDisplayNewScoreBoardData(strAction, strNextStopImage, strNextPlayImage)
{
   //These are 'disabled' cases lets leave
   if ( ((strAction == "stop") && (g_bScoreboardPaused)) || 
        ((strAction == "play") && (!g_bScoreboardPaused)) ) {
      return false;
   }

   if (strAction == "back") {
      g_bScoreboardPaused = true;
      g_iScoreTrace--;
   } else if (strAction == "stop") {
      //Since during the play process we increment, we must decrement to get back to the current location
      g_iScoreTrace--;
      g_bScoreboardPaused = true;
   } else if (strAction == "play") {
      g_bScoreboardPaused = false;
   } else if (strAction == "forward") {
      g_bScoreboardPaused = true;
      g_iScoreTrace++;
   } 

   //Making sure g_iScoreTrace is in the desired range.
   if (g_iScoreTrace < 0) {
       g_iScoreTrace = (g_iNumberOfScores - 1);
   }
   if ( g_iScoreTrace >=  g_iNumberOfScores) {
      g_iScoreTrace = 0;
   }

   if (strNextStopImage != '') {
       document.scoreboard_stop_button.src = strNextStopImage;
   }
   if (strNextPlayImage != '') {
       document.scoreboard_play_button.src = strNextPlayImage;
   }

   changeCurrentScoreboardDisplay(g_aScores[g_iScoreTrace][0],g_aScores[g_iScoreTrace][1], g_aScores[g_iScoreTrace][2], g_aScores[g_iScoreTrace][3], g_aScores[g_iScoreTrace][4], g_aScores[g_iScoreTrace][5], g_aScores[g_iScoreTrace][6], g_aScores[g_iScoreTrace][7], g_aScores[g_iScoreTrace][8], g_aScores[g_iScoreTrace][9], g_aScores[g_iScoreTrace][10], g_aScores[g_iScoreTrace][11]);

   //If scoreboard is not paused lets continue changing it every 4 seconds
   if (!g_bScoreboardPaused) {
      g_iScoreTrace++;
      setTimeout("PrepareToDisplayNewScoreBoardData('', '', '')", 4000);
   }
   return true;
}


