
function changeLanguage(new_lng, old_lng)
{
    JsHttpRequest.query(
    	  "/index?request_mode=ajax",
    	  {'new_lng' : new_lng, 'ajax_action' : 'change_language'},
    	  function(result, errors)
    	  {
    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }

	     	 if(result && result.answer_ok)
	     	 {
	     		var new_location = document.location.href;
	     		
	     		new_location = new_location.replace('lng='+old_lng, 'lng='+new_lng);
	     		
	     		document.location = new_location;
	     	 }
    	  },
    	  true
      );
    
    return false;
}

function registerUser(form)
{
    var remember_me = parseInt($('#remember_me').val());
    var terms = parseInt($('#accept_terms').val());
    var email = $('#reg_email').val();
    var pwd   = $('#reg_pwd').val();
    var user_name = $('#reg_user_name').val();

    $('#reg_warning_msg').html('').hide(); $('#reg_block').attr("class","move reg");
    $('#reg_email').attr("class","text");$('#reg_pwd').attr("class","text");$('#reg_user_name').attr("class","text");

    if(!terms)
    {
        $('#reg_warning_msg').html(js_register_login_dictionary.register_accept_terms_notice);
        $('#reg_block').attr("class","move regerror regerrorhight"); $('#reg_warning_msg').show();

        return;
    }

    $('#do_reg_user').hide();
    $('#reg_progress_bar').show();

    JsHttpRequest.query(
    	  "/?request_mode=ajax",
    	  {'register_form' : form, 'ajax_action' : 'register_new_user'},
    	  function(result, errors) {

    	     $('#reg_progress_bar').hide();
    	     $('#do_reg_user').show();

    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
		     else
		     	if(result)
		     	{
		     	    if(parseInt(result.error_status))
		     	    {
		     	        $('#reg_warning_msg').html(result.error_msg);
                        $('#reg_block').attr("class","move regerror regerrorhight"); $('#reg_warning_msg').show();
		     	    }
		     	    else{
		     	        document.location.reload();
		     	    }
		     	}
    	  },
    	  true
      );
}
function loginUser(form)
{
    $("#forgoterr").slideUp("slow");

    if ( forgot_hint_visible ) {
        sendNewPassword(form);
        return;
    }

    $("#forgothint").slideUp("slow");

    var login = $('#login_email').val().replace(/^\s+/, '').replace(/\s+$/, '');
    var pwd = $('#login_pwd').val().replace(/^\s+/, '').replace(/\s+$/, '');

    $('#login_email_wr').attr("class","inputbox text119");
    $('#login_pwd_wr').attr("class","inputbox text89");

    if(login == 'e-mail' || login == '')
    {
        $('#login_email_wr').attr("class","inputbox text119pink");
        $('#autherrormsg').html(js_register_login_dictionary.login_no_email);
        $("#autherror").slideDown("slow");
        return;
    }

    if(pwd == 'password' || pwd == '')
    {
        $('#login_pwd_wr').attr("class","inputbox text89pink");
        $('#autherrormsg').html(js_register_login_dictionary.login_no_password);
        $("#autherror").slideDown("slow");
        return;
    }

    $('#do_login').hide();
    $("#autherror").slideUp("slow");
    /*$('#login_progress_bar').show();*/

    JsHttpRequest.query(
    	  "/?request_mode=ajax",
    	  {'register_form' : form, 'ajax_action' : 'login'},
    	  function(result, errors) {

    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
		     else
		     	if(result)
		     	{
		     	    if( result.error_status == '0' )
		     	    {						
		     	        document.location.reload();
		     	        return;
		     	    }
		     	    else if( result.error_status == '1' ){
		     	        $('#do_login').show();
                        /*$('#login_progress_bar').hide();*/
		     	        $('#login_email_wr').attr("class","inputbox text119pink");
		     	    }
		     	    else if( result.error_status == '2' ){
		     	        $('#do_login').show();
                        /*$('#login_progress_bar').hide();*/
		     	        $('#login_pwd_wr').attr("class","inputbox text89pink");
		     	    }
		     	    else{
		     	        $('#do_login').show();
                        /*$('#login_progress_bar').hide();*/
		     	        $('#login_email_wr').attr("class","inputbox text119pink");
		     	        $('#login_pwd_wr').attr("class","inputbox text89pink");
		     	    }
		     	    if ( result.error_status != '0' && result.error_msg && result.error_msg.length > 0) 
					{
                        $('#autherrormsg').html(result.error_msg);
                        $("#autherror").slideDown("slow");
		     	    }
		     	}
    	  },
    	  true
      );
}

function sendNewPassword(form)
{
    var login = $('#login_email').val().replace(/^\s+/, '').replace(/\s+$/, '');
    $('#login_pwd').val().replace(/^\s+/, '').replace(/\s+$/, '');

    $('#login_email_wr').attr("class","inputbox text119");
    $('#login_pwd_wr').attr("class","inputbox text89");

    if(login == 'e-mail' || login == '')
    {
        $('#login_email_wr').attr("class","inputbox text119pink");
        return;
    }

    $('#do_login').hide();
    $("#forgothint").slideUp("slow");

    JsHttpRequest.query(
    	  "/?request_mode=ajax",
    	  {'register_form' : form, 'ajax_action' : 'send_new_password'},
    	  function(result, errors) {

    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);$('#do_login').show();}
		     }
		     else
		     	if(result)
		     	{
		     	    status = parseInt(result.error_status)

		     	    if( status == 0 )
		     	    {
		     	        $("#forgothint").slideDown("slow");
		     	        $('#forgothintmsg').html(js_register_login_dictionary.password_sent);
		     	        $('#login_pwd_wr').show();
                        $('#do_login').val('ok').attr('class', 'button button20').show();
                        $('#forgot_pass').show();
                        forgot_hint_visible = false;
		     	        return;
		     	    }
		     	    if ( status != 0 && result.error_msg && result.error_msg.length > 0) {
		     	        $('#do_login').show();
		     	        $('#forgoterrmsg').html(result.error_msg);
                        $("#forgoterr").slideDown("slow");
		     	    }
		     	}
    	  },
    	  true
      );
}

function forgotEmail() {

    forgot_hint_visible = true;

    $('#forgot_pass').hide();

    $('#login_pwd_wr').hide();
    $('#login_email_wr').attr("class","inputbox text119");
    $('#do_login').val(js_register_login_dictionary.login_send).attr('class', 'button button57');;

    $("#autherror").slideUp("slow");

    $("#forgothint").slideDown("slow");
    $("#forgothintmsg").html( js_register_login_dictionary.insert_email );


}

function logoutUser()
{
    $('#do_logout').hide();
    /*$('#login_progress_bar').show();*/

    JsHttpRequest.query(
    	  "/?request_mode=ajax",
    	  {'ajax_action' : 'logout'},
    	  function(result, errors) {

    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
		     else
		     	if(result)
		     	{
		     	    document.location.reload();
		     	}
    	  },
    	  true
      );
}

/**
 *Обработка формы обратной связи
 *
 */
function sendFeedBack(form)
{
    JsHttpRequest.query(
    "/?request_mode=ajax",
    {'feedback_form': form, 'ajax_action' : 'send_feedback'},
    function (result, errors) {
    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
		     else
		     	if(result)
		     	{
		     	    if (result.error_status!=0)
                    {
                        $('#feedBackMessageDiv').show();
                        $('#feedBackMessageDiv').html(result.error_msg);
                        $('#feedBackFormMainDiv').attr({'class' : 'move feedback feedbacklogin feedbackloginerror'});
                    }
                    else
                    {
                        $("#feedback").slideToggle(0);
    		     	    $("#feedback_form").each( function() {
                                                    this.reset();
    		     	                                });
                        $("#feedback_ok").slideToggle("slow");
                        $('#feedBackMessageDiv').html('');
                        $('#feedBackFormMainDiv').attr({'class' : 'move feedback feedbacklogin'});
                    }

		     	}
    },
    true
    );
}

function update_click(id)
{
    JsHttpRequest.query(
    	  "/market/?request_mode=ajax",
    	  {'id' : id, 'ajax_action' : 'update_click'},
    	  function(result, errors) {

    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
		     else
		     	if(result)
		     	{

		     	}
    	  },
    	  true
      );
}

function update_views(id)
{
    JsHttpRequest.query(
    	  "/market/?request_mode=ajax",
    	  {'id' : id, 'ajax_action' : 'update_views'},
    	  function(result, errors) {

    	  	 if(errors)
		     {
		     	if(!production_flag){alert(errors);}
		     }
		     else
		     	if(result)
		     	{

		     	}
    	  },
    	  true
      );	
}

function blogTabClick()
{
	$('#blog_tab').addClass('current');
	
	$('#forum_tab').removeClass('current');
	
	$('#forum_text').attr({'style' : 'color: #ADB1BC;'});
	
	$('#blog_text').attr({'style' : 'color: #00AAFF;'});
	
	$('#blog_box').show();
	
	$('#forum_box').hide();
}

function forumTabClick()
{
	$('#blog_tab').removeClass('current');
	
	$('#forum_tab').addClass('current');
	
	$('#blog_text').attr({'style' : 'color: #ADB1BC;'});
	
	$('#forum_text').attr({'style' : 'color: #00AAFF;'});
	
	$('#blog_box').hide();
	
	$('#forum_box').show();
}


$(document).ready(function(){
	$("#register").hide();
	$("#registeropen").click(function(){
		$("#register").slideToggle("slow");
		return false;
	});
});
$(document).ready(function(){
	$("#register").hide();
	$("#registerclose").click(function(){
		$("#register").slideUp("slow");
		return false;
	});
});

$(document).ready(function(){
	$("#autherror").hide();
	/*$("#registeropen").click(function(){
		$("#register").slideToggle("slow");
		return false;
	});*/
	$("#autherrorclose").click(function(){
		$("#autherror").slideUp("slow");
		return false;
	});
	$("#forgoterrclose").click(function(){
		$("#forgoterr").slideUp("slow");
		return false;
	});
	forgot_hint_visible = false;
	$('#forgothint').hide();
	$("#forgothintclose").click(function(){
	    /*$('#forgothintmsg').html( forgothintmsg );*/
		$("#forgothint").slideUp("slow");
		$('#login_pwd_wr').show();
		$('#do_login').val('ok').attr('class', 'button button20');
		forgot_hint_visible = false;
		return false;
	});
});

function clean(what) {
 what.previousSibling.style.display = "none";
}
function show(what) {
 if(what.value == ''){
 what.previousSibling.style.display = "block";
 }else{
 what.previousSibling.style.display = "none";
 }
}
function cleanIfNotEmpty(ids) {
    var e = [];
    for (var i = 0; i < ids.length; i++) {
        e[i] = document.getElementById(ids[i]);
    }
    var f = function() {
        for (var i = 0; i < e.length; i++) {
            if (e[i].value) {
                clean(e[i]);
            }
        }
    };
    setInterval(f, 100);
}
