//JAVASCRIPT DOCUMENT BY OXTAL
//JAVASCRIPT DOCUMENT: html2.js.php

var image_to_load = new Array();
function image_init(){
	var i, imgObj;
	
	if (document.all && document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule){
		document.styleSheets[0].addRule('*', 'behavior: url(/css/htc/iepngfix.htc)');
	}
	
	imgObj = new Image();
	
	for(i = 0; i < image_to_load.length; i++){
	
		if( !image_to_load[i][1] ){
		
			imgObj.src = image_to_load[i][0];
			image_to_load[i][1] = true;
		}
	}
	
}

function add_image_preload(imageSrc){
	image_to_load[image_to_load.length] = new Array(imageSrc, false);
}



//Add characters
//	alphabet, 	65 a 90
//	numbers, 	48 a 57
//	keypad, 	96 a 105
//	enter,		13
//	space,		32
//	specials,	59, 61, 106, 107, 108, 109, 110, 111, 188, 190, 191, 192, 219, 220, 221, 222

	
//Remove characters
//	backspace,	8
//	delete,		46
	


var unicode_add_chars = new Array(65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105,13,32,59,61,106,107,108,109,110,111,188,190,191,192,219,220,221,222);

var unicode_rem_chars = new Array(8,46);

var ERROR_CONTROL_BORDERCOLOR = "#FFAAAA";


//GRAPHICAL PURPOSE
function findPos(obj){

	var curleft = 0;
	var curtop = 0;
	if(obj != null){
		if (obj.offsetParent) {
		
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			
			while( (obj = obj.offsetParent) ) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		
		return [curleft,curtop];
	}else
		return [0,0];
}

function empty( variable ){
	var res = true;

	if( typeof(variable) == "string" ){
		if(variable.length > 0)
			res = false;	
			
	}else if( typeof(variable) == "number" ){
		if(variable != 0)
			res = false;
			
	}else if( typeof(variable) == "boolean" ){
		res = !variable;

	}else if( typeof(variable) == "object" ){
	
		if(variable != null && variable != "null"){
		  	try{
				if( typeof(variable.attributes) != "undefined" ){
					if(variable.attributes.length)
						res = false;
				}
		
				if( typeof(variable.childNodes) != "undefined" ){
					if(variable.childNodes.length)
						res = false;
				}
			}catch(error){
				res = false;
			}

		}else
		  res = true;
		  
	}else if( typeof(variable) == "function" ){
		res = false;
	}

	return res;
	
}

function sec_nav(url){
	window.open(url, "external");
}

// JavaScript Document

function Ajax( url, method, is_async){

	this.url = url;
	this.method = method;
	this.async = is_async;
	if(this.async == null || this.async == "null")
	  this.async = true;

	this.set_function = Ajax_set_function;
	this.statechange = Ajax_statechange;
	this.send = Ajax_send;
	this.valid_http_request = Ajax_valid_http_request;
	var status = this.valid_http_request();

	if( status ){
		if (window.XMLHttpRequest) {
			//MOZ
			this.xhr = new XMLHttpRequest();

			this.xhr.open(this.method, this.url, this.async);

		} else if (window.ActiveXObject) {
			//IE
			this.xhr = new ActiveXObject("Microsoft.XMLHTTP");
			if (this.xhr) {
				this.xhr.open(this.method, this.url, this.async);
			}
		}

		if (this.xhr){
			//Request header type to carry post data
			this.xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');

			//Request header type to remove cache on query
			this.xhr.setRequestHeader("Cache-Control", "no-cache");

			this.xhr.onreadystatechange = this.set_function(this);
		}else{
			alert("Your browser does not support AJAX, some features will be missing.");
		}

	}else{
		alert("Ajax missing request information: url=" + this.url + ", ou methode=" + this.method );
	}
}

	function Ajax_valid_http_request(){

		if(	!( typeof(this.url)=="string" && this.url!="" && this.url!="undefined" && this.url!="null") )
			return false;
			
		if(	!( typeof(this.method)=="string" && (this.method=="POST" || this.method=="GET") ) )
			return false;
		
		return true;
	}

	function Ajax_statechange(obj){
		try{

			if(this.xhr.readyState == 4){

				if(this.xhr.status == 200){
	
					if(typeof(obj.onComplete) == "function"){

						obj.onComplete.call(obj, obj.xhr.responseText, obj.xhr.responseXML );
					}
					
				}
			}
		}catch(error){
//			alert( "AJAX ERROR: " + error );
		}	
	}
	
	function Ajax_set_function(param){
		return function(){param.statechange(param); };
	}	
	
	function Ajax_send(post_data){
		if(this.method == "POST"){

			if(typeof(post_data) == "object"){
				if( post_data.tagName.toUpperCase() == "FORM" ){

					var send_data = new Array();
					var i, elem;
					var coll = post_data.elements;
					
					for(i = 0; i < coll.length; i++){
						
						elem = coll[i];
						
						if(elem.tagName.toUpperCase() == "INPUT"){
							
							if( elem.type.toUpperCase() == "CHECKBOX"){

								if( elem.checked ||  elem.checked=="CHECKED" )
									send_data[send_data.length] = elem.name + "=" + elem.value;
								else
									send_data[send_data.length] = elem.name + "=__NULL__";

							}else if( elem.type.toUpperCase() == "RADIO" ){

								if( elem.checked ||  elem.checked=="CHECKED" )
									send_data[send_data.length] = elem.name + "=" + elem.value;
								//else
								//	send_data[send_data.length] = elem.name + "=__NULL__";
							
							}else if( elem.type.toUpperCase() == "TEXT" || elem.type.toUpperCase() == "HIDDEN" ) {

								if(elem.value.length > 0)
									send_data[send_data.length] = elem.name + "=" + elem.value;
								else
									send_data[send_data.length] = elem.name + "=__NULL__";

							}else if( elem.type.toUpperCase() == "PASSWORD" ){
								
								var val = elem.value;
								var no_encrypt = elem.getAttribute("no_encrypt");
								
								if( val.length > 0){
									if( typeof(hex_sha1) == "function" && no_encrypt != "1")
										val = hex_sha1(val);
									
									
								}else
									send_data[send_data.length] = elem.name + "=__NULL__";
									
								send_data[send_data.length] = elem.name + "=" + val;
							}
							
						}else if(elem.tagName.toUpperCase() == "SELECT"){
							if(elem.selectedIndex >= 0){
								if(elem.options[elem.selectedIndex].value != "__UNSET__")
									send_data[send_data.length] = elem.name + "=" + elem.options[elem.selectedIndex].value;
							}else
								send_data[send_data.length] = elem.name + "=__UNSET__";
						
						}else if(elem.tagName.toUpperCase() == "TEXTAREA"){
						
							var atr =  elem.getAttribute("validate");

							if( atr ){
							
							  if(atr.toUpperCase() == "TRUE" ){
							  
								if(elem.value.length > 0)
									send_data[send_data.length] = elem.name + "=" + elem.value;
								else
									send_data[send_data.length] = elem.name + "=__NULL__";
									
							  }else
								send_data[send_data.length] = elem.name + "=" + elem.value;
							  
							}else
								send_data[send_data.length] = elem.name + "=" + elem.value;
						}
					}
					
					//CHANGE OF SPECIALS CHARS
					var j = 0;
					for(j = 0; j < send_data.length; j++){
						send_data[j] = send_data[j].replace(/&/g, '!@!');

					}

					send_data = send_data.join("&");

					if(this.xhr.readyState == 1)
						this.xhr.send(send_data);
					else
						alert("Ajax state not ready");
					
				}else{

					if(this.xhr.readyState == 1)
						this.xhr.send(null);
					else
						alert("Ajax state not ready");

				}
					
			}else{

				if(this.xhr.readyState == 1)
					this.xhr.send(post_data);

				else
					alert("Ajax state not ready");
				
			}
		}else{

			if(this.xhr.readyState == 1)
				this.xhr.send(null);
			else
				alert("Ajax state not ready");

		}
		
		if(!this.async)
		  return this.xhr.responseText
		else
		  return null;
	}
//--JAVASCRIPT PART: node_handling
	function page_init(){

		if( (typeof(page_load)).toUpperCase() == "FUNCTION")
			page_load();

		if( typeof(menu_init) == "function" )
			menu_init();

		if(typeof(NO_ERGO) == "undefined")
			controls_ergo();

		if( (typeof(page_load)).toUpperCase() == "FUNCTION")
			listing_ergo();

		if( (typeof(email_scrambler)).toUpperCase() == "FUNCTION")
			email_scrambler();

		if( (typeof(cms_meta_interact)).toUpperCase() == "FUNCTION")
			cms_meta_interact();

	}

	function controls_ergo(){
		var inputs = document.getElementsByTagName('input');
		var i;

		for(i = 0; i < inputs.length; i++){
			if( inputs[i].onfocus == null){
				if(inputs[i].type.toLowerCase() == 'text'  || inputs[i].type.toLowerCase() == 'password'){
					inputs[i].onfocus = control_ergo_in;
					inputs[i].onblur = control_ergo_out;
				}
			}
			
		}

		inputs = document.getElementsByTagName('select');
		for(i = 0; i < inputs.length; i++){
			if( inputs[i].onfocus == null){
				inputs[i].onfocus = control_ergo_in;
				inputs[i].onblur = control_ergo_out;
			}
			
		}

		inputs = document.getElementsByTagName('textarea');
		for(i = 0; i < inputs.length; i++){
			if( inputs[i].onfocus == null){
				inputs[i].onfocus = control_ergo_in;
				inputs[i].onblur = control_ergo_out;
			}
			
		}

	}
	
	function control_ergo_in(){
		if(typeof(ERGO_COLOR) == "undefined")
		  this.style.backgroundColor = "#EEEEEE";
		else
		  this.style.backgroundColor = ERGO_COLOR;

	}
	
	function control_ergo_out(){
		this.style.backgroundColor = "";
	}

	function apply_control_saver(formObj){
		formObj = formObj.getElementsByTagName('form')[0];
		if(formObj){
		
			var i;
			
			if( typeof(formObj.is_saved) == 'object' && typeof(formObj.is_changed) == 'object' ){
			
				var ctrl = formObj.getElementsByTagName('input');
				for(i = 0; i < ctrl.length; i++){
					if(ctrl[i].type == "text" || ctrl[i].type == "password" || ctrl[i].type == "checkbox" ||ctrl[i].type == "radio" ){
						if( typeof(ctrl[i].onchange) == "function"){
						
						}else
							ctrl[i].onchange = function(){ control_changed(this);};
					}
				}

				ctrl = formObj.getElementsByTagName('select');
				for(i = 0; i < ctrl.length; i++){
					if( typeof(ctrl[i].onchange) == "function"){
						
					}else
						ctrl[i].onchange = function(){ control_changed(this);};
				}
				
				ctrl = formObj.getElementsByTagName('textarea');
				for(i = 0; i < ctrl.length; i++){
					if( typeof(ctrl[i].onchange) == "function"){
						
					}else
						ctrl[i].onchange = function(){ control_changed(this);};
				}
				
				
			}
		}
	}
	
	function close_context_menu(event, force_close){
	  var target = event.target;
	  
	  var parent_target = target;
	  var is_in_context = false;
	  
	  while(parent_target != null){
	  
	  	if(parent_target.id == "context_menu"){
  		  is_in_context = true;
		  break;
		}
		
	    parent_target = parent_target.parentNode;
	  }
	  
	  
	  if(!is_in_context || force_close){
		var menu = document.getElementById("context_menu");

		if(menu != null)
			menu.parentNode.removeChild( menu );
	  }
	}
	
	function clear_object_content(obj){
		var tmp;
		var cpt;
		try{
			if(obj.lang != "list_template" && obj.lang != "form_template"){
				for((cpt = obj.childNodes.length - 1); cpt >= 0; cpt--){
				  tmp = obj.childNodes[cpt];
				  if(tmp.lang != "list_template" && tmp.lang != "form_template")
					  obj.removeChild(tmp);
				}
			}
		}catch(error){
		
		}
	}
	
	function cancel_bubble(e){

		if( !e )
			e = window.event;
			
		e.cancelBubble = true;
		if( e.stopPropagation )
			e.stopPropagation();
	}

	var localXML = new Array();

	function bring_center_form(objBody, objFormOr, close_callback_function, destroy_original, vertical_offset, no_overlay, super_modal, is_fixed){

		if(!no_overlay || typeof(no_overlay) == "undefined")
			no_overlay = false;

		if(!super_modal || typeof(super_modal) == "undefined")
			super_modal = false;
			
		if(!is_fixed || typeof(is_fixed) == "undefined")
			is_fixed = false;
			
		var objForm = objFormOr.cloneNode(true);
		var objOverlay = document.createElement("div");
		
		objOverlay.setAttribute('id', 'ol_' + objForm.id);
		objOverlay.className  = "overlay";

		if(!super_modal)
			objOverlay.onclick = add_close_function(objForm, close_callback_function);
			
		if(!no_overlay)
			objBody.appendChild(objOverlay);

		
	
		var objCenter = document.createElement("div");
		objCenter.className = "centerer";
		objCenter.setAttribute('id', 'fc_' + objForm.id);

		if(!super_modal)
			objCenter.onclick = add_close_function(objForm, close_callback_function);
		
		objForm.onclick = add_cb_function();
		
		objForm.style.display = "block";
		
		if(typeof(objFormOr.className) == "undefined")
			objForm.className = "form_container";
		else
			objForm.className = "form_container " + objFormOr.className;

		if( !vertical_offset)
			vertical_offset = 10;
			
		if( typeof(window) == "object"){
			if( window.scrollY ){
					var scrollX = window.scrollX;
					var scrollY = window.scrollY;
			}else{
				var scrollX = document.documentElement.scrollLeft;
				var scrollY = document.documentElement.scrollTop;
	
			}
		}else{
			var scrollX = 0;
			var scrollY = 0;
		}

		var docHeight = document.documentElement.scrollHeight;

		var form_total_height = parseInt(objForm.style.height) +  parseInt(scrollY);
		//Vertical offset + Adjustement
		form_total_height +=  10 + 30;

		if(docHeight < form_total_height)
			docHeight = form_total_height
			
		objOverlay.style.height = docHeight + "px";
		
		objForm.style.top = (scrollY + vertical_offset) + "px";

		objCenter.appendChild(objForm);	

		if(is_fixed)
		  objCenter.style.position = "fixed";

		objBody.appendChild(objCenter);

		//-- SET VALUES FOR SELECT BOX, DUE TO CLONING PROBLEM

		var original_container = objFormOr; 
		var bring_container = objCenter;
		
		var k;
		
		var original_coll_form = original_container.getElementsByTagName("form");
		var bring_coll_form = bring_container.getElementsByTagName("form");
		
		for(k = 0; k < original_coll_form.length; k++){
		
			var original_myForm = original_coll_form[k];
			var bring_myForm = bring_coll_form[k];

			if( original_myForm.elements ){
				if(original_myForm.elements.getElementsByTagName){
					var original_select_tags = original_myForm.elements.getElementsByTagName("SELECT");
					var bring_select_tags = bring_myForm.elements.getElementsByTagName("SELECT");
					
					var i = 0; 
					var j;
					
					var original_opt, bring_opt;
		
					for(i = 0; i < original_select_tags.length; i++){
					
						for(j = 0; j < original_select_tags[i].options.length; j++){
						
							original_opt = original_select_tags[i].options[j];
							bring_opt = bring_select_tags[i].options[j];
							
							if(original_opt.selected){
								bring_opt.selected = "SELECTED";
							}
						}
		
					}
				}
			}
		}

		if(destroy_original){
			objFormOr.parentNode.removeChild(objFormOr);
		}
		
		if(typeof(bring_opened) == "function"){
			bring_opened();
		} 

		controls_ergo();
		apply_control_saver(objForm);
		objForm.onclick = function(event){cancel_bubble(event); calendar_close(); };
		return objForm;
	}

	function add_close_function(objForm, close_callback_function){
		var func = function() { close_sub_form( objForm, close_callback_function ); calendar_close(); return false; };
		return func;
	}
	
	function add_cb_function(){
		return function(event){ cancel_bubble(event); };
	}

	function control_changed(control){
		var form = recursive_form_find(control,0);
		var is_changed = form.getElementsByTagName("INPUT")[1];
		is_changed.value = "1";
	}

	var max_level = 15;
	function recursive_form_find(obj, level){
	
		if( typeof(obj) != "undefined" && obj != null ){
			if(obj.tagName == "FORM"){
				return obj;
			}else{
				if(level > max_level){
					alert("Erreur de recherche de formulaire:#188");
					return null;
				}else{
					return recursive_form_find(obj.parentNode, level++);
				}
			}
		}else
			return null;
	}

	function fill_form(objForm, record, id){
		var i, j, k, l, field, field_name, form_element;
	
		if( (objForm.tagName).toUpperCase() == "FORM" )
			form_control = objForm;
		else
			form_control = objForm.getElementsByTagName("FORM")[0];
	
		if(record.constructor != null)
			var pos = record.constructor.toString().indexOf("Array");
		else{
			if( typeof(record.join) != "undefined")
				pos = 1;
		}

		if( pos > 0 ){
			//IF THE RECORD IS AN ASSOCIATIVE ARRAY, MUST CONVERT TO XMLDOC
			var xmlRecord = convert_array_to_xml( record, id );
			xmlRecord = xmlRecord.firstChild;
		}else
			var xmlRecord = record;
	
		var field_value;
		
		for(i = 0; i < xmlRecord.childNodes.length; i++){
		
			field = xmlRecord.childNodes[i];
			if(field.hasChildNodes()){
				field_value = field.childNodes[0].nodeValue
			}else
				field_value = '';
			
			if(field_value == null)
				field_value = ''
				
			if(typeof(field) != "undefined"){
			
				field_name = field.tagName;
			
				if(field_name == "dynamic_properties"){
	
				}else{
					if(field_name != ""){

						for(k = 0; k < form_control.elements.length; k++){
							form_element = form_control.elements[k];
						
							if(form_element.name == field_name){
								 if(form_element.tagName.toUpperCase() == "INPUT"){
		
									if(form_element.type.toUpperCase() == "TEXT"){
		
										form_element.value = field_value;

									}else if(form_element.type.toUpperCase() == "HIDDEN"){
										if(form_element.className != "no_clear")
											form_element.value = field_value;
		
									}
								 
								 }else{
									if(form_element.tagName.toUpperCase() == "SELECT"){
										
										for(l = 0; l < form_element.options.length; l++){
										
											if( form_element.options[l].value == field_value){
												form_element.options[l].selected = "selected";
												form_element.options[l].setAttribute('selected', 'selected');
												break;
											}
										}
									}
								 }
								
								break;	
							}
							
						}
					}
				}		
			}
		}

	}

	function updateHtmlNode(templateObject, xmlNode, update_existing, index, name_extra){

		var j, tmp_object, tmp_cell;
		var property_name, content, tmp_content, node;
		
		var is_new = false;

		if(typeof(name_extra) != "string")
			name_extra = "";
			
	
		if(xmlNode != null){
		
			var template = templateObject.innerHTML;

			template = template.split("#@#");

			var template_out = templateObject.innerHTML;
			template_out = template_out.split("#@#");
			
			var id = xmlNode.getAttribute('id');
			
			if( ! update_existing){
	
				var tmp_object = document.createElement( templateObject.tagName );
				tmp_object.className = templateObject.className;
				//tmp_object.name = templateObject.name;
				var value_property = templateObject.value;
				
				if(typeof(value_property) != "undefined" && value_property != null && typeof(value_property) == "string" ){
				
					value_property = value_property.split("#@#");
					value_property = value_property[1];
					
					if(value_property.length > 0){
					
						var value_node = xmlNode.getElementsByTagName(value_property)[0];
						tmp_object.value = value_node.firstChild.nodeValue;
					}

				}
				
				tmp_object.setAttribute('id', name_extra + "rec_" + xmlNode.getAttribute('id') );
				
				if(templateObject.getAttribute('name') != null)
					tmp_object.setAttribute('name', templateObject.getAttribute('name') );

				is_new = true;

			}else{
			
				var top_object = templateObject.parentNode;
				var tmp_object;
				for(j = 0; j < top_object.childNodes.length; j++){
				
					tmp_object = top_object.childNodes[j];
					
					if( typeof(tmp_object) == 'object' && tmp_object.innerHTML != "undefined" ){
					
						if( typeof(tmp_object.id) == "string" ){
						
							if(tmp_object.id != templateObject.id){

								if(tmp_object.id == name_extra + "rec_" + id){
								
									tmp_object = top_object.childNodes[j];
									break;
			
								}
							}
						}
	
					}
					
					tmp_object = '';
				}
			}
			

			
			if( typeof( tmp_object.id) == "undefined" || tmp_object.id == templateObject.id){
			
				var tmp_object = document.createElement( templateObject.tagName );
				tmp_object.className = templateObject.className;
				tmp_object.setAttribute('id', name_extra + "rec_" + xmlNode.getAttribute('id') );
				
				if(templateObject.getAttribute('name') != null)
					tmp_object.setAttribute('name', templateObject.getAttribute('name') );

				is_new = true;
			}
			


			for(j = 1; j < template.length; j = j + 2){
		
				property_name = template[j];
				
				if(property_name == "index")
					template_out[j] = id;
				else{
					node = xmlNode.getElementsByTagName(property_name)[0];

					if( typeof(node) != "undefined"){

						node = node.firstChild;
						
						if(typeof(node) != "undefined" && node != null)
							template_out[j] = node.nodeValue;
						else
							template_out[j] = "";

					}
				}
			}

			content = "";
			for(j = 0; j < template_out.length; j++){
				content = content + "" + template_out[j];
		
			}

			if(templateObject.tagName.toUpperCase() == "TR"){

				content = content.replace(/\<TD\>/g, "<td>");
				content = content.replace(/\<\/td\>/gi, "");
				var tempo_div = document.createElement('div');

				try{
					if(typeof(content.split) == "function"){
						tmp = content.split("<td>");
						tmp.shift();
					}else
						tmp = new Array();
						
				}catch(error2){
					tmp = new Array();
				}
				
				
				content = "";

				if( is_new && tmp.length > 0){
				
				

					for(j = 0; j < tmp.length; j++){
					
						if( typeof(tmp_object.cells.length) == "undefined"){
							tmp_cell = tmp_object.insertCell(0);
							
							if( tmp_cell == null){
								tmp_cell = document.createElement("TD");
								tmp_object.appendChild( tmp_cell );
								
							}
							
							tmp_cell.innerHTML = tmp[j];
						
						}else{
							tmp_cell = tmp_object.insertCell(tmp_object.cells.length);
							tmp_cell.innerHTML = tmp[j];
						}
					}

				}else{
				
					for(j = 0; j < tmp.length; j++){
					
						tmp_cell = tmp_object.cells[j];
						tmp_cell.innerHTML = tmp[j];
			
					}
				
				}

			}else{
			
				if(templateObject.tagName.toUpperCase() == "TABLE"){
				
					tmp_object = document.createElement("span");
					tmp_object.innerHTML = "<TABLE>" + content + "</TABLE>";

				}else
					tmp_object.innerHTML = content;
			}
			
//			alert( templateObject + " : " + tmp_object );

			templateObject.parentNode.appendChild(tmp_object);
			
		}else{
		//xmlNode == null, DELETING
			var id = index;

			//DELETING
			var top_object = templateObject.parentNode;
			var tmp_object;

			for(j = 0; j < top_object.childNodes.length; j++){
			
				tmp_object = top_object.childNodes[j];
				if( typeof( tmp_object ) == 'object' && tmp_object.innerHTML != undefined ){
					index = tmp_object.getAttribute('id');

					if( index == name_extra + "rec_" + id ){
					
						if(tmp_object.tagName != "row")
							tmp_object.parentNode.removeChild(tmp_object);
						else
							tmp_object.parentNode.deleteRow(index);

						break;
	
					}
	
				}
			}

		}

	}

	function recursive_element_by_id(parent_object, object_id, depth){
	
		var i, node;
			
		if( (typeof(parent_object)).toUpperCase() == "OBJECT" && depth < 25){
		
			if(parent_object.hasChildNodes){
			
				for(i = 0; i < parent_object.childNodes.length; i++){
				
					node = parent_object.childNodes[i];
					
					if(node.id == object_id){
						return node;
					}else{
					
						node = recursive_element_by_id( node, object_id, (depth + 1) );
						if(node != null){
						
							if(node.id == object_id){
						
								return node;
							}
						}
					}
				
				}
				
			}else{
				return null;
			}
		}else{
			return null;
		}
	
		return null;
	}
	
	function convert_array_to_xml( my_array, id ){
	
		if (document.implementation && document.implementation.createDocument) {
		//MOZ
			xmlDoc = document.implementation.createDocument("", "", null);
			return build_xml(xmlDoc, my_array, id );
			
		}else if (window.ActiveXObject) {
	  	//IE
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			return build_xml(xmlDoc, my_array, id );
		}else {
			alert('Your browser can\'t handle this XML CREATION script');
			return null;
		}

	}
	
	function build_xml(xmlDoc, my_array, id ){
	
		var field, elem, text_node;
		
		var record = xmlDoc.createElement("record");
		xmlDoc.appendChild(record);
		record.setAttribute('id', id);
				
		for( field in my_array){
			
			elem = xmlDoc.createElement(field);
			record.appendChild( elem );
			
			text_node = xmlDoc.createTextNode( my_array[field] );
			elem.appendChild( text_node );
			
			
		}
	
		return xmlDoc;
	
	}
//--JAVASCRIPT PART: ajax_form
	function load_datas( remote_object, add_url_param){

		var command = retrieve_template( remote_object );
		var data_provider_url = command['data_provider'];
		var complement_control_id = command['complement_control_id'];
		var xml_name = command['xml_name'];

		if(!add_url_param)
			add_url_param = ''

		

		if(data_provider_url != null && data_provider_url != "null"){
			data_provider_url += add_url_param;

			var myAjax = new Ajax( data_provider_url , 'POST');
			

			if( typeof(complement_control_id) != "undefined" && complement_control_id!="null" && complement_control_id!=null && complement_control_id != ""){

				//EDITING AND DELETING
				var datas = remote_object;
	
				var found = false;
				var recursive_limit = 15;
				var recursive_count = 0;
				
				var main_container = remote_object;			
				
				while( !found ){
				
						
					if( typeof(main_container) == "object"){
					
						if(typeof(main_container.id) == "string"){
							if(main_container.id == "main_container"){
								
								found = true;
							}
						}
					
					}
					recursive_count++;
					
					if(recursive_count > recursive_limit)
						break;
						
					main_container = main_container.parentNode;
				}
				
	
				var listing_object = recursive_element_by_id( main_container, complement_control_id, 0);
				
				if( typeof( listing_object ) == "undefined" ){
					listing_object = document.getElementById( complement_control_id );
				}
	
	
				myAjax.onComplete = add_function(receive_update, remote_object, listing_object);
				
				var is_saved = datas.getElementsByTagName("INPUT")[0];
				is_saved.value = "1";

				myAjax.send(datas);
	
				datas.getElementsByTagName("INPUT")[0].value = "0";
				datas.getElementsByTagName("INPUT")[1].value = "0";

			}else{
				//LISTING AND AUTHENTICATION
	
	
					myAjax.onComplete = add_function(receive_listing_datas, remote_object);
					myAjax.send(remote_object);
			}
		}

	}

	function retrieve_template( remote_object ){
	
		if( (typeof(remote_object)).toUpperCase() == "OBJECT" && remote_object != null && remote_object != "null" && remote_object != ""){
			var title_command = remote_object.getAttribute('title');

			var data_provider_command = remote_object.getAttribute('data_provider');
			//ALTERNATE ATTRIBUTE
			if(typeof(data_provider_command) != "undefined"){
				if(data_provider_command != null){
					if(data_provider_command.length > 0)
						title_command = data_provider_command;
				}
			}
			
			

			var commands = title_command.split(";");

			var command_args = new Array();
			var tmp, command_name, command_value;
				
			for(var i = 0; i < commands.length; i++){
			
				tmp = commands[i].split(":");
	
				command_name = tmp.shift();
				command_value = tmp.join(":");
				
				if(command_value == "true")
					command_value = true;
				else if(command_value == "false")
					command_value = false;
					
				command_args[ command_name ] = command_value;

			}
			
			return command_args
		}else{
			return new Array();
		}
	}

	function add_function(receive_function, param1, param2){
	
		return function(responseText, responseXML){ receive_function(responseText, responseXML, param1, param2); };
	}
	
	function close_this_sub_form( control, close_callback_function){
	
		var objForm = recursive_form_find(control, 0);
		var fc = objForm.parentNode.parentNode;
		fc.onclick.call();
		
		if( (typeof(close_callback_function)).toUpperCase() == "FUNCTION" ){
			close_callback_function.call();
		}
	}

	function close_sub_form( objForm, close_callback_function){
	
		var is_saved = objForm.getElementsByTagName("INPUT")[0];
		var is_changed = objForm.getElementsByTagName("INPUT")[1];
		var res = true;

		if( typeof(is_saved) == "object" && typeof(is_changed) == "object" ){
		
			if(is_saved.value == "0" && is_changed.value != "0"){
				var res = confirm("Voulez vous quitter sans sans sauvegarder vos informations?");
			}
		}

		
		if(res){
			//PRECLOSE JOB
			if( (typeof(preclose_sf)).toUpperCase() == "FUNCTION" ){
	
				preclose_sf(objForm.id);
			}
		
			var objCenter = document.getElementById( "fc_" + objForm.id );
			var objOverlay = document.getElementById( "ol_" + objForm.id );
			
			if(typeof(objCenter) == "undefined" || objCenter == null || objCenter == "null" || objCenter == ""){

				objCenter = document.getElementById( "fc_" + objForm.parentNode.id );
				objOverlay = document.getElementById( "ol_" + objForm.parentNode.id );
			
			}
			
			if( typeof(objCenter) != "undefined" && objCenter != null){
				objCenter.parentNode.removeChild(objCenter);
			}
			
			if( typeof(objOverlay) != "undefined" && objOverlay != null){
				objOverlay.parentNode.removeChild(objOverlay);
			}
			
			clear_ajax_form(objForm);
					
			if( (typeof(close_callback_function)).toUpperCase() == "FUNCTION" ){
				close_callback_function.call();
			}
		}

	}
	
	function save_ajax_form(control){
	
		var form = recursive_form_find(control,0);
		load_datas( form );
	}

	function delete_ajax_form(control, action){
	
		var form = recursive_form_find(control,0);
		load_datas( form, action );
	}
	
	function delete_ajax_list_item(control){
//		load_datas(, 'del');
	}
	
	function clear_ajax_form(form_object){
	
		if( form_object != null && typeof(form_object) != "undefined" ){
		
			if(form_object.tagName != "form")
				form_object = recursive_form_find(form_object,0);
				
			if( form_object != null && typeof(form_object) != "undefined" ){
				for(i = 0; i < form_object.elements.length; i++){
			
					elem = form_object.elements[i];
					if( (typeof(elem)).toUpperCase() == "OBJECT") {
						
						if(elem.name != ""  && (elem.tagName).toUpperCase() != "FIELDSET" && (elem.disabled == false || elem.disabled == "false") ){
		
							if(elem.type != "checkbox" && elem.type != "radio" &&elem.type != "button" && elem.type != "image" && elem.type != "submit" && elem.type != "reset" && elem.className != "no_clear" )
								elem.value = "";
								
							
							
						}
					}							
				}
			}
		}
	}
	
	function lock_ajax_form(form_object){
	
		if(form_object.tagName != "form")
			form_object = recursive_form_find(form_object,0);
	
		for(i = 0; i < form_object.elements.length; i++){
	
			elem = form_object.elements[i];
			if( (typeof(elem)).toUpperCase() == "OBJECT") {
			
				
				if(elem.name != ""  && (elem.tagName).toUpperCase() != "FIELDSET" && (elem.type).toUpperCase() != "HIDDEN" && (elem.disabled == false || elem.disabled == "false") ){

					elem.disabled = true;
					elem.className = "control_disabled";
					
				}
			}							
		}
	}
	
	function unlock_ajax_form(form_object){
	
		if(form_object.tagName != "form")
			form_object = recursive_form_find(form_object,0);
	
		for(i = 0; i < form_object.elements.length; i++){
	
			elem = form_object.elements[i];
			if( (typeof(elem)).toUpperCase() == "OBJECT") {
			
				
				if(elem.name != ""  && (elem.tagName).toUpperCase() != "FIELDSET" && (elem.type).toUpperCase() != "HIDDEN" && (elem.disabled == true || elem.disabled == "true") ){

					elem.disabled = false;
					elem.className = "";
					
				}
			}							
		}
	}
		
	function show_sub_form(body_name, form_name, template_name, index, is_password_checked){
	
		var objBody = document.getElementById(body_name);
		var objForm =  document.getElementById(form_name);
		objForm = bring_center_form(objBody, objForm);
	
		if(typeof is_password_checked == "undefined" || is_password_checked == "undefined")
			is_password_checked = true;
				
		try{
		
			if(is_password_checked){
				form_control = objForm.getElementsByTagName("FORM")[0];
				password_validator.add_security_to_form(form_control);
			}
		}catch(error){
		
		}
	
		var tmp = retrieve_template( template_name );
		var data_provider_url = tmp[0];
		var data_provider_action = tmp[1];
		var complementary_form = tmp[2];
		var data_provider_varname = tmp[3];
		var owner_object = tmp[4];
		var template_info = tmp[5];
		
		var xmlObj = localXML[data_provider_varname];
		
		var global = xmlObj.getElementsByTagName("GLOBAL")[0];
		var records_container = global.getElementsByTagName("records")[0];
		if(typeof(records) != "undefined"){
			var records = records_container.getElementsByTagName("record");
		
			var record = find_record(records, index);
			if( typeof(record) == "object" ){
				fill_form(objForm, record );
			}
		}		
	}

	function find_record(xmlRecords, id){
		var res = null;
		var i = 0;
		for(i = 0; i < xmlRecords.length; i++){
			if(xmlRecords[i].getAttribute('id') == id){
				res = xmlRecords[i];
				break;
			}
		}
		
		return res;
		
	}
	
	function receive_update(responseText, responseXML, form_object, list_object){
		var global = responseXML.getElementsByTagName("GLOBAL")[0];
		var status = global.getElementsByTagName("status")[0].childNodes[0].nodeValue;
		var description = global.getElementsByTagName("description")[0].childNodes[0].nodeValue;
		var insert_item = global.getElementsByTagName("insert_item");

		if( insert_item.length == 1)
			insert_item = insert_item[0].childNodes[0].nodeValue;
		else
			insert_item = null;
			
		var command = retrieve_template( form_object );
		var close_after = command['close_after'];
	
		var xml_name = command['xml_name'];
		localXML[ xml_name ] = responseXML;
	
		if( close_after != false){
			//INTERFACE MUST BE CLOSE
			close_sub_form(form_object);
		}else{
			if( typeof(form_changed) == "function")
				form_changed(form_object, list_object);
		}
		
		
		
		var call_back_function = command['call_back_function'];
		if(call_back_function != null && typeof(call_back_function) != "undfined"){
			
			try{
				if(insert_item){
					var protected_item = "'" + escape(insert_item) + "'";
					
					eval(call_back_function + "(" + protected_item + ")");
					
				}else
					eval(call_back_function + "()");
			}catch(error){
//				alert(error);
			}

		}
		
		
		if( typeof( call_back_function) == "function" ){
			call_back_function.call(null, insert_item);
		}

		load_datas( list_object );
		
	}

	function receive_listing_datas(responseText, responseXML, remote_object){

		var command = retrieve_template( remote_object );
		var data_provider_url = command['data_provider'];
		var complement_control_id = command['complement_control_id'];
		var xml_name = command['xml_name'];
		var call_back_function = command['call_back_function'];
		var record_name_extra = command['record_name_extra'];

		if( complement_control_id != "" && typeof(complement_control_id) !="undefined" )
			myRemote = document.getElementById( complement_control_id );	
		else
			myRemote = remote_object;

		var previousXML = localXML[ xml_name ];

		if( typeof( previousXML ) != "undefined" && previousXML != null && previousXML != "null" ){

			if(previousXML == responseXML)
				return null;
				
			else{
				//UPDATE

				var pglobal = previousXML.getElementsByTagName("GLOBAL")[0];
				var precords_list = pglobal.getElementsByTagName("records")[0];
				
				if(typeof(precords_list) == "object"){

					var precords = precords_list.getElementsByTagName("record");
					
					var global = responseXML.getElementsByTagName("GLOBAL")[0];
					var records_list = global.getElementsByTagName("records")[0];
					var records = records_list.getElementsByTagName("record");
					
					var id, is_found, rec, prec;
					var updated_ids = new Array();
					
					for(i = 0; i < precords.length; i++){
		
						prec = precords[i];
						id = prec.getAttribute('id');
						rec = find_record(records, id );
						updated_ids[ updated_ids.length ] = id;				
						
						if( rec != null){
						
							if( prec != rec){
								//UPDATED RECORD
								updateHtmlNode(myRemote, rec, true, 0, record_name_extra);
							}
							
						}else{
							//DELETED RECORD
							updateHtmlNode(myRemote, null, false, id, record_name_extra);
						}

					}
					
					for(i = 0; i < records.length; i++){

						rec = records[i];
						is_found = false;

						for(j = 0; j < updated_ids.length; j++){

							if( rec.getAttribute('id') == updated_ids[j] ){
								is_found = true;
								break;

							}
						}

						if(!is_found){
							//NEW RECORD
							updateHtmlNode(myRemote, rec, false, 0, record_name_extra);
						}
					}


					localXML[ xml_name ] = responseXML;
				}
			}
			
		}else{
			//INSERT
			localXML[ xml_name ] = responseXML;

			var global = responseXML.getElementsByTagName("GLOBAL")[0];
			if(typeof(global) == "object"){

				var records_list = global.getElementsByTagName("records")[0];
				
				if( typeof(records_list) == "object"){
					var records = records_list.getElementsByTagName("record");
		

					if( typeof(records) != "undefined"){

						var id;
						for(i = 0; i < records.length; i++){
							id = records[i].getAttribute('id');
							var tmp_control  = document.getElementById( record_name_extra + "rec_" + id);
							
							if(tmp_control)
								updateHtmlNode(myRemote, records[i],true, 0, record_name_extra);
							else
								updateHtmlNode(myRemote, records[i],false, 0, record_name_extra);
						}
					}
				}
			}
			
		}
		

		
		if(call_back_function != null && typeof(call_back_function) != "undfined"){
			
			try{
				eval(call_back_function + "()");
			}catch(error){

			}

		}
		
		
		if( typeof( call_back_function) == "function" ){
			call_back_function.call(null);
		}

		if( typeof(listing_ergo_filter_loaded) =="function"){
			listing_ergo_filter_loaded(remote_object);
		}

		return true;
	}
	
	function page_receive(responseText, responseXML){
	
		var global = responseXML.getElementsByTagName("GLOBAL")[0];
		var html_content = global.getElementsByTagName("html_content")[0];
		var html_content = html_content.firstChild.nodeValue;
		
		var myDiv = document.createElement("DIV");
		myDiv.setAttribute('id', 'mydiv');
		myDiv.style.display = "none";
		myDiv.innerHTML = html_content;	

		document.body.appendChild(myDiv);
		var myInsert = myDiv.getElementsByTagName("DIV")[0];
		var myForm = myDiv.getElementsByTagName("FORM")[0];
		bring_center_form( document.body, myInsert, close_cb_fc, true );
		
		if(typeof(page_receive_init) == "function")
			page_receive_init(myInsert.id);
	
	}
	
	function close_cb_fc(){
		
		var myDiv = document.getElementById('mydiv');
		myDiv.parentNode.removeChild( myDiv );

		if( (typeof(close_sf)).toUpperCase() == "FUNCTION" ){

			close_sf();
		}
	}
//JAVASCRIPT DOCUMENT
//READ AND WRITE, CENTRALIZED
//JAVASCRIPT DOCUMENT: rating.php
function rate(rate_type, rate_remote, amount, rating_token){

	myAjax = new Ajax( "http://www.centrevillemagog.ca/data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&module=rating_comment_favorite&a=rate&rating_type=" + rate_type + "&rating_remote_id=" + rate_remote + "&rating_value=" + amount + "&rating_token=" + rating_token, 'POST');
				myAjax.onComplete = receive_rating;
				myAjax.send();
}

function receive_rating(responseText, responseXML){

}

var rating_set = new Array();

function rating_move(obj, amount, rating_type, rating_remote_id){

	var i = 0;
	
	var cell = obj.parentNode;
	var row = cell.parentNode;
	
	
	if(typeof(rating_set[rating_type + ":" + rating_remote_id]) != "undefined"){
		return;
	}
	
	rating_set_images(row, amount);
	

}

function rating_set_images(row, amount){

	if( amount <= row.cells.length ){
	
		var nb = Math.floor(amount);

		for(i = 0; i < nb; i++){

			row.cells[i].style.backgroundImage = "url(/images/design/etoile_pleine_profil.jpg)";
		}
		
		if( (amount - nb) > 0){
			row.cells[i].style.backgroundImage = "url(/images/design/etoile_demi_profil.jpg)";
			i++;
		}
		
		while(i < row.cells.length){
			row.cells[i].style.backgroundImage = "url(/images/design/etoile_vide_profil.jpg)";
			i++;
		}
	
	}
}

function rating_clear(obj){
	var table = obj.parentNode.parentNode;
	var rating_value = table.getAttribute("rating_value");
	var diff, icon;
	
	var full_star_path = "/images/design/etoile_pleine_profil.jpg";
	var half_star_path = "/images/design/etoile_demi_profil.jpg";
	var empty_star_path = "/images/design/etoile_vide_profil.jpg";
	
	for(i = 0; i < obj.cells.length; i++){
		diff = (rating_value - i ) * 10 ;
				
		if( diff >= 10 ){
			icon = full_star_path;
		}else if( diff > 0 && diff < 10){
			icon = half_star_path;
		}else{
			icon = empty_star_path;
		}
		obj.cells[i].style.backgroundImage = "url(" + icon + ")";
	}
}

function rating_process(obj, amount, rating_type, rating_remote_id, rating_token){

	var cell = obj.parentNode;
	var row = cell.parentNode;
	
	
	if(typeof(rating_set[rating_type + ":" + rating_remote_id]) == "undefined"){
		rating_set[rating_type + ":" + rating_remote_id] = amount;
		row.onmouseout = null;
		rate(rating_type, rating_remote_id, amount, rating_token);
	}

}

var favorite_control_pending;

function show_favorite_form(curr_obj ){

	favorite_control_pending = curr_obj;
	var ns = curr_obj.parentNode.getElementsByTagName("div")[0];

	show_callout(ns.innerHTML, curr_obj, 10, 10, 225, 158, 0, false, null );
}

var favorite_object_pending;

function add_to_favorite( curr_obj ){
	
	favorite_object_pending = curr_obj;
	
	var myForm = recursive_form_find( curr_obj );
	
	var id_favorite_group = myForm.id_favorite_group;
	var fg_name = recursive_element_by_id(myForm, 'name', 0);
	
	var is_valid = true;
	
	if(typeof(id_favorite_group) != "object" || id_favorite_group == null)
		is_valid = false;
//	else if(id_favorite_group.value <= 0 )
//		is_valid = false;
	
	if( !is_valid && fg_name.value.length > 0 )
		is_valid = true;

	if( is_valid ){
		var url = "http://www.centrevillemagog.ca/data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&module=rating_comment_favorite&a=add_favorite";
		var myAjax = new Ajax(url, 'POST');
		myAjax.onComplete = add_to_favorite_receive
		myAjax.send(myForm);
		
	}else{
		id_favorite_group.style.border="1px solid #FF0000;"
		fg_name.style.border="1px solid #FF0000;"
	}
}

function add_to_favorite_receive(responseText, responseXML){
	close_this_callout(favorite_object_pending);

	var global = responseXML.getElementsByTagName("GLOBAL")[0];
	var status = global.getElementsByTagName("status")[0].firstChild.nodeValue;
	var id_favorite= global.getElementsByTagName("id_favorite")[0].firstChild.nodeValue;

	if(status == "OK"){

		var pn = favorite_control_pending.parentNode;
		favorite_control_pending.style.display = "none";
		var conf = document.createElement("span");
		conf.innerHTML = "Fait parti de vos favoris <span class='favorite_link' onclick='JAVASCRIPT: remove_from_favorite("+id_favorite+", this)'>retirer</span>&nbsp;&nbsp;<span class='favorite_link' onclick='JAVASCRIPT: view_favorite("+id_favorite+");'>voir</span>";
		pn.appendChild( conf );
		
	}
	
	
	favorite_object_pending = null;
	favorite_control_pending = null;
}

function remove_from_favorite(id_favorite, obj){

	if(typeof(obj) == "object"){
		var sn = obj.parentNode;
		var pn = sn.parentNode;
	
		clear_object_content(sn);
		
		pn.removeChild(sn);
	
		var btn = pn.getElementsByTagName("input")[0];
		btn.style.display = "block";
	}
	
	var url = "http://www.centrevillemagog.ca/data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&module=rating_comment_favorite&a=del_favorite&id_favorite=" + id_favorite;
	var myAjax = new Ajax(url, 'GET');
	myAjax.send();
	
}

function view_favorite(id_favorite){
	document.location = "/page_module.php?tZ=1328756736&module=rating_comment_favorite&page=favorite_view&id_favorite=" + id_favorite;
}
//JAVASCRIPT DOCUMENT: side_menu.js.php

var menu_mouse_over_add = "_up";
var menu_file_ext = ".png";
var color_switch = new Array("#000000", "#FFFFFF");

function sm_over(obj){

	var id = obj.id;
			
	var rem_obj = document.getElementById(id + "_reg");
	var bring_obj = document.getElementById(id + "_over");
	
	
	if(typeof(rem_obj) == "object")
		rem_obj.style.display = "none";
		
	if(typeof(bring_obj) == "object")
		bring_obj.style.display = "block";
	
	
}

function sm_out(obj){

	var id = obj.id;
			
	var rem_obj = document.getElementById(id + "_over");
	var bring_obj = document.getElementById(id + "_reg");
	
	
	if(typeof(rem_obj) == "object")
		rem_obj.style.display = "none";
		
	if(typeof(bring_obj) == "object")
		bring_obj.style.display = "block";

}//JAVASCRIPT DOCUMENT: keyword.js.php
var current_keyword_type_obj;
var current_id_mmi;
function keyword_type(obj, id_mmi){

	current_keyword_type_obj = obj;
	current_id_mmi = id_mmi;
	var myAjax = new Ajax( "data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&module=photo_album&a=keyword_list&is_accepted=Y&mask=" + obj.value + "&id_mmi=" + id_mmi, "POST");
	myAjax.onComplete = keyword_type_receive;
	myAjax.send();
	
}

function keyword_type_receive(responseText, responseXML){

	var obj = current_keyword_type_obj;
	
	if( typeof(obj) != "object")
		return;
		
	var control_name = obj.getAttribute("lister_control");
	var lister_control = document.getElementById( control_name );
	
	var global = responseXML.getElementsByTagName("GLOBAL")[0];
	var records_container = global.getElementsByTagName("records")[0];
	var records = records_container.getElementsByTagName("record");
	var i, id_keywords, text, token, new_li;
	
	clear_object_content(lister_control);
	
	if( lister_control.tagName == "UL" ){
	
		
		if(typeof(records) != "undefined"){
			
			for(i = 0; (i < 10 &&  i < records.length); i++){
			
				id_keywords = records[i].getElementsByTagName("id_keywords")[0].firstChild.nodeValue;
				text = records[i].getElementsByTagName("text_")[0].firstChild;
				if(text)
					text = text.nodeValue;
					
				token = records[i].getElementsByTagName("token")[0];
				if(token)
					token = token.firstChild.nodeValue;
						
				new_li = document.createElement("LI");
				new_li.innerHTML = text;
				new_li.setAttribute("token", token);
				new_li.setAttribute("id_keywords", id_keywords);
				new_li.onclick = keyword_add;
				new_li.style.cursor = "pointer";
				new_li.onmouseover = list_over;
				new_li.onmouseout = list_out;
				
				lister_control.appendChild(new_li);
				
			}
		
			
		}	
	
	}else if( lister_control.tagName == "SELECT" ){
	
	}

}

function keyword_add(){

	if(current_id_mmi){
	
		var id_keywords = this.getAttribute("id_keywords");
		var token = this.getAttribute("token");
		
		var myAjax = new Ajax( "data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&module=photo_album&a=keyword_add&is_accepted=Y&id_keywords=" +id_keywords + "&id_multimedia_item=" + current_id_mmi + "&token=" + token, "POST");
		myAjax.onComplete = keyword_add_receive;
		myAjax.send();
	}
}

function keyword_add_receive(responseText, responseXML){
	var lister = document.getElementById('tag_list');
	load_datas(lister);
}//JAVASCRIPT DOCUMENT: chat.js.php
var pool_check_timer;
function load_poolling_system(){
}

var pool_notifier;

function pool_check(){

	//CHAT POOL
	var myAjax = new Ajax( "data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&module=chat&a=chat_get_action" , "POST");
	myAjax.onComplete = chat_get_action;
	myAjax.send();
	
}

var users_sender = new Array();
var rooms = new Array();

function chat_get_action(responseText, responseXML){

	if(typeof(responseXML) != "undefined"){
	
		var global = responseXML.getElementsByTagName("GLOBAL")[0];
		var records_list = global.getElementsByTagName("records")[0];
		
		if(typeof(records_list) != "undefined"){
		
			var records = records_list.getElementsByTagName("record");
			var id, sender, receiver, room, moment, type, value, user_nickname, user_avatar, room_name, room_created, token;
			
			if(typeof(records) != "undefined"){
			
				var i;
				
				for(i = 0;  i < records.length; i++){
				
					id = records[i].getElementsByTagName("id_chat_action")[0].firstChild.nodeValue;

					token = records[i].getElementsByTagName("token")[0].firstChild.nodeValue;
					
					sender = records[i].getElementsByTagName("id_chat_users_sender")[0];
					if(sender.hasChildNodes())
						sender = sender.firstChild.nodeValue;
						
					receiver = records[i].getElementsByTagName("id_chat_users_receiver")[0];
					if(receiver.hasChildNodes())
						receiver = receiver.firstChild.nodeValue;
						
					room = records[i].getElementsByTagName("id_chat_room")[0];
					if(room.hasChildNodes())
						room = room.firstChild.nodeValue;

					moment = records[i].getElementsByTagName("moment")[0].firstChild.nodeValue;

					type = records[i].getElementsByTagName("chat_action_type")[0].firstChild.nodeValue;

					value = records[i].getElementsByTagName("chat_value")[0].firstChild.nodeValue;

					user_nickname = records[i].getElementsByTagName("user_nickname")[0];
					if(user_nickname.hasChildNodes())
						user_nickname = user_nickname.firstChild.nodeValue;

					room_name = records[i].getElementsByTagName("room_name")[0];
					if(room_name.hasChildNodes())
						room_name = room_name.firstChild.nodeValue;
						
					room_created = records[i].getElementsByTagName("room_created")[0];
					if(room_created.hasChildNodes())
						room_created = room_created.firstChild.nodeValue;
				
					if(type == "PRIVATE_MESSAGE"){
					
						if(typeof(users_sender[sender]) != "undefined"){
							var check_window = pm_windows[sender];
							
							if( typeof(check_window) == "object"){
								if(!check_window.closed){
									//TODO: emphasize the line of this users because the window is opened						
								}else{
									//TODO: emphasize the line of this users because the window is closed
								}
							}else{
									//TODO: emphasize the line of this users because the window is closed
							}
							
						}else{
							users_sender[sender] = new Array(moment, value, user_nickname, token, receiver );
							update_chat_status_content(type, sender);
						}
						
					}
					
				
				}
				
				
			
			}
		}else{
			var code = global.getElementsByTagName("code")[0];
			
			if(typeof(code) != "undefined"){
				code = code.firstChild.nodeValue;
				
				if(code == 1)
					window.clearInterval( pool_check_timer );
			}
			
		}	
	}
}


function update_chat_status_content(type, sender){

	var sender_infos = users_sender[sender];
	
	var notifier = document.createElement("a");

	notifier.onclick = show_chat_status;
	notifier.innerHTML = "Vous avez des messages priv&eacute;s";
	pool_notifier.appendChild(notifier);
	
	var div;
	var elem;
	var is_found = false;
	
	div = document.getElementById('chat_status');
	var is_div = false;
	try{
	
		if(typeof(div) == "object" && div.tagName=="div")
			is_div = true;
			
	}catch(error){
	
	}
	
	if( !is_div){

		div = document.createElement("div");
		div.style.position = "relative";
		div.style.float = "left";
		div.style.clear = "both";
		div.style.zIndex = "6";
		div.style.display = "none";
		div.id = 'chat_status';
		document.body.appendChild(div);
	
	}else{
	
		var coll = div.getElementsByTagName("div");
		var i = 0;
		
	
			
		for(i = 0; i < coll.length; i++){
			elem = coll[i];
			if(elem.id == "sender_" + sender){
				is_found = true;
			}
		}
	}
		
	if( !is_found ){
	
		elem = document.createElement("div");
		elem.id = "sender_" + sender;
//		elem.innerHTML = "<img src='" + sender_infos[3] + "' /><br />" + sender_infos[2] + "<br /> A dit: " + sender_infos[1];
		elem.innerHTML = "Interlocuteur: <b>" + sender_infos[2] + "</b> A dit: " + sender_infos[1];
		elem.innerHTML = elem.innerHTML + "<a href=\"JAVASCRIPT: show_private_chat(" + sender + ", '" + sender_infos[3]+ "','" + sender_infos[4] + "');\">Discuter</a>";
		div.appendChild(elem);
	}


}

function show_chat_status(){
	var div = document.getElementById('chat_status');
	div.style.display = "block";
}

var pm_windows = new Array();

function show_private_chat(sender, token, receiver){
  var url = "/page_module.php?tZ=1328756736&module=chat&page=private_messaging&sender=" + sender + "&token=" + token + "&receiver=" + receiver;
	
  pm_windows[sender] = window.open(url, "pc_" + sender, "location = no, menubar = no, resizable = yes, scrollbars = yes, status = no, toolbar = no, width=450, height=450");
   pm_windows[sender].id = sender;
}

function close_private_chat(){
	pm_windows[this.id] = null;
}

var room_windows = new Array();
function show_room_chat(id_chat_room){
	var url = "/page_module.php?tZ=1328756736&module=chat&page=room_messaging&id_chat_room=" + id_chat_room;

	room_windows[id_chat_room] = window.open(url, "rc_" + id_chat_room, "location = no, menubar = no, resizable = yes, scrollbars = yes, status = no, toolbar = no, width=920, height=660");

}


function close_room_chat(id_chat_room){
	room_windows[id_chat_room] = null;
	try{
		var url = "/data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&module=chat&a=chat_add_message&chat_action_type=QUIT&id_chat_room=" + id_chat_room;
	
		var myAjax = new Ajax(url, "POST");
		myAjax.send();
	}catch(error){
		//ERROR TROWN IN CASE OF PREMATURE WINDOW CLOSING
	alert("Error on close_room_chat: " + error);
	}
}


function change_status(status, obj){
	var url = "data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&module=chat&a=chat_set_status&status=" + status;

	var myAjax = new Ajax(url, "POST");
	myAjax.send();

	var active_status = document.getElementById("active_status");
	
	if( typeof(active_status) == "object" && active_status != null){
	
		if( typeof(obj) == "object" )
			active_status.innerHTML = obj.innerHTML;
		else
			active_status.innerHTML = obj;
	}
}//JAVASCRIPT DOCUMENT: motd.js.php
function edit_motd(control){

	if( control.childNodes[0].tagName!="INPUT" ){

		var itext = document.createElement("INPUT");
		itext.type = "text";
		itext.id = "motd_control";
		itext.onchange = save_motd;
		var original_value = control.innerHTML;
		value = encode_macro(original_value);
		value = spell_uncheck(value);
		itext.value = value;
		
		itext.onkeypress = function(event){kp_motd(event,original_value)};


		itext.size = 64;
		itext.maxLength = 64;

		itext.style.backgroundColor = "#FFFFFF";
		itext.style.color = "#000000";
		control.innerHTML = "";
		control.appendChild(itext);
		itext.focus();
	}			
}

function kp_motd(evObj, original_value){
	if(window.event){
		var keyCode = window.event.keyCode;
	}else
		var keyCode = evObj.keyCode;
		
	if(keyCode == 27){
		var itext = document.getElementById('motd_control');
		var pn = itext.parentNode;
	
		pn.removeChild(itext);
		pn.innerHTML = original_value;
	}
}

function save_motd(){
	var itext = document.getElementById('motd_control');
	if(itext.value.length > 0 ){
		var url = "http://www.centrevillemagog.ca/data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&module=security&a=user_edit&motd=" + itext.value;
		var myAjax = new Ajax(url, "POST");
		myAjax.onComplete = changed_motd;
		myAjax.send();
	}else{

		 itext.value = "Laisser votre message en cliquant";
		 changed_motd(null, null);
	}
}

function changed_motd(responseText, responseXML){

	var itext = document.getElementById('motd_control');
	
	var value = itext.value;
	var pn = itext.parentNode;
	
	pn.removeChild(itext);
	
	value = spell_check(value);
	value = decode_macro(value);
	//value = wordwrap(value, 64, "<br />");
	pn.innerHTML = value;
	
}//JAVASCRIPT DOCUMENT: callout.js.php


var callouts = new Array();
	
function show_callout( html_content, curr_obj, offset_x, offset_y, width, height, timer, mouse_based, evObj, prevent_opening, drawType ){
	
	var in_height = height
	var i;
	var present_callout = false;
	if(typeof(mouse_based) != "boolean")
		mouse_based = false;

	if(typeof(prevent_opening) != "boolean")
		prevent_opening = true;

	if( typeof(drawType) != "string" )
		drawType = "BL";
	
	if( typeof(curr_obj) == "string" ){
		curr_obj = document.getElementById(curr_obj);
	}
	
	for(i = 0; i < callouts.length; i++){
		if(curr_obj == callouts[i][0]){
			present_callout = true;
		}
	}
	
	var style_set = new Array();
	
	//UPPER ROW
	style_set[0] = " style='background-image: url(/images/design/core/callout_tl.png);width:40px;height:32px;' ";
	style_set[1] = " style='background-image: url(/images/design/core/callout_top.png);height:32px;' ";
	style_set[2] = " style='background-image: url(/images/design/core/callout_tr.png);width:41px;height:32px;' ";

	//MIDDLE ROW	
	style_set[3] = " style='background-image: url(/images/design/core/callout_left.png);width:40px;' ";
	style_set[4] = " style='background-color: #FFFFFF;' ";
	style_set[5] = " style='background-image: url(/images/design/core/callout_right.png);width:41px;vertical-align:top;' ";

	//LOWER ROW
	style_set[6] = " style='background-image: url(/images/design/core/callout_bl.png);width:40px;height:34px;' ";
	style_set[7] = " style='background-image: url(/images/design/core/callout_bottom.png);height:34px;' ";
	style_set[8] = " style='background-image: url(/images/design/core/callout_br.png);width:41px;height:34px;' ";

	
	if(drawType.toUpperCase() == "BL"){
		style_set[6] = " style='background-image: url(/images/design/core/callout_bl_selected.png);width:40px;height:34px;' ";
	}else if(drawType.toUpperCase() == "TL"){
		style_set[0] = " style='background-image: url(/images/design/core/callout_tl_selected.png);width:40px;height:32px;' ";
	}else if(drawType.toUpperCase() == "TR"){
		style_set[2] = " style='background-image: url(/images/design/core/callout_tr_selected.png);width:41px;height:32px;' ";
	}else if(drawType.toUpperCase() == "BR"){
		style_set[8] = " style='background-image: url(/images/design/core/callout_br_selected.png);width:41px;height:34px;' ";
	}
	


	var closing_control = "<span style=\"position:relative;left:10px;cursor:pointer;font-weight:bold;font-size:1.3em; color:#C00000;\" onclick=\"JAVASCRIPT: close_this_callout(this);\" title=\"Fermer cette fen&ecirc;tre\">X</span>"


	var begin_html = "<table style=\"width:"+width+"px; height:"+(height + 34)+"px;\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
	begin_html += "<tr><td " + style_set[0] + "><!-- TL --></td>";
	begin_html += "<td " + style_set[1] + "><!-- TC --></td>";
	begin_html += "<td " + style_set[2] + "></td></tr>";

	begin_html += "<tr><td " + style_set[3] + "><!-- TL --></td>";
	begin_html += "<td valign=\"top\" " + style_set[4] + "><div style=\"overflow:hidden;height:" + (in_height-(32+34)+ 34) + "px;\">";
	
	var end_html = "</div></td><td " + style_set[5] + "><!-- TR -->" + closing_control + "</td></tr><tr>";
	end_html += "<td " + style_set[6] + "><!-- TL --></td>";
	end_html += "<td " + style_set[7] + "><!-- TC --></td>";
	end_html += "<td " + style_set[8] + "><!-- TR --></td></tr></table>";
	
	if( !present_callout){
	
		var callout_control = document.createElement("DIV");
		callout_control.className = "call_out";
		
//-		var corner = document.createElement("IMG");
//-		corner.src = "/images/design/callout_coin_droite.png";
//-		corner.className = "corner_image";

		var callout_container = document.createElement("DIV");
		callout_container.className = "call_out_container";

		if( typeof(width) != "undefined")
			callout_container.style.width = parseInt(width) + "px";
			
		if( typeof(height) != "undefined")
			callout_container.style.height = parseInt(height) + "px";
		
		if( typeof(html_content) == "string")
			callout_container.innerHTML = begin_html + html_content + end_html;
		else{
			if( typeof(html_content) == "object"){
				callout_container.innerHTML = begin_html + html_content.innerHTML + end_html;
			}
		}
		
		callout_control.style.zIndex = 50;

//-		callout_control.appendChild(corner);
		callout_control.appendChild(callout_container);
	
		if( typeof(curr_obj) != "undefined" ){
			var pos = findPos(curr_obj);
	
			callout_control.style.position = "absolute";
	
			if(mouse_based){
			
					if(window.event){
	
//						var callout_x = event.clientX + document.documentElement.scrollLeft + offset_x + "px";
//						var callout_y = (event.clientY - (height) - 50) + document.documentElement.scrollTop + offset_y + "px";

						var rX = event.clientX + document.documentElement.scrollLeft;
						var rY = event.clientY + document.documentElement.scrollTop;
						var tmp = callout_pos(rX, rY, offset_x, offset_y, width, height, drawType);
						callout_x = tmp[0];
						callout_y = tmp[1];
					}else{
	
						if(typeof(evObj) != "undefined"){
	
//							var callout_x = evObj.pageX + offset_x + "px";
//							var callout_y = evObj.pageY - (height) - 50 + offset_y + "px";
							var rX = evObj.pageX;
							var rY = evObj.pageY;
							var tmp = callout_pos(rX, rY, offset_x, offset_y, width, height, drawType);
							callout_x = tmp[0];
							callout_y = tmp[1];

						}
					}
	
			}else{
	
//				var callout_x = (parseInt(pos[0]) + offset_x ) + "px";
//				var callout_y = (parseInt(pos[1]) + offset_y - (height) - 50 ) + "px";

				var rX = parseInt(pos[0]);
				var rY = parseInt(pos[1]);
				var tmp = callout_pos(rX, rY, offset_x, offset_y, width, height, drawType);
				callout_x = tmp[0];
				callout_y = tmp[1];
				
			}
	
			callout_control.style.left = callout_x + "px";
			callout_control.style.top = callout_y + "px";
		}
		
		
		document.body.appendChild(callout_control);
		callouts[callouts.length] = new Array(curr_obj, callout_control);
	
		if(typeof(timer) != "undefined"){
			if(timer > 0)
				window.setTimeout( add_close_callout(curr_obj, prevent_opening) , timer * 1000);
		}
	}
	
	return curr_obj;
}

function add_close_callout(curr_obj, prevent_opening){

	return function(){ close_callout(curr_obj, prevent_opening); };
}

function add_remove_callout( index ){

	return function(){ remove_callout( index ); };
}

function remove_callout(index){
	callouts.splice(index,1);
}

function callout_pos(x, y, offsetX, offsetY, width, height, drawType){
	
	var callout_x, callout_y;
	drawType = drawType.toUpperCase();
	var top_type = drawType.substring(0,1);
	var left_type = drawType.substring(1,2);
	
	// BOTTOM OR TOP	
	if(top_type == "B"){
		callout_y = y - (height) - 50 + offsetY;
	}else{
		callout_y = y + offsetY;
	}

	// LEFT OR RIGHT
	if(left_type == "L"){
		callout_x = x + offsetX;	
	}else{
		callout_x = x - width - offsetX;
	}

	return new Array(callout_x, callout_y);	

}

function close_callout(curr_obj, prevent_opening){
	var i;
	if(typeof(curr_obj) == "object" && curr_obj != null){
		for(i = 0; i < callouts.length; i++){
			if(curr_obj == callouts[i][0]){
				var tmp = callouts[i][1];
				clear_object_content(tmp);
				tmp.parentNode.removeChild(tmp);
				
				if(prevent_opening){
					//WONT APPEAR FOR 30 SECONDS
					window.setTimeout( add_remove_callout( i ) , 30000);
				}else{
					remove_callout(i)
				}
				
				break;
			}
		}
	}
}

function close_this_callout(curr_obj, prevent_opening){
	var curr_obj_pointer = curr_obj;
	var max_iteration = 15;
	var iteration = 0;
	var i;
	
	while(typeof(curr_obj_pointer.parentNode) == "object"){
		
		if(curr_obj_pointer.className == "call_out"){
		
			for(i = 0; i < callouts.length; i++){
				if(curr_obj_pointer == callouts[i][1]){
					var tmp = callouts[i][1];
					clear_object_content(tmp);
					tmp.parentNode.removeChild(tmp);
					
					if(prevent_opening){
						//WONT APPEAR FOR 30 SECONDS
						window.setTimeout( add_remove_callout( i ) , 30000);
					}else{
						remove_callout(i)
					}
					
					i = callouts.length + 1;
					iteration = max_iteration + 1;
				}
			}
		
			
		}else
			curr_obj_pointer = curr_obj_pointer.parentNode;
		
		if(iteration < max_iteration)
			iteration++;
		else
			break;
	}
}

//-- CONTENT HELER
function callout_helper_content_error(title, message, errors){
	var error_message = "<p style=\"color:#C00000;text-transform:uppercase;margin:0px;font-size:1.3em;padding-bottom:5px;\">";
	error_message +=  title + "</p>";
	
	error_message += "<p>" + message + " </p>";
	error_message +=  "<p style=\"color:" + ERROR_CONTROL_BORDERCOLOR + ";\">" + errors.join("<br />") + "</p>";
	
	return error_message;
}

function callout_helper_content_confirm(title, message, buttons){

	var error_message = "<p style=\"color:#C00000;text-transform:uppercase;margin:0px;font-size:1.3em;padding-bottom:5px;\">";
	error_message +=  title + "</p>";
	
	error_message += "<p>" + message + " </p>";
	error_message +=  "<p>" + buttons.join(" ") + "</p>";
	
	return error_message;
}

function callout_helper_content_info(title, message){

	var error_message = "<p style=\"color:#C00000;text-transform:uppercase;margin:0px;font-size:1.3em;padding-bottom:5px;\">";
	error_message +=  title + "</p>";

	error_message += "<p>" + message + " </p>";

	return error_message;
}

//------------------------------------- CUSTOM_MENU
//-------------------------------------------------


var nbMenu = 6;
var menu_height = 31;
var v_duration = 400;
var backgroundColors = new Array("#FFFFFF", "#4CA9BB");
var backgroundOpacity = new Array(0.30, 0.80);
var slider_is_open = false;

var slider_timer;
var drop_menu_timer;
var menu_timer;

var time_drop_menu = 200;
var time_bg =  1000;

function open_menu_slider(){
	if(!slider_is_open){
		myFXbg = new Fx.Style('menu_slider', 'background-color', {duration: v_duration, wait:false });
		myFXbg.start(backgroundColors[0], backgroundColors[1]);
		myFXbg = new Fx.Style('menu_slider', 'opacity', {duration: v_duration, wait:false });
		myFXbg.start(backgroundOpacity[0], backgroundOpacity[1]);
		slider_is_open = true;
	}
}

function close_menu_slider(){
	if(slider_is_open){
		myFXbg = new Fx.Style('menu_slider', 'background-color', {duration: v_duration });
		myFXbg.start(backgroundColors[1], backgroundColors[0]);
		myFXbg = new Fx.Style('menu_slider', 'opacity', {duration: v_duration });
		myFXbg.start(backgroundOpacity[1], backgroundOpacity[0]);
		slider_is_open = false;
	}
}
//-- MENU_INIT() CALLED BY DEFAULT, CHANGE NAME TO PREVENT OPERATION
function menu_init01(){
	var i, myMenuItem, myContentItem, myDropMenuItem;
	
	for(i = 0; i < nbMenu; i++){
	
		myMenuItem = $('menu_item_' + (i+1) );
		myContentItem = $('menu_content_' + (i+1) );
		
		
		myDropMenuItem = $('drop_menu_item_' + (i+1) );
		var pos = findPos(myMenuItem);
		
		
		
		if( myDropMenuItem != null){
		
			myDropMenuItem = myDropMenuItem.cloneNode( true );
			document.body.appendChild(myDropMenuItem);
			
			myDropMenuItem.addEvent('mouseenter', add_mouseenter_dm(myDropMenuItem));
			myDropMenuItem.addEvent('mouseleave', add_mouseleave_dm(myMenuItem, myContentItem, myDropMenuItem));
		
		}
		
		myMenuItem.addEvent('mouseleave', add_mouseleave(myMenuItem, myContentItem, myDropMenuItem, pos));
		myMenuItem.addEvent('mouseenter', add_mouseenter(myMenuItem, myContentItem, myDropMenuItem, pos));
		

		
	}
}

function add_mouseenter(menuItem, contentItem, dropMenuItem, menuPos){
	return function(){
	
			if(typeof(slider_timer) != "undefined" && slider_timer != null && slider_timer != "null")
				window.clearTimeout(slider_timer);

			var myFX = new Fx.Style(contentItem, 'opacity', {duration: v_duration, wait:false });
			myFX.start(0,1);

			if(typeof(dropMenuItem) == "object" && dropMenuItem != null && dropMenuItem != "null"){

				dropMenuItem.style.position = "absolute";
				dropMenuItem.style.zIndex = 90;
				dropMenuItem.style.left = (menuPos[0] - 2) + "px";
				dropMenuItem.style.top = (menuPos[1] + menu_height + 7  ) + "px";
				
				var myFX = new Fx.Style(dropMenuItem, 'opacity', {duration: v_duration, wait:false });
				myFX.set(0);
				dropMenuItem.style.display = "block";
				myFX.start(0,1);
			}
			
				
			if(!slider_is_open)
				open_menu_slider();
		};
}

function add_mouseleave(menuItem, contentItem, dropMenuItem, menuPos){
	return function(){

			if(typeof(dropMenuItem) == "object" && dropMenuItem != null && dropMenuItem != "null"){
				drop_menu_timer = window.setTimeout(function(){close_drop_menu(dropMenuItem)}, time_drop_menu);
				
				menu_timer = window.setTimeout(function(){
					var myFX = new Fx.Style(contentItem, 'opacity', {duration: v_duration, wait:false });
					myFX.start(1,0);
				}, time_drop_menu);
			}else{
				var myFX = new Fx.Style(contentItem, 'opacity', {duration: v_duration, wait:false });
				myFX.start(1,0);
				
			}
			
			slider_timer = window.setTimeout("close_menu_slider()", time_bg);
			
		};
}

function add_mouseenter_dm(dropMenuItem){
	return function(){
		window.clearTimeout(slider_timer);	
		window.clearTimeout(drop_menu_timer);
		window.clearTimeout(menu_timer);
	};
}

function add_mouseleave_dm(menuItem, contentItem, dropMenuItem){
	return function(){
	
		var myFX = new Fx.Style(contentItem, 'opacity', {duration: v_duration, wait:false });
		myFX.start(1,0);
		
		close_drop_menu(dropMenuItem);
		slider_timer = window.setTimeout("close_menu_slider()", time_bg);
		
	};
}

function close_drop_menu(dropMenuItem){
	var myFX = new Fx.Style(dropMenuItem, 'opacity', {duration: v_duration, wait:false });
	myFX.start(1,0);
}

//JAVASCRIPT DOCUMENT: text_and_array.js.php

function in_array(needle, haystack ){

	var i = 0;
	
	for(i = 0; i < haystack.length; i++){
	
		if( haystack[i] == needle ){
		
			return true;
		}
	}

	return false;
}

function check_keystroke(obj, event_object, startup){

	if( typeof(text_color_matrix) == "undefined" )
		var color = new Array("#77E818", "#C0E818","#E3E818", "#E88E18", "#E82118");
	else
		var color = text_color_matrix;

	if(!startup){
	
	
		if(!event_object )
			event_object = Window.event;

		var keyCode = event_object.keyCode;
		var shiftKey = event_object.shiftKey;
		
		var limiter_control_name = obj.getAttribute("limiter_control");
		var control_limit = parseInt( obj.getAttribute("limit") );
		
		var limiter_control = document.getElementById( limiter_control_name );
		var control_content = obj.value.toString();
		var char_count = control_content.length;
				
		if( in_array(keyCode, unicode_add_chars) )
			char_count++;
	
		if( in_array(keyCode, unicode_rem_chars) ){
			char_count--;

		}

		var resulting_chars = control_limit - char_count;

		if(resulting_chars < 0){
			obj.value = control_content.substr( 0, control_limit);
			char_count = control_limit;
		}
			
		var perc = Math.round(100 * char_count / control_limit);
		var color_level = color[ Math.round( ( (perc / 100) * color.length) - 1 ) ];
		if(limiter_control){
			limiter_control.style.backgroundColor = color_level;																				
			limiter_control.style.width = perc + "%";
		}
		
		if(char_count <= control_limit ){
			return true;
			
		}else{
		
			if( in_array(keyCode, unicode_rem_chars) ){
				return true;
				
			}else{
			
				if( in_array(keyCode, unicode_add_chars) ){
	
					return false;
				}else{
					return true;
				}
				
			}
		}
		
	}else{
		//STARTUP INIT
		var limiter_control_name = obj.getAttribute("limiter_control");
		var control_limit = parseInt( obj.getAttribute("limit") );
		
		var limiter_control = document.getElementById( limiter_control_name );
		var control_content = obj.value.toString();
		var char_count = control_content.length;
		var resulting_chars = control_limit - char_count;
		
		var perc = Math.round(100 * char_count / control_limit);
		var color_level = color[ Math.round( ( (perc / 100) * color.length) - 1 ) ];

		limiter_control.style.backgroundColor = color_level;																				
		limiter_control.style.width = perc + "%";
		
		return false;
	
	}	
}

function wordwrap(text, length, separator){
	var actual_length = text.length;
	var output = new Array();
	
	if(actual_length > length){
		var pointer = 0;
		while(pointer < actual_length){
			output[output.length] = text.substr(pointer,length)
			pointer = pointer + length;
		}
		
		return output.join(separator);
	}else
		return text;
}

function spell_check(text){
	
	text = text.replace( /\</g , "&lt;" );
	text = text.replace( /\>/g , "&gt;" );
		
	return text;
}

function spell_uncheck(text){
	
	text = text.replace( /&lt;/g , "<" );
	text = text.replace( /&gt;/g , ">" );
		
	return text;
}


function encode_macro( text){

	//BOLD
	text = text.replace( /\<(b|B)\>/g , "[b]" );
	text = text.replace( /\<\/(b|B)\>/g , "[/b]" );

	//ITALIC
	text = text.replace( /\<(i|I)\>/g , "[i]" );
	text = text.replace( /\<\/(i|I)\>/g , "[/i]" );

	//UNDELINE
	text = text.replace( /\<(u|U)\>/g , "[u]" );
	text = text.replace( /\<\/(u|U)\>/g , "[/u]" );

	return text;
}

function decode_macro( text ){

	//BOLD
	text = text.replace( /\[(b|B)\]/g , "<b>" );
	text = text.replace( /\[\/(b|B)\]/g , "</b>" );

	//ITALIC
	text = text.replace( /\[(i|I)\]/g , "<i>" );
	text = text.replace( /\[\/(i|I)\]/g , "</i>" );

	//UNDELINE
	text = text.replace( /\[(u|U)\]/g , "<u>" );
	text = text.replace( /\[\/(u|U)\]/g , "</u>" );

	return text;
}
//JAVASCRIPT DOCUMENT: control.js.php

//--------------------------------- GENERAL TOOLS

function list_over(control){
	control.className = "line_over";
}

function list_out(control){
	control.className = "";
}

var last_page = new Array();

function show_page(id_page, className, page_prefix){

	if(typeof(className) == "undefined" || className == null || className == '')
		className = "selected";

	if(typeof(page_prefix) == "undefined" || page_prefix == null || page_prefix == '')
		page_prefix = "page_";

	if( ! last_page[page_prefix])
		last_page[page_prefix] = 1;

	if(id_page != last_page[page_prefix]){

		var oldcontrol = document.getElementById( page_prefix + last_page[page_prefix]);
		if( oldcontrol )
			oldcontrol.style.display = "none";

		var tab = document.getElementById("tab_" + page_prefix + last_page[page_prefix]);
		
		if(tab != null)
			tab.className = "";

		
		var control = document.getElementById( page_prefix + id_page);
		
		if(control)
			control.style.display = "block";

		var tab = document.getElementById("tab_" + page_prefix + id_page);
		
		if(tab != null)
			tab.className = className;

		

		last_page[page_prefix] = id_page;
		
		//REMOVE ANY PAGE INTEGRATE
		control = document.getElementById("page_integrate");
		
		if(control)
		  control.style.display = "none";
		
	}
}

function validate_email(elementValue){
  var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
  return emailPattern.test(elementValue);
}

function password_strength(pass){
	var nb_chars = pass.length;
		
	var strength = 0;
	var error = "";
	
	if(nb_chars >= 6){
		strength++;
	}
	
	if(pass.match(/[a-z]/)){
	  strength++;
	}
	
	if(pass.match(/[A-Z]/)){
	  strength++;
	}
	
	if(pass.match(/\d/)){
	  strength++;
	}    

	if(pass.match(/\W/)){
	  strength++;
	} 
	
	return strength;
}

function set_ro(currObj){
			
	var name = currObj.name;
	var form = currObj.form;
	var coll = form[name];
	var i;
	
	if( currObj.tagName.toUpperCase() == "INPUT" ){
	
		if( currObj.type.toUpperCase() == "CHECKBOX" || currObj.type.toUpperCase() == "RADIO" ){
			for( i = 0; i < coll.length; i++ )
				coll[i].checked = coll[i].defaultChecked;
		}else if(currObj.type.toUpperCase() == "TEXT"){
			currObj.value = currObj.defaultValue;
		}else
			alert("This control type(" + currObj.type.toUpperCase() + ") must be protected with the 'READONLY' attribute");
			
	}else if( currObj.tagName.toUpperCase() == "SELECT"){
		coll = currObj.options;
		
		if(currObj.multiple){
			for( i = 0; i < coll.length; i++ )
				coll[i].selected = coll[i].defaultSelected;
			
		}else{
			for( i = 0; i < coll.length; i++ ){
				if(coll[i].defaultSelected){
					currObj.selectedIndex = i;
					break;
				}
			}
		}
	}else if( currObj.tagName.toUpperCase() == "TEXTAREA"){
		currObj.value = currObj.defaultValue;
	}
	
	return false;
}

//--------------------------------- STEP AND PAGE LOADER

	var current_step = false;
	
	function entry_step( step_id, old_step_id, remote_variable){
	
		var control = document.getElementById('wizzard_page_' + old_step_id);
		var indicator = document.getElementById('step_indicator');
		
		if(remote_variable != null && remote_variable != "null"){
	    	eval( "var remote_id = " + remote_variable +  ";" );
			
			if(remote_id == 0 || remote_id == "0" ||remote_id == "" || remote_id == null || remote_id == "null" || remote_id == false )
				return;
	
		}
	
		if(control){
	
			control.style.display = "none";
	
			if(indicator)
				indicator.rows.item(old_step_id -1).className = "";
	
		}else{
	
			control = document.getElementById('wizzard_page_' + current_step);
	
			if(control){
	
				control.style.display = "none";
	
				if(indicator)
					indicator.rows.item(current_step -1).className = "";
				
			}
			
		}
	
		control = document.getElementById('wizzard_page_' + step_id);
		
		if(control){
	
			control.style.display = "block";
	
			if(indicator)
				indicator.rows.item(step_id -1).className = "step_selected";
		}
	
		current_step = step_id;
	
		
		
	}

	var page_loading_opened = 0;

	function open_loader( message ){

		if( page_loading_opened == 0 ){
			var my_loader = get_loader( message );
			page_loading_opened = bring_center_form(document.body, my_loader, null, false, 100, false, true, true);
		}
	}

	function close_loader(){

		if(page_loading_opened != 0){
			close_sub_form(page_loading_opened);
			page_loading_opened = 0;
		}
	}
	
	function get_loader( message ){

		if(empty(message))
		  message = "Chargement";

		var main = document.createElement("div");
		main.style.width = "300px";
		main.style.height = "100px";
		
		var main_table = document.createElement("table");
		main.appendChild( main_table );
		main_table.style.width = "100%";
		main_table.style.height = "100%";
		
		main_table.style.backgroundColor = "transparent";
		main_table.style.backgroundImage = "url(/images/design/main_loader_box.png)";
		main_table.style.backgroundRepeat = "no-repeat";
		var tr = main_table.insertRow(0);
		var td = tr.insertCell(0);
		td.vAlign = "bottom";
		td.align = "center";
		
		var place_table = document.createElement("table");
		td.appendChild( place_table );
		place_table.style.marginBottom = "20px";
		
		
		
		tr = place_table.insertRow(0);
		td = tr.insertCell(0);
		td.className = "main_loader_text";
		td.innerHTML = message;
		
		td = tr.insertCell(1);
		td.innerHTML = "<img src='/images/design/main_loader.gif' title='Chargement/Loading' />";
		
	
		return main;
	}

	function page_integrate(responseText, responseXML, page_select){

		var html_content = responseXML.getElementsByTagName("html_content")[0].firstChild.nodeValue;
		var basic = document.getElementById('page_integrate');
		
		if( basic != null && basic != "null" ){

			basic.innerHTML = html_content;
			basic.style.display = "block";
		}else{

			alert( "Page integration lack 'page_integrate' div." );
		}
		
		controls_ergo();
	}

	function email_scrambler(){
	/***********************************************
	* Easy Email Scrambler script- © Dynamic Drive (www.dynamicdrive.com)
	* This notice MUST stay intact for legal use
	* Visit http://www.dynamicdrive.com/ for full source code
	***********************************************/
	
	var maildivider="[a!t]" //enter divider you use to divide your email address strings
	
	for (i=0; i<=(document.links.length-1); i++){
	if (document.links[i].href.indexOf(maildivider)!=-1)
	document.links[i].href=document.links[i].href.split(maildivider)[0]+"@"+document.links[i].href.split(maildivider)[1]
	}
	}
//--------------------------------- SEARCHBOX TOOLS

function search_input_out(obj, text, color){
			
	if(obj.value == ""){
		obj.style.color = color;
		obj.value = text;
	}

}

function search_input_in(obj, text, color){

	if(obj.value == text){
		obj.style.color = color;
		obj.value = "";
	}
}

function search_form_submit(form_name, search_object_name, text){
	var form_object = document.getElementById(form_name);
	
	var search_object = document.getElementById(search_object_name);
	if( search_object.value == text){
		search_object.value = "";
	}
	
	form_object.submit();

}

function search_input_kp(evObj, obj, form_name, search_object_name, text  ){

	if(evObj)
		var keyCode = evObj.keyCode;
	else
		var keyCode = event.keyCode;

	if(keyCode == 13){
		search_form_submit(form_name, search_object_name, text);
	}
}

//--------------------------------- CALENDAR TOOLS
function show_calendar(currObj, inputControlName, format ){

	var tmp_date, tmp_time;
	var pos = findPos(currObj);

	if( typeof(format) == "undefined" || format == null || format == "null"){
		format = "date";
	}else{
		if(format != "date" && format != "datetime")
			format = "date";
	}

	var ownerForm = recursive_form_find(currObj);
	var inputControl = ownerForm[inputControlName];
	set_date = inputControl.value

	if(set_date != null && set_date.length >= 10){
		var tmp = set_date.split(" ");
		if(tmp[0])
			var tmp_date = tmp[0].split("-");
		if(tmp[1])
			var tmp_time = tmp[1].split(":");
		else
			var tmp_time = new Array(0,0,0);
			
		var dateObj = new Date(tmp_date[0], (tmp_date[1] - 1), tmp_date[2], tmp_time[0], tmp_time[1], tmp_time[2]);
	}else
		var dateObj = new Date();

	//-- BUILD CALENDAR CONTAINER
	var myDiv = document.createElement('DIV');
	
	//-- NAME FOR CLEARING OF CONTROLS
	myDiv.setAttribute('name', "CALENDAR_CONTROL");
	myDiv.name = "CALENDAR_CONTROL";

	//-- STORE DATE OBJECT WITHIN MAIN DIV ATTRIBUTE: DATE
	myDiv.setAttribute('date', dateObj.toString() );
	myDiv.date = dateObj.toString();
	
	myDiv.setAttribute('setdate', dateObj.toString() );
	myDiv.setdate = dateObj.toString();
	
	myDiv.setAttribute('linked_control', inputControlName);
	myDiv.linked_control = inputControlName;

	myDiv.setAttribute('format', format);
	myDiv.format = format;

	//-- POSITIONNING
	myDiv.style.zIndex = 100;
	myDiv.style.position = "absolute";
	myDiv.style.left = pos[0] + 30 + "px";
	myDiv.style.top = pos[1] + 30 + "px";
	
	//-- DESIGN, SUBJECT TO MIGRATE IN CSS SOON
	myDiv.className = "calendar_control";
	
	myDiv.onclick = cancel_bubble;
	//-- INTEGRATE INNERHTML
	myDiv.innerHTML = calendar_content( dateObj, dateObj );


	document.body.appendChild(myDiv);
}

//var day_of_week = new Array('D', 'L', 'M', 'M', 'J', 'V', 'S');
var day_of_week = new Array('Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam');
var month_of_year = new Array('Janvier','F&eacute;vrier', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Ao&ucirc;t', 'Septembre', 'Octobre', 'Novembre', 'D&eacute;cembre' );

var nb_days_of_month = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function calendar_content(dateObj, setdateObj){
	var content = "<table cellpadding='0' cellspacing='0' class='calender_main_table'>";
	
	if( dateObj == null)
		dateObj = new Date();
	
	var month = dateObj.getMonth();
	var year =  dateObj.getFullYear();
	
	if(setdateObj){
		var seldate = setdateObj.getDate();
		var selmonth = setdateObj.getMonth();
		var selyear = setdateObj.getFullYear();
	}else
		var seldate = dateObj.getDate();
		
	var origin_date = new Date( dateObj.toString() );
	origin_date.setDate(1);
	var day = origin_date.getDay();

	var begin_year = new Date( origin_date.toString() );
	begin_year.setMonth(0);

	var begin_ts = begin_year.getTime() / 1000;
	var month_ts = origin_date.getTime() / 1000;
	var to_days = Math.floor( (month_ts - begin_ts) / 86400) + 2;
	
	var begin_week = Math.floor( (begin_year.getDay() + to_days) / 7) + 1;

	var is_leap = calendar_is_leap_year( year );
	if( is_leap ){
		var nb_days = 366;
		
		if(month != 1 )
			var nb_days_in_month = nb_days_of_month[month];
		else
			var nb_days_in_month = nb_days_of_month[month] + 1;
	}else{
		var nb_days = 365;
		var nb_days_in_month = nb_days_of_month[month];
	}
	
	if(month > 0){
		var nb_days_prev_month = nb_days_of_month[month - 1];
		if(month == 2 && is_leap)
			nb_days_prev_month + 1
	}else
		var nb_days_prev_month = nb_days_of_month[11];
	
	var i, j;

	content += "<tr>";
		content += "<th class='help' onmouseover=\"JAVASCRIPT: calendar_over(this);\" onmouseout=\"JAVASCRIPT: calendar_out(this);\" onclick=\"JAVASCRIPT: calendar_help();\" align=\"center\" title=\"Ce calendrier Ã  Ã©tÃ© conÃ§u et dÃ©velopper par Oxtal Solutions Â©2008-2009\">";
			content += " <img src=\"/images/calender/btn_calender_help.png\" alt=\"Aide\" /> ";
		content += "</th>";
		
		content += "<th colspan='6'  class='today' align=\"center\"><div>";
			content += month_of_year[ month ];
			content += " " + year;
		content += "</div></th>";
		
		content += "<th class='close' onmouseover=\"JAVASCRIPT: calendar_over(this);\" onmouseout=\"JAVASCRIPT: calendar_out(this);\" onclick=\"JAVASCRIPT: calendar_close();\"  align=\"center\">";
			content += " <img src=\"/images/calender/btn_calender_close.png\" alt=\"Fermer\" /> ";
		content += "</th>";

	content += "</tr>";
	
	content += "<tr>";
		content += "<td class='prev_year' onmouseover=\"JAVASCRIPT: calendar_over(this);\" onmouseout=\"JAVASCRIPT: calendar_out(this);\" onclick=\"JAVASCRIPT: calendar_redraw(this, -1, 'y');\" >";
			content += " ";
		content += "</td>";
		content += "<td class='prev_month' onmouseover=\"JAVASCRIPT: calendar_over(this);\" onmouseout=\"JAVASCRIPT: calendar_out(this);\" onclick=\"JAVASCRIPT: calendar_redraw(this, -1, 'm');\">";
			content += "  ";
		content += "</td>";
	
	content += "<td colspan='4' class='today' align=\"center\" onclick=\"JAVASCRIPT: calendar_go_today(this);\" >";
	content += "Aujourd'hui";
	content += "</td>";
		content += "<td  class='next_month' onmouseover=\"JAVASCRIPT: calendar_over(this);\" onmouseout=\"JAVASCRIPT: calendar_out(this);\" onclick=\"JAVASCRIPT: calendar_redraw(this, 1, 'm');\">";
		content += "  ";
		content += "</td>";
		content += "<td  class='next_year' onmouseover=\"JAVASCRIPT: calendar_over(this);\" onmouseout=\"JAVASCRIPT: calendar_out(this);\" onclick=\"JAVASCRIPT: calendar_redraw(this, 1, 'y');\">";
		content += "  ";
		content += "</td>";

	content += "</tr>";

	content += "<tr><td align=\"center\" class=\"day_header week_num\">sem</td>";

	for(i = 0; i < day_of_week.length; i++){
	
		if(i == 0 || i == 6)
			content += "<td class='day_header week-end'>" + day_of_week[i] + "</td>";
		else
			content += "<td class='day_header'>" + day_of_week[i] + "</td>";
	}

	content += "</tr>";

	content += "<tr> <td align=\"center\" class=\"week_num\">" + begin_week + "</td>";

	if(day > 0){
		
		for(i = 0; i < day; i++){
			content+= "<td class='prev_day' onmouseover=\"JAVASCRIPT: calendar_over(this);\" onmouseout=\"JAVASCRIPT: calendar_out(this);\" onclick=\"calendar_redraw(this, -1, 'm');\" title=\"" + (to_days + (i - day)) + "\">";
			content+= (nb_days_prev_month - (day - i ) + 1) + "</td>";
		}
	}
		
	var is_close = false;
	var sel = "";

	for(i = 0; i < nb_days_in_month; i++){
		
		if( (i + 1) == seldate && month == selmonth && year == selyear )
			sel = " selected ";
		else
			sel = "";
			
		if( (i + day ) % 7 == 0 && i + day != 0){

			content+= "</tr><tr> <td align=\"center\" class=\"week_num\">" + (begin_week + Math.floor( (i + day) / 7) )+ "</td>";
			content+= "<td class='day" + sel + "' onmouseover=\"JAVASCRIPT: calendar_over(this);\" onmouseout=\"JAVASCRIPT: calendar_out(this);\" onclick=\"JAVASCRIPT: calendar_set_date(this,'";
			content+= year + "', '"+month+"', '"+(i+1)+"')\" title=\"" + (to_days + i) + "\" >" + (i + 1) + "</td>";
			
		}else{
			if( (i + day + 1 ) % 7 == 0 && i == (nb_days_in_month -1)){
				content+= "<td class='day" + sel + "' onmouseover=\"JAVASCRIPT: calendar_over(this);\" onmouseout=\"JAVASCRIPT: calendar_out(this);\" onclick=\"JAVASCRIPT: calendar_set_date(this,'"+year+"', '"+month+"', '"+(i+1);
				content+= "')\" title=\"" + (to_days + i) + "\" >" + (i + 1) + "</td>";

				is_close = true;
				content+= "</tr>";
			}else{
				content+= "<td class='day" + sel + "' onmouseover=\"JAVASCRIPT: calendar_over(this);\" onmouseout=\"JAVASCRIPT: calendar_out(this);\" onclick=\"JAVASCRIPT: calendar_set_date(this,'"+year+"', '"+month+"', '"+(i+1);
				content+= "')\" title=\"" + (to_days + i) + "\" >" + (i + 1) + "</td>";
			}
		}
	}

	
	if(!is_close){

		var nb_leaving = 7 - ((nb_days_in_month + day) % 7);

		for(i = 0; i < nb_leaving; i++){
			content+= "<td class='next_day' onmouseover=\"JAVASCRIPT: calendar_over(this);\" onmouseout=\"JAVASCRIPT: calendar_out(this);\" onclick=\"JAVASCRIPT: calendar_redraw(this, 1, 'm');\" title=\"" + (to_days + (i + nb_days_in_month)) + "\">" + (i + 1) + "</td>";	
		}

		content += "</tr>";
	}

	content += "<tr><td colspan=\"8\" class=\"pick_a_day\" align=\"center\">Veuillez chiosir une date</td></tr>";
			
	content += "</table>";
	
	return content;
}

function calendar_set_date(currObj, year, month, date){

	if(currObj.getAttribute('name') == "CALENDAR_CONTROL"){
		var myDiv = currObj;
	}else{
		var myDiv = currObj.parentNode;
		
		while(myDiv = myDiv.parentNode){
	
			if(myDiv.getAttribute('name') == "CALENDAR_CONTROL")
				break;
		}
	}

	month++
	if(month <=9)
		month= "0" + month;


	if(date <=9)
		date= "0" + date;
	
	var linked_control = myDiv.getAttribute('linked_control');
	var format = myDiv.getAttribute('format');

	if(linked_control){
		linked_control = document.getElementById(linked_control);
		if(linked_control){
		
			dateObj = new Date();
			var hours = dateObj.getHours(); 
			var minutes = dateObj.getMinutes();
			var seconds = dateObj.getSeconds();
			
			if(hours <= 9)
				hours = "0" + hours;

			if(minutes <= 9)
				minutes = "0" + minutes;

			if(seconds <= 9)
				seconds = "0" + seconds;
				
			
			var add = " " + hours;
			add +=  ":" + minutes;
			add += ":" + seconds;

			if(format == "datetime")
				linked_control.value = year + "-" + month + "-" + date + add; //" 00:00:00";
			else
				linked_control.value = year + "-" + month + "-" + date;
		}
	}
	
	dateObj = new Date(year, month, date);
		
	myDiv.setAttribute('date', dateObj.toString());
	myDiv.setAttribute('setdate', dateObj.toString());
	
	calendar_close();
}

function calendar_redraw(currObj, step, unit){
	var myDiv = currObj.parentNode;
	
	while(myDiv = myDiv.parentNode){

		if(myDiv.getAttribute('name') == "CALENDAR_CONTROL")
			break;
	}
	
	var dateObj = myDiv.getAttribute('date');
	dateObj = new Date(dateObj);

	var setdateObj = myDiv.getAttribute('setdate');
	setdateObj = new Date(setdateObj);


	var month = dateObj.getMonth();
	var year =  dateObj.getFullYear();

	if(unit == 'm'){
		dateObj.setMonth(month + step);
	}else if(unit == "y"){
		dateObj.setYear(year + step);
	}
	
	myDiv.setAttribute('date', dateObj.toString() );
	myDiv.date = dateObj.toString();
	
	myDiv.innerHTML = calendar_content( dateObj, setdateObj );

}

function calendar_is_leap_year(year){
	if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0 ) )
		return true;
	else
		return false;
}

function calendar_go_today(currObj){
	var myDiv = currObj.parentNode;
	
	while(myDiv = myDiv.parentNode){

		if(myDiv.getAttribute('name') == "CALENDAR_CONTROL")
			break;
	}
	
	var dateObj = new Date();
	myDiv.setAttribute('date', dateObj.toString() );
	myDiv.date = dateObj.toString();
	
	var setdateObj = myDiv.getAttribute('setdate');
	setdateObj = new Date(setdateObj);
	
	myDiv.innerHTML = calendar_content( dateObj, setdateObj );

	calendar_set_date(myDiv, dateObj.getFullYear(), dateObj.getMonth(), dateObj.getDate());
}

function calendar_close(){

	var ctls = document.getElementsByTagName("DIV");
	var i, ctl;

	for(i = 0; i < ctls.length; i++){

		ctl = ctls[i];
		
		if(ctl.name == "CALENDAR_CONTROL"){
			clear_object_content( ctl );
			ctl.parentNode.removeChild( ctl );
		}
	}

}

function calendar_over(currObj){
	currObj.style.color = "#FF0000";
}

function calendar_out(currObj){
	currObj.style.color = "";
}

function calendar_help(){
	alert("Ce calendrier Ã  Ã©tÃ© conÃ§u et dÃ©velopper par Oxtal Solutions Â©2008");
}

function btn_over(currObj, imageName){

	if(currObj){

		var image = currObj.getElementsByTagName("img")[0];

		if(image){
			image.src = "/images/design/core/" + imageName + ".png";
		}
	
	}
}

function btn_out(currObj, imageName){

	if(currObj){

		var image = currObj.getElementsByTagName("img")[0];

		if(image){
			image.src = "/images/design/core/" + imageName + ".png";
		}
	
	}
}

function form_captcha(currObj, captcha_control_name){

	var myForm = recursive_form_find( currObj );
	var input = myForm[captcha_control_name];
	
	if(typeof(input) != "undefined" && typeof(hex_sha1) == "function"){
		var user_token = input.value;
		user_token = user_token.toUpperCase();
		
		var token = hex_sha1(user_token);
		
		var token_check = input.getAttribute("token");
		if( token == token_check)
			return true;
		
	}else
		alert("Error, no control or SHA1 JS function");
	
	return false;
}

//--------------------------------- ERGONOMIC TOOLS

function controls_ergo(){
	var inputs = document.getElementsByTagName('input');
	var i;

	for(i = 0; i < inputs.length; i++){

		if(inputs[i].type.toLowerCase() == 'text'  || inputs[i].type.toLowerCase() == 'password')
			apply_control_ergo( inputs[i] );
	}

	inputs = document.getElementsByTagName('select');
	for(i = 0; i < inputs.length; i++){
		apply_control_ergo( inputs[i] );
	}

	inputs = document.getElementsByTagName('textarea');
	for(i = 0; i < inputs.length; i++){
		apply_control_ergo( inputs[i] );
	}

}
	
function apply_control_ergo(currObj){

	if(currObj.onfocus == null)
		currObj.onfocus = control_ergo_in;
		
	if(currObj.onblur == null)
		currObj.onblur = control_ergo_out;
		
	var additionnal_info = currObj.getAttribute("additionnal_info");
	if(!empty(additionnal_info)){
	
	  //var skip_line = document.createElement("br");
	  //currObj.parentNode.appendChild( skip_line );
	
	  var span_info = document.createElement("span");
	  span_info.className = "additionnal_info";
	  span_info.innerHTML = additionnal_info;
	  currObj.parentNode.appendChild( span_info );
	}
	
	var currrObj_type = currObj.getAttribute("type")
	if(!empty(currrObj_type))
		currrObj_type = currrObj_type.toLowerCase();
	else
	  currObj_type = "";
	
	
	var mask = currObj.getAttribute("mask");
	if(!empty(mask)){
		if(currObj.onkeyup == null){
			currObj.onkeyup = add_control_ergo_mask_keyup(currObj);
		}
	}
	
	var indicator = currObj.getAttribute("indicator");
	if(!empty(indicator)){
	  var indicator = indicator.split("@");
	  
	  if(in_array("strength", indicator) ){
	  
		if(currObj != null && currObj != "null"){
		
		  if(currrObj_type == "password" || currObj.getAttribute("original_type") == "password"  ){
	  
			if(currObj.onkeyup == null){
				currObj.onkeyup = add_control_ergo_keyup(currObj);
			}
		  }

		}
	  }
	}
	

	
	var hint = currObj.getAttribute("hint");
					
	if(currrObj_type == 'text'  ||  currrObj_type == 'password'){
	
		if(!empty(hint) && empty(currObj.value)){

			try{
				currObj.setAttribute("type", "text");
				currObj.setAttribute("original_type", currrObj_type );
				currObj.setAttribute("original_class", currObj.className);

			}catch(error){

				var c_obj = document.createElement("input");
				c_obj.setAttribute("name", currObj.getAttribute("name"));
				c_obj.setAttribute("id", currObj.getAttribute("id"));
				c_obj.setAttribute("style", currObj.getAttribute("style"));
				c_obj.setAttribute("className", currObj.getAttribute("className"));
				c_obj.setAttribute("title", currObj.getAttribute("title"));
				c_obj.setAttribute("original_type", currrObj_type );
				c_obj.setAttribute("original_class", currObj.className);
				
				c_obj.setAttribute("hint", currObj.getAttribute("hint"));
				c_obj.setAttribute("additionnal_info", currObj.getAttribute("additionnal_info"));
				c_obj.setAttribute("quick_help", currObj.getAttribute("quick_help"));
				c_obj.setAttribute("validation", currObj.getAttribute("validation"));
				c_obj.setAttribute("indicator", currObj.getAttribute("indicator"));
				c_obj.onkeyup = currObj.onkeyup;
				
				c_obj.onfocus = control_ergo_in;
				c_obj.onblur = control_ergo_out;
				c_obj.setAttribute("type", "text");
				
				
				currObj.parentNode.insertBefore(c_obj, currObj);
				currObj.parentNode.removeChild( currObj );
				currObj = c_obj;
				
				
			}

			currObj.value = hint;
			
			currObj.className = currObj.getAttribute("original_class") + " input_hint";
		}
	}


	var quick_help = currObj.getAttribute("quick_help");
	if(!empty(quick_help)){
	  quick_help = quick_help.split("@");
	  quick_help = quick_help[2];
	
	  if( empty(currObj.title))
		currObj.title = quick_help;
	}


}
	
function control_ergo_in(){
	var currObj = this;

	if(typeof(NO_ERGO) == "undefined")
	  NO_ERGO = false;

	control_reset_validate( currObj );

	if(!NO_ERGO){

		if(typeof(ERGO_COLOR) == "undefined")
		  currObj.style.backgroundColor = "#EEEEEE";
		else
		  currObj.style.backgroundColor = ERGO_COLOR;
	}


	var hint = currObj.getAttribute("hint");

	if(!empty(hint)){
	
		if(hint == currObj.value)
			currObj.value = "";

		if(currObj.type != currObj.getAttribute("original_type")){
			
			try{
				currObj.setAttribute("type", currObj.getAttribute("original_type"));
				
			}catch(error){

				var c_obj = document.createElement("input");
				c_obj.setAttribute("name", currObj.getAttribute("name"));
				c_obj.setAttribute("id", currObj.getAttribute("id"));
				c_obj.setAttribute("style", currObj.getAttribute("style"));
				c_obj.setAttribute("className", currObj.getAttribute("className"));
				c_obj.setAttribute("title", currObj.getAttribute("title"));
				c_obj.setAttribute("original_type", currObj.getAttribute("original_type") );
				c_obj.setAttribute("original_class", currObj.getAttribute("original_class"));

				c_obj.setAttribute("hint", currObj.getAttribute("hint"));
				c_obj.setAttribute("additionnal_info", currObj.getAttribute("additionnal_info"));
				c_obj.setAttribute("quick_help", currObj.getAttribute("quick_help"));
				c_obj.setAttribute("validation", currObj.getAttribute("validation"));
				c_obj.setAttribute("indicator", currObj.getAttribute("indicator"));
				c_obj.onkeyup = currObj.onkeyup;

				c_obj.setAttribute("type", "password");
				
				currObj.parentNode.insertBefore(c_obj, currObj);
				currObj.parentNode.removeChild( currObj );
				currObj = c_obj;
				
				currObj.focus();
				

				currObj.onblur = control_ergo_out;
				currObj.onfocus = control_ergo_in;					
			}
			
		  
			
		}
		
		

		if(currObj.className != currObj.getAttribute("original_class") )
			currObj.className = currObj.getAttribute("original_class");

	}
	
	var quick_help = currObj.getAttribute("quick_help");
	if(!empty(quick_help)){
	  control_quick_help(quick_help, currObj);

	}
}

function control_ergo_out(){
	var currObj = this;
	if(typeof(NO_ERGO) == "undefined")
	  NO_ERGO = false;
	  
	if(!NO_ERGO){
		currObj.style.backgroundColor = "";
	}
	
	
	var qhc = document.getElementById('qhc');
	if(qhc != null && qhc != "null")
		qhc.parentNode.removeChild(qhc);


	var validation = currObj.getAttribute("validation");
	if(!empty(validation))
	  control_ergo_validate(currObj);

	var hint = currObj.getAttribute("hint");
	if(!empty(hint)){
	
		if(currObj.value.length == 0){
			
			currObj.className = currObj.getAttribute("original_class") + " input_hint";

			if(currObj.getAttribute("original_type") == "password"){
			//	currObj.type = "text";
				try{
					currObj.setAttribute("type", "text");
					
				}catch(error){
				
					var c_obj = document.createElement("input");
					c_obj.setAttribute("name", currObj.getAttribute("name"));
					c_obj.setAttribute("id", currObj.getAttribute("id"));
					c_obj.setAttribute("style", currObj.getAttribute("style"));
					c_obj.setAttribute("className", currObj.getAttribute("className"));
					c_obj.setAttribute("title", currObj.getAttribute("title"));
					c_obj.setAttribute("original_type", currObj.getAttribute("original_type") );
					c_obj.setAttribute("original_class", currObj.getAttribute("original_class"));

					c_obj.setAttribute("hint", currObj.getAttribute("hint"));
					c_obj.setAttribute("additionnal_info", currObj.getAttribute("additionnal_info"));
					c_obj.setAttribute("quick_help", currObj.getAttribute("quick_help"));
					c_obj.setAttribute("validation", currObj.getAttribute("validation"));
					c_obj.setAttribute("indicator", currObj.getAttribute("indicator"));
					c_obj.onkeyup = currObj.onkeyup;

					c_obj.onfocus = control_ergo_in;
					c_obj.onblur = control_ergo_out;
					c_obj.setAttribute("type", "text");

					currObj.parentNode.insertBefore(c_obj, currObj);
					currObj.parentNode.removeChild( currObj );

					currObj = c_obj;
					
					
				}
				
				
			}
			currObj.value = hint;
		}
			
		

	}

}

function add_control_ergo_keyup(currObj){
  return function(){control_ergo_keyup( currObj); };

}

function control_ergo_keyup( currObj){

  if(window.event){
	currObj = window.event.srcElement;
  }
  var current_password = currObj.value;
  
  var i;	  

  var strength = parseInt(password_strength(current_password));
  var strength_message = "";
  if(strength > 0){
	  var data = "10,0@@";
	  
	  var validation = currObj.getAttribute("validation");
	  if(!empty(validation)){
		var tmp = validation.split("@");
		var message = tmp[3];
		var message_index, message_content;
		
		if(!empty(message)){

		  message = message.toString().split("|");
		  for(i = 0; i < message.length; i++){
		   tmp = message[i].split("=");
		   message_index = tmp.shift();
		   message_content = tmp.join("=");
		   
		   
		   
		   if(message_index == "password" + strength){
			 strength_message = message_content;
			 break;
		   }
		  }
		}
		
	  }

	  for(i = 0; i < strength; i++)
		data += "<div class='indicator_password_icon'>&nbsp;</div>";
	  
	  data += "<div class='indicator_password_message'>" + strength_message + "</div>";

	  control_quick_indicate(data, currObj);
  }else{
	  var qhc = document.getElementById('qhi_' + currObj.id);
	  if(qhc != null && qhc != "null")
		qhc.parentNode.removeChild(qhc);

  }
}

function add_control_ergo_mask_keyup(currObj){
  return function(){control_ergo_mask_keyup( currObj); };

}

function control_ergo_mask_keyup( currObj){

  if(window.event){
	currObj = window.event.srcElement;
  }
  
  control_ergo_mask(currObj);
}

function control_quick_help(quick_help, currObj){

	  var tmp = quick_help.split("@");
	  var help_title = tmp[1];
	  var help_info = tmp[2];
	  
	  tmp = tmp[0].split(",");
	  var offset_x = parseInt(tmp[0]);
	  var offset_y = parseInt(tmp[1]);
	  
	  var pos = findPos(currObj);
	  
	  if(offset_x > 0)
		var x = pos[0] + currObj.clientWidth + offset_x;
	  else
		var x = pos[0] + offset_x;
		
	  if(offset_y > 0)
		var y = pos[1] + currObj.clientHeight + offset_y;
	  else
		var y = pos[1] + offset_y;
	
	  var qhc = document.getElementById('qhc');
	  if(qhc != null && qhc != "null")
		qhc.parentNode.removeChild(qhc);

	  qhc = document.createElement("div");
	  qhc.className = "quick_help_container";
	  qhc.id = "qhc";
	  
	  var qht = document.createElement("div");
	  qht.className = "quick_help_title";
	  qht.innerHTML = help_title;
	  qhc.appendChild(qht);
	  
	  var qhi = document.createElement("div");
	  qhi.className = "quick_help_info";
	  qhi.innerHTML = help_info;
	  qhc.appendChild(qhi);
	  
	  qhc.style.position = "absolute";
	  qhc.style.left = x + "px";
	  qhc.style.top = y + "px";

	  document.body.appendChild(qhc);

}

function control_quick_indicate(quick_indicate, currObj){

	  var tmp = quick_indicate.split("@");
	  var help_title = tmp[1];
	  var help_info = tmp[2];
	  
	  tmp = tmp[0].split(",");
	  var offset_x = parseInt(tmp[0]);
	  var offset_y = parseInt(tmp[1]);
	  
	  var pos = findPos(currObj);
	  
	  if(offset_x > 0)
		var x = pos[0] + currObj.clientWidth + offset_x;
	  else
		var x = pos[0] + offset_x;
		
	  if(offset_y > 0)
		var y = pos[1] + currObj.clientHeight + offset_y;
	  else
		var y = pos[1] + offset_y;
	
	  var qhc = document.getElementById('qhc');
	  if(qhc != null && qhc != "null")
		qhc.parentNode.removeChild(qhc);

	  qhc = document.getElementById('qhi_'+currObj.id);
	  if(qhc != null && qhc != "null")
		qhc.parentNode.removeChild(qhc);

	  qhc = document.createElement("div");
	  qhc.className = "quick_indicator_container";
	  qhc.id = "qhi_"+currObj.id;
	  
	  var qht = document.createElement("div");
	  qht.className = "quick_indicator_title";
	  qht.innerHTML = help_title;
	  qhc.appendChild(qht);
	  
	  var qhi = document.createElement("div");
	  qhi.className = "quick_indicator_info";
	  qhi.innerHTML = help_info;
	  qhc.appendChild(qhi);
	  
	  qhc.style.position = "absolute";
	  qhc.style.left = x + "px";
	  qhc.style.top = y + "px";

	  document.body.appendChild(qhc);

}

function control_quick_validation(msg, currObj){
	if( typeof(currObj) == "string")
	  currObj = document.getElementById(currObj);

	  var quick_help = currObj.getAttribute("quick_help");
	  if(!empty(quick_help)){
		  var tmp = quick_help.split("@");
		  tmp = tmp[0].split(",");
		  var offset_x = parseInt(tmp[0]);
		  var offset_y = parseInt(tmp[1]);
	  }else{
		  var offset_x = 1;
		  var offset_y = 0;
	  }

	  var pos = findPos(currObj);
	  
	  if(offset_x > 0)
		var x = pos[0] + currObj.clientWidth + offset_x;
	  else
		var x = pos[0] + offset_x;
		
	  if(offset_y > 0)
		var y = pos[1] + currObj.clientHeight + offset_y;
	  else
		var y = pos[1] + offset_y;
	
	  var qvc = document.getElementById('qvi_' + currObj.id);
	  if(qvc != null && qvc != "null")
		qvc.parentNode.removeChild(qvc);
		
	  qvc = document.getElementById('qvc_' + currObj.id);
	  if(qvc != null && qvc != "null")
		qvc.parentNode.removeChild(qvc);

	  qvc = document.createElement("div");
	  qvc.className = "quick_validation_container";
	  qvc.id = "qvc_" + currObj.id;
	  qvc.innerHTML = msg;

	  qvc.style.position = "absolute";
	  qvc.style.left = x + "px";
	  qvc.style.top = y + "px";

	  document.body.appendChild(qvc);

}

function control_ergo_validate(currObj, is_submit){

	var validation = currObj.getAttribute("validation");
	if(empty(validation))
	  return true;

	var icon_loader_delay = 1200;

	if(is_submit == null || is_submit == "null")
	  is_submit = false;

	var tmp = validation.split("@");
	var type = tmp[0].split(',');
	var parent_level = parseInt(tmp[1]);
	var behavior = tmp[2].split(',');
	var message = tmp[3];

	var i, tmpObj;
	
	if(in_array("submit", type) && !is_submit)
	  return true;

	var hint = currObj.getAttribute("hint");

	var valid = control_ergo_validate_content( type, currObj);
	var is_valid = empty(valid);

	if(in_array("bg", behavior)){    
	
		tmpObj = currObj; 

		for(i = 0; i < parent_level; i++){
			tmpObj = tmpObj.parentNode;
		}
	}

	if(in_array("icon", behavior)){
		control_ergo_loader(currObj);
		window.setTimeout(function(){ control_ergo_loader_close(currObj.id); }, icon_loader_delay);
	}

	if(!is_valid){

		if(in_array("msg", behavior)){
		
			var tmp = message.split("|");
			var out_message = "";
			
			if(tmp.length >= 1){ //MANY MESSAGES, FIND CORRESPONDING INDEX
				var message_index, message_content, tmp2;
				
				for(i = 0; i < tmp.length; i++){
					tmp2 = tmp[i].split("=");
					message_index = tmp2[0];
					tmp2.shift(); //REMOVE TYPE AND THE CONCAT LATER CHARS
					message_content = tmp2.join("=");
					
					if(message_index == valid){
						out_message = message_content;
						break;
					}
				}
				
				if(empty(out_message) && !empty(message) ){
				  tmp2 = tmp[0].split("=");
				  tmp2.shift();
				  out_message = tmp2.join("=");
				}	
			}
			
			
			
			if(!empty(out_message)){
				if(in_array("icon", behavior)){
					window.setTimeout(function(){control_quick_validation(out_message, currObj.id);}, icon_loader_delay+1 );
				}else
					control_quick_validation(out_message, currObj);
				
				
			}
		}

		if( tmpObj != null){
			var tmp_class = tmpObj.getAttribute("original_class");
			
			if(empty(tmp_class)){

				tmp_class = tmpObj.className;
				tmpObj.setAttribute("original_class",tmp_class);
			}

			var res_class = tmp_class + " input_container_error";
			
			if(in_array("icon", behavior)){
				window.setTimeout(function(){tmpObj.className = res_class;}, icon_loader_delay+1 );
			}else
				tmpObj.className = res_class;
		}
	}else{

		

		if(tmpObj != null){

			tmpObj.className = tmpObj.getAttribute("original_class");
		}

		if(!empty(hint)){

			if(currObj.type.toLowerCase() == "text" || currObj.type == "password"){
				var value = currObj.value;
			}else if( currObj.type.toLowerCase() == "select"){
				var value = currObj.options[currObj.selectedIndex].value;
			}

			if(value != hint){
			  currObj.className += currObj.getAttribute("original_class");
			}
			
		}
		
		control_reset_validate(currObj);
		
		if(in_array("approved", behavior))
			window.setTimeout(function(){ control_ergo_approved(currObj.id); }, icon_loader_delay+1);
	}

	return is_valid;
}

function form_ergo_validate(currForm){
	var is_valid = true;

	if(currForm != null && currForm != "null"){
		var elems = currForm.elements;
		var i, elem, res, validation;
		var valid_tag_types = new Array("text", "password", "checkbox", "radio");
	 
	  
		for(i = 0; i < elems.length; i++){
			elem = elems[i];
			validation = elem.getAttribute("validation");
			
			if(!empty(validation)){
				
				if(elem.tagName.toLowerCase() == "input"){
				
					if(in_array(elem.type.toLowerCase(), valid_tag_types)){
						
						res = control_ergo_validate(elem, true);

						if(is_valid && !res)
						  is_valid = res;
						  
					}

				}else if(elem.tagName.toLowerCase() == "select"){
				
					res = control_ergo_validate(elem, true);

					if(is_valid && !res)
					  is_valid = res;
				}
			}
		}
	}
  
  return is_valid;
  
}

function control_reset_validate(currObj){
	var validation = currObj.getAttribute("validation");


	if(validation == null)
	  return;

	var tmp = validation.split("@");
	var type = tmp[0].split(',');
	var parent_level = parseInt(tmp[1]);
	var behavior = tmp[2].split(',');
	var message = tmp[3];

	var i, tmpObj;

	//REMOVE HIERARCHY CLASS
	if(in_array("bg", behavior)){
		tmpObj = currObj;
		for(i = 0; i < parent_level; i++){
				tmpObj = tmpObj.parentNode;
		}

		if(tmpObj != null)
			tmpObj.className = tmpObj.getAttribute("original_class");
	}

	//REMOVE ERROR MESSAGE
	if(in_array("msg", behavior)){
	  var qvc = document.getElementById('qvc_' + currObj.id);
	  if(qvc != null && qvc != "null")
		qvc.parentNode.removeChild(qvc);

	}
	
	var indicator = currObj.getAttribute("indicator");
	if(!empty(indicator)){
		var qhc = document.getElementById('qhi'+currObj.id);
		if(qhc != null && qhc != "null")
			qhc.parentNode.removeChild(qhc);
	}

}

//SUPPORT: alpha, caps, numbers, symbols, accents, email, required, user, minlength, maxlength, confirm, password, captcha
function control_ergo_validate_content(type, currObj){
	var i;
	
	if(currObj.type.toLowerCase() == "text" || currObj.type == "password"){
		var value = currObj.value.toString();
	}else if( currObj.type.toLowerCase() == "select"){
		var value = currObj.options[currObj.selectedIndex].value.toString();
	}

	
	//CHECK FOR EMPTY
	if(in_array("required", type)){
	
		var hint = currObj.getAttribute("hint");
		
		if(!empty(hint)){
			if( hint == value)
				return "required";
		}
		
		if(value.length == 0)
			return "required";
	}

	//CHECK FOR EMAIL PATTERN
	if(in_array("email", type) && !validate_email(value) )
	  return "email";

	//CHECK FOR UNIQUE USER LOOKUP
	if(in_array("user", type)){
		var url = "/data_provider/module_softwares.php?dp=module_loader&module=security&a=user_unique&username=" + value;
		var ar = new Ajax(url, "GET", false)
		var res = ar.send();
		res = res.search(/\<status\>FAIL\<\/status\>/gi);
		if(res != -1)
		  return "user";
	}
	
	//CHECK FOR MINLENGTH
	if(in_array("minlength", type)){
	  var index = array_pos("minlength", type);
	  var minlength = parseInt( type[index+1] );
	  
	  if(value.length < minlength)
		return "minlength";
	
	}
	

	//CHECK FOR MAXLENGTH
	if(in_array("maxlength", type)){
	  var index = array_pos("maxlength", type);
	  var maxlength = parseInt( type[index+1] );
	  
	  if(value.length > maxlength)
		return "maxlength";
	
	}
	
	//CHECK FOR CONTROL MATCH
	if(in_array("confirm", type)){
	  var index = array_pos("confirm", type);
	  var confirm = type[index+1];
	  
	  var current_form = recursive_form_find( currObj);
	  
	  var confirm_control = current_form[confirm]
	  if(confirm_control != null && confirm_control != "null"){
		if(confirm_control.value != value)
		  return "confirm";
	  }
	}
	
	//CHECK FOR PASSWORD MINIMUM LEVEL
	if(in_array("password", type)){
		var index = array_pos("password", type);
		var min_level = parseInt(type[index+1]);
		
		var strength = parseInt(password_strength(value));
		
		if(strength < min_level)
			return "password";
	}
	
	//CHECK FOR PASSWORD MINIMUM LEVEL
	if(in_array("captcha", type)){
		var captcha = document.getElementsByName('captcha')[0];
		var res = form_captcha(captcha, 'captcha');
		if(!res)
		  return "captcha";
	}
	
	//CHECK FOR ACCENTS
	
	var is_accents = false;
	for(i = 0; i < value.length; i++){

		if(value.charCodeAt(i) > 127){
		  is_accents = true;
		  break;
		}
	}
	
	if(is_accents && !(in_array("accents",type)  || in_array("all", type)))
	  return "accents";


	//CHECK FOR SPECIAL SYMBOLS
	var pattern_symbols = /[\\\.\[\]\{\}\(\)\?~!@#\$%\^&\*()_\+<>=;:'",\/|]/g;
	var ptest = value.search(pattern_symbols);
	if(ptest>=0 && !(in_array("symbols",type) || in_array("all", type) ) )
	  return "symbols";


	//CHECK FOR CAPS LETTERS
	pattern_caps = /[A-Z\-]/g;
	ptest = value.search(pattern_caps);
	if(ptest>=0 && !(in_array("caps",type) || in_array("all", type)) )
	  return "caps";


	//CHECK FOR LETTERS
	var pattern_letters = /[a-z\-]/g;
	ptest = value.search(pattern_letters);
	if(ptest>=0 && !(in_array("alpha",type) || in_array("all", type)) )
	  return "alpha";


	//CHECK FOR NUMBERS
	var pattern_numbers = /[0-9]/g;
	ptest = value.search(pattern_numbers);
	if(ptest>=0 && !(in_array("numbers",type) || in_array("all", type)) )
	  return "numbers";

	return "";
}

function control_ergo_mask(currObj){

	var mask = currObj.getAttribute("mask");
	
	if(!empty(mask)){
	
	  mask = mask.split("@");
	  var pattern = mask[0];
	  var pattern_type = mask[1];
	  var pattern_param = mask[2];
	  
	  if(!empty(pattern_param))
		  pattern_param = pattern_param.split(",");
		  
	  var value = currObj.value;
	  var i,pchar, vchar, res;
	  var out_value = "";
	  
	  
	  var pattern_chars = new Array("a", "n", "A", "N", "*");
	  
	  if( in_array("uppercase", pattern_param) || in_array("lowercase", pattern_param)  ){
	  	var pattern_regs = new Array( /[a-z]/gi , /[0-9]/g , /[A-Z]/gi , /[0-9]/g, /[\w\W]/g );
	  }else
		  var pattern_regs = new Array( /[a-z]/g , /[0-9]/g , /[A-Z]/g , /[0-9]/g, /[\w\W]/g );
		  
	  var ppos, preg;
	  
	  if(pattern_type == "direct"){



		for(i = 0; i < value.length && i < pattern.length; i++){

		  pchar = pattern.charAt(i);
		  vchar = value.charAt(i);
		  
		  if( in_array(pchar, pattern_chars) ){
			ppos = array_pos(pchar, pattern_chars);
			preg = pattern_regs[ ppos ];
			
			res = vchar.search( preg);
			
			if(res >= 0){
			  out_value += vchar;
			}
			
		  }else{
			out_value += pchar;
			
			pchar = pattern.charAt(i+1);
			if( in_array(pchar, pattern_chars) ){
				ppos = array_pos(pchar, pattern_chars);
				preg = pattern_regs[ ppos ];
			
				res = vchar.search( preg);
			
				if(res >= 0){
				  out_value += vchar;
				}
			
			}

			
		  }
		  
		}
		
		if( in_array("uppercase", pattern_param))
		  out_value = out_value.toUpperCase();
		else if( in_array("lowercase", pattern_param))
			out_value = out_value.toLowerCase();
		
		currObj.value = out_value;

	  }else if(pattern_type == "related"){
	  // RELATED TO A ANOTHER CONTROL MASK

	    var related_control = pattern_param[0];
		related_control = document.getElementById( related_control );
		
		if(related_control.tagName.toLowerCase() == "select"){
		  pattern = related_control.options[related_control.selectedIndex].getAttribute(currObj.name + "_mask");
		  
			for(i = 0; i < value.length && i < pattern.length; i++){

		  pchar = pattern.charAt(i);
		  vchar = value.charAt(i);
		  
		  if( in_array(pchar, pattern_chars) ){
			ppos = array_pos(pchar, pattern_chars);
			preg = pattern_regs[ ppos ];
			
			res = vchar.search( preg);
			
			if(res >= 0){
			  out_value += vchar;
			}
			
		  }else{
			out_value += pchar;
			
			pchar = pattern.charAt(i+1);
			if( in_array(pchar, pattern_chars) ){
				ppos = array_pos(pchar, pattern_chars);
				preg = pattern_regs[ ppos ];
			
				res = vchar.search( preg);
			
				if(res >= 0){
				  out_value += vchar;
				}
			
			}

			
		  }
		  
		}

		
			if( in_array("uppercase", pattern_param))
			  out_value = out_value.toUpperCase();
			else if( in_array("lowercase", pattern_param))
				out_value = out_value.toLowerCase();
		
			currObj.value = out_value;
		}
	  }


	}
}

function control_ergo_loader(currObj){

	var quick_help = currObj.getAttribute("quick_help");
	  if(!empty(quick_help)){
		  var tmp = quick_help.split("@");
		  tmp = tmp[0].split(",");
		  var offset_x = parseInt(tmp[0]);
		  var offset_y = parseInt(tmp[1]);
	  }else{
		  var offset_x = 1;
		  var offset_y = 0;
	  }

	  var pos = findPos(currObj);
	  
	  if(offset_x > 0)
		var x = pos[0] + currObj.clientWidth + offset_x;
	  else
		var x = pos[0] + offset_x;
		
	  if(offset_y > 0)
		var y = pos[1] + currObj.clientHeight + offset_y;
	  else
		var y = pos[1] + offset_y;
	
	  var qvc = document.getElementById('qvi_' + currObj.id);
	  if(qvc != null && qvc != "null")
		qvc.parentNode.removeChild(qvc);
		
	  qvc = document.getElementById('qvc_' + currObj.id);
	  if(qvc != null && qvc != "null")
		qvc.parentNode.removeChild(qvc);
		
	qvc = document.getElementById('qvl_' + currObj.id);
	  if(qvc != null && qvc != "null")
		qvc.parentNode.removeChild(qvc);

	  qvc = document.createElement("div");
	  qvc.className = "quick_validation_loader";
	  qvc.id = "qvl_" + currObj.id;
	  qvc.innerHTML = "&nbsp;";

	  qvc.style.position = "absolute";
	  qvc.style.left = x + "px";
	  qvc.style.top = y + "px";

	  document.body.appendChild(qvc);

}

function control_ergo_approved(currObj){

	if( typeof(currObj) == "string")
	  currObj = document.getElementById(currObj);

	var quick_help = currObj.getAttribute("quick_help");
	if(!empty(quick_help)){
	  var tmp = quick_help.split("@");
	  tmp = tmp[0].split(",");
	  var offset_x = parseInt(tmp[0]);
	  var offset_y = parseInt(tmp[1]);
	}else{
	  var offset_x = 1;
	  var offset_y = 0;
	}

	  var pos = findPos(currObj);
	  
	  if(offset_x > 0)
		var x = pos[0] + currObj.clientWidth + offset_x;
	  else
		var x = pos[0] + offset_x;
		
	  if(offset_y > 0)
		var y = pos[1] + currObj.clientHeight + offset_y;
	  else
		var y = pos[1] + offset_y;
	
	  var qvc = document.getElementById('qvi_' + currObj.id);
	  if(qvc != null && qvc != "null")
		qvc.parentNode.removeChild(qvc);
		
	  qvc = document.getElementById('qvc_' + currObj.id);
	  if(qvc != null && qvc != "null")
		qvc.parentNode.removeChild(qvc);
		
	 qvc = document.getElementById('qvl_' + currObj.id);
	  if(qvc != null && qvc != "null")
		qvc.parentNode.removeChild(qvc);

	  qvc = document.createElement("div");
	  qvc.className = "quick_validation_icon";
	  qvc.id = "qvi_" + currObj.id;
	  qvc.innerHTML = "&nbsp;";

	  qvc.style.position = "absolute";
	  qvc.style.left = x + "px";
	  qvc.style.top = y + "px";

	  document.body.appendChild(qvc);

}


function control_ergo_loader_close(currObj){

	if( typeof(currObj) == "string")
		var qvc = document.getElementById('qvl_' + currObj);
	else
		var qvc = document.getElementById('qvl_' + currObj.id);
		
	if(qvc != null && qvc != "null")
		qvc.parentNode.removeChild(qvc);
}


function listing_ergo(){

  if(typeof(tables_ergo) != "undefined" && tables_ergo != null && tables_ergo != "null"){
  
    var section, element, parent_table, i, j, k, tmp,tmp2, opt, row, cell, field, fields, field_type, field_control, sel_values, tmp_sel_value;
	

    for(i = 0; i < tables_ergo.length; i++){
	  section = tables_ergo[i];
	  element = document.getElementById( section );

	  parent_table = get_parent_lister( element );

		var xml_name = get_xml_name( element );

		if(!empty(xml_name)){
			var recordsXML = localXML[ xml_name ];

			if(!empty(recordsXML))
				var nb_records = recordsXML.getElementsByTagName("records")[0].getAttribute("nb_records");
			else
				var nb_records = 0;


		}else
			var nb_records = 0;


	  //var nb_records = element.parentNode.rows.length - 1;
	  var nb_records_per_pages = 10;
	  var nb_pages = Math.ceil(nb_records / nb_records_per_pages) ;

	  if(!empty(parent_table)){

	    var top_info = document.createElement("div");
		top_info.className = "dynamic_list_header";
		top_info.id = "top_info_" + section;

		if(element.getAttribute("page_nav") == "1"){

		  tmp = document.createElement("span");
		  tmp.className = "page_text1";
		  tmp.innerHTML = "Page";
		  top_info.appendChild(tmp);

		  tmp = document.createElement("input")
		  tmp.type = "button";
		  tmp.className = "page_previous";
		  tmp.value = " ";
		  tmp.onclick = add_function_listing_ergo_previous(section);
		  top_info.appendChild(tmp);
		  
		  tmp = document.createElement("input")
		  tmp.type = "text";
		  tmp.className = "page_indicator";
		  tmp.value = "1";
		  tmp.onblur = add_function_listing_ergo_indicator_change(section);
		  top_info.appendChild(tmp);
		  
		  tmp = document.createElement("input")
		  tmp.type = "button";
		  tmp.className = "page_next";
		  tmp.value = " ";
		  tmp.onclick = add_function_listing_ergo_next(section);
		  top_info.appendChild(tmp);
		  
		  tmp = document.createElement("span");
		  tmp.className = "page_text2";
		  tmp.innerHTML = "  de <span id='nb_pages'>" + nb_pages + "</span> pages";
		  top_info.appendChild(tmp);


		  tmp = document.createElement("span");
		  tmp.className = "header_separator";
		  tmp.innerHTML = "|";
		  top_info.appendChild(tmp);
		  
		  tmp = document.createElement("span");
		  tmp.className = "page_text3";
		  tmp.innerHTML = " Visualisant ";
		  top_info.appendChild(tmp);

		  tmp = document.createElement("select");
		  tmp.className = "page_select";
//		  tmp.onblur = add_function_listing_ergo_page_select(section);
		  tmp.onchange = add_function_listing_ergo_page_select(section);

		  opt = document.createElement("option");
		  opt.value = "10";
		  opt.innerHTML = "10";
		  tmp.appendChild(opt);
		  
		  opt = document.createElement("option");
		  opt.value = "25";
		  opt.innerHTML = "25";
		  tmp.appendChild(opt);
		  
		  opt = document.createElement("option");
		  opt.value = "100";
		  opt.innerHTML = "100";
		  tmp.appendChild(opt);
		  
		  top_info.appendChild(tmp);
		  
  		  tmp = document.createElement("span");
		  tmp.className = "header_separator";
		  tmp.innerHTML = "|";
		  top_info.appendChild(tmp);

		  
		  tmp = document.createElement("span");
		  tmp.className = "page_text4";
		  tmp.innerHTML = " Total de <span id='nb_records'>" + nb_records + "</span> trouv&eacute;s";
		  top_info.appendChild(tmp);
		  
		}

		if(element.getAttribute("page_filter") == "1"){
		  
		  tmp2 = document.createElement("span");
		  tmp2.className = "page_filter_controls";
		  
		  tmp = document.createElement("input");
		  tmp.type = "button";
		  tmp.className = "page_filter_reset";
		  tmp.value = "Remise a zero";
		  tmp.onclick = add_function_listing_ergo_filter_reset(section);
		  tmp2.appendChild(tmp);
		  
		  tmp = document.createElement("input");
		  tmp.type = "button";
		  tmp.className = "page_filter_search";
		  tmp.value = "Rechercher";
		  tmp.onclick = add_function_listing_ergo_filter_search(section);
		  tmp2.appendChild(tmp);
		  
		  top_info.appendChild(tmp2);
		  
		  //- LOCATE SUB TH
		 fields = element.getAttribute("columns_currently").split(",");
		 var parent_container = parent_table.getElementsByTagName("THEAD")[0];
		 
		 if( typeof(parent_container) != "undefined" && parent_container != null && parent_container != "null")
		   tmp = parent_container.insertRow(1);
		 else
		   tmp = parent_table.insertRow(1);

		 tmp.className = "dynamic_search_filter_row";
		  
		  for(j = 0; j < parent_table.rows[0].cells.length; j++){
		    cell = tmp.insertCell(j);
			cell.style.backgroundColor = "";
			cell.className = "dynamic_search_filter_cell";
			cell.style.verticalAlign = "top";
			
			field = fields[j];
			
			
			
			if(field != "null"){

				field_type = element.getAttribute("field_" + field).toUpperCase().split(",");
				sel_values = element.getAttribute("field_" + field).split(",");
				sel_values.shift();
				
				if( in_array("TEXT", field_type)){

				  field_control = document.createElement("input");
				  field_control.type = "text";
				  field_control.name = "dynamic_search_filter[]";
				  field_control.id = "dynamic_search_filter_" + field;
				  field_control.style.width = "98%";
//				  field_control.onblur = add_function_listing_ergo_filter_search(section);
				  cell.appendChild( field_control );

	
				}else if( in_array("BOOL", field_type)){
				
				  field_control = document.createElement("select");
				  field_control.name = "dynamic_search_filter[]";
				  field_control.id = "dynamic_search_filter_" + field;
				  
				  opt = document.createElement("option");
				  opt.value = "";
				  opt.innerHTML = "";
				  
				  field_control.appendChild(opt);
				  field_control.selectedIndex = 0;
				  
				  
				  opt = document.createElement("option");
				  opt.value = "Y";
				  opt.innerHTML = "Oui";
				  field_control.appendChild(opt);

				  opt = document.createElement("option");
				  opt.value = "N";
				  opt.innerHTML = "Non";
				  field_control.appendChild(opt);

				  cell.appendChild( field_control );
				  
				}else if( in_array("NUMBER", field_type)){
				
				  if( !in_array("RANGE", field_type)){
				  
				    field_control = document.createElement("input");
  				    field_control.type = "text";
				    field_control.name = "dynamic_search_filter[]";
				    field_control.id = "dynamic_search_filter_" + field;
				    field_control.style.width = "98%";
				    cell.appendChild( field_control );
					
				  }else{

					var liner = document.createElement("div");
					liner.style.textAlign = "right";
					cell.appendChild( liner );

				  	//FROM
				    field_control = document.createElement("span");
					field_control.innerHTML = "De: ";
					liner.appendChild( field_control );

					//FROM CONTROL
				    field_control = document.createElement("input");
  				    field_control.type = "text";
				    field_control.name = "dynamic_search_filter[]";
				    field_control.id = "dynamic_search_filter_" + field;
				    field_control.style.width = "60px";
				    liner.appendChild( field_control );

					liner = document.createElement("div");
					liner.style.textAlign = "right";
					cell.appendChild( liner );

					//TO
					field_control = document.createElement("span");
					field_control.innerHTML = "A: ";
					liner.appendChild( field_control );

					//TO CONTROL
				    field_control = document.createElement("input");
  				    field_control.type = "text";
				    field_control.name = "dynamic_search_filter[]";
				    field_control.id = "dynamic_search_filter_" + field;
				    field_control.style.width = "60px";
				    liner.appendChild( field_control );

				  }
				}else if( in_array("SELECT", field_type)){
				
				  field_control = document.createElement("select");
				  field_control.name = "dynamic_search_filter[]";
				  field_control.id = "dynamic_search_filter_" + field;
				  
				  opt = document.createElement("option");
				  opt.value = "";
				  opt.innerHTML = "";
				  
				  field_control.appendChild(opt);
				  field_control.selectedIndex = 0;
				  
				  
				  for(k = 0; k < sel_values.length; k++){
					  tmp_sel_value =  sel_values[k].split("@");
						
					  opt = document.createElement("option");
					  opt.value = tmp_sel_value[0];
					  opt.innerHTML = tmp_sel_value[1];
					  field_control.appendChild(opt);

				  }

				  cell.appendChild( field_control );
				  
				}else if( in_array("MULTISELECT", field_type)){
				
					field_control = document.createElement("input");
					field_control.type = "hidden";
					field_control.name = "dynamic_search_filter[]";
					field_control.id = "dynamic_search_filter_" + field;
					
					cell.appendChild( field_control );
					
					var tmp_button = document.createElement("input");
					tmp_button.type = "button";
					tmp_button.value = "Choisir";
					tmp_button.onclick = add_function_listing_ergo_filter_multiselect(section, field);
					
					cell.appendChild( tmp_button );
				}
			}
		  }
		}

		if( parseInt(element.getAttribute("page_sort")) >= 1){

		  fields = element.getAttribute("columns_currently").split(",");
		  row = parent_table.rows[0];

		  for(j = 0; j < row.cells.length; j++){

		    cell = row.cells[j];
			field = fields[j];
			
			if(field != "null"){
				cell.onclick = add_function_listing_ergo_sort(section, cell);
				cell.className += " dynamic_list_header_cell";
			}

		  }
		  
		}
		
		parent_table.parentNode.insertBefore(top_info, parent_table);
	  }
	}

  }

}

//--------------------------------- ADDING FUNCTIONS
function add_function_listing_ergo_previous(section){
  return function(){ listing_ergo_previous_page(section); };
}

function add_function_listing_ergo_next(section){
  return function(){ listing_ergo_next_page(section); };
}

function add_function_listing_ergo_indicator_change(section){
  return function(){ listing_ergo_indicator_change(section, this); };
}

function add_function_listing_ergo_page_select(section){
  return function(){ listing_ergo_page_select(section, this); };
}

function add_function_listing_ergo_filter_reset(section){
  return function(){ listing_ergo_filter_reset(section); };
}

function add_function_listing_ergo_filter_search(section){
  return function(){ listing_ergo_filter_search(section); };
}

function add_function_listing_ergo_sort(section, currObj){
  return function(event){ listing_ergo_sort(section, currObj, event); };
}

function add_function_listing_ergo_filter_multiselect(section, field){
 return function(){ listing_ergo_filter_multiselect(section, field); };
}

//--------------------------------- EVENT FUNCTIONS
function listing_ergo_previous_page(section){

	listing_ergo_page_change(section, -1, 0);
}

function listing_ergo_next_page(section){

	listing_ergo_page_change(section, 1, 0);
}

function listing_ergo_indicator_change(section, currObj){
  
  listing_ergo_page_change(section, 0, currObj.value);
}

function listing_ergo_page_change(section, page_inc, page_requested){

	var element = document.getElementById(section);
	var top_info = document.getElementById("top_info_" + section);
	var spans = top_info.getElementsByTagName("span");
	
	//LOADER
	vis_area_title_loader_show(element, "Navigation: ");
	
	var i;
	var nb_records = 0;
	var nb_pages = 0;
	var page_control = null;
	
	for(i = 0; i < spans.length; i++){
	  if(spans[i].id == "nb_records")
	    nb_records = parseInt(spans[i].innerHTML);
	  else if(spans[i].id == "nb_pages")
	    nb_pages = parseInt(spans[i].innerHTML);
	}
	
	
	var page_control = top_info.getElementsByTagName("input");

	for(i = 0; i < page_control.length; i++){

	  if(page_control[i].type.toUpperCase() == "TEXT"){
	  
	    page_control = page_control[i];
	    break;
	  }
	    
	}

	var page = parseInt(page_control.value);
    var page_changed = false;
	
	if(page_inc > 0){
	//- NEXT PAGE
		if(page < nb_pages){
		  page++;
		  page_control.value = page;
		  page_changed = true;
		}
	}else if(page_inc < 0){
	//- PREVIOUS PAGE
		if(page > 1){
		  page--;
		  page_control.value = page;
		  page_changed = true;
		}
	
	} else{
	//- REQUESTED PAGE
		if(page_requested > 0 && page_requested <= nb_pages){
		
			page_control.value = page_requested;
			page_changed = true;
			
		}else{
		
			if(page_requested < 1){

				page_changed = true;		
				page_control.value = 1;
	
				
			}else{

				page_changed = true;
				page_control.value = nb_pages;
				
			}
		}
	}

	if(page_changed){
		//CLEAR TABLE AND PREVIOUS DATA
		clear_object_content(element.parentNode);
		localXML[section] = null;
	
		//EXEC REQUEST
		load_datas(element, get_element_add_link(element) );
	}else
		vis_area_title_loader_hide(element);
}

function listing_ergo_page_select(section, currObj){
	var element = document.getElementById(section);
	var top_info = document.getElementById("top_info_" + section);

	//LOADER
	vis_area_title_loader_show(element, "Navigation: ");

	//SET PAGE NAV AT 1
	set_nav_page(section, 1);
	
	//CLEAR TABLE AND PREVIOUS DATA
	clear_object_content(element.parentNode);
	localXML[section] = null;
	
	//EXEC REQUEST
	load_datas(element, get_element_add_link(element) );
}

function listing_ergo_filter_reset(section){

  var element = document.getElementById(section);
  var lister = get_parent_lister( element );

	//LOADER
	vis_area_title_loader_show(element, "Remise a z&eacute;ro: ");


  var inputs = document.getElementsByName("dynamic_search_filter[]");
  var i, input;

  for(i = 0; i < inputs.length; i++){
    input = inputs[i];
	
	if(input.tagName.toUpperCase() == "INPUT"){
	  input.value = "";
	}else if( input.tagName.toUpperCase() == "SELECT" ){
	  input.selectedIndex = 0;
	}

  }

	element.setAttribute("filter", "");

	//SET PAGE NAV AT 1
	set_nav_page(section, 1);

	//CLEAR TABLE AND PREVIOUS DATA
	clear_object_content(element.parentNode);
	localXML[section] = null;

	load_datas(element, get_element_add_link(element) );
}

function listing_ergo_filter_search(section){

  var element = document.getElementById(section);
  var lister = get_parent_lister( element );

  //LOADER
  vis_area_title_loader_show(element, "Recherche: ");


//  var inputs = document.getElementsByName("dynamic_search_filter[]");
  
  var i, input, value, field;

  var add_link = "";

  var inputs = lister.getElementsByTagName("input");
  //INPUTS
	for(i = 0; i < inputs.length; i++){
  
		input = inputs[i];
		field = input.id;
		field = field.substr(22);
		value = input.value;
		add_link += "&filter[]=" + field + "&filter_value[]=" + value;
	}
	
    

	inputs = lister.getElementsByTagName("select");
	//SELECTBOX
	for(i = 0; i < inputs.length; i++){
  
		input = inputs[i];
		field = input.id;
		field = field.substr(22);
		value = input.options[input.selectedIndex].value;
		add_link += "&filter[]=" + field + "&filter_value[]=" + value;
	}
	
    
  
  
  element.setAttribute("filter", add_link);

	//SET PAGE NAV AT 1
	set_nav_page(section, 1);
	

	clear_object_content(element.parentNode);
	localXML[section] = null;
	
  
  load_datas(element, get_element_add_link(element) );
  
}

function listing_ergo_sort(section, currObj, evt){

  var element = document.getElementById( section );	  
  var page_sort = parseInt(element.getAttribute("page_sort"));
  var parent_table = get_parent_lister( element );
  var index = currObj.cellIndex;

  if(empty(parent_table))
    return;

	

	if( typeof(evt) == "undefined" || evt == null || evt == "null" )
	  evt = window.event;
		
	var fields = element.getAttribute("columns_currently").split(",");
	var field = fields[index];
	
	var sorting = element.getAttribute("sorting");
	
	if(!empty(sorting))
		sorting = sorting.split(",");
	else
		sorting = new Array();
	
	var secondaryClick = false;
	  
	if(evt.ctrlKey)
		secondaryClick = true;
		
	if(evt.altKey)
		secondaryClick = true;
	
	if(evt.shiftKey)
		secondaryClick = true;
	
	
	//RETURN: ONLY ALLOW main click
	if(secondaryClick && page_sort > 1)
	  return;
	
	//LOADER
	vis_area_title_loader_show(element, "Trie: ");


	if(!secondaryClick){
	
	
		if(sorting[0] != field){
		
			sorting[0] = field;
			sorting[1] = 1;
		}else{
		
		if(parseInt(sorting[1]) == 1)
			sorting[1] = 0;
		else
			sorting[1] = 1;
		}
		
		sorting[2] = "";
		sorting[3] = "";
	
	}else{
	
		if(empty(sorting[0])){
		
			sorting[0] = field;
			sorting[1] = 1;
		
		}else if(sorting[0] != field){
		
			if(sorting[2] != field){
			
				sorting[2] = field;
				sorting[3] = 1;
			}else{
			
				if(parseInt(sorting[3]) == 1)
				  sorting[3] = 0;
				else
				  sorting[3] = 1;
			}
		}
	  
	
	}
	
	
	//CLEAN SORTERS
	var i, j, cell, cn;
	var row = currObj.parentNode;
	var restricted = new Array("main_sorter_up","main_sorter_down","secondary_sorter_up","secondary_sorter_down");
	
	for(i = 0; i < row.cells.length; i++){
	
		cell = row.cells[i];
		cn = cell.className;
		cn = cn.split(" ");
		
		for(j = 0; j < cn.length; j++){
		
		if( in_array(cn[j], restricted))
		  cn[j] = "";
		}
		
		cell.className = cn.join(" ");
	}
	
	//DRAW SORTERS
	for(i = 0; i < fields.length; i++){
	
		cell = row.cells[i];
		field = fields[i];
		
		if(sorting[0] == field){
			if(parseInt(sorting[1]) == 1)
				cell.className += " main_sorter_up";
			else
				cell.className += " main_sorter_down";
			
		}else if(sorting[2] == field){
		
			if(parseInt(sorting[3]) == 1)
				cell.className += " secondary_sorter_up";
			else
				cell.className += " secondary_sorter_down";
		}
	}
	
	
	clear_object_content(element.parentNode);
	localXML[section] = null;
	element.setAttribute("sorting", sorting.join(",") );
		
	
	//EXEC REQUEST
	load_datas(element, get_element_add_link(element) );
  
}

function listing_ergo_filter_loaded(remote_object){

	var section = remote_object.id;
	listing_nav_sync( section );
	
	vis_area_title_loader_hide(remote_object);
  
	//DETERMINE IF NO RECORDS
	var xml_name = get_xml_name( remote_object );

	//NO XML_NAME SET
	if(empty(xml_name))
	  return;
	
	recordsXML = localXML[ xml_name ];
	
	var nb_records = parseInt( recordsXML.getElementsByTagName("records")[0].getAttribute("nb_records") );
	if(nb_records == 0){
	  
	  var columns_currently = remote_object.getAttribute("columns_currently");
	  columns_currently = columns_currently.split(",");
	  var nb_fields = columns_currently.length;
	  var empty_item;
	  
	  if(remote_object.tagName.toUpperCase() == "TR"){

	  	empty_item = remote_object.parentNode.insertRow(remote_object.parentNode.rows.length);
		empty_item = empty_item.insertCell(0);
		empty_item.colSpan = nb_fields;
		empty_item.innerHTML = "<span class='listing_ergo_filter_no_result'> Aucun r&eacute;sultats </span>";
		
	  }else if(remote_object.tagName.toUpperCase() == "LI"){
	  
	  	empty_item = document.createElement("li");
		empty_item.innerHTML = "<em> Aucun r&eacute;sultats </em>";
		remote_object.parentNode.appendChild(empty_item);
	  
	  }
	  
	}
  
}

function listing_ergo_filter_multiselect(section, field){

	var dynamic_field = document.getElementById('dynamic_search_filter_' + field);
	dynamic_field = dynamic_field.value;
	dynamic_field = dynamic_field.split(",");
	
	var element = document.getElementById( section );
	sel_values = element.getAttribute("field_" + field).split(",");
	sel_values.shift();

	var lister = get_parent_lister( element );
	var i, opt_value, span, opt_text, tmp_sel_value, index

    var field_name = "";
	
	fields = element.getAttribute("columns_currently").split(",");
	var row = lister.rows[0];
	for(i = 0; i < fields.length; i++){
	  if(fields[i] == field){
	    field_name += row.cells[i].innerHTML;
	    break;
	  }
	}

	var main_container = document.createElement("div");

	main_container.style.width = "400px";
	main_container.style.backgroundColor = "transparent";
	main_container.id = "main_container";
	
	var myForm = document.createElement("form");
  
	var myCtrl = document.createElement("input");
	myCtrl.type = "hidden";
	myCtrl.name = "is_saved";
	myCtrl.value = "0";
	myCtrl.id = "multiselect_box_form";
	myForm.appendChild( myCtrl );
	
	myCtrl = document.createElement("input");
	myCtrl.type = "hidden";
	myCtrl.name = "is_changed";
	myCtrl.value = "0";
	myForm.appendChild( myCtrl );
	
	myCtrl = document.createElement("input");
	myCtrl.type = "hidden";
	myCtrl.name = "section";
	myCtrl.value = section;
	myForm.appendChild( myCtrl );
	
	myCtrl = document.createElement("input");
	myCtrl.type = "hidden";
	myCtrl.name = "field";
	myCtrl.value = field;
	myForm.appendChild( myCtrl );
	
	var div_vis_area = document.createElement("div");
	div_vis_area.className = "vis_area listing_ergo_multiselect_box";
	div_vis_area.style.width = "392px";
	myForm.appendChild( div_vis_area );
	
	var div_vis_area_title = document.createElement("div");
	div_vis_area_title.className = "vis_area_title";
	div_vis_area_title.innerHTML = "Choix d'items: " + field_name;
	div_vis_area.appendChild( div_vis_area_title );
	
	var div_vis_area_content = document.createElement("div");
	div_vis_area_content.className = "vis_area_content";
	div_vis_area.appendChild( div_vis_area_content );
  
  
 
	for(i = 0; i < sel_values.length; i++){
		tmp_sel_value =  sel_values[i].split("@");
		
		span = document.createElement("span");
		span.className = "listing_ergo_multiselect_item";
		
		
		opt_value = document.createElement("input");
		opt_value.type = "checkbox";
		opt_value.value = tmp_sel_value[0];
		opt_value.name = "items[]";
		opt_value.id = "item_" + tmp_sel_value[0];
		
		if(in_array(tmp_sel_value[0], dynamic_field))
			opt_value.checked = true;
		
		
		span.appendChild( opt_value );
		
		opt_text = document.createElement("label");
		opt_text.type = "checkbox";
		opt_text.htmlFor = "item_" + tmp_sel_value[0];
		opt_text.innerHTML = tmp_sel_value[1];
		
		span.appendChild( opt_text );
		
		
		div_vis_area_content.appendChild(span);
	}
  
	span = document.createElement("span");
	span.className = "listing_ergo_multiselect_buttons";
	span.innerHTML = "<span  onclick=\"JAVASCRIPT: close_this_sub_form(this );\" onmouseover=\"JAVASCRIPT: btn_over(this, 'btn_f_cancel_o');\" onmouseout=\"JAVASCRIPT: btn_out(this, 'btn_f_cancel');\"  style=\"cursor:pointer;  display:inline-block; \" title=\"Annuler\"> <img src=\"/images/design/core/btn_f_cancel.png\" alt=\"Annuler\" title=\"Annuler\" style=\"float:left;clear:none;\" /><div style=\"margin-top:8px;float:left;clear:none;\" >Annuler</div></span>";
							
	span.innerHTML += "<span  onclick=\"JAVASCRIPT: listing_ergo_filter_multiselect_choose(this);\" onmouseover=\"JAVASCRIPT: btn_over(this, 'btn_f_enregistrer_o');\" onmouseout=\"JAVASCRIPT: btn_out(this, 'btn_f_enregistrer');\"  style=\"cursor:pointer;  display:inline-block; \" title=\"Choisir ces items\"> <img src=\"/images/design/core/btn_f_enregistrer.png\" alt=\"Choisir ces items\" title=\"Choisir\" style=\"float:left;clear:none;\" /><div style=\"margin-top:8px;float:left;clear:none;\" >Choisir</div></span>";
								
	div_vis_area_content.appendChild(span);
	
	main_container.appendChild(myForm);
	
	bring_center_form(document.body, main_container);
}

function listing_ergo_filter_multiselect_choose(currObj){

	var myForm = recursive_form_find(currObj);
	var inputs = myForm.getElementsByTagName("input");
	var is_saved_control = null;
	var i, inp;

	var result = new Array();
	
	for(i=0; i < inputs.length; i++){

		inp = inputs[i];
		
		if(inp.name.toUpperCase() == "FIELD"){
			var field = inp.value;		
		}else if(inp.name.toUpperCase() == "SECTION"){
			var section = inp.value;
		}else if(inp.name.toUpperCase() == "ITEMS[]"){
			if( !empty(inp.checked))
			    result[result.length] = inp.value;
		}else if(inp.name.toUpperCase() == "IS_SAVED"){
			is_saved_control = inp;
		}

	}

	result = result.join(",");
	
	var dynamic_field = document.getElementById('dynamic_search_filter_' + field);
	dynamic_field.value = result;

	if(!empty(is_saved_control)){
		is_saved_control.value = 1;
		close_this_sub_form( is_saved_control );
	}else
		close_this_sub_form( currObj );
}

function listing_nav_sync( section ){

	var element = document.getElementById(section);
	var top_info = document.getElementById("top_info_" + section);
	var nb_items_per_page_control = top_info.getElementsByTagName("select")[0];
	
	var xml_name = get_xml_name( element );

	//NO XML_NAME SET
	if(empty(xml_name))
	  return;

	recordsXML = localXML[ xml_name ];

    var nb_records = recordsXML.getElementsByTagName("records")[0].getAttribute("nb_records");

	var nb_records_per_pages = nb_items_per_page_control.options[nb_items_per_page_control.selectedIndex].value;
	var nb_pages = Math.ceil(nb_records / nb_records_per_pages) ;
	if(nb_pages <= 0)
	  nb_pages = 1;

	var spans = top_info.getElementsByTagName("span");
	var i;
	
	for(i = 0; i < spans.length; i++){
	  if(spans[i].id == "nb_records")
	    spans[i].innerHTML = nb_records;
	  else if(spans[i].id == "nb_pages")
	    spans[i].innerHTML = nb_pages;
	}
}

function vis_area_title_loader_show(currObj, text){

	vis_area_title_loader_hide(currObj);

	if(empty(text))
		text = "Chargement: ";
	
	var title_object = get_vis_area_title(currObj);
	
	title_object.innerHTML += "<span>" + text+ " <img src='/images/design/vis_area_title_loader.gif' alt='loading image' /></span>";
}

function vis_area_title_loader_hide(currObj){

	var title_object = get_vis_area_title(currObj);
	var span = title_object.getElementsByTagName("span")[0];

	if(!empty(span))
		title_object.removeChild( span );
}

function get_parent_lister(currObj){

  var found = false;
  var max_itteration = 0;
  var tmp = currObj;
	  
  while(!found){
  
	tmp = tmp.parentNode;

	//- SEARCH FOR PARENT TABLE	
	if(currObj.tagName.toUpperCase() == "TR"){
		if(tmp.tagName.toUpperCase() == "TABLE"){
		  found = true;
		  break;
		}

	//- SEARCH FOR PARENT UL
	}else if(currObj.tagName.toUpperCase() == "LI"){
		if(tmp.tagName.toUpperCase() == "UL"){
		  found = true;
		  break;
		}
	}
  
	max_itteration++;
	if(max_itteration > 5)
	  break;
  }

  if(found)
    return tmp;
  else
    return null;

}

function get_vis_area_title(currObj){

  var found = false;
  var max_itteration = 0;
  var tmp = currObj;
  
  while(!found){
  
	tmp = tmp.parentNode;

	//- SEARCH FOR PARENT TABLE	
	if(tmp.tagName.toUpperCase() == "DIV"){
		if(tmp.className.toUpperCase().indexOf("VIS_AREA_CONTENT") > -1){
		  found = true;
		  break;
		}

	}

	max_itteration++;
	if(max_itteration > 10)
	  break;
  }


  if(!found)
    return null;
	

  //GET VIS AREA (TOPMOST DIV)
  tmp = tmp.parentNode;
  found = false;
  var i, div;
  var divs = tmp.getElementsByTagName("div");
  
  for(i = 0; i < divs.length; i++){
	div = divs[i];
    if(div.className.toUpperCase().indexOf("VIS_AREA_TITLE") > -1){
		
		found = true;
		break;
	}
  }
  

  if(found)
    return div;
  else
    return null;

}

function get_nav_link_add( section ){

	var element = document.getElementById(section);
	var top_info = document.getElementById("top_info_" + section);
	var nb_items_per_page_control = top_info.getElementsByTagName("select")[0];
	var nb_items_per_page = nb_items_per_page_control.options[nb_items_per_page_control.selectedIndex].value
	var page_control = top_info.getElementsByTagName("input");

	var i;

	for(i = 0; i < page_control.length; i++){

	  if(page_control[i].type.toUpperCase() == "TEXT"){
	  
	    page_control = page_control[i];
	    break;
	  }
	    
	}

	var page = page_control.value;

	return "&pi=" + page + "&ni=" + nb_items_per_page;
}

function get_element_add_link(element){

	var add_link = "";

	//LINK: FILTER
	var add_link_filter =  element.getAttribute("filter");
	if(!empty(add_link_filter))
		add_link += add_link_filter;

	//LINK: SORTING
	var add_link_sorting =  element.getAttribute("sorting");
	if(!empty(add_link_sorting))
	    add_link += "&sorting=" + add_link_sorting;
	
	//LINK: NAV
	add_link += get_nav_link_add( element.id );

	return add_link;
}

function get_xml_name( element ){

	var data_provider = element.getAttribute("data_provider").split(";");
	var i, tmp;
	var found = false;

	for(i = 0; i < data_provider.length; i++){

	  tmp = data_provider[i].split(":");

	  if(tmp[0] == "xml_name"){
	    tmp = tmp[1];
	    found = true;
	    break;
	  }
	  
	}
	

	if(!found)
	  return null;
	else
	  return tmp;
}

function set_nav_page(section, page){
	//SET PAGE NAV
	var top_info = document.getElementById("top_info_" + section);
	var page_control = top_info.getElementsByTagName("input");

	for(i = 0; i < page_control.length; i++){

	  if(page_control[i].type.toUpperCase() == "TEXT"){
	  
	    page_control = page_control[i];
	    break;
	  }
	    
	}
	
	page_control.value = page;
}

//--------------------------------- KEYWORD TOOLS

var hilighted_keyword = '';
var hilighted_index = null;
var last_pattern_asked = '';

function keywording_check_type(event_object, pattern_control_id, list_choice_id, list_added_id, data_transport){

	cancel_bubble( event_object );
	
	var list_choice_control = document.getElementById( list_choice_id );
	var pattern_control = document.getElementById( pattern_control_id );

	var choices_coll = list_choice_control.getElementsByTagName("li");


	if(event_object)
		var keyCode = event_object.keyCode;
	else
		var keyCode = event.keyCode;
	
	switch(keyCode){
		case 13: //ENTER
			if(hilighted_index < choices_coll.length)
				keywording_add(choices_coll[hilighted_index].name, choices_coll[hilighted_index].innerHTML, pattern_control_id, list_choice_id, list_added_id)
			else
				keywording_add(0, pattern_control.value, pattern_control_id, list_choice_id, list_added_id);
		break;
		
		case 27: //ESCAPE

			keywording_clear(pattern_control_id, list_choice_id);
		break;
		
		case 38: //TOP ARROW

			if(hilighted_index > 0){
				choices_coll[ hilighted_index ].className = "keywords_selection_none";
				hilighted_index--;
				choices_coll[ hilighted_index ].className = "keywords_selection_none";
				hilighted_keyword = choices_coll[ hilighted_index ].innerHTML;
			}
		break;

		case 40: //BOTTOM ARROW

			if(hilighted_index < (choices_coll.length - 1)){
				choices_coll[ hilighted_index ].className = "keywords_selection_none";
				hilighted_index++;
				choices_coll[ hilighted_index ].className = "keywords_selection_selected";
				hilighted_keyword = choices_coll[ hilighted_index ].innerHTML;
			}
		break;
		
		case 37: // LEFT ARROW
		case 39: // RIGHT ARROW
			// USE FOR MOVING CURSOR
		break;

		default: //KEYTYPE
			//SEARCH
			keywording_search(pattern_control_id, list_choice_id, list_added_id);
		break;		
	}

	
	// return false;
}

function keywording_add(id, text, pattern_control_id, list_choice_id, list_added_id){

	var data_transport = document.getElementById(pattern_control_id + '_td').value;
	var remote_variable = document.getElementById(pattern_control_id + '_kv').value;
	
	eval( "var remote_id = " + remote_variable +  ";" );

	
	var url = "/page_module.php?tZ=1328756736&page=keywords_thesaurus_add&module=system&a=page&sid=";
		url += "&pc=" + pattern_control_id;
		url += "&lc=" + list_choice_id;
		url += "&la=" + list_added_id;
		
		url += "&dt=" + data_transport;
		url += "&idk=" + id;
		url += "&txt=" + text;
		url += "&rid=" + remote_id;

		var Ar = new Ajax(url, "GET");
		Ar.onComplete = keywording_add_receive;
		Ar.send();
}

function keywording_add_receive(responseText, responseXML){

	var pattern_control_id = responseXML.getElementsByTagName('pc')[0].firstChild.nodeValue;
	var list_choice_id = responseXML.getElementsByTagName('lc')[0].firstChild.nodeValue;
	var list_added_id = responseXML.getElementsByTagName('la')[0].firstChild.nodeValue;

	var kwt_id = responseXML.getElementsByTagName('idk')[0].firstChild.nodeValue;
	var kwt_text = responseXML.getElementsByTagName('kwt')[0].firstChild.nodeValue;
	

	keywording_clear(pattern_control_id, list_choice_id);
	
	var list_added_control = document.getElementById(list_added_id);

	var listItem = document.createElement("LI");
	listItem.innerHTML = keywording_item_html(kwt_id, kwt_text, pattern_control_id, list_choice_id, list_added_id);
	listItem.name = kwt_id;
	listItem.setAttribute("name", kwt_id);

	list_added_control.appendChild( listItem );
}

function keywording_populate(pattern_control_id, list_choice_id, list_added_id){
	
	var data_transport = document.getElementById(pattern_control_id + '_td').value;
	var remote_variable = document.getElementById(pattern_control_id + '_kv').value;
	
	eval( "var remote_id = " + remote_variable +  ";" );
	if(remote_id > 0){
		var url = "/page_module.php?tZ=1328756736&page=keywords_thesaurus_get&module=system&a=page&sid=";
		url += "&pc=" + pattern_control_id;
		url += "&lc=" + list_choice_id;
		url += "&la=" + list_added_id;
		
		url += "&dt=" + data_transport;
		url += "&rid=" + remote_id;
		
		
		var Ar = new Ajax(url, "GET");
		Ar.onComplete = keywording_populate_receive;
		Ar.send();
	}	
}

function keywording_populate_receive(responseText, responseXML){

	var pattern_control_id = responseXML.getElementsByTagName('pc')[0].firstChild.nodeValue;
	var list_choice_id = responseXML.getElementsByTagName('lc')[0].firstChild.nodeValue;
	var list_added_id = responseXML.getElementsByTagName('la')[0].firstChild.nodeValue;

	var records = responseXML.getElementsByTagName('record');
	
	var i, listItem, kwt_id, kwt_text;
	
	var list_added_control = document.getElementById( list_added_id );
	clear_object_content( list_added_control );

	if(records != null ){
		var found = false;

		for(i = 0; i < records.length; i++){
		
			kwt_id = records[i].getElementsByTagName("id_keywords")[0].firstChild.nodeValue;
			kwt_text = records[i].getElementsByTagName("text_")[0].firstChild.nodeValue;
			
			listItem = document.createElement("LI");
			listItem.innerHTML = keywording_item_html(kwt_id, kwt_text, pattern_control_id, list_choice_id, list_added_id);
			listItem.name = kwt_id;
			listItem.setAttribute("name", kwt_id);

			list_added_control.appendChild( listItem );
	
		}
	}
}

function keywording_item_html(id_keywords, text,  pattern_control_id, list_choice_id, list_added_id){

	var html_content = "";

	html_content = "<span class=\"keywords_added_term\">" + text + "</span>";
	html_content += "<span class=\"keywords_added_remover\" onclick=\"JAVASCRIPT:keywording_del(" + id_keywords;
	
	html_content +=  ", '"+pattern_control_id+"','"+list_choice_id+"','"+list_added_id+"');\">x</span>";
	return html_content;
}

function keywording_del(id, pattern_control_id, list_choice_id, list_added_id){

	var data_transport = document.getElementById(pattern_control_id + '_td').value;
	var remote_variable = document.getElementById(pattern_control_id + '_kv').value;
	
	eval( "var remote_id = " + remote_variable +  ";" );
	
	
	var url = "/page_module.php?tZ=1328756736&page=keywords_thesaurus_del&module=system&a=page&sid=";
	url += "&pc=" + pattern_control_id;
	url += "&lc=" + list_choice_id;
	url += "&la=" + list_added_id;
	

	url += "&dt=" + data_transport;
	url += "&idk=" + id;
	url += "&rid=" + remote_id;
	
	var Ar = new Ajax(url, "GET");
	Ar.onComplete = keywording_del_receive;
	Ar.send();
}

function keywording_del_receive(responseText, responseXML){

	var pattern_control_id = responseXML.getElementsByTagName('pc')[0].firstChild.nodeValue;
	var list_choice_id = responseXML.getElementsByTagName('lc')[0].firstChild.nodeValue;
	var list_added_id = responseXML.getElementsByTagName('la')[0].firstChild.nodeValue;

	var kwt_id = parseInt(responseXML.getElementsByTagName('idk')[0].firstChild.nodeValue);


	var list_added_control = document.getElementById(list_added_id);

	
	var coll = list_added_control.getElementsByTagName("li");
	var i, val;
	
	for(i = 0; i < coll.length; i++){
		val = parseInt( coll[i].getAttribute("name") );

		if(val == kwt_id){
			list_added_control.removeChild(coll[i]);
			break;
		}
	}
}

function keywording_clear(pattern_control_id, list_choice_id){

	var list_choice_control = document.getElementById( list_choice_id );
	var pattern_control = document.getElementById( pattern_control_id );

	last_pattern_asked = '';
	pattern_control.value = '';

	clear_object_content(list_choice_control);
	list_choice_control.style.display = "none";
				
	hilighted_keyword = '';
	hilighted_index = null;
}

function keywording_search(pattern_control_id, list_choice_id, list_added_id){

	var pattern_object = document.getElementById( pattern_control_id);
	
	if(pattern_object.value != last_pattern_asked){
	
		var url = "/page_module.php?tZ=1328756736&page=keywords_thesaurus&module=system&a=page&sid=";
		url += "&pa=" + pattern_object.value;
		url += "&pc=" + pattern_control_id;
		url += "&lc=" + list_choice_id;
		url += "&la=" + list_added_id;
		
		var Ar = new Ajax(url, "GET");
		Ar.onComplete = keywording_search_receive;
		Ar.send();
		last_pattern_asked = pattern_object.value;
	}
}

function keywording_search_receive(responseText, responseXML){
	
	var pattern_control_id = responseXML.getElementsByTagName('pc')[0].firstChild.nodeValue;
	var list_choice_id = responseXML.getElementsByTagName('lc')[0].firstChild.nodeValue;
	var list_added_id = responseXML.getElementsByTagName('la')[0].firstChild.nodeValue;
	
	var records = responseXML.getElementsByTagName('record');

	
	var i, listItem, kwt_id, kwt_text;
	
	var list_choice_control = document.getElementById( list_choice_id );
	clear_object_content( list_choice_control );

	if(records != null ){
		var found = false;

		for(i = 0; i < records.length; i++){
		
			kwt_id = records[i].getElementsByTagName("id_keywords")[0].firstChild.nodeValue;
			kwt_text = records[i].getElementsByTagName("text_")[0].firstChild.nodeValue;
	
			listItem = document.createElement("LI");
			listItem.innerHTML = kwt_text;
			listItem.name = kwt_id;
			
			if(hilighted_keyword == ''){
				hilighted_keyword = kwt_text;
				hilighted_index = i;
			}
			
			if(hilighted_keyword == kwt_text){
				listItem.className = "keywords_selection_selected";
				hilighted_index = i;
				found = true;
			}
			
			listItem.style.textAlign = "left";
			
			listItem.onclick = keywording_attach_function(kwt_id, kwt_text, pattern_control_id, list_choice_id, list_added_id);
			list_choice_control.appendChild( listItem );
		}
		

		if(records.length >= 1){
			list_choice_control.style.display = "inline";

			if(!found){
				hilighted_index = 0;
				list_choice_control.getElementsByTagName("li")[ hilighted_index ].className = "keywords_selection_selected";
				hilighted_keyword = choices_coll[ hilighted_index ].innerHTML;
				
			}

		}else
			list_choice_control.style.display = "none";

	}else
		list_choice_control.style.display = "none";

}

function keywording_attach_function(id, text, pattern_control_id, list_choice_id, list_added_id){

	return function(){ keywording_add(id, text, pattern_control_id, list_choice_id, list_added_id); };
}


//--------------------------------- DYNAMIC EVENTS CALENDAR      
var reloading_calendar = null;
function events_calendar_load(year, month, id_cat, currObj, request_url){

	reloading_calendar = currObj.parentNode.parentNode.parentNode.parentNode;
	
	if(reloading_calendar.tagName.toLowerCase() == "table")
	  reloading_calendar = reloading_calendar.parentNode;
	  
	var url = "http://www.centrevillemagog.ca/data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&module=events&a=entry_calendar&sid=";
	
	url += "&y=" + year;             
	url += "&m=" + month;            
	url += "&id_events_category=" + id_cat;
	url += "&request_url=" + escape(request_url);
	
	var Ar = new Ajax(url, "GET");
	Ar.onComplete = events_calendar_reloaded;
	Ar.send();
}

function events_calendar_reloaded(responseText, responseXML){

	if(reloading_calendar != null){
		var html_content = responseXML.getElementsByTagName("html_content")[0].firstChild.nodeValue;
		clear_object_content( reloading_calendar );
		reloading_calendar.innerHTML = html_content;
	}

	reloading_calendar = null;
}

function events_calendar_reloaded(responseText, responseXML){

	if(reloading_calendar != null){
		var html_content = responseXML.getElementsByTagName("html_content")[0].firstChild.nodeValue;
		clear_object_content( reloading_calendar );
		reloading_calendar.innerHTML = html_content;
	}
	
	reloading_calendar = null;
}

function events_calendar_view(year, month, day, id_cat, request_url){

	if(typeof(request_url) == "undefined")
		request_url = "";
		

	if(request_url.length <= 0){
	
		var url = "/evenements-a-magog.php?tZ=1328756736";
		
	}else
		var url = request_url;
		
		
		url += "&y=" + year;
		url += "&m=" + month;
		url += "&d=" + day;
		url += "&id_cat=" + id_cat;
		
		document.location = url;

}//JAVASCRIPT DOCUMENT: auth.js.php
var auth_in_progress = "";
function check_auth(form_id){
	auth_in_progress = form_id;
	
	var my_form = document.getElementById(form_id);
	var url_auth = "http://www.centrevillemagog.ca/data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&module=security&a=user_auth";
	
	var myAjax = new Ajax(url_auth, 'POST');
	
	myAjax.onComplete = receive_auth;
	var username = my_form.elements.namedItem('lusername').value;
	var userpass = my_form.elements.namedItem('luserpass').value;
	
	userpass = hex_sha1(userpass);
	var qs = "&username=" + username + "&userpass=" + userpass;
	
	myAjax.send(qs);

}


function receive_auth(responseText, responseXML){

	var my_form = document.getElementById(auth_in_progress);
	auth_in_progress = "";
	
	var global = responseXML.getElementsByTagName("GLOBAL")[0];
	
	var status = global.getElementsByTagName("status")[0].childNodes[0].nodeValue;
	var description = global.getElementsByTagName("description")[0].childNodes[0].nodeValue;
	var i;
	
	if(status == "OK"){
		var cookie_send = global.getElementsByTagName("cookie");
		
		var sid = global.getElementsByTagName("sid")[0].firstChild.nodeValue;
		
		for(i = 0; i < cookie_send.length; i++){
			document.cookie = cookie_send[i].childNodes[0].nodeValue;
		}
		

		document.location = "/main.php?sid=" + sid;

	}else{

		var code = parseInt( global.getElementsByTagName("code")[0].childNodes[0].nodeValue );
		send_auth_error( code );
	}

}

var is_showing_auth_error = false;

function send_auth_error( code ){
	if(!is_showing_auth_error){

			var btn = document.getElementById('btnCheckAuth');
      if (btn != null)
      {
	  			if(code == 3)
		  			var fail_message = "L'utilisateur doit etre confirme, veuillez verifier vos courriel. ";
				else if(code == 2)
					var fail_message = "Le nom d'utilisateur et le mot de passe sont requis. ";
				else
					var fail_message = "Le nom d'utilisateur ou le mot de passe que vous avez saisi est incorrecte. ";

				var error_content = callout_helper_content_error("Attention", fail_message, new Array());
				show_callout(error_content, btn, 25, 15, 300, 120,0, false, null, false, 'bl');
			}
  }
}


function auth_error_close(){
	is_showing_auth_error = false;
}


function key_type(event_object, obj){

	if(event_object)
		var keyCode = event_object.keyCode;
	else
		var keyCode = event.keyCode;

	if(keyCode == 13){

		check_auth(obj.form.id);
	}

}//JAVASCRIPT DOCUMENT: auth.js.php

function show_help(topic){
	if(!topic)
		topic = '0';
	
	var tmp = topic.split("_");
	
	if(tmp.length >1){
		last_opened_category = '' + tmp[0];
		last_opened_topic = tmp[0] + '_' + tmp[1];
	}else{
		last_opened_category = '' + tmp[0];
		last_opened_topic = '0';
	}
	var Ar = new Ajax("http://www.centrevillemagog.ca/data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&page=help.admin&module=system&a=page&sid=&topic=" + topic, "GET");
	Ar.onComplete = page_receive;
	Ar.send();
}

var last_opened_category = '0';

function help_open_category(index){

	var category_panel, category_menu;
	
	if( last_opened_category != null){
		category_panel = document.getElementById('category_panel_' + last_opened_category);
		category_panel.style.display = "none";
	}
	
	if(last_opened_category != '0' && last_opened_category != null){

		category_menu = document.getElementById('category_menu_' + last_opened_category);
		category_menu.style.display = "none";
		category_menu.parentNode.getElementsByTagName("span")[0].style.backgroundColor = "";
	}
	
	if(last_opened_topic != '0'){

		var topic_panel = document.getElementById('topic_panel_' + last_opened_topic);
		topic_panel.style.display = "none";	
		var topic_menu = document.getElementById('topic_menu_' + last_opened_topic);
		topic_menu.getElementsByTagName("span")[0].style.backgroundColor = "";
	}
	
	category_panel = document.getElementById('category_panel_' + index);
	category_panel.style.display = "block";
	
	if(index != '0' ){
		category_menu = document.getElementById('category_menu_' + index);
		category_menu.style.display = "block";

		category_menu.parentNode.getElementsByTagName("span")[0].style.backgroundColor = "#9999FF";
	}
		
	last_opened_category = index;
}

var last_opened_topic = '0';

function help_open_topic(index, e){

	if(last_opened_category != null){

		var category_panel = document.getElementById('category_panel_' + last_opened_category);
		category_panel.style.display = "none";
	}

	if(last_opened_topic != '0'){
		var topic_panel = document.getElementById('topic_panel_' + last_opened_topic);
		topic_panel.style.display = "none";	
		
		var topic_menu = document.getElementById('topic_menu_' + last_opened_topic);
		topic_menu.getElementsByTagName("span")[0].style.backgroundColor = "";

	}

	var topic_panel = document.getElementById('topic_panel_' + index);
	topic_panel.style.display = "block";	
	
	var topic_menu = document.getElementById('topic_menu_' + index);
	topic_menu.getElementsByTagName("span")[0].style.backgroundColor = "#9999FF";
	
	last_opened_topic = index;
	if(e != null)
		cancel_bubble(e);
}
//JAVASCRIPT DOCUMENT: image_bank.js.php

function show_image_bank_form(){
	var Ar = new Ajax("http://www.centrevillemagog.ca/data_provider/module_softwares.php?tZ=1328756736&dp=module_loader&page=image_bank_box&module=system&a=page", "GET");
	Ar.onComplete = page_receive;
	Ar.send();
	
}

var old_zone = null;

function show_zone(zone_id, old_zone_id){
	var main_table = document.getElementById('fc_main_container');
	main_table = main_table.getElementsByTagName("table")[0];
	var tables = main_table.getElementsByTagName("table");
	var i;
	var old_zone_ref, zone;
	
	for(i = 0; i < tables.length; i++){
	
		if( tables[i].id == zone_id )
			if( tables[i] )
				zone = tables[i];

		if(tables[i].id == old_zone_id)
			old_zone_ref = tables[i];
	}

	if(zone){
		
		if(old_zone != null ){
			old_zone.style.display = "none";
		}else{
			old_zone = old_zone_ref;
			old_zone.style.display = "none";
		}
		
		zone.style.display = "block";
		old_zone = zone;
	}

}


var crop_tool_forced_ratio_available = true;
var crop_tool_image_width = 0;
var crop_tool_image_height = 0;
var crop_tool_scale_ratio = 0;
var crop_tool_posA = new Array(0,0);
var crop_tool_posB = new Array(0,0);
var crop_tool_posA_or = new Array(0,0);
var crop_tool_posB_or = new Array(0,0);
var crop_tool_pos_or = new Array();

var crop_tool_resizing = false;
var crop_tool_moving = false;
var crop_tool_handle_resize = -1;
var crop_tool_forced_ratio = 0;

var crop_tool_min_width = 0;
var crop_tool_min_height = 0;


function toggle_image_crop(currObj, image_source, image_save_url, ratio_x, ratio_y, crop_tool_min_width, crop_tool_min_height){

	if(typeof(type) == "undefined")
		type = "photo_album"

	var i;
	var container = document.getElementById('image_show');
	var crop_image_id;

	var crop_tool = document.getElementById('crop_tool');

	var is_opened = false;

	if( typeof(crop_tool) == "object"){

		if(crop_tool != null){

			if(crop_tool.tagName == "DIV")
				is_opened = true;
		}
	}

	if(!is_opened){

		open_loader();

		var image_test = new Image();
		image_test.src = image_source;
		image_test.style.display = "none";
		image_test.id = "image_test";
		image_test.onload = function(){ crop_tool_appear( image_save_url, ratio_x, ratio_y, crop_tool_min_width, crop_tool_min_height); };
		document.body.appendChild(image_test);
		
		

	}

}

function crop_tool_appear( image_save_url, ratio_x, ratio_y, crop_image_id, crop_tool_min_width, crop_tool_min_height){

		close_loader();

		var image_test = document.getElementById('image_test');
		image_source = image_test.src;
		image_width = image_test.width;
		image_height = image_test.height;
		document.body.removeChild( image_test );
	
		var base_left = 24;
		var crop_tool_main = document.createElement("DIV");

		crop_tool_main.style.width = "640px";
		crop_tool_main.style.height = "690px";
		crop_tool_main.style.backgroundColor = "#FFFFFF";
		crop_tool_main.className = "vis_area";
		
		crop_tool_main.innerHTML = "<form><input type=\"hidden\" id=\"form_closer\" />";
		crop_tool_main.innerHTML += "<div class=\"vis_area_title\"> Op&eacute;ration de recadrage </div>";
		crop_tool_main.innerHTML += "<div id=\"crop_tool\" class=\"vis_area_content\" onmouseup=\"crop_tool_up(event)\" onmousemove=\"crop_tool_displace(event);\" style=\"height:592px;padding:0px;\"></div>";

		crop_tool = crop_tool_main.getElementsByTagName("div")[1];


		var image_shown = document.createElement("img");
		image_shown.src = image_source;
		image_shown.style.position = "absolute";
		image_shown.style.zIndex = 1;
		
		var ratio = image_width / image_height;
		

		if(ratio >= 1){
		
			crop_tool_scale_ratio = 592 / image_width;
			var image_height_scaled = parseInt(image_height * crop_tool_scale_ratio);
			var image_width_scaled = 592;
			
			var image_top = ((592 - image_height_scaled) / 2) + 30;
			var image_left = base_left;

		}else{
		
			crop_tool_scale_ratio = 592 / image_height;
			var image_width_scaled = parseInt(image_width * crop_tool_scale_ratio);
			var image_height_scaled = 592;

			var image_left = ((592 - image_width_scaled) / 2) + base_left;
			var image_top = 30;

		}
		
		image_shown.style.width = image_width_scaled + "px";
		image_shown.style.height = image_height_scaled + "px";
		image_shown.style.left = image_left + "px"
		image_shown.style.top = image_top + "px";
		
		crop_tool_image_width = image_width;
		crop_tool_image_height = image_height;
		
		crop_tool.appendChild(image_shown);
		
		crop_tool_table = document.createElement("TABLE");
		crop_tool_table.id = "crop_tool_table";
		
		if(ratio_x > 0 && ratio_y > 0){

			var initial_crop_ratio = ratio_x / ratio_y;

			//DEFINE INITIAL POSITION

			if(initial_crop_ratio - ratio > 0){

				var vertical_adj = image_height_scaled - (image_width_scaled * (ratio_y / ratio_x));
				vertical_adj = vertical_adj / 2;

				crop_tool_posA_or[0] = crop_tool_posA[0] = 0;
				crop_tool_posA_or[1] = crop_tool_posA[1] = vertical_adj;
				
				crop_tool_posB_or[0] = crop_tool_posB[0] = image_width_scaled;
				crop_tool_posB_or[1] = crop_tool_posB[1] = image_height_scaled - vertical_adj;

			}else{

				var horizontal_adj = image_width_scaled - (image_height_scaled * (ratio_x / ratio_y));
				horizontal_adj = horizontal_adj / 2;

				crop_tool_posA_or[0] = crop_tool_posA[0] = horizontal_adj;
				crop_tool_posA_or[1] = crop_tool_posA[1] = 0;
				
				crop_tool_posB_or[0] = crop_tool_posB[0] = image_width_scaled - horizontal_adj;
				crop_tool_posB_or[1] = crop_tool_posB[1] = image_height_scaled;
			
			}

			crop_tool_forced_ratio = initial_crop_ratio;
			crop_tool_forced_ratio_available = false;
		}else{
			
			var initial_crop_ratio = 0.20;
			
			//DEFINE INITIAL POSITION
			crop_tool_posA_or[0] = crop_tool_posA[0] = parseInt(image_width_scaled * initial_crop_ratio );
			crop_tool_posA_or[1] = crop_tool_posA[1] = parseInt(image_height_scaled * initial_crop_ratio );
			
			crop_tool_posB_or[0] = crop_tool_posB[0] = image_width_scaled - crop_tool_posA[0];
			crop_tool_posB_or[1] = crop_tool_posB[1] = image_height_scaled - crop_tool_posA[1];
		}
		

		crop_tool_table.style.width = image_width_scaled + "px";
		crop_tool_table.style.height = image_height_scaled + "px";
		crop_tool_table.cellPadding = "0px";
		crop_tool_table.cellSpacing = "0px";
		
		
		crop_tool_table.style.position = "absolute";
		crop_tool_table.style.top = image_top + "px";
		crop_tool_table.style.left = image_left + "px";
		crop_tool_table.style.zIndex = 3;
		
		var row = crop_tool_table.insertRow(0);
		
		var cell = row.insertCell(0);
		cell.className = "crop_tool_dark";
		cell.style.width = crop_tool_posA[0] + "px";
		cell.style.height = crop_tool_posA[1] + "px";
		cell.innerHTML = "<!-- CT -->";
		
		cell = row.insertCell(1);
		cell.className = "crop_tool_dark";
		cell.style.width = (crop_tool_posB[0] - crop_tool_posA[0]) + "px";
		cell.style.height = crop_tool_posA[1] + "px";
		cell.innerHTML = "<!-- CT -->";
		
		cell = row.insertCell(2);
		cell.className = "crop_tool_dark";
		cell.style.width = crop_tool_posA[0] + "px";
		cell.style.height = crop_tool_posA[1] + "px";
		cell.innerHTML = "<!-- CT -->";
		
		row = crop_tool_table.insertRow(1);
		
		cell = row.insertCell(0);
		cell.className = "crop_tool_dark";
		cell.style.width = crop_tool_posA[0] + "px";
		cell.style.height = (crop_tool_posB[1] - crop_tool_posA[1]) + "px";
		cell.innerHTML = "<!-- CT -->";
		
		cell = row.insertCell(1);

			var handle2, ht_cell, ht_row, h;

			cell.innerHTML = "<div onmousedown=\"JAVASCRIPT:crop_move(this, event); \"></div>";
			var div_handle_table = cell.firstChild;
			
			var handle_table = document.createElement("TABLE");
			div_handle_table.appendChild( handle_table ); 
			
			handle_table.cellPadding = "0px";
			handle_table.cellSpacing = "0px";
			//crop_move(handle_table, evt);
			
			div_handle_table.onclick = function(){ alert('DAMN!'); };
			
			div_handle_table.style.height = "100%";
			

			handle_table.style.cursor = "move";
			
			handle_table.style.width = "100%";
			handle_table.style.height = "100%";
			handle_table.style.zIndex = 4;
			//ROW 0
			ht_row = handle_table.insertRow(0);
			ht_row.height = "33%";

				//CELL 0
				ht_cell = ht_row.insertCell(0);
				ht_cell.style.verticalAlign = "top";
				ht_cell.align = "left";
				h = "<div class=\"crop_handle\" style=\"cursor:nw-resize;\" onmousedown=\"JAVASCRIPT: crop_resize(this, event, 0 ); cancel_bubble(event);\"></div>";
				ht_cell.innerHTML = h;

				//CELL 1
				ht_cell = ht_row.insertCell(1);
				ht_cell.style.verticalAlign = "top";
				ht_cell.align = "center";
				h = "<div class=\"crop_handle\" style=\"cursor:n-resize;\" onmousedown=\"JAVASCRIPT:crop_resize(this, event, 1 );cancel_bubble(event); \"></div>";
				ht_cell.innerHTML = h;

				//CELL 2
				ht_cell = ht_row.insertCell(2);
				ht_cell.style.verticalAlign = "top";
				ht_cell.align = "right";
				h = "<div class=\"crop_handle\" style=\"cursor:ne-resize;\" onmousedown=\"JAVASCRIPT:crop_resize(this, event, 2 );cancel_bubble(event); \"></div>";
				ht_cell.innerHTML = h;


			//ROW 1
			ht_row = handle_table.insertRow(1);
			ht_row.height = "34%";
			
				//CELL 0
				ht_cell = ht_row.insertCell(0);
				ht_cell.style.verticalAlign = "middle";
				ht_cell.align = "left";
				h = "<div class=\"crop_handle\" style=\"cursor:w-resize;\" onmousedown=\"JAVASCRIPT:crop_resize(this, event, 3);cancel_bubble(event); \"></div>";
				ht_cell.innerHTML = h;

				//CELL 1
				ht_cell = ht_row.insertCell(1);
				ht_cell.style.verticalAlign = "middle";
				ht_cell.align = "center";
				// h = "<div class=\"crop_handle\" style=\"cursor:move;\" onmousedown=\"JAVASCRIPT:crop_move(this, event); \"></div>";
				h = "<div class=\"crop_handle\"></div>";
				ht_cell.innerHTML = h;

				//CELL 2
				ht_cell = ht_row.insertCell(2);
				ht_cell.style.verticalAlign = "middle";
				ht_cell.align = "right";
				h = "<div class=\"crop_handle\" style=\"cursor:e-resize;\" onmousedown=\"JAVASCRIPT:crop_resize(this, event, 4);cancel_bubble(event); \"></div>";
				ht_cell.innerHTML = h;

		
			//ROW 2
			ht_row = handle_table.insertRow(2);
			ht_row.height = "33%";

				//CELL 0
				ht_cell = ht_row.insertCell(0);
				ht_cell.style.verticalAlign = "bottom";
				ht_cell.align = "left";
								h = "<div class=\"crop_handle\" style=\"cursor:sw-resize;\" onmousedown=\"JAVASCRIPT:crop_resize(this, event, 5);cancel_bubble(event); \"></div>";
				ht_cell.innerHTML = h;

				//CELL 1
				ht_cell = ht_row.insertCell(1);
				ht_cell.style.verticalAlign = "bottom";
				ht_cell.align = "center";
								h = "<div class=\"crop_handle\" style=\"cursor:s-resize;\" onmousedown=\"JAVASCRIPT:crop_resize(this, event, 6);cancel_bubble(event); \"></div>";
				ht_cell.innerHTML = h;


				//CELL 2
				ht_cell = ht_row.insertCell(2);
				ht_cell.style.verticalAlign = "bottom";
				ht_cell.align = "right";
								h = "<div class=\"crop_handle\" style=\"cursor:se-resize;\" onmousedown=\"JAVASCRIPT:crop_resize(this, event, 7);cancel_bubble(event); \"></div>";
				ht_cell.innerHTML = h;

		
		cell.style.width = (crop_tool_posB[0] - crop_tool_posA[0]) + "px";
		cell.style.height = (crop_tool_posB[1] - crop_tool_posA[1]) + "px";
		cell.appendChild( div_handle_table );

		
		cell = row.insertCell(2);
		cell.className = "crop_tool_dark";
		cell.style.width = crop_tool_posA[0] + "px";
		cell.style.height = (crop_tool_posB[1] - crop_tool_posA[1]) + "px";
		cell.innerHTML = "<!-- CT -->";
		
		row = crop_tool_table.insertRow(2);
		
		cell = row.insertCell(0);
		cell.className = "crop_tool_dark";
		cell.style.width = crop_tool_posA[0] + "px";
		cell.style.height = crop_tool_posA[1] + "px";
		cell.innerHTML = "<!-- CT -->";
		
		cell = row.insertCell(1);
		cell.className = "crop_tool_dark";
		cell.style.width = (crop_tool_posB[0] - crop_tool_posA[0]) + "px";
		cell.style.height = crop_tool_posA[1] + "px";
		cell.innerHTML = "<!-- CT -->";
		
		cell = row.insertCell(2);
		cell.className = "crop_tool_dark";
		cell.style.width = crop_tool_posA[0] + "px";
		cell.style.height = crop_tool_posA[1] + "px";
		cell.innerHTML = "<!-- CT -->";

		crop_tool.appendChild( crop_tool_table );
		//document.body.appendChild( crop_tool );
		
		var reporter = document.createElement("div");
		//reporter.id = "crop_tool_reporter";
		reporter.className = "vis_area_content";
		
		reporter.innerHTML = "<table width=\"100%\"><tr><td id=\"crop_tool_reporter\" valign=\"top\"></td><td width=\"200px\" id=\"crop_tool_option\" valign=\"top\"></td></tr></table>";
		
		crop_tool_main.appendChild( reporter );
		
		var buttons = document.createElement("div");
		buttons.id = "crop_tool_buttons";
		buttons.className = "vis_area_content";
		
		crop_tool_main.appendChild( buttons );
		
		buttons.innerHTML = "<span  onclick=\"JAVASCRIPT: close_crop_tool();\" onmouseover=\"JAVASCRIPT: btn_over(this, 'btn_f_cancel_o');\" onmouseout=\"JAVASCRIPT: btn_out(this, 'btn_f_cancel');\"  style=\"cursor:pointer;  display:inline-block; \" title=\"Annuler\"> <img src=\"/images/design/core/btn_f_cancel.png\" alt=\"Annuler\" title=\"Annuler\" style=\"float:left;clear:none;\" /><div style=\"margin-top:8px;float:left;clear:none;\" >Annuler</div></span>";
				
		buttons.innerHTML += "<span  onclick=\"JAVASCRIPT: crop_tool_send('" + image_save_url + "');\" onmouseover=\"JAVASCRIPT: btn_over(this, 'btn_f_enregistrer_o');\" onmouseout=\"JAVASCRIPT: btn_out(this, 'btn_f_enregistrer');\"  style=\"cursor:pointer;  display:inline-block; \" title=\"Sauvegarder le recadrage\"> <img src=\"/images/design/core/btn_f_enregistrer.png\" alt=\"Sauvegarder le recadrage\" title=\"Appliquer le recadrage\" style=\"float:left;clear:none;\" /><div style=\"margin-top:8px;float:left;clear:none;\" >Appliquer le recadrage</div></span>";

		
	
		bring_center_form(document.body, crop_tool_main, null, false, 10, false );
		
	
}

function close_crop_tool(){

	close_this_sub_form( document.getElementById('form_closer') );
}

function crop_zone_repaint(){

	var crop_tool_table = document.getElementById('crop_tool_table');
	
	var image_width = crop_tool_image_width * crop_tool_scale_ratio;
	var image_height = crop_tool_image_height * crop_tool_scale_ratio;
	
	// FIRST ROW
	var cell = crop_tool_table.rows[0].cells[0];
	cell.style.width = crop_tool_posA[0] + "px";
	cell.style.height = crop_tool_posA[1] + "px";

	cell = crop_tool_table.rows[0].cells[1];
	cell.style.width = (crop_tool_posB[0] - crop_tool_posA[0]) + "px";
	cell.style.height = crop_tool_posA[1] + "px";
	
	cell = crop_tool_table.rows[0].cells[2];
	cell.style.width = (image_width - crop_tool_posB[0]) + "px";
	cell.style.height = crop_tool_posA[1] + "px";

	// SECOND ROW
	cell = crop_tool_table.rows[1].cells[0];
	cell.style.width = crop_tool_posA[0] + "px";
	cell.style.height = (crop_tool_posB[1] - crop_tool_posA[1]) + "px";
	
	cell = crop_tool_table.rows[1].cells[1];
	cell.style.width = (crop_tool_posB[0] - crop_tool_posA[0]) + "px";
	cell.style.height = (crop_tool_posB[1] - crop_tool_posA[1]) + "px";
	
	cell = crop_tool_table.rows[1].cells[2];	
	cell.style.width = (image_width - crop_tool_posB[0]) + "px";
	cell.style.height = (crop_tool_posB[1] - crop_tool_posA[1]) + "px";

	// THIRD ROW
	cell = crop_tool_table.rows[2].cells[0];
	cell.style.width = crop_tool_posA[0] + "px";
	cell.style.height = (image_height - crop_tool_posB[1]) + "px";
	
	cell = crop_tool_table.rows[2].cells[1];
	cell.style.width = (crop_tool_posB[0] - crop_tool_posA[0]) + "px";
	cell.style.height = (image_height - crop_tool_posB[1]) + "px";
	
	cell = crop_tool_table.rows[2].cells[2];
	cell.style.width = (image_width - crop_tool_posB[0]) + "px";
	cell.style.height = (image_height - crop_tool_posB[1]) + "px";

	crop_tool_report();
}

function crop_tool_displace(ev){

	var can_repaint = true;
	
	if(crop_tool_moving){
		//-- MOVING CROP ZONE

		var event_info = infos_from_event(ev);
		
		var deltaX = event_info[0] - crop_tool_pos_or[0];
		var deltaY = event_info[1] - crop_tool_pos_or[1];
		
		var crop_tool_width = crop_tool_posB[0] - crop_tool_posA[0];
		var crop_tool_height = crop_tool_posB[1] - crop_tool_posA[1];
		
		crop_tool_posA[0] = crop_tool_posA_or[0] + deltaX;
		crop_tool_posA[1] = crop_tool_posA_or[1] + deltaY;
		
		crop_tool_posB[0] = crop_tool_posB_or[0] + deltaX;
		crop_tool_posB[1] = crop_tool_posB_or[1] + deltaY;
		
		if( crop_tool_posA[0] < 0){
			crop_tool_posA[0] = 0;
			crop_tool_posB[0] = crop_tool_width ;
		}

		if( crop_tool_posB[0] > (crop_tool_image_width * crop_tool_scale_ratio)){
			crop_tool_posB[0] = crop_tool_image_width * crop_tool_scale_ratio;
			crop_tool_posA[0] = crop_tool_posB[0] - crop_tool_width;
		}


		if( crop_tool_posA[1] < 0){
			crop_tool_posA[1] = 0;
			crop_tool_posB[1] = crop_tool_height;
		}


		if( crop_tool_posB[1] > (crop_tool_image_height * crop_tool_scale_ratio) ){
			crop_tool_posB[1] = crop_tool_image_height * crop_tool_scale_ratio;
			crop_tool_posA[1] = crop_tool_posB[1] - crop_tool_height;
		}
		 
		crop_zone_repaint();
		
	}else{
		//-- EXPAND/RETRACT CROP ZONE

		var event_info = infos_from_event(ev);
		var deltaX = event_info[0] - crop_tool_pos_or[0];
		var deltaY = event_info[1] - crop_tool_pos_or[1];
	
		switch(crop_tool_handle_resize){

			case 0:	//NW
			
				var tmpX = crop_tool_posA_or[0] + deltaX;
				var tmpY = crop_tool_posA_or[1] + deltaY;

				if(crop_tool_forced_ratio != 0){
				
					var tool_width = crop_tool_posB[0] - tmpX;
					var tool_height = crop_tool_posB[1] - tmpY;
					
					var deltaRatio = tool_width / tool_height;

					if(deltaRatio != crop_tool_forced_ratio){
					
						if(Math.abs(deltaX) > Math.abs(deltaY)){
							deltaY = deltaX / crop_tool_forced_ratio;
						}else{
							deltaX = deltaY * crop_tool_forced_ratio;
						}
						
						tmpX = crop_tool_posA_or[0] + deltaX;
						tmpY = crop_tool_posA_or[1] + deltaY;

					}

				}

				if( tmpX < 0){
					tmpX = 0;
					can_repaint = false;
				}
				
				if( tmpY < 0){
					tmpY = 0;
					can_repaint = false;
				}

				var crop_tool_width = crop_tool_posB[0] - crop_tool_posA[0];
				var crop_tool_height = crop_tool_posB[1] - crop_tool_posA[1];
				
				if( crop_tool_width < (crop_tool_min_width * crop_tool_scale_ratio) && deltaX > 0)
					can_repaint = false;
					
				if( crop_tool_height < (crop_tool_min_height * crop_tool_scale_ratio) && deltaY > 0)
					can_repaint = false;


				if(can_repaint){

					crop_tool_posA[0] = tmpX;
					crop_tool_posA[1] = tmpY;
				}

			break;
			
			case 1:	//N
				var tmpX = crop_tool_posA_or[0] + deltaX;
				var tmpY = crop_tool_posA_or[1] + deltaY;
				

				
				if(crop_tool_forced_ratio != 0){
				
					var tool_width = crop_tool_posB[0] - tmpX;
					var tool_height = crop_tool_posB[1] - tmpY;
					
					var deltaRatio = tool_width / tool_height;

					if(deltaRatio != crop_tool_forced_ratio){

						deltaX = deltaY * crop_tool_forced_ratio;

						tmpX = crop_tool_posA_or[0] + deltaX;
						tmpY = crop_tool_posA_or[1] + deltaY;
					}

				}

				if( tmpX < 0){
					tmpX = 0;
					can_repaint = false;
				}
				
				if( tmpY < 0){
					tmpY = 0;
					can_repaint = false;
				}
				
				var crop_tool_width = crop_tool_posB[0] - crop_tool_posA[0];
				var crop_tool_height = crop_tool_posB[1] - crop_tool_posA[1];
				
				if( crop_tool_width < (crop_tool_min_width * crop_tool_scale_ratio) && deltaX > 0)
					can_repaint = false;
					
				if( crop_tool_height < (crop_tool_min_height * crop_tool_scale_ratio) && deltaY > 0)
					can_repaint = false;
				
				if(can_repaint){

					crop_tool_posA[0] = tmpX;
					crop_tool_posA[1] = tmpY;
				}

				
			break;
			
			case 2:	//NE
				
				var tmpX = crop_tool_posB_or[0] + deltaX;
				var tmpY = crop_tool_posA_or[1] - deltaY;

				if(crop_tool_forced_ratio != 0){
				
					var tool_width = tmpX - crop_tool_posA[0];
					var tool_height = crop_tool_posB[1] - tmpY;
					
					var deltaRatio = tool_width / tool_height;



					if(deltaRatio != crop_tool_forced_ratio){

						if(Math.abs(deltaX) > Math.abs(deltaY)){
							deltaY = -1 * deltaX / crop_tool_forced_ratio;
						}else{
							deltaX = (-1 * deltaY) * crop_tool_forced_ratio;
						}
						
						tmpX = crop_tool_posB_or[0] + deltaX;
						tmpY = crop_tool_posA_or[1] + deltaY;

					}

				}

				if( tmpX > (crop_tool_image_width * crop_tool_scale_ratio) ){
					tmpX = crop_tool_image_width * crop_tool_scale_ratio;
					can_repaint = false;
				}
				
				if( tmpY < 0){
					tmpY = 0;
					can_repaint = false;
				}

				var crop_tool_width = crop_tool_posB[0] - crop_tool_posA[0];
				var crop_tool_height = crop_tool_posB[1] - crop_tool_posA[1];
				
				if( crop_tool_width < (crop_tool_min_width * crop_tool_scale_ratio) && deltaX < 0)
					can_repaint = false;
					
				if( crop_tool_height < (crop_tool_min_height * crop_tool_scale_ratio) && deltaY > 0)
					can_repaint = false;


				if(can_repaint){

					crop_tool_posB[0] = tmpX;
					crop_tool_posA[1] = tmpY;
				}

			break;


			case 3: //W
			
			
				var tmpX = crop_tool_posA_or[0] + deltaX;
				var tmpY = crop_tool_posB_or[1] + deltaY;

				if(crop_tool_forced_ratio != 0){
				
					var tool_width = crop_tool_posB[0] - tmpX;
					var tool_height = crop_tool_posB[1] - tmpY;
					
					var deltaRatio = tool_width / tool_height;

					if(deltaRatio != crop_tool_forced_ratio){
					
						
						deltaY = deltaX / crop_tool_forced_ratio;

						tmpX = crop_tool_posA_or[0] + deltaX;
						tmpY = crop_tool_posA_or[1] + deltaY;

					}

				}

				if( tmpX < 0){
					tmpX = 0;
					can_repaint = false;
				}
				
				if( tmpY < 0){
					tmpY = 0;
					can_repaint = false;
				}

				var crop_tool_width = crop_tool_posB[0] - crop_tool_posA[0];
				var crop_tool_height = crop_tool_posB[1] - crop_tool_posA[1];
				
				if( crop_tool_width < (crop_tool_min_width * crop_tool_scale_ratio) && deltaX > 0)
					can_repaint = false;
					
				if( crop_tool_height < (crop_tool_min_height * crop_tool_scale_ratio) && deltaY > 0)
					can_repaint = false;

				if(can_repaint){

					crop_tool_posA[0] = tmpX;
					crop_tool_posA[1] = tmpY;
				}
			
			break;
			
			case 4: //E
			
				var tmpX = crop_tool_posB_or[0] + deltaX;
				var tmpY = crop_tool_posA_or[1] - deltaY;

				if(crop_tool_forced_ratio != 0){
				
					var tool_width = tmpX - crop_tool_posA[0];
					var tool_height = crop_tool_posB[1] - tmpY;
					
					var deltaRatio = tool_width / tool_height;



					if(deltaRatio != crop_tool_forced_ratio){

						
						deltaY = -1 * deltaX / crop_tool_forced_ratio;
						
						
						tmpX = crop_tool_posB_or[0] + deltaX;
						tmpY = crop_tool_posA_or[1] + deltaY;

					}

				}

				if( tmpX > (crop_tool_image_width * crop_tool_scale_ratio) ){
					tmpX = crop_tool_image_width * crop_tool_scale_ratio;
					can_repaint = false;
				}
				
				if( tmpY < 0){
					tmpY = 0;
					can_repaint = false;
				}

				var crop_tool_width = crop_tool_posB[0] - crop_tool_posA[0];
				var crop_tool_height = crop_tool_posB[1] - crop_tool_posA[1];
				
				if( crop_tool_width < (crop_tool_min_width * crop_tool_scale_ratio) && deltaX < 0)
					can_repaint = false;
					
				if( crop_tool_height < (crop_tool_min_height * crop_tool_scale_ratio) && deltaY > 0)
					can_repaint = false;


				if(can_repaint){

					crop_tool_posB[0] = tmpX;
					crop_tool_posA[1] = tmpY;
				}

			break;
			
			
			case 5: //SW
			
				var tmpX = crop_tool_posA_or[0] + deltaX;
				var tmpY = crop_tool_posB_or[1] + deltaY;

				if(crop_tool_forced_ratio != 0){

					var tool_width = crop_tool_posB[0] - tmpX;
					var tool_height = tmpY - crop_tool_posA[1];

					var deltaRatio = tool_width / tool_height;

					if(deltaRatio != crop_tool_forced_ratio){

						if(Math.abs(deltaX) > Math.abs(deltaY)){
							deltaY = (-1*deltaX) / crop_tool_forced_ratio;
						}else{
							deltaX = (-1*deltaY) * crop_tool_forced_ratio;
						}

						tmpX = crop_tool_posA_or[0] + deltaX;
						tmpY = crop_tool_posB_or[1] + deltaY;

					}

				}

				if( tmpX < 0){
					tmpX = 0;
					can_repaint = false;
				}
				
				if( tmpY > (crop_tool_image_height * crop_tool_scale_ratio) ){
					tmpY = (crop_tool_image_height * crop_tool_scale_ratio);
					can_repaint = false;
				}

				var crop_tool_width = crop_tool_posB[0] - crop_tool_posA[0];
				var crop_tool_height = crop_tool_posB[1] - crop_tool_posA[1];
				
				if( crop_tool_width < (crop_tool_min_width * crop_tool_scale_ratio) && deltaX > 0)
					can_repaint = false;
					
				if( crop_tool_height < (crop_tool_min_height * crop_tool_scale_ratio) && deltaY < 0)
					can_repaint = false;


				if(can_repaint){

					crop_tool_posA[0] = tmpX;
					crop_tool_posB[1] = tmpY;
				}
			
			
			break;


			case 6: //S

				var tmpX = crop_tool_posA_or[0] + deltaX;
				var tmpY = crop_tool_posB_or[1] + deltaY;

				if(crop_tool_forced_ratio != 0){

					var tool_width = crop_tool_posB[0] - tmpX;
					var tool_height = tmpY - crop_tool_posA[1];

					var deltaRatio = tool_width / tool_height;

					if(deltaRatio != crop_tool_forced_ratio){

						deltaX = (-1*deltaY) * crop_tool_forced_ratio;

						tmpX = crop_tool_posA_or[0] + deltaX;
						tmpY = crop_tool_posB_or[1] + deltaY;

					}

				}

				if( tmpX < 0){
					tmpX = 0;
					can_repaint = false;
				}
				
				if( tmpY > (crop_tool_image_height * crop_tool_scale_ratio) ){
					tmpY = (crop_tool_image_height * crop_tool_scale_ratio);
					can_repaint = false;
				}

				var crop_tool_width = crop_tool_posB[0] - crop_tool_posA[0];
				var crop_tool_height = crop_tool_posB[1] - crop_tool_posA[1];
				
				if( crop_tool_width < (crop_tool_min_width * crop_tool_scale_ratio) && deltaX > 0)
					can_repaint = false;
					
				if( crop_tool_height < (crop_tool_min_height * crop_tool_scale_ratio) && deltaY < 0)
					can_repaint = false;

				if(can_repaint){

					crop_tool_posA[0] = tmpX;
					crop_tool_posB[1] = tmpY;
				}
			

			break;

			case 7: //SE

				var tmpX = crop_tool_posB_or[0] + deltaX;
				var tmpY = crop_tool_posB_or[1] + deltaY;

				if(crop_tool_forced_ratio != 0){

					var tool_width = crop_tool_posB[0] - tmpX;
					var tool_height = tmpY - crop_tool_posA[1];

					var deltaRatio = tool_width / tool_height;

					if(deltaRatio != crop_tool_forced_ratio){

						if(Math.abs(deltaX) > Math.abs(deltaY)){
							deltaY = deltaX / crop_tool_forced_ratio;
						}else{
							deltaX = deltaY * crop_tool_forced_ratio;
						}

						tmpX = crop_tool_posB_or[0] + deltaX;
						tmpY = crop_tool_posB_or[1] + deltaY;

					}

				}

				if( tmpX > (crop_tool_image_width * crop_tool_scale_ratio) ){
					tmpX = crop_tool_image_width * crop_tool_scale_ratio;
					can_repaint = false;
				}
				
				if( tmpY > (crop_tool_image_height * crop_tool_scale_ratio) ){
					tmpY = (crop_tool_image_height * crop_tool_scale_ratio);
					can_repaint = false;
				}

				var crop_tool_width = crop_tool_posB[0] - crop_tool_posA[0];
				var crop_tool_height = crop_tool_posB[1] - crop_tool_posA[1];
				
				if( crop_tool_width < (crop_tool_min_width * crop_tool_scale_ratio) && deltaX < 0)
					can_repaint = false;
					
				if( crop_tool_height < (crop_tool_min_height * crop_tool_scale_ratio) && deltaY < 0)
					can_repaint = false;


				if(can_repaint){

					crop_tool_posB[0] = tmpX;
					crop_tool_posB[1] = tmpY;
				}
			
			break;

		
		}

		if(can_repaint)
			crop_zone_repaint();

	}
}

function crop_tool_report(){

	var crop_tool_width = crop_tool_posB[0] - crop_tool_posA[0];
	var crop_tool_height = crop_tool_posB[1] - crop_tool_posA[1];
	var crop_tool_real_width = parseInt(crop_tool_width / crop_tool_scale_ratio);
	var crop_tool_real_height = parseInt(crop_tool_height / crop_tool_scale_ratio);
	
	var actual_ratio = 	parseInt( (crop_tool_width / crop_tool_height) * 100 ) /100;

	var crop_tool_reporter = document.getElementById("crop_tool_reporter");

	crop_tool_reporter.innerHTML = "Position de la zone: " + parseInt(crop_tool_posA[0] / crop_tool_scale_ratio) + " X " + parseInt(crop_tool_posA[1] / crop_tool_scale_ratio) + "<br />";	

	crop_tool_reporter.innerHTML += "Taille de la zone: " + crop_tool_real_width + " X " + crop_tool_real_height;
	crop_tool_reporter.innerHTML += "<br />Ratio ";
	
	if(!crop_tool_forced_ratio_available)
		crop_tool_reporter.innerHTML += "<b>fix&eacute;</b>";
	
	crop_tool_reporter.innerHTML += ": <span id=\"crop_tool_ratio_indicator\">" + actual_ratio + "</span>";
	
	var crop_tool_option = document.getElementById("crop_tool_option");

	if(crop_tool_option.innerHTML.length<=0){
	
		if(crop_tool_forced_ratio_available){

			crop_tool_option.innerHTML += " Forcer le ratio : <input type=\"checkbox\" id=\"forced_ratio\" value=\"Y\" onclick=\"toggle_ratio_lock();\"/><br />";
			crop_tool_option.innerHTML += " Largeur <input type=\"text\" id=\"ratio_width\" value=\"" + crop_tool_real_width + "\" readonly=\"true\" size=\"5\" /><br />";
			crop_tool_option.innerHTML += " Hauteur <input type=\"text\" id=\"ratio_height\" value=\"" + crop_tool_real_height + "\" readonly=\"true\" size=\"5\" /><br />";
	
			crop_tool_option.innerHTML += "<span  onclick=\"JAVASCRIPT: crop_tool_forced_ratio_changed();\" onmouseover=\"JAVASCRIPT: btn_over(this, 'btn_f_enregistrer_o');\" onmouseout=\"JAVASCRIPT: btn_out(this, 'btn_f_enregistrer');\"  style=\"cursor:pointer;  display:inline-block; \" title=\"Changer le ratio\"> <img src=\"/images/design/core/btn_f_enregistrer.png\" alt=\"Changer le ratio\" title=\"Appliquer le ratio\" style=\"float:left;clear:none;\" /><div style=\"margin-top:8px;float:left;clear:none;\" >Appliquer le ratio</div></span>";
		}

	}else{
	
		forced_ratio = document.getElementById('forced_ratio').checked;
		
		if( !forced_ratio){
			document.getElementById('crop_tool_ratio_indicator').innerHTML = actual_ratio;
			document.getElementById('ratio_width').value = crop_tool_real_width;
			document.getElementById('ratio_height').value = crop_tool_real_height;
		}
	}
}

function toggle_ratio_lock(){

	forced_ratio = document.getElementById('forced_ratio').checked;
	
	if( forced_ratio){
		document.getElementById('ratio_width').readOnly = false;
		document.getElementById('ratio_height').readOnly = false;
	}else{
		document.getElementById('ratio_width').readOnly = true;
		document.getElementById('ratio_height').readOnly = true;
		crop_tool_forced_ratio = 0;
	}
}

function crop_tool_forced_ratio_changed(){


	forced_ratio = document.getElementById('forced_ratio').checked;

	if( forced_ratio){

		var ratio_width = parseInt( document.getElementById('ratio_width').value );
		var ratio_height = parseInt( document.getElementById('ratio_height').value );
		var actual_ratio = ratio_width / ratio_height;

		actual_ratio = 	parseInt( actual_ratio * 100 ) /100

		var crop_tool_width = crop_tool_posB_or[0] - crop_tool_posA_or[0] ;
		crop_tool_width = parseInt(crop_tool_width / crop_tool_scale_ratio);

		var crop_tool_height = crop_tool_posB_or[1] - crop_tool_posA_or[1];
		crop_tool_height = parseInt(crop_tool_height / crop_tool_scale_ratio);

		var width_ratio = Math.abs( parseInt( 100 * crop_tool_width / ratio_width));
		
		if(width_ratio > 100)
			width_ratio /= 100;
		else
			width_ratio = 100 / width_ratio;
			
		var height_ratio = Math.abs( parseInt( 100 * crop_tool_height / ratio_height));

		if(height_ratio > 100)
			height_ratio /= 100;
		else
			height_ratio = 100 / height_ratio;


		if(width_ratio < height_ratio){  //ACTUAL WIDTH IS CLOSER

			var height_anticipated = (1 / actual_ratio) * crop_tool_width;

			crop_tool_posB[1] = crop_tool_posA[1] + (height_anticipated * crop_tool_scale_ratio);

		}else{	//ACTUAL HEIGHT IS CLOSER
			var width_anticipated = actual_ratio * crop_tool_height;
			
			crop_tool_posB[0] = crop_tool_posA[0] + (width_anticipated * crop_tool_scale_ratio);

		}
		
		crop_tool_forced_ratio = actual_ratio;
		
		document.getElementById('crop_tool_ratio_indicator').innerHTML = actual_ratio;
		crop_zone_repaint();
		crop_tool_up();
	}
}

function crop_move(currObj, ev){

	crop_tool_moving = true;
	crop_tool_resizing = false;
	var event_info = infos_from_event(ev);
	crop_tool_pos_or[0] = event_info[0];
	crop_tool_pos_or[1] = event_info[1];
}

function crop_resize(currObj, ev, crop_tool_handle ){

		crop_tool_resizing = true;
		crop_tool_moving = false;
		
		var event_info = infos_from_event(ev);
		crop_tool_pos_or[0] = event_info[0];
		crop_tool_pos_or[1] = event_info[1];
		crop_tool_handle_resize = crop_tool_handle;

}

function infos_from_event(evt){

	if( typeof(evt) == "object" ){
		ev = evt;
	}else{
		ev = window.event;
	}

	if(ev)
		return new Array(ev.clientX + document.documentElement.scrollLeft, ev.clientY + document.documentElement.scrollTop, ev.button, ev.shiftKey);	
	else
		return new Array(0, 0, -1, false);
	
	
}

function crop_tool_up(){

	crop_tool_resizing = false;
	crop_tool_moving = false;
	
	crop_tool_posA_or[0] = crop_tool_posA[0];
	crop_tool_posA_or[1] = crop_tool_posA[1];
	
	crop_tool_posB_or[0] = crop_tool_posB[0];
	crop_tool_posB_or[1] = crop_tool_posB[1];

	crop_tool_handle_resize = -1;
}

function crop_tool_send( save_url ){


	var crop_tool_width = crop_tool_posB[0] - crop_tool_posA[0];
	var crop_tool_height = crop_tool_posB[1] - crop_tool_posA[1];
	
	var orx = parseInt(crop_tool_posA[0] / crop_tool_scale_ratio);
	var ory = parseInt(crop_tool_posA[1] / crop_tool_scale_ratio);
	
	var w = parseInt(crop_tool_width / crop_tool_scale_ratio);
	var h = parseInt(crop_tool_height / crop_tool_scale_ratio);

	save_url += "&orx=" + orx;
	save_url += "&ory=" + ory;
	save_url += "&w=" + w;
	save_url += "&h=" + h;
	
	var Ar = new Ajax(save_url, "GET");
	Ar.onComplete = crop_tool_done;
	Ar.send();
	open_loader();

	
	close_crop_tool();
}

function crop_tool_done(responseText, responseXML){

	var type = responseXML.getElementsByTagName("type")[0].firstChild.nodeValue;
	
	if(type == "photo_album"){
		var html_content = responseXML.getElementsByTagName("insert_item")[0].firstChild.nodeValue;
		photo_saved(html_content);
	
		window.setTimeout( function(){ load_image_info(document.getElementById('image_' + last_selected_image), last_selected_image, last_selected_album); }, 1000 );
	}else if(type == "online_directory_cover" || type == "online_directory_main"){
		close_loader();
	}
	
}
// JavaScript Document
/*
 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
 * in FIPS PUB 180-1
 * Version 2.1a Copyright Paul Johnston 2000 - 2002.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for details.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}

/*
 * Perform a simple self-test to see if the VM is working
 */
function sha1_vm_test()
{
  return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
}

/*
 * Calculate the SHA-1 of an array of big-endian words, and a bit length
 */
function core_sha1(x, len)
{
  /* append padding */
  x[len >> 5] |= 0x80 << (24 - len % 32);
  x[((len + 64 >> 9) << 4) + 15] = len;

  var w = Array(80);
  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;
  var e = -1009589776;

  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;
    var olde = e;

    for(var j = 0; j < 80; j++)
    {
      if(j < 16) w[j] = x[i + j];
      else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
      var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
                       safe_add(safe_add(e, w[j]), sha1_kt(j)));
      e = d;
      d = c;
      c = rol(b, 30);
      b = a;
      a = t;
    }

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
    e = safe_add(e, olde);
  }
  return Array(a, b, c, d, e);

}

/*
 * Perform the appropriate triplet combination function for the current
 * iteration
 */
function sha1_ft(t, b, c, d)
{
  if(t < 20) return (b & c) | ((~b) & d);
  if(t < 40) return b ^ c ^ d;
  if(t < 60) return (b & c) | (b & d) | (c & d);
  return b ^ c ^ d;
}

/*
 * Determine the appropriate additive constant for the current iteration
 */
function sha1_kt(t)
{
  return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 :
         (t < 60) ? -1894007588 : -899497514;
}

/*
 * Calculate the HMAC-SHA1 of a key and some data
 */
function core_hmac_sha1(key, data)
{
  var bkey = str2binb(key);
  if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);

  var ipad = Array(16), opad = Array(16);
  for(var i = 0; i < 16; i++)
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
  return core_sha1(opad.concat(hash), 512 + 160);
}

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * Convert an 8-bit or 16-bit string to an array of big-endian words
 * In 8-bit function, characters >255 have their hi-byte silently ignored.
 */
function str2binb(str)
{
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
  return bin;
}

/*
 * Convert an array of big-endian words to a string
 */
function binb2str(bin)
{
  var str = "";
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < bin.length * 32; i += chrsz)
    str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
  return str;
}

/*
 * Convert an array of big-endian words to a hex string.
 */
function binb2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8  )) & 0xF);
  }
  return str;
}

/*
 * Convert an array of big-endian words to a base-64 string
 */
function binb2b64(binarray)
{
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i += 3)
  {
    var triplet = (((binarray[i   >> 2] >> 8 * (3 -  i   %4)) & 0xFF) << 16)
                | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
                |  ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
    for(var j = 0; j < 4; j++)
    {
      if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
      else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
    }
  }
  return str;
}

	

//END OF GLOBAL JAVASCRIPT FILE
