var _PAGING = new Object();

_PAGING.pagingShowCount=10;
_PAGING.orderDirection='od'
_PAGING.orderByFName='ob'
_PAGING.startFromField='sf'
_PAGING.curentINDEX=1;
_PAGING.formName='listForm';
_PAGING.setForm=function (formName){
	this.formOBJ=document[formName];
}
_PAGING.setProperties=function(prop,value){
	this[prop]=value;
}
_PAGING.setFormValue=function(element,value){
	this.formOBJ[element].value=value;
}
_PAGING.getFormValue=function(element){
	if(this.formOBJ[element])
	return this.formOBJ[element].value;
	else
	return null;
}
_PAGING.orderBy=function (index,direction){
	
	_PAGING.formOBJ[_PAGING.orderByFName].value=index;
	_PAGING.formOBJ[_PAGING.orderDirection].value=direction;
	_PAGING.formOBJ[_PAGING.startFromField].value=0;
	_PAGING.submitForm()

}
_PAGING.checkFormFunction="";
_PAGING.submitForm=function (){
	if(_PAGING.checkFormFunction)
	{
		if(_PAGING.checkFormFunction())
		{
		 
			this.formOBJ.submit();
		}
	}
	else
	this.formOBJ.submit();
}

_PAGING.setPagingParams=function(totalCount,totalPerPage,startFrom){
	this.total=totalCount
	this.perPage=totalPerPage
	this.totalResult=Math.ceil(totalCount/totalPerPage);
	this.startFrom=startFrom/totalPerPage
	this.center=Math.ceil(this.pagingShowCount/2)

}
 
_PAGING.paging=function(){
	//KALDIK BURDA
	var  prefix='<a href="javascript:javascript:_PAGING.first()"><<</a>&nbsp;&nbsp;'
	var  postfix='&nbsp;&nbsp;<a href="javascript:_PAGING.last()">>></a>'
	var divELem=document.getElementById(this.listingDiv)
	divELem.innerHTML="";
	//var tempBg = this.pagerBg == "paging_yellow" ? "paging_yellow" : "paging_green";
	var i=0
	var totalPaging=Math.ceil(this.total/this.perPage)
	this.startIndice=1;
	if(totalPaging>this.pagingShowCount)
	{
	
	if(this.startFrom>this.center){

		this.startIndice=this.startFrom-this.center+1;
		
		if(this.startIndice+1>Math.ceil(this.total/this.perPage)-Math.ceil(this.pagingShowCount))
		{
			postfix="";
			this.startIndice=Math.ceil(this.total/this.perPage)-Math.ceil(this.pagingShowCount)+1;
		}
	}
	}
	else
		prefix=postfix=""
		 
	if(this.startIndice <2)
		prefix=""
	var to=this.startIndice+this.pagingShowCount;
	for(i=this.startIndice;i<to;i++)
	{
		if(totalPaging>=i)
		{
			t=i
			if(i<10)
				t=""+i
			if(i==_PAGING.curentINDEX)
				divELem.innerHTML+='<span id="currentNum">'+t+'</span> '
			else
				divELem.innerHTML+='<a class="pagingNum" href="javascript:_PAGING.setPaging('+((i-1)*this.perPage)+',\''+_PAGING.uniqueId+'\')" onclick="">'+t+'</a> '
			  
			if(i<to-1 && i< totalPaging)
				divELem.innerHTML+="  "
		}
		else
			break;
	}
	document.getElementById('prevPage').innerHTML=prefix + document.getElementById('prevPage').innerHTML;
	document.getElementById('nextPage').innerHTML+=postfix;
	//divELem.innerHTML=prefix+divELem.innerHTML+postfix
}
_PAGING.next=function()
{ 	
	if(this.startFrom<this.center)
	{
		this.startFrom=this.center
	}
	if((this.startFrom)+1>Math.ceil(this.total/this.perPage)-Math.ceil(this.pagingShowCount/2)) 
	{ 
		 
		return
	}
	this.setProperties('startFrom',(this.startFrom+1));
	this.paging()
}
_PAGING.previous=function(){
	this.setProperties('startFrom',this.startFrom-1);
	this.paging()
}
_PAGING.setPaging=function(sf){

	this.formOBJ[this.startFromField].value=sf
	this.submitForm();
}
_PAGING.last=function(){
	 
	this.setPaging((this.totalResult-1)*this.perPage);
	//this.paging()
}
_PAGING.first=function(){
	this.setPaging(0);
	//this.paging()
}
_PAGING.goPrevious=function()
{
	if(this.curentINDEX>1)	
	{
		_PAGING.setFormValue('sf',(this.curentINDEX-2)*this.perPage)
		_PAGING.submitForm();
	}
	
}
_PAGING.goNext=function()
{
	if(this.curentINDEX<this.totalResult)	
	{
		_PAGING.setFormValue('sf',(this.curentINDEX)*this.perPage)
		_PAGING.submitForm();
	}
}
_PAGING.nextPreviousControl=function()
{
	if(parseInt(document[this.formName].tc.value) <= parseInt(document[this.formName].sc.value))
	{
		document.getElementById('prevPage').innerHTML="";
		document.getElementById('nextPage').innerHTML="";
				
	}
	else if(parseInt(document[this.formName].tc.value)<=parseInt(document[this.formName].sf.value)+parseInt(document[this.formName].sc.value))
	{
	  	document.getElementById('nextPage').innerHTML="";
	}
	else if(parseInt(document[this.formName].sf.value)==0||document[this.formName].sf.value=="")
	{
		 document.getElementById('prevPage').innerHTML=""
	}	 
}