/* Används av masterpage(jquery.nnp.login.tb.js används av Login.aspx ) */

/*$(document).ready(function(){
	SetupIpLoginFunctionality();	
});*/
var tb_pathToImage = "/images/sections/loadingAnimations/loadingAnimation.gif";
var submit = false;

$(document).ready(function()
{   
    imgLoader = new Image();// preload image
    imgLoader.src = tb_pathToImage;
    
    //Attach keypress-event to loginbutton
    $("form#form_login")
        .livequery('keypress', function(e) {
            if (e.which == 13) {
                submitLoginForm('form_login');
            }
            
        });
    
    
        //Attach keypress-event to password recovery submit button
    $("#btnRecoverPassword")
        .livequery('click', function(e) {
            submitLoginForm('form_passwordrecovery');
        });  
        
            
    //Attach keypress-event to password recovery submit button
    $("form#form_passwordrecovery")
        .livequery('keypress', function(e) {
            if (e.which == 13) {
                submitLoginForm('form_passwordrecovery');
            }
        });    

     
     //Attach click-event to clear email textbox
     $('#tbPwRecoveryEmail')
      .livequery('click', function(e){
            $('#tbPwRecoveryEmail').val('');    
        }
    )
        
});


/**
 * submitLoginForm - Submits login-form with ajax.
 * Uses jquery & jquery.form.js & thickbox.js.
 * 
 * @author Jonas Norrby
**/
function submitLoginForm(formId)
{   
    var newUrl = $('#'+formId).attr('action');
    
    //inleder url:en med en slash
    if(!($('#'+formId).attr('action')[0] == '/'))
    {
        newUrl = '/' + $('#'+formId).attr('action');
    }

    var options;
    if(formId == 'form_passwordrecovery')
    { 
        options = { 
            target:        '#TB_ajaxContent',   // target element(s) to be updated with server response 
            success:       showPWRResponse,  // post-submit callback 
            url: newUrl
        }; 
    }
    else
    {
          options = { 
            target:        '#TB_ajaxContent',   // target element(s) to be updated with server response 
            success:       showResponse,  // post-submit callback 
            url: newUrl
        }; 
    }
            
    $('#'+formId).submit(function() { 
            // inside event callbacks 'this' is the DOM element so we first 
            // wrap it in a jQuery object and then invoke ajaxSubmit 
            $(this).ajaxSubmit(options); 
            
            // !!! Important !!! 
            // always return false to prevent standard browser submit and page navigation 
            return false; 
        }); 

}


    // pre-submit callback 
    function showRequest(formData, jqForm, options) { 
        // formData is an array; here we use $.param to convert it to a string to display it 
        // but the form plugin does this for you automatically when it submits the data 
        var queryString = $.param(formData); 
     
        // jqForm is a jQuery object encapsulating the form element.  To access the 
        // DOM element for the form do this: 
        // var formElement = jqForm[0]; 
     
        //alert('About to submit: \n\n' + queryString); 
     
        // here we could return false to prevent the form from being submitted; 
        // returning anything other than false will allow the form submit to continue 
        return true; 
    } 
 
    // post-submit callback 
    function showResponse(responseText, statusText)  { 
        // If login succeded response is empty. Close thickbox
        if (responseText.length == 0)
        {
            tb_remove();
            
            $('body').css('cursor','progress'); 
            
            /* Ta bort #-tecken eftersom det kan skapa system.threading.abortexception. */
            window.location.href = window.location.href.replace('#', '');
        }
    } 
    
    // post-submit callback 
    function showPWRResponse(responseText, statusText)  
    { 
        if (responseText.length == 0)
        {
            tb_remove();
           // location.reload(); 
        }
    }
     
    function show_progress() 
    { 
        $("body").append("<div id='TB_load'><img id='IMG_load' src='"+imgLoader.src+"' /></div>");//add loader to the page
	    $('#TB_load').show();//show loader
    } 

//Function som ser til att textboxarna för användarnamn
//och lösenord töms och inaktiveras när checkboxen för 
//Ip-login är markerad.
function SetupIpLoginFunctionality()
{
    var isChecked = $("#loginform input:checkbox").attr("checked") == true;
	EnableDisableTextboxes(isChecked);
		
	//Metod för att aktivera/inaktivera textboxarna i inloggningsformuläret
	//doDisable = Bool inparameter hurtillvida användarnamn och lösenord ska inaktiveras.	
	function EnableDisableTextboxes(doDisable)
	{
		if (doDisable == true) {		
			$("#loginform input:text").attr("disabled", "disabled");
			$("#loginform input:text").attr("value", "");
			$("#loginform input:text").addClass("disable");
			$("#loginform input:password").attr("disabled", "disabled");
			$("#loginform input:password").attr("value", "");
			$("#loginform input:password").addClass("disable");
			
			$("#validatorReqFieldUserName").hide();
			$("#validatorReqFieldPassWord").hide();
		}
		else {
			$("#loginform input:text").removeAttr("disabled");
			$("#loginform input:text").removeClass("disable");
			$("#loginform input:password").removeAttr("disabled");
			$("#loginform input:password").removeClass("disable");
			$("#loginform input:text").addClass("text");
			$("#loginform input:password").addClass("text");
		}
	}
}




