menu_status = new Array();
tabsToHideIDs = new Array();
var allTabs = new Array();
var allTabButtons = new Array();
var ratAddList = new Array();
var ratRemoveList = new Array();

function in_array(seed, arr){
	var a = false;
	for(var i = 0; i < arr.length; i++){
		if(seed == arr[i]){
			a = true;
			break;
		}
	}
	return a;
}

function find_in_array(seed, arr){
	var a = false;
	for(var i = 0; i < arr.length; i++){
		if(seed == arr[i]){
			a = true;
			break;
		}
	}
	return i;
}

//check if an object is an array
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
	  return false;
   else
	  return true;
}

//add a tab to the system
function registerTab(tabName, display) {
	allTabs.push(tabName);
	registerTabButton(tabName);
	regTabID = document.getElementById("tab_"+tabName);
	regTabID.style.display = display;
	if(display != "none")
		toggleTabButton(tabName);
}
		
//add a tab to the system
function registerTabButton(buttonName) {
	allTabButtons.push(buttonName);
}

function toggleTabButton(tabButton) {
	//if(in_array(tabButton.id, allTabButtons)) {
		for(i = 0; i < allTabButtons.length; i++) {
			if(tabButton == allTabButtons[i]) {
				document.getElementById("tabButton_"+allTabButtons[i]).className = 'selectedTabButton';
			}
			else {
				document.getElementById("tabButton_"+allTabButtons[i]).className = 'tabButton';
			}
		}
	//}
}

//used to switch between tabs. makes all tabs in tabsToShow visible and hides all tabs in tabsToHide
function switchTabs(tabsToShow, tabsToHide) {

	if(in_array(tabsToShow, allTabs)) {
		
		if(isArray(tabsToShow) && tabsToShow.length > 0) {
			for(i = 0; i < tabsToShow.length; i++) {
				tabToShowID = document.getElementById("tab_"+tabsToShow[i]);
				tabToShowID.style.display = 'block';
				toggleTabButton(tabsToShow[i]);
			}
		}
		else {
			tabToShowID = document.getElementById("tab_"+tabsToShow);
			tabToShowID.style.display = 'block';
			toggleTabButton(tabsToShow);
		}
		
		if(isArray(tabsToHide) && tabsToHide.length > 0) {					
			for(i = 0; i < tabsToHide.length; i++) {
				tabToHideID = document.getElementById("tab_"+tabsToHide[i]);
				if(tabToHideID != tabToShowID && document.getElementById("tab_"+tabsToHide[i])) {
					tabToHideID.style.display = 'none';
				}
			}
		}
		else {
			tabToHideID = document.getElementById("tab_"+tabsToHide);
			tabToHideID.style.display = 'none';
		}
		
		window.location.hash = tabsToShow;				
	}
}


//display possible tab specified in url
function urlTabSwitch() {
	urlTab = window.location.hash;
	if(urlTab.length > 1) {
		switchTabs(urlTab.substr(1), allTabs);
	}
}

//simple  function to hide an element
function hideBox() {
	document.getElementById("deleteRaidConfirmationBox").style.display="none";
}

//simple  function to how an element
function showBox() {
	document.getElementById("deleteRaidConfirmationBox").style.display="block";
}

//simple  function to hide an element
function hideBox2() {
	document.getElementById("deleteRaidTargetConfirmationBox").style.display="none";
}

//simple  function to how an element
function showBox2() {
	document.getElementById("deleteRaidTargetConfirmationBox").style.display="block";
}

//rawr
function admin_doRaidTakeout(charid, toggler, cellObj) {
	if(toggler == 0)
		admin_doRaidTakeoutAdd(charid, cellObj);
	else if(toggler == 1)
		admin_doRaidTakeoutRemove(charid, cellObj);
}

//select a character to sign for a raid
function setSignupCharProfileBox(charid, charname, guild, level, race, clas, avatarcode, role, priority) {
	document.getElementById("signupCharProfileBox").innerHTML = '<img class="classAvatarMedium left" src="images/icons/wowavatars/'+avatarcode+'.gif" alt="'+race+' '+clas+'"/>'+
						'<div class="characterProfileBox left"><p class="classColoredName">&nbsp;'+charname+'</p>'+
						'<p class="guildTag">&nbsp;&lt;'+guild+'&gt;</p>'+
						'<p class="guildTag">&nbsp;'+level+' '+race+' '+clas+'</p></div>';
	
	var roles = new Array();
	roles["Damage Dealer"] = 0;
	roles["Healer"] = 1;
	roles["Tank"] = 2;
	document.form_raidSignup.raidSignup_role[roles[role]].checked = true;
	document.form_raidSignup.raidSignup_priority[priority].checked = true;
	document.form_raidSignup.raidSignup_charId.value = charid;
	document.form_raidSignup.raidSignup_submit.disabled = false;
	
	setAvaliableRoles(clas);
	
	function setAvaliableRoles(selectedClass) {
		dps = true;
		tank = true;
		healer = true;
		switch(selectedClass) {
			case "Death Knight":
				dps = true;
				tank = true;
				healer = false;
			break;
			case "Druid":
				dps = true;
				tank = true;
				healer = true;
			break;
			case "Hunter":
				dps = true;
				tank = false;
				healer = false;
			break;
			case "Mage":
				dps = true;
				tank = false;
				healer = false;
			break;
			case "Paladin":
				dps = true;
				tank = true;
				healer = true;
			break;
			case "Priest":
				dps = true;
				tank = false;
				healer = true;
			break;
			case "Rogue":
				dps = true;
				tank = false;
				healer = false;
			break;
			case "Shaman":
				dps = true;
				tank = false;
				healer = true;
			break;
			case "Warlock":
				dps = true;
				tank = false;
				healer = false;
			break;
			case "Warrior":
				dps = true;
				tank = true;
				healer = false;
			break;				
		}
		document.form_raidSignup.raidSignup_role[0].disabled = !dps;
		document.form_raidSignup.raidSignup_role[1].disabled = !healer;
		document.form_raidSignup.raidSignup_role[2].disabled = !tank;
	}	
}

//add/remove character id to raid takeout list
function admin_doRaidTakeoutAdd(charid, cellObj) {
	var i = find_in_array(charid, ratAddList);
	if(i < ratAddList.length) {
		ratAddList.splice(i, 1);
		cellObj.style.background = "";
	}
	else {
		ratAddList.push(charid);
		cellObj.style.background = "green";
	}
	document.form_raidAdminPanel.rat_addIds.value = ratAddList.join(",");
	//alert(document.form_raidAdminTakout.rat_addIds.value);
}

//add/remove character id to raid removal list
function admin_doRaidTakeoutRemove(charid, cellObj) {
	var i = find_in_array(charid, ratRemoveList);
	if(i < ratRemoveList.length) {
		ratRemoveList.splice(i, 1);
		cellObj.style.background = "";
	}
	else {
		ratRemoveList.push(charid);
		cellObj.style.background = "red";
	}
	document.form_raidAdminPanel.rat_removeIds.value = ratRemoveList.join(",");
	//alert(document.form_raidAdminTakout.rat_removeIds.value);
}

//set the group nbr to add/remove players in a raid from
function admin_setGroupNbr(nbr) {
	document.form_raidAdminPanel.rat_selectedGroupNbr.value = nbr;	
	document.form_raidAdminPanel.rat_hash.value = window.location.hash;	
}

function toSt(n) {
	s="";
	//if(n < 10)
	//	s+="0";
	return s+n.toString();
}

function countdown(y, m, da, h, min, s, cdelem) {
	cdelement = cdelem;
	ed = new Date();
	day = da;
	year = y;
	month = m;
	minute = min;
	hour = h;
	second = s;
	ed.setDate(day);
	ed.setMonth(month);
	ed.setFullYear(year);
	ed.setHours(hour);
	ed.setMinutes(minute);
	ed.setSeconds(second);
	cl = document.clock;
	d=new Date();
	count=Math.floor((ed.getTime()-d.getTime())/1000)-2592000;
	dateText = "";
	if(count <= 0) {
		return;
	}
	
	/*
	days = Math.floor(timeleft/86400);
	timeleft = timeleft - days*86400;
	hours = Math.floor(timeleft/3600);
	timeleft = timeleft - hours*3600;
	minutes = Math.floor(timeleft/60);	
	timeleft = timeleft - hours*60;	
	seconds = Math.floor(timeleft/60);
	*/
	
	//cl.secs.value=toSt(count%60);
	seconds=toSt(count%60);	
	
	count=Math.floor(count/60);
	//cl.mins.value=toSt(count%60);
	minutes=toSt(count%60);
	
	count=Math.floor(count/60);
	//cl.hours.value=toSt(count%24);
	hours=toSt(count%24);
	
	count=Math.floor(count/24);
	//cl.days.value=count;
	days=count;
	if(days > 1)
		dateText+=days+" days, ";
	else if(days == 1)
		dateText+=days+" day, ";
	if(hours > 1)
		dateText+=hours+" hours, ";
	else if(hours == 1)
		dateText+=hours+" hour, ";
	if(minutes > 1 || minutes == 0)
		dateText+=minutes+" mins, ";
	else if(minutes == 1)
		dateText+=minutes+" min, ";
	if(seconds > 1 || seconds == 0)
		dateText+=seconds+" secs.";
	else
		dateText+=seconds+" sec.";
	
	document.getElementById(cdelement).innerHTML = dateText;

	setTimeout("countdown(year, month, day, hour, minute, second, cdelement)", 1000);
}

function countup(y, m, da, h, min, s, cdelem) {
	cdelement = cdelem;
	ed = new Date();
	day = da;
	year = y;
	month = m;
	minute = min;
	hour = h;
	second = s;
	ed.setDate(day);
	ed.setMonth(month);
	ed.setFullYear(year);
	ed.setHours(hour);
	ed.setMinutes(minute);
	ed.setSeconds(second);
	cl = document.clock;
	d=new Date();
	count=Math.floor((d.getTime()-ed.getTime())/1000)-2592000;
	dateText = "";
	if(count <= 0) {
		return;
	}
		
	//cl.secs.value=toSt(count%60);
	seconds=toSt(count%60);	
	
	count=Math.floor(count/60);
	//cl.mins.value=toSt(count%60);
	minutes=toSt(count%60);
	
	count=Math.floor(count/60);
	//cl.hours.value=toSt(count%24);
	hours=toSt(count%24);
	
	count=Math.floor(count/24);
	//cl.days.value=count;
	days=count;
	if(days > 1)
		dateText+=days+" days, ";
	else if(days == 1)
		dateText+=days+" day, ";
	if(hours > 1)
		dateText+=hours+" hours, ";
	else if(hours == 1)
		dateText+=hours+" hour, ";
	if(minutes > 1 || minutes == 0)
		dateText+=minutes+" mins, ";
	else if(minutes == 1)
		dateText+=minutes+" min, ";
	if(seconds > 1 || seconds == 0)
		dateText+=seconds+" secs.";
	else
		dateText+=seconds+" sec.";
	
	document.getElementById(cdelement).innerHTML = dateText;

	setTimeout("countup(year, month, day, hour, minute, second, cdelement)", 1000);
}


function editChar() {
	if (xmlhttp.readyState==4) {// 4 = "loaded"
		if (xmlhttp.status==200) {// 200 = "OK"
			updateCharEditForm(xmlhttp.responseText);
		}
		else {
			alert("Problem retrieving armory data:" + xmlhttp.statusText + "\n\r" + "Please try again later.");
		}
	}
}
function newChar() {
	if (xmlhttp.readyState==4) {// 4 = "loaded"
		if (xmlhttp.status==200) {// 200 = "OK"
			updateCharNewForm(xmlhttp.responseText);
		}
		else {
			alert("Problem retrieving armory data:" + xmlhttp.statusText + "\n\r" + "Please try again later.");
		}
	}
}

function updateCharEditForm(text) {
		/*
		id, name, guild, class, role, level, race, sex, prio, realm
		echo $xml->characterInfo->character['name']."|";
		echo $xml->characterInfo->character['guildName']."|";
		echo $xml->characterInfo->character['gender']."|";
		echo $xml->characterInfo->character['race']."|";
		echo $xml->characterInfo->character['class']."|";
		echo $xml->characterInfo->character['level'];
		realm
		*/
	if(text.length < 10) {
		alert("Could not find character. Please try again.");
	}
	else {
		charData = text.split("|");
		setCharToEdit("", charData[0], charData[1], charData[4], "", charData[5], charData[3], charData[2], "", charData[6]);
	}
}

function updateCharNewForm(text) {
		/*
		 name, guild, class,  level, race, sex,  realm
		echo $xml->characterInfo->character['name']."|";
		echo $xml->characterInfo->character['guildName']."|";
		echo $xml->characterInfo->character['gender']."|";
		echo $xml->characterInfo->character['race']."|";
		echo $xml->characterInfo->character['class']."|";
		echo $xml->characterInfo->character['level'];
		realm
		*/
	if(text.length < 10) {
		alert("Could not find character. Please try again.");
	}
	else {
		charData = text.split("|");
		setDataFromArmory(charData[0], charData[1], charData[4], charData[5], charData[3], charData[2], charData[6]);
	}
}

var xmlhttp;
function loadXMLDoc(url, method) {
	xmlhttp=null;
	if(window.XMLHttpRequest) {// code for Firefox, Opera, IE7, etc.
		xmlhttp=new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(xmlhttp!=null) {
		xmlhttp.onreadystatechange=method;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else {
		alert("Your browser does not support XMLHTTP. You will have to do this shit manually, sorry!");
	}
}

function state_Change() {
	if (xmlhttp.readyState==4) {// 4 = "loaded"
		if (xmlhttp.status==200) {// 200 = "OK"
			callback(xmlhttp.responseText);
		}
		else {
			alert("Problem retrieving armory data:" + xmlhttp.statusText + "\n\r" + "Please try again later.");
		}
	}
}

function readArmoryProfile(name, realm, method) {
	if(name.length < 2)
		alert("The specified name, \""+name+"\", is invalid!");
	else if(realm.length < 2)
		alert("The specified realm, \""+realm+"\", is invalid!");
	else
		loadXMLDoc("armoryreader.php?n="+name+"&r="+realm, method);
}








/*
var xmlHttp=null;

function displayTooltip(searchString) {

	//var oRequest = new XMLHttpRequest();
	//var sURL  = "http://"+self.location.hostname+"/faq/requested_file.htm";

	//oRequest.open("GET",sURL,false);
	//oRequest.setRequestHeader("User-Agent",navigator.userAgent);
	//oRequest.send(null)

	//if (oRequest.status==200) alert(oRequest.responseText);
	//else alert("Error executing XMLHttpRequest call!");

	//var oRequest = new XMLHttpRequest();
	//oRequest.open( method, URL, async )



	if (searchString.length==0)
	  { 
	  document.getElementById("testbox").innerHTML="kuk";
	  return;
	  }
	try
	  {// Firefox, Opera 8.0+, Safari, IE7
	  xmlHttp=new XMLHttpRequest();
	  }
	catch(e)
	  {// Old IE
	  try
	    {
	    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  catch(e)
	    {
	    alert ("Your browser does not support XMLHTTP!");
	    return;  
	    }
	  }
	  //http://www.wowhead.com/?item="+searchString+"&xml
	var url="";
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	document.getElementById("testbox").innerHTML=xmlHttp.responseText;

}
*/


