jQuery(document).ready(function(){
    
    //Serach story form handling
    if (jQuery('#BodySrch').get(0))
    {                
        jQuery('#BodySrch').submit(function()
        {            
            if (jQuery('#BodySrchInp', this).val() && !jQuery('#BodySrchInp', this).val().match(/^[a-zA-Z0-9 а-яА-Я_-]+$/))
            {
                alert('Для ввода в поле "Ключевое слово" допускаются только буквы, пробелы и знаки тире и подчеркивания!');
                return false;
            }
            
            this.action = jQuery('#base_url').val() + 'search/story/';
            if (jQuery('#countries', this).val() >= 1) this.action += "countries/" + jQuery('#countries', this).val() + "/";            
            if (jQuery('#main_tems', this).val() >= 1) this.action += "main_tems/" + jQuery('#main_tems', this).val() + "/";
            if (jQuery('#tematics', this).val() >= 1) this.action += "tematics/" + jQuery('#tematics', this).val() + "/";
            if (jQuery('#budget', this).val() >= 1) this.action += "budget/" + jQuery('#budget', this).val() + "/";
            if (jQuery('#BodySrchInp', this).val()  !== '' && jQuery('#BodySrchInp', this).val() != 'Ключевое слово') this.action += "word/" + encodeURIComponent(jQuery('#BodySrchInp', this).val()) + "/";            
            this.submit();
            return false;
        });      
	
    }
    
    //Serach users form handling
    if (jQuery('#BodySrchUsers').get(0))
    {
                
        jQuery('#BodySrchUsers').submit(function()
        {                        
            if (jQuery('#town', this).val() && !jQuery('#town', this).val().match(/^[a-zA-Z а-яА-Я_-]+$/))
            {
                alert('Для ввода в поле "Город" допускаются только буквы, пробел и знаки тире и подчеркивания!');
                return false;
            }
            if (jQuery('#BodySrchUsersInp', this).val() && !jQuery('#BodySrchUsersInp', this).val().match(/^[a-zA-Z0-9 а-яА-Я_-]+$/))
            {
                alert('Для ввода в поле "Ключевое слово" допускаются только буквы, пробелы и знаки тире и подчеркивания!');
                return false;
            }
            this.action = jQuery('#base_url').val() + 'search/users/';
            if (jQuery('#countries', this).val() >= 1) this.action += "countries/" + jQuery('#countries', this).val() + "/";
            if (jQuery('#town', this).val() !== '' && jQuery('#town', this).val() != 'Город') this.action += "town/" + encodeURIComponent(jQuery('#town', this).val()) + "/";
            if (jQuery('#BodySrchUsersInp', this).val() !== '' && jQuery('#BodySrchUsersInp', this).val() != 'Ключевое слово') this.action += "word/" + encodeURIComponent(jQuery('#BodySrchUsersInp', this).val()) + "/";
            this.submit();
            return false;
        });
    }
            
    //Login forms handling
    if (jQuery('#header_forms').get(0))
    {
        if (jQuery.browser.mozilla) {
            jQuery('#mem').css({'vertical-align': 'middle'});
        }
        
        jQuery('#login_form_link').bind("click", function(e) {                                    
            if (jQuery('#FormLogin').css('display') == 'none')
            {                
                jQuery('#header_forms form').css({display: 'none'});            
                jQuery('#FormLogin').css({display: 'block'});
            }
            else
            {             
                jQuery('#FormLogin').css({display: 'none'});
            }
        });
        
        jQuery('#forgot_passwd_link').bind("click", function(e) {            
            jQuery('#header_forms form').css({display: 'none'});            
            jQuery('#FormForgot').css({display: 'block'});
        });
        
        jQuery('#FormLogin').submit(function () {
            if (jQuery('#loginField').val() != "Логин" && jQuery('#passwdField').val() != "Пароль")
            {
                try {
                jQuery.ajax( 
                {                
                    type: "POST",
                    url: '/ajax/login/',
                    data: 'username=' + jQuery('#loginField').val() + "&password=" + MD5(jQuery('#passwdField').val()),
                    dateType: 'text',                    
                    success: function(message) 
                    {                                                                                            
                        if (message.indexOf("http") != -1) {
                            document.location = message;
                        }
                        else {
                            alert(message);    
                        }
                    }                
                });
                }
                catch (e) { alert(e); }
            }
            return false;
        });
        
        jQuery('#FormForgot').submit(function () {
            if (jQuery('#HdrNavInptForgot').val() != "Введите свой адрес email")
            {
                jQuery.ajax( 
                {                
                    type: "POST",
                    url: '/ajax/forgot/',
                    data: 'email=' + jQuery('#HdrNavInptForgot').val(),
                    dateType: 'text',
                    success: function(message) 
                    {                                                                    
                        alert(message);
                    }                    
                });
            }
            return false;
        });
    }    
    
});