var messageDelay = 3000;  // How long to display status messages (in milliseconds)
// Init the form once the document is ready
$( init );
 
 
// Initialize the form
 
function init() {
	
	$('a[name=loginForm]').click(function(e) {
		e.preventDefault();
		
		var val = $(this).attr('href');
		var fadeInscreenHeight = $(document).height();
		var fadeInscreenWidth = $(window).width();
		$('#fadeInscreen').css({'width':fadeInscreenWidth,'height':fadeInscreenHeight});
		
		$('#fadeInscreen').fadeIn();	
		$('#fadeInscreen').fadeTo("slow",0.8);	
	
});


  // Hide the form initially.
  // Make submitForm() the form's submit handler.
  // Position the form so it sits in the centre of the browser window.
  $('#loginForm').hide().submit( submitForm ).addClass( 'positioned' );
  
 
 
  // When the "Send us an email" link is clicked:
  // 1. Fade the content out
  // 2. Display the form
  // 3. Move focus to the first field
  // 4. Prevent the link being followed
 
  $('a[href="#loginForm"]').click( function() {
    $('#loginContent').fadeTo( 'slow', .2 );
    $('#loginForm').fadeIn( 'slow', function() {
      $('#userName').focus();
    } )
 
    return false;
  } );
   
  // When the "Cancel" button is clicked, close the form
  $('#cancel').click( function() { 
    $('#loginForm').fadeOut();
	$('#fadeInscreen').fadeOut();
    $('#loginContent').fadeTo( 'slow', 1 );
  } );  
 
  // When the "Escape" key is pressed, close the form
  $('#loginForm').keydown( function( event ) {
    if ( event.which == 27 ) {
      $('#loginForm').fadeOut();
	  $('#fadeInscreen').fadeOut();
      $('#loginContent').fadeTo( 'slow', 1 );
    }
  } );
  
   // When the "fadeInscreen" is clicked, do nothing
  $('#fadeInscreen').click( function() {
	 $('#loginForm').fadeOut();
	$('#fadeInscreen').fadeOut();
    $('#loginContent').fadeTo( 'slow', 1 );
  } );
 
}
// Submit the form via Ajax
 
function submitForm() {
  var loginForm = $(this);
 
  // Are all the fields filled in?
 
  if ( !$('#userName').val() || !$('#password').val() ) {
//   if ( !$('#senderName').val() || !$('#senderEmail').val() || !$('#message').val() ) {
 
    // No; display a warning message and return to the form
    $('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut();
    logintForm.fadeOut().delay(messageDelay).fadeIn();
 
  } else {
 
    // Yes; submit the form to the PHP script via Ajax
	
	//$('#sendingMessage').fadeIn().delay(messageDelay).fadeOut();
 
    $('#sendingMessage').fadeIn(1000);
    loginForm.fadeOut();
 
    $.ajax( {
      url: loginForm.attr( 'action' ) + "?ajax=true",
      type: loginForm.attr( 'method' ),
      data: loginForm.serialize(),
      success: submitFinished
    } );
  }
  // Prevent the default form submission occurring
  return false;
}
// Handle the Ajax response
 
function submitFinished( response ) {
  response = $.trim( response );
  $('#sendingMessage').fadeOut();
 
  if ( response == "success" ) {
 
    // Form submitted successfully:
    // 1. Clear the form fields
    // 3. Reload the window
 
    //$('#successMessage').fadeIn().delay(messageDelay).fadeOut();
	$('#userName').val( "" );
    $('#password').val( "" );
	$("#remember").val( "" );
    	
    //$('#loginContent').delay(messageDelay+500).fadeTo();
	window.location.reload();//to refresh the page after successful log-in
 
  } else {
 
    // Form submission failed: Display the failure message,
    // then redisplay the form
    $('#failureMessage').fadeIn().delay(messageDelay-800).fadeOut();
    $('#loginForm').delay(messageDelay+10).fadeIn();
  }
}
 
