$( function() {
	$( '#terms_link' ).click( function( event ) {
		event.preventDefault();//Cancel the link behavior
	
		var winH = $( window ).height();
		var winW = $( window ).width();
		
		var maskHeight = $( document ).height();
		var maskWidth = $( window ).width();
		
		$( '<div></div>' ).appendTo( 'body' ).attr( 'id', 'mask' ).css( {'width':maskWidth,'height':maskHeight} ).fadeIn( 1000, function() {
			$( this ).fadeTo( 'slow', 0.8, function () {
				//this runs after fade in - Set the popup window to center
				var dialogueHeight = $( '#terms' ).height();
				var dialogueWidth = $( '#terms' ).width();
				
				var dialogueTop = ( winH / 2 ) - ( dialogueHeight / 2 );
				if ( dialogueTop < 0 ) dialogueTop = 20;
				var dialogueLeft = ( winW / 2 ) - ( dialogueWidth / 2 );
				
				$( '#terms' ).css( { 'top': dialogueTop, 'left': dialogueLeft } ).slideDown( 'normal' );
				$( '.closeMask' ).show( 'normal' ).click( function() {
					$( this ).hide( 'normal', function() {
						removeMask( '#terms' );
					} );
				} );
			} );	
		} ).click( function() {
			removeMask( '#terms' );
		} );
	} );
	
	$( '#client_login_link' ).click( function( event ) {
		event.preventDefault();//Cancel the link behavior
	
		var winH = $( window ).height();
		var winW = $( window ).width();
		
		var maskHeight = $( document ).height();
		var maskWidth = $( window ).width();
		
		$( '<div></div>' ).appendTo( 'body' ).attr( 'id', 'mask' ).css( {'width':maskWidth,'height':maskHeight} ).fadeIn( 1000, function() {
			$( this ).fadeTo( 'slow', 0.8, function () {
				//this runs after fade in - Set the popup window to center
				var dialogueHeight = $( '#clientLogin' ).height();
				var dialogueWidth = $( '#clientLogin' ).width();
				
				var dialogueTop = ( winH / 2 ) - ( dialogueHeight / 2 );
				if ( dialogueTop < 0 ) dialogueTop = 20;
				var dialogueLeft = ( winW / 2 ) - ( dialogueWidth / 2 );
				
				$( '#clientLogin' ).css( { 'top': dialogueTop, 'left': dialogueLeft } ).slideDown( 'normal', function () {
					$( '#mask' ).css( 'height', $( document ).height() + 50 );
				} ); 
				$( '.closeMask' ).show( 'normal' ).click( function() {
					$( this ).hide( 'normal', function() {
						removeMask( '#clientLogin' );
					} );
				} );
			} );
		} ).click( function() {
			removeMask( '#clientLogin' );
		} );
	} );
	
	$( '#clientLogin' ).submit( function( e ) {
		e.preventDefault();//Cancel the form submission
		var clientPassword = escape( $( '#u' ).val() );
		if ( clientPassword == '' ) {
			alert( 'Please enter your password.' );
			return false;
		}
		
		$.ajax({
			url: '/zinvoice/ajax/checkuser.php?u=' + clientPassword,
			cache: false,
			success: function( data ) {
				data = parseInt( data );
				if ( data == 0 ) {
					alert( 'Your login was invalid. Please try again.' );
				} else {
					//login was ok so ajax in new data
					$.ajax({
						url: '/zinvoice/ajax/clientInfo.php?clientID=' + data + '&clientPassword=' + clientPassword,
						cache: false,
						success: function( html ) {
							if ( html == 'ERROR' ) alert( 'There was an error loading your data. Please try again.' );
								else $( '#clientInfo' ).html( html );
							$( '#clientLogin' ).css( 'top', '20px' );
							$( '#mask' ).css( 'height', $( document ).height() + 50 );
						}
					});
				}
			}
		});
	} );
} );

function removeMask( mainElement ) {
	$( mainElement ).slideUp( 'normal', function() {
		$( '#mask' ).fadeOut( 1000, function() {
			$( this ).remove();
		} );//get rid of the mask we just created
	} );
}