var buddy;
var newwindow = null;
var hudwindow = null;

function pop_replay(url) {

	if (!newwindow)
    	newwindow = window.open(url,"replayer","location=yes,menubar=no,width=1045,height=790,toolbar=no,scrollbars=yes");
	else if (!newwindow.closed && !$.browser.opera)
		newwindow.location.href = url;
	else
		newwindow = window.open(url,"replayer","location=yes,menubar=no,width=1045,height=790,toolbar=no,scrollbars=yes");
    	
	if (window.focus) {
		newwindow.focus();
	}
}

function pop_replay2(url) {
	//url = 'http://staging' + url.substr(11);

	if (!newwindow)
    	newwindow = window.open(url,"replayer","location=yes,menubar=no,width=936,height=570,toolbar=no");
	else if (!newwindow.closed)
		newwindow.location.href = url;
	else
		newwindow = window.open(url,"replayer","location=yes,menubar=no,width=936,height=570,toolbar=no");
    	
	if (window.focus) {
		newwindow.focus();
	}
}

function pop_hud(site,player) {
	if (!site)
		url = '/web_hud/';
	else
		url = '/web_hud/' + site + '/' + player;
	if (!hudwindow)
    	hudwindow = window.open(url,"WebHUD","location=no,menubar=no,width=840,height=480,toolbar=no,scrollbars=no");
	else if (!hudwindow.closed)
		hudwindow.location.href = url;
	else
		hudwindow = window.open(url,"WebHUD","location=no,menubar=no,width=840,height=480,toolbar=no,scrollbars=no");
    	
	if (window.focus) {
		hudwindow.focus();
	}
}


function pop_share(url) {
    newwindow = window.open(url,"share","location=yes,menubar=no,width=850,height=400,toolbar=no");
	if (window.focus) {
		newwindow.focus();
	}
}

function pop_buddylist(url) {
	if (url != 'old' && $('#logggg').html() == 42) {
		newwindow = window.open("/buddy_list","buddylist","location=no,menubar=no,titlebar=no,toolbar=no,width=410,height=610");		
	} else {
		if ($('#logggg').html() == 42) { 
			newwindow = window.open('/buddy_list',"buddylist","location=no,menubar=no,titlebar=no,toolbar=no,width=390,height=620,toolbar=no");
		} else {
			window.location = "/register.php";
			return false;
		}
	}
	if (window.focus) {
		newwindow.focus();
	}
}

function ajax(url, func, id){
	var url_arr = new Array();
	url_arr = url.split('?');
	xmlhttp=null;
	if (window.XMLHttpRequest){// code for all new browsers
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject){// code for IE5 and IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null){
		xmlhttp.onreadystatechange = function() { 
			if (xmlhttp.readyState==4)
				if (xmlhttp.status==200)
					func(xmlhttp, id);
		}

		xmlhttp.open("GET",url,true);
		//xmlhttp.open("POST",url_arr[0],true);
		//Send the proper header information along with the request
		//xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//xmlhttp.setRequestHeader("Content-length", url_arr[1].length);
		//xmlhttp.setRequestHeader("Connection", "close");
		//xmlhttp.send(url_arr[1]);
		xmlhttp.send(null);
	}
	else{
		alert("Your browser does not support XMLHTTP.");
	}
}

function addbuddy(req, id){
	response = req.responseText;
	if(response == "-1"){
		alert("You must login before adding a player to your Buddy List.");
	}
	else{
		xmlDoc = parseXML(response);
		buddynode = xmlDoc.getElementsByTagName("buddy")[0];
		buddy = buddynode.getAttribute("name");
		sid = buddynode.getAttribute("sid");
		alert(buddy+" has been added to your buddy list!");
		document.getElementById(id).innerHTML="<a href=\"javascript:ajax('/buddies.php?action=del_buddy&buddy="+addslashes(buddy)+"&sid="+sid+"',removebuddy,'"+id+"')\"><img src=\"/css/images/remove.png\" align=\"absmiddle\" border=\"0\" alt=\"Remove "+addslashes(buddy)+" from BuddyList\" title=\"Remove "+addslashes(buddy)+" from BuddyList\"></a>";
	}
}

function removebuddy(req, id){
	response = req.responseText;
	if(response == "-1"){
		alert("You must login before removing a player from your Buddy List.");
	}
	else{
		xmlDoc = parseXML(response);
		buddynode = xmlDoc.getElementsByTagName("buddy")[0];
		buddy = buddynode.getAttribute("name");
		sid = buddynode.getAttribute("sid");
		alert(buddy+" has been removed from your buddy list!");
		document.getElementById(id).innerHTML="<a href=\"javascript:ajax('/buddies.php?action=add_buddy&buddy="+addslashes(buddy)+"&sid="+sid+"',addbuddy,'"+id+"')\"><img src=\"/css/images/add.png\" align=\"absmiddle\" border=\"0\" alt=\"Add "+addslashes(buddy)+" to BuddyList\" title=\"Add "+addslashes(buddy)+" to BuddyList\"></a>";
	}
}

function isbuddy(req, id){
	response = req.responseText;
	if(response != "-1"){
		xmlDoc = parseXML(response);
		buddynode = xmlDoc.getElementsByTagName("buddy")[0];
		buddy = buddynode.getAttribute("name");
		sid = buddynode.getAttribute("sid");
		isbuddy = buddynode.getAttribute("isbuddy");

		if(isbuddy == "1"){
			document.getElementById(id).innerHTML="<a href=\"javascript:ajax('/buddies.php?action=del_buddy&buddy="+addslashes(buddy)+"&sid="+sid+"',removebuddy,'"+id+"')\"><img src=\"/css/images/remove.png\" align=\"absmiddle\" border=\"0\" alt=\"Remove "+addslashes(buddy)+" from BuddyList\" title=\"Remove "+addslashes(buddy)+" from BuddyList\"></a>";
		}
		else{
			document.getElementById(id).innerHTML="<a href=\"javascript:ajax('/buddies.php?action=add_buddy&buddy="+addslashes(buddy)+"&sid="+sid+"',addbuddy,'"+id+"')\"><img src=\"/css/images/add.png\" align=\"absmiddle\" border=\"0\" alt=\"Add "+addslashes(buddy)+" to BuddyList\" title=\"Add "+addslashes(buddy)+" to BuddyList\"></a>";
		}
	}
}

function getalerts(req, id){
	content = "";
	response = req.responseText;
	if(response != "-1"){
		xmlDoc = parseXML(response);
		buddies = xmlDoc.getElementsByTagName("buddy");
		for(var i=0; i<buddies.length; i++){
			buddynode = buddies[i];
			buddy = buddynode.getAttribute("name");
			sid = buddynode.getAttribute("sid");
			sitename = buddynode.getAttribute("site");
			alt = buddynode.getAttribute("alert");
			if(alt == "1"){
				js = "ajax('/buddies.php?action=del_alert&sid="+sid+"&buddy="+buddy+"',removealert,'"+id+"')";
				img = "<img src='/css/images/alerton.png' width='16' height='16' align='absmiddle' border='0' title='Disable Alert'>";
			}
			else{
				js = "ajax('/buddies.php?action=add_alert&sid="+sid+"&buddy="+buddy+"',addalert,'"+id+"')";
				img = "<img src='/css/images/alertoff.png' width='16' height='16' align='absmiddle' border='0' title='Enable Alert'>";
			}
			content += "<div id='buddy_"+i+"'><a href=\"javascript:"+js+"\">"+img+"</a> <a href='/overview/"+buddy+"'>"+buddy +"</a> - "+sitename+"</div>";
		}
	}
	document.getElementById(id).innerHTML=content;
}

function addalert(req, id){
	response = req.responseText;
	if(response == "-1"){
		alert('Sorry, there is a maximum of 5 alerts allowed! Please remove an alert then try again.');
	}
	else{
		alert('Alert Added!');
	}
	ajax('/buddies.php?action=get_buddies_alerts', getalerts, id);
}

function addbuddyandlist(req, id){
	response = req.responseText;
	if(response == "-1"){
		alert("You must login before adding a player to your Buddy List.");
	}
	else{
		xmlDoc = parseXML(response);
		buddynode = xmlDoc.getElementsByTagName("buddy")[0];
		buddy = buddynode.getAttribute("name");
		sid = buddynode.getAttribute("sid");
		alert(buddy+" has been added to your buddy list!");
		document.getElementById('new_buddy_name').value = "";
	}
	ajax('/buddies.php?action=get_buddies_alerts', getalerts, id);
}
function addbuddywrapper(){
	site = document.getElementById('new_buddy_site').value;
	buddy = document.getElementById('new_buddy_name').value;

	if(site == '0'){
		alert('Please select a site.');
		return;
	}
	if(buddy == ''){
		alert('Please enter the buddy name.');
		return;
	}

	url='/buddies.php?action=add_buddy&sid='+site+'&buddy='+buddy;
	ajax(url,addbuddyandlist,'tr_pro_alerts_leftlist');
}

function addusernamewrapper(){
	user = document.getElementById('inputusername').value;
	if(user == ''){
		alert('Please enter a username!');
		return;
	}
	url='/members/member.php?req=saveusername&username='+user;
	ajax(url, addusername, 'username');
}

function addusername(req, id){
	response = req.responseText;
	if(response == "-1"){
		alert("Username already exists.  Please choose another");
	}
	else{
		document.getElementById(id).innerHTML=response;
	}
}

function removealert(req, id){
	alert('Alert Removed!');
	ajax('/buddies.php?action=get_buddies_alerts', getalerts, id);
}

function parseXML(text)
{
	try{ //Internet Explorer
	  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	  xmlDoc.async="false";
	  xmlDoc.loadXML(text);
	}
	catch(e){
		try //Firefox, Mozilla, Opera, etc.
		{
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(text,"text/xml");
		}
		catch(e)
		{
			alert(e.message);
			return -1;
		}
	}
	return xmlDoc;
}

function addslashes(str) {
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\%27/g,'\\%27');
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'&#34;');
	str=str.replace(/\0/g,'\\0');
	str=str.replace(/\&/g,'&amp;');
	return str;
}

