//BEGIN Tab javascript (formerly of tabs.js)
var enabletabpersistence=0 //enable tab persistence via session only cookies, so selected tab is remembered?

////NO NEED TO EDIT BELOW////////////////////////
var tabcontentIDs=new Object()

function expandcontent(linkobj){
var ulid=linkobj.parentNode.parentNode.id //id of UL element
var ullist=document.getElementById(ulid).getElementsByTagName("li") //get list of LIs corresponding to the tab contents
for (var i=0; i<ullist.length; i++){
ullist[i].className=""  //deselect all tabs
if (typeof tabcontentIDs[ulid][i]!="undefined") //if tab content within this array index exists (exception: More tabs than there are tab contents)
document.getElementById(tabcontentIDs[ulid][i]).style.display="none" //hide all tab contents
}
linkobj.parentNode.className="selected"  //highlight currently clicked on tab
document.getElementById(linkobj.getAttribute("rel")).style.display="block" //expand corresponding tab content
saveselectedtabcontentid(ulid, linkobj.getAttribute("rel"))
}

function expandtab(tabcontentid, tabnumber){ //interface for selecting a tab (plus expand corresponding content)
var thetab=document.getElementById(tabcontentid).getElementsByTagName("a")[tabnumber]
if (thetab.getAttribute("rel"))
expandcontent(thetab)
}

function savetabcontentids(ulid, relattribute){// save ids of tab content divs
if (typeof tabcontentIDs[ulid]=="undefined") //if this array doesn't exist yet
tabcontentIDs[ulid]=new Array()
tabcontentIDs[ulid][tabcontentIDs[ulid].length]=relattribute
}

function saveselectedtabcontentid(ulid, selectedtabid){ //set id of clicked on tab as selected tab id & enter into cookie
if (enabletabpersistence==1) //if persistence feature turned on
setCookie(ulid, selectedtabid)
}

function getullistlinkbyId(ulid, tabcontentid){ //returns a tab link based on the ID of the associated tab content
var ullist=document.getElementById(ulid).getElementsByTagName("li")
for (var i=0; i<ullist.length; i++){
if (ullist[i].getElementsByTagName("a")[0].getAttribute("rel")==tabcontentid){
return ullist[i].getElementsByTagName("a")[0]
break
}
}
}

function initializetabcontent(){
for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
if (enabletabpersistence==0 && getCookie(arguments[i])!="") //clean up cookie if persist=off
setCookie(arguments[i], "")
var clickedontab=getCookie(arguments[i]) //retrieve ID of last clicked on tab from cookie, if any
var ulobj=document.getElementById(arguments[i])
var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL
for (var x=0; x<ulist.length; x++){ //loop through each LI element
var ulistlink=ulist[x].getElementsByTagName("a")[0]
if (ulistlink.getAttribute("rel")){
savetabcontentids(arguments[i], ulistlink.getAttribute("rel")) //save id of each tab content as loop runs
ulistlink.onclick=function(){
expandcontent(this)
return false
}
if (ulist[x].className=="selected" && clickedontab=="") //if a tab is set to be selected by default
expandcontent(ulistlink) //auto load currenly selected tab content
}
} //end inner for loop
if (clickedontab!=""){ //if a tab has been previously clicked on per the cookie value
var culistlink=getullistlinkbyId(arguments[i], clickedontab)
if (typeof culistlink!="undefined") //if match found between tabcontent id and rel attribute value
expandcontent(culistlink) //auto load currenly selected tab content
else //else if no match found between tabcontent id and rel attribute value (cookie mis-association)
expandcontent(ulist[0].getElementsByTagName("a")[0]) //just auto load first tab instead
}
} //end outer for loop
}


function getCookie(Name){ 
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return ""
}

function setCookie(name, value){
document.cookie = name+"="+value //cookie value is domain wide (path=/)
}
//END Tab javascript (formerly of tabs.js)

//BEGIN navbar hide/display javascript - used on MyRotoWire
//This javascript is used to display/hide items in the left nav

function getItem(id)
//return item based on its ID
{
    var itm = false;
    if(document.getElementById)
        itm = document.getElementById(id);
    else if(document.all)
        itm = document.all[id];
    else if(document.layers)
        itm = document.layers[id];

    return itm;
}

function toggleItem(id)
//toggle the visibility of an item
{
    itm = getItem(id);

    if(!itm)
        return false;

    if(itm.style.display == 'none')
    {
        itm.style.display = '';
    } 
    else
    {
        itm.style.display = 'none';
    }

    //return false;
}
//END navbar hide/display javascript

//BEGIN Text Counter - used on Ask an Expert Page
function getObject(obj)
{
  var theObj;
  if(document.all)
  {
    if(typeof obj=="string")
    {
      return document.all(obj);
    }
    else
    {
      return obj.style;
    }
  }

  if(document.getElementById)
  {
    if(typeof obj=="string")
    {
      return document.getElementById(obj);
    }
    else
    {
      return obj.style;
    }
  }
  return null;
}

function TextCounter(input, display, strText, intMaxChars)
//this checks on the number of characters in input and updates display with the number of character space left
{
  var objInput = getObject(input);
  var objDisplay = getObject(display);
  var intLength = intMaxChars - objInput.value.length;
  
  if(intLength <= 0)
  {
    intLength = 0;
    strText = '<span style="background-color: #ff0000; color: #ffffff; font-weight: bold; padding: 3px;">You are at the maximum number of characters.</span>';
    objInput.value = objInput.value.substr(0,intMaxChars);
  }
  
  if (intLength == 1)
  {
    objDisplay.innerHTML = '1 character left';
  }
  else
  {
    objDisplay.innerHTML = strText.replace("{CHAR}", intLength);
  }
}
//END Text Counter - used on Ask an Expert Page

function openSizedWindow(n,u,w,h)
{
  remote = window.open(u, n, 'width=' + w + ',height=' + h +',resizable=yes,scrollbars=yes');
  if (remote != null)
  {
    if (remote.opener == null)
      remote.opener = self;
      
    window.name = 'Sub Window';
    remote.location.href = u;
  }
}

/* addToSelectList and removeListItem are used to handle select lists*/
function addToSelectList(sourceList, destinationList)
{
  var sourceListInfo;
  var sourceListValue;
  var destListIndex;
  
  if (sourceList.selectedIndex >= 0)
  {
    sourceListInfo = sourceList[sourceList.selectedIndex].text;
    sourceListValue = sourceList[sourceList.selectedIndex].value;
    
    /*loop through destination list to make sure the value does not already exist*/
    for(destListIndex = destinationList.options.length - 1; destListIndex >= 0; destListIndex--)
    {
      if (destinationList[destListIndex].value == sourceListValue)
      {
        alert("That player is already in your list.");
        return;
      }
    } 
    
	  destinationList.options[destinationList.length] = new Option(sourceListInfo,sourceListValue);
  }
}

function removeListItem(currentList)
{
  var index = currentList.selectedIndex;
  if (index == -1)
  {
    alert("You must first select the item to remove.");
  }
  else
  {
    currentList.options[index] = null;
  }
}

function displayHiddenItem(id)
//show an item that is hidden
{
  itm = getItem(id);

  if(!itm)
    return false;

  if(itm.style.display == 'none')
  {
    itm.style.display = '';
  }
}

function hideItem(id)
//hide an item that is being shown
{
  itm = getItem(id);

  if(!itm)
    return false;

  if(itm.style.display != 'none')
  {
    itm.style.display = 'none';
  }
}

function displayHiddenItemWithText(id, strMessage, strColor)
//show an item that is hidden, update the text, change the color
{
  itm = getItem(id);

  if(!itm)
    return false;

  if(itm.style.display == 'none')
  {
    itm.style.display = '';
  }
  
  itm.innerHTML = strMessage;
  itm.style.backgroundColor = strColor;
}

function enableItem(id)
//enable an item that is disabled
{
  itm = getItem(id);

  if(!itm)
    return false;

  if(itm.disabled == true)
  {
    itm.disabled = false;
  }
}

function disableItem(id)
//disable an item that is enabled
{
  itm = getItem(id);

  if(!itm)
    return false;

  if(itm.disabled == false)
  {
    itm.disabled = true;
  }
}

function moveSelectBoxToHiddenField(selectBox, hiddenField)
//this creates a string from the values of the input selectBox and saves the string to the input hiddenField value
{
  var selectBoxIndex;
  var strValues;
  var blnFirstValue = true;

  for(selectBoxIndex = selectBox.options.length - 1; selectBoxIndex >= 0; selectBoxIndex--)
  {
    //for first value, do not put comma in string
    if (blnFirstValue)
    {
      strValues = selectBox[selectBoxIndex].value;
      blnFirstValue = false;
    }
    else
    {
      strValues = strValues + ', ' + selectBox[selectBoxIndex].value;
    }
  }
  
  hiddenField.value = strValues;
}

function clearSelectList(selectBox)
//this clears the contents of the input selectBox
{
  selectBox.options.length = 0;
}

//this calls the AJAX code to get the results from another page
function autoCompleteSearch(strSearch, strSport)
{
  xmlHttp=GetXmlHttpObject();
  
  if (xmlHttp==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  }
  
  var url="/ajax_getnames.htm";
  url=url+"?ajaxSearchName="+strSearch;
  url=url+"&sport="+strSport;  
  url=url+"&sid="+Math.random();
  xmlHttp.onreadystatechange=searchStateChanged;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

//this calls the AJAX code and makes sure the browser supports it
function GetXmlHttpObject()
{
  var xmlHttp=null;
 
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    
      //the following line makes it so that accent marks do not show as question marks
      //xmlHttp.overrideMimeType('text/xml; charset=iso-8859-1');
      //when trying to use it, the error is junk after document element
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
   
  return xmlHttp;
}

//this is used to update the AJAX autoCompleteSearch name box
//readyState Codes:
//0 - request is not initialized, 1 - request has been set up, 2 - request has been sent, 3 - request is in process, 4 - request is complete 
function searchStateChanged() 
{ 
  if (xmlHttp.readyState==4)
  { 
    document.getElementById("mainPlayerSearch").innerHTML=xmlHttp.responseText;
  }
  else
  {
    //display animated gif if user is waiting for a response
    if (xmlHttp.readyState!=0) 
    { 
      document.getElementById("mainPlayerSearch").innerHTML='<img src="/images/loading.gif" style="margin-top: 40px; margin-left: 100px;" />'
    }
  }
}

//this calls the AJAX code to work on the rating stars
function updateStarRating(strUserRating, intTeamID)
{
  xmlHttp=GetXmlHttpObject();
  
  if (xmlHttp==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  }
  
  var url="../community/ajax_starrating.htm";
  url=url+"?teamID="+intTeamID;
  url=url+"&userRating="+strUserRating;
  xmlHttp.onreadystatechange=starClicked;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

//this calls the AJAX code to work on the rating stars - for polls
function updatePollStarRating(strUserRating, intPollID)
{
  xmlHttp=GetXmlHttpObject();
  
  if (xmlHttp==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  }
  
  var url="../community/ajax_pollstarrating.htm";
  url=url+"?pollID="+intPollID;
  url=url+"&userRating="+strUserRating;
  xmlHttp.onreadystatechange=starClicked;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

//this is used to update the AJAX starRating
//readyState Codes:
//0 - request is not initialized, 1 - request has been set up, 2 - request has been sent, 3 - request is in process, 4 - request is complete 
function starClicked() 
{ 
  if (xmlHttp.readyState==4)
  { 
    document.getElementById("userRating").innerHTML=xmlHttp.responseText;
  }
  else
  {
    //display animated gif if user is waiting for a response
    if (xmlHttp.readyState!=0) 
    { 
      document.getElementById("userRating").innerHTML='<img src="/images/loading.gif" />'
    }
  }
}

//this trims a string using a regular expression
function trim(stringToTrim)
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

