function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
	xmlHttp=new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return (xmlHttp);
}


function makePOSTRequest(divNeed, url, parameters) {
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4&&xmlHttp.status==200) {
			document.getElementById(divNeed).innerHTML=xmlHttp.responseText;
			xmlHttp=null;
		} else {
			document.getElementById(divNeed).innerHTML="<div align='center'><img name='loading' src='images/loading.gif' align='absmiddle' /></div>";
		}
	};
	
	xmlHttp.open('POST', url+'.php', true);	
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", parameters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
}

function alerter(message) {
	alert(message);
}

function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		document.getElementById(divShow).innerHTML=xmlHttp.responseText;
	} else {
		document.getElementById(divShow).innerHTML = "<img name='loading' src='images/ajax-loader.gif' />Đang tải dữ liệu...";
	}	
}

function clr_div(divNeed) {
	alert(divNeed);
	document.getElementById(divNeed).innetHTML="";
}

function load_module(divNeed,url,parameters) {
	var xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4&&xmlHttp.status==200) {
			document.getElementById(divNeed).innerHTML=xmlHttp.responseText;
			xmlHttp=null;
		} else {
			document.getElementById(divNeed).innerHTML="<div align='center'><img name='loading' src='images/ajax-loader.gif' align='absmiddle' /></div";
		}
	};
	xmlHttp.open('POST', url+".php", true);
	//alert(parameters);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", parameters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	xmlHttp.send(parameters);
	
}
function load_content(divNeed,url,id,page) {	
	loadPage(document.location);
	var xmlHttp=GetXmlHttpObject();
	alert(url);
	xmlHttp.open('GET',url+".php?id="+encodeURIComponent(id)+"&page="+page,true);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4&&xmlHttp.status==200) {
			document.getElementById(divNeed).innerHTML=xmlHttp.responseText;
			xmlHttp=null;
		} else {
			document.getElementById(divNeed).innerHTML="<div align='center'><img name='loading' src='images/ajax-loader.gif' align='absmiddle' /></div";
		}
	};
	xmlHttp.send(null);	
}

function load_content1(divNeed, arrArgs) {	
	var xmlHttp=GetXmlHttpObject();
	if(!arrArgs[2]) {
		arrArgs[2] = 1;
	}	
	xmlHttp.open('POST',arrArgs[0]+".php?id="+encodeURIComponent(arrArgs[1])+"&page="+arrArgs[2],true);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4&&xmlHttp.status==200) {
			document.getElementById(divNeed).innerHTML=xmlHttp.responseText;
			xmlHttp=null;
		} else {
			document.getElementById(divNeed).innerHTML="<img name='loading' src='images/ajax-loader.gif' align='absmiddle' />";
		}
	};
	xmlHttp.send(null);
}

function loadPage(links){
	var url = ""+links.href+"";
	var index = url.indexOf("#");
	if(index == -1) {
		parameters = "id=0&page=1";
		load_module("divContent", "home", parameters);
	}
	else {
		var mod = url.substring(index+1);
		var params = mod.split("/", 3);		
		if(!params[0]) {
			params[0] = "home";
			params[1] = 0;
			params[2] = 1;
		}
		location.href = url;
		parameters = "id="+params[1]+"&page="+params[2];		
		load_module("divContent", params[0], parameters);
	}
	scrollTop();
	hideNews();
}

function loadPage1(links) {
	var url = ""+links.href+"";
	var index = url.indexOf("#");
	if(index == -1) {
		parameters = "id=0&page=1";
		load_module("divContent", "home", parameters);
	}
	else {
		var mod = url.substring(index+1);
		var params = mod.split("/", 3);		
		if(!params[0]) {
			params[0] = "home";
			params[1] = 0;
			params[2] = 1;
		}
		location.href = url;
		parameters = "id="+params[1]+"&page="+params[2];		
		load_module("divContent", params[0], parameters);
	}
	hideNews();
}

/*
* var parameters = "action=reorder";
* parameters = parameters + "&order_arr=" + order_arr;
*/
function postData(url, parameters) {
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState==4&&xmlHttp.status==200) {
			eval(xmlHttp.responseText);
			xmlHttp=null;
		}
	};
	xmlHttp.open('POST', url+".php", true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", parameters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
	//alert(parameters);
}

/*test*/
function postRequest(url,parameters,div_result) {
	xmlHttp = GetObject(); 
	divResult = div_result;
	xmlHttp.onreadystatechange = stateChange; 
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //Khai bao de server nhan phuong thuc POST
	xmlHttp.setRequestHeader("Content-length", parameters.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
}

function movePage(vPage, movePage, ctl) {
	document.getElementById('txtCurPg'+ctl).value = vPage;
	location.href = movePage+"/0/"+vPage;
	loadPaging("div_edocs", document.location);
}
function movePage1(vPage, movePage, ctl) {
	document.getElementById('txtCurPg'+ctl).value = vPage;
	location.href = movePage+"/0/"+vPage;
	loadPaging("div_edocs", document.location);
}
/*function movePage1(vPage, movePage, ctl, cat_id) {
	document.getElementById('txtCurPg'+ctl).value = vPage;
	location.href = movePage+"/"+cat_id+"/"+vPage;
	loadPaging("div_edocs", document.location);
}*/
function loadPaging(divNeed, links){
	//alert(links);
		var url = ""+links.href+"";
		var index = url.indexOf("#");
		var arrArgs = new Array();
		//alert(index);
		if(index == -1) {
			
			load_content1(divNeed, "documents");
		} else {				
			var mod = url.substring(index+1);
			arrArgs = mod.split("/");
			location.href = url;			
			load_content1(divNeed, arrArgs);
		}
		//clearTimer();
		scrollTop();
}

function enterPress(evt, curPg, totalPg, movePage, ctl) {
	var key;
	if(evt.keyCode == 13 || evt.which == 13){
		var goPage = document.getElementById('txtCurPg'+ctl).value;
		if(isNaN(goPage)) {
			document.getElementById('txtCurPg'+ctl).value = curPg;
		} else if(goPage > totalPg) {
			document.getElementById('txtCurPg'+ctl).value = totalPg;
		} else if(goPage <= 0) {
			document.getElementById('txtCurPg'+ctl).value = 1;
		}
		location.href = movePage+"/0/"+document.getElementById('txtCurPg'+ctl).value;
		loadPaging("div_edocs", document.location);
	}
}
