login_ok = false;
email_ok = false;
password_confirmed = false;
spaces_placeholder = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"

$(document).ready(function() 
{
     /* Registration form */
     $("#reg").click(function() {
	 $("#login_form").hide();
	 $("#reg_form").fadeIn("fast");
	 $("#reg_login").focus();
     });
    $("#reg_bage").click(function() {
	$("#reg").click();
    });
     $("#reg_cancel").click(function() {
	 reset_registration_form();
	 $("#reg_form").slideUp("fast");
     });
     $("#email").bind("blur", function() {
	 email = $("#email").attr("value");
	 if (!validate(email)) {
	     $("#existing_email").slideUp("fast");
             $("#bad_email").fadeIn("fast");
	     email_ok = false;
	 } else {
             $("#bad_email").slideUp("fast");
	     $("#existing_email").slideUp("fast");
	     $.get("/?c=auth&a=check-email&id=" + email, function(response) {
		 if (response.indexOf('f') > 0) {
		     $("#existing_email").fadeIn("fast");
		     email_ok = false;
		 } else {
		     email_ok = true;
		 }
		 check_register_form();
	     });
	 }
	 check_register_form();
     });
     $("#reg_login").bind("blur", function() {
	 if (this.value.trim().length == 0) {
             $("#taken").slideUp("fast");
	     login_ok = false;
	     return;
	 }
         $.get("/?c=auth&a=check&id=" + this.value, function(response) {
             if (response.indexOf('f') > 0) { // Name already taken
                 $("#taken").fadeIn("fast");
		 login_ok = false;
             } else {
                 $("#taken").slideUp("fast");
		 login_ok = true;
             }
	     check_register_form();
         });
     });
     $("#reg_button").click(function() {
	 if (email_ok && login_ok && password_confirmed) 
	 {
	     $("#reg_button").html(spaces_placeholder).attr("class", "fake_button");
	     $.post ("/?c=auth&a=register", { 
			 login:    $("#reg_login").attr("value"),
			 email:    $("#email").attr("value"),
			 password: $("#password").attr("value"),
			 receive_spam: $("#receive-spam").attr("value")
		     },
		     function(response) {
			 $("#reg_button").html("Register").attr("class", "disabled_button");
			 if (response.indexOf('t') > 0) {
			     $("#reg_button").hide();
			     $("#reg_fail").hide();
			     $("#reg_ok").show();
			 } else {
			     $("#reg_ok").hide();
			     $("#reg_fail").show();
			     $("#reg_button").show();
			 }
		     });
	 } 
	 return false;
     });
     $("#reg_form").submit(function() {
	 if (!(email_ok && login_ok && password_confirmed)) {
	     return false;
	 }
     });
     $("#password").bind("blur change keyup focus", check_passwords);
     $("#confirm_password").bind("blur change keyup focus", check_passwords);

     /* Login form */
     $("#forgotten_password").click(function() {
	 $("#login_form").hide();
	 $("#password_recovery_form").show();
     });
     $("#password_recovery_cancel").click(function() {
     	 $("#password_recovery_form").slideUp("fast");
	 reset_recovery_form();
     });
     $("#login").click(function() {
	 $("#password_recovery_form").hide();
	 $("#reg_form").hide();
	 $("#login_form").fadeIn("fast");
	 $("#login_login").focus();
     });
     $("#password_recovery_form").submit(function() {
	 $("#password_recovery_send_button").click();
	 return false;
     });
     $("#password_recovery_send_button").click(function() {
	 $("#password_recovery_send_button").html(spaces_placeholder).attr("class", "fake_button");
	 $.get("/?c=password-recovery&a=send-recovery-letter&email=" + 
	       $("#password_recovery_email_text").attr("value"), 
	       function(response) {
		   if (response.indexOf('t') > 0) {
		       $("#password_recovery_message_error").hide();
                       $("#password_recovery_message_sent").show();
		       $("#password_recovery_send_button").hide();
		   } else {
		       $("#password_recovery_send_button").attr("class", "button").html("Send");
		       $("#password_recovery_message_sent").hide();
                       $("#password_recovery_message_error").show();
		   }
	       })
     });
     $("#password_recovery_email_text").bind("change keypress", function() {
	 $("#password_recovery_message_error").slideUp("fast");
         $("#password_recovery_message_sent").slideUp("fast");	 
     });
     $("#login_cancel").click(function() {
	 $("#login_form").slideUp("fast");
     });

     /* Request form */
     $("#request_form").submit(function() {
	 $("#request_send").click();
	 return false;
     });
     $("#request_email").bind("blur focus", function() {
	 validate_request_form(false);
     });
     $("#request_name").bind("blur focus", function() {
	 validate_request_form(false);
     });
     $("#msg").bind("blur focus change keyup", function() {
	 validate_request_form(false);
     });
     $("#request_send").click(function() {
	if(validate_request_form(true))
	{
	    $("#request_send").html(spaces_placeholder).attr("class", "fake_button");
	    $("#request_send").click(function() { return false; });
	    $.post("/?c=main&a=feedback", { 
			 request_name:  $("#request_name").attr("value"),
			 request_email: $("#request_email").attr("value"),
			 company:       $("#company").attr("value"),
			 text:          $("#msg").attr("value")
		     },
		    function(response) {
			$("#request_form").fadeOut("fast");
			$("#request_send").hide();
			$("#request_result").fadeIn("fast");
		    });
	}
     });

     /* FAQ form */
     $("#faq_form").submit(function() {
	 $("#faq_send").click();
	 return false;
     });
     $("#faq_email").bind("blur focus", function() {
	 validate_faq_form(false);
     });
     $("#faq_name").bind("blur focus", function() {
	 validate_faq_form(false);
     });
     $("#faq_text").bind("blur focus keyup", function() {
	 validate_faq_form(false);
     });
     $("#faq_send").click(function() {
	if(validate_faq_form(true))
	{
	    $("#faq_send").html(spaces_placeholder).attr("class", "fake_button");
	    $("#faq_send").click(function() { return false; });
	    $.post("/?c=main&a=ask-question", { 
			 faq_name:  $("#faq_name").attr("value"),
			 faq_email: $("#faq_email").attr("value"),
			 faq_text:  $("#faq_text").attr("value")
		    },
		    function(response) {
			$("#faq_form").fadeOut("fast");
			$("#faq_send").hide();
			$("#faq_result").fadeIn("fast");
		    });
	}
     });

    /* Unsubscribe form */
     $("#uns_form").submit(function() {
	 $("#uns_send").click();
	 return false;
     });
    
    $("#uns_send").click(function() {
	$("#uns_fail").fadeOut("fast");
	$("#uns_ok").fadeOut("fast")
	$("#uns_email_fail").fadeOut("fast");
	uns_email = $("#uns_email").attr("value");
	if (!validate(uns_email)) {
	    $("#uns_email_fail").fadeIn("fast");
	    return false;
	} else {
	    $("#uns_email_fail").fadeOut("fast");
	}
	$.post("/?c=main&a=unsubscribe", {uns_email: uns_email},
	       function(response) {
		   if (response.indexOf('t') > 0) {
		       $("#uns_ok").fadeIn("fast")
		       $("#uns_fail").fadeOut("fast")
		   } else {
		       $("#uns_fail").fadeIn("fast")
		       $("#uns_ok").fadeOut("fast")
		   }
	       });
	});

    /* kg form */
     $("#kg_form").submit(function() {
	 $("#kg_send").click();
	 return false;
     });
    
    $("#kg_send").click(function() {
	$.post("/?c=admin&a=keygen", {email: $("#kg_email").attr("value"), version: $("#kg_version").attr("value")},
	       function(response) {
		   if (response) {
		       $("#gk").html(response)
		   } 
	       });
    });

    $("#quickreg_send").click(function() {
	var email = $("#quickreg_email").attr("value");
	if (validate(email)) {
	    $("#quickreg_error").fadeOut("fast");
	    $("#quickreg_ok").fadeOut("fast");
	    $("#quickreg_duplicate_email").fadeOut("fast");
	    $.post("/?c=auth&a=quickregister", {email: email},
		   function(response) {
		       if (response == "#t") {
			   $("#quickreg_ok").fadeIn("fast");
		       } else {
			   $("#quickreg_duplicate_email").fadeIn("fast");
		       }
		   });
	} else {
	    $("#quickreg_error").fadeIn("fast");
	}
    });

    /* acc conf form*/
     $("#acc_conf_cancel").click(function() {
	 $("#acc_conf_form").fadeOut("fast");
     });
    
    /* Language change */
    $("#ru_lang").click(function() {
	$.get("/?c=lang&a=set-lang&id=ru", 
	      function(response) {
		  location.reload(true);
	      })
    });

    $("#en_lang").click(function() {
	$.get("/?c=lang&a=set-lang&id=en", 
	      function(response) {
		  location.reload(true);
	      })
    });

    $("#add_comment_form").submit(function() {
	var email = $("#unregistered_comment_email").attr("value");
	var name = $("#unregistered_comment_name").attr("value");
	$("#unregistered_comment_bad_email").fadeOut("fast");
	$("#unregistered_comment_bad_name").fadeOut("fast");
	if (validate(email)) {
	    if (name.trim().length > 0) {
		return true;
	    } else {
		$("#unregistered_comment_bad_name").fadeIn("fast");
	    }
	} else {
	    $("#unregistered_comment_bad_email").fadeIn("fast");	    
	}
	return false;
    });

    $("#spam_form_preview").click(function() {
	var subject = $("#spam_subj").attr("value");
	var body = $("#spam_body").attr("value");
	$("#spam_form_subject").html("Subj: " + subject);
	$("#spam_form_preview_body").html(body);
    });

    // $(".report_expander").click(function() {
    // 	var self_id = $(this).attr("id").split("_")[0]
    // 	$("#report_" + self_id).toggle("fast");
    // 	$(this).toggle("Надоело!", "Развернуть!");
    // 	// $(self_id + "_fold_report").show();
    // });

    $(".report_expander").toggle(
	function() {
    	    var self_id = $(this).attr("id").split("_")[0]
    	    $("#report_" + self_id).slideDown("fast");
    	    $(this).html("Прекратите это!");
    	},
    	function() {
    	    var self_id = $(this).attr("id").split("_")[0]
    	    $("#report_" + self_id).slideUp("fast");
    	    $(this).html("Развернуть");
    	});

     /* Fancybox settings */
     $("a.inline").fancybox({
	 'hideOnContentClick': false,
	 'transitionIn'	        : 'none',
	 'transitionOut'	: 'none',
	 'speedIn'		: 600, 
	 'speedOut'		: 200, 
	 'overlayShow'	        : true,
	 'overlayOpacity'       : 0.0,
	 'showNavArrows'        : true,
	 'hideOnOverlayClick'   : true
     });
     $(".screenshot").fancybox({
	 'transitionIn'	        : 'none',
	 'transitionOut'	: 'none',
	 'speedIn'		: 600, 
	 'speedOut'		: 200, 
	 'overlayShow'	        : true,
	 'overlayOpacity'       : 0.0,
	 'showNavArrows'        : true,
	 'hideOnOverlayClick'   : true
     });

    $("a.info_tooltip").tooltip({ 
    	track: false, 
    	delay: 0,
    	showURL: false, 
    	showBody: " - ", 
    	// fixPNG: true,
    	fade: 100,
    	left: -120, 
    	bodyHandler: function() { 
            return $($(this).attr("href")).html(); 
    	}
    });
    $(".datepicker").datepicker({ dateFormat: 'yy-mm-dd', firstDay: 1});
});

function check_register_form()
{
    if (email_ok && login_ok && password_confirmed) {
	$("#reg_button").attr("class", "button");
    } else {
	$("#reg_button").attr("class", "disabled_button");
    }
}

function reset_recovery_form() 
{
    $("#password_recovery_email_text").attr("value", "")
    $("#password_recovery_send_button").attr("class", "button").html("Send");
    $("#password_recovery_message_sent").hide();
    $("#password_recovery_message_error").hide();
    $("#password_recovery_send_button").show();
}


function reset_registration_form() 
{
    $('#reg_form :input').each(function() {
	this.value = "";
    });
    $('#reg_form .error_message').each(function() {
	$(this).hide();
    });
    $("#reg_button").show();
}

function check_passwords() 
{
    password = $("#password").attr("value");
    confirmation = $("#confirm_password").attr("value");
    if (password == "") {
	return;
    }
    if (password != confirmation) {
        $("#pwd_mismatch").fadeIn("slow");
        password_confirmed = false;
    } else {
        $("#pwd_mismatch").fadeOut("slow");
        password_confirmed = true;
    }
    check_register_form();
}

function validate(email) 
{
    var reg = /^([A-Za-z0-9_\-+\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email);
}

function validate_request_form(only_check) 
{
    email = $("#request_email").attr("value");
    var is_email_valid = validate(email);
    var is_name_exists = ($("#request_name").attr("value").length > 0);
    var is_msg_exists = ($("#msg").attr("value").length > 0);
    if (is_email_valid && is_name_exists && is_msg_exists) {
	if (!only_check) {
	    $("#request_send").attr("class", "button");
	    $("#bad_req_email").slideUp("fast");
	}
	return true;
    } else {
	if (!only_check) {
	    $("#request_send").attr("class", "disabled_button");
	    if (!is_email_valid && email.length > 0)
		$("#bad_req_email").fadeIn("fast");
	    else
		$("#bad_req_email").slideUp("fast");
	}
	return false;
    }
}

function validate_faq_form(only_check) 
{
    email = $("#faq_email").attr("value");
    var is_email_valid = validate(email);
    var is_name_exists = ($("#faq_name").attr("value").length > 0);
    var is_text_exists = ($("#faq_text").attr("value").length > 0);
    if (is_email_valid && is_name_exists && is_text_exists) {
	if (!only_check) {
	    $("#faq_send").attr("class", "button");
	    $("#bad_faq_email").slideUp("fast");
	}
	return true;
    } else {
	if (!only_check) {
	    $("#faq_send").attr("class", "disabled_button");
	    if (!is_email_valid && email.length > 0)
		$("#bad_faq_email").fadeIn("fast");
	    else 
		$("#bad_faq_email").slideUp("fast");
	}
	return false;
    }
}