function WriteDbMenu(username) {
  document.writeln('<center><font size=-1 color=ffe888>');
  document.writeln('<a href=index.php?p=view>VIEW</a> | ');
  document.writeln('<a href=index.php?p=new>NEW</a> | ');
  if (username == '') {
    document.writeln('<a href=index.php?p=login>LOGIN</a> | ');
  } else {
    if (username.length > 15) {
      username = username.substring(0,15) + '~';
    }
    document.writeln('<a href=index.php>'+username.toUpperCase()+'</a> | ');
  }
  document.writeln('<a href=index.php?p=gen>BUILD</a>');
  document.writeln('</font>&nbsp;</center>');
  document.writeln('');
}

function optional() {
  if (navigator.userAgent.indexOf("Safari") > 0) {
    return '';
  }
  return '<font size=-2>(optional)</font>';
}

function formatCode(code) {
  return code.substring(0,4) + '-' + code.substring(4,8) + '-' + code.substring(8,12) + '-' + code.substring(12,16);
}

function wrapCode(code, utility, pretext, posttext) {
  //wrap the code in a table so that triple-click will select just the code
  if (pretext == null) {
    pretext = '';
  } else {
    pretext = '<td>' + pretext + '</td>';
  }
  if (posttext == null) {
    posttext = '';
  } else {
    posttext = '<td>' + posttext + '</td>';
  }
  if (utility == null) {
    utility = '';
  }
  code = formatCode(code);
  utility = '<td>' + CopyHeroLink(code) + utility + '</td>';
  return '<table cellpadding=0 cellspacing=0 border=0 align=top><tr valign=bottom>'+pretext+'<td><font face=FixedSys><span class="rankcode">' + code + '</span></font></td>' + utility + posttext + '</tr></table>';
}

function ColorRankCodes() {
  var code = window.clipboardData.getData('Text');
  if (code != null)
    if (code.length == 16)
      code = formatCode(code);
    else if (code.length == 20) //there tends to be an extra space on the end when you manually copy in the browser
      code = code.substring(0,19); //remove it
  var codes = document.getElementsByTagName("span");
  for (var i=0; i < codes.length; i++)
    if (codes[i].className == "rankcode")
      if (codes[i].innerText == code)
        codes[i].style.color = "lime";
      else
        codes[i].style.color = "white";
}

function ShowCodeCopyButtons() {
  if (!window.clipboardData)
    return;
  if (!document.getElementById)
    return;
  ColorRankCodes();
  var btns = document.getElementsByTagName("span");
  for (var i=0; i < btns.length; i++)
    if (btns[i].className == "copycode")
      btns[i].style.display = "";
}

function CopyCode(code) {
  window.clipboardData.setData('Text',code);
  ColorRankCodes();
}

function BadCredentials(generate) {
  document.writeln('Unknown player or incorrect password to access that player.');
  WriteBr();
  WriteBr();
  if (generate) {
    WriteGenerateLogin('');
  } else {
    WritePlayerLogin('');
  }
}

function BadSearch() {
  document.writeln('No players found matching search criteria.');
}

function BadNew(player, exists) {
  if (!exists) {
    document.writeln('Invalid player name or password.');
  } else {
    document.writeln('Player <font color=ffe888>' + player + '</font> already exists. You must specify a player name that is not already taken.');
  }
  WriteBr();
  WriteBr();
  WritePlayerAdd();
}

function GoodNew(player) {
  document.writeln('You have successully created a new player named <font color=ffe888>' + player + '</font>.<br>');
  document.writeln('<font size=-1><i><a href=index.php?p=login&user='+player+'>Click here to login.</a></i></font>');
}

function TitleDesc(title) {
  //Yeah, you know their names now. You're so l33t.
  //I could have hid them if I wanted to.
  if (title == 0) { return 'Honor Guard'; }
  if (title == 1) { return 'Valiant Martyr'; }
  if (title == 2) { return 'City Saviour'; }
  if (title == 3) { return 'Living Legend'; }
  if (title == 4) { return 'Monster Slayer'; }
  if (title == 5) { return 'Vigilant Hero'; }
  if (title == 6) { return 'Core Elite'; }
  if (title == 7) { return 'Lone Ranger'; }
  if (title == 8) { return 'Hardened Veteran'; }
  if (title == 9) { return 'Renowned Veteran'; }
  if (title ==10) { return 'Highly Decorated'; }
  if (title ==11) { return 'Keymaster'; }
  if (title ==12) { return 'Swat Centurion'; }
  if (title ==13) { return 'Infinite Legion'; }
  return '';
}

function ListTitles(titles, twolines) {
  var s = '';
  var i = 0;
  while (i < 14) {
    if (titles.substr(i,1) != '0') {
      s = s + '<td width=9><img src=pics/title'+i+'.gif title="'+TitleDesc(i)+'"></td>';
    } else {
      s = s + '<td width=9><img src=pics/titled.gif title="unearned"></td>';
    }
    if (twolines && (i == 6)) {
      s = s + '</tr><tr height=9>';
    }
    i = i + 1;
  }
  return '<table cellpadding=0 cellspacing=3 border=0><tr height=9>' + s + '</tr></table>';
}

function WriteTitles(titles) {
  document.writeln(ListTitles(titles));
  if (titles == 0) {
    document.writeln('&nbsp;<font size=-2>You must join the Honor Guard in order to earn titles, which requires that you first delete all of your heroes.</font><br>');
  }
  document.writeln('<br>');
}

function PlayerIcon(player, icon, honorguard, inactive) {
  var d = '';
  var t = '';
  if (inactive) { d = 'd'; }
  if (honorguard) { t = '<br><img src=pics/title.gif title="'+TitleDesc(0)+'">'; }
  return '<a href=index.php?p=view&user='+player+'><img src=pics/class'+icon+d+'.gif width=31 height=31 border=0></a><br><img src=/pics/empty.gif width=1 height=2>' + t;
}

function PlayerIconEmpty() {
  return '<img src=/pics/empty.gif width=31 height=31 border=0><br><img src=/pics/empty.gif width=1 height=1>';
}

function PlayerDesc(player, clan, rank, description) {
  var playername = player;
  if (playername.length > 15) {
    playername = playername.substring(0,15) + '~';
  }
  return '<a href=index.php?p=view&user='+player+'><b>'+playername+'</b></a><br><font color=ffe888 face=Arial size=-1>'+rank+clan+'</font><br>'+description;
}

function RankDesc(rank) {
  if (rank == 12) { return 'Legendary Hero'; }
  if (rank == 11) { return 'National Hero'; }
  if (rank == 10) { return 'Chief'; }
  if (rank == 9) { return 'Deputy Chief'; }
  if (rank == 8) { return 'Commander'; }
  if (rank == 7) { return 'Captain'; }
  if (rank == 6) { return 'Lieutenant'; }
  if (rank == 5) { return 'Sergeant'; }
  if (rank == 4) { return 'Detective'; }
  if (rank == 3) { return 'Officer III'; }
  if (rank == 2) { return 'Officer II'; }
  if (rank == 1) { return 'Officer I'; }
  return 'Cadet';
}

function DiffDesc(rank, diff) {
  if (rank >11) { return ' (12/12)'; }
  if (diff > 3) { return ' ('+rank+'/11)'; }
  if (diff > 2) { return ' ('+rank+'/10)'; }
  if (diff > 1) { return ' ('+rank+'/9)'; }
  if (diff > 0) { return ' ('+rank+'/6)'; }
  return ' ('+rank+'/3)';
}

var colCur = 0;
var colMax = 3;
var colSize = '32%';
var colSpce = '2%';
var rowData;
function WriteDbRow(icon, data) {
  if (icon == '') {
    icon = PlayerIconEmpty();
    data = '&nbsp';
  }
  if (colCur == 0) {
    rowData = '<table cellpadding=0 cellspacing=0 border=0 width=100%><tr valign=top>';
  }
  colCur = colCur + 1;
  rowData = rowData + '<td align=right>'+icon+'</td><td width=4>&nbsp;</td><td width='+colSize+'>' + data + '</td>';
  if (colCur < colMax) {
    rowData = rowData + '<td width='+colSpce+'></td>';
  } else {
    rowData = rowData + '</tr></table>';
    WriteOldRow('',rowData);
    colCur = 0;
  }
}

function WritePlayer(player, clan, icon, rank, description, honorguard, inactive, corrupt) {
  WriteDbRow(PlayerIcon(player, icon, honorguard, inactive), PlayerDesc(player, clan, ListRankDesc(rank, inactive, corrupt), description));
}

function WriteDbFooter() {
  while (colCur > 0) {
    WriteDbRow('');
  }
  WriteOldFooter();
}

function ListPlayerIcon(icon, inactive) {
  var d = '';
  if (inactive) { d = 'd'; }
  return '<img src=pics/class'+icon+d+'.gif width=31 height=31 border=0><br><img src=/pics/empty.gif width=1 height=1>';
}

function ListRankDesc(rank, inactive, corrupt) {
  retired = '';
  if (corrupt) { retired = 'Corrupt '; }
  else if (inactive) { retired = 'Retired '; }
  if (rank ==11) { return retired + 'Immortalized Hero'; }
  if (rank ==10) { return retired + 'Legendary Hero'; }
  if (rank == 9) { return retired + 'National Hero'; }
  if (rank == 8) { return retired + 'Chief'; }
  if (rank == 7) { return retired + 'Deputy Chief'; }
  if (rank == 6) { return retired + 'Commander'; }
  if (rank == 5) { return retired + 'Captain'; }
  if (rank == 4) { return retired + 'Lieutenant'; }
  if (rank == 3) { return retired + 'Sergeant'; }
  if (rank == 2) { return retired + 'Detective'; }
  if (rank == 1) { return retired + 'Officer'; }
  if (corrupt)   { return 'Dropout'; }
  if (!inactive) { return 'Cadet'; }
  return 'Civilian';
}

function ListPlayer(player, clan, icon, standing, rank, inactive, titles, description, corrupt) {
  var s = '<table cellpadding=0 cellspacing=0 border=0><tr valign=top>' +
          '<td align=right>'+ListPlayerIcon(icon,inactive)+'</td><td width=4>&nbsp;</td><td width=100%>' +
          '<font color=ffcc00><b>'+player+'</b></font><br><font color=ffe888 face=Arial size=-1>'+ListRankDesc(rank,inactive,corrupt)+clan+'</font><br>' +
          description + '</td><td align=right><font color=ffcc00><b>Standing:&nbsp;</b></font>'+standing+'&nbsp;'+ListTitles(titles,true)+'</td><td width=10></td>' +
          '</tr></table><br>';
  WriteOldHeader();
  rowHighlight = false;
  WriteOldRow('', s);
  rowHighlight = false;
  WriteDbFooter();
}

var linkBase = 'http://redscull.com/swat/';
function LinkClass(hero) {
  var link = '';
  if (hero =='a'){ link = 'tech'; } else
  if (hero == 0) { link = 'sniper'; } else
  if (hero == 1) { link = 'medic'; } else
  if (hero == 2) { link = 'tact'; } else
  if (hero == 3) { link = 'psy'; } else
  if (hero == 4) { link = 'mav'; } else
  if (hero == 5) { link = 'hvyord'; } else
  if (hero == 6) { link = 'demo'; } else
  if (hero == 7) { link = 'cyborg'; } else
  if (hero == 8) { link = 'pyro'; } else
  if (hero == 9) { link = 'watchman'; } else
  { return linkBase+'readmeafterclasses.html'; } 
  return linkBase+'classinfo-'+link+'.htm';
}

function LinkGun(val) {
  var link = '';
  if (val == 0) { link = 'wassault'; } else
  if (val == 1) { link = 'wsniper'; } else
  if (val == 2) { link = 'wchain'; } else
  if (val == 3) { link = 'wrocket'; } else
  if (val == 4) { link = 'wflame'; } else
  if (val == 5) { link = 'wrifle'; } else
  if (val == 6) { link = 'wgatling'; } else
  { return linkBase+'readmeafterweapons.html'; } 
  return linkBase+'readmeafterweapons.html#'+link;
}

function LinkArmor(val) {
  var link = '';
  if (val == 0) { link = 'Compact'; } else
  if (val == 1) { link = 'Standard'; } else
  if (val == 2) { link = 'Large'; } else
  if (val == 3) { link = 'Large'; } else
  { return linkBase+'readmeafterarmor.html'; } 
  return linkBase+'readmeafterarmor.html#'+link;
}

function LinkCfg(name, val) {
  if (name == 'class') { return LinkClass(val); }
  if (name == 'gun') { return LinkGun(val); }
  if (name == 'armor') { return LinkArmor(val); }
  if (name == 'trait') { return linkBase+'readmeaftertraits.html'; }
  if (name == 'spec') { return linkBase+'readmeafterspecs.html'; }
  if (name == 'talent') { return linkBase+'readmeaftertalents.html'; }
  return linkBase;
}

function HintClass(hero) {
  if (hero =='a'){ return 'Tech Ops'; }
  if (hero == 0) { return 'Covert Sniper'; }
  if (hero == 1) { return 'Field Medic'; }
  if (hero == 2) { return 'Tactician'; }
  if (hero == 3) { return 'Psychologist'; }
  if (hero == 4) { return 'Maverick'; }
  if (hero == 5) { return 'Heavy Ordnance'; }
  if (hero == 6) { return 'Demolitions'; }
  if (hero == 7) { return 'Cyborg'; }
  if (hero == 8) { return 'Pyrotechnician'; }
  if (hero == 9) { return 'Watchman'; }
  return '';
}

function HintGun(val) {
  if (val == 0) { return 'Assault Rifle'; }
  if (val == 1) { return 'Sniper Rifle'; }
  if (val == 2) { return 'Chaingun'; }
  if (val == 3) { return 'Rocket Launcher'; }
  if (val == 4) { return 'Flamethrower'; }
  if (val == 5) { return 'Laser Rifle'; }
  if (val == 6) { return 'Gatling Laser'; }
  return '';
}

function HintArmor(val) {
  if (val == 0) { return 'Light'; }
  if (val == 1) { return 'Medium'; }
  if (val == 2) { return 'Heavy'; }
  if (val == 3) { return 'Advanced'; }
  return '';
}

function HintTrait(val) {
  if (val == 0) { return 'Skilled'; }
  if (val == 1) { return 'Gifted'; }
  if (val == 2) { return 'Survivalist'; }
  if (val == 3) { return 'Dragoon'; }
  if (val == 4) { return 'Acrobat'; }
  if (val == 5) { return 'Swift Learner'; }
  if (val == 6) { return 'Healer'; }
  if (val == 7) { return 'Flower Child'; }
  if (val == 8) { return 'Chem. Reliant'; }
  if (val == 9) { return 'Rad. Resistant'; }
  if (val ==10) { return 'Gadgeteer'; }
  if (val ==11) { return 'Prowler'; }
  if (val ==12) { return 'Energizer'; }
  if (val ==13) { return 'Pack Rat'; }
  if (val ==14) { return 'Engineer'; }
  if (val ==15) { return 'Reckless'; }
  return '';
}

function HintSpec(val) {
  if (val == 0) { return 'Weaponry'; }
  if (val == 1) { return 'Power Armor'; }
  if (val == 2) { return 'Energy Cells'; }
  if (val == 3) { return 'Cybernetics'; }
  if (val == 4) { return 'Triage'; }
  if (val == 5) { return 'Chemistry'; }
  if (val == 6) { return 'Leadership'; }
  if (val == 7) { return 'Robotics'; }
  if (val == 8) { return 'Espionage'; }
  return '';
}

function HintTalent(val) {
  if (val == 0) { return 'Courage'; }
  if (val == 1) { return 'Wiring'; }
  if (val == 2) { return 'Running'; }
  if (val == 3) { return 'Spotting'; }
  if (val == 4) { return 'Toughness'; }
  if (val == 5) { return 'Tinkering'; }
  if (val == 6) { return 'Hacking'; }
  return 'Hidden';
}

function HintCfg(name, val) {
  if (name == 'class')  { return HintClass(val);   }
  if (name == 'gun')    { return HintGun(val);   }
  if (name == 'armor')  { return HintArmor(val); }
  if (name == 'trait')  { return HintTrait(val); }
  if (name == 'spec')   { return HintSpec(val);  }
  if (name == 'talent') { return HintTalent(val);}
  return '';
}

function HintMedal(name, val) {
  if (val < 1) {
    if (name == 'key') { return 'KEY (unearned)'; }
    if (name == 'moh') { return 'MOH (unearned)'; }
    if (name == 'pcc') { return 'PCC (unearned)'; }
    if (name == 'cob') { return 'COB (unearned)'; }
    if (name == 'lsa') { return 'LSA (unearned)'; }
    if (name == 'rem') { return 'REM (unearned)'; }
    return '(unearned)';
  }
  if (name == 'key') { return 'Key to the City'; }
  if (name == 'moh') { return 'Medal of Honor'; }
  if (name == 'pcc') { return 'Police Combat Cross'; }
  if (name == 'cob') { return 'Commendation Bar'; }
  if (name == 'lsa') { return 'Life Saving Award'; }
  if (name == 'rem') { 
    if (val == 3)    { return 'Recognition for Exceptional Merit III'; }
    if (val == 2)    { return 'Recognition for Exceptional Merit II'; }
    return 'Recognition for Exceptional Merit';
  }
  return '';
}

function RankIcon(rank, diff) {
  var tooltip = RankDesc(rank)+DiffDesc(rank,diff);
  return '<a href='+linkBase+'readmeafterrank.html title="'+tooltip+'"><img src="pics/rank'+rank+diff+'.gif" alt="'+tooltip+'" border=0 width=12 height=14 align=top></a>';
}

function LogoIcon(icon) {
  var tooltip = HintClass(icon);
  return '<a href='+LinkClass(icon)+' title="'+tooltip+'"><img src="pics/logo'+icon+'.gif" alt="'+tooltip+'" border=0 width=15 height=15></a>';
}

function HeroIcon(icon, rank, diff) {
  var br = '<br>';
  var blank = '<img src=/pics/empty.gif width=3 height=3>';
  var tooltip = HintClass(icon);
  var hero = '<a href='+LinkClass(icon)+' title="'+tooltip+'"><img src=pics/class'+icon+'.gif width=31 height=31 border=0 alt="'+tooltip+'"></a>';
  return hero+br+blank+br+LogoIcon(icon)+blank+RankIcon(rank,diff);
}

function HeroDesc(name, sub, desc) {
  return '<b>'+name+'</b><br><font color=ffe888 face=Arial size=-1>'+sub+'</font><br>'+desc;
}

function CfgIcon(name, icon) {
  var tooltip = HintCfg(name, icon);
  return '<a href='+LinkCfg(name, icon)+' title="'+tooltip+'"><img src=pics/'+name+icon+'.gif width=31 height=31 border=0 alt="'+tooltip+'"></a>&nbsp;';
}

function MedalIcon(name, val) {
  var tooltip = HintMedal(name,val);
  return '<a href='+linkBase+'readmeafterrank.html#medals title="'+tooltip+'"><img src="pics/m'+name+val+'.gif" alt="'+tooltip+'" border=0 width=15 height=12></a><img src=/pics/empty.gif width=2 height=1>';
}

function TitleIcon(id, earned) {
  if (!earned) {
    return '<img src="pics/titd.gif" border=0 width=8 height=12><img src=/pics/empty.gif width=1 height=1>';
  }
  return '<img src="pics/tit'+id+'.gif" border=0 width=8 height=12><img src=/pics/empty.gif width=1 height=1>';
}

function TitleIcons(titles) {
  var icons = '';
  for (var i=0; i < 7; i++) {
    icons = icons + TitleIcon(i, titles.substr(i,1) == '1');
  }
  return icons;
}

function CopyHeroLink(code) {
  return '<span class="copycode" style="display:none">&nbsp;<img src=pics/copy.gif onClick="CopyCode(\''+code+'\');" border=0 alt=Copy title="Copy to Clipboard"></span>';
}

function DeleteHeroLink(heroname) {
  var tooltip = 'Delete Hero';
  return '<a href=index.php?p=delete&heroname='+heroname+' title="'+tooltip+'"><img src=pics/del.gif border=0 alt="'+tooltip+'" width=9 height=9></a>';
}

function WriteHero(name, code, rank, diff, hero, gun, armor, trait, spec, talent, key, moh, pcc, cob, lsa, rem, titles, heroname) {
  var config = CfgIcon('gun',gun)+CfgIcon('armor',armor)+CfgIcon('trait',trait)+CfgIcon('spec',spec)+CfgIcon('talent',talent);
  config = config + '<br>' + MedalIcon('key',key)+MedalIcon('moh',moh)+MedalIcon('pcc',pcc)+MedalIcon('cob',cob)+MedalIcon('lsa',lsa)+MedalIcon('rem',rem)
  if (code != '') {
    //maintenance mode
    config = config + TitleIcons(titles);
    config = config + '<br> ' + wrapCode(code, '&nbsp;' + DeleteHeroLink(heroname));
  } else {
    //public listing
    if (titles == '1') {
      config = config + TitleIcon(0, true);
    }
    config = config + '<br>';
  }
  if (name != '') {
    config = config + '<font size=-1 color=ffe888>"'+ name +'"</font>';
  }
  WriteDbRow(HeroIcon(hero,rank,diff), config);
}

function WritePlayerSearch(title) {
  document.writeln('<form action=index.php method=GET>');
  document.writeln('<table cellpadding=0 cellspacing=0 border=0><tr valign=bottom>');
  document.writeln('<td>' + title + '</td>');
  document.writeln('<td>&nbsp;&nbsp;</td>');
  document.writeln('<td><input type=submit value=Search class=mainoption /></td>');
  document.writeln('<td><input name=p type=hidden value=view><input name=user type=text size=25 maxlength=40 class=entry><img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
  document.writeln('</tr></table>');
  document.writeln('</form>');
}

function WritePlayerAdd() {
  document.writeln('<form action=index.php method=POST>');
  document.writeln('<input name=p type=hidden value=new>');
  document.writeln('<table cellpadding=0 cellspacing=0 border=0>');
  document.writeln('<tr valign=bottom><td align=right>Name:&nbsp;</td>');
  document.writeln('<td><input name=secureusr type=text size=35 class=entry maxlength=40><img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
  document.writeln('</tr><tr valign=bottom><td align=right>Password:&nbsp;</td>');
  document.writeln('<td><input name=securepw type=text size=35 class=entry maxlength=16><img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
  document.writeln('</tr><tr valign=bottom><td align=right>Secret:&nbsp;</td>');
  document.writeln('<td><input name=secret type=text size=35 class=entry maxlength=60>'+optional()+'<img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
  document.writeln('</tr><tr><td height=4></td><td></td>');
  document.writeln('</tr><tr valign=bottom><td align=right>&nbsp;</td>');
  document.writeln('<td><input type=submit value=Create class=mainoption /></td>');
  document.writeln('</tr></table>');
  document.writeln('</form>');
  document.writeln('Your player name must be at least three characters long and consist of only letters, numbers, hyphens, periods, underscores, brackets, parenthese, and exclamation points.<br>You must specify a password consisting of only letters and numbers.');
  WriteBr();
  WriteBr();
  WriteBr();
  document.writeln('<font color=00ff00 size=-1><b>NOTE</b></font><br><font size=-1>The value you enter as your <font color=ffe888>secret</font> is utilized only for recovering your password. Using your <font color=ffe888>email address</font> is recommended.</font>');
}

function WritePlayerLogin(player) {
  document.writeln('<form action=index.php method=POST>');
  document.writeln('<table cellpadding=0 cellspacing=0 border=0>');
  document.writeln('<tr valign=bottom><td align=right>Name:&nbsp;</td>');
  document.writeln('<td><input name=secureusr type=text size=25 class=entry maxlength=40 value='+player+'><img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
  document.writeln('</tr><tr valign=bottom><td align=right>Password:&nbsp;</td>');
  document.writeln('<td><input name=securepw type=password size=25 class=entry maxlength=16><img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
  document.writeln('</tr><tr><td height=4></td><td></td>');
  document.writeln('</tr><tr valign=bottom><td align=right>&nbsp;</td>');
  document.writeln('<td><input type=submit value=Login class=mainoption /></td>');
  document.writeln('</tr></table>');
  document.writeln('</form>');
}

function ToggleDetails(hinttext, fullnote) {
  if (fullnote.style.display == "none") {
    hinttext.innerHTML = "Click this icon to minimize your private notes.";
    fullnote.style.display = "block";
  } else {
    hinttext.innerHTML = "Click this icon to expand the full text of your private notes.";
    fullnote.style.display = "none";
  }
}

function getFullNoteBlock(note) {
  var s = '</tr><tr valign=top><td align=right><img src=pics/tiny-zmenu.gif onclick="ToggleDetails(togglenote, detailednote);" alt="Toggle Expanded Notes">&nbsp;</td><td>' +
          //'&nbsp;<img src=pics/tiny-zmenu.gif onclick="ToggleDetails(togglenote, detailednote);" alt="Toggle Expanded Notes">&nbsp;' +
          '<font size=-1><sup><span id=togglenote>Click this icon to expand the full text of your private notes.</span></sup></font>' +
          '<span id=detailednote style="display:none">' +
          '<table cellpadding=4 cellspacing=0 border=1 bordercolor=ffe888 bgcolor=212121 width="100%"><tr><td><font size=-1>' + note.replace(/\|/g, '<br>') + '</font></td></tr></table>' +
          '</span></td>';
  return s;
}


function WriteUpdateDesc(desc, note, clan, status) {
  var fullNote = '';
  if (note && note.length > 48) {
    fullNote = getFullNoteBlock(note);
  }
  document.writeln('<form action=index.php method=GET>');
  document.writeln('<input type=hidden name=p value=update />');
  document.writeln('<table cellpadding=0 cellspacing=0 border=0><tr valign=bottom>');
  document.writeln('<td><input type=submit value=Update class=mainoption /></td>');
  document.writeln('<td><input name=desc type=text value="'+desc+'" size=85 maxlength=1024 class=entry /><img src=/pics/doty.gif width=100% height=1 align=bottom></td></tr><tr valign=bottom><td />');
  document.writeln('<td><input name=clan type=text value="'+clan+'" size=85 maxlength=7    class=entry /><img src=/pics/doty.gif width=100% height=1 align=bottom></td></tr><tr valign=bottom><td />');
  document.writeln('<td><input name=note type=text value="'+note+'" size=85 maxlength=4096 class=entry /><img src=/pics/doty.gif width=100% height=1 align=bottom></td>'+fullNote);
  if (status != '') {
  document.writeln('</tr><tr valign=top><td /><td><font color=ffe888 size=-1>'+status+'</font></td>');
  } else {
  document.writeln('</tr><tr><td><font size=-1>&nbsp</font></td><td />');
  }
  document.writeln('</tr></table>');
  document.writeln('</form>');
}

function WriteUpdateMainIcon(hero, status) {
  var i = 0;
  var iClass;
  var link = 'index.php?p=update&icon=';
  var tooltip;
  document.writeln('<table cellpadding=0 cellspacing=0 border=0><tr valign=middle align=center>');
  while (i < 11) {
    if (i < 10) {
      iClass = i;
    } else {
      iClass = 'a';
    }
    tooltip = HintClass(iClass);
    if (iClass == hero) {
      document.writeln('<td height=35 width=35 bgcolor=ffe888><img src=pics/class'+iClass+'.gif width=31 height=31 border=0 alt="'+tooltip+'"></td>');
    } else {
      document.writeln('<td height=35 width=35><a href='+link+iClass+' title="'+tooltip+'"><img src=pics/class'+iClass+'.gif width=31 height=31 border=0 alt="'+tooltip+'"></a></td>');
    }
    i = i + 1;
  }
  document.writeln('</tr></table>');
  if (status != '') {
    document.writeln('<font color=ffe888 size=-1>'+status+'</font>');
  } else {
    document.writeln('<font size=-1>&nbsp;</font>');
  }
}

function WriteHeroesTitle(deletemsg) {
  if (deletemsg == null) {
    deletemsg = '';
  }
  document.writeln('<table cellpadding=0 cellspacing=0 border=0><tr><td><font color=ffcc00><b>Heroes</b></font></td><td>&nbsp;&nbsp;</td><td>'+deletemsg+'</td></tr></table>');
}

function WriteAddHero(rankcode, status) {
  document.writeln('<form action=index.php method=GET>');
  document.writeln('<input type=hidden name=p value=update />');
  document.writeln('<table cellpadding=0 cellspacing=0 border=0>');
  document.writeln('<tr valign=bottom><td align=right>Build&nbsp;Name:&nbsp;</td>');
  document.writeln('<td><input name=heroname type=text size=35 class=entry maxlength=15>'+optional()+'<img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
  document.writeln('</tr><tr valign=bottom><td align=right>Rank&nbsp;Code:&nbsp;</td>');
  document.writeln('<td><input name=code type=text value="'+rankcode+'" size=35 class=entry maxlength=19><img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
  document.writeln('</tr><tr><td height=4></td><td></td>');
  document.writeln('</tr><tr><td align=right>&nbsp;</td>');
  document.writeln('<td><input type=submit value=Submit class=mainoption /><sup>&nbsp;<font color=ffe888>'+status+'</font></sup></td>');
  document.writeln('</tr></table>');
  document.writeln('</form>');
}

function WriteGenerateLogin(username) {
  if (username == '') {
    document.writeln('<form action=index.php method=POST>');
    document.writeln('<input name=p type=hidden value=gen>');
    document.writeln('<table cellpadding=0 cellspacing=0 border=0>');
    document.writeln('<tr valign=bottom><td align=right>Name:&nbsp;</td>');
    document.writeln('<td><input name=secureusr type=text size=25 class=entry maxlength=40><img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
    document.writeln('</tr><tr valign=bottom><td align=right>Password:&nbsp;</td>');
    document.writeln('<td><input name=securepw type=password size=25 class=entry maxlength=16><img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
    document.writeln('</tr><tr><td height=4></td><td></td>');
    document.writeln('</tr><tr valign=bottom><td align=right>&nbsp;</td>');
    document.writeln('<td><input type=submit value=Login class=mainoption /></td>');
    document.writeln('</tr></table>');
    document.writeln('</form>');
    return;
  }
  document.writeln('<form action=index.php method=GET>');
  document.writeln('<td><input name=p type=hidden value=gen>');
  document.writeln('<table cellpadding=0 cellspacing=0 border=0>');
  document.writeln('<tr valign=bottom><td align=right>Name:&nbsp;</td>');
  document.writeln('<td><font color=ffe888>'+username+'</font></td>');
  document.writeln('</tr><tr valign=bottom><td align=right>Spoof:&nbsp;</td>');
  document.writeln('<td><input name=user type=text size=25 class=entry maxlength=40>'+optional()+'<img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
  document.writeln('</tr><tr><td height=4></td><td></td>');
  document.writeln('</tr><tr valign=bottom><td align=right>&nbsp;</td>');
  document.writeln('<td><input type=submit value=Update class=mainoption />&nbsp;<input name=reset type=submit value=Reset class=mainoption /></td>');
  document.writeln('</tr></table>');
  document.writeln('</form>');
}

var cangenerate;
var ranklimit;
function WriteGenerateHeader(spoofcap) {
  cangenerate = 0;
  ranklimit = spoofcap;
  document.writeln('<font face=FixedSys color=ffcc00>BUILD</font>');
  document.writeln('<table cellpadding=0 cellspacing=12 border=0><tr valign=top>');
}

function WriteGenerateBreak() {
  document.writeln('</tr><tr valign=top>');
}

function RankLimitIcons(val) {
  var l0; var l1;
  var i = 2;
  var j = 0;
  var result = '';
  var s0 = '<td height=22 width=20';
  var s1 = '<img src=pics/rank';
  var s2 = '.gif alt="';
  var s3 = ' \nMax: ';
  var s4 = '" border=0>';
  if (val < ranklimit) {
    ranklimit = val;
  }
  while (i < 13) {
    if (i == val) {
      l0 = ' bgcolor=ffe888><table cellpadding=2 cellspacing=0 bgcolor=0><tr><td>';
      l1 = '</td></tr></table></td>';
    } else {
      l0 = '><a href=index.php?p=gen&rankcap='+i+' title="'+RankDesc(i)+' \nMax: '+i+'">';
      l1 = '</a></td>';
    }
    result = result + s0 + l0 + s1 + i + j + s2 + RankDesc(i) + s3 + i + s4 + l1;
    i = i + 1;
    if (i > 10) 
      { j = 4; }
    else 
      { j = Math.floor((i-1)/3); }
  }
  return result;
}

function WriteGenerateFooter(rankcap, rankcode) {
  document.writeln('</tr></table>');
  document.writeln('<font face=FixedSys color=ffcc00>RANK&nbsp;CAP</font>');
  document.writeln('<table cellpadding=0 cellspacing=4 border=0>');
  document.writeln('<tr valign=middle align=center><td width=4 />');
  document.writeln(RankLimitIcons(rankcap));
  document.writeln('</tr></table>');
  if (cangenerate > 5) {
    if (rankcode != '') {
      document.writeln('<font face=FixedSys color=ffcc00>TEMP&nbsp;CODE</font><br>');
      document.writeln(wrapCode(rankcode, '&nbsp;<font size=-1>(<font color=ffe888>'+RankDesc(ranklimit)+'</font>)</font>', '&nbsp;&nbsp;&nbsp;&nbsp;') + '<br>');
    }
    document.writeln('<form action=index.php method=GET>');
    document.writeln('<input name=p type=hidden value=update />');
    document.writeln('<input name=code type=hidden value=generate />');
    document.writeln('<table cellpadding=0 cellspacing=0 border=0>');
    document.writeln('<tr valign=bottom><td align=right>Build&nbsp;Name:&nbsp;</td>');
    document.writeln('<td><input name=heroname type=text size=35 class=entry maxlength=15>'+optional()+'<img src=/pics/doty.gif width=100% height=1 align=bottom></td>');
    document.writeln('</tr><tr><td height=4></td><td></td>');
    document.writeln('</tr><tr><td align=right>&nbsp;</td>');
    document.writeln('<td><input type=submit value=Submit class=mainoption /></td>');
    document.writeln('</tr></table><br>');
    document.writeln('</form>');
    document.writeln('<font size=-1><font color=00ff00><b>NOTE</b></font><br>' +
                     'If you intend to keep this build, earning additional ranks and medals, you should press <font color=ffe888><b>Submit</b></font> and ' +
                     'use the code it displays at the top of your saved builds. If your selections match a build you\'ve already saved, that build\'s code ' +
                     'is listed so that you can continue your progress on that hero.<br><br>' +
                     '<i>The temporary code represents the build you\'ve selected on this page without regard to your saved builds. It is still capped based on what you\'ve unlocked, and if you provided a spoof name, that is the name it works for.</i></font>');
  }
}

function WriteGenerate(cfg, max, val, locks) {
  var i = 0;
  var iItem;
  var link = 'index.php?p=gen&'+cfg+'=';
  var s1; var s2; var dis; var level; var unlock; var tooltip;
  document.writeln('<td><table cellpadding=0 cellspacing=0 border=0><tr valign=middle align=center>');
  while (i < max) {
    if ((i < 10) || (cfg != 'class')) {
      iItem = i;
    } else {
      iItem = 'a';
    }
    level = locks.substr(i*2,2);
    s2 = '</a>';
    dis = '';
    if (level < 2) {
      s1 = '>';
      s2 = '';
      dis = 'd';
      tooltip = 'unavailable';
    } else {
      tooltip = HintCfg(cfg, iItem) + ' \nMax: '+parseInt(level, 10);
      if (iItem == val) {
        s1 = ' bgcolor=ffe888>';
        s2 = '';
        cangenerate = cangenerate + 1;
        if (level < ranklimit) {
          ranklimit = level;
        }
      } else {
        s1 = '><a href='+link+iItem+' title="'+tooltip+'">';
      }
    }
    unlock = '<img src=pics/u'+level+'.gif width=5 height=31>';
    document.writeln('<td height=35 width=35'+s1+'<img src=pics/'+cfg+iItem+dis+'.gif width=31 height=31 border=0 alt="'+tooltip+'">'+s2+'</td><td>'+unlock+'</td>');
    i = i + 1;
    if ((i % 4) == 0) {
      document.writeln('</tr><tr valign=middle align=center>');
    }
  }
  document.writeln('</tr></table></td>');
}

function WritePlayerJoinHonorGuard() {
  document.writeln('<br><br>Would you like to join the <font color=ffe888><b>Honor Guard</b></font>? Its members have ' +
                   'pledged to earn ranks and medals legitimately, and it welcomes all who are committed to that goal. ' +
                   'Membership is not required and has no impact on who you can play with. Being in the Honor Guard does not ' + 
                   'confer any in-game bonuses; it is purely an assertion of your <font color=ffe888><b>personal integrity</b></font>.');
  document.writeln('<br><br>Joining the Honor Guard clears all of your heroes and <font color=orange>resets everything</font> you\'ve ' +
                   'unlocked. You will have to build your roster with rank codes earned for new heroes created in the latest ' +
                   'version of the game. Your older codes can still be stored, but they will not contribute to unlocks or titles.');
  document.writeln('<br><br><i><a href=index.php?hg=1>I understand that titles I earn have no effect in the game. Even so, I am ' +
                   'committed to playing legitimately and want to <font color=orange><b><u>reset my account</u></b></font> now ' + 
                   'so that I may join the Honor Guard.</a></i>');
}
