/*======= Common util function ========*/

// *************************************** AJAX UTILS ***************************
var ENABLE_AJAX = true;
var AJAX_URL_TRUE = true;
var AJAX_URL_FALSE = false;
var LOG_DEBUG = false;

function setFormJ(formNameP){
	ajaxAnywhere.formName = formNameP;
}
function startAjaxWait(msgP){
	if($('wait')){
		Element.show('wait');
	}else{
	    var div = $("AA_" + this.id + "_loading_div");
	    if (div == null) {
	        div = document.createElement("DIV");
	
	        document.body.appendChild(div);
	        div.id = "AA_" + this.id + "_loading_div";
	
	        if( msgP == ''){
	            msgP = '&nbsp;Chargement...';
	        }
	        //div.innerHTML = msgP+'<img src="images/admin/wait.gif"/>';
	        div.innerHTML = msgP;
	        div.style.position = "absolute";
	        div.style.color = "#fff";
	        div.style.backgroundColor = "#ff5500";
	        div.style.width = "150px";
	        div.style.heigth = "50px";
	        div.style.fontFamily = "Arial, Helvetica, sans-serif";
	        div.style.fontWeight = "bold";
	        div.style.fontSize = "11px";        
	    }
	    div.style.top = document.body.scrollTop + "px";
	    div.style.left = (document.body.offsetWidth - 100 - (document.all?20:0)) + "px";
		Element.show(div.id);
	}
}

function endAjaxWait(){
	if($('wait')){
		Element.hide('wait');	
	}else{
	    var div = document.getElementById("AA_" + this.id + "_loading_div");
	    if (div != null){
		    Element.hide(div.id);
	    }
	}
}

function _submitJ(urlP, zoneP, isViaUrl){
  if( ajaxAnywhere && ENABLE_AJAX ){  
      ajaxAnywhere.showLoadingMessage = function() {
        return startAjaxWait('');
      }	
      ajaxAnywhere.hideLoadingMessage = function() {      	
        return endAjaxWait();
      }
      ajaxAnywhere.handlePrevousRequestAborted = function(){
      	return startAjaxWait('');
      }      
      if(isViaUrl){      	
      	ajaxAnywhere.getAJAX(urlP, zoneP);
      }else{
        ajaxAnywhere.getZonesToReload = function() {
        	if(zoneP ==''){
        		return 'document.all';
        	}else{        	
	        	return zoneP;
        	}
      	}      
      	ajaxAnywhere.submitAJAX();
      }
  }else{
  	  if(ajaxAnywhere.formName){
	  	  var aJform = ajaxAnywhere.formName;
	      document.forms[aJform].submit();
  	  }	else{
  	  	  window.location = urlP;	
  	  }
  }
}
function submitJ(zoneP){
	return _submitJ('', zoneP, AJAX_URL_FALSE);
}

function submitUrlJ(urlP, zoneP){
	return _submitJ(urlP, zoneP, AJAX_URL_TRUE);
}

function submitJ2(zoneP){
  	var aJform = ajaxAnywhere.formName;  	
  	return submitJ3(aJform, zoneP);
}

function submitJ3(aJform, zoneP){
	if(!aJform || aJform==''){
	  	var aJform = ajaxAnywhere.formName;
	  	var aJformObj = document.forms[aJform];	
	  	var postData = Form.serialize(aJformObj);
	}if(document.forms[aJform]){
  		var aJformObj = document.forms[aJform];	
 		var postData = Form.serialize(aJformObj);
	}else if($(aJform)){
		var aJformObj = $(aJform);
	  	var postData = Form.serialize(aJform);
	}
  	logging(postData);
  	var aJformAction = aJformObj.action;
  	logging(aJformAction);
    var postUrl = aJformAction + '?' + postData;    
    logging(postUrl);
	return submitUrlJ(postUrl, zoneP);
}

function submitJs(aJform){
	return submitJ3(aJform, '');
}
function validateJ(aJFormId){
	submitJ3(aJFormId, 'ErrorsZone');
	if(AJAX_VALIDATE){
		return true;
	}else{
		return false;
	}
}

function logging(msgP){
	if(LOG_DEBUG){
		alert(this + msgP);
	}
}

function goLast(){
	window.history.go(-1);	
}

function goNext(){
	window.history.go(+1);
}

function loader(){
	setTimeout('endWait()',500); 
	//endWait();
}

function unloader(){
	setTimeout('startWait()',500);
	//startWait();
}

function startWait(){
	//startProgress('');

	if($('wait')){
		Element.show('wait');	
	}
	if($('body')){
		Element.hide('body');	
	}
	
}

function endWait(){	
	//endProgress();
	if($('wait')){
		Element.hide('wait');
	}
	if($('body')){
		Element.show('body');
	}
}

/**
Author : Mehrez MAROUANI
Description : turn a groupe of checkbox to checked and vice-versa
*/
function toggleCheckAll(trigger, formName, fieldName) {

checked = trigger.checked;
form = document.forms[formName];
for (i = 0; i < form[fieldName].length; i++){
	form[fieldName][i].checked = checked;
   }
}
	
function popUp(id, URL, width, height, scroll) {
		width = (width == null) ? 500 : width;
		height = (height == null) ? 400 : height;
		scroll = (height == null) ? 1 : scroll;
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars="+scroll+",location=0,statusbar=0,menubar=0,resizable=1,width="+ width +",height=" + height + ",left = 340,top = 262');");
}

function getPopUp(idP){
	return $('page'+idP);
}

function hideFlash(){
	Element.hide('header');
}

/** 
	Methode pour aficher le nombre limite de caractére pour un texte area
**/
function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) {// if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
    }else{// otherwise, update 'characters left' counter
        countfield.value = maxlimit - field.value.length;
    }
}

/**
author : Mehrez MAROUANI
Date : Oct. 10 2007
Description : Metod used to confirm a deletion operation.
**/
function confirmDelete(selectedItems){
	var msg = "coucou";
	
	if(selectedItems == undefined || selectedItems == 1){
		msg = "Voulez-vous vraiment supprimer cet élément ?\nCette action est irréversible.";
	}
	else if(selectedItems == -1){
		msg = "Voulez-vous vraiment supprimer tous les éléments ?\nCette action est irréversible.";
	}
	else{
		msg = "Voulez-vous vraiment supprimer ces " + selectedItems + " éléments ?\nCette action est irréversible.";
	}
	return confirm(msg);
}

/**
Author : Mehrez MAROUANI
Date : Oct. 10 2007
Description : Method used to check if there is at least one checkbox checked before apply a modification.
Params: 
	formName = the id of the Html form
	elementName = the name of the checkbox.
Return : the number of selected items.
**/
function atLeastOne(formName, elementName){
		
		var cboxArray = $(formName)[elementName];
		var l = cboxArray.length;
		var selectedItems = 0;
		for(i=0;i<l;i++){
			if(cboxArray[i].checked){
				selectedItems++;
			}
		}
		if(selectedItems == 0){
			alert("Vous devez sélectionner au moins un élément pour pouvoir appliquer des modifications !");
		}
		return selectedItems;
}

