// Ajax Code:
var http = false;
if(navigator.appName == "Microsoft Internet Explorer") http = new ActiveXObject("Microsoft.XMLHTTP");
else http = new XMLHttpRequest(); 

// Mouse movement function.  Can be used through out. posX and posY
if (navigator.appName == "Netscape") window.captureEvents(Event.MOUSEMOVE);
var posX = 0;
var posY = 0;
document.onmousemove = getMouseXY;


/* -----------------------------------------
onlineListLoaded (JS) this function hides the red "loading" screen and shows the online list on the main page
  ----------------------------------------*/
function onlineListLoaded()
{
  var currentPage = window.location.pathname;
  //if (currentPage == "/tpc/main.php") document.getElementById('onlineListLink').style.display = 'block'; 
  //if (currentPage == "/tpc/main.php") document.getElementById('loadingIndicators').style.display = 'none';
}
/* -----------------------------------------
showOnlineList(JS) this closes the red "loading" screen and shows the online list.
  ----------------------------------------*/
function showOnlineList()
{
  document.getElementById('online_list').style.display = 'block'; 
  document.getElementById('now_approaching').style.display = 'none';
}
  
  
function getMouseXY(e) {
  var tempX = (document.all) ? event.x : e.pageX;
  var tempY = (document.all) ? event.y : e.pageY;
  posX = tempX;
  posY = tempY;
}

  
  
// Populates the team breakdown 
function team_breakdown(team)
{
  http.abort();
  http.open("GET", "scrips.php?function=1&team=" + team, true);
  http.onreadystatechange=function()
  {
    if(http.readyState == 4) document.getElementById('breakdown_div').innerHTML = http.responseText;
  }
  http.send(null);
}




// This function will call an php document that displays the selected tab.
function main_tabs(tab)
{
  switch (tab)
  {
    case 1: tab_contents = "recent_games";  break;
    case 2: tab_contents = "xfactor_table"; break;
    case 3: tab_contents = "top_players";   break;
  }
  
  http.abort();
  http.open("GET", "functions.php?function=" + tab_contents, true);
  http.onreadystatechange=function()
  {
    if(http.readyState == 4) document.getElementById('tab_sidebar').innerHTML = http.responseText;
  }
  http.send(null);
}





// ------------------------- rom editing scripts below
function change_rating(id, stat, rating, oldrating) {
  http.abort();
  http.open("GET", "rom_ajax.php?id=" + id + "&stat=" + stat + "&rating=" + rating + "&oldrating=" + oldrating + "&function=" + 1, true);
  http.onreadystatechange=function()
  {
    if(http.readyState == 4) document.getElementById('status_box').innerHTML = http.responseText;
  }
  http.send(null);
}

function check_name(name) {
  http.abort();
  http.open("GET", "rom_ajax.php?name=" + name + "&function=" + 2, true);
  http.onreadystatechange=function()
  {
    if(http.readyState == 4) document.getElementById('name_is_good').innerHTML = http.responseText;
  }
  http.send(null);
}

function change_name(id, name, oldname) {
  http.abort();
  http.open("GET", "rom_ajax.php?id=" + id + "&name=" + name + "&oldname=" + oldname + "&function=" + 3, true);
  http.onreadystatechange=function()
  {
    if(http.readyState == 4) document.getElementById('status_box').innerHTML = http.responseText;
  }
  http.send(null);
}

function change_num(id, num, oldnum) {
  http.abort();
  http.open("GET", "rom_ajax.php?id=" + id + "&num=" + num + "&oldnum=" + oldnum + "&function=" + 4, true);
  http.onreadystatechange=function()
  {
    if(http.readyState == 4) document.getElementById('status_box').innerHTML = http.responseText;
  }
  http.send(null);
}

// This function populates the little pink box on the edit players page.
function original_rom_box(pos, stat){
  http.abort();
  http.open("GET", "rom_ajax.php?pos=" + pos + "&stat=" + stat + "&function=" + 6, true);
  http.onreadystatechange=function()
  {
    if(http.readyState == 4) document.getElementById('original_rom_box').innerHTML = http.responseText;
  }
  http.send(null);
}

function display_message(display_message){
  if (display_message.length != 0) document.getElementById('status_box').innerHTML = display_message;
}

function trade_box(team, side){
  // which side
  if (side == 1) team_div = "roster1";
  if (side == 2) team_div = "roster2";
  
  http.abort();
  http.open("GET", "rom_ajax.php?team=" + team + "&side=" + side + "&function=" + 7, true);
  http.onreadystatechange=function()
  {
    if(http.readyState == 4) document.getElementById(team_div).innerHTML = http.responseText;
  }
  http.send(null);
}

function process_trade(team_from, team_to, team_order){
  // One checkbox selected - have to do this because javascript is gay and doesn't know what data type something is if only one box is checked
  // i used hyphens so i could use the php function explode to parse the data
  var traded = "TradedPlayers";
  
  // if have to do this twice because there are two roster sides
  if (team_order == 1)
  {
    if (typeof document.forms.trade_roster1.traded_player.type == "string") traded += "-" + document.forms.trade_roster1.traded_player.value;
    else  // is an array
    {
      for (i = 0; i < document.forms.trade_roster1.traded_player.length; i++)
      {
        if (document.forms.trade_roster1.traded_player[i].checked == true) traded += "-" + document.forms.trade_roster1.traded_player[i].value;
      }
    }
  }
  if (team_order == 2)
  {
    if (typeof document.forms.trade_roster2.traded_player.type == "string") traded += "-" + document.forms.trade_roster2.traded_player.value;
    else  // is an array
    {
      for (i = 0; i < document.forms.trade_roster2.traded_player.length; i++)
      {
        if (document.forms.trade_roster2.traded_player[i].checked == true) traded += "-" + document.forms.trade_roster2.traded_player[i].value;
      }
    }
  }

  http.abort();
  http.open("GET", "rom_ajax.php?team_from=" + team_from + "&team_to=" + team_to + "&traded_players=" + traded + "&side=" + team_order + "&function=" + 8, true);
  http.onreadystatechange=function()
  {
    if(http.readyState == 4)
    {
      document.getElementById('trade_result').innerHTML = http.responseText;
      document.getElementById('roster1').innerHTML = "Select Team";
      document.getElementById('roster2').innerHTML = "Select Team";
      document.getElementById('team1').selectedIndex = "- Select Team -";
      document.getElementById('team2').selectedIndex = "- Select Team -";
    } 
  }
  http.send(null);
}

function face_popup(id,oldface,name){
  mywindow = window.open ("rom_faces.php?id=" + id + "&oldface=" + oldface + "&name=" + name, "face_window", "location=1,status=1,scrollbars=1, width=500,height=600");
}

function showBigFace(face)
{

  document.getElementById('face_box').style.visibility = "visible";
  document.getElementById('face_box').innerHTML = "<img src='../tecmo/faces/" + face + ".GIF' style='padding-top: 4px;'>"  + posX;
  posX += 25;
  posY -= 32;
  document.getElementById('face_box').style.left = posX + "px";
  document.getElementById('face_box').style.top  = posY + "px";
}


function change_face(id, face, oldface) {
  http.abort();
  http.open("GET", "rom_ajax.php?id=" + id + "&face=" + face + "&oldface=" + oldface + "&function=" + 5, true)
  http.onreadystatechange=function()
  {
    if(http.readyState == 4) window.opener.location.reload();
  }
  http.send(null);
}
// --------------------------------------- rom editing scripts above

//function change_face(id, face, oldface) {
//  http.abort();
//  http.open("GET", "rom_ajax.php?id=" + id + "&face=" + face + "&oldface=" + oldface + "&function=" + 5, true);
//  http.onreadystatechange=function()
//  {
//    if(http.readyState == 4) document.getElementById('status_box').innerHTML = http.responseText;
//  }
//  http.send(null);
//}
