tinyMCE.init({
			mode : "textareas",
			theme : "advanced",
			theme_advanced_buttons1 : "bold,italic,underline,separator,fontselect,fontsizeselect,forecolor,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,undo,redo,link,unlink",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style|color]"
});

function toggleEditor(id) {
	if (!tinyMCE.getInstanceById(id)) {
		tinyMCE.execCommand('mceAddControl', false, id);
	} 
}


//--------------------------------------------------------------------------//
// XMLHttpRequest Object Pool
//
// @author    legend <legendsky@hotmail.com>
// @link      http://www.ugia.cn/?p=85
// @Copyright www.ugia.cn
//--------------------------------------------------------------------------//

var XMLHttp = {
    _objPool: [],
	
	_getInstance: function ()
    {
        for (var i = 0; i < this._objPool.length; i ++) {
            if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4) {
                return this._objPool[i];
            }
        }

        // IE5 - do not support push method
        this._objPool[this._objPool.length] = this._createObj();
        return this._objPool[this._objPool.length - 1];
    },
	
    _createObj: function () {
        if (window.XMLHttpRequest) {
            var objXMLHttp = new XMLHttpRequest();
        } else {
            var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
            for(var n = 0; n < MSXML.length; n ++) {
                try {
                    var objXMLHttp = new ActiveXObject(MSXML[n]);
                    break;
                } catch(e) { }
            }
         }          

        // some mozilla do notreadyState property
        if (objXMLHttp.readyState == null) {
            objXMLHttp.readyState = 0;
            objXMLHttp.addEventListener(
				"load", 
				function () {
                    objXMLHttp.readyState = 4;
                    if (typeof objXMLHttp.onreadystatechange == "function") {
                        objXMLHttp.onreadystatechange();
                    }
                },
				false);
	        }

    	    return objXMLHttp;
	    },

    	// Send Request (method[post/get], url, [post/get]data, callback function)
	    sendReq: function (method, url, data, callback,id) {
	        var objXMLHttp = this._getInstance();
	
			with(objXMLHttp) {
	            try {
	                // Random prevent cacah
    	            if (url.indexOf("?") > 0) {
	                    url += "&randnum=" + Math.random();
    	            } else {
	                    url += "?randnum=" + Math.random();
    	            }
	
					open(method, url, true);

	                // Request Coding
    	            setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        	        send(data);

	                onreadystatechange = function () {
	                    if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304)) {
	                        callback(objXMLHttp,id);
    	                }
        	        }
            	} catch(e) {
	                alert(e);
    	        }
        	}
    	}
};  

//--------------------------------------------------------------------------//
	function comment_sh(obj,id) {
		if (obj.value=='Leave Comment') {
			obj.value = 'Hide';
			document.getElementById('comment_body_'+id).style.display = 'block';					
			document.getElementById('comment_form_'+id).style.display = 'block';			
		} else {
			obj.value = 'Leave Comment';
			document.getElementById('comment_body_'+id).style.display = 'none';
			document.getElementById('comment_form_'+id).style.display = 'none';
		}
		
		toggleEditor('message_'+id);		
	}

//--------------------------------------------------------------------------//	
	function SubmitForm(id) {
		var parameters = "name=" 		+ encodeURI(document.getElementById("name_"+id).value ) +
						  "&email="		+ encodeURI(document.getElementById("email_"+id).value) +
						  "&homepage="	+ encodeURI(document.getElementById("homepage_"+id).value) +
						  "&message="	+ tinyMCE.activeEditor.getContent() +
						  "&admin="		+ encodeURI(document.getElementById("admin_"+id).value) +
						  "&title="		+ encodeURI(document.getElementById("title_"+id).value) +
						  "&gpid="		+ encodeURI(document.getElementById("gpid_"+id).value);
		XMLHttp.sendReq('POST', '../../comment/post.php',parameters, StateChangeSubmitForm,id);	
		document.getElementById('name_error_'+id).className = 'hidden';
		document.getElementById('email_error_'+id).className = 'hidden';		
	}	

	function StateChangeSubmitForm(obj,id) {
		var str;
		var cntError;
		//alert(obj.responseText);
		
		//Error Response
		str = obj.responseText.split('|')	
		
		
		if (str[0] == 'ERROR') {
			cntError = str[str.length-1];
				
			var strError = new Array(cntError);
				
			for (i=1;i<=cntError;i++) {
				strError[i] = str[i].split('.');
				
				if (strError[i][0] == 'timer') {
					alert(strError[i][1]);
					return;
				}
				
				if (strError[i][0] == 'ip') {
					alert("Your IP address had been block!");
					return;
				}
				
				if (strError[i][0] == 'name') {
					document.getElementById('name_error_'+id).className = '';
					document.getElementById('name_error_'+id).innerHTML = strError[i][1];
					document.getElementById('name_'+id).focus();
				}
					
				if (strError[i][0] == 'email') {
					document.getElementById('email_error_'+id).className = '';
					document.getElementById('email_error_'+id).innerHTML = strError[i][1];
					document.getElementById('email_'+id).focus();
				}
					
				if (strError[i][0] == 'message') {
					alert("No Message??");
				}
			}
		} else {
			var newTotal = eval(document.getElementById('total_'+id).innerHTML)+1;
			document.getElementById('total_'+id).innerHTML = newTotal;			
			ChangePage(id,'1');
			tinyMCE.activeEditor.setContent('');
			alert("Thanks for your post");
		}		
	}
	
//--------------------------------------------------------------------------//
	function ShowComment(id,title,status) {
		XMLHttp.sendReq('GET', '../../comment/comment.php?title='+encodeURI(title)+'&status='+status+'&id='+id, '', StateChangeComment,id);		
	}
		
	function StateChangeComment(obj,id) {
		document.getElementById(id).innerHTML = obj.responseText;
	}
	
//--------------------------------------------------------------------------//
	function ChangePage(id,page) {
		XMLHttp.sendReq('POST', '../../comment/changepage.php?id='+id+'&page='+page,'', StateChangePage,id);
	}
	
	function StateChangePage(obj,id) {
		document.getElementById('comment_body_'+id).innerHTML = obj.responseText;

	}
	
//--------------------------------------------------------------------------//
	function DeletePost(type,msgid,id) {
		if(type==1) {
			XMLHttp.sendReq('POST', '../../comment/deletepost.php?id='+msgid+'&type='+type,'', StateChangeDeletePost,id);
		} else if (type==2) {
			XMLHttp.sendReq('POST', '../../comment/deletepost.php?ip='+msgid+'&type='+type,'', StateChangeDeletePost,id);
		}
	}
	
	function StateChangeDeletePost(obj,id) {
		var newTotal = eval(document.getElementById('total_'+id).innerHTML)-1;
		document.getElementById('total_'+id).innerHTML = newTotal;		
		ChangePage(id,'1');
		alert("Message Deleted!");
	}
	
//--------------------------------------------------------------------------//	
	function ShowTop10(id) {
		XMLHttp.sendReq('POST', 'comment/top10.php','', StateChangeShowTop10,id);
	}
	
	function StateChangeShowTop10(obj,id) {
		document.getElementById(id).innerHTML = obj.responseText;
	}	
//--------------------------------------------------------------------------//	
