/*Created by Bellegarde, the Master Programmer*/
if(!umen) var umen = {};
if(!umen.defined) umen.defined = {};
umen.defined.common = true;
if(!umen.util) umen.util = {};
if(!umen.event) umen.event = {};

if(!umen.util.elaborateException){
	umen.util.elaborateException = function(err, full){
		var el = "";
		if(full){
			for (var prop in err){ 
			   el += "property: "+ prop+ " value: ["+ err[prop]+ "]\n";
			}
			el += "toString(): " + " value: [" + err.toString() + "]";
		}
		return el;
	};
};

if(!umen.event.onBeforeUnload){
	umen.event.onBeforeUnloadCalls = [];
	window.onbeforeunload = function(event){
		var str = "";
		for(var i in umen.event.onBeforeUnloadCalls){
			var si = umen.event.onBeforeUnloadCalls[i];
			if(si) si = si(event);
			if(str.length > 0 && si){ str +="\n\n"; }
			if(si) str += si;
		}
		if(str){
			return str;
		}
	};
	umen.event.onBeforeUnload = function(callback){		
		umen.event.onBeforeUnloadCalls.push(callback);
	};
};

if(!umen.util.isString){
	umen.util.isString = function(str){
		return typeof str == "string";
	};
};

if(!umen.util.isStringNullOrEmpty){
	umen.util.isStringNullOrEmpty = function(str){
		var type =  typeof str;
		return type == "undefined" || str == null || (type == "string" && str.length==0);
	};
};

if(!umen.util.isNullOrEmptyOrNotString){
	umen.util.isNullOrEmptyOrNotString = function(str)
	{
		return !(umen.util.isString(str) && !umen.util.isStringNullOrEmpty(str));
	};
};

if(!umen.util.isJQuery){
	umen.util.isJQuery = function(obj,testSize)
	{
		return obj && obj instanceof jQuery && (!testSize || obj.size() > 0);
	};
};

if(!umen.util.isNumber){
	umen.util.isNumber = function(val){
		return typeof val == "number";
	};
};

if(!umen.util.getNumber){
	umen.util.getNumber = function(source, defaultValue){
		return isNaN(source) ? (defaultValue ? defaultValue : null) : Number(source);
	};
};

if(!umen.util.getBoolean){
	umen.util.getBoolean = function(source, defaultValue){
		return typeof source == "undefined" ? (defaultValue ? defaultValue : null) : Number(source) > 0 || String(source).toLowerCase() == "true";
	};
};

if(!umen.util.numberValidationFunction){
	umen.util.numberValidationFunction = function(integer, min, max, emptyIsZero, emptyIsValid, acceptComma){
		return function(source,args){
			try{
				
				var fixComma = acceptComma==true ? function(v){return v.replace(",",".");}:function(v){return v;};
				var typeSource = typeof source;
				var n = null;
				
				if(typeSource == "number"){
					n = source;
				}
				else if(typeSource == "string"){
					n = Number(fixComma(source.trim()));
				}
				else{
					n = fixComma(umen.util.getControl(source, true).val().trim());
				}
				
				var str = !n || n == null ? '' : String(n);
				if(emptyIsValid != true && str == '' && emptyIsZero != true){
					args.IsValid  = false;
				}
				else if(str == '' && emptyIsValid == true){
					args.IsValid  = true;
				}
				else if((emptyIsZero == true || str != '') && !isNaN(n = Number(str))){
					var ok = true;
					if(!integer || (integer == true && n == Math.floor(n))){
						if(typeof min == "number"){
							if(n<min){ ok = false; }
						}
						if(typeof max == "number"){
							if(n>max){ ok = false; }
						}
					}
					else{
						ok = false;
					}
					args.IsValid  = ok;
				}
				else{					
					args.IsValid  = false;
				}
			}catch(e){
				args.IsValid = false;
				alert(e);
			}
		}
	};
};

if(!umen.util.numberOffset){
	umen.util.numberOffset = function(control,integer,offset,min,max, emptyIsZero, emptyIsValid, acceptComma){
		try
		{
			var $ctrl = umen.util.getControl(control, true);
			var valArgs = new Object();
			valArgs.IsValid = true;
			var valFunct = umen.util.numberValidationFunction(integer, min, max, emptyIsZero, emptyIsValid, acceptComma);
			valFunct($ctrl, valArgs);
			if(valArgs.IsValid == true){
				var newVal = Number($ctrl.val()) + offset;
				valFunct(newVal, valArgs);
				if(valArgs.IsValid == true){
//					var dv = newVal/offset;
//					var f = Math.floor(dv);
//					var s = newVal == 0 ? 0 : newVal/Math.abs(newVal);
//					if(dv != f){
//						newVal = s < 0 ? Math.ceil(newVal) : Math.floor(newVal);
//					}
					newVal = newVal == 0 && emptyIsZero == true ? "" : String(newVal);
					$ctrl.val(String(newVal));
				}
			}
		}
		catch(e)
		{
			alert(e);
		}
	};
};

if(!umen.util.getControl){
	umen.util.getControl = function(source,jquery){
		var $ctrl = null;
		if(typeof source == "string"){
			$ctrl = jquery==true ?  $(["#",source].join('')) : document.getElementById(source);
		}else if(umen.util.isJQuery(source,true)){
			$ctrl = jquery==true ? source : source.get(0);
		}
		else if(source && typeof source.controltovalidate == "string")
		{
			$ctrl = jquery == true ?  $(["#",source.controltovalidate].join('')) : document.getElementById(source.controltovalidate);
		}
		else if(source && typeof source.tagName == "string" && source.tagName.length > 0){
			$ctrl = jquery==true ? $(source) : source;
		}
		return $ctrl;
	};
};

if(!umen.util.queryString){
	umen.util.queryString = function(data) {
		var qs = data.query == null ? window.location.search : data.query;
		var r = "";
		if (data.key) {
			var i = qs.indexOf("?" + data.key);
			if (i < 0) {i = qs.indexOf("&" + data.key);}
			if (i < 0) {i = qs.indexOf(data.key);}
			if (i >= 0) {
				var j = qs.indexOf("=", i);
				if (j >= 0) {
					var k = qs.indexOf("&", j);
					if (k >= 0) {r = qs.substring(j + 1, k);}
					else {r = qs.substring(j + 1);}
				}
			}
			return unescape(r);
		}
	};
};

// Join un array d'objets quelconques. la conversion de chaque item en string est assurée par le callback.
if (!umen.util.arrayJoin) {
    umen.util.arrayJoin = function (separator, arr, callback) {
		var strArr = [];
        if(arr && arr.length){
			var n = arr.length;
			for(var i = 0; i<n; i++){	
				var obj = arr[i];
				var tmpStr = callback ? callback(obj) : obj.toString();
				strArr.push(tmpStr);
			}
		}
		return strArr.join(separator);
    };
};

// retourne le premier index trouvé grâce au callback
if (!umen.util.arrayForEach) {
    umen.util.arrayForEach = function (arr, callback) {
        if(arr && arr.length){
			var n = arr.length;
			for(var i = 0; i<n; i++){	
				callback(arr[i]);
			}
		}
        return index;
    };
};

// retourne le premier index trouvé grâce au callback
if (!umen.util.arrayFindIndex) {
    umen.util.arrayFindIndex = function (arr, callback, startIndex) {
        var index = -1;
        if(arr && arr.length){
			var n = arr.length;
			for(var i = startIndex ? startIndex : 0; i<n; i++){	
				var obj = arr[i];
				if (callback(obj)) {
					index = i;
					break;
				}
			}
		}
        return index;
    };
};

// retire des objets d'un array
if (!umen.util.arraySplice) {
    umen.util.arraySplice = function (arr, callback) {
        if(arr && arr.length){
			for(var i = arr.length-1; i>=0; i--){
				if (callback(arr[i])) {
					arr.splice(i,1);
				}
			}
		}
    };
};

// retourne le premier objet trouvé grâce au callback
if (!umen.util.arrayFind) {
	umen.util.arrayFind = function (arr, callback, startIndex){
		var ret = null;
        if(arr && arr.length){
			var n = arr.length;
			for(var i = startIndex ? startIndex : 0; i<n; i++){	
				var obj = arr[i];
				if(callback(obj)){
					ret = obj;
					break;
				}
			}
		}
		return ret;
	};
};

// retourne tous les objets trouvés grâce au callback
if (!umen.util.arrayFindAll) {
	umen.util.arrayFindAll = function (arr, callback, startIndex){
		var list = [];
        if(arr && arr.length){
			var n = arr.length;
			for(var i = startIndex ? startIndex : 0; i<n; i++){	
				var obj = arr[i];
				if(callback(obj)){
					list.push(obj);
				}
			}
		}
		return list;
	};
};

// retourne tous les objets trouvés grâce au callback
if (!umen.util.arrayConvert) {
	umen.util.arrayConvert = function(arr, callback, returnNullEntries){
		var list = [];
        if(arr && arr.length){
			var n = arr.length;
			for(var i = 0; i<n; i++){	
				var obj_i = arr[i];
				var obj = callback(obj_i);
				if(obj != null){
					list.push(obj);
				}
				else if(returnNullEntries){
					list.push(null);
				}
			}
		}
		return list;
	};
};

// enleve une class css d'un element dom, beaucoup plus rapide que jquery (encore plus rapide avec regex)
if (!umen.util.removeClass) {
	umen.util.removeClass = function(node, class_name_or_regex){
		if(typeof class_name_or_regex == "string"){
			class_name_or_regex = new RegExp(["(^| )","( |$)"].join(class_name_or_regex),"i");
		}
		node.className = node.className.replace(class_name_or_regex, " ");
	};
};

// verifie si un element du dom contient une classe css, beaucoup plus rapide que jquery (encore plus rapide avec regex)
if (!umen.util.hasClass) {
	umen.util.hasClass = function(node, class_name_or_regex){
		if(typeof class_name_or_regex == "string"){
			class_name_or_regex = new RegExp(["(^| )","( |$)"].join(class_name_or_regex),"i");
		}
		return class_name_or_regex.test(node.className);
	};
};

/*PROTOTYPES*/
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
};

String.format = function() {
    var s = arguments[0];
    for (var i = 0; i < arguments.length - 1; i++) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        s = s.replace(reg, arguments[i + 1]);
    }
    return s;
};
