function getHTTPObject() {
	var req;

	if(window.XMLHttpRequest){
		// Firefox, Safari, Opera...
		req = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		// Internet Explorer 5+
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		// There is an error creating the object,
		// just as an old browser is being used.
		alert('There was a problem creating the XMLHttpRequest object');
	}
	return req;
}
var http = getHTTPObject(); //init http object

function highlight_active(id) {
	var field = document.getElementById(id);
	field.style.backgroundColor = '#737373';
}

function remove_highlight(id) {
	var field = document.getElementById(id);
	field.style.backgroundColor = '#fff';
}

function show_block (block_id) {
	document.getElementById(block_id).style.display = '';
}

function hide_block (block_id) {
	document.getElementById(block_id).style.display = 'none';
}

function toggle_div (id) {
	var box = document.getElementById(id);
	
	if (box.style.display == 'none') {
		box.style.display = '';
	} else {
		box.style.display = 'none';
	}
}

function disable_submit(input_id) {
	document.getElementById(input_id).disabled = true;
}

function set_checkbox (id) {
	document.getElementById(id).checked = true;
}

function clearSearch () {
	var val = document.getElementById('search').value;
	if (val == "search") {
		document.getElementById('search').value = "";
	}
}

function chkEmptySearch () {
	var val = document.getElementById('search').value;
	if (val == "") {
		document.getElementById('search').value = "search";
	}
}

function clearInput (input_id, default_val) {
	var input = document.getElementById(input_id);
	var val = input.value;
	
	if (val == default_val) {
		input.value = '';
		input.style.color = '#000';
	}
}

function chkEmptyInput (input_id, default_val) {
	var input = document.getElementById(input_id);
	var val = input.value;
	
	if (val == "") {
		input.style.color = '#919191';
		input.value = default_val;
	}
}

function pass_match(p1, p2) {
	var pass = document.getElementById(p1).value;
	var cpass = document.getElementById(p2).value;
	var alertField = document.getElementById('pass_alert');
	
	if (pass != cpass) {
		alertField.style.display = '';
		alertField.innerHTML = 'Password Mismatch';
	} else {
		alertField.style.display = 'none';
		alertField.innerHTML = '';
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function check_length(maxchars, messageID, displayID) {
   var message = document.getElementById(messageID);
   var len = message.value.length;

   if (len > maxchars) {
	 message.value = message.value.substr(0,maxchars);
	 len = maxchars;
   }

   document.getElementById(displayID).innerHTML = maxchars - len;
}

function popUp(targUrl){
	newWindow = window.open(targUrl, '_blank', 'status=no,toolbar=no,location=no,menubar=no');
	if (window.focus) {newWindow.focus()}
}

function setCookie(name, value, expire) {
	var extdate = new Date();
	extdate.setDate(extdate.getDate()+expire);
	
	document.cookie = name + '=' + escape(value) + ((expire == null) ? '' : ";expires="+extdate.toGMTString());
}

function getCookie(name) {
	if (document.cookie.length > 0) {
		var start = document.cookie.indexOf(name + '=');
		
		if (start != -1) {
			start = start + name.length + 1;
			var end = document.cookie.indexOf(';', start);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(start, end));
		}
	}
	
	return '';
}

function toggle_openClose_icon(id, icon) {
	var box = document.getElementById(id);
	
	if (box.style.display == 'none') {
		MM_swapImage(icon, '', 'images/open_icon.gif', 1);
	} else {
		MM_swapImage(icon, '', 'images/close_icon.gif', 1);
	}
}

function swap_image(myId, file){
	tag = document.getElementById(myId);
	tag.src = "images/" + file + ".png";		
}

function pm_check() {
	var query = '';
	http3.open('post', 'ajax/pm_check.php', true);
	http3.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http3.onreadystatechange = pm_display;
	http3.send(query);
}

function pm_display() {
	
	if (http3.readyState == 4) {
		var response = http3.responseText;
		var div = document.getElementById('pm_invite');
		
		if (response != 'false') {
			var arr = response.split(';;');
			
			if (arr.length == 3) {
				var mkup = "<table cellpadding='3' cellspacing='0' border='0'>";
				
				mkup += "<tr><td><a onclick=\"window.open('private_chat.php?conv_id="+arr[0]+"', '', 'width=550,height=385')\" style='cursor: pointer;'><img src='images/fans/"+arr[1]+"/thumbs/thumb_"+arr[2]+"' alt='' /></a></td>";
				//mkup += "<tr><td><a href='private_chat.php?conv_id="+arr[0]+"' target='_blank'><img src='images/fans/"+arr[1]+"/thumbs/thumb_"+arr[2]+"' alt='' border='0' /></a></td>";
				mkup += "<td style='color: #000000;'><strong>"+arr[1]+"</strong><br />Is sending you an instant message<br />";
				mkup += "<div align='right' style='font-weight: bold;'><a onclick=\"window.open('private_chat.php?conv_id="+arr[0]+"', '', 'width=550,height=385')\" style='cursor: pointer;'>Accept</a> &nbsp;";
				mkup += "<a onclick=\"close_invite('"+arr[0]+"');\" style='cursor: pointer;'>Decline</a>";
				mkup += " &nbsp;<a onclick=\"block_sender('"+arr[1]+"', 'pm_invite');\" style='cursor: pointer;'>Block</a></div>";
				mkup += "</td></tr></table>";
				
				div.innerHTML = mkup;
				div.style.display = '';
			}
		} else {
			div.style.display = 'none';
		}
	}
}

function chat_invite() {
	var query = '';
	http2.open('post', 'ajax/chat_invite.php', true);
	http2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http2.onreadystatechange = chat_display;
	http2.send(query);
}

function chat_display() {
	if (http2.readyState == 4) {
		var response = http2.responseText;
		var div = document.getElementById('chat_invite');
		
		if (response != 'false') {
			var arr = response.split(';;');
			
			if (arr.length == 4) {
				var mkup = "<table cellpadding='3' cellspacing='0' border='0'>";
				mkup += "<tr><td><a href='chat.php?band="+arr[0]+"&accept="+arr[3]+"'><img src='images/fans/"+arr[1]+"/thumbs/thumb_"+arr[2]+"' alt='' border='0' /></a></td>";
				mkup += "<td style='color: #000000;'><strong>"+arr[1]+"</strong><br />Would like to invite you<br /> to a chatroom<br />";
				mkup += "<div align='right' style='font-weight: bold;'><a href='chat.php?band="+arr[0]+"&accept="+arr[3]+"' style='color: #000000;'>Accept</a> &nbsp;";
				mkup += "<a onclick=\"close_chat_invite('"+arr[3]+"')\" style='cursor: pointer;'>Decline</a>";
				mkup += " &nbsp;<a onclick=\"block_sender('"+arr[1]+"', 'chat_invite');\" style='cursor: pointer;'>Block</a></div>";
				mkup += "</td></tr></table>";
				
				div.innerHTML = mkup;
				div.style.display = '';
			}
		} else {
			div.style.display = 'none';
		}
	}
}

function close_chat_invite(id) {
	var div = document.getElementById('chat_invite');
	div.style.display = 'none';
	
	var query = 'del='+id;
	http2.open('post', 'ajax/chat_decline.php', true);
	http2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http2.onreadystatechange = close_chat_handle;
	http2.send(query);
}

function close_chat_handle() {return true;}

function close_invite(conv_id) {
	var div = document.getElementById('pm_invite');
	div.style.display = 'none';
	
	var query = 'conv_id='+conv_id;
	http3.open('post', 'ajax/pm_decline.php', true);
	http3.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http3.onreadystatechange = close_handle;
	http3.send(query);
}

function close_handle() {return true;}

function block_sender(uname, div_id) {
	var query = 'block=' + uname + '&d=' + div_id;
	http2.open('post', 'ajax/block_user.php', true);
	http2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http2.onreadystatechange = block_handle;
	http2.send(query);
}

function block_handle() {
	if (http2.readyState == 4) {
		var response = http2.responseText;
		
		var arr = response.split(';;;');
		
		var div = document.getElementById(arr[1]);
		div.innerHTML += "<div align='center' style='font-weight: bold;'>"+arr[0]+"</div>";
	}
}


function toggle_tools() {
	var box = document.getElementById('user_tools');
	var today = new Date();
	var expire = new Date();
	
	expire.setTime(today.getTime() + 3600000 * 24 * 7);
	
	if (box.style.display == 'none') {
		box.style.display = '';
		MM_swapImage('tools_icon', '', 'images/close_icon.gif', 1);
		document.cookie = 'user_tools=true;expires='+expire.toGMTString();
	} else {
		box.style.display = 'none';
		MM_swapImage('tools_icon', '', 'images/open_icon.gif', 1);
		document.cookie = 'user_tools=false;expires='+expire.toGMTString();
	}
}



var http2 = getHTTPObject();
var http3 = getHTTPObject();
chat_invite();
setInterval('chat_invite()', 15000);
pm_check();
setInterval('pm_check()', 10000);

