var domain_name = "";

function doLookup(domain_name, security_code)
{
  document.getElementById("cell1").style.visibility = "visible";
  document.getElementById("cell2").style.visibility = "visible";
  document.getElementById("cell3").style.visibility = "visible";
  document.getElementById("cell4").style.visibility = "visible";
  getThumbnail(domain_name, security_code); 
  getWebInfo(domain_name, security_code);
  getGraph(domain_name);
  getServerInfo(domain_name, security_code);
  getWhoisInfo(domain_name, security_code);
}

function runFromQueryString()
{
  if(getQueryStrData() != "emptyStr")
  {
    domain_name = getQueryStrData();
    //getCaptcha("first");
    doLookup(domain_name, null);
  }
}

function runFromButtonEvent()
{
  if(document.getElementById("domain_name").value != "")
  {
    domain_name = document.getElementById("domain_name").value;
    //getCaptcha("first");
    doLookup(domain_name, null);
  }
  else
  {
    alert("Empty! Please type in a domain name and click Lookup");
  }
}

function getCaptcha(howmanytries)
{
  var buttonVal = null;
  var formCell = document.getElementById("featureBoxWide");
  var captchaRow;
  if(howmanytries == "first")
  {
    buttonVal = "Continue";
  }
  else if(howmanytries == "second")
  {
    buttonVal = "Try Again";
  }
  captchaRow = '<table id="formCell" cellpadding="0" cellspacing="0"><tr><td style="text-align:right;"><img src="http://latin.instra.com/whoisengine/captcha.jpg.php?width=120&height=30&character=6&font_size=22&bgcolor=6D9BF1&fgcolor=4E78A0?' + Math.random() + '"></td><td style="width:270px;"><input style="width:250px; height:30px; font-size:20px; padding-left:3px;" type="text" id="security_code" name="security_code" /></td><td style="text-align:left; width:150px;"><input style="width:100px; height:30px; font-size:16px; font-weight:bold;" type="button" value="' + buttonVal + '" onclick="processLookup()"/></td></tr></table>';
  formCell.innerHTML = captchaRow;
}

function drawSearchPanel()
{
  var formCell = document.getElementById("featureBoxWide");
  var searchRow = '<table id="formCell" cellpadding="0" cellspacing="0"><tr><td style="text-align:right; font-size:20px; color:black; width:150px;">www.</td><td style="width:270px;"><input style="width:250px; height:30px; font-size:20px; padding-left:3px;" type="text" id="domain_name" name="domain_name" /></td><td style="text-align:left; width:150px;"><input style="width:100px; height:30px; font-size:16px; font-weight:bold;" type="button" value="Lookup" onclick="runFromButtonEvent()"/></td></tr></table>';
  formCell.innerHTML = searchRow;
}

function processLookup()
{
  if(document.getElementById("security_code").value != "")
  {
    var security_code = document.getElementById("security_code").value;
    doLookup(domain_name, security_code);
  }
  else
  {
    alert("Empty! Please type in the security code and click Continue");
  }
}

function getThumbnail(domain_name, security_code)
{ 
  insertLoadImage("thumbnail");
  var oXmlHttp = new createXMLHttp();
  oXmlHttp.open("GET","whoisengine/request/alexathumbnail.php?domain_name=" + domain_name + "&security_code=" + security_code, true);
  oXmlHttp.onreadystatechange = function()
  {
    if(oXmlHttp.readyState == 4)
    {
      if(oXmlHttp.status == 200)
      {
        displayThumbnail(oXmlHttp.responseText);
      }
      else
      {
        displayThumbnail("Failed to retrive thumbnail " + oXmlHttp.statusText); 
      }
    }
  };
  oXmlHttp.send(null);
}

function getGraph(domain_name)
{
  insertLoadImage("alexagraphinfo");
  var graphImg = '<img src="http://traffic.alexa.com/graph?w=700&h=280&r=6m&y=t&u='+ domain_name + '" />';
  displayGraph(graphImg);
}

function getWebInfo(domain_name, security_code)
{
  insertLoadImage("webinfo");
  var oXmlHttp = new createXMLHttp();
  oXmlHttp.open("GET","whoisengine/request/alexawebinfo.php?domain_name=" + domain_name + "&security_code=" + security_code, true);
  oXmlHttp.onreadystatechange = function()
  {
    if(oXmlHttp.readyState == 4)
    {
      if(oXmlHttp.status == 200)
      {
        displayWebInfo(oXmlHttp.responseText);
      }
      else
      {
        displayWebInfo("Failed to retrieve Web Information " + oXmlHttp.statusText);
      }
    }
  };
  oXmlHttp.send(null);
}

function getServerInfo(domain_name, security_code)
{
  insertLoadImage("serverinfo");
  var oXmlHttp = new createXMLHttp();
  oXmlHttp.open("GET","whoisengine/request/serverinfo.php?domain_name=" + domain_name + "&security_code=" + security_code, true);
  oXmlHttp.onreadystatechange = function()
  {
    if(oXmlHttp.readyState == 4)
    {
      if(oXmlHttp.status == 200)
      {
        displayServerInfo(oXmlHttp.responseText);
      }
      else
      {
        displayServerInfo("Failed to retrieve Server Information " + oXmlHttp.statusText);
      }
    }  
  };
  oXmlHttp.send(null);
}

function getWhoisInfo(domain_name, security_code)
{
  insertLoadImage("whoisinfo");
  var oXmlHttp = new createXMLHttp();
  oXmlHttp.open("GET","whoisengine/request/whoisinfo.php?domain_name=" + domain_name + "&security_code=" + security_code, true);
  oXmlHttp.onreadystatechange = function()
  {
    if(oXmlHttp.readyState == 4)
    {
      if(oXmlHttp.status == 200)
      {
        displayWhoisInfo(oXmlHttp.responseText);
      }
      else
      {
        displayWhoisInfo("Failed to retrieve Whois " + oXmlHttp.statusText);
      }
    }
  };
  oXmlHttp.send(null);
}

function displayThumbnail(data)
{
  var thumbnailcell = document.getElementById("thumbnail");
  thumbnailcell.innerHTML = data;
}

function displayWebInfo(data)
{
  var webinfocell = document.getElementById("webinfo");
  webinfocell.innerHTML = data;
}

function displayGraph(data)
{
  var graphcell = document.getElementById("alexagraphinfo");
  if(graphcell != null)
  {
    graphcell.innerHTML = data;
  }
}

function displayServerInfo(data)
{
  var servercell = document.getElementById("serverinfo");
  servercell.innerHTML = data;
}

function displayWhoisInfo(data)
{
  var whoiscell = document.getElementById("whoisinfo");
  whoiscell.innerHTML = data;
}

function createXMLHttp()
{
  if(typeof XMLHttpRequest != "undefined")
  {
    return new XMLHttpRequest();
  }
  else if(window.ActiveXObject)
  {
    var aVersion = [ "MSXML2.XMLHttp.5.0",
                     "MSXML2.XMLHttp.4.0",
                     "MSXML2.XMLHttp.3.0",
                     "MSXML.XMLHttp",
                     "Microsoft.XMLHttp"];
    for (var i = 0; i < aVersion.length; i++)
    {
      try
      {
        var oXmlHttp = new ActiveXObject(aVersion[i]);
        return oXmlHttp;
      }
      catch(oError)
      {
        //Do nothing
      }
    }
  }
  throw new Error("XMLHttp object could not be created");
}

function insertLoadImage(cellname)
{
  var cell = document.getElementById(cellname);
  var loading = "<table width='100%' height='100%'><tr><td align='center' valign='middle'><img src='../whoisengine/loading.gif'><br />Loading...</td></tr> </table>";
  if(cell != null)
  {
    cell.innerHTML = loading;
  }
}

function getQueryStrData()
{
  var que = unescape(location.search);
  var que = que.substring(1, que.length);
  var domain_name = que.substring((que.indexOf("=")) + 1);
  if(domain_name == "")
  {
    domain_name = 'emptyStr';
  }
  return domain_name;
}

