var ajaxes = new Array();
if(window.XMLHttpRequest){
	ajaxes[0] = new XMLHttpRequest();
	ajaxes[1] = new XMLHttpRequest();
	ajaxes[2] = new XMLHttpRequest();
	ajaxes[3] = new XMLHttpRequest();
	ajaxes[4] = new XMLHttpRequest();
	ajaxes[5] = new XMLHttpRequest();
	ajaxes[6] = new XMLHttpRequest();
}else if(window.ActiveXObject){
	ajaxes[0] = new ActiveXObject("Microsoft.XMLHTTP");
	ajaxes[1] = new ActiveXObject("Microsoft.XMLHTTP");
	ajaxes[2] = new ActiveXObject("Microsoft.XMLHTTP");
	ajaxes[3] = new ActiveXObject("Microsoft.XMLHTTP");
	ajaxes[4] = new ActiveXObject("Microsoft.XMLHTTP");
	ajaxes[5] = new ActiveXObject("Microsoft.XMLHTTP");
	ajaxes[6] = new ActiveXObject("Microsoft.XMLHTTP");
}

function evalAJAXHtml(source,nr){
	ajaxes[nr].open("GET",source,true);
	ajaxes[nr].onreadystatechange = function(){
		try{
			if(ajaxes[nr].readyState==4){
				if (ajaxes[nr].status == 200){
					eval(ajaxes[nr].responseText);
				}
			}
		}
		catch(e){
			//Exception-bug in FF
		}
	}
	ajaxes[nr].send(null);
}
function evalpostAJAXHtml(source,datan,nr){
	ajaxes[nr].open("POST",source,true);
	ajaxes[nr].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxes[nr].setRequestHeader("Content-length", datan.length);
    ajaxes[nr].setRequestHeader("Connection", "close");
	ajaxes[nr].onreadystatechange = function(){
		try{
			if(ajaxes[nr].readyState==4){
				if (ajaxes[nr].status == 200){
					eval(ajaxes[nr].responseText);
				}
			}
		}
		catch(e){
			//Exception-bug in FF
		}
	}
	ajaxes[nr].send(datan);
}

function pullThingsOnLogin(){
	evalAJAXHtml('loginPull.php?cache='+Math.random(),0);
}


function action(type,m){
	updateCookies();
	switch (type){
		case "move":
			evalpostAJAXHtml('move.php?cache='+Math.random(),'dir='+m,3);
		break;
	}
}

function subOptions(){
	updateCookies();
	if ($('optionsPassword').value != ""){
		var data = "p="+$('optionsPassword').value+"&np="+$('optionsNewPassword').value+"&np2="+$('optionsNewPassword2').value+"&chatUpdate="+$('chatUpdateSpeed').value;
		evalpostAJAXHtml("options.php?r="+Math.random(),data,3);
	}
	else{
		alert("You have to fill in your password to change options!");
	}
}

function donateGold(){
	updateCookies();
	if ($('clandonate').value!=''){
		
		var donate = $('clandonate').value;
		donate = donate.replace(/k/g,"000");
		donate = donate.replace(/,/g,"");
		donate = donate.replace(/\./g,"");
		donate = donate.replace(/m/g,"000000");
		donate*=1;
		if (donate<=player.secondaryStats.bank){
	        if(confirm("Donate "+donate+" gold?")){
			    var data = "ammount="+donate;
			    evalpostAJAXHtml("clandonate.php?r="+Math.random(),data,3);
			}
		}
		else{
			alert("You do not have that much gold!");
		}
	}
	else{
		alert("Please enter a number!");
	}
}

function clansend(type){
	updateCookies();
	var isLeader = false;
	if (player.clan.leader.toLowerCase()!=player.info.name.toLowerCase() || player.clan.leader2.toLowerCase()!=player.info.name.toLowerCase()){
		isLeader = true;
	}
	if (type=="apply"){
		if (player.clan.name == ""){
			var data = "type=apply";
			evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
		}
		else{
			alert("You are already in a clan!");
		}
	}
	else if(type=="create"){
		if (player.clan.name == ""){
			if ($('createClanName').value!=""){
				var data = "type=create&clanname="+$('createClanName').value;
				evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
			}
			else{
				alert("Please enter a name!");
			}
		}
		else{
			alert("You are already in a clan!");
		}
	}
	else if(type=="coleader"){
		if ($('setCoLeader').value!=""){
			if (player.clan.name != ""){
				if (isLeader){
					var data = "type=coleader&leader="+$('setCoLeader').value;
					evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
				}
				else{
					alert("You are not a leader of your clan!");
				}
			}
			else{
				alert("You are not even in a clan!");
			}
		}
		else{
			alert("You have to enter a name!");
		}
	}
	else if(type=="leave"){
		if (player.clan.name != ""){
			if (isLeader){
				var data = "type=leave";
				evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
			}
			else{
				alert("You are too important to leave!");
			}
		}
		else{
			alert("You are not even in a clan!");
		}
	}
	else if(type=="tax"){
		if (player.clan.name != ""){
			if (isLeader){
				
				var tax = $('clanTax').value;
				tax = tax.replace(/%/g,"");
				tax*=1;
				if (tax<1){
					tax = 0;
				}
				else if (tax>99){
					tax = 100;					
				}
				var data = "type=tax&tax="+tax;
				evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
			}
			else{
				alert("You are not a leader of your clan!");
			}
		}
		else{
			alert("You are not even in a clan!");
		}
	}
	else if(type=="accept"){
		if (player.clan.name != ""){
			if (isLeader){
				if ($('acceptee').value!=""){
					var data = "type=accept&acceptee="+$('acceptee').value;
					evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
				}
				else{
					alert("You have to enter a name!");
				}
			}
			else{
				alert("You are not a leader of your clan!");
			}
		}
		else{
			alert("You are not even in a clan!");
		}
	}
	else if (type=="destroy"){
		if (player.clan.name != ""){
			if (isLeader){
				var data = "type=destroy";
				evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
			}
			else{
				alert("You are not a leader of your clan!");
			}
		}
		else{
			alert("You are not even in a clan!");
		}
	}
	else if (type=="boot"){
		if (player.clan.name != ""){
			if (isLeader){
				var data = "type=boot&bootee="+$('bootee').value;
				evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
			}
			else{
				alert("You are not a leader of your clan!");
			}
		}
		else{
			alert("You are not even in a clan!");
		}
	}
	else if(type=="vault"){
		if (player.clan.name != ""){
			if (isLeader){
				var wdraw = $('goldee').value;
				wdraw = wdraw.replace(/k/g,"000");
				wdraw = wdraw.replace(/,/g,"");
				wdraw = wdraw.replace(/\./g,"");
				wdraw = wdraw.replace(/m/g,"000000");
				wdraw*=1;
				var data = "type=withdraw&goldee="+wdraw;
				evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
			}
			else{
				alert("You are not a leader of your clan!");				
			}
		}
		else{
			alert("You are not even in a clan!");
		}
	}
	else if(type=="bonus1"){
		if (player.clan.name != ""){
			if (isLeader){
				var data = "type=bonus1&bonus1="+$('bonus1').value;
				evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
			}
			else{
				alert("You are not a leader of your clan!");				
			}
		}
		else{
			alert("You are not even in a clan!");
		}
	}
	else if(type=="bonus2"){
		if (player.clan.name != ""){
			if (isLeader){
				var data = "type=bonus2&bonus2="+$('bonus2').value;
				evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
			}
			else{
				alert("You are not a leader of your clan!");				
			}
		}
		else{
			alert("You are not even in a clan!");
		}
	}	else if(type=="bonus3"){
		if (player.clan.name != ""){
			if (isLeader){
				var data = "type=bonus3&bonus3="+$('bonus3').value;
				evalpostAJAXHtml("clanmanage.php?r="+Math.random(),data,2);
			}
			else{
				alert("You are not a leader of your clan!");				
			}
		}
		else{
			alert("You are not even in a clan!");
		}
	}
}

function questSend(t,i){
	updateCookies();
	var data = "t="+t+"&i="+i;
	evalpostAJAXHtml("quests.php?r="+Math.random(),data,2);
}

function itemSubmit(){
	updateCookies();
	var data="items=1"; //Just so we can say it's a safe bet we should use & in the other querystrings.
	var slots = new Array('Helmet','Weapon1','Armor','Weapon2','Spell1','Legs','Spell2','Gauntlets','Cape','Boots','Special1','Special2');
	for (a=0;a<$('eqForm').elements.length;a++){
		if ($('eqForm').elements[a].value!=""){
			data+="&"+slots[a]+"="+$('eqForm').elements[a].value;
		}
	}
	evalpostAJAXHtml("itemCheck.php?r="+Math.random(),data,3);
}

function shopMagic(){
	updateCookies();
	x = $('item').value*1;
	if (x<10){
		var APrice = Math.pow(x+2,4)
		var APrice = Math.round(APrice+1)
	}else if(x < 50){
		APrice = (Math.pow(x+2,4.65)-16000)
		APrice = Math.round((APrice+1))
	}
	else{
		var APrice = (Math.pow(x+2,4.79)-65999999)
		var APrice = Math.round((APrice+1))
	}
	if (player.secondaryStats.gold>=APrice){
		if(confirm("Buy this IC "+(x+1)+" "+$('weapontype').value+"?")){
		    var data="type="+$('weapontype').value+"&whichone="+x;
		    evalpostAJAXHtml("shopping.php?r="+Math.random(),data,2);
		}
	}
	else{
		alert("You do not have enough gold!");
	}
}

function bankSend(){
	updateCookies();
	var goAhead=1;
	var amt = $('bankAmmount').value;
	amt = amt.replace(/k/g,"000");
	amt = amt.replace(/,/g,"");
	amt = amt.replace(/\./g,"");
	amt = amt.replace(/m/g,"000000");
	amt*=1;
	if (amt<1){
		if ($('bankAction').value == 1 || $('bankAction').value == 3){
			goAhead=0;
			theError = "You have to enter a number higher than 0!";
		}		
	}
	if($('bankAction').value == 0 || $('bankAction').value == 1){
		//maxBank = Math.round(Math.pow((player.stats.lvl/10),2) * 15000);
		//if(amt > maxBank ){
		//	goAhead = 0;
		//	theError = "You have exceeded your maximum amount!";
		//}
	}
	if (goAhead==1){
		var data="type="+$('bankAction').value+"&ammount="+amt;
		evalpostAJAXHtml("bankerrand.php?r="+Math.random(),data,2);
	}
	else{
		alert(theError);
	}
}

function bankTransfer(){
	updateCookies();
	if ($('bankUser').value!=""){
		var transfer = $('bankUserAmmount').value;
		transfer = transfer.replace(/k/g,"000");
		transfer = transfer.replace(/,/g,"");
		transfer = transfer.replace(/\./g,"");
		transfer = transfer.replace(/m/g,"000000");
		transfer*=1;
		if (transfer>0){
			if ($('bankUser').value.toLowerCase()!=player.info.name.toLowerCase()){
			    if(confirm("Send "+$('bankUserAmmount').value+" gold to "+$('bankUser').value+" ?")){
				    var data="player="+$('bankUser').value+"&ammount="+transfer;
				    evalpostAJAXHtml("banktransfer.php?r="+Math.random(),data,2);
				}
			}
			else{
				alert("You cannot send gold to yourself!");
			}
		}
		else{
			alert("You have to enter a number higher than 0! ");
		}
	}
	else{
		alert("You have to enter a player\'s name!");
	}
}

function shopItems(){
	updateCookies();
	var x = $('item').value*1;
	if ($('weapontype').value!="Cape"){
		if (x<10){
			var APrice = Math.pow(x+2,4)
			var APrice = Math.round((APrice+1))
		}else if(x < 50){
			APrice = (Math.pow(x+2,4.65)-16000)
			APrice = Math.round((APrice+1))
		}
		else{
			var APrice = (Math.pow(x+2,4.79)-65999999)
			var APrice = Math.round((APrice+1))
		}
	}
	else{
		x = ((x+1)*5)-1;
		var APrice = (x+1)*10000000;
	}
	
	if (player.secondaryStats.gold>=APrice){
	    if(confirm("Buy this IC "+(x+1)+" "+$('weapontype').value+"?")){
		    var data="type="+$('weapontype').value+"&whichone="+x;
		    evalpostAJAXHtml("shopping.php?r="+Math.random(),data,2);
		}
	}
	else{
		alert("You do not have enough gold! '"+APrice+"'");
	}
}

function shopSell(){
	updateCookies();
	if (confirm("Do you really want to sell this item?")){
		if($('sell').value){
			var data="id="+$('sell').value;
			evalpostAJAXHtml("shopSell.php?r="+Math.random(),data,2);
		}
	}
}

function switchZone(){
	var exit = -1;
	for (k=0;k<player.arrays.exits[0].length;k++){
		if (player.arrays.exits[0][k]==player.located.x && player.arrays.exits[1][k]==player.located.y){
			exit = k;
		}
	}
	if (exit!=-1){
		var data="zone="+player.arrays.exits[2][exit];
		evalpostAJAXHtml("entrance.php?r="+Math.random(),data,2);
	}
	else{
		alert("You are not at an entrance!");
	}
}

function sendShrine(){
	var data = "revive=1";
	evalpostAJAXHtml("shrine.php?r="+Math.random(),data,2);
}

function sendEnchantScroll(){
    if(confirm("Enchant this item?")){
	    var data = "item="+$('enchantItems').value+"&scroll="+$('elScrollos').value;
	    evalpostAJAXHtml("enchant.php?r="+Math.random(),data,3);
	}
}

function sendLevelUp(){
	var data = "str="+player.maintenance.str+"&dex="+player.maintenance.dex+"&vit="+player.maintenance.vit+"&knw="+player.maintenance.knw+"&mana="+player.maintenance.mana+"&wis="+player.maintenance.wis;
	evalpostAJAXHtml("levelup.php?r="+Math.random(),data,2);
}

function addSkill(index){
    index++;
    var data = "skill="+index;
    evalpostAJAXHtml("addskill.php?r="+Math.random(),data,3);
}

function buyTrade(){
	var data = "tradeId="+$('receiveTrade').value;
	evalpostAJAXHtml("buytrade.php?r="+Math.random(),data,3);
}

function addTrade(itemID,price,sentTo){
	for (i=0;i<player.arrays.trades[0].length;++i){
       if(player.arrays.trades[1][i] == itemID){
           alert("Item already in trade!");
           return false;
       }         
    }
    price = price.replace(/k/g,"000");
	price = price.replace(/,/g,"");
	price = price.replace(/\./g,"");
	price = price.replace(/m/g,"000000");
    if((price*1)>=0){
        if(confirm("Send the item to "+sentTo+" for "+addCommas(price)+"?")){
	        var data = "itemID="+itemID+"&price="+price+"&sentTo="+sentTo+"&sender="+player.info.name+"&r="+Math.random();
	        evalpostAJAXHtml("addtrade.php?r="+Math.random(),data,3);
	    }
	}else{
	    alert("Invalid input!");
	}
}

function deleteTrade(tradeID,itemID){
    if(confirm("Delete this trade?")){
        var data = "tradeID="+tradeID+"&itemID="+itemID+"&sender="+player.info.name+"&r="+Math.random();
        evalpostAJAXHtml("deletetrade.php?r="+Math.random(),data,3);
    }
}

function sendRound(n){
	var data='fightType='+$('fightType').value+'&mob='+$('target').value+'&mode='+n;
	evalpostAJAXHtml('battle.php?r='+Math.random(),data,2);
}

function teleZone(){
	var data = "zone="+$('teleZoneID').value;
    evalpostAJAXHtml("teleport.php?r="+Math.random(),data,2);
}

function upgradeThisUnique(){
	var data = "uniqueID="+$('uniqueToUpg').value+"&IC="+$('uniqueIC').value;
    evalpostAJAXHtml("upgradeUnique.php?r="+Math.random(),data,2);
}

function locateThisPlayer(){
	if (player.secondaryStats.gold>=25000){
		var data="player="+$('locatee').value;
		evalpostAJAXHtml("locatePlayer.php?r="+Math.random(),data,2);
	}
	else{
		alert("You don't have enough gold to locate a player!");
	}
}

function checkNews(){
	evalpostAJAXHtml("chatincs/checkNews.php","",6);
}

function sendReinc(){
	var reincCost = 0;
	
	if (player.stats.lvl>50){
		reincCost=100000;
	}
	var specialRaces = new Array("Balrog","Blood Demon","Vampire","Centaur","Archangel","Werewolf","Leviathan","Mermaid");
	
	if (specialRaces.in_array($('racesToWearPantsTo').value)==true){
		reincCost*=10;
	}
	
	if (player.secondaryStats.gold>=reincCost){
		var data = "race="+$('racesToWearPantsTo').value;
		evalpostAJAXHtml("reinc.php?r="+Math.random(),data,2);
	}
	else{
		alert("You don't have enough gold to reincarnate!");
	}
}

function setLastName(){
	if($("ln").value!=""){
		data="lastName="+$("ln").value;
		evalpostAJAXHtml("chatincs/lastname.php?r="+Math.random(),data,2);
	}else{
		alert("You did not enter a last name!");
	}
}

function capthaSend(){
	evalpostAJAXHtml('captcha.php?r='+Math.random(),'answer='+$('mainArea1').getElementsByTagName('input')[0].value,4);
}