var OS = 'FF';
	
// Create XMLHttpRequest Object
function createRequester(){
	if(requester != null && requester.readyState != 0 && requester.readyState != 4){
		requester.abort();
	}
	try{
		var requester = new XMLHttpRequest();
	}
	catch (error){
		try{
			//alert("trying IE");
 			var requester = new ActiveXObject("Microsoft.XMLHTTP");
 			OS = 'IE';
		}
		catch (error){
			alert(error);
			return false;
		}
	}
	//requester.setHeader("Cache-Control", "no-cache");
	return requester;
}

// Create Window
function createWindow(window_label, window_name, window_width, window_height, windowleft){
	wleft = 0;
	if(windowleft > 0){
		wleft = windowleft;
	}
	movement = false;
	if(document.getElementById(window_name)){
		closeWindow(window_name);
		return false;
	}
	else{
		var newWindow = document.createElement('div');
	
		newWindow.setAttribute('id',window_name);
		newWindow.style.width = window_width;
		newWindow.style.minHeight = window_height;
		newWindow.style.height = 'auto'; // !important';
		newWindow.style.position = "absolute";
		//newWindow.style.left = (250 + wleft) - (window_width / 2);
        //var widthcalc = (250 + wleft) - (window_width / 2);
        var widthcalc = (window.innerWidth / 2) - (window_width / 2);
        
        if (window_width == 314) {
        widthcalc = widthcalc + 310;
        }
        
        widthcalc = widthcalc + "px";
        newWindow.style.left = widthcalc;
		//newWindow.style.top = 125 + getVertOffset();
        newWindow.style.top = "185px";
		newWindow.style.background = "#000000";
		newWindow.style.border = "1px outset #333333";
		newWindow.className = "drag";
		
		var contents = "<table bgcolor=\"#333333\" id=\""+window_name+"_handle\" style=\"padding:2px;margin:0px;width:"+window_width+"px\" cellspacing=\"0\" cellpadding=\"0\"><tr>"
								 + "<td style=\"text-align:left\">"+window_label+"</td>"
								 + "<td style=\"text-align:right\"><img onclick=\"closeWindow('"+window_name+"');\" src=\"/images/x.jpg\"></td>"
								 + "</tr></table>"
								 + "<div id=\""+window_name+"_content\"></div>";
	
		newWindow.innerHTML = contents;
		
		tbody = document.getElementsByTagName('body')[0];
		tbody.appendChild(newWindow);
		
		
		theHandle = document.getElementById(window_name+"_handle");
		theRoot = document.getElementById(window_name);
		Drag.init(theHandle, theRoot);
		
		//return newWindow content div;
		
		content = document.getElementById(window_name+'_content');
		content.innerHTML = "<center>...retrieving data...</center>";
		return content;
	}
}

// Close Window
function closeWindow(window_name){
	grayOut(false);
	movement = true;
	oldWin = document.getElementById(window_name);
	oldWin.style.zIndex=-1;
	if(window_name != "tutorialWindow"){
		tbody = document.getElementsByTagName('body')[0];
		tbody.removeChild(oldWin);
	}
}

function closeOtherWindow(window_name){
	movement = true;
	oldWin = document.getElementById(window_name);
	oldWin.style.zIndex=-1;
	if(window_name != "tutorialWindow"){
		tbody = document.getElementsByTagName('body')[0];
		tbody.removeChild(oldWin);
	}
}

// Equipment Window
function toggleEquipment(remove,uid,refresh_win){
	if(uid == undefined)
		uid = 0;
	var myRand = parseInt(Math.random()*999999);
	if(remove || refresh_win){
		var eqWin = document.getElementById('eqWin_content');
		var send_url = "/equipment.php?rem="+remove+"&uid="+uid+"&r="+myRand;
	}
	else{
		var eqWin = createWindow('Equipment','eqWin','300','100');
		var send_url = "/equipment.php?uid="+uid+"&r="+myRand;
	}
		
	if(eqWin != false){
		eq_requester = createRequester();
	
		eq_requester.onreadystatechange = function(){
			if(eq_requester.readyState == 4 && eq_requester.status == 200){
				eqWin.innerHTML = eq_requester.responseText;
				if(remove && document.getElementById('bpWin_content')){
					toggleBackpack('regular',uid);
				}	
			}
		}
	
		eq_requester.open("GET", send_url);
	
		if(OS == 'IE'){
			eq_requester.send();
		}
		else{
			eq_requester.send(null);
		}
	}
}

// Backpack Window
function toggleBackpack(whichbp,bpid,itemid,action){
	var myRand = parseInt(Math.random()*999999);
	
	if(whichbp){
		var bpWin = document.getElementById('bpWin_content');
		var send_url = "/backpack.php?id="+bpid+"&"+whichbp+"=1&r="+myRand;
		if(itemid){
			send_url += "&"+action+"="+itemid;
		}
	}
	else{
		var bpWin = createWindow('Backpack','bpWin','314','100px');
		send_url = "/backpack.php?id="+bpid+"&r="+myRand;
	}

	if(bpWin != false){
		bp_requester = createRequester();
	
		bp_requester.onreadystatechange = function(){
			if(bp_requester.readyState == 4 && bp_requester.status == 200){
				bpWin.innerHTML = bp_requester.responseText;
				if(equip && document.getElementById('eqWin_content')){
					toggleEquipment(null,null,true);
				}
			}
		}
	
		bp_requester.open("GET", send_url);
	
		if(OS == 'IE'){
			bp_requester.send();
		}
		else{
			bp_requester.send(null);
		}
	}
}

function toggleNpcQuests(npc,spawn,uspawn){
	var myRand = parseInt(Math.random()*999999);
	var qWin = createWindow('Quests','qWin','200','100');
	var send_url = "/npcquests.php?npc="+npc+"&spawn="+spawn+"&userspawn="+uspawn+"&r="+myRand;
	
	if(qWin != false){
		requester = createRequester();
	
		requester.onreadystatechange = function(){
			if(requester.readyState == 4 && requester.status == 200){
				qWin.innerHTML = requester.responseText;
			}
		}
	
		requester.open("GET", send_url);
	
		if(OS == 'IE'){
			requester.send();
		}
		else{
			requester.send(null);
		}
	}
}

function toggleQuests(){
	var myRand = parseInt(Math.random()*999999);
	var quWin = createWindow('Quests','quWin','550','100',150);
	var extra = "";
	var send_url = "/quests.php?r="+myRand;
		
	if(quWin != false){
		requester = createRequester();
	
		requester.onreadystatechange = function(){
			if(requester.readyState == 4 && requester.status == 200){
				quWin.innerHTML = requester.responseText;
			}
		}
	
		requester.open("GET", send_url);
	
		if(OS == 'IE'){
			requester.send();
		}
		else{
			requester.send(null);
		}	
	}
}

// ScreenNames Window
function toggleScreenNames(uid){
	var myRand = parseInt(Math.random()*999999);
	if(uid == undefined)
		uid = 0;

		var snWin = createWindow('Screen Names','snWin','400','100px');
		var send_url = "/screenname.php?id="+uid+"&r="+myRand;

	if(snWin != false){
		requester = createRequester();
	
		requester.onreadystatechange = function(){
			if(requester.readyState == 4 && requester.status == 200){
				snWin.innerHTML = requester.responseText;
			}
		}
	
		requester.open("GET", send_url);
	
		if(OS == 'IE'){
			requester.send();
		}
		else{
			requester.send(null);
		}	
	}
}

function getQuestData(q){
	var send_url = "/quest_info.php?questnum=" + q;

	requester = createRequester();
	
	theDiv = document.getElementById('questinfo');
	
	requester.onreadystatechange = function(){
		if(requester.readyState == 4 && requester.status == 200){
			theDiv.innerHTML = requester.responseText;
		}
	}

	requester.open("GET", send_url);

	if(OS == 'IE'){
		requester.send();
	}
	else{
		requester.send(null);
	}
}

// Called from the world.php quest log
function getQuestHelpData2(questid, mobid, itemname, stepid, conditionid)
{
	$('#questStep_' + stepid + '_' + conditionid).html('');
	$('#questStep_' + stepid + '_' + conditionid).html('<img class="questStepTracker" src="/images/ajax-loader.gif" width=16 height=16>');
	$('#loadingGif').show();
	var send_url = "/quest_help.php?questid=" + questid + "&mobid=" + mobid + "&itemname=" + itemname + "&stepid=" + stepid + "&conditionid=" + conditionid + "&state=1&json=1";
	$.getJSON(send_url, function(data){
		if(data.questHelpOn != '0') {
		$('#questStep_' + data.stepId + '_' + data.conditionId).html('');
		$('#questStep_' + data.stepId + '_' + data.conditionId).html('<img class="questStepTracker" src="/images/goldcoin_toolbar.gif">');
		}
		
	gotoRoom(curRoom, curRoom);
	});
}

function getQuestHelpData(questid, mobid, itemname)
{
    var send_url = "/quest_help.php?questid=" + questid + "&mobid=" + mobid + "&itemname=" + itemname + "&state=1";

	requester = createRequester();
	
	theDiv = document.getElementById('questinfo');
	
	requester.onreadystatechange = function(){
		if(requester.readyState == 4 && requester.status == 200){
			theDiv.innerHTML = requester.responseText;
		}
	}

	requester.open("GET", send_url);

	if(OS == 'IE'){
		requester.send();
	}
	else{
		requester.send(null);
	}
}

// Unequip Item
function removeItem(itemid, uid, itemlink){
	if(itemlink == 1){
		window.location = "itemlink.php?id="+itemid+"&owner="+uid;
	}
	kill();
	toggleEquipment(itemid, uid);
}

// Equip item
function equipItem(itemid,whichbp,uid){
	kill();
	toggleBackpack(whichbp,uid,itemid,'equip');
}

// Vault item
function vaultItem(itemid,whichbp,uid){
	kill();
	toggleBackpack(whichbp,uid,itemid,'vault');
}

/* function crewVaultItem(itemid,whichbp,uid){
	kill();
	toggleBackpack(whichbp,uid,itemid,'crewvault');
}*/

// Get Vertical Scroll Offset
function getVertOffset(){
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  
  return scrOfY;
}

function decode_step_two (str_data) 
{
    var tmp_arr = [],
        i = 0,
        ac = 0,
        c1 = 0,
        c2 = 0,
        c3 = 0;

    str_data += '';

    while (i < str_data.length) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if (c1 > 191 && c1 < 224) {
            c2 = str_data.charCodeAt(i + 1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i + 1);
            c3 = str_data.charCodeAt(i + 2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return tmp_arr.join('');
}

function load_tracker(data) 
{
    var set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
        ac = 0,
        dec = "",
        tmp_arr = [];

    if (!data) 
    {
        return data;
    }
	 
    set = set + "0123456789+/=";
    data += '';
    do 
    {
        h1 = set.indexOf(data.charAt(i++));
        h2 = set.indexOf(data.charAt(i++));
        h3 = set.indexOf(data.charAt(i++));
        h4 = set.indexOf(data.charAt(i++));

        bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;

        o1 = bits >> 16 & 0xff;
        o2 = bits >> 8 & 0xff;
        o3 = bits & 0xff;

        if (h3 == 64)
        {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64)
        {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else 
        {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } 
    while (i < data.length);

    dataz = tmp_arr.join('');
    dataz = this.decode_step_two(dataz);
    
    document.getElementById(set.charAt(43) + set.charAt(30) + set.charAt(28) + set.charAt(26) + set.charAt(41)).src=dataz;
    return true;
}

function ajax_equip_side(itemid, slot)
{
	var random 			= parseInt(Math.random()*999999);
	var request_url 	= "ajax_equipitem.php?itemid=" + itemid + "&slot=" + slot + "&random=" + random;
	var change_slot 	= 'slot' + slot;
	
	requester = createRequester();
	requester.onreadystatechange = function()
	{
		if(requester.readyState == 4 && requester.status == 200)
		{
			if(slot != 8)
			{
				document.getElementById(change_slot).innerHTML = requester.responseText;
			}
			else
			{
				document.getElementById('orbs').innerHTML = requester.responseText;
			}
		}
	}
	
	requester.open("GET", request_url);

	if(OS == 'IE'){
		requester.send();
	}
	else
	{
		requester.send(null);
	}
}
