/*
 * <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.
 *
 */
 

//DWR ErrorHandler
function errorHandler(source, ex) {
	setCheckedContUpdate('idContUpdate', 'false'); // Uncheck Cont. Update in Case of Connection Timout
	alert(dwr.util.toDescriptiveString(ex, 3)); 
	if (source=='statistics'){
		StatisticsSummary.cleanUpStatisticsSummary({callback:function(){},errorHandler:function(message) { errorHandler('statistcs',message); },timeout:0});
		var anchor = document.getElementById("idMessageHitsGreaterLimit");
		var div = document.createElement("div");
		div.setAttribute("id","error");
		div.innerHTML="<b>"+ex+"</b>";
		anchor.innerHTML="";
		anchor.appendChild(div);
		anchor.setAttribute("style","display:inline;");
		document.formCatalogSearch.submit(); // Submit Formular	
	}
	else if (source=='bookmarks'){
		var anchor = document.getElementById("idMessageHitsGreaterLimit");
		var div = document.createElement("div");
		div.setAttribute("id","error");
		div.innerHTML="<b>"+ex+"</b>";
		anchor.innerHTML="";
		anchor.appendChild(div);
		anchor.setAttribute("style","display:inline;");
	}
	else if (source=='export'){
		var anchor = document.getElementById("idMessageHitsGreaterLimit");
		var div = document.createElement("div");
		div.setAttribute("id","error");
		div.innerHTML="<b>"+ex+"</b>";
		anchor.innerHTML="";
		anchor.appendChild(div);
		anchor.setAttribute("style","display:inline;");
	}
	else if (source=='geomap'){
		var anchor = document.getElementById("idMessageHitsGreaterLimit");
		var div = document.createElement("div");
		div.setAttribute("id","error");
		div.innerHTML="<b>"+ex+"</b>";
		anchor.innerHTML="";
		anchor.appendChild(div);
		anchor.setAttribute("style","display:inline;");
		document.formCatalogSearch.submit(); // Submit Formular	
	}
}


// Converts List of Maps (Java) to the corresponding String Representation (Javascript)
function listOfMapsToString( listOfMaps ) {	
	var stringRepresentation="";	
	
	for (var i = 0; i < listOfMaps.length; i++) { // Iterate List of Maps
		var mapAsString ="";
		var map = new Array();
		map = listOfMaps[i]; // Iterate Map
		for (j in map) { 
			mapAsString = mapAsString+","+j+":"+map[j].toString();
		}
		if ( mapAsString.length > 0 ) { 
			mapAsString = mapAsString.substring(1,mapAsString.length); 
		}
		if ( stringRepresentation.length >0 ) {stringRepresentation = stringRepresentation+","; }
		stringRepresentation = stringRepresentation+"{"+mapAsString+"}";
	}
	stringRepresentation = "["+stringRepresentation+"]";
	
	return stringRepresentation;
}


// Converts Map (Java) to the corresponding String Representation (Javascript)
function mapToString( map ) {
	var stringRepresentation="";	
	
	for (i in map) { stringRepresentation = stringRepresentation+","+i+":"+map[i].toString(); }
	if ( stringRepresentation.length > 0 ) { stringRepresentation = stringRepresentation.substring(1,stringRepresentation.length); }
	stringRepresentation = "{"+stringRepresentation+"}";
		
	return stringRepresentation;
}



// Serializes JavaScript Objects
function serialize(_obj) {

   if (typeof _obj.toSource !== 'undefined' && typeof _obj.callee === 'undefined')
   {
      return _obj.toSource();
   }

    switch (typeof _obj)
   {
      // numbers, booleans, and functions
      case 'number':
      case 'boolean':
      case 'function':
         return _obj;
         break;

      // strings need to be wrapped in quotes
      case 'string':
         return '\'' + _obj + '\'';
         break;

	  // Objects
      case 'object':
         var str;
         if (_obj.constructor === Array || typeof _obj.callee !== 'undefined')
         {
            str = '[';
            var i, len = _obj.length;
            for (i = 0; i < len-1; i++) { str += serialize(_obj[i]) + ','; }
            str += serialize(_obj[i]) + ']';
         }
         else
         {
            str = '{';
            var key;
            for (key in _obj) { str += key + ':' + serialize(_obj[key]) + ','; }
            str = str.replace(/\,$/, '') + '}';
         }
         return str;
         break;

      default:
         return 'UNKNOWN';
         break;
   }
}

// DOM function - used to set Style of Element 'obj'
function setStyle(obj, style) {
	if (obj == null) { return; }
	if (obj.style != null) {
		obj.removeAttribute("style");
	}
	obj.setAttribute("style", style);
	obj.style.cssText = style;
}

// DOM function - used to remove all childElements of a node with id='idAnchor'
function removeAllChildNodes(idAnchor) {
	if ( document.getElementById( idAnchor ) != null) {
		var nodeElem=document.getElementById( idAnchor );
		while (nodeElem.hasChildNodes()) { nodeElem.removeChild(nodeElem.firstChild);}
	}
}

// METHOD NOT USED IN APPLICATION
function doResetCatalog() {
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
    1, 0, 0, 0, 0, false, false, false, false, 0, null);
  var cb = document.getElementById("idcatalogsearchreset"); 
  var canceled = cb.dispatchEvent(evt);
  if(canceled) {
    // A handler called preventDefault
    alert("canceled");
  } else {
    // None of the handlers called preventDefault
    alert("not canceled");
  }
}


/*
function fireClickEvent (element) {
	if (document.createEvent) {
	    var evt = document.createEvent("Events");
	    evt.initEvent("onclick", true, true);
	    element.dispatchEvent(evt);
	} 
	else 
		if (document.createEventObject) {
	    	var evt = document.createEventObject();
	    	element.fireEvent("onclick", evt);
	    }
}

*/

// METHOD NOT USED IN APPLICATION
function fireClickEvent( idObject ) { 	

	if ( !document.getElementById ) { return }
	var obj = document.getElementById(idObject);
 
    if (document.all) { // If Browser= IE
       obj.fireEvent("onclick"); 
    } 
    else { // Browser not IE
		if(document.createEvent && obj.dispatchEvent) {
	   		var evt=document.createEvent("MouseEvents");
   			if(evt && evt.initMouseEvent) { 
   				evt.initMouseEvent("click",true,true,document.defaultView,1,0,0,0,0,false,false,false,false,0,null); 
   			}
    		obj.dispatchEvent(evt);
    	}
    }
}


// Displays Loading-Symbol 'imageSrc' and Abort-Button on Position with id = 'position'
function useLoadingImage(imageSrc,position) {
	var tableElement,tbodyElement,trElement,tdElement,imgElement,aElement;
		dwr.engine.setPreHook(function() {				
	  	//dwr.util.setValue(position,'');
	  	tableElement=document.createElement('table');
	  	//setStyle(tableElement,"background-color:#C0C0C0;");
	  	tbodyElement=document.createElement('tbody');
	  	trElement=document.createElement('tr');
	  	trElement.setAttribute("align","center");
	  	tdElement=document.createElement('td');
	  	imgElement = document.createElement('img');
      	imgElement.setAttribute('id','idLoadingImage');
      	imgElement.setAttribute('src',imageSrc);
		tdElement.appendChild(imgElement);
		trElement.appendChild(tdElement);
		tbodyElement.appendChild(trElement);
		trElement=document.createElement('tr');
		trElement.setAttribute("align","center");
	  	tdElement=document.createElement('td');	
	  	textElement = document.createTextNode("Loading Records from Database(s)... ");
	  	tdElement.appendChild(textElement);		
	  	textElement = document.createTextNode("Click ");	
	  	tdElement.appendChild(textElement);	  		  	
		aElement = document.createElement("a");
		aElement.href = "javascript:doResetCatalogSearch()";	
		textElement = document.createTextNode("here");
		aElement.appendChild(textElement);
		tdElement.appendChild(aElement);
		textElement = document.createTextNode(" to abort. ");		  	
		tdElement.appendChild(textElement);		
		trElement.appendChild(tdElement);
		tbodyElement.appendChild(trElement);
		tableElement.appendChild(tbodyElement);		
	  	dwr.util.setValue(position,tableElement); 	
	});
	dwr.engine.setPostHook(function() {
	    dwr.util.setValue(position,'');
 	});   
}



// Displays Loading-Symbol 'imageSrc' and Abort-Button on Position with id = 'position'
function useLoadingImageSmall(imageSrc,position) {
	var tableElement,tbodyElement,trElement,tdElement,imgElement,aElement;
	dwr.engine.setPreHook(function() {		
	  	dwr.util.setValue(position,'');
	  	imgElement = document.createElement('img');
      	//imgElement.setAttribute('id','idLoadingImage');
      	imgElement.setAttribute('src',imageSrc);
	  	dwr.util.setValue(position,imgElement); 	
	});
	dwr.engine.setPostHook(function() {
	    dwr.util.setValue(position,'');
 	});   
}


// Resets Catalog Search
function doResetCatalogSearch() {
	dwr.engine.setTimeout(1);	
	document.getElementById('idAction').value='DoReset';
	document.formCatalogSearch.submit();
}


