var elArr 		= new Array();
var openEl;
var timerJS		= null;

function getEl(id)
{
  return document.getElementById(id);
}

// Initializes the page onload.
function init()
{
  setFocus();

  // Get the menu items.
  var el1     = getEl('help');
  var el2     = getEl('about');

  el1.open = false;
  el1.openHeight = el1.clientHeight;
  
  el2.open = false;
  el2.openHeight = el2.clientHeight;

  el1.style.height = 1 +"px";
  el2.style.height = 1 +"px";

  el1.style.visibility = "visible";
  el2.style.visibility = "visible";

  elArr[0] = el1;
  elArr[1] = el2;

  var flash    = getEl('flash');

  flash.style.display = "";
}

function toggle(nr, el)
{
  var txt = el.firstChild.nodeValue;

  if (openEl != null)
  {
    closeText(openEl);
    el.title = "Click here to expand the " +txt;
  }
  if (elArr[nr] != openEl)
  {
    openText(elArr[nr]);
    openEl = elArr[nr];

    el.title = "Click here to collapse the " +txt;
  }
  else
  {
    openEl = null;
  }
}

function openText(el)
{
  t1 = new Tween(el.style,'height',Tween.bounceEaseOut, 1, el.openHeight, 1, 'px');
  t1.start();
  el.open = true;
}

function closeText(el)
{
  t2 = new Tween(el.style, 'height',Tween.bounceEaseOut, el.openHeight, 1, 1, 'px');
  t2.start();
  el.open = false;
}

function sayAgain()
{
  document.forms['form1'].input.value = document.forms['form1'].previous.value;
  document.forms['form1'].submit();
}

function setPreviousInput()
{
  setCookie('previous', document.forms['form1'].input.value);
}

function sayAgain()
{
  var previousValue = getCookie('previous');
  document.forms['form1'].input.value = previousValue;
}

function setCookie( sName, sValue, nDays ) 
{
  var expires = "";
  if ( nDays ) 
  {
    var d = new Date();
    d.setTime( d.getTime() + nDays * 24 * 60 * 60 * 1000 );
    expires = "; expires=" + d.toGMTString();
  }
  document.cookie = sName + "=" + sValue + expires + "; path=/";
};

function getCookie(sName) 
{
  var re = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
  var res = re.exec( document.cookie );
  return res != null ? res[3] : null;
};

function removeCookie( name ) 
{
  setCookie( name, "", -1 );
};

function setFocus()
{
  document.form1.input.select();
  document.form1.input.focus();
}

function xmlhttpPost(strURL, formObj, id) 
{
  if (connectionStatus > 0)
  {
    if (!isSent)
    {
      setPreviousInput(); 
      sendToActionScript('');
    }

    var xmlHttpReq = false;
    var self = this;

    // Mozilla/Safari
    if (window.XMLHttpRequest) 
    {
      self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) 
    {
      self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() 
    {
      if (self.xmlHttpReq.readyState == 0)
      {
        window.status = "Request is not initialized";
      }
      if (self.xmlHttpReq.readyState == 1)
      {
        window.status = "Request has been set up";
      }
      if (self.xmlHttpReq.readyState == 2)
      {
        window.status = "Request has been sent";
        document.form1.input.value = "";
      }
      if (self.xmlHttpReq.readyState == 3)
      {
        window.status = "Request in progress";
      }
      if (self.xmlHttpReq.readyState == 4) 
      {
        window.status = "Request completed";
        getEl(id).innerHTML = self.xmlHttpReq.responseText;
        timerJS = window.setInterval(function()
        {
          if (getEl(id).firstChild != null)
          {
            clearInterval(timerJS);
            var customwindow = document.getElementsByTagName("DISPLAY");


            for (var i = 0; i < customwindow.length; i++)
            {
              var theURL = customwindow[i].firstChild.nodeValue;
              window.open(theURL, 'WINDOW', 'width=800, height=600,status=yes,toolbar=yes,menubar=yes');
            }
          }

        }, 1000);
      }
    }
    self.xmlHttpReq.send(collectForm(formObj)); 
  }
}

function collectForm(formObj)
{
  var str = 'sid=' +Math.random()+ '&'; // botname=Dave1&';
  var frmElements = formObj.elements;

  if (formObj == document.form1)
  {
    for (var i = 0; i < frmElements.length; i++)
    {
      str += frmElements[i].name +'='+ frmElements[i].value;
      if (i < frmElements.length -1) str += '&';
    }
  }
  return str;
}

window.onload = init;
