trim = function(myString){
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
} 

inputFocus = function(element){         
	if (element.value == element.defaultValue){  
		element.value = ''; 
	}  
}  
   
inputBlur = function(element){     
	if (element.value == ''){  
		element.value = element.defaultValue;  
	}
} 
language = function(id){
	var sURL = unescape(window.location.pathname);
	var tlang = "?user.locale=";
	var lang = tlang+id;
    window.location.href = sURL+lang;
}

/*
 *	This function reload the page in the chosen language
 */

changeLang = function(selectId){
	var sel = $(selectId);
	var lang = sel.options[sel.selectedIndex].value;
	var elKey = null;
	var curUrl = document.location.href.split("/");
	for(var i=0;i < curUrl.length;i++){
		if(curUrl[i] == "portal"){
			elKey = i + 1;
		}
	}
	curUrl.splice(elKey, 1, lang);
	var newUrl = "http:";
	for(var i=1;i < curUrl.length;i++){
		newUrl+= ("/" + curUrl[i]);
	}
	if(lang != null){
		document.location.href = newUrl;
	}
}

/*
 *	This function determine the version of
 *	internet Explorer browser 
 */

ieVersion = function(){
	var browser = navigator.appName;
	var b_version = navigator.appVersion;
	var version = parseFloat(b_version);
}

/*
 *	This function add a class to a given element
 *	Parameters:
 *		- String cl = the class name to be added
 *		- String d = The id of the element to add the class to
 */

function showActive(cl, d){
	var i, ob, tA, h = document.location.href;
	if(document.getElementById){
	  ob=(d)?document.getElementById(d):document;
	    if(ob){
		  tA = ob.getElementsByTagName('A');
		  for(i=0;i<tA.length;i++){
			if(tA[i].href==h){
		      tA[i].className=cl;
			}
		  }
		}
	}
}

/*
 * 	This function clear all the fields of a given form
 */

clearField = function(formName) {
	document.forms[formName].reset();
}

/*
 * 	This function clear the field of the given id
 */

clearId = function(id){
	$(id).update('');
}

loadImg = function(screen){
	Effect.Appear(screen); 
	return false;
}
hideImg = function(screen){
	$(screen).fade({ duration: 0.5 }); 
	return false;
}
loadSolution = function(base, lang, product){
	document.location.href= base+"/portal/"+lang+"/solutions/"+product+".html";
}

/* ########################### tools ############################### */

/*
 *	This function change the font color of the active menu  
 */

manageMenu = function(topic, elementId){
	$(topic).style.cssText = "color:#000";
	if(elementId){
		$(elementId).style.cssText = "color:#000";
	}
}

/*
 *	This function disable the return key  
 */

stopRKey = function(evt) {
   var evt = (evt) ? evt : ((event) ? event : null);
   var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
   if ((evt.keyCode == 13) && (node.type=="text")) {
	   return false;
   }
}

/*
 *	This function gets a given URL parameter
 */

getUrlParam = function(name){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if(results == null)
    return "";
  else
    return results[1];
}

/*
 * This function format the given number with the given parameters
 * 
 * Parameters (optional):
 *	- Int decimals: number of decimals (for exemple: 2)
 *	- String sign: the sign preceding the decimals (for exemple: "," or ".")
 *	- String splitter: the sign used to separate thousands (for exemple: "," or " ")
 */

Number.prototype.formatNumber = function (decimals, sign, splitter) {
	var _sNumber = String(this), i, toReturn = "", _sDecimals = "";
	if (decimals == undefined) decimals = 2;
	if (sign == undefined) sign = '.';
	if (splitter == undefined) splitter = ' ';
	
	function splitThousands (sNumber) {
		var sReturn = "";
		while (sNumber.length % 3 != 0) {
			sNumber = "0"+sNumber;
		}
		for (i = 0; i < sNumber.length; i += 3) {
			if (i ==  sNumber.length-1) splitter = '';
			sReturn += sNumber.substr(i, 3)+splitter;
		}
		while (sReturn.substr(0, 1) == "0") {
			sReturn = sReturn.substr(1);
		}
		return sReturn.substr(0, sReturn.lastIndexOf(splitter));
	}
	if (_sNumber.indexOf('.') == -1) {
		for (i = 0; i < decimals; i++) {
			_sDecimals += "0";
		}
		toReturn = splitThousands(_sNumber)+sign+_sDecimals;
	} else {
		var sDecimalsTmp = (_sNumber.substr(_sNumber.indexOf('.')+1));
		if (sDecimalsTmp.length > decimals) {
			var nDecimalsManquantes = sDecimalsTmp.length - decimals;
			var nDiv = 1;
			for (i = 0; i < nDecimalsManquantes; i++) {
				nDiv *= 10;
			}
			_sDecimals = Math.round(Number(sDecimalsTmp) / nDiv);
		}else{
			_sDecimals = sDecimalsTmp;
		    for(i = sDecimalsTmp.length; i < decimals; i++) {
		    	_sDecimals += "0";
		    }
		}
		toReturn = splitThousands(_sNumber.substr(0, _sNumber.indexOf('.')))+String(sign)+_sDecimals; 
	}
	/*
	if(toReturn.length == decimals + 1){
		toReturn ='0'+toReturn;
	}
	*/
	
	return toReturn;
}

/*
 * This function checks if the authentified user as the given permission
 * and make visible the given element.
 * 
 * Parameters:
 *	- String aPermission: the permission to verify
 *	- String elementId: the id of the element to make visible if the permission is allowed
 */

checkRegistrationPermission = function(elementId){
	successCallback = function(result){		
		if(result.permissions.createConsumers == true) $(elementId).show();
	}
	new Ajax.Request("$[settings:portal.cm.base.url]/json-rest/cm/cmsession/getSessionData", wrapCallback(successCallback));
}

/*
 * This function gets the given cookie
 * 
 * Parameters:
 *	- String name: the name of the cookie to get
 */
readCookie = function(name){
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}

