
/**
 * This function is responsible for :
 * a) presenting the panel results, when the user clicks in the 
 * button for launching a query
 * b) shows the ajax image while the form is 
 * being processed
 * @param data that corresponds to the status of the form processing (begin, success, etc)
 **/
function showResultsPanel(data) {  
	
	 if (data.status == 'begin') {
		 var item = document.getElementById('formSearch:ajaxWait');
   	     if (item) {
   		     if (item.className=='hidden') {
   	             item.className = 'unhidden';
   	         } 
   	     }
	 } 
	
     if (data.status == "success")  {
    	 
    	 var item = document.getElementById('page_nav');
    	 if (item) {
    		 if (item.className=='hidden') {
    	         item.className = 'unhidden';
    	    }
    	 }
    	 
    	 var item = document.getElementById('space_table');
    	 if (item) {
    		 if (item.className=='hidden') {
    	         item.className = 'unhidden';
    	    }
    	 }
    	 
    	 var item = document.getElementById('no_results_message');
    	 if (item) {
    		 if (item.className=='hidden') {
    	         item.className = 'unhidden';
    	    }
    	 }
    	 
    	 var item = document.getElementById('page_summary');
    	 if (item) {
    		 if (item.className=='hidden') {
    	         item.className = 'unhidden';
    	    }
    	 }
    	 
    	 var item = document.getElementById('no_semantic_message');
    	 if (item) {
    		 if (item.className=='hidden') {
    	         item.className = 'unhidden';
    	    }
    	 }
    	 
    	 var f = document.getElementById('formSearch:buttonLaunchSearch');
    	 if (f) {
    	   f.blur();
    	 } 
    	 
    	 var item = document.getElementById('formSearch:ajaxWait');
   	     if (item) {
   		     if (item.className=='unhidden') {
   	             item.className = 'hidden';
   	         } 
   	     }
     }     
}

/**
 * This function removes the content of the input keyword component
 * if its content is the default message 'msg.search_input_keywords = Enter keywords or indicators'.
 * This function is needed because the default value of this field is associated
 * to a property bean (that is the default value of the field) and each time the user 
 * wants to enter with a first keyword, he/she has to suppress the message 
 * (character by character). 
 * @param component representing the input text element (to avoid search by id)
 * @param default input keyword message 
 */
function removeContentInput(component,message) {
	    if (component) {
	    	if (component.value == message) {
	    		component.value = "";
	    	}
	    }
}

/**
 * Creates a Cookie.
 * 
 * @param name the name of the cookie to create
 * @param value the value of the cookie
 * @param days numbers of day the cookie will exist.
 */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/**
 * Finds a Cookie if exists and return its value. 
 * 
 * @param name the name of the cookie to find
 */
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0){
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

/**
 * Erases a Cookie.
 * 
 * @param name the name of the cookie to erase.
 */
function eraseCookie(name) {
	createCookie(name,"",-1);
}

/**
 * Checks every second if the download is finished or not.
 * 
 */
 var fileDownloadCheckTimer;
 
 /**
  * Blocks User Interface while the selected indicator result is written on a file than relaod the page in order to set the "terms and condition" to accepted.
  * 
  * @param panelId the html id of the wait modal panel (for example: 'formMetadata:waitModalPanelReload') to close at the end of the downlaod.
  */
 function blockUIForDownloadAndReload(panelId){
	 blockUIForDownload(panelId, true);
 }
 
 /**
  * Blocks User Interface while the selected indicator result is written on a file.
  * 
  * @param panelId the html id of the wait modal panel (for example: 'formMetadata:waitModalPanelReload') to close at the end of the downlaod.
  * @param reloadDoc a boolean indicates if the page must be reload or not, at the end of the download.
  */
 function blockUIForDownload(panelId, reloadDoc) {
	 //alert("input id "+panelId);
  	  var cookieValue =  createCookie("fileDownloadToken","false",1);
      fileDownloadCheckTimer = window.setInterval(function () {
    	  var x = readCookie("fileDownloadToken");
		    if (x == "true") {
		    	finishDownload(panelId, reloadDoc);
		    }
      }, 1000);
    }
 

   
 /**
  * Finish Download: erases cookie created, hides the 'wait modal panel'.
  * 
  * @param panelId the html id of the wait modal panel (for example: 'formMetadata:waitModalPanelReload') to close at the end of the downlaod.
  * @param reloadDoc a boolean indicates if the page must be reload or not, at the end of the download.
  * 
  */
 function finishDownload(panelId, reloadDoc) {
 	 window.clearInterval(fileDownloadCheckTimer);
 	 eraseCookie('fileDownloadToken');
 	 if (reloadDoc != null && reloadDoc == true){
 		 document.location.reload(true);
 	 }
 	 RichFaces.ui.ComponentControl.execute('oncomplete',{"selector":null,"target":[panelId] ,"callback":function(event,component){component['hide'].apply(component,[] );}} ); return false;
 	 
 }

    
