function addslashes(str)
{
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\\/g,'\\\\');
    //    str=str.replace(/\0/g,'\\0');
    return str;
}
function htmlspecialchars(ch)
{
    ch = ch.replace(/&/g,"&amp;")
    ch = ch.replace(/\"/g,"&quot;")
    ch = ch.replace(/\'/g,"&#039;")
    ch = ch.replace(/</g,"&lt;")
    ch = ch.replace(/>/g,"&gt;")
    return ch
}
function stripslashes(str)
{
    str=str.replace(/\\'/g,'\'');
    str=str.replace(/\\"/g,'"');
    str=str.replace(/\\\\/g,'\\');
    str=str.replace(/\\0/g,'\0');
    return str;
}
function nl2br(str)
{
    return str.replace(/\n/g, '<br />');
}
function trim(string)
{
    return string.replace(/(^\s*)|(\s*$)|(\r?\n)/g, '');
}

/*******
descr: Cache le div passé en parametre
param: id du div
********/
function Hide(div){
    document.getElementById(div).style.display='none';
}

/*******
descr: Affiche le div passé en parametre
param: id du div
********/
function Show(div){
    document.getElementById(div).style.display='block';
}

/*******
descr: Renvoie le code de la touche appuyer
param: event
********/
function getkey(e)
{
    if (window.event)
    return window.event.keyCode;
    else if (e)
    return e.which;
    else
    return null;
}

/*******
descr: Fonction qui limites les caractères saisies dans un input
param: event, liste de caractères autorisé
********/
function goodchars(e, goods)
{
    var key, keychar;
    key = getkey(e);
    if (key == null) return true;

    // get character
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    goods = goods.toLowerCase();

    // check goodkeys
    if (goods.indexOf(keychar) != -1)
    return true;

    // control keys
    if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
    return true;

    // else return false
    return false;
}





/*******
descr: Récupère la hauteur de l'écran
param: ---
********/
function getWindowHeight() {
    var windowHeight = 0;
    if (document.documentElement && document.documentElement.clientHeight) {
        windowHeight = document.documentElement.clientHeight;
    }
    else if (typeof(window.innerHeight) == 'number') {
        windowHeight = window.innerHeight;
    }
    else if (document.body && document.body.clientHeight) {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}

/*******
descr: Div alerte
param: le texte à afficher
********/
function showAlerte(id, txt)
{
    document.getElementById(id+'_message').innerHTML = nl2br(txt);
    showPopup(id);
}

function closeAlerte(id)
{
    hidePopup(id);
}

function showConfirmation(id, txt, action)
{
    document.getElementById(id+'_message').innerHTML = nl2br(txt);
    document.getElementById(id+'_ok').href = action;
    showPopup(id);
}

function showPrompt(id, txt, action)
{
    $(id+'_form').action=action;
    document.getElementById(id+'_message').innerHTML = nl2br(txt);
    showPopup(id);
}


/**************************************/
/**************************************/
/**************************************/
function ajax_fieldvalue_exist_control(classname, field, auth, value) {

    var code;
    var request = new Request.JSON({
        url: '/actions/action-fieldvalue/',
        async: false,
        onComplete: function(jsonObj) {
            code=jsonObj.code;
        }
    }).get({'classname':classname, 'field':field, 'auth':auth, 'value':value});

    return((code==0)?true:false)

}

function check_text(value) {
    if (!trim(value).match(/^.+$/i)) {
        return false;
    }
    return true;
}

function check_format(format, value) {
    if (!trim(value).match(format)) {
        return false;
    }
    return true;
}

function check_select(field) {
    if ((field.options[field.options.selectedIndex].value != '') && (field.options[field.options.selectedIndex].value != 0)) {
        return true
    }
    return false;
}

function check_checkbox(field) {
    if (field.checked) {
        return true
    }
    return false;
}

function check_mult_checkbox(field, req) {
    var checked = 0;
    var elements = document.getElementsByName(field+'[]');
    for (var i=0; i<elements.length; i++) {
        if (elements[i].checked) {
            if (++checked>=req) {
                return true;
            }
        }
    }
    return false;
}

function check_image(value) {
    if (!trim(value).match(/(gif|jpeg|jpg|png$)|(^$)/i)) {
        return false;
    }
    return true;
}

function check_confirm(value1, value2) {
    if ((trim(value1)!='') && (value1 != value2)) {
        return false;
    }
    return true;
}

function check_radio(field) {
    var radios = document.getElementsByName(field);
    checked=false;
    for(var i=0; i<radios.length; i++) {
        if (radios[i].checked) {
            return true;
        }
    }
    return false;
}

function select_all(field) {
    for(var i=0; i<field.options.length; i++) {
        field.options[i].selected=true;
    }
}

function checkForm() {
    var error = false;
    var error_str = 'Merci de corriger les champs suivants :\n';
    var CheckId;
    var formId;

    switch (arguments.length) {
        case 1 :
        CheckId = arguments[0];
        formId  = arguments[0];
        break;
        case 2 :
        formId = arguments[0];
        CheckId = arguments[1];
        break;
        default :
        exit;
        break;
    }

    with (document.forms[formId]) {

        var control = getControl(CheckId);
        var check_tab = control.check_tab;
        var combos_tab = control.combos_tab;

        for (var i=0; i<check_tab.length; i++) {
        	
        	if(!$(check_tab[i][0])) {
        		alert('Champ '+check_tab[i][0]+' introuvable!');
        		return false;
        	}
        	
            switch(check_tab[i][1]) {
                case 'ajax_fieldvalue_exist_control' :
                if (!ajax_fieldvalue_exist_control(check_tab[i][3], check_tab[i][4], check_tab[i][5], elements[check_tab[i][0]].value)) {
                    error = true;
                    error_str += '- '+check_tab[i][2].replace('{value}', elements[check_tab[i][0]].value)+'\n';
                }
                break;
                case 'check_text' :
                if (!check_text(elements[check_tab[i][0]].value)) {
                    error = true;
                    error_str += '- '+check_tab[i][2]+'\n';
                }
                break;
                case 'check_format' :
                if (!check_format(check_tab[i][3], elements[check_tab[i][0]].value)) {
                    error = true;
                    error_str += '- '+check_tab[i][2]+'\n';
                }
                break;
                case 'check_select' :
                if (!check_select(elements[check_tab[i][0]])) {
                    error = true;
                    error_str += '- '+check_tab[i][2]+'\n';
                }
                break;
                case 'check_checkbox' :
                if (!check_checkbox(elements[check_tab[i][0]])) {
                    error = true;
                    error_str += '- '+check_tab[i][2]+'\n';
                }
                break;
                case 'check_mult_checkbox' :
                if (!check_mult_checkbox(check_tab[i][0], check_tab[i][3])) {
                    error = true;
                    error_str += '- '+check_tab[i][2]+'\n';
                }
                break;
                case 'check_image' :
                if (!check_image(elements[check_tab[i][0]].value)) {
                    error = true;
                    error_str += '- '+check_tab[i][2]+'\n';
                }
                break;
                case 'check_confirm' :
                if (!check_confirm(elements[check_tab[i][0]].value, elements[check_tab[i][0]+'_confirm'].value)) {
                    error = true;
                    error_str += '- '+check_tab[i][2]+'\n';
                }
                break;

                case 'check_radio' :
                if (!check_radio(check_tab[i][0])) {
                    error = true;
                    error_str += '- '+check_tab[i][2]+'\n';
                }
                break;
            }
        }


        if(error) {
            showAlerte("alerte", error_str);
            return false;
        }

        for(var i=0; i<combos_tab.length;i++) {
            select_all(elements[combos_tab[i]+'[]']);
        }

        return true;

    }
}


function ajax_feed(feed_type, from, to) {
    var from_value=$(from).get('value');
    var req = new Request(
    {
        url:'/actions/action-ajax_feed',
        method:'post',
        async:true,
        onSuccess: function(json) {
            var result=JSON.decode(json);
            if (result.code==0) {
                $(to).options.length=0;
                $(to).options[0]=new Element('option', {'value':'', 'text':'---'});
                for(var i=0; i<result.result.length;i++) {
                    $(to).options[i+1]=new Element('option', {'value':result.result[i].value, 'text':result.result[i].text});
                }
            }
            if(result.message!==undefined) {
                showAlerte('alerte', result.message);
            }
        }
    }
    );
    req.send('feed_type='+feed_type+'&from_value='+from_value);
}

function ajax_action(url, confirm, callback) {
    var req = new Request(
    {
        url:url,
        method:'post',
        async:true,
        onSuccess: function(json) {
            var obj=JSON.decode(json);
            if (this.callback) {
                this.callback(obj);
            }
            if(obj.message!==undefined) {
                showAlerte('alerte', obj.message);
            }
        }
    }
    );
    req.callback=callback;
    if (confirm!=undefined && confirm!='') {
        showConfirmation('confirmation', confirm, '#');
        var clink = $('confirmation_ok');
        clink.req = req;
        clink.addEvent('click',  function(e) {
            hidePopup('confirmation');
            this.req.send();
            return false;
        });

    }
    else {
        alert('no click');
        req.send();
    }
}


