// av_dialogs.js


  	//define cancel button function
   	function	avlg_cancel () 
	{	//close the dialog
	    $("#dlgLogin").dialog("close");
   	}



    //define login button function
	function	avlg_getResponse()
	{
		// get the fields from the dialog
		var		username = $( '#txtEmailLogin' ).val();
		var		pwd = $( '#txtPwdLogin' ).val();
	
		if ( isValidEmail( username ) )
		{
			$( '#dlgLogin' ).dialog("close");
			avlg_postResponseFunc( g_servletPath, username, pwd );
		}
		else
		{
			alert( 'Please anter a valid email address' );
			$( '#txtEmailLogin' ).focus();
		}
		
		
		return;
   	}


  


    	
	/* login dialog options */
	var		avlg_postResponseFunc = null;		// js function to call after response received, allows specific form control
	var		g_servletPath = "";					// used to avoid java in script library - App.getServletPath()

	//define config object
    var avlg_dialogOpts =
	  	{
    		dialogClass:	'jidLoginDlg',	// helps find the buttons
		    modal: true,
		    overlay: { background: "url(img/modal.png) repeat" },

			buttons: { "Cancel": avlg_cancel, "Login": avlg_getResponse },
			autoOpen: false
    	}





	function	avlg_init( servletPath, funcResponse )
	{
		//create the dialog
		avlg_postResponseFunc = funcResponse;
		g_servletPath = servletPath;
			
    	$("#dlgLogin").dialog( avlg_dialogOpts );
    	
    	// call the dialog on click
	   	$("#btnLoginDlg").click
			(
				function() 
				{    //open the dialog
					// clear out the password
					$( '#txtPwdLogin').val( '' );
					
					updateDlgButtonState();
					
		   			$("#dlgLogin").dialog("open");
	    		}
			);
	    	
	    // auto submit
	    $('#dlgLogin').keyup
	    	(
	    		function(e) 
	    		{
	    			updateDlgButtonState();
	    			
	    			if (e.keyCode == 13 && isDlgFull() ) 
	    			{
	    				//Close dialog and/or submit here...
	    				avlg_getResponse();
	    			}
	    		}
	    	);
    	
    }




/////////////////////////////////////////////////////////////////////////


//logout : 
//do an ajax call to logout.  
//The ajax response marks the user name as anonymous.
//
function	av_logout( servletPath )
{
	$.get( servletPath + "AjaxServlet", { action: 'ajax_logout' },
			function(xml)
			{
				$( '#username' ).text( 'anonymous' );
				$( '#btnLoginDlg' ).show();
				$( '#btnLogout' ).hide();
				
			//	$( '#useron' ).hide();
			} );
}



//login : using the passed username and password
//try an ajax call, passing username & password.
//the ajax response looks for the user name (a sucessful logon) and displays the user name
//a failed logon pops up a message.
//
function	av_login( servletPath, username, pwd )
{
	//alert( 'av_login' );
	$.get( servletPath + "AjaxServlet", { action: 'ajax_login', username: username, password: pwd  },
			function(xml)
			{
				var  username = $(xml).find( 'custname' ).text();
				
				if ( username == null || username.length < 1 )
					alert( 'Username or password were not recognised.' );
				else
				{
					$( '#username' ).text( username );
					
					$( '#btnLogout' ).show();
					$( '#btnLoginDlg' ).hide();
					
					
//					$( '#useron' ).show();
				}
			} );
} 









function	updateDlgButtonState()
{
	if ( isDlgFull() )
		jqEnableButton( 'jidLoginDlg', 'Login' );
	else
		jqDisableButton( 'jidLoginDlg', 'Login' );
}


function	isDlgFull()
{
	return ( jQuery.trim( $( '#txtEmailLogin' ).val() ).length != 0 && jQuery.trim( $( '#txtPwdLogin' ).val() ).length != 0 );
}
