$( document ).ready( function() {
	$( '#landlord_form').validate({
		submitHandler: function () {
			$( '#landlord_form' ).ajaxSubmit( {
				url: $( '#landlord_form' ).attr('action'),
				type: 'post',
				dataType: 'json',
				beforeSend: function( request )
				{
					_hideMessages( 'landlord' );
					_showLoader();
					return true;
				},		
				success: function( response, textStatus )
				{
					_hideLoader();
					_showMessages( 'landlord', response, true );
				},
				error: function( request, textStatus, errorThrown )
				{
					_hideLoader();
					alert( "Sorry, an error occurred while sending the AJAX request. Please try again later." );
				}
			});
		},
		rules: {
			login: {
				required: true
			},
			pwd: {
				required: true
			}
		},
		errorPlacement: function ( error, element ) {
		},
		highlight: function ( element, errorClass ) {			
		},
		unhighlight: function ( element, errorClass ) {
		
		}
	});	$( '#landlord_form' ).click(function() { isValidLoginForm('landlord_form', 'btn_submit_landlord');}).keyup( function() { isValidLoginForm('landlord_form', 'btn_submit_landlord'); } ).change( function() { isValidLoginForm('landlord_form', 'btn_submit_landlord'); } ).blur( function() { isValidLoginForm('landlord_form', 'btn_submit_landlord'); } );
	isValidLoginForm('landlord_form', 'btn_submit_landlord');
	$( '#tenant_form').validate({
		submitHandler: function () {
			$( '#tenant_form' ).ajaxSubmit( {
				url: $( '#tenant_form' ).attr('action'),
				type: 'post',
				dataType: 'json',
				beforeSend: function( request )
				{
					_hideMessages( 'tenant' );
					_showLoader();
					return true;
				},
				success: function( response, textStatus )
				{
					_showMessages( 'tenant', response, true );
					_hideLoader();
				},
				error: function( request, textStatus, errorThrown )
				{
					_hideLoader();
					alert( "Sorry, an error occurred while sending the AJAX request. Please try again later." );
				}
			} );
		},
		rules: {
			login: {
				required: true
			},
			pwd: {
				required: true
			}
		},
		errorPlacement: function ( error, element ) {
		},
		highlight: function ( element, errorClass ) {			
		},
		unhighlight: function ( element, errorClass ) {
		
		}
	});
	$( '#tenant_form' ).click(function() { isValidLoginForm('tenant_form', 'btn_submit_tenant');}).keyup( function() { isValidLoginForm('tenant_form', 'btn_submit_tenant'); } ).change( function() { isValidLoginForm('tenant_form', 'btn_submit_tenant'); } ).blur( function() { isValidLoginForm('tenant_form', 'btn_submit_tenant'); } );
	isValidLoginForm('tenant_form', 'btn_submit_tenant');
	
	$( '#login1' ).popup( 'landlord' );
	$( '#login2' ).popup( 'tenant' );
	
	$( '#landlord_forgot_form' ).ajaxForm( {
		url: $( '#landlord_forgot_form' ).attr('action'),
		type: 'post',
		dataType: 'json',
		beforeSend: function( request )
		{
			_hideMessages( 'landlord_forgot' );
			_showLoader();
			return true;
		},
		success: function( response, textStatus )
		{
			_hideLoader();
			_showMessages( 'landlord_forgot', response, false );
		},
		error: function( request, textStatus, errorThrown )
		{
			_hideLoader();
			alert( "Sorry, an error occurred while sending the AJAX request. Please try again later." );
		}
	} );
	$( '#tenant_forgot_form' ).ajaxForm( {
		url: $( '#tenant_forgot_form' ).attr('action'),
		type: 'post',
		dataType: 'json',
		beforeSend: function( request )
		{
			_hideMessages( 'tenant_forgot' );
			_showLoader();
			return true;
		},
		success: function( response, textStatus )
		{
			_showMessages( 'tenant_forgot', response, false );
			_hideLoader();
		},
		error: function( request, textStatus, errorThrown )
		{
			_hideLoader();
			alert( "Sorry, an error occurred while sending the AJAX request. Please try again later." );
		}
	} );
	
	$.popup( 'landlord_forgot' );
	$( '#forgot1' ).click( function() {
		$.popuphide( 'landlord' );
		$.popupdisplay( 'landlord_forgot' );
	});
	$.popup( 'tenant_forgot' );
	$( '#forgot2' ).click( function() {
		$.popuphide( 'tenant' );
		$.popupdisplay( 'tenant_forgot' );
	});
	
	$.popup( 'alert' );
} );
function _showMessages( id, data, redirect )
{
	if ( data.success && redirect )
	{
		window.location = data.message;
	}
	else if ( data.success )
	{
		$.popuphide( id );
		$( '#' + id + '_form' ).resetForm();
		$( '#alert_content' ).text( data.message );
		$.popupdisplay( 'alert' );
	}
	else
	{
		$( '.' + id + '_error' ).html( data.message );
		$( '#' + id + '_form' ).resetForm();
	}
}

function isValidLoginForm(formID, buttonID)
{
	if ( $( '#' + formID ).valid() )
	{
		$( '#' + buttonID ).removeClass( 'land_btn3' ).addClass( 'land_btn2' );
	}
	else
	{
		$( '#' + buttonID ).removeClass( 'land_btn2' ).addClass( 'land_btn3' );
	}
}
function _hideMessages( id )
{
	$( '.' + id + '_error' ).empty();
}
function _showLoader()
{
	$( '#ajax-loader' ).css( {
		'top': $( document ).scrollTop(),
		'padding-top': Math.round( ( $( window ).height() - 100 ) / 2 ) + 'px',
		'display': 'block'
	} );
}
function _hideLoader()
{
	$( '#ajax-loader' ).css( 'display', 'none' );
}
