var TEXT_SAVE_EDIT ="";


function trim (myString)
{
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}


function lostpass()
{
    $('#div_genpass').toggle();
}

function uploadimg()
{
    $('#div_uploadimg').toggle();
}

function clicklogin()
{
    $('#textPublicSubmit').hide();
    $('#loginLoad').show();
}

function cancellostpass()
{
    $('#plogin').val('');
    $('#pemail').val('');
    lostpass();
}

function confirmlostpass()
{
    $('#genpassA').hide();
    $('#genpassB').hide();
    $('#genpassLoad').show();
    
    var plogin = $('#plogin').val();
    var pemail = $('#pemail').val();

    $.ajax({
        url: 'public/ajax/call-genpass.php',
        type: 'POST',
        data: 'plogin='+encodeURIComponent(plogin)
                + '&pemail='+encodeURIComponent(pemail),

        success: function(result)
        {
            tresult = result.split('&&&');
            if(tresult[0] == "Ok")
            {
                $('#plogin').val('');
                $('#pemail').val('');
                alert(tresult[1]);
                $('#genpassA').show();
                $('#genpassB').show();
                $('#genpassLoad').hide();
                lostpass();
            }
            else
            {
                alert(result);
                $('#genpassA').show();
                $('#genpassB').show();
                $('#genpassLoad').hide();
            }
        }
    });
}

function referSubmbit()
{
    $('#ReferSubmit').hide();
    $('#referLoad').show();

    var uLName = $('#uLName').val();
    var uFName = $('#uFName').val();
    var uNName = $('#uNName').val();
    var uTel = $('#uTel').val();
    var uEmail = $('#uEmail').val();
    var fLName = $('#fLName').val();
    var fFName = $('#fFName').val();
    var fTel = $('#fTel').val();
    var fEmail = $('#fEmail').val();

    $.ajax({
        url: 'public/ajax/call-referer.php',
        type: 'POST',
        data: 'uLName='+encodeURIComponent(uLName)
                + '&uFName='+encodeURIComponent(uFName)
                + '&uNName='+encodeURIComponent(uNName)
                + '&uTel='+encodeURIComponent(uTel)
                + '&uEmail='+encodeURIComponent(uEmail)
                + '&fLName='+encodeURIComponent(fLName)
                + '&fFName='+encodeURIComponent(fFName)
                + '&fTel='+encodeURIComponent(fTel)
                + '&fEmail='+encodeURIComponent(fEmail),

        success: function(result)
        {
            tresult = result.split('&&&');
            if(tresult[0] == "Ok")
            {
                $('#uLName').val('');
                $('#uFName').val('');
                $('#uNName').val('');
                $('#uTel').val('');
                $('#uEmail').val('');
                $('#fLName').val('');
                $('#fFName').val('');
                $('#fTel').val('');
                $('#fEmail').val('');
                alert(tresult[1]);
                $('#ReferSubmit').show();
                $('#referLoad').hide();
            }
            else
            {
                alert(result);
                $('#ReferSubmit').show();
                $('#referLoad').hide();
            }
        }
    });
}

function signinSubmbit()
{
    $('#signinSubmbitB').hide();
    $('#signinLoad').show();

    var iNName = $('#iNName').val();
    var iLName = $('#iLName').val();
    var iFName = $('#iFName').val();
    var iDateNaissance = $('#date').val();
    var iTel1R = $('#iTel1R').val();
    var iTel11 = $('#iTel11').val();
    var iTel12 = $('#iTel12').val();
    var iEmail = $('#iEmail').val();
    var iCode = $('#iCode').val();
    var iSexe = $('#iSexe').val();

    $.ajax({
        url: 'public/ajax/call-inscription.php',
        type: 'POST',
        data: 'iNName='+encodeURIComponent(iNName)
                + '&iLName='+encodeURIComponent(iLName)
                + '&iFName='+encodeURIComponent(iFName)
                + '&iDateNaissance='+encodeURIComponent(iDateNaissance)
                + '&iTel1R='+encodeURIComponent(iTel1R)
                + '&iTel11='+encodeURIComponent(iTel11)
                + '&iTel12='+encodeURIComponent(iTel12)
                + '&iEmail='+encodeURIComponent(iEmail)
                + '&iSexe='+encodeURIComponent(iSexe)
                + '&iCode='+encodeURIComponent(iCode),

        success: function(result)
        {
            tresult = result.split('&&&');
            if(tresult[0] == "Ok")
            {
                alert(tresult[1]);
                location.href="?s=BV";
            }
            else
            {
                alert(result);
                $('#signinSubmbitB').show();
                $('#signinLoad').hide();
            }
        }
    });
}

function signinCal(userID,calID,msg)
{
    var cancontinue=false;
    var reponse = "";

    if(msg!="")
    {
        reponse = prompt(msg,"");
        if(reponse!=null)
        {
            cancontinue = true;
        }
    }
    else
        cancontinue = true;

    
    if(cancontinue)
    {
        $.ajax({
            url: 'public/ajax/call-calendrier.php',
            type: 'POST',
            data: 'userID='+encodeURIComponent(userID)
                    + '&calID='+encodeURIComponent(calID)
                    + '&reponse='+encodeURIComponent(reponse),

            success: function(result)
            {
                tresult = result.split('&&&');
                if(tresult[0] == "Ok")
                {
                    alert(tresult[1]);
                    $('#div_cal_b'+calID).html(tresult[2]);
                }
                else
                {
                    alert(result);
                }
            }
        });
    }
}

function signinTopCal(userID,calID,msg)
{
    var cancontinue=false;
    var reponse = "";

    if(msg!="")
    {
        reponse = prompt(msg,"");
        if(reponse!=null)
        {
            cancontinue = true;
        }
    }
    else
        cancontinue = true;


    if(cancontinue)
    {
        $.ajax({
            url: 'public/ajax/call-calendrier.php',
            type: 'POST',
            data: 'top=top&userID='+encodeURIComponent(userID)
                    + '&calID='+encodeURIComponent(calID)
                    + '&reponse='+encodeURIComponent(reponse),

            success: function(result)
            {
                tresult = result.split('&&&');
                if(tresult[0] == "Ok")
                {
                    alert(tresult[1]);
                    $('#div_cal_b'+calID).html(tresult[2]);
                }
                else
                {
                    alert(result);
                }
            }
        });
    }
}

$(function() {
	$('#AddBabiB').click(function()
        {
            $('#AddBabiB').hide();
            $('#eDisk').hide();
            $('#loadB').show();

            var TypeID = $('#TypeID').val();
            $('#TypeID').attr('disabled', 'disabled');
            var Sujet = $('#Sujet').val();
            $('#Sujet').attr('disabled', 'disabled');
            var Description = $('#Description').val();
            $('#Description').attr('disabled', 'disabled');

            $.ajax({
                    url: 'public/ajax/call-babillard.php',
                    type: 'POST',
                    data: 'TypeID='+encodeURIComponent(TypeID)
                        +"&Sujet="+encodeURIComponent(Sujet)
                        +"&Description="+encodeURIComponent(Description),

                    success: function(result)
                    {
                        tresult = result.split('&&&');
                        if(tresult[0] == "Ok")
                        {
                            alert(tresult[1]);
                            location.href="?s=babillard";
                        }
                        else
                        {
                            $('#loadB').hide();
                            $('#AddBabiB').show();
                            $('#eDisk').show();
                            alert(result);
                            $('#Sujet').removeAttr('disabled');
                            $('#TypeID').removeAttr('disabled');
                            $('#Description').removeAttr('disabled');
                        }
                    }
            });

            return false;
	});
});

function delMSG(id,msg)
{
    if(confirm(msg))
    {
        $.ajax({
                url: 'public/ajax/call-messagerie.php',
                type: 'POST',
                data: 'delmsgid='+encodeURIComponent(id),

                success: function(result)
                {
                    if(result!="Ok")
                    {
                        alert(result);
                    }
                    else
                        location.href="?s=Messagerie";
                }
        });
    }
}

function delsMSG(id,msg)
{
    if(confirm(msg))
    {
        $.ajax({
                url: 'public/ajax/call-messagerie.php',
                type: 'POST',
                data: 'delmsgid='+encodeURIComponent(id),

                success: function(result)
                {
                    if(result!="Ok")
                    {
                        alert(result);
                    }
                    else
                        location.href="?s=Messagerie&view=Messagerie_Send";
                }
        });
    }
}

function fdelMSG(id,msg)
{
    if(confirm(msg))
    {
        $.ajax({
                url: 'public/ajax/call-messagerie.php',
                type: 'POST',
                data: 'fdelmsgid='+encodeURIComponent(id),

                success: function(result)
                {
                    if(result!="Ok")
                    {
                        alert(result);
                    }
                    else
                        location.href="?s=Messagerie&view=Messagerie_Corbeille";
                }
        });
    }
}

/* AUTOCOMPLET */
function CalfindValue(li)
{
    var sValue;
    // if coming from an AJAX call, let's use the CityId as the value
    if( !!li.extra ) sValue = li.extra[0];

    // otherwise, let's just display the value in the text box
    else sValue = li.selectValue;
}

function CalselectItem(li)
{
    CalfindValue(li);
}

function CalformatItem(row)
{
    return row[0];
}

$(function()
{
    $("#toID").autocomplete(
    "public/ajax/call-users.php",
    {
    delay:10,
    minChars:4,
    //matchSubset:0,
    //matchContains:0,
    maxItemsToShow:10,
    //cacheLength:1,
    onItemSelect:CalselectItem,
    onFindValue:CalfindValue,
    formatItem:CalformatItem,
    autoFill:false
    }
    );
});
/* AUTOCOMPLET  END*/

$(function() {
	$('#sendMSGB').click(function()
        {
            $('#sendMSGB').hide();
            $('#eDisk').hide();
            $('#loadB').show();

            var toID = $('#toID').val();
            $('#toID').attr('disabled', 'disabled');
            var Sujet = $('#Sujet').val();
            $('#Sujet').attr('disabled', 'disabled');
            var Description = $('#Description').val();
            $('#Description').attr('disabled', 'disabled');

            $.ajax({
                    url: 'public/ajax/call-messagerie.php',
                    type: 'POST',
                    data: 'toID='+encodeURIComponent(toID)
                        +"&Sujet="+encodeURIComponent(Sujet)
                        +"&Description="+encodeURIComponent(Description),

                    success: function(result)
                    {
                        tresult = result.split('&&&');
                        if(tresult[0] == "Ok")
                        {
                            alert(tresult[1]);
                            location.href="?s=Messagerie&view=Messagerie_Send";
                        }
                        else
                        {
                            $('#loadB').hide();
                            $('#sendMSGB').show();
                            $('#eDisk').show();
                            alert(result);
                            $('#Sujet').removeAttr('disabled');
                            $('#toID').removeAttr('disabled');
                            $('#Description').removeAttr('disabled');
                        }
                    }
            });

            return false;
	});
});

function delCal(id,msg,reload)
{
    if(confirm(msg))
    {
        $.ajax({
                url: 'public/ajax/call-calendrier.php',
                type: 'POST',
                data: 'delcalid='+encodeURIComponent(id),

                success: function(result)
                {
                    if(result!="Ok")
                    {
                        alert(result);
                    }
                    else
                    {
                        if(reload)
                            location.reload(true);
                        else
                            location.href="?s=BV&view=BV_Profil&r="+rand(1000)+"#event";
                    }
                }
        });
    }
}

function delSujet(id,msg,reload)
{
    if(confirm(msg))
    {
        $.ajax({
                url: 'public/ajax/call-babillard.php',
                type: 'POST',
                data: 'delbabid='+encodeURIComponent(id),

                success: function(result)
                {
                    if(result!="Ok")
                    {
                        alert(result);
                    }
                    else
                    {
                        if(reload)
                            location.reload(true);
                        else
                            location.href="?s=BV&view=BV_Profil&r="+rand(1000)+"#sujet";
                    }
                }
        });
    }
}

function rand(max)
{
    return Math.floor(Math.random()*max);
}

function ilikeyou(id,div,reload)
{
    $.ajax({
        url: 'public/ajax/call-users.php',
        type: 'POST',
        data: 'ilikeID='+encodeURIComponent(id)+
              '&div='+encodeURIComponent(div),

        success: function(result)
        {
            tresult = result.split('&&&');
            if(tresult[0] == "Ok")
            {
                if(reload)
                    location.reload(true);
                else
                {
                    $('#'+div).html(tresult[1]);
                }
            }
            else alert(result);
        }
    });
}

function idontlikeyou(id,div,reload)
{
    $.ajax({
        url: 'public/ajax/call-users.php',
        type: 'POST',
        data: 'idontlikeID='+encodeURIComponent(id)+
              '&div='+encodeURIComponent(div),

        success: function(result)
        {
            tresult = result.split('&&&');
            if(tresult[0] == "Ok")
            {
                if(reload)
                    location.reload(true);
                else
                {
                    $('#'+div).html(tresult[1]);
                }
            }
            else alert(result);
        }
    });
}
