/*
 * <copyright>
 *  Copyright (c) 2009 by IICM, Graz University of Technology, Austria
 * </copyright>
 *
 *  This software is the confidential information of IICM,
 *  Graz University of Technology, Austria. You shall not disclose such
 *  confidential information and shall use it only in accordance with
 *  the IICM.
 *
 *  IICM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 *  SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 *  PURPOSE, OR NON-INFRINGEMENT. IICM SHALL NOT BE LIABLE FOR ANY DAMAGES
 *  SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 *  THIS SOFTWARE OR ITS DERIVATIVES.
 *
 */
 

var statisticMessages= new Array();
var statisticParameters= new Array();

/* Initialises Arrays with Statistic Messages*/
function initStatisticMessages(statisticMessages) {
	this.statisticMessages=statisticMessages;
}

/* Initialises Arrays with Statistic Parameters */
function initStatisticParameters(statisticParameters) {
	this.statisticParameters = statisticParameters;
}

function initStatisticsSummary( mapBaseQueries, searchFields, languageKey, mapDbInfo ) {
	var selectedDbs = "";
	var dbsWithResults = "";
	var currentSearchField = "dc.title";
	var totalHits = 0;
	if ( !document.getElementById || !document.createElement ) { return; }
 		
	// Create database-list depending on 'hits>0' and 'database checked'
	for (db in mapDbInfo) {
		if ( mapDbInfo[db] == 'yes') { 
			// Check if hits of selected database greater 0, otherwise do not append to checked list for remote search 
			var hitsCurrentDb= dwr.util.getValue('idSpanDBConnected'+db);			
			if ( isNaN(Number(hitsCurrentDb)) ) { hitsCurrentDb = 0; }
			selectedDbs = selectedDbs+","+trim(db);
			if ( Number(hitsCurrentDb) > 0 ) {
				dbsWithResults = dbsWithResults+","+trim(db);
				//selectedDbs = selectedDbs+","+trim(db);
				totalHits = Number(totalHits) + Number(hitsCurrentDb);
			}			
		}
	}	
	if ( selectedDbs.length > 0 ) { selectedDbs = selectedDbs.substring(1,selectedDbs.length); } else { return; }
	if ( dbsWithResults.length > 0 ) { dbsWithResults = dbsWithResults.substring(1,dbsWithResults.length); } else { return; }

	if ( trim(languageKey) == "") { languageKey="en"; }

	var limitationHits = 5000;
	if ( !isNaN(Number(statisticParameters['limitationHits'])) ) { 
		if ( Number(statisticParameters['limitationHits'])>0 ) {		
			limitationHits = Number(statisticParameters['limitationHits']);
		} 
	}
	

	// Create Table for Statistics Summary and call DWR-Method to get Images and statistical parameters
	if (totalHits <= limitationHits ){
		//if (totalHits > 2000 ){alert(this.statisticMessages['warningStatistic']);}
	 	dwr.engine.setErrorHandler(errh); // TEST
	 	
		dwr.engine.setOrdered(true); // TEST
		StatisticsSummary.cleanUpStatisticsSummary({callback:function(){},errorHandler:function(message) { errorHandler('statistcs',message); },timeout:0});
	
		if (trim(searchFields).length >0 ) {
			createTableStatisticsSummary( searchFields );
			var searchFieldArr = searchFields.split(',');	
			for (var i=0;i<searchFieldArr.length;i++){ 
			currentSearchField = trim(searchFieldArr[i]);	
			getStatisticsSummary( mapBaseQueries, currentSearchField, selectedDbs, dbsWithResults, languageKey )	
			}
		}	
		// Show statiscsoptions.jsp an hide resultoptions.jsp
		document.getElementById('idTdCatalogPresentationOptions').style.display='inline';		
	}
	else{
		alert(this.statisticMessages['errorStatistic']+' '+limitationHits);
	}
}


function errh(msg) {   // TEST
  	alert("ERROR:"+msg);
}

function getStatisticsSummary( mapBaseQueries, currentSearchField, selectedDbs, dbsWithResults, languageKey ) {

	useLoadingImage("resources/appl_images/loadingImage24x24.gif","idTdStatisticsSummary"+currentSearchField); // CHECK THIS
			
	StatisticsSummary.getStatisticsSummary( mapBaseQueries, currentSearchField, selectedDbs, dbsWithResults, languageKey,
		{ 	callback:function(StatisticsSummaryTO){ createStatisticsSummary(StatisticsSummaryTO, currentSearchField, mapBaseQueries, selectedDbs);},
			errorHandler:function(message) { errorHandler('statistics',message); },
			timeout:0
		}		
	 );	

}
// Creates Table (including Anchors) for Statistics-Summary	Images and statistical parameters
function createTableStatisticsSummary( searchFields ) {
	var tableElement,tbodyElement,trElement,tdElement,imgElement;
		
	if ( !document.getElementById || !document.createElement ) { return; }
	// Clear Table if exists
	if ( document.getElementById('idStatisticsSummaryPosition') != null && document.getElementById('idTableStatisticsSummary') != null) {
		document.getElementById('idStatisticsSummaryPosition').removeChild(document.getElementById('idTableStatisticsSummary'));
	}
	if ( document.getElementById('idStatisticsSummaryPosition') != null && document.getElementById('idTableStatisticsDetails') != null) {
		document.getElementById('idStatisticsSummaryPosition').removeChild(document.getElementById('idTableStatisticsDetails'));
	}
	
	// Disable Button Show Detailed List
	if (document.getElementById('idButtonShowStatisticsList') != null ) {
		document.getElementById('idButtonShowStatisticsList').disabled=true;
	}
	if (document.getElementById('idButtonStatisticsExport') != null ) {
		document.getElementById('idButtonStatisticsExport').disabled=true;
	}
		
	// Clear Message 'simpleSearchInfoBelow' if exist
	trElement = document.getElementById('idSimpleSearchInfoBelow_txt')
	if ( trElement != null ) {
		trElement.parentNode.removeChild(trElement);
	}	
	trElement = document.getElementById('idSimpleSearchInfoBelow_hr')
	if ( trElement != null ) {
		trElement.parentNode.removeChild(trElement);
	}	
	
	tableElement = document.createElement("table");
	tableElement.id = "idTableStatisticsSummary";
	tbodyElement = document.createElement("tbody");
	trElement = document.createElement("tr");
	var searchFieldArr = searchFields.split(',');	
	for (var i=0;i<searchFieldArr.length;i++){ 
		currentSearchField = trim(searchFieldArr[i]);	
		tdElement = document.createElement("td");
		tdElement.id = "idTdStatisticsSummary"+currentSearchField;
		setStyle(tdElement,"vertical-align:top;");
		trElement.appendChild(tdElement);
		if ( (i+1)%3 == 0) { // new row after 3 Colums
			tbodyElement.appendChild(trElement);
			trElement = document.createElement("tr");
		}		
		
	}	
	tbodyElement.appendChild(trElement);
	tableElement.appendChild(tbodyElement);		
	document.getElementById('idStatisticsSummaryPosition').appendChild(tableElement);
}
	
	
// Fills Table StatisticsSummary	
function createStatisticsSummary(StatisticsSummaryTO, currentSearchField, mapBaseQueries, selectedDbs, languageKey) {	

	var tableElement,tbodyElement,trElement,tdElement,img1Element,img2Element,textElement;	
	var statisticSummary = StatisticsSummaryTO;
	var imageData1 = statisticSummary.img1;
	var statisticalParams = statisticSummary.statisticalParams;
	var messages = statisticSummary.lgKeys;
	
	//alert(statisticalParams.toSource());
	
	// Create Image	
	img1Element = document.createElement("img");
	img1Element.id = "idImg1StatisticsSummary"+currentSearchField;
	document.getElementById("idTdStatisticsSummary"+currentSearchField).appendChild(img1Element);		
	dwr.util.setValue('idImg1StatisticsSummary'+currentSearchField,imageData1);  
		
	// Create Table for Statistical Parameters
	tableElement = document.createElement("table");
	tbodyElement = document.createElement("tbody");
	//for ( i in statisticalParams ) {
		//numberInSelection
		if (statisticalParams['numberInSelection']){
			trElement = document.createElement("tr");
			tdElement = document.createElement("td");
			setStyle(tdElement,"font-size:14px;color:black; font-weight:bold;")			
			textElement = document.createTextNode(messages['statSummary.numberInSelection']+" "+statisticalParams['numberInSelection']);
			tdElement.appendChild(textElement);	
			trElement.appendChild(tdElement);
			tbodyElement.appendChild(trElement);
		}
		//totalInDatabase
		if (statisticalParams['totalInDatabase']){
			trElement = document.createElement("tr");
			tdElement = document.createElement("td");
			setStyle(tdElement,"font-size:14px;color:black; font-weight:bold;")
			textElement = document.createTextNode(messages['statSummary.totalInDatabase']+" "+statisticalParams['totalInDatabase']);
			tdElement.appendChild(textElement);	
			trElement.appendChild(tdElement);
			tbodyElement.appendChild(trElement);
		}
		//percentage
		if (statisticalParams['percentage']){
			trElement = document.createElement("tr");
			tdElement = document.createElement("td");
			setStyle(tdElement,"font-size:14px;color:black; font-weight:bold;")			
			textElement = document.createTextNode(messages['statSummary.percentage']+" "+statisticalParams['percentage']);
			tdElement.appendChild(textElement);	
			trElement.appendChild(tdElement);
			tbodyElement.appendChild(trElement);
		}
		//firstDate
		if (statisticalParams['firstDate']){
			trElement = document.createElement("tr");
			tdElement = document.createElement("td");
			setStyle(tdElement,"font-size:14px;color:black; font-weight:bold;")		
			textElement = document.createTextNode(messages['statSummary.firstDate']+" "+statisticalParams['firstDate']);
			tdElement.appendChild(textElement);	
			trElement.appendChild(tdElement);
			tbodyElement.appendChild(trElement);
		}
		//lastDate
		if (statisticalParams['lastDate']){
			trElement = document.createElement("tr");
			tdElement = document.createElement("td");
			setStyle(tdElement,"font-size:14px;color:black; font-weight:bold;")			
			textElement = document.createTextNode(messages['statSummary.lastDate']+" "+statisticalParams['lastDate']);
			tdElement.appendChild(textElement);	
			trElement.appendChild(tdElement);
			tbodyElement.appendChild(trElement);
		}
		
		//firstValue
		if (statisticalParams['firstValue']){
			trElement = document.createElement("tr");
			tdElement = document.createElement("td");
			setStyle(tdElement,"font-size:14px;color:black; font-weight:bold;")
			textElement = document.createTextNode(messages['statSummary.firstValue']+" "+statisticalParams['firstValue']);
			tdElement.appendChild(textElement);	
			trElement.appendChild(tdElement);
			tbodyElement.appendChild(trElement);
		}
		//lastValue
		if (statisticalParams['lastValue']){
			trElement = document.createElement("tr");
			tdElement = document.createElement("td");
			setStyle(tdElement,"font-size:14px;color:black; font-weight:bold;")
			textElement = document.createTextNode(messages['statSummary.lastValue']+" "+statisticalParams['lastValue']);
			tdElement.appendChild(textElement);	
			trElement.appendChild(tdElement);
			tbodyElement.appendChild(trElement);
		}
		//arithmeticMean
		if (statisticalParams['arithmeticMean']){
			trElement = document.createElement("tr");
			tdElement = document.createElement("td");
			setStyle(tdElement,"font-size:14px;color:black; font-weight:bold;")
			textElement = document.createTextNode(messages['statSummary.arithmeticMean']+" "+statisticalParams['arithmeticMean']);
			tdElement.appendChild(textElement);	
			trElement.appendChild(tdElement);
			tbodyElement.appendChild(trElement);
		}
		
		//standardDev
		if (statisticalParams['standardDev']){
			trElement = document.createElement("tr");
			tdElement = document.createElement("td");
			setStyle(tdElement,"font-size:14px;color:black; font-weight:bold;")
			textElement = document.createTextNode(messages['statSummary.standardDev']+" "+statisticalParams['standardDev']);
			tdElement.appendChild(textElement);	
			trElement.appendChild(tdElement);
			tbodyElement.appendChild(trElement);
		}
		//minInterquartileMax
		if (statisticalParams['minInterquartileMax']){
			trElement = document.createElement("tr");
			tdElement = document.createElement("td");
			setStyle(tdElement,"font-size:14px;color:black; font-weight:bold;")
			textElement = document.createTextNode(messages['statSummary.minInterquartileMax']+" "+statisticalParams['minInterquartileMax']);
			tdElement.appendChild(textElement);	
			trElement.appendChild(tdElement);
			tbodyElement.appendChild(trElement);
		}


	//}
	tableElement.appendChild(tbodyElement);			
	document.getElementById("idTdStatisticsSummary"+currentSearchField).appendChild(tableElement);		
	
}

