var http_request = false;   // ket qua ajax
var out_id = 'pagemain';     // Bien ID chinh cua cac function ajax
var doc_height = getdocumentheight();  // Chieu cao cua document
var ieNOTopera = document.all && navigator.userAgent.indexOf("Opera")==-1; //Xac dinh loai trinh duyet


function MakeRequest(url, functionName, httpType, sendData) {

	http_request = false;
	if (!httpType) httpType = "GET";

	if (window.XMLHttpRequest) { // Non-IE...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/plain');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Khong the gui truy van !');
		return false;
	}

	var changefunc="http_request.onreadystatechange = "+functionName;
	eval (changefunc);
	http_request.open(httpType, url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(sendData);
}



function GetedRequest(inurl, outid, add) {
	if(allow_ajax==1) {
	    var url = page_domain + "/export.php?" + inurl;
	    if (outid) {
			out_id = outid;
		}
		if (add) {
			addcont=1;
		}
		if (out_id=='pagemain') {
			AddBackContent(outid);
			window.parent.frames['_iframe'].location.reload();
			//scroll(0,0);
		}
		
		if(url.indexOf("a=tutorial")>0 || url.indexOf("a=article")>0) {// Kiem tra co phai la trang bai viet khong, cac trang bai viet co the chua code
			is_article_page = true;
		} else {
			is_article_page = false;
		}
		
		
	    showstatusajax();
	    MakeRequest(url, 'PrintResult', 'GET', null);
	    return false;
	} else {
		showerrorbar("Ch&#7913;c n&#259;ng Ajax t&#7841;m th&#7901;i b&#7883; v&#244; hi&#7879;u h&#243;a.  (<B>Chu&#7897;t ph&#7843;i -> Open</b> n&#7871;u mu&#7889;n m&#7903; link.)");
	}
}


function PrintResult() {
    var data;
	if (http_request.readyState == 4) {
		if (http_request.status==200 || http_request.status==0) {
			if(addcont==1) {
			    addcont=0;
			    data = http_request.responseText + old_cont[occount-1];
			} else {
			    data = http_request.responseText;
			}
			document.getElementById(out_id).innerHTML = data;
			
			if(is_article_page==true) {	// Neu la trang bai viet, init lai cac code tren trang
				//SyntaxHighlighter.all();

			}
			
			if (document.getElementById('pagemain').offsetHeight <700) {
				document.getElementById('pagemain').height = 700;
			} else {
				//window.scrollTo(0,0);
				if(out_id=='pagemain') {
					movetotop(0);
				}
			}
			out_id = 'pagemain';			
			

		} else {
			showerrorbar("C&#243; l&#7895;i trong qu&#225; tr&#236;nh k&#7871;t n&#7889;i Ajax v&#7899;i server.  (<B>Chu&#7897;t ph&#7843;i -> Open</b> n&#7871;u mu&#7889;n m&#7903; link.)");
		}
	}
setTimeout("hidestatusajax()",1500);
//hidestatusajax();	

}


function showerrorbar(message) {	// Hien thi bang thong bao o bottom hide bar
			document.getElementById('errorbar').style.visibility = "";
			document.getElementById('errorbar').style.background = "#CCCCCC";
			document.getElementById('errorbar').style.border = "#FF0000 1px solid";
			document.getElementById('errorbar').innerHTML = message;
			setTimeout("hideerrorbar()",5000);
}


function movetotop(ponst) {			// Di chuyen len tren khi noi dung duoc nap xong
	if (ponst==undefined) {
		ponst = 0;
	}
	if(document.body.scrollTop>ponst) {
		setTimeout("movetotop(" + ponst + ")",5);
		window.scrollBy(0,-30);
	}
}

function movetobottom(ponst) { // Di chuyen xuong duoi, cach bottom mot khoang cach nhat dinh
	if (ponst==undefined) {
		ponst = 30;
	}
	if((document.body.scrollTop + ponst) < (document.body.scrollHeight - document.body.scrollTop)) {
		setTimeout("movetobottom(" + (ponst+1) + ")",5);
		window.scrollBy(0,ponst);
	}

}



function movescrollby(ponst, speed) {	// Di chuyen scroll bar cach vi tri cu ponst
	if(ponst>0) {
		ponst = ponst - speed;
		window.scrollBy(0,speed);
		setTimeout("movescrollby(" + ponst + ", " + speed + ")",5);
	}
}

function getdocumentheight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function showstatusajax() {
	document.getElementById("statusbar").style.visibility =""
}

function hidestatusajax() {
	document.getElementById("statusbar").style.visibility="hidden"
}

function hideerrorbar() {
	document.getElementById('errorbar').style.visibility = "hidden";
}