// filter.js

//filter for Year/Month
//this function is using several variables which have to defined in the html page
//it copies values from input elements to data array using current (previous) selected index
//then copies values from data array to the input fields using index just selected by user
function copyValues(){
	selectedIdx=ymFilter.selectedIndex;
	
	//is user selected new value?
	if(selectedIdx != prevSelectedIdx){
		var tableRows=tab.tBodies[0].rows;
		var rowId=0;
		
		//iterate over all table rows
		for(i=1;i<tableRows.length;i++){
			//row id is stored in the id property of element 'input'
			//first digit it is an element number, all next digits are row id
			rowId=tableRows[i].cells[0+inputColumnIdxShift].childNodes[inputChildNodeIdx].id.substr(1);
			
			//iterate over input elements
			for(j=0;j<numMothToShow;j++){
				//save values from inputs in the data array
				dataArray[rowId][j+prevSelectedIdx][1]=tableRows[i].cells[j+inputColumnIdxShift].childNodes[inputChildNodeIdx].value;
				//copy values from data array to inputs
				tableRows[i].cells[j+inputColumnIdxShift].childNodes[inputChildNodeIdx].value=dataArray[rowId][j+selectedIdx][1];
				tableRows[i].cells[j+inputColumnIdxShift].childNodes[inputChildNodeIdx].disabled=!dataArray[rowId][j+selectedIdx][0];
			}
		}
		//change headers
		for(i=0;i<numMothToShow;i++){
			document.getElementById(headerIdPrefix+i).innerText=dateHeaders[i+selectedIdx];
		}
		
		//store new selected index
		prevSelectedIdx=selectedIdx;
	}
}

//filter parameters object
function filterParams(columnNum,filterValue){
	this.columnNum=columnNum;
	this.filterValue=filterValue;
}

//filter rows of given table using parameters array
function filter(param,tab){
	var tableRows=tab.tBodies[0].rows;
	var includeRow=true;
	//iterate over all original table rows
	for(i=1;i<tableRows.length;i++){
		includeRow=true;
		//params is a pairs of columnNum/filterValue
		//iterate over filter parameters
		for(j=0;j<param.length;j++){
			//check next parameter
			if(ts_getInnerText(tableRows[i].cells[param[j].columnNum]).indexOf(param[j].filterValue)!= -1){
				//row meets the filter parameter
				continue;
			}
			//row doesn't meet the filter parameter
			hideObj(tableRows[i]);
			includeRow=false;
			break;
		}
		if(includeRow){
			showObj(tableRows[i]);
		}
	}
}

function showObj(obj){
	obj.style.display='';
}

function hideObj(obj){
	obj.style.display='none';
}