// 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";

var hexa = new Array();
hexa["0"] = "000000";
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["b"] = "106F59";
hexa["c"] = "5A134F";
hexa["d"] = "035AFF";
hexa["e"] = "681EA7";
hexa["f"] = "5097C1";
hexa["g"] = "BEDAC4";
hexa["h"] = "024D2C";
hexa["i"] = "7D081B";
hexa["j"] = "90243E";
hexa["k"] = "743313";
hexa["l"] = "A7905E";
hexa["m"] = "555C26";
hexa["n"] = "AEAC97";
hexa["o"] = "C0BF7F";
hexa["p"] = "555555";
hexa["q"] = "DA0120";
hexa["r"] = "00B906";
hexa["s"] = "E8FF19";
hexa["t"] = "170BDB";
hexa["u"] = "23C2C6";
hexa["v"] = "E201DB";
hexa["w"] = "FFFFFF";
hexa["x"] = "CA7C27";
hexa["y"] = "757575";
hexa["z"] = "CC8034";
hexa["/"] = "DBDF70";
hexa["*"] = "BBBBBB";
hexa["-"] = "747228";
hexa["+"] = "993400";
hexa["?"] = "670504";
hexa["@"] = "623307";

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(banner) {
 for (var Key in hexa)
 {
  var NickArray = banner.split("^"+Key);
  banner = NickArray.join("<span style=\"color:#"+hexa[Key]+";\">");
 }
 document.getElementById("Nick").innerHTML = "<span style=\"color:#ffffff;\">"+banner+"</span>";
}

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

// **********************************  ****************************

//ie compatibility to jump on a select option
function jump(objSelect){
	if (objSelect.selectedIndex<=0) return false;
		var objOpt = objSelect.options[objSelect.selectedIndex];
		window.location.href="index.php?page=clan&user="+objOpt.value;
    }

// mail validation check
function checkMail(email)
{
	var x = email;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}

// ***************************

/* Menus collapse function */
function toggleMiniframe(boxid,folded,expanded) {
	var pos = document.getElementById('box-'+boxid);
	if (pos.className == 'unfolded') {
		document.getElementById('box-'+boxid).className = 'folded';
		document.getElementById('icon-'+boxid).src = folded;
		document.cookie = "box-"+boxid+"=folded; expires=Wed, 11 Mar 2020 18:00:00 GMT; path=/";
	}
	else { 
		document.getElementById('box-'+boxid).className = 'unfolded';
		document.getElementById('icon-'+boxid).src = expanded;
		document.cookie = "box-"+boxid+"=unfolded; expires=Wed, 11 Mar 2020 18:00:00 GMT; path=/";
	}
}

// ***************************

// CAPTCHA reload
function new_freecap() {
	// loads new freecap image
	if(document.getElementById)
	{
		// extract image name from image source (i.e. cut off ?randomness)
		thesrc = document.getElementById("randomImg").src;
		thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
		// add ?(random) to prevent caching
		document.getElementById("randomImg").src = thesrc+"?"+Math.round(Math.random()*10000);
	} else {
		alert("Unable to load captcha image");
	}
}