// Original script by EasyRider
// Wolfenstein: Enemy Territory - Resource Site
// http://www.enemy-territory.com/modules.php?name=Nick_Colors
// Color dropdown and some modification by kenda or ^4-daKen- when playing ET :)

/* Array for nick color */
var color = new Array();
color["0"] = "Black";
color["1"] = "Red";
color["2"] = "Green";
color["3"] = "Yellow";
color["4"] = "Blue";
color["5"] = "Cyan";
color["6"] = "Pink";
color["7"] = "White";
color["8"] = "Orange";
color["9"] = "Gray";
color["a"] = "Orange";
color["b"] = "Turquoise";
color["c"] = "Purple";
color["d"] = "Light Blue";
color["e"] = "Purple";
color["f"] = "Light Blue";
color["g"] = "Light Green";
color["h"] = "Dark Green";
color["i"] = "Dark Red";
color["j"] = "Claret";
color["k"] = "Brown";
color["l"] = "Light Brown";
color["m"] = "Olive";
color["n"] = "Beige";
color["o"] = "Beige";
color["p"] = "Black";
color["q"] = "Red";
color["r"] = "Green";
color["s"] = "Yellow";
color["t"] = "Blue";
color["u"] = "Cyan";
color["v"] = "Pink";
color["w"] = "White";
color["x"] = "Orange";
color["y"] = "Gray";
color["z"] = "Orange";
color["/"] = "Beige";
color["*"] = "Gray";
color["-"] = "Olive";
color["+"] = "Foxy Red";
color["?"] = "Dark Brown";
color["@"] = "Brown";
color["\!"] = "Orange";

var hexa = new Array();
hexa["0"] = "121212";
hexa["1"] = "DA0120";
hexa["2"] = "00B906";
hexa["3"] = "E8FF19";
hexa["4"] = "170BDB";
hexa["5"] = "23C2C6";
hexa["6"] = "E201DB";
hexa["7"] = "FFFFFF";
hexa["8"] = "CA7C27";
hexa["9"] = "757575";
hexa["a"] = "EB9F53";hexa["A"] = hexa["a"];
hexa["b"] = "106F59";hexa["B"] = hexa["b"];
hexa["c"] = "5A134F";hexa["C"] = hexa["c"];
hexa["d"] = "035AFF";hexa["D"] = hexa["d"];
hexa["e"] = "681EA7";hexa["E"] = hexa["e"];
hexa["f"] = "5097C1";hexa["F"] = hexa["f"];
hexa["g"] = "BEDAC4";hexa["G"] = hexa["g"];
hexa["h"] = "024D2C";hexa["H"] = hexa["h"];
hexa["i"] = "7D081B";hexa["I"] = hexa["i"];
hexa["j"] = "90243E";hexa["J"] = hexa["j"];
hexa["k"] = "743313";hexa["K"] = hexa["k"];
hexa["l"] = "A7905E";hexa["L"] = hexa["l"];
hexa["m"] = "555C26";hexa["M"] = hexa["m"];
hexa["n"] = "AEAC97";hexa["N"] = hexa["n"];
hexa["o"] = "C0BF7F";hexa["O"] = hexa["o"];
hexa["p"] = "121212";hexa["P"] = hexa["p"];
hexa["q"] = "DA0120";hexa["Q"] = hexa["q"];
hexa["r"] = "00B906";hexa["R"] = hexa["r"];
hexa["s"] = "E8FF19";hexa["S"] = hexa["s"];
hexa["t"] = "170BDB";hexa["T"] = hexa["t"];
hexa["u"] = "23C2C6";hexa["U"] = hexa["u"];
hexa["v"] = "E201DB";hexa["V"] = hexa["v"];
hexa["w"] = "FFFFFF";hexa["W"] = hexa["w"];
hexa["x"] = "CA7C27";hexa["X"] = hexa["x"];
hexa["y"] = "757575";hexa["Y"] = hexa["y"];
hexa["z"] = "CC8034";hexa["Z"] = hexa["z"];
hexa["/"] = "DBDF70";
hexa["*"] = "BBBBBB";
hexa["-"] = "747228";
hexa["+"] = "993400";
hexa["?"] = "670504";
hexa["@"] = "623307";
hexa[">"] = "623307";
hexa["["] = "bec0bb";
hexa["]"] = "7f7c0b";
hexa["&"] = "3795c8";
hexa[";"] = "c0bfc7";
hexa["("] = "066432";
hexa[")"] = "f70303";
hexa[","] = "cf9a3a";
hexa["."] = "f7f8b6";
hexa["="] = "7f7f00";
hexa["\\"] = "007f03";
hexa["'"] = "c5f9c8";
hexa["\!"] = "CA7C27";

function KeyColor(x) {
  var Pressed = String.fromCharCode(window.event.keyCode);
  for (var Key in color) {
    if (Pressed == Key) 
    	document.getElementById("Colors").innerHTML = "<span style=\"color:#"+hexa[Key]+";\">["+Key.toUpperCase()+"] "+color[Key]+"</span>";
  }
}

function SubmitNick(inputName) {
 for (var Key in hexa)
 {
  var NickArray = inputName.split("^"+Key);
  inputName = NickArray.join("<span style=\"color:#"+hexa[Key]+";\">");
 }
 document.getElementById("Nick").innerHTML = "<span style=\"color:#ffffff;\">"+inputName+"</span>";
}

function SubmitNickNoColor(inputName) {
 for (var Key in hexa)
 {
  var NickArray = inputName.split("^"+Key);
  inputName = NickArray.join("<span>");
 }
 document.getElementById("Nick").innerHTML = "<span style=\"color:#ffffff;\">"+inputName+"</span>";
}

function SubmitNickPrint(inputName) {
 for (var Key in hexa)
 {
 var NickArray = inputName.split("^"+Key);
 inputName = NickArray.join("</span><span style=\"color:#"+hexa[Key]+";\">");
 }
 document.write(inputName+"</span></span>");
}

function SubmitNickPrintNoColor(inputName) {
 for (var Key in hexa)
 	{
 	var NickArray = inputName.split("^"+Key);
 	inputName = NickArray.join("</span><span>");
 	}
 document.write(inputName+"</span></span>");
}

function picked(thisColor) {
 banner_post.pickcolor.options[0].selected = true;
 document.banner_post.banner.focus();
 //document.banner_post.banner.value = //document.banner_post.inputName.value+thisColor;
 replaceSelection (document.banner_post.banner, thisColor);
 SubmitNick(document.banner_post.banner.value);
}
	
//----- REAL TIME COLORATION TD (used in User module)
function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}

function setCaretToEnd (input) {
  setSelectionRange(input, input.value.length, input.value.length);
}

function setCaretToBegin (input) {
  setSelectionRange(input, 0, 0);
}

function setCaretToPos (input, pos) {
  setSelectionRange(input, pos, pos);
}

function selectString (input, string) {
  var match = new RegExp(string, "i").exec(input.value);
  if (match) {
    setSelectionRange (input, match.index, match.index + match
[0].length);
  }
}

function replaceSelection (input, replaceString) {
  if (input.setSelectionRange) {
    var selectionStart = input.selectionStart;
    var selectionEnd = input.selectionEnd;
    input.value = input.value.substring(0, selectionStart)
                  + replaceString
                  + input.value.substring(selectionEnd);
    if (selectionStart != selectionEnd) // has there been a selection
      setSelectionRange(input, selectionStart, selectionStart + 
replaceString.length);
    else // set caret
      setCaretToPos(input, selectionStart + replaceString.length);
  }
  else if (document.selection) {
    var range = document.selection.createRange();
    if (range.parentElement() == input) {
      var isCollapsed = range.text == '';
      range.text = replaceString;
      if (!isCollapsed)  { // there has been a selection
        //it appears range.select() should select the newly 
        //inserted text but that fails with IE
        range.moveStart('character', -replaceString.length);
        range.select();
      }
    }
  }
}

