// Place for shared utility functions to keep them out of page code.

var realms = ['Aegwynn','Aerie Peak','Agamaggan','Aggramar','Ahn\'Qiraj','Akama','Al\'Akir','Alexstrasza','Alleria','Alonsus','Altar of Storms','Alterac Mountains','Aman\'Thul','Ambossar','Anachronos','Andorhal','Anetheron','Antonidas','Anub\'arak','Anvilmar','Arak-arahm','Arathi','Arathor','Archimonde','Area 52','Argent Dawn','Arthas','Arygos','Aszune','Auchindoun','Azgalor','Azjol-Nerub','Azshara','Azuremyst','Baelgun','Balnazzar','Barthilas','Black Dragonflight','Blackhand','Blackmoore','Blackrock','Blackwater Raiders','Blackwing Lair','Blade\'s Edge','Bladefist','Bleeding Hollow','Blood Furnace','Bloodfeather','Bloodhoof','Bloodscalp','Blutkessel','Bonechewer','Boulderfist','Bronze Dragonflight','Bronzebeard','Burning Blade','Burning Legion','Burning Steppes','Caelestrasz','C\'Thun','Cenarion Circle','Cenarius','Chants éternels','Cho\'gall','Chromaggus','Coilfang','Confrérie du Thorium','Conseil des Ombres','Crushridge','Culte de la Rive noi','Daggerspine','Dalaran','Dalvengyr','Dark Iron','Darkmoon Faire','Darksorrow','Darkspear','Darrowmere','Das Syndikat','Dath\'Remar','Deathwing','Defias Brotherhood','Demon Soul','Dentarg','Der abyssische Rat','Der Mithrilorden','Der Rat von Dalaran','Destromath','Dethecus','Detheroc','Die Aldor','Die Arguswacht','Die ewige Wacht','Die Nachtwache','Die Silberne Hand','Die Todeskrallen','Doomhammer','Draenor','Dragonblight','Dragonmaw','Drakkari','Drak\'thul','Draka','Drek\'Thar','Drenden','Dun Modr','Dun Morogh','Dunemaul','Durotan','Duskwood','Earthen Ring','Echo Isles','Echsenkessel','Eitrigg','Eldre\'Thalas','Elune','Emerald Dream','Emeriss','Eonar','Eredar','Executus','Exodar','Farstriders','Feathermoon','Fenris','Festung der Stürme','Firetree','Forscherliga','Frostmane','Frostmourne','Frostwhisper','Frostwolf','Garithos','Garona','Genjuros','Ghostlands','Gilneas','Gnomeregan','Gorefiend','Gorgonnash','Greymane','Grim Batol','Gul\'dan','Gurubashi','Hakkar','Haomarush','Hellfire','Hellscream','Hydraxis','Hyjal','Icecrown','Illidan','Jaedenar','Jubei\'Thos','Kael\'thas','Kalecgos','Karazhan','Kargath','Kazzak','Kel\'Thuzad','Khadgar','Khaz Modan','Khaz\'goroth','Kil\'Jaeden','Kilrogg','Kirin Tor','Kor\'gall','Korgath','Korialstrasz','Krag\'jin','Krasus','Kul Tiras','Kult der Verdammten','La Croisade écarlate','Laughing Skull','Les Clairvoyants','Les Sentinelles','Lethon','Lightbringer','Lightning\'s Blade','Lightninghoof','Llane','Lordaeron','Los Errantes','Lothar','Madmortem','Madoran','Maelstrom','Magtheridon','Maiev','Mal\'Ganis','Malfurion','Malorne','Malygos','Mannoroth','Marécage de Zangar','Mazrigos','Medivh','Minahonda','Misha','Mok\'Nathal','Molten Core','Moon Guard','Moonglade','Moonrunner','Mug\'thol','Muradin','Nagrand','Nathrezim','Naxxramas','Nazgrel','Nazjatar','Nefarian','Neptulon','Ner\'zhul','Nera\'thor','Nethersturm','Nordrassil','Norgannon','Nozdormu','Onyxia','Outland','Perenolde','Proudmoore','Quel\'dorei','Quel\'Thalas','Ragnaros','Rajaxx','Rashgarroth','Ravencrest','Ravenholdt','Rexxar','Rivendare','Runetotem','Sargeras','Scarlet Crusade','Scarshield Legion','Scilla','Sen\'Jin','Sentinels','Shadow Council','Shadowmoon','Shadowsong','Shandris','Shattered Halls','Shattered Hand','Shattrath','Shen\'dralar','Shu\'halo','Silver Hand','Silvermoon','Sinstralis','Sisters of Elune','Skullcrusher','Skywall','Smolderthorn','Spinebreaker','Spirestone','Sporeggar','Staghelm','Steamwheedle Cartel','Stonemaul','Stormrage','Stormreaver','Stormscale','Sunstrider','Suramar','Sylvanas','Taerar','Talnivarr','Tanaris','Tarren Mill','Teldrassil','Temple noir','Terenas','Terokkar','Terrordar','Thaurissan','The Forgotten Coast','The Maelstrom','The Scryers','The Sha\'tar','The Underbog','The Venture Co','The Venture Co.','Theradras','Thorium Brotherhood','Thrall','Throk\'Feroth','Thunderhorn','Thunderlord','Tichondrius','Tirion','Todeswache','Tortheldrin','Trollbane','Turalyon','Twilight\'s Hammer','Twisting Nether','Tyrande','Uldaman','Uldum','Un\'Goro','Undermine','Ursin','Uther','Varimathras','Vashj','Vek\'lor','Vek\'nilash','Velen','Vol\'Jin','Warsong','Whisperwind','WildHammer','Windrunner','Wrathbringer','Xavius','Ysera','Ysondre','Zangarmarsh','Zenedar','Zirkel des Cenarius','Zul\'jin','Zuluhed'];

function newToonShow(ua, headerText) {
	
	document.getElementById("createUpdateContainer").style.display = "block";
	document.getElementById("createUpdateContainer").className = "createUpdateFullScreen";
	
	if (ua == "") ua = 1
	document.getElementById('toon').style.display = 'block';
	if (ua == 3) {
		document.getElementById('pwdcol1').style.display = 'inline';
		document.getElementById('pwdcol2').style.display = 'inline';
	} else {
		document.getElementById('pwdcol1').style.display = 'none';
		document.getElementById('pwdcol2').style.display = 'none';
	}
	document.getElementById('uaMode').value = ua;
	document.getElementById("createUpdateHeader").innerHTML = headerText;
	document.getElementById("submitSig").value = "preview sig";
	document.getElementById("realmName").focus();
}
	
function updateToonShow(ua, headerText) {
	
	document.getElementById("createUpdateContainer").style.display = "block";
	document.getElementById("createUpdateContainer").className = "createUpdateFullScreen";
	
	if (ua == "") ua = 2;
	document.getElementById('toon').style.display = 'block';
	document.getElementById('pwdcol1').style.display = 'inline';
	document.getElementById('pwdcol2').style.display = 'inline';
	document.getElementById('uaMode').value = ua;
	document.getElementById("createUpdateHeader").innerHTML = headerText;
	document.getElementById("submitSig").value = "preview sig";
	document.getElementById("realmName").focus();
}


function userSig(sigaction, formaction, actiontext) {
	
	document.getElementById("createUpdateContainer").style.display = "block";
	document.getElementById("createUpdateContainer").className = "createUpdateFullScreen";
	document.getElementById('toon').style.display = 'block';
	document.getElementById("createUpdateHeader").innerHTML = actiontext;
	
	switch (sigaction) {
		case "create":
			ua = 3;
			document.getElementById("userAddSigText").style.display = "block";
			document.getElementById("userCreateSigText").style.display = "none";
			document.getElementById('pwdcol1').style.display = 'inline';
			document.getElementById('pwdcol2').style.display = 'inline';
		break;
		
		case "add":
			ua = 4;
			document.getElementById("userAddSigText").style.display = "none";
			document.getElementById("userCreateSigText").style.display = "block";
		break;
		
		default:
			ua = 3;
		break;
	}
	
	
	document.getElementById("submitSig").value = sigaction + " sig";
	document.getElementById("siggen").action = formaction;	
	document.getElementById("uaMode").value = ua;
	document.getElementById("realmName").focus();
}


function trim(id) {
	stringToTrim = document.getElementById(id).value;
	document.getElementById(id).value = stringToTrim.replace(/^\s+|\s+$/g,"");
}


function validateReq(field,alerttxt) {
	
	
	
	with (field) {
			
		if (value==null||value=="") {
			alert(alerttxt);
			return false;
		} else {
			return true
		}
	}
}
function validate(thisform, session) {
	

	
	ua = document.getElementById("uaMode");
	
	with (thisform) {
		if (validateReq(realm,"Realm name must be filled out")==false) {
			realm.focus();
			return false;
		}
		if (validateReq(name,"Character name must be filled out")==false) {
			name.focus();
			return false;
		}
		if ((ua.value == 2)||(ua.value == 3)) {
			if (validateReq(password,"Password must be filled out")==false) {
				password.focus();
				return false;
			}
		}
	}
	
}


function valPassword(thisform) {

	if (document.getElementById("ua").value != "4") {

		with (thisform) {
			if (validateReq(password,"Password must be filled out")==false) {
				password.focus();
				return false;
			} else {
				thisform.submit();	
			}
		}
	}

}


function userGetCode(type, toonid) {
	targetLocation = document.getElementById("target" + toonid);
	closeItem =  '<div style="float: right"><small><a href="#" onclick="getCodeClose(\''+toonid+'\')"><b>CLOSE</b></a></small></div>';
	switch(type) {
		
		case "code":
			display = '<div class="codebox">' + closeItem + '<b>Use this code to display your ToonSigs on the web</b><br /><br />BB Code:<br /><input id="bb'+toonid+'" onClick="selectAll(this.id)" type="text" size="50" value="[img]http://toonsig.com/sig/' +toonid+ '.jpg[/img]" /><br />';		
			display = display + 'HTML:<br /> <input id="html'+toonid+'" onClick="selectAll(this.id)" type="text" size="75" value="&lt;img src=&quot;http://toonsig.com/sig/' +toonid+ '.jpg&quot; width=&quot;500&quot; height=&quot;25&quot; /&gt;" /></div>';				
		break;
		case "delete":
			display = 'To delete this toon, enter its password: <form method="post" onsubmit="return confirmDelete();" action="/user_deletesig.php"><input type="hidden" name="t_id" value="'+toonid+'" /><input type="password" name="password" /> <input type="submit" value="delete toon" /> </form> ' + closeItem;
		break;
		
	}
	if (targetLocation.style.display != "none") {
		targetLocation.style.display = "none";
	} else {
		targetLocation.style.display = "block";
	}
	targetLocation.innerHTML = display;
	
}
function getCodeClose(toonid) {
	targetLocation = document.getElementById("target" + toonid);
	targetLocation.style.display = "none";
}
function selectAll(id) {
    document.getElementById(id).focus();
    document.getElementById(id).select();
}


function confirmDelete() {

	var message = confirm("Are you sure you wish to delete this toon?");
	if (message == true) {
		return true;
	} else {
		return false;
	}
	
}



function locLabel (serv) {
	var addr = location.href;
	var addr = addr.toLowerCase();
	var label = addr.match(serv);
	if (label) {
		document.write ("<div id=\"staginglabel\">"); for (i=0; i<100; i++) { document.write(label + " "); } document.write ("</div>");
	}
}


function showhide(targetid) {
	currentState = document.getElementById(targetid).style.display;
	//alert(currentState);
	switch(currentState) {
		case "none":
			document.getElementById(targetid).style.display = "block";
		break;
		case "block":
			document.getElementById(targetid).style.display = "none";		
		break;
	}
}


function statusUpdate(newText) {	
	currentText = document.getElementById("statusupdate").innerHTML;	
	document.getElementById("statusupdate").innerHTML = currentText + newText;		
}


function showTools(toolid) {
	document.getElementById(toolid).style.display = "block";
	document.getElementById(toolid).className = "showTools";
}
function hideTools(toolid) {
	document.getElementById(toolid).style.display = "none";
}