
/**
*** ************************** ***
***           COMMON           ***
*** ************************** ***
**/

/* Return the ID of an element */
var ID = function( id ) {
	return $( id );
	// return document.getElementById( id );
}

/**
*** ************************** ***
***         SITE TOOLS         ***
*** ************************** ***
**/

/* Display application specified message */
var displayAlert = function( msg ) {
	if( msg != "" ) {
		alert( msg.unescapeHTML() );
	}
}

/* Display application errors messages */
var displayMessage = function() {
	if( last_error != "" ) {
		displayAlert( last_error );
	}
}

/* Manages effects on top band menu */
var initTopBand = function( id, men_obj ) {
	if( ! isId( id ) ) return "";
	men_init( id, men_obj );
}

/* Manages effects on left band menu */
var initLeftBand = function ( id, root ) {
	if( ! isId( id ) ) return "";
	var root = 'left_menu_ul';
	var a = $$('#'+root+' a');
	var ul = $$('#'+root+' ul.level_2');
	var css_off = 'off';
	var css_on = 'on';	
	ul.each( function(e,index) {
		if(e.style.display!='block'){
			e.style.display='none';
		}
	});
	a.each( function(e) {
		if(!e.id.empty()){
			Event.observe(e,'click',function(){
				var ul_affiche = $(e.id).nextSibling.nextSibling;
				if(ul_affiche.style.display=='none'){
					$(e.id).blur();
					$(e.id).className = css_on;
					new Effect.BlindDown(ul_affiche,{duration:0.5});
				}else{
					$(e.id).blur();
					$(e.id).className = css_off;
					new Effect.BlindUp(ul_affiche,{duration:0.5});
				}
			});
		}
	});
}

/* Affiche ou masque le bandeau haut */
var showHideTopBand = function( top_band )
{
	Effect.toggle( top_band, "Appear", { duration: 0.5 } );
	invokeAjaxUrl( "_ajax_set_app_param.php?param=top_band" );
}

/* Manages send page to a friend action */
var sendToFriend = function( rid ) {
	displayAlert( "Sending a link to rubrik "+rid+" to your firend !" );
}

/* Display the About site message */
var displayAbout = function() {
	displayAlert( cl_message_11 );
}

/* Launch printing of current page */
var printPage = function() {
	window.print();
}


/**
*** ************************** ***
***         MORE TOOLS         ***
*** ************************** ***
**/

/* Invoke an url using ajax get mode without callback */
var invokeAjaxUrl = function( url ) {
	new Ajax.Request(url, {
		method: 'get',
		asynchronous: false
	});
}

/* Invoke an url using ajax get mode without callback AND returns received content or error message */
var getServerContentWithAjax = function( url ) {
	var ajax_results = '';
	new Ajax.Request(url, {
	  method: 'get',
	  asynchronous: false,
	  onSuccess: function( http ) {
		  ajax_results = http.responseText;
	  },
	  onFailure: function( http ) {
		  ajax_results = "Unable to retrieve url contents for "+url;
	  }
	});
	return ajax_results;
}

/* Invoke an url using ajax get or post mode, using 2 callbacks for failure or success management */
var getAjaxUrl = function( url, mod, ok_cbk, ko_cbk ) {
	new Ajax.Request(url, {
	  method: mod,
	  asynchronous: true,
	  onSuccess: ok_cbk,
	  onFailure: ko_cbk
	});
}

/* Display a popup window and fill it's body with url contents */
var popup = function( title, url, width, height ) 
{
	option = "width="+width+",height="+height+",toolbar=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes,alwaysRaised=1,dependent=1";
	w = window.open(url, title, option);
	w.style.left = window.style.left+100;
	w.style.top = window.style.top+100;
	w.focus();
}

/* Display a scrollable resizable popup window */
var zoomImage = function( title, url, width, height ) {
	// To do : replace popup by lightbox
	var option = "width="+width+",height="+height+",toolbar=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes,alwaysRaised=1,dependent=1";
	w = window.open(url, title, option);
	w.focus();
}

/* Encode une url */
var urlEncode = function( str ) {
    var str = escape(str);
    str = str.replace(new RegExp('\\+','g'),'%2B');
    return str.replace(new RegExp('%20','g'),'+');
}

/* Check or uncheck all checkbox element of a form */
var checkUncheckAll= function( itm, ignore ) {
	var theForm = itm.form, z = 0;
	for(z=0; z<theForm.length;z++) {
		if(theForm[z].type == 'checkbox' && theForm[z].name != ignore) {
			theForm[z].checked = itm.checked;
		}
	}
}

/* Select or unselect all options of a select or radio list */
var selectUnselectAll= function( itm, chk ) {
	var cmb = ID(itm);
	if( ! cmb ) return;
	for( i=0; i<cmb.length; i++ ) {
		cmb.options[i].selected = chk;
	}
}

/* Select all items of a list box */
var selectAllComboItems = function( sel_src, value ) {
	var src_id = ID(sel_src);
	if( ! src_id ) return;
	var src_opt = src_id.options;
	for( i=0; i<src_opt.length; i++ ) {
		src_opt[i].selected = ( value ? true : false );
	}
}
