﻿/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{

  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod,sURL+"?"+sVars, true);
        sVars = "";     
    }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          
     
          
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


arrRoomName = new Array();
arrRoomLink = new Array();
arrGamesType = new Array();


arrRoomName[1] = "Carbon poker";
arrRoomLink[1] ="http://www.pokercs.com/_074fa52d5a0b56d1e4b6002f06212308/1";
arrRoomName[2] = "a";
arrRoomLink[2] ="http://www.a.com";

arrGamesType[1] = "texas holdem";
arrGamesType[3] = "omaha hi/lo";
arrGamesType[4] = "razz";
arrGamesType[5] = "seven card stud";
arrGamesType[6] = "seven stud hi/lo";
arrGamesType[7] = "HORSE";
arrGamesType[8] = "HOSE";


var xCon = new XHConn();
var arrTrnys = new Array();
var numOfCol = 9 ;
var rowStyle = "tMonitorLine1";
var tmString = ""; // the table 
var dateTimeNow;

xCon.connect('trnysMonitor/getTrnysData.aspx','GET','',TrnysResponse);

function TrnysResponse(txt)
{   
    //good to check if there an error 
    document.getElementById('mt').innerHTML = txt.responseText;
    dateTimeNow = new Date();
    
   arrTrnys = txt.responseText.split(',');
   
   
   tmString = "<table border='1' width='700px' cellspacing='3'>" +
   "<tr>"+
       "<th width='20%'>Room</th>"+
       "<th width='33%'>Description</th>"+
       "<th width='18%'>Game</th>"+
       "<th width='14%'>Buy-in +<br />Fee</th>"+
       "<th width='10%'>Grntd.</th>"+    
       "<th width='5%'></th>" +
   "</tr>" ;  
   
  for(var i = 0 ; i < arrTrnys.length-1 ; i += numOfCol)
   {
   
       if(rowStyle == "tMonitorLine1")
            rowStyle = "tMonitorLine2";
       else
            rowStyle = "tMonitorLine1";
   
       tmString += "<tr class='"+ rowStyle+"'>" + 
       "<td><a href='" + arrRoomLink[arrTrnys[i+8]] + "' class='tMonitorPokerRoomName'>carbon poker</a></td>" +
       "<td>" + arrTrnys[i] +
       "<br />" + ShowTime(arrTrnys[i+2]) + "</td>" +
       "<td>" + GetGameType(arrTrnys[i+3]) + "<br>limit" + 
       "<td>" + GetCurrency(arrTrnys[i+6]) + GetBuyInFee(arrTrnys[i+4],arrTrnys[i+5]) + "</td>"+
       "<td>" + GetCurrency(arrTrnys[i+6]) + GetPrize(arrTrnys[i+7]) + "</td>";    
   }

    tmString += "</tr></table>";
    
    document.getElementById('mt').innerHTML = tmString;
    
}

function GetAllowUsImg(type)
{
    if(type == 0)
        return "no";
    else
        return "yes";   
}

function ShowTime(time)
{
    
    spliter1 = new Array();
    dateSpliter = new Array();
    timeSplitter = new Array();
    
    spliter1 = time.split(' ');
        
    dateSpliter = spliter1[0].split('/');
    timeSpliter = spliter1[1].split(':');

// localhost
//    var day = dateSpliter[0];
//    var month = dateSpliter[1];
//    var year = dateSpliter[2];
    
  
  
    var day = dateSpliter[1];
    var month = dateSpliter[0];
    var year = dateSpliter[2];

 // chcking   alert("0:"+day+" 1:"+month+" 2:"+year);
    
    var hours = timeSpliter[0];
    var mins = timeSpliter[1];
    
    var add12hours = 0 ;
    if( (spliter1[2]=="PM") && (hours != 12) )
       add12hours  = 12;
    
    var newDateTime = new Date();
    newDateTime.setUTCFullYear(year,month-1,day);
    newDateTime.setUTCHours(parseInt(hours) + parseInt(add12hours) ,mins,0,0);     



/*
    alert("year :"+year);
    alert(month-1);    
    alert("day :"+day);
    alert("hours :"+hours);
    alert("mins :"+mins);

    alert("DateTimeNow :")
    alert("year :"+ dateTimeNow.getFullYear());
    alert(dateTimeNow.getMonth());    
    alert("day :"+dateTimeNow.getDate());
    alert("hours :"+dateTimeNow.getHours());
    alert("mins :"+dateTimeNow.getMinutes());
    alert("dateTimeNow.toString()"+dateTimeNow.toString() );
*/

    
    timeLeft = newDateTime - dateTimeNow;
    
    if(timeLeft <= 60000)
        return " < 1 minute";
    else
    {
        timeLeft = timeLeft / 60000;
        
        if(timeLeft < 60)
          return Math.round(timeLeft) + " minutes";
        else
        {
             timeLeft = timeLeft / 60;
             
             if(timeLeft < 24)
                 return Math.round(timeLeft) + " hours";
             else
             {
                   timeLeft = timeLeft / 24;
                      return Math.round(timeLeft) + " days";
             }
        }
        return "N/A";
    }
}

function GetGameType(type)
{
   return arrGamesType[type];
}

function GetCurrency(type)
{
    if(type == 1)
        return "$";
    else
      return "";    
}

function GetBuyInFee(num1,num2)
{
    sum = +num1 + +num2;
    
    return sum;
}

function GetPrize(prize)
{
    if(prize == 0)
        return "0";
    else
        return prize;    
}