lo2.dataTables={};

lo2.datatable=function(name,sortCol,sortDir,pageSize,maxPage,url,filters){
	this.name = name;
	this.sortCol = sortCol;
	this.sortDir = sortDir;
	this.pageSize= pageSize;
	this.maxPage = maxPage;
	this.maxCol  = 0;
	this.url = url;
	this.filters = filters;
	
	this.currentPage = 0;
	this.newPage = 0;
	this.beforeLoad=function(){}
	this.afterLoad=function(){}
	
	lo2.dataTables[name] = this;
	
	for(var a=0;a<10;a++){
		obj = lo2('dt_'+name+'_head'+a);
		if(obj){
			this.maxCol++;
		}
	}
}

lo2.datatable.prototype.page=function(newPage){
	switch(newPage){
		case 'first':
			this.newPage = 0;
			break;
		case 'previous':
			this.newPage = this.currentPage - 1;
			if(this.newPage < 0)	this.newPage = 0;
			break;
		case 'next':
			this.newPage = this.currentPage + 1;
			if(this.newPage > this.maxPage) this.newPage = this.maxPage;
			break;
		case 'last':
			this.newPage = this.maxPage;
			break;
	}
	this.getData();
}

lo2.datatable.prototype.sortOn=function(newColNum){
	lo2.css('dt_'+this.name+'_head'+this.sortCol).unsetClass('dt_sort_asc').unsetClass('dt_sort_desc');
	
	if(newColNum == this.sortCol){
		this.sortDir = (this.sortDir == 'asc')?'desc':'asc';
	}else{
		this.sortCol = newColNum;
		this.sortDir = 'asc';
	}
	lo2.css('dt_'+this.name+'_head'+this.sortCol).setClass('dt_sort_'+this.sortDir);
	this.page('first');
}

lo2.datatable.prototype.updateData=function(name,requestor){
	this.beforeLoad();
	data = requestor.responseText;
	data = 'lo2.dataTables["'+this.name+'"].data='+data+';';
	eval(data);
	
	this.currentPage = parseInt(this.data.current_page)-1;
	this.maxPage     = parseInt(this.data.max_page) - 1;
	this.maxRow      = this.data.max_row;
	
	for(var a = 0;a<this.pageSize;a++){
		var obj = document.getElementById('dt_'+this.name+'_'+a);
		if(obj)
			obj.style.display=(a >= this.maxRow)?'none':'';
	}
	
	document.getElementById('dt_'+this.name+'_page_label').innerHTML = 'Page '+(this.currentPage + 1)+' of '+(this.maxPage + 1);
	
	for(var key in this.data.insert_data){
		$('#'+key).html(this.data.insert_data[key]);
	}
	this.afterLoad();
}

lo2.getXmlRequestor=function(){
	if (window.XMLHttpRequest)
		return new XMLHttpRequest();
	else if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
}



lo2.datatable.prototype.getData=function(){
	dtName = this.name;
	requestor=lo2.getXmlRequestor();
	requestor.onreadystatechange=function(){
		if(requestor.readyState==4)
			lo2.dataTables[dtName].updateData(dtName,requestor);
	}
	dtData = '&return_data=yes&page='+this.newPage+'&sort_col='+this.sortCol+'&sort_dir='+this.sortDir;
	for (i = 0; i < this.filters.length; i++){
		if(lo2(this.filters[i][0]).nodeName =='SELECT'){
			if(lo2(this.filters[i][0]).options[lo2(this.filters[i][0]).selectedIndex].value!=''){
				dtData += '&'+this.filters[i][0]+'='+escape(lo2(this.filters[i][0]).value);
			}
		}else{
			if(lo2(this.filters[i][0]).value != ''){
				dtData += '&'+this.filters[i][0]+'='+escape(lo2(this.filters[i][0]).value);
			}
		}
	}
	//alert(dtData);
	
	
	requestor.open('POST',this.url,true);
	requestor.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	requestor.send(dtData);
}
