(document.getElementById) ? dom = true : dom = false;

/*####################################################
##                CREDITS / DISCLAIMER               #
## Vortex's Master Site CGI program v1.0 alpha       #
## Designed Exclusively for Helen Carames            #
##                                                   #
## For use at http://www.worldbynight.net,           #
## this entire file and associated configuration     #
## files are copywritten material of Vortex Studios, #
## Alexander Grosstephan, Helen Carames, and         #
## worldbynight.net                                  #
##                                                   #
## Distribution and/or use of, or any part of, this  #
## file is strictly forbidden. Punishable by law.    #
####################################################*/

var verify = null;
var userRoot = null;
var userLast = null;
var userData = null;
var userPass = null;
var userName = null;
var userKey = null;
var userResult = null;
var userAdmin = null;
var userMesg = null;
var userFile = null;
var menuType = null;
var menuStyle = null;
var menuMesg = null;
var controlState = null;

function checkWait(inState) {
  if ( inState == 'stop' && controlState == 'active' ) { controlState="dead" }
  else if ( inState == 'none' && controlState == 'active' ) { return; }
  else if ( inState == 'stop' && controlState != 'active' ) { controlState="dead" }
  else if ( inState == 'none' && controlState != 'active' ) { controlState="dead" }
  else if ( inState == 'timeout' ) { inState="null"; self.frames['track'].location.href = 'tracker.cgi'; }
  else if ( inState == 'pass' && controlState == 'active' ) { self.frames['events'].location.href = 'events.cgi'; }
  else if ( inState == 'first' && controlState == 'active' ) { self.frames['events'].location.href = 'events.cgi?func=first'; } } //this line could be removed?

function catchEvent(inCount,inWhere,inWho,inWhen,inNow,inWhat) {
  if (inWho == "system") {
    if (inWhat == null) { inWhat = 4; }
    var tmp = String(inWhere);
    elert(tmp,inWhat,'push'); }
  else {
    if (inWhat == null) { inWhat = 1; }
    var tmp = inWho+': '+'Caused event '+inWhere+' at '+inWhen+'<br>';
    tmp += 'It is currently '+inNow+'<br>';
    elert(tmp,inWhat,'push'); } }

function pushEvent() { self.frames['events'].location.href = 'events.cgi?func=push'; }
function pauseEvent() { self.frames['events'].location.href = 'events.cgi?func=wait'; }
function softEvent() { self.frames['events'].location.href = 'events.cgi'; }

function runEvent(inData,inUser,inPass,inAdmin,inMesg) {
  userName = inUser;
  userPass = inPass;
  userResult = inData;
  userAdmin = inAdmin;
  userKey = inAdmin;
  userMesg = String(inMesg);
  if (userResult == 'fail') {
    if (userMesg != '' && userMesg != null) { document.getElementById('loginmesg').value=userMesg; }
    userResult="null";
    userPass="null";
    controlState="dead";
    document.getElementById('passtext').value="";
    pauseEvent();
    hideControl();
    if (userMesg == "Login Expired" && inPass != "cut") { startLogin(); }
    else if (userMesg == "Verification Failure") { cancelLogin(); }
    checkFrames(); }
  else if (userResult == 'pass') {
    document.getElementById('loginmesg').value="Success";
    document.getElementById('passtext').value="";
    showControl(); }
  else if (userResult == 'logout') {
    document.getElementById('loginmesg').value=userMesg;
    userResult="null";
    userPass="null";
    controlState="dead";
    document.getElementById('passtext').value="";
    pauseEvent();
    hideControl();
    cancelLogin(); } }

function showControl() {
  hideLogin(); hideLbutton();
// Control Bar -->
  var controlout='<TABLE cellpadding="0" class="control"><TR><TD valign="top">';
  controlout += '<DIV class="controlbutton"><A onClick="skiptoMenu(\'user\');" class="control" alt=""><SPAN>Menu</SPAN></A>';
  if ( userAdmin >= 1 ) {
  controlout += '<A onClick="openMenu(\'admin\');" class="control" alt=""><SPAN>Admin</SPAN></A>'; }
  controlout += '</DIV></TD><TD width="100%" height="100%" valign="top"><DIV class="controlbutton"><A class="control" alt="" href="tracker.cgi?func=logout" target="track"><SPAN>Logout</SPAN></A></DIV>';
  controlout += '</TD></TR></TABLE></DIV>';
// <--
  document.getElementById('controlFrame').innerHTML=controlout;
  if (dom) {document.getElementById('controlFrame').style.visibility='visible';}
  if (document.layers) {document.layers['controlFrame'].visibility='show';}
  controlState = "active"; checkWait('first'); } // may not need 'first'?

function showMenu(inType) {
  if (inType != userLast) {
    if (dom) {document.getElementById('menuFrame').style.visibility='hidden';}
    if (document.layers) {document.layers['menuFrame'].visibility='hide';} }
  var label;
  if (userKey == 1) { label="Chat Moderator"; } // Room Only Control Ranks
  if (userKey == 2) { label="Game Moderator"; }
  if (userKey == 3) { label="Game Master"; }
  if (userKey == 4) { label="Site Moderator"; } // Sitewide Control Ranks
  if (userKey == 5) { label="Administrator"; }
  if (userKey == 6) { label="Management"; }
  if (userKey >= 7 && userKey <= 96) { label="Error: Invalid"; } // Catcher for unused Ranks
  if (userKey == 97) { label="Edditor"; } // Alex's Rank
  if (userKey == 98) { label="Owner"; } // Helen's Rank
  if (userKey == 99) { label="Console"; } // Used for build/debug process during Developement
  var tmpLtr = userName.substr(0,1);
  var tmpWrd = userName.substr(1);
  var capName = tmpLtr.toUpperCase() + tmpWrd;
// Menus -->
  if (inType == 'user') { userLast="user";
    var out='<table class=\"dialog\" cellspacing=\"0\" width=\"180\"><tr><th class=\"dialog\">Options</th></tr><tr height=\"100%\"><td class=\"dialog\"><div class=\"buttonize\">Options For '+capName+'<br> '+userKey+'<br><br><p align=\"right\"><a class=\"blanklink\"><span onClick=\"window.top.closeMenu();\">Cancel</span></a></p></div></td></tr></table>';
    document.getElementById('menuFrame').innerHTML=out; }
  else if (inType == 'admin') { userLast="admin";
    var out='<table class=\"dialog\" cellspacing=\"0\" width=\"180\"><tr><th class=\"dialog\">'+label+' Menu</th></tr><tr height=\"100%\"><td class=\"dialog\"><table width="100%" align=\"left\" cellspacing=\"0\" cellpadding=\"0\"><tr><td valign=\"middle\" width=\"100%\"><form><div class=\"buttonize\"><br>';
    if (userKey >= 4) { out += '<a class=\"blanklink\"><span onClick=\"window.top.skiptoMenu(\'site\');\">Site Options</span></a><br>'; }
    out += '<br>Target IP or Account<br>&nbsp;<input class=\"logininput\" type=\"text\" name=\"finduserin\" maxlength=\"20\"><br><br><a class=\"blanklink\"><span onClick=\"window.top.startFind(\'user\');\">Find User</span></a><br><br>Target Chat or Game<br>&nbsp;<input class=\"logininput\" type=\"text\" name=\"findroomin\" maxlength=\"20\"><br><br><a class=\"blanklink\"><span onClick=\"window.top.startFind(\'room\');\">Find Room</span></a><br><br><br><p align=\"right\"><a class=\"blanklink\"><span onClick=\"window.top.closeMenu();\">Cancel</span></a></p></div></form></td></tr></table></td></tr></table>';
    document.getElementById('menuFrame').innerHTML=out; }
  else if (inType == 'site') { userLast="site";
    var out='<table class=\"dialog\" cellspacing=\"0\" width=\"180\"><tr><th class=\"dialog\">'+label+' Menu</th></tr><tr height=\"100%\"><td class=\"dialog\"><div class=\"buttonize\">Site Options<br><br>Test Alert Messages<br><A onClick=\"window.top.elert(\'Error Box\',\'0\',\'\');\" class=\"blanklink\">0</A> <A onClick=\"window.top.elert(\'One\',\'1\',\'\');\" class=\"blanklink\">1</A> <A onClick=\"window.top.elert(\'Two\',\'2\',\'\');\" class=\"blanklink\">2</A> <A onClick=\"window.top.elert(\'Three\',\'3\',\'\');\" class=\"blanklink\">3</A> <A onClick=\"window.top.elert(\'Four\',\'4\',\'\');\" class=\"blanklink\">4</A> <br><br><p align=\"right\"><a class=\"blanklink\"><span onClick=\"window.top.closeMenu();\">Cancel</span></a></p></div></td></tr></table>';
    document.getElementById('menuFrame').innerHTML=out; }
// <--
  var getVis = null;
  if (dom) {getVis=document.getElementById('menuFrame').style.visibility;}
  if (document.layers) {getVis=document.layers['menuFrame'].visibility;}
  if ((controlState == "active") && (getVis == "hidden" || getVis == "hide")) {
    if (dom) {document.getElementById('menuFrame').style.visibility='visible';}
    if (document.layers) {document.layers['menuFrame'].visibility='show';} }
  else if ((controlState == "active") && (getVis == "visible" || getVis == "show")) {
    if (dom) {document.getElementById('menuFrame').style.visibility='hidden';}
    if (document.layers) {document.layers['menuFrame'].visibility='hide';} } }

function openMenu(inType) { runVerify('test','menu',userName,userPass,userKey,inType); }

function skiptoMenu(inType) { self.frames['track'].location.href = 'tracker.cgi?func=update'; showMenu(inType); }

function closeMenu() {
    if (dom) {document.getElementById('menuFrame').style.visibility='hidden';}
    if (document.layers) {document.layers['menuFrame'].visibility='hide';} }

function runVerify(inVerify,inRoot,inUser,inPass,inAdmin,inMesg) {
  verify = null;
  userKey = inAdmin;
  if (inVerify == "test") { self.frames['track'].location.href = 'tracker.cgi?func=verify&root='+inRoot+'&data='+inMesg; }
  else if (inVerify == "yes") {
    userRoot=inRoot;
    userData=inMesg;
    verify="yes";
    userKey=inAdmin;
    if (userAdmin != userKey) {
      var msg = "Your rank has been \'";
      if ( userKey > userAdmin ) { msg += "Promoted"; }
      else if ( userKey < userAdmin ) { msg += "Demoted"; }
      msg += "\'<br>By an Administrator!";
      elert(msg,3,''); userAdmin=userKey; }
    if (userRoot == "menu") { showMenu(userData); }
    else if (userRoot == "event") { checkWait("pass"); } }
  else { verify="no"; var msg="User verification failure.<br><br>One or more security checks returned <i>invalid</i>.<br><br>Please contact an administrator, this problem is due to either a corrupt user file, unexpected deletion/blocking of cookie files, or tampering with i/o functions maliciously.";
  elert(msg,4,''); self.frames['track'].location.href = 'tracker.cgi?func=kill'; } }

function startLogin() { hideLbutton(); showLogin(); }

function cancelLogin() { hideLogin(); showLbutton(); }

function checkFrames() {
  var checkLbut = null;
  var checkLwin = null;
  if (dom) {
    if (document.getElementById('loginFrame').style.visibility == 'hidden') { checkLwin="no"; }
    if (document.getElementById('buttonLogin').style.visibility == 'hidden') { checkLbut="no"; } }
  else if (document.layers) {
    if (document.layers['loginFrame'].visibility == 'hide') { checkLwin="no"; }
    if (document.layers['buttonLogin'].visibility == 'hide') { checkLbut="no"; } }
  if (checkLwin == "no" && checkLbut == "no") { showLbutton(); } }

function hideControl() {
  if (dom) {document.getElementById('controlFrame').style.visibility='hidden';}
  if (document.layers) {document.layers['controlFrame'].visibility='hide';}
  if (dom) {document.getElementById('menuFrame').style.visibility='hidden';}
  if (document.layers) {document.layers['menuFrame'].visibility='hide';} }

function hideLogin() {
  if (dom) {document.getElementById('loginFrame').style.visibility='hidden';}
  if (document.layers) {document.layers['loginFrame'].visibility='hide';} }

function showLogin() {
  if (controlState != "active") {
    if (dom) {document.getElementById('loginFrame').style.visibility='visible';}
    if (document.layers) {document.layers['loginFrame'].visibility='show';} } }

function showLbutton() {
  if (dom) {document.getElementById('buttonLogin').style.visibility='visible';}
  if (document.layers) {document.layers['buttonLogin'].visibility='show';} }

function hideLbutton() {
  if (dom) {document.getElementById('buttonLogin').style.visibility='hidden';}
  if (document.layers) {document.layers['buttonLogin'].visibility='hide';} }

function elert(msg,lvl,going) {
  var label; var cls;
  if ( lvl == 0 ) { label="Error!"; cls="ElertRed";}
  else if ( lvl == 1 ) { label="Message"; cls="ElertGrn";}
  else if ( lvl == 2 ) { label="Attention!"; cls="ElertYwl";}
  else if ( lvl == 3 ) { label="Alert!"; cls="ElertOrn";}
  else if ( lvl >= 4 ) { label="Warning!"; cls="ElertRed";}
  else { label="Message"; cls="ElertGrn";} // Default
  var out='<TABLE class=\"loginscreen\"><TR><TD width=\"100%\" align=\"center\">';
  out+='<table class=\"'+cls+'\" cellspacing=\"0\" width=\"180\"><tr><th class=\"'+cls+'\">'+label+'</th></tr><tr height=\"100%\"><td class=\"'+cls+'\"><div class=\"ElertBtn\">'+msg+'<br><br><p align=\"right\"><a class=\"blanklink\"><span onClick=\"window.top.Xelert(\''+going+'\');\">Close</span></a></p></div></td></tr></table>';
  out+='</TABLE>';
  document.getElementById('elertFrame').innerHTML=out;
  if (dom) {document.getElementById('elertFrame').style.visibility='visible';}
  if (document.layers) {document.layers['elertFrame'].visibility='show';}
  return; }

function Xelert(going) {
  if (dom) {document.getElementById('elertFrame').style.visibility='hidden';}
  if (document.layers) {document.layers['elertFrame'].visibility='hide';} 
  if (going == "push") { pushEvent(); }
}

