/**
BMForum AJAX Script
Copyright (C) Bluview Technology
*/
var ajaxed = new Array(); 
var ajaxinfo = new Array(); 
var ajaxtmp = new Array();
var tname = new Array();
ajaxtmp['times'] = 0;
/**
Make a request of AJAX
*/
function makeRequest(url,data,funname,httptype) {
	http_request = false;
	
	if (!httptype) httptype = "GET";

	if (window.XMLHttpRequest) { // If IE7, Mozilla, Safari, etc: Use native object
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // ...otherwise, use the ActiveX control for IE5.x and IE6
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Cannot Create an XMLHttp request');
		return false;
	}
	http_request.onreadystatechange = funname;
	http_request.open(httptype, url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(data);

}
/**
Debug Alert Function
*/
function alertContents() {

	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			alert(http_request.responseText);
		} else {
			alert('There was a problem with the request. Check your permission or contact with administrator.\n\n'+http_request.responseText);
		}
	}
}
/**
Modify Thread Title
*/
function bmb_ajax_modifytitle_back() {

	if (http_request.readyState == 4) {
		filename = ajaxtmp['filename'];
		if (http_request.status == 200) {
			document.getElementById("t"+filename).innerHTML = "<span id='t"+filename+"'><a id='forum' onfocus='this.blur()' href='"+filetopn+"?filename="+filename+"'>" +http_request.responseText+"</a></span>";
			tname[filename] = http_request.responseText;
		} else {
			alert('There was a problem with the request. Check your permission or contact with administrator.\n\n'+http_request.responseText);
		}
		ajaxed[filename] = 0;
		ajaxtmp['filename'] = "";
	}
}
function bmb_ajax_tabletitle(threadtitle,filename,sublang,filetopn){
	if (ajaxed[filename] == 1) {
		document.getElementById("t"+filename).innerHTML = ajaxinfo[filename];
		ajaxed[filename] = 0;
	}else{
		ajaxed[filename] = 1;
		ajaxinfo[filename] = document.getElementById("t"+filename).innerHTML;
		document.getElementById("t"+filename).innerHTML = "<input onkeydown='javascript: if((event.ctrlKey && event.keyCode==13) || (event.altKey && event.keyCode == 83)) { document.getElementById(\"s"+filename+"\").click(); }' size='35' type='text' value='"+tname[filename]+"' id='title"+filename+"'>  <input onclick=\"ajaxtmp['filename']="+filename+";this.disabled='disabled';makeRequest('plugins.php?p=ajax&act=modifytitle&filetopn="+filetopn+"&filename="+filename+"&newtitle='+bmb_ajax_encode(document.getElementById('title"+filename+"').value),null,bmb_ajax_modifytitle_back,'');\" type='button' value='"+sublang+"' name='s"+filename+"' id='sub"+filename+"'>";
	}
}
/**
Turn pages
*/
function ajax_gotopages(eventobject,object,filename) {
	if(eventobject.keyCode == 13) {
		bmb_ajax_displaypost(object.value,filename);
	}
}
function bmb_ajax_displaypost_back() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText.split('<div class="bmbajaxpagebar"></div>');
			document.getElementById("posts").innerHTML = result[0];
			document.getElementById("multi_page_bar").innerHTML = result[1];
			document.getElementById("multi_page_bar_2").innerHTML = result[1];
		} else {
			alert('There was a problem with the request. Check your permission or contact with administrator.\n\n'+http_request.responseText);
		}
	}
}
function bmb_ajax_displaypost(pagenum,filename){
	document.getElementById("multi_page_bar").innerHTML = exchanging;
	makeRequest("topic.php?ajax_display=1&filename="+filename+"&page="+pagenum,null,bmb_ajax_displaypost_back,"GET");
	document.getElementById("pageurladdress").value = script_pos+"/topic.php?filename="+filename+"&page="+pagenum;
}
/**
Reply Thread
*/
function bmb_ajax_replythread(formobj){
	document.getElementById("quicksubmit").disabled="disabled";
	submitstring = "ajax_reply=1";
	for (i = 0; i < formobj.elements.length; i++)
	{
		var obj = formobj.elements[i];
		if (obj.name && !obj.disabled)
		{
			if (obj.type != "checkbox") {
				submitstring += '&' + obj.name + '=' + bmb_ajax_encode(obj.value);
			} else {
				if (obj.checked==1) submitstring += '&' + obj.name + '=' + bmb_ajax_encode(obj.value);
			}
		}
	}
	ajaxtmp['filename'] = formobj.filename.value;
	makeRequest("post.php",submitstring,bmb_ajax_display_ajax,"POST");
}
function bmb_ajax_display_ajax(){
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
			makeRequest("topic.php?ajax_display=1&pid="+result+"&filename="+ajaxtmp['filename']+"&page=last",null,bmb_ajax_displaypost_reply_back,"GET");
			document.getElementById("resetform").click();
			clckcnt=0;
		} else {
			document.getElementById("quicksubmit").disabled="";
			clckcnt=0;
			alert('There was a problem with the request. Check your permission or contact with administrator.\n\n'+http_request.responseText);
		}
	}
}
function bmb_ajax_displaypost_reply_back() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
			document.getElementById("posts").innerHTML += result;
			document.getElementById("quicksubmit").disabled="";
		} else {
			clckcnt=0;
			document.getElementById("quicksubmit").disabled="";
			alert('There was a problem with the request. Check your permission or contact with administrator.\n\n'+http_request.responseText);
		}
	}
}
/**
AJAX Online List Refresh
*/
function bmb_ajax_online_back() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.body.style.cursor = '';
			result = http_request.responseText;
			document.getElementById("onlinelist").innerHTML = result;
		} else {
			document.body.style.cursor = '';
			alert('There was a problem with the request. Check your permission or contact with administrator.\n\n'+http_request.responseText);
		}
	}
}
function bmb_ajax_online(filename){
	document.body.style.cursor = 'wait';
	makeRequest(filename,null,bmb_ajax_online_back,"GET");
}
function bmb_ajax_preprocess(urlname,addlinks,allow_ajax_browse){
	if (allow_ajax_browse==1) {
		bmb_ajax_online(urlname+addlinks+"ajax_online=1&online_show="+online_show_anti);
	}else{
		window.location=urlname+addlinks+"online_show="+online_show_anti+"#onlinearea";
	}
	
	if (online_show_anti == "show") {
		online_show_anti = "hide";
	} else {
		online_show_anti = "show";
	}
}
/**
Modify Content
*/
function bmb_ajax_modifycontent(filename){
	document.body.style.cursor = 'wait';

	submitstring = "ajax_content=1";
	submitstring = "&newcontent="+bmb_ajax_encode(document.getElementById("c"+filename).value);
	
	makeRequest("plugins.php?p=ajax&act=modifycontent&filename="+filename,submitstring,bmb_ajax_modifycontent_back,"POST");
}
function bmb_ajax_modifycontent_back() {

	if (http_request.readyState == 4) {
		filename = ajaxtmp['filename'];
		if (http_request.status == 200) {
			document.body.style.cursor = '';
			document.getElementById('sub'+filename).disabled='';
			document.getElementById("text"+filename).innerHTML = http_request.responseText;
			ajaxed[filename] = 0;
			ajaxtmp['filename'] = "";
		} else {
			document.getElementById('sub'+filename).disabled='';
			ajaxtmp['times']++;
			document.body.style.cursor = '';
			alert('There was a problem with the request. Check your permission or contact with administrator.\n\n'+http_request.responseText);
		}
	}
}
function bmb_ajax_getcontent(filename){
	document.body.style.cursor = 'wait';
	makeRequest("plugins.php?p=ajax&times="+ajaxtmp['times']+"&act=getcontent&filename="+filename,null,bmb_ajax_getcontent_back,"GET");
}
function bmb_ajax_getcontent_back() {

	if (http_request.readyState == 4) {
		filename = ajaxtmp['filename'];
		if (http_request.status == 200) {
			document.body.style.cursor = '';
			document.getElementById("text"+filename).innerHTML = "<textarea onkeydown='javascript: if((event.ctrlKey && event.keyCode==13) || (event.altKey && event.keyCode == 83)) { document.getElementById(\"sub"+filename+"\").click(); }' cols='90' rows='20' id='c"+filename+"'>"+http_request.responseText+"</textarea><br /><input onclick=\"this.disabled='disabled';bmb_ajax_modifycontent("+filename+");\" type='button' value='"+ajaxtmp['sublang']+"' id='sub"+filename+"''>";
		} else {
			document.body.style.cursor = '';
			alert('There was a problem with the request. Check your permission or contact with administrator.\n\n'+http_request.responseText);
		}
	}
}
function bmb_ajax_tablecontent(filename,sublang){
	if (ajaxed[filename] == 1) {
		document.getElementById("text"+filename).innerHTML = ajaxinfo[filename];
		ajaxed[filename] = 0;
	}else{
		ajaxed[filename] = 1;
		ajaxtmp['filename'] = filename;
		ajaxtmp['times'] = ajaxtmp['times']+1;
		ajaxtmp['sublang'] = sublang;
		ajaxinfo[filename] = document.getElementById("text"+filename).innerHTML;
		bmb_ajax_getcontent(filename);
	}
}
/**
AJAX Encode URL
*/
function bmb_ajax_encode (str) { //encode string
    str=encodeURIComponent(str);
    if (navigator.product == 'Gecko') str=str.replace(/%0A/g, "%0D%0A"); //In IE, a new line is encoded as rn, while in Mozilla it's n
    return str;
}
