
function check_mail ( mail ) {

    if (/^\s*\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+\s*$/.test( mail )) return true;

    return false;
}

function changeLang ( lang ) {
    $('#langForm #newlang').val ( lang );

    $('#langForm').submit ();
}

Array.prototype.in_array = function(p_val) {
    for(var i = 0, l = this.length; i < l; i++) {
        if(this[i] == p_val) {
            return i;
        }
    }
    return -1;
}

function sym_to_lat( sym )
{
    var _chset = current_language ? charset_lv : charset;
    var i1 = _chset[0].in_array( sym );
    var i2 = _chset[1].in_array( sym );

    if ( i1 >= 0)
        return _chset[2][i1];

    else if ( i2 >= 0 )
        return _chset[3][i2];

    return sym;
}

function sym_to_rus( sym )
{
    var _chset = current_language ? charset_lv : charset;
    var s1 = ( typeof(sym.charAt(0)) != 'undefined' ? sym.charAt(0) : '' );

    var i1 = _chset[2].in_array( sym );
    var i2 = _chset[3].in_array( sym );

    if ( i1 >= 0)
        return [_chset[0][i1],2];

    else if ( i2 >= 0 )
        return [_chset[1][i2],2];

    var i11 = _chset[2].in_array( s1 );
    var i12 = _chset[3].in_array( s1 );

    if ( i11 >= 0 )
        return [_chset[0][i11],1];

    else if ( i12 >= 0 )
        return [_chset[1][i12],1];

    else
        return [s1,1];
}

function to_rus( string )
{
    var new_string = '';

    if( string == '' || typeof(string) == 'undefined' ) return;

    for (i=0; i<string.length; )
    {
        var tmp = sym_to_rus( string.substr( i, 2 ) );
        new_string += tmp[0];
        i += tmp[1];
    }

    return new_string;
}

function all_to_rus()
{

    var string = $('#form #text').val();

    if( string == '' || typeof(string) == 'undefined' ) return;

    $('#form #text').val( to_rus( string ) );
}

function to_lat( string )
{
    var new_string = '';

    if( string == '' || typeof(string) == 'undefined' ) return;

    for (i=0; i<string.length; i++)
        new_string += sym_to_lat( string.charAt(i) );

    return new_string;
}

function all_to_lat()
{

    var string = $('#form #text').val();

    if( string == '' || typeof(string) == 'undefined' ) return;

    $('#form #text').val( to_lat( string ) );
}

function updateNotes() {
    $('#ind').show();

    $.ajaxSetup({
        dataType: 'html',

        success: function(data){
            $('#noteList').html( data );

            $('#ind').hide();
        }
    });

    $.post( '/xquery.php', {act: 'notelist'} );
}

function shNotes() {

    if ( $('#noteList').css('display') == 'none' ) {
        updateNotes();

        $('#noteList').show();

    } else
        $('#noteList').hide();
}

function reset_eid () {	document.getElementById ('eid').value = '0';
}

function save_btn() {
    $('#ind').show();

    $.ajaxSetup({
        dataType: 'json',

        success: function (json) {
            updateNotes();

            $('#form #eid').val(json.id);
            $('#xres').html(json.html);

            $('#ind').hide();
        }
    });

    var _autosave = (document.getElementById('autosave').checked?1:0);

    $.post( '/xquery.php', {act: 'save', text: $('#form #text').val(), name: $('#form #savename').val(), autosave: _autosave, eid: $('#form #eid').val() } );
}

function saveas_btn() {
    $('#ind').show();

    $.ajaxSetup({
        dataType: 'json',

        success: function (json) {
            updateNotes();

            $('#form #eid').val(json.id);
            $('#form #savename').val(json.name);
            $('#xres').html(json.html);

            $('#ind').hide();
        }
    });

    var _autosave = (document.getElementById('autosave').checked?1:0);

    $.post( '/xquery.php', {act: 'saveas', text: $('#form #text').val(), name: $('#form #saveasname').val(), autosave: _autosave } );
}

function delNote( nid ) {
    $('#ind').show();

    $.ajaxSetup({
        dataType: 'html',

        success: function (data) {
            updateNotes();

            $('#xres').html(data);

            $('#ind').hide();
        }
    });

    $.post( '/xquery.php', {act: 'delnote', id: nid } );
}

function loadNote( nid ) {
    $('#ind').show();

    $.ajaxSetup({
        dataType: 'json',

        success: function (json) {
            $('#form #eid').val(json.id);
            $('#form #savename').val(json.name);
            $('#form #text').val(json.content);
            $('#form #autosave').attr('checked', ((json.autosave == 1)?'checked':''));

            $('#ind').hide();
        }
    });

    $.post( '/xquery.php', {act: 'loadnote', id: nid } );
}

function loadList( pagenum ) {
    $('#ind').show();

    $.ajaxSetup({
        dataType: 'html',
        global: false,

        success: function(data){
            $('#editNoteList').html( data );
            $('#ind').hide();
        }
    });

    $.post( '/xquery.php', {act: 'editnotelist', page: pagenum} );
}

/*
    $.ajaxSetup({
        dataType: 'json',

        success: function (json) {
            $('#form #eid').val(json.id);
            $('#form #savename').val(json.name);
            $('#form #text').val(json.content);
            $('#form #autosave').attr('checked', ((json.autosave == 1)?'checked':''));

            $('#ind').hide();
        }
    });

    $.post( '/xquery.php', {act: 'loadnote', id: nid } );

*/
/*
function delNotes() {
    $('#ind').show();

    $.ajaxSetup({
        dataType: 'json',

        success: function (json) {
            updateNotes();

            $('#xres').html(data);

            $('#ind').hide();
        }
    });

    $.post( '/xquery.php', {act: 'delnotes', id: nid } );
}
*/

$(document).ready( function() {
    $('a.cf').bind('click', function (event) {        if(typeof($('#form #text').val()) != 'undefined' && $('#form #text').val() != '' && !confirm("Данные будут потеряны!")) return false;
    });
})



