var g_isGameOpen = true; //gets set to false when the countdown finishes.
var g_iAdCodeRandom = (Math.random() * 10000000000000000); // used by ad code.

/*
 * Dynamically creates curved headers for panels
 * @dependant on: jquery-1.2.1.js
 */

/*
//function to dynamically change the sport header
function changeSportHeader() {
  var s_type = document.getElementById("sport_type").value;
  document.sportMastHead.src = "../images/fantasy_masthead_" + s_type + ".jpg";
}
*/

/**
 * Used by the top navigation bar to change location of page when each tab is clicked
 * @param sPageLocation Where to forward the page.
 * @param sConfirmationMessage If this has a value, a confirm box will appear before proceeding.
 */
function goToPage(sPageLocation, sConfirmationMessage){
  if(g_sDesignId == "1")
  {
    if((sConfirmationMessage == null)){
      if(sPageLocation != null)
        if(sPageLocation == "SUBMIT")
          document.forms[0].submit();
        else
          document.location = g_sBaseLocation + sPageLocation;
    }
    else
    {
      if(sPageLocation != null){
        g_sUrl = sPageLocation;
        jQuery('#popUpLogout').dialog('open');
      }
    }
  }
  else
  {
    if((sConfirmationMessage == null) || (confirm(sConfirmationMessage))){
      if(sPageLocation != null){
        document.location = g_sBaseLocation + sPageLocation;
      }
    }
  }
}


function MastHeadLinkClicked()
{
  if(typeof(g_mastHeadLink) != "undefined" && g_sCompetitionId != "1003")
    document.location = g_mastHeadLink;
}

function logOut()
{
  document.location = g_sBaseLocation + g_sUrl;
}

/**
 * Displays the help pop-up window.
 * @param sPreferredTopic: String
 */
function showHelp(sPreferredTopic){
  if(sPreferredTopic != ""){
    sPreferredTopic = "?topic=" + sPreferredTopic;
  }
  var windowHelp = "";
  if(g_sDesignId == "")
   windowHelp = window.open(g_sBaseLocation + '/jsp/help.jsp' + sPreferredTopic,'Fantasy_Help','scrollbars=yes,menubar=0,location=0,resizable=yes,directories=0,status=0,height=500,width=760');
  else
    windowHelp = window.open(g_sBaseLocation + '/jsp/help.jsp' + sPreferredTopic,'Fantasy_Help','scrollbars=yes,menubar=0,location=0,resizable=yes,directories=0,status=0,height=680,width=910');
  if (window.focus) {
    windowHelp.focus();
  }
  return false;
}

/**
 * Displays the other-persons-USquad pop-up window.
 * @param iUsquadId Id of the USquad to be shown.
 */
function showPeek(iUSquadId){
  if(!g_isPeekPopupWindow){
    return;
  }
  var sUSquadId = "" + iUSquadId;
  if(sUSquadId != ""){
    sUSquadIdParam = "?usquadid=" + sUSquadId;
  }
  if(typeof g_sPeekPopupWindowSettings == "undefined"){
    // This is set in the settingsSport<sport-id>.jsp javascript library. If no setting is found, use default.
    g_sPeekPopupWindowSettings = "scrollbars=yes,menubar=0,location=0,resizable=yes,directories=0,status=0,width=555,height=505";
  }
  var windowHelp = window.open(g_sBaseLocation + '/jsp/peekUSquad.jsp' + sUSquadIdParam,'Fantasy_USquad', g_sPeekPopupWindowSettings);
  if (window.focus) {
    windowHelp.focus();
  }
  return false;
}

/**
 * Put a string value into a named cookie.
 * @param sCookieName String to identify the cookie.
 * @param sCookieValue String to set as the value.
 * @param iDaysToLast How long to keep the cookie. 0 will erase the cookie.
 */
function setCookie(sCookieName, sCookieValue, iDaysToLast){
  var dtToday = new Date();
  if(isNaN(iDaysToLast)){
    iDaysToLast = -1;
  }
  if (iDaysToLast != -1){
    iDaysToLast = iDaysToLast * 1000 * 60 * 60 * 24;
  }
  var dtExpiryDate = new Date(dtToday.getTime() + (iDaysToLast));
  var sOut = "";
  sOut += sCookieName + "=" +escape(sCookieValue);
  if(iDaysToLast != -1){
    sOut += ";expires=" + dtExpiryDate.toGMTString();
  }
  document.cookie = sOut;
}

/**
 * Remove a named cookie.
 * @param sCookieName String to identify the cookie.
 */
function deleteCookie(sCookieName){
  setCookie(sCookieName, "", 0);
}

/**
 * Return a string value associated with a named cookie.
 * @param sCookieName String to identify the cookie.
 */
function getCookie(sCookieName){
  var sCookieName = sCookieName;
  var iStartPos = document.cookie.indexOf( sCookieName + "=" );
  var iLength = iStartPos + sCookieName.length + 1;
  if ((!iStartPos) && (sCookieName != document.cookie.substring( 0, sCookieName.length))) {
    return "";
  }
  if (iStartPos == -1) return "";
  var iEndPos = document.cookie.indexOf(";", iLength );
  if (iEndPos == -1) iEndPos = document.cookie.length;
  //a lert("getting cookie " + sCookieName + "\n" + unescape(document.cookie.substring(iLength, iEndPos)));
  return unescape(document.cookie.substring(iLength, iEndPos));
}

/**
 * Puts commas where appropriate eg. changes 1000 to 1,000.
 * @param sNum String to which commas are added.
 */
function addCommas(sNum) {
  if(!isNaN(sNum)){
    sNum += '';
    var x = sNum.split('.');
    var x1 = x[0];
    var x2 = ((x.length > 1) ? '.' + x[1] : '');
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
      x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
  } else {
    return sNum;
  }
}

/**
 * Pop the chattelResearch page up in a floating window.
 * @param iCompId The competition ID - the context of the chattel.
 * @param iChattelId Pointer to the chattel.
 */
function showChattelResearch(iCompId, iChattelId){
  var windowChattelScores = window.open(g_sBaseLocation + "/jsp/chattelResearch.jsp" + "?competitionid=" + iCompId + "&chattelid=" + iChattelId, "Chattel_Research_Fantasy_Points", "height=500,width=620,menubar=no,location=no,resizable=yes,directories=no,status=no,scrollbars=yes");
  if (window.focus) {
    windowChattelScores.focus();
  }
  return windowChattelScores;
}

/**
 * Runs if the graph fails to load or is not available.
 * @param imgObj The image object that fails to load.
 * Usage Example: onError="graphError(this)".
 */
function showGraphErrorMsg(imgObj) {
  $(imgObj).parent().html("<div style=\"color:#000000;font-weight:bold;height:150px;\">Graph not available</div>");
}

/**
 * Run gets the safe node value when parse xml data
 * @param nodeSrc: Source Node
 * Usage Example: getSafeNodeValue(XmlData[1].firstChild)
 */
function getSafeNodeValue(nodeSrc){
  try {
    var sValue = nodeSrc.nodeValue;
  } catch (err) {
      sValue = "";
  }
  return sValue;
}

/*
 * Force a refresh of the page-baseline content.
 * Requires each page to have g_sCompetitionId  g_sSessionSiteCode vars.
 */
function reloadPageBaseline(){
  try {
    var sNelNetRatUrl = g_sBaseLocation + "/includes/dynamicContent.jsp?competitionid=" + g_sCompetitionId + "&contentkey=pagebaseline" + g_sSessionSiteCode;
    //alert(sNelNetRatUrl);
    var iframeTrg = document.getElementById("page-baseline");
    if(iframeTrg){
      iframeTrg.src = sNelNetRatUrl;
    }
  } catch (e){}
}


