function LimitToDecimal(field) {

	var val = field.value;
	val = val.replace(/,/g, '.');
	val = val.replace(/[^0-9\.]/g, '');
	field.value = val;

}


function comaField(champ) {
	var valeur = champ.value;
	pos = valeur.indexOf(",");
	if (pos >= 0) {
		valeur = valeur.substr(0, pos) + "." + valeur.substr(pos+1);
	}
	champ.value = valeur;
}

function DateFrToDate(date) {
	date = date.replace(/^0+/g, "");
	date = date.replace(/\/0+/g, "/");
	var s = date.split('/');
	return new Date(parseInt(s[2]), parseInt(s[1])-1, parseInt(s[0]));
}

function DateFrToSql(date) {
	var s = date.split('/');
	return s[2]+"-"+s[1]+"-"+s[0];
}

function TimeMinuteToSql(hm) {
	var hmpos = hm;
	if (hmpos < 0) hmpos = -hmpos;
	var h = Math.floor(hmpos/60);
	var m = hmpos%60;
	if (h < 10) h = '0' + h;
	if (m < 10) m = '0' + m;
	return (hm<0?'-':'') + h + ':' + m;
}

function DateToFr(date) {
	var day = (date.getDate()).toString();
	var month = (date.getMonth()+1).toString();
	var year = (date.getFullYear()).toString();
	if (day.length < 2) day = '0' + day;
	if (month.length < 2) month = '0' + month;
	return day + '/' + month + '/' + year;
}

function TimeSqlToMinute(t) {
	var s = t.split(':');
	return parseInt(s[0].replace(/^0/g,""))*60 + parseInt(s[1].replace(/^0/g,""));
}


function urlRemoveVariable(url, variable) {
	return url.replace(new RegExp(variable + '=[^&]*', 'g'), '').replace(new RegExp('&+', 'g'), '&').replace(new RegExp('&$', 'g'), '');
}
function urlRemoveVariables(url, variables) {
	for(var variable in variables)
		url = urlRemoveVariable(url, variable);

	return url;
}
function urlAddVariable(url, variable, value) {
	url = urlRemoveVariable(url, variable);
	if (url.indexOf('?',0) === false) return url + '?' + variable + '=' + value;
	else	return url + '&' + variable + '=' + value;
}

function Post(url, args) {
	var id = 'frmpostid' + Math.random();
	var html = '<form action="'+url+'" method="post" id="'+id+'">';
	for (key in args) {
		html += '<input type="hidden" name="'+key+'" value="'+args[key]+'" />';
	}
	html += '</form>';
	$('body').append(html);
	document.getElementById(id).submit();
}

function formise(jQueryObject){
    	jQueryObject.find("a.ajaxFormTrigger, a[type=submit]").click(submitForm);
    	jQueryObject.find("input[type=text].ajaxFormTrigger").change(submitForm);
    	jQueryObject.find("input.ajaxFormTrigger").not("[type=text]").click(submitForm);
    	jQueryObject.find("select.ajaxFormTrigger").change(submitForm);
  }

function submitForm(){
	var jQueryObject = $(this);
	//var jQueryParentObject = $(this).parent();//l'element HTML declencheur doit etre seul dans sa balise parente
	var method = jQueryObject.attr("method") != undefined ? jQueryObject.attr("method"): "POST";
	var action = jQueryObject.attr("action");
	var ajax = jQueryObject.attr('ajax');
    var myForm = jQueryObject.attr('formId');
    var type = jQueryObject.attr('typeOfData') != undefined ? jQueryObject.attr('typeOfData') : 'json';
   
    if (myForm != undefined) {
        var fields = '{';
        $('#' + myForm + ' .field').each(function() {
            if ($(this).hasClass('fckeditor')) {
                var editor = FCKeditorAPI.GetInstance($(this).attr('id'));
                var value = editor.GetHTML();
            } else {
                var value = $(this).val();
            }

            value = value.replace(/\"/g,"\\\"");

            var key = $(this).attr('name');
            if (fields != '{') fields += ',';
            fields += '"' + key + '":' + '"' + value + '"';
        });
        fields += '}';
    } else {
        fields = jQueryObject.attr("fields");   
    }

    eval("var fields = " + fields);

	//jQueryObject.trigger('beforeformpost');

	if(ajax != undefined){
		switch(method){
			case "POST":
				$.post(action, fields, function(data){
                    jQueryObject.trigger('afterformpost', data);
				}, type);
				break;
			case "GET":
				$.get(action, fields, function(data){
                    jQueryObject.trigger('afterformpost', data);
				}, type);
				break;
		}
	}else{
		switch(method){
			case "POST":
				Post(action, fields);
				break;
			case "GET":
				Get(action, fields);
				break;
		}
	}

    return false;
}

function changeOnEnter(jQueryObject){
	jQueryObject.find("input[type=text]").keydown(function(key){
		if(key.keyCode == 13 && $(this).attr("changeOnEnter") != undefined)	$(this).trigger('change');
	});
}

function initCommandElement(jQueryObject) {
    jQueryObject('[command]').click(function(e) {
        switch($(this).attr('command')) {
            case 'hideShow':
                if ($(this).attr('hide')) {
                    $('#' + $(this).attr('hide')).hide();
                }
                if ($(this).attr('show')) {
                    $('#' + $(this).attr('show')).show();
                }
            break;
        }

        e.stopPropagation();
        return false;
    });
}


function initFckEditorField(jQueryObject) {
    jQueryObject('.fckeditor').each(function() {
        var oFCKeditor = new FCKeditor($(this).attr('id'));
        oFCKeditor.BasePath = "/fckeditor/";
        oFCKeditor.Width = 700;
        if ($(this).attr('id') == 'description') {
        	oFCKeditor.Height = 150;
        } else {
        	oFCKeditor.Height = 300;
        }
        oFCKeditor.Config["ToolbarCanCollapse"] = false;
        oFCKeditor.ToolbarSet = $(this).attr('toolbar');
        oFCKeditor.ReplaceTextarea();
    });

    return false;
}