/*
 * Common functions used by all table_data scripts.
 */

function callbackGetRemoteListDataBean(resp, page, parent) {
	var pageNumber = Number(page);
	parent.fillTable(resp);
	parent.updatePaginationTable(parent.page, pageNumber, resp.numPages, "#" + parent.pNameSpace + "TPagination");
	parent.page = pageNumber;
	try { changeContentsHeight(); } catch(e) { }
}

function callbackExceptionGetRemoteListDataBean(exceptionMessage, exception, parent, colspan) {
	var cellFuncs = [
		function(data) {return parent.getErrorHtml(data);}
	];
	DWRUtil.removeAllRows(parent.pNameSpace + "Trans");
	DWRUtil.addRows(parent.pNameSpace + "Trans", [exceptionMessage], cellFuncs, 
		{
      		cellCreator:function(options) {
			    var td = document.createElement("td");			    
          		td.setAttribute('colspan', colspan);          			  
			    return td;
			},escapeHtml:false
		}
	);
	try { changeContentsHeight(); } catch(e) { }
}

function addLoading(tBodyId, colspan) {
	var cellFuncs = [
		function(data) {return data;}
	];
	DWRUtil.removeAllRows(tBodyId);	
	
	DWRUtil.addRows(tBodyId, [""], cellFuncs, 
		{
      		cellCreator:function(options) {
			    var td = document.createElement("td");
			    td.className = "tableDataLoading";
          		td.setAttribute('colspan', colspan);          		
			    return td;
			},escapeHtml:false
		}
	);
}

function getErrorHtml(message) {
	var errorHtml = '<div class="contentsColumnLoginAlertErrorContainer">';                                         
	errorHtml += '<p class="contentsColumnLoginAlertErrorPic"> </p>'; 
	errorHtml += '<p class="contentsColumnLoginAlertErrorContent">' + message + '</p>';                                                 
	errorHtml += '<div style="clear: both;">';
	errorHtml += '</div>';
	errorHtml += '</div>';
	
	return errorHtml;
}

function backPaginatedPage() {
	this.getRemoteListDataBean(this.page-1);
}
function nextPaginatedPage() {
	this.getRemoteListDataBean(this.page+1);
}
function changePaginatedPage(clicked) {
	var a = $(clicked).text();
	this.getRemoteListDataBean(a);
}

function updatePaginationTable(oldP, currP, maxP, pagTable) {
	if(oldP != currP) {
		if(currP == 1) {
			$(pagTable).find('span[id="backPage"]').hide();
		} else {
			$(pagTable).find('span[id="backPage"]').show();
		}
		
		if(currP == maxP) {
			$(pagTable).find('span[id="nextPage"]').hide();
		} else {
			$(pagTable).find('span[id="nextPage"]').show();
		}
		
		
		$(pagTable).find('a[name=PNumLink]').each(function(){
			if($(this).text() == currP) { $(this).hide(); } else { $(this).show(); }
		 });
		
		$(pagTable).find('a[name=PNum]').each(function(){
			if($(this).text() == currP) { $(this).show(); } else { $(this).hide(); }
		 });						
	}
	
	if(this.numPages != maxP) { 
		$("#" + this.pNameSpace + "TPagination").find('span[class="pagesNumber"]:first').nextAll().not(":last").remove();
											
		$("#" + this.pNameSpace + "TPagination").find('span[class="pagesNumberBack"]').hide();
					
		if(maxP > 1) {
			$("#" + this.pNameSpace + "TPagination").find('span[class="pagesNumberNext"]').show();
		} else {
			$("#" + this.pNameSpace + "TPagination").find('span[class="pagesNumberNext"]').hide();
		}
		for(var i = 2; i <= maxP; i++) {
			var newElm = $("#" + this.pNameSpace + "TPagination").find('span[class="pagesNumber"]:first').clone(true);
			$(newElm).find("a").html(i);
			$(newElm).find("a.selectedPaginated").hide();
			$(newElm).find("a.paginated").show();
			newElm.insertAfter($("#" + this.pNameSpace + "TPagination").find('span[class="pagesNumber"]:last'));
		}
		this.numPages = maxP;
	}
}

function orderListDataBean(col) {		
	var headerClassDown = this.headerClassDown;
	var headerClassUp = this.headerClassUp;
	var headerClassNormal = this.headerClassNormal;
	
	if(col == this.col) {
		if(this.order == 0) {
			this.order = 1;
		} else {
			this.order = 0;
		}
	} else {
		this.order = 0;
		this.col = col;
	}		
	
	var localOrder = this.order;
	
	$("#" + this.pNameSpace + "TransHeader td").each(function(i) {
	     
	     $(this).removeClass();
	     
	     if(i == col) {
	     	if(localOrder == 0) {
	     		$(this).addClass(headerClassDown[i]);
	     	} else {
	     		$(this).addClass(headerClassUp[i]);
	     	}
	     } else {
	     	$(this).addClass(headerClassNormal[i]);
	     }
	});
			
	this.getRemoteListDataBean(1);
}	


