var toptabIsLeft=true;
var joinexistIsLeft=true;
var joinpubIsLeft=false;
var createIsLeft=false;

function activateMoreOptions() {
  $('subcreatetablepanel1').style.display='none';
  $('subcreatetablepanel2').style.display='';
  $('panel1tab').style.backgroundColor='#DDDDFF';
  $('panel2tab').style.backgroundColor='#BBFFBB';
}
function activateGameSetup() {
  $('subcreatetablepanel1').style.display='';
  $('subcreatetablepanel2').style.display='none';
  $('panel1tab').style.backgroundColor='#BBFFBB';
  $('panel2tab').style.backgroundColor='#DDDDFF';
}
function clickRight(hideError) {
  if ($('errormsg_container') && hideError) {
    $('errormsg_container').style.display='none';
  }
  if (toptabIsLeft) {
    toptabIsLeft=false;
    $('toptabright').style.backgroundColor='#EEEEEE';
    $('toptabright').style.borderLeft='1px solid #BBBBFF';
    $('toptabright').style.borderBottom='1px solid #EEEEEE';
    $('toptableft').style.backgroundColor='#E5E5FF';
    $('toptableft').style.borderRight='1px solid #E5E5FF';
    $('toptableft').style.borderBottom='1px solid #BBBBFF';
    $('createpanel').style.display='';
    $('joinpanel').style.display='none';
    $('toptableft').innerHTML='<h2><a href="#" onclick="clickLeft(1);return false">Join Game</a></h2>';
    $('toptabright').innerHTML='<h2>Create Game</h2>';
  }
}
function clickLeft(hideError) {
  if ($('errormsg_container') && hideError) {
    $('errormsg_container').style.display='none';
  }
  if (!toptabIsLeft) {
    toptabIsLeft=true;
    $('toptableft').style.backgroundColor='#EEEEEE';
    $('toptableft').style.borderRight='1px solid #BBBBFF';
    $('toptableft').style.borderBottom='1px solid #EEEEEE';
    $('toptabright').style.backgroundColor='#E5E5FF';
    $('toptabright').style.borderLeft='1px solid #E5E5FF';
    $('toptabright').style.borderBottom='1px solid #BBBBFF';
    $('createpanel').style.display='none';
    $('joinpanel').style.display='';
    $('toptableft').innerHTML='<h2>Join Game</h2>';
    $('toptabright').innerHTML='<h2><a href="#" onclick="clickRight(1);return false">Create Game</a></h2>';
  }
}

function joinpubLeft() {
  if ($('errormsg_container')) {
    $('errormsg_container').style.display='none';
  }
  if (!joinpubIsLeft) {
    joinpubIsLeft=true;
    $('subjoinpub1').style.display='';
    $('subjoinpub2').style.display='none';

    $('joinpub_left').style.display='none';
    $('joinpub_left').value='';
    $('joinpub_right').style.display='';
    $('joinpub_right').value="   Next >>   ";
  }
}

function joinpubRight() {
  if ($('errormsg_container')) {
    $('errormsg_container').style.display='none';
  }
  if (joinpubIsLeft) {
    joinpubIsLeft=false;
    $('subjoinpub1').style.display='none';
    $('subjoinpub2').style.display='';

    $('joinpub_left').style.display='';
    $('joinpub_left').value="   << Previous   ";
    $('joinpub_right').style.display='';
    $('joinpub_right').value='   Join Game   ';
    if ($('joinpub_type_live').checked) {
      $('joinpub_playerpass').style.display='';
      $('joinpub_playeremail').style.display='none';
    } else {
      $('joinpub_playerpass').style.display='none';
      $('joinpub_playeremail').style.display='';
    }
  } else {
    $('joinpubform').submit();
  }
}

function createLeft() {
  if ($('errormsg_container')) {
    $('errormsg_container').style.display='none';
  }
  if (!createIsLeft) {
    createIsLeft=true;
    $('subcreatepanel1').style.display='';
    $('subcreatepanel2').style.display='none';
    $('create_left').style.display='none';
    $('create_left').value='';
    $('create_right').style.display='';
    $('create_right').value="   Next >>   ";
  }
}

function createRight() {
  if ($('errormsg_container')) {
    $('errormsg_container').style.display='none';
  }
  if (createIsLeft) {
    createIsLeft=false;
    $('subcreatepanel1').style.display='none';
    $('subcreatepanel2').style.display='';

    $('create_left').style.display='';
    $('create_left').value="   << Previous   ";
    $('create_right').style.display='';
    $('create_right').value='   Create Game   ';
    if ($('create_type_live').checked) {
      $('create_playerpass').style.display='';
      $('create_playeremail').style.display='none';
    } else {
      $('create_playerpass').style.display='none';
      $('create_playeremail').style.display='';
    }
  } else {
    $('createform').submit();
  }
}

function joinexistLeft() {
  if ($('errormsg_container')) {
    $('errormsg_container').style.display='none';
  }
  if (!joinexistIsLeft) {
    joinexistIsLeft=true;
    $('subjoinexist1').style.display='';
    $('subjoinexist2').style.display='none';
    $('subjoinexist3').style.display='none';
    $('joinexist_left').style.display='none';
    $('joinexist_left').value='';
    $('joinexist_right').style.display='';
    $('joinexist_right').value="   Next >>   ";
  }
}

function joinexistRight(hideError) {
  if ($('errormsg_container') && hideError) {
    $('errormsg_container').style.display='none';
  }
  if (joinexistIsLeft) {
    var targetGid=$('joinexist_gameid').value+"";
    var targetSearch=$('joinexist_gamename').value+"";
    if (targetGid.length > 0) {
      if (!numeric(targetGid+"")) {
	showErrorMessage("Error: please enter a valid Game ID");
      } else {
	doAjax('action=checkgid&g='+targetGid);
      }
    } else if (targetSearch.length > 0) {
      $('subjoinexist3').innerHTML='';
      doAjax('action=searchbyname&q='+targetSearch);
    } else {
      showErrorMessage("Error: please enter a valid Game ID");
    }
  } else {
    $('joinexistform').submit();
  }
}

function joinexistShowForm(gname) {
  joinexistIsLeft=false;
  $('subjoinexist1').style.display='none';
  $('subjoinexist2').style.display='';
  $('subjoinexist3').style.display='none';

  $('joinexist_left').style.display='';
  $('joinexist_left').value="   << Previous   ";
  $('joinexist_right').style.display='';
  $('joinexist_right').value='   Join Game   ';
  $('subjoinexist_gamename').innerHTML=gname+"";
}

function showErrorMessage(msg) {
  $('errormsg_container').style.display='';
  $('errormsg_messagetext').innerHTML=msg;
}

function addGameSearchResult(gid,name,started,creator) {
  if ($('subjoinexist3').style.display == 'none') {
    $('subjoinexist1').style.display='none';
    $('subjoinexist2').style.display='none';
    $('subjoinexist3').style.display='';
  }
  
  var startedText='No';
  var joinText='Join';
  if (started) {
    startedText='Yes';
    joinText='Rejoin';
  }

  var html='<div class="namesearchresult">';
  html+='<table border="0" cellspacing="0" cellpadding="2">';
  html+='<tr><td style="width:1%"><b>Name:</b></td><td>'+name+'</td><td rowspan=3 style="text-align:right;vertical-align:middle"><div style="float:right;text-align:center"><input type=button value="'+joinText+'" style="font-weight:bold" onclick="joinexistSearchShowForm(\''+gid+'\',\''+name+'\')"><br/><input type=button value="Observe" disabled=true></div></td></tr>';
  html+='<tr><td><b>Creator:</b></td><td>'+creator+'</td></tr>';
  html+='<tr><td><b>Started?:&nbsp;&nbsp;</b></td><td>'+startedText+'</td></tr>';
  html+='</table></div>';
  $('subjoinexist3').innerHTML+=html;
}

function joinexistSearchShowForm(gid,gname) {
  $('joinexist_gameid').value=gid;
  joinexistShowForm(gname);
}

function joinexistShowSearchResults() {
  joinexistIsLeft=false;
  $('joinexist_left').style.display='';
  $('joinexist_left').value="   << Previous   ";
  $('joinexist_right').style.display='none';
}
