// JavaScript Document

function findObj(n) { 
	  var x; 
	  if(document.all){x = document.all[n] } 
	  else if(document.getElementById){x = document.getElementById(n) } 
	  else if(document.layers){x = document[n] }
	  return x;
 };

function clearOptions(sel){
	//for( ky in findObj(sel).options) {
	if(findObj(sel).options.length >=0) {
		for(var i=findObj(sel).options.length; i>=0 ; i--) {
			findObj(sel).options[i] = null;
			}
		findObj(sel).selectedIndex = 0;	
	}
};

function toggleMe(n) {
	n.style.display=='none'?n.style.display='inline':n.style.display='none';
	}


// Main Dark Mana functions

function setMove(x,y) {
	findObj('x').value=x;
	findObj('y').value=y;
	findObj('action').value='move';
	findObj('actform').submit();
}

function speak() {
	findObj('action').value='speak';
	findObj('actform').submit();
}
function sneak() {
	findObj('action').value='sneak';
	findObj('actform').submit();
}
function unsneak() {
	findObj('action').value='unsneak';
	findObj('actform').submit();
}
function scan() {
	findObj('action').value='perceive';
	findObj('actform').submit();
}
function wakeup() {
	findObj('action').value='wakeup';
	findObj('actform').submit();
}
function search() {
	findObj('action').value='search';
	findObj('actform').submit();
}
function meditate() {
	findObj('action').value='meditate';
	findObj('actform').submit();
}

function castSpell() {
	findObj('target').value=findObj('spelltarget').options[findObj('spelltarget').selectedIndex].value;
	findObj('action').value='cast';
	findObj('actform').submit();
	}

function updateMap() {
	findObj('action').value='updatemap';
	findObj('actform').submit();
	}

/* for(key in array)*/

/* aFoo = {'key':'val','key2':'val'}*/


function displaySpellWidget(spell,target) {
	document.write('Cast <select name="spell" id="spell"  onchange="buildSpellTargets();">');
	for(var ky in aSpells) {
		document.write('<option value="'+ky+'" targetFlags="'+aSpellFlags[ky]+'"');
		if(spell == ky) document.write(' selected="selected"');
		document.write('>'+aSpells[ky]+'</option>');
		
	}
	document.write('</select> on&nbsp;<select name="spelltarget" id="spelltarget">');
	document.write('</select> <input type="button" value="Cast" onClick="castSpell();">');
	
	buildSpellTargets(target);
}





function buildSpellTargets(){
	if(findObj('spelltarget').selectedIndex >=0 && !isNaN(findObj('spelltarget').selectedIndex))
		defaultOpt = findObj('spelltarget').options[findObj('spelltarget').selectedIndex].value;
	else defaultOpt = 0;
	
	
	flagKey = findObj('spell').options[findObj('spell').selectedIndex].value;
	
	clearOptions('spelltarget');
	targetFlags = aSpellFlags[flagKey];
	//Self
	
	var i=0;
	if(targetFlags.substr(3,1) == '1') {
		for(ky in user_id)
			findObj('spelltarget').options[++i]= new Option(user_id[ky],ky,false,ky==defaultOpt);
		
		
	}
	//Local Users
	if(targetFlags.substr(1,1) == '1') {
		for(ky in aMapMates) {
			findObj('spelltarget').options[++i] = new Option(aMapMates[ky],ky,false,ky==defaultOpt);
		}
				
	}
	//Area
	if(targetFlags.substr(2,1) == '1') {
		for(ky in map_id) 
			findObj('spelltarget').options[++i]= new Option(map_id[ky],ky,false,ky==defaultOpt);
		
		
		
	}
	
	
	findObj('spelltarget').options[0] = null;
}



function setTarget(target) {
	findObj('target').value=target;
	//alert(findObj('target').value);
	findObj('action').value='attack';
}

function setAttack(target) {
	setTarget(target);
	findObj('action').value='attack';
	findObj('actform').submit();
}
function useItem(item_i) {
	findObj('action').value='item';
	findObj('item').value = item_i;
	findObj('actform').submit();
	}
function dropItem(id) {
	if(confirm('Drop this item?')) {
		findObj('action').value='drop';
		findObj('item').value = id;
		findObj('actform').submit();
		}
	}

function selectValue(id){
	
	ret = findObj(id).options[findObj(id).selectedIndex].value;
	return ret;
}
function selectSetValue(id,val) {
	for(i=0;i<findObj(id).options.length;i++){
		if (findObj(id).options[i].value == val) findObj(id).selectedIndex = i;
	}
}


function balanceBGColor(bal, balanceType){
	if(balanceType == 'blue') return '#0000'+balanceBGBasic(bal);
	if(balanceType == 'red') return '#'+balanceBGBasic(bal)+'0000';
	newbal = balanceBGBasic(bal);
	return '#'+newbal+newbal+newbal;
	
}

function balanceBGBasic(bal) {
	greyScale = Math.floor((bal + 100) * 256 / 200);
	if(greyScale > 255) return 'FF';
	bgcolor = (greyScale).toString(16);
	
	if (greyScale < 16) bgcolor = '0'+bgcolor;
	return bgcolor;	
}

var white = '#ffffff';
var black = '#000000';
var blue = '#0000ff';
var gold = '#FBCA02';
var bloodred = '#A90000';
var red = '#FF0000';
var royalblue = '#0000A9';

function balanceColor(bal, balanceType){
	greyScale = Math.floor((bal + 100) * 256 / 200);
	if(balanceType == 'blue') {
		if(greyScale <=153) return white; else return gold;	
	}
	if(balanceType == 'red') {
		if(greyScale <=153) return white; else return royalblue;	
	}
	if(greyScale <=153) return white; else return black;
}
function displayBalance(bal, balanceType) {
	displayBalance(bal, balanceType, 175, 12);
}

function displayBalance(bal, balanceType,width,height){
	if(isNaN(width)) width = 175;
	if(isNaN(height)) height = 12;
	document.write('<table cellpadding="0" cellspacing="0" style="empty-cells: show; height: '+height+'px; border: #888888 solid 1px;overflow: hidden;"><tr>');
	//width: '+Math.floor(width)+'px;  
	iteration = 1;
	slices = Math.floor(200/iteration)
	td_width = Math.floor(width/slices);
	marker = '#FFFF00';
	colorType = 'grey';
	if (bal <= -20) {
		marker = white;
		colorType = 'red';
		}
	if (bal >= 20) {
		marker = white;
		colorType = 'blue';
	}
	
	for(i=-100; i<=100; i+=iteration){
		bgcolor = balanceBGColor(i,colorType);
		fontcolor = balanceColor(i);
		if (bal >= i && bal <= i+iteration) bgcolor = marker;
		document.write('<td style="background-color: '+bgcolor+';color: '+fontcolor+'; padding: 0px; width: '+td_width+'px; overflow: hidden;"><img src="images/blank.gif" width="1" height="1"></td>');
		// 
		//alert('<td style="background-color: '+bgcolor+';color: '+fontcolor+'; padding: 0px; width: '+Math.floor(td_width)+'px; overflow: hidden;">&nbsp;</td>');
		}
	document.write('</tr></table>');
}


function setMapEditor(typ) {
	if(typ == 'w') {
		findObj('name').value = 'River';
		selectSetValue('N',1);
		selectSetValue('S',1);
		selectSetValue('E',1);
		selectSetValue('W',1);
		findObj('description_good').value = 'You are on a river.';
		findObj('description_bad').value = 'You are on a river.';
	} else if(typ == 's') {
		findObj('name').value = 'Ocean';
		selectSetValue('N',1);
		selectSetValue('S',1);
		selectSetValue('E',1);
		selectSetValue('W',1);
		findObj('description_good').value = 'You are on the open sea.';
		findObj('description_bad').value = 'You are on the ocean. Seaweed floats by.';
	} else if(typ == 'm') {
		findObj('name').value = 'Swamp';
		selectSetValue('N',1);
		selectSetValue('S',1);
		selectSetValue('E',1);
		selectSetValue('W',1);
		findObj('description_good').value = 'You are in a swamp.';
		findObj('description_bad').value = 'You are in a mosquito infested cesspit.';
	} else if(typ == 'f') {
		findObj('name').value = 'Field';
		selectSetValue('N',0);
		selectSetValue('S',0);
		selectSetValue('E',0);
		selectSetValue('W',0);
		findObj('description_good').value = 'You are in a field.';
		findObj('description_bad').value = ' 	You are in a barren field.';
	} else if(typ == 'o') {
		findObj('name').value = 'Forest';
		selectSetValue('N',0);
		selectSetValue('S',0);
		selectSetValue('E',0);
		selectSetValue('W',0);
		findObj('description_good').value = 'You are in a forest.';
		findObj('description_bad').value = 'You are in a forest.';
	} else if(typ == 'r') {
		findObj('name').value = 'Road';
		selectSetValue('N',0);
		selectSetValue('S',0);
		selectSetValue('E',0);
		selectSetValue('W',0);
		findObj('description_good').value = 'You are on a well traveled road.';
		findObj('description_bad').value = 'You are on a old road with heavy ruts worn into it.';
	} else if(typ == 'c') {
		findObj('name').value = 'Church';
		selectSetValue('N',0);
		selectSetValue('S',0);
		selectSetValue('E',0);
		selectSetValue('W',0);
		findObj('description_good').value = 'You are in a church of the Light.';
		findObj('description_bad').value = 'You are in a church of the Dark.';
	} else {
		findObj('name').value = '';
		selectSetValue('N',0);
		selectSetValue('S',0);
		selectSetValue('E',0);
		selectSetValue('W',0);
		findObj('description_good').value = '';
		findObj('description_bad').value = '';	
		
	}
	
	
}

 
/// AJAX Code
/*  More info about XMLHttpRequest go here: http://www.w3.org/TR/XMLHttpRequest/    */
var aXMLRequests=new Array();

function newXMLHttpRequest() {
	var oXML;
	try {
		oXML = new XMLHttpRequest();
	} catch (error) {
		try {
			oXML = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (error) {
			return false;
		}
	}
	aXMLRequests.push(oXML);
	return oXML;
}

function setContent(id, cont){
	try{
		document.getElementById(id).innerHTML=cont;
		return true;
		}
	catch(E){
		return false;
		}
	}
	
function readyStateChange(){
	for (key in aXMLRequests) {

		if (aXMLRequests[key].readyState == 4) {
			if (aXMLRequests[key].status == 200) {
				success(aXMLRequests[key].responseText);
			}else{
				failure(aXMLRequests[key].responseText);
			}
			aXMLRequests[key]=null;
			delete aXMLRequests[key];
		}	
		else {
			processing();
			}

	}
	return true;
}
function success(txt){
	return true;
	}
function failure(txt){
	return false;
	}
function processing(){
	return false;
	}
///General Functions
String.prototype.trim=function(){return this.replace(/^ +| +$/g,'');};