function callAHAH(url, pageElement, callMessage, errorMessage) { if (pageElement && callMessage) { document.getElementById(pageElement).innerHTML = callMessage; } try { req = new XMLHttpRequest(); /* e.g. Firefox */ } catch(e) { try { req = new ActiveXObject("Msxml2.XMLHTTP"); /* some versions IE */ } catch (e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); /* some versions IE */ } catch (E) { req = false; } } } req.onreadystatechange = function() {responseAHAH(pageElement, errorMessage);}; req.open("GET",url,true); req.send(null); }
function responseAHAH(pageElement, errorMessage) { if(req.readyState == 4) { if(req.status == 200) { output = req.responseText; if (pageElement) { document.getElementById(pageElement).innerHTML = output;}  } else { if (pageElement) { document.getElementById(pageElement).innerHTML = errorMessage+"\n"+responseText; } } } }
function rate_comment(c_id,rate,score){
	callAHAH("http://www.ffxiah.com/action.php?comment="+c_id+"&rate="+rate+"&score="+score,"com"+c_id,document.getElementById('com'+c_id).innerHTML,"");
}
function rate_object(obj,id,rate,score,element){
	callAHAH("http://www.ffxiah.com/action.php?"+obj+"="+id+"&rate="+rate+"&score="+score,element,document.getElementById(element).innerHTML,"");
}
function captcha(){
	callAHAH("http://www.ffxiah.com/php-captcha.inc.php");
}
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}
function showMemberMenu(){
	document.getElementById('members_popup').style.display='block';
}
function hideMemberMenu(){
	document.getElementById('members_popup').style.display='none';
}
function date_diff(d1,d2){
	var sec=1000;
	var min=1000*60;
	var hour=1000*60*60;
	var day=1000*60*60*24;
	var month=1000*60*60*24*30;
	var diff = Math.ceil(d2.getTime()-d1.getTime());
	if (diff>=month){ return Math.round(diff/month)+' Months'; }
	if (diff>=day){ return Math.round(diff/day)+' Days'; }
	if (diff>=hour){ return Math.round(diff/hour)+' Hours'; }
	if (diff>=min){ return Math.round(diff/min)+ ' Mins'; }
	return Math.round(diff/sec)+' Secs';
}
function number_format(a, b, c, d) {
	// number_format(number, decimals, comma, formatSeparator)
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if(!f[0]) f[0] = '0';
	if(!f[1]) f[1] = '';
	if(f[1].length < b){
		g = f[1];
		for(i = f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j += 3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '': c;
	return f[0] + c + f[1];
}
function sortNum(a, b) { return a - b }
function get_median(a){
	var i=0, m=0, l=3;
	var p = new Array();
	var al = a.length;
	if (al>0){
		while(true){
			if (a[i][1] != a[i][3]){ p[i]=a[i][5].replace(/,/g,""); }
			i++;
			if (i>=al || i>=l){ break; }
		}
		var n=p.length;
		if (n>0){ 
			var h=parseInt(n/2);
			p.sort(function(a,b){return a - b});
			if (n%2==0){ m=(parseInt(p[h])+parseInt(p[h-1]))/2; }
			else { m=p[h]; }
			return number_format(m,0,'.',',');
		}
		else { return m; }
	}
	else { return m; }
}
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
function confirmSubmit()
{
var agree=confirm("Are you sure you wish to continue?");
if (agree)
	return true ;
else
	return false ;
}

function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}
function GetAbsPosition(object) {
var position = new Object;
position.x = 0;
position.y = 0;

if( object ) {
position.x = object.offsetLeft;
position.y = object.offsetTop;

if( object.offsetParent ) {
var parentpos = GetAbsPosition(object.offsetParent);
position.x += parentpos.x;
position.y += parentpos.y;
}
}

position.cx = object.offsetWidth;
position.cy = object.offsetHeight;

return position;
}
function showComposer(obj,sid,charName,subject){
	pos=GetAbsPosition(obj);
	$('#compose_server_id').val(sid);
	$('#compose_char_name').val(charName);
	$('#compose').css('top',pos.y+'px').css('left',(pos.x-150)+'px').css('display','block');
	if (typeof(subject)=="undefined"){
		$('#compose_subject').focus();
	}
	else {
		$('#compose_subject').val(subject);
		$('#compose_body').focus();
	}
}
function toggleBody(id){
	if ($("#"+id).css('display')=='block'){
		$("#"+id).css('display','none');
	}
	else {
		$("#"+id).css('display','block');
	}
}
function closeAds(){
	$('#bottom_ad').css('display','none');
}
function postQuote(id){
	var source;
	var name=$('#poster_name_'+id).html();
	if (!(source=$('#source_'+id).html())){
		$.get('/members/adminAction.php',{ action: 'get_post_source', id: id },
		function(data){ 
			$('#source_'+id).html(data); 
			source=$('#source_'+id).html();
			$('#post_body').val($('#post_body').val()+"[quote="+name+"]"+urldecode(source)+"[/quote]\n");
		});
	} else {
		$('#post_body').val($('#post_body').val()+"[quote="+name+"]"+urldecode(source)+"[/quote]\n");
	}
}
function getRandString(string_length) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}
function getCharSelectBox(){
	var x="<select name=char_id>";
	for(var i in chars){
		s=(main==i)?"selected":"";
		x+="<option value='"+i+"' "+s+">"+chars[i]+"</option>";
	}
	x+="</select>";
	return x;
}
function getLangSelectBox(){
	var x="<select name=lang>";
	for(var i in langs){
		s=(lang==i)?"selected":"";
		x+="<option value='"+i+"' "+s+">"+langs[i]+"</option>";
	}
	x+="</select>";
	return x;
}
function stripHTML(s){	
		var matchTag = /<(?:.|\s)*?>/g;
        return s.replace(matchTag, "");
}
function urldecode( str ) {
 
    var histogram = {};
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
 
    for (replace in histogram) {
        search = histogram[replace]; 
        ret = replacer(search, replace, ret) 
    }
    ret = decodeURIComponent(ret);
    return ret;
}
function alertx(msg,style){
	if (style==null) style='msg_neutral';
	$("#msg").removeClass('hidden').addClass(style).show().html(msg);
	setTimeout("$('#msg').fadeOut('slow')" ,3000)
}
function sendPM(){
	closePM();
	$.ajax({
		   type: "POST",
		   url: "/members/adminAction.php",
		   data: $("#PM-Dialog-Form").serialize(),
		   success: function(msg){
				alertx(msg,'msg_neutral');
		   }
	});
} 
function closePM(){
	$("#PM-Dialog").dialog("close");
	$("#overlay").hide();
	$("object").show();
}
function showPMDialog(sid,charName,subject){
	$("object").hide();
	$('#compose2_server_id').val(sid);
	$('#compose2_char_name').val(charName);
	$("#PM-Dialog").show();
	$("#PM-Dialog").dialog("open");
	if ((typeof subject)=='undefined'){
		$('#compose2_subject').focus();
	}
	else {
		$('#compose2_subject').val(subject);
		$('#compose2_body').focus();
	}
	$("#overlay").fadeIn("fast");
}
function identAvatarChange(){
	var selected = $("#ident-sel option:selected");
	$("#ident-avatar-sel span").hide();
	$("#ident-avatar-"+selected.val()).show();
	$("#ident-avatar-sel").removeClass("hidden");
}
function fbconnected() { 
	window.location="/action.php?fbconnect=1";
} 