jQuery.noConflict();

    function updateTips( atips, t ) {
        atips.text( t ).addClass( "ui-state-highlight" );
            setTimeout(function() {
            atips.removeClass( "ui-state-highlight", 1500 );
            }, 500 );
    }

    function checkLength( atips, o, n, min, max ) {
        if ( o.val().length > max || o.val().length < min ) {
            o.addClass( "ui-state-error" );
            updateTips( atips, lang_text.field_min_max.format(n, min, max) );
            return false;
            }
         else {
                return true;
            }
    }

    function checkSelected( atips, o, n) {
        if ( o.val() == "" ) {
            o.addClass( "ui-state-error" );
            updateTips( atips, lang_text.field_not_selected.format(n) );
            return false;
            }
         else {
                return true;
            }
    }

    function checkEqual( atips, o, m, t) {
        if ( o.val() != m.val() ) {
            o.addClass( "ui-state-error" );
            updateTips( atips, t );
            return false;
            }
         else {
                return true;
            }
    }

    function checkNotEmpty( o, n) {
        if ( o.val() == "" ) {
            o.addClass( "ui-state-error" );
            return false;
            }
         else {
                return true;
            }
    }

jQuery(document).ready(function() {

    jQuery("#dialog:ui-dialog").dialog( "destroy" );

    jQuery('#login-link').click(function() {
	jQuery.ajax({
           async:false,
           cache:false,
           data: '',
           dataType: "text",
           timeout:5000,
           type: "POST",
/*         context: jQuery(this),*/
           context: document.body,
           url: "/ajax/login-window/",
           error: function(jqXHR, textStatus, errorThrown) {
             alert("error");
             console.log(textStatus);
             },
           success: function(data, textStatus){
             if ((data!=null) && (data.length>=1)){
		jQuery("#dialog-login").html(data);
		jQuery("#dialog-login").dialog( "open" );
                var button = jQuery("#dialog-button-lost-pass");
  	        if (button.length > 0) {
                   if (button.is(":visible")) { button.hide(); }
                   }
                }
             }
        });
    });

    jQuery( "#dialog-login" ).dialog({
        autoOpen: false,
        height: 250,
        width: 350,
        modal: true,
        resizable: false,
        buttons: [
	  {
    	    id: "dialog-button-lost-pass",
	    text: lang_text.lost_password,
	    click: function() {
    	      jQuery( this ).dialog( "close" );
	      window.location.href ='/zabyl-parol/';
    	      }
	  },
          {
    	    text: "Ok",
	    click: function() {
	      var 
                bValid=true,
		email= jQuery("#login_email"),
		pass= jQuery("#login_password"),
                allFields = jQuery( [] ).add(email).add(pass);
	      allFields.removeClass( "ui-state-error" );
              bValid = bValid && checkNotEmpty(email, lang_text.login_id);
              bValid = bValid && checkNotEmpty(pass, lang_text.password);
	      if (! bValid) {
  	 	 jQuery( ".LoginValidateTips" ).html(lang_text.fields_complite_all);
	 	 jQuery( ".LoginValidateTips" ).removeClass( "ui-state-error" );
                 }
              else {
                jQuery.ajax({
                  async:false,
                  cache:false,
                  data:{email: email.val(), password: pass.val() },
                  dataType: "text",
                  timeout:5000,
                  type: "POST",
                  context: document.body,
                  url: "/ajax/login/",
                  error: function(jqXHR, textStatus, errorThrown) {
                    alert("error");
                    console.log(textStatus);
                    },
                  success: function(data, textStatus){
                    if ((data!=null) && (data=='ok')){
                       jQuery( this ).dialog( "close" );
		       window.location.href ='/';
                       }
		    else {
  		      jQuery( ".LoginValidateTips" ).html(lang_text.wrong_login_pass);
		      jQuery( ".LoginValidateTips" ).addClass( "ui-state-error" );
		      /* email.val(''); */
		      pass.val('');
                      var button = jQuery("#dialog-button-lost-pass");
  	              if (button.length > 0) {
                         if (! button.is(":visible")) { button.show(); }
                         }
                      }
 	            }
	          });
                }
	    }
	  }],
        close: function() {
/*  	  allFields.val( "" ).removeClass( "ui-state-error" );*/
	  }
    });

    jQuery('#logout-link').click(function() {
	jQuery.ajax({
           async:false,
           cache:false,
           data: '',
           dataType: "text",
           timeout:5000,
           type: "POST",
/*         context: jQuery(this),*/
           context: document.body,
           url: "/ajax/logout/",
           error: function(jqXHR, textStatus, errorThrown) {
             alert("error");
             console.log(textStatus);
             },
           success: function(data, textStatus){
	     window.location.href ='/';
             }
        });
    });

    jQuery('#start-test-button').button().click(function() {
	jQuery.ajax({
           async:false,
           cache:false,
           data: {test_id: jQuery("#test_id").val(), test_type: jQuery(":radio[name=test_type]").filter(":checked").val(), question_quantity: jQuery("#question_quantity").val()},
           dataType: "text",
           timeout:5000,
           type: "POST",
/*         context: jQuery(this),*/
           context: document.body,
           url: "/ajax/start-test/",
           error: function(jqXHR, textStatus, errorThrown) {
             alert("error");
             console.log(textStatus);
             },
           success: function(data, textStatus){
             if ((data!=null) && (data.length>=1)){
/*	        window.location.href ='/test/id/'+data+'/';*/
		window.open('/test/id/'+data+'/', '_blank');
                }
             }
        });
    });

    jQuery('a.p-test').click(function() {
      jQuery('div[class^=qa_]').hide();
      jQuery('div[class='+this.id+']').show();
      jQuery('a.p-test').removeClass('p-test-active');
      jQuery(this).addClass('p-test-active');
    });

}); /* eof ready */

