//	|---------------------------------------------------------------------------|
//	|	PHP-Fusion 7 Content Management System									|
//	|	Copyright © 2002 - 2005 Nick Jones										|
//	|	http://www.php-fusion.co.uk/											|
//	|---------------------------------------------------------------------------|
//	| 	This program is released as free software under the						|
//	| 	Affero GPL license. You can redistribute it and/or						|
//	| 	modify it under the terms of this license which you						|
//	| 	can read by viewing the included agpl.txt or online						|
//	| 	at www.gnu.org/licenses/agpl.html. Removal of this						|
//	| 	copyright header is strictly prohibited without							|
//	| 	written permission from the original author(s).							|
//	|---------------------------------------------------------------------------|
//	|	xComments															|
//	|	Copyright © 2008 Rizald 'Elyn' Maxwell									|
//	|	www.NubsPixel.com														|
//	|	Filename : util.js														|
//	|	Read the source easier via Notepad++ 									|
//	|		comment fonts changed - Courrier New, 10pt							|
//	|---------------------------------------------------------------------------|
//	|	For more informations, please refer to README.TXT						|
//	|---------------------------------------------------------------------------|

function gE(id){
	return document.getElementById(id);
}

// fade timing :P
var fadeSteps = 44; var fadeDelay = 44; 
var fadeElem;
var xsb_self = false;
var xsb_edt = 0; var xsb_frs = 0; 
var xsb_las = 0; var intr; 

function htmlentities(text){
	text = text.split("%20").join("");
	return text.split("&").join("[and]");
}

function clear(){
	gE('error').innerHTML = "";
}

// stolen from Panda  :D
function ajaxRequest(content_type,c,method,url,async,funct,param) {
	if (c) {
		c.open(method,url,async);
		c.onreadystatechange = funct;
		if(content_type!=null)
			c.setRequestHeader("Content-Type",content_type);
		c.send(typeof(param)=="undefined"?null:param);
	}
}

function initXmlHttp(){
	var xmlhttp=false;
	if(window.ActiveXObject){
		var aVers = ["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
		for (var i=0;i<aVers.length&&!xmlhttp;i++){
			try{ xmlhttp=new window.ActiveXObject(aVers[i]); }catch(e){ xmlhttp=false; }
		}
		if (!xmlhttp){
			try{ xmlhttp = window.createRequest(); }
			catch(e){ xmlhttp=false; }
		}
	}
	if (!xmlhttp){
		if (typeof XMLHttpRequest != "undefined"){ xmlhttp = new XMLHttpRequest(); }
	}
	return xmlhttp;
}

function post(user_id, comment_item_id, comment_type){
	var cM = gE('comment_message').value;	
	gE('comment_message').value = '';
	var cid = '&cid='+comment_item_id+'&';
	var ctype = '&ctype='+comment_type+'&';
	cM = htmlentities(cM);
	var sn = '';var cp = '';var ne = false; var ed = 0;
	if ( user_id != 0 )
		sn = 'comment_name='+user_id+'&';
	else
		sn = 'comment_name='+gE('comment_name').value+'&';
	if(gE('captcha_code') == undefined)
		cp = '';
	else{
		cp = 'captcha_code='+gE('captcha_code').value+'&';
		gE('captcha_code').value = '';
	}
	
	if (gE('xcomment') != undefined && gE('xcomment') != null ){
		var width=gE('xcomment').offsetWidth;
		var height=gE('xcomment').offsetHeight;
	}
	gE('x_notice').style.visibility = 'visible';
	gE('x_notice').style.opacity = 1;
	gE('x_notice').style.width = width+'px';
	gE('x_notice').style.height = height+'px';
	
	// change this part if you are using different loading image.
	width = (width-100)/2;
	height = (height-100)/2;
	
	gE('load_img').style.left = width+'px';
	gE('load_img').style.top = height+'px';
	gE('comment_message').disabled = true;
	gE('post_comment').disabled = true;
	
	var xmlElemn = initXmlHttp();
	ajaxRequest('application/x-www-form-urlencoded',xmlElemn,'POST', INCLUDES+'comments_post.php',true,function(){}, cid+ctype+sn+cp+'post_comment=1&'+'comment_message='+cM );
	setTimeout('add("'+user_id+'", "'+comment_item_id+'", "'+comment_type+'")', 5000);
}

function add(user_id, comment_item_id, comment_type){
	var iN = gE('test').innerHTML;
	var xmlElem = initXmlHttp();
	ajaxRequest(null,xmlElem,'GET', INCLUDES+'json_page.php?cid='+comment_item_id+'&ctype='+comment_type,true,function(){
		if (xmlElem.readyState==4 && xmlElem.status==200){
			var xmlObj = xmlElem.responseText;
			var g = eval("("+ xmlObj +")");
			if (g.error){
				gE('test').innerHTML = '<tr><td align="center"><strong>'+g.error+'<strong><\/i><\/td><\/tr>'+iN;
			}
			else{
				if (g.comments_order == 0)
					gE('test').innerHTML = g.comments+iN;
				else	
					gE('test').innerHTML = iN+g.comments;
			}
		}	
	},null);
	if(gE('sb_captcha_code') != undefined)
		gE('sb_captcha').src = '".INCLUDES."securimage/securimage_show.php?sid=' + Math.random();
	gE('comment_message').disabled = false;
	gE('post_comment').disabled = false;
	gE('x_notice').style.visibility = 'hidden';
	gE('x_notice').style.opacity = 0;
	gE('x_notice').style.height = '0px';
	gE('x_notice').style.width = '0px';
}