function sort_by(sort_name,where){
	document.getElementById('loading').innerHTML = "processing<br/><img style=\"margin-left:10px;\" src=\"images/loading.gif\">";	

	if(where == 'search'){
		string = document.getElementById('query_string').value;
	}
	else{
		string = "";
	}
	sort_xmlhttpPost("ajax/sort_by.php","sort_by",sort_name,string);
}

function sort_xmlhttpPost(strURL,elementID,sort_name,query_string) {
	prev = document.getElementById('prev_sort').value;
	sort_type = document.getElementById('prev_sort_type').value;
	if(prev == sort_name){
		if(sort_type == 'asc'){
			sort_type = 'desc';
		}
		else if(sort_type == 'desc'){
			sort_type = 'asc';
		}
	}
	else{
		sort_type = 'desc';
	}

	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById(elementID).innerHTML = self.xmlHttpReq.responseText;
				document.getElementById('prev_sort').value = sort_name;
				document.getElementById('prev_sort_type').value = sort_type;
				first_page_num("ajax/display_first_page_num.php","page_num_span",sort_name,sort_type,query_string);
			}
		}

		self.xmlHttpReq.send('input= '+sort_name+"|"+sort_type);
		
}
function first_page_num(strURL,elementID,sort_name,sort_type,query_string){
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById(elementID).innerHTML = self.xmlHttpReq.responseText;
				first_page_info("ajax/display_first_page_info.php","page_span",sort_name,sort_type,query_string);
			}
		}
		self.xmlHttpReq.send('input= '+query_string);
}

function first_page_info(strURL,elementID,sort_name,sort_type,query_string){
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById(elementID).innerHTML = self.xmlHttpReq.responseText;
				wiki_paging("ajax/display_first_paging.php","rank",sort_name,sort_type,query_string);
			}
		}
		self.xmlHttpReq.send('input= '+query_string);	
}

function wiki_paging(strURL,elementID,sort_name,sort_type,query_string){
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
		self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById(elementID).innerHTML = self.xmlHttpReq.responseText;
				document.getElementById('loading').innerHTML = "";	
			}
		}
		self.xmlHttpReq.send('input= '+sort_name+"|"+sort_type+"|"+query_string);	
}