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);
		newWindow.style.top = 125 + getVertOffset();
		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);
	}
}

// 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;
}
