
/*
 * Depends on table_data_base.js
 */

function TableDataCards(groupId, companyId, pNameSpace, numPages, locale, userId, username, role, removeButtonText, noCardsMessage) {
	
	this.groupId = groupId;
	this.companyId = companyId;
	this.page = 1;
	this.pNameSpace = pNameSpace;
	this.numPages = numPages;
	this.locale = locale;
	this.userId = userId;
	this.username = username;
	this.role = role;
	this.removeText = removeButtonText;
	this.noCardsMessage = noCardsMessage;	
	
	this.numberOfColumns = 3;
	
	this.getRemoteListDataBean = function getRemoteListDataBean(page) {
	    
		this.addLoading(this.pNameSpace + "Trans");	    
		var parent = this;
		var callbackProxy = function(dataFromServer) {
			callbackGetRemoteListDataBean(dataFromServer, page, parent);				
		};
		var exceptionHandlerProxy = function(exceptionMessage, exception) {
			callbackExceptionGetRemoteListDataBean(exceptionMessage, exception, parent, numberOfColumns);				
		};
		
	    var callMetaData = {
	    	callback: callbackProxy,		  		  
		  	exceptionHandler: exceptionHandlerProxy
		};
	    CompaniesRemoteService.getCorporateCards(this.groupId, this.companyId, page, this.locale, this.userId, this.username, this.role, callMetaData);	
	}
	
	this.fillTable = function fillTable(resp) {
		var val;		
		
		var cellFuncs = [
		    function(data) {return data.name;},
			function(data) {return data.tpNumber;}	  	
		];	
		
		
		DWRUtil.removeAllRows(this.pNameSpace + "Trans");
		
		if (resp.data.length > 0){
			DWRUtil.addRows(this.pNameSpace + "Trans", resp.data, cellFuncs, 
			{ 
				rowCreator:function(options) {
				    var row = document.createElement("tr");			    
				    row.className = "tableListNormalTr";
				    return row;
			  	},
				cellCreator:function(options) {
				    var td = document.createElement("td");			   
				    td.className = "tableListNormal";
					
				    var index = options.cellNum;
				    
				    switch(index) {
							
						case 1:
							td.className = "tableListNormal";
							var tpNumberContainerParagraph = document.createElement("p");
							tpNumberContainerParagraph.className = "supportSmallBtnRemoveCard supportSmallBtnRemove";
							var tpNumberLabel = document.createElement("label");
							tpNumberLabel.className = "cripledTpNumber";
							tpNumberLabel.innerHTML = options.rowData.tpNumber;
							var removeCardAnchor = document.createElement("a");
							removeCardAnchor.href = "javascript:deactivateCard('" + options.rowData.id + "','" + companyId + "');"; 
							var removeCardSpan = document.createElement("span");
							removeCardSpan.innerHTML = removeButtonText;
							
							options.data = null;
							
							removeCardAnchor.appendChild(removeCardSpan);
							tpNumberContainerParagraph.appendChild(tpNumberLabel);
							//nasty hack to fix problem where the 2 elements in the paragraph where joined together. Time pressures suck!
							var breaker = document.createElement("b");
							tpNumberContainerParagraph.appendChild(breaker);
							tpNumberContainerParagraph.appendChild(removeCardAnchor);
							td.appendChild(tpNumberContainerParagraph);
							break;
							
						default: break;
					}	
				    
				    return td;
				},
				escapeHtml:false 
			});		
		}
		else{
			var tr = document.createElement("tr");	
			var td = document.createElement("td");			    
			td.setAttribute('colspan', 3);
			td.innerHTML = this.getErrorHtml(noCardsMessage);
			tr.appendChild(td);
			$('#' + this.pNameSpace + 'Trans').append(tr);
		}
	}
	
	this.loadNewData = function loadNewData(){
	    this.getRemoteListDataBean(1);
	}	
	
	this.getErrorHtml = getErrorHtml;
	this.addLoading = addLoading;
	this.backPaginatedPage = backPaginatedPage;
	this.nextPaginatedPage = nextPaginatedPage;
	this.changePaginatedPage = changePaginatedPage;
	this.updatePaginationTable = updatePaginationTable;					
}