// JavaScript Document
$( document ).ready( function()
{	
// INIT RECOMMENDATION FUNCTIONS
	// Initiate Plugin
	/*if( typeof override_recommend_lightbox != 'undefined' )
	{
		$( '#jq_lightbox_recommend' ).jqm( { trigger : '.jq_initiate_recommend_btn' } );
		
		// Create JS vars required
		if( typeof user_kiosk_id == 'undefined' )
			var user_kiosk_id = kiosk_id;
		if( typeof user_logged_in == 'undefined' )
			var user_logged_in = 1;	// true
		
		// Create Special Event Handler
		$( '.jq_initiate_recommend_btn' ).click(
			function()
			{
				// Parse Href
				var this_href = $( '' ).parseHref( this.href );
				this_href = this_href.split( '|' );
				
				// Update id in form
				$( '#recommend_kiosk' ).val( kiosk_id );
				if( this_href[1] == 'product' )
					$( '#recommend_product' ).val( this_href[0] );
				else if( this_href[1] == 'service' )
					$( '#recommend_service' ).val( this_href[0] );
				
				
				// Default return
				return false;
			} );
	}
	else*/
	$( '#jq_lightbox_recommend' ).jqm( { trigger : '#jq_initiate_recommend_btn' } );

	// Event Handler: Submit Form
	$( '#recommend_form' ).submit(
		function()
		{
			// Success Function
			var recommendSuccess =
				function( data )
				{
					// Hide Loading Bar
					$( '.btn_1', '#jq_lightbox_recommend' ).show();
					$( '.btn_4', '#jq_lightbox_recommend' ).show();
					$( '.jq_loading_bar', '#jq_lightbox_recommend' ).hide();
					
					// Analyze data
					if( data.substr(0, 1) != '1' )
					{
						// Invalid login, please try again
						$( '' ).showAlert( 'a1', data.substr(2, data.length) );
					}
					else
					{
						// Sent message
						
						// Clear Fields
						$( '#recommend_to' ).val( '' );
						$( '#recommend_subject' ).val( '' );
						$( '#recommend_message' ).val( '' );
						
						// Close Form
						$( '.jqmClose' ).click();
						
						// Show confirmation
						$( '' ).showAlert( 'a2', data.substr(2, data.length) );
					}
				}
				
			// Options
			var recommend_options = { success : recommendSuccess, url : 'http://' + location.host + ajax_prefix + $( this ).attr( 'action' ) };
			
			try
			{
				// Hide Buttons
				$( '.btn_1', '#jq_lightbox_recommend' ).hide();
				$( '.btn_4', '#jq_lightbox_recommend' ).hide();
				// Show Loading Bar
				$( '.jq_loading_bar', '#jq_lightbox_recommend' ).show();
				// Submit with new ajax url
				$( this ).ajaxSubmit( recommend_options );
			}
			catch( err )
			{
				// Close Form
				$( '.jqmClose' ).click();
				// Alert Error
				alert( "Error Submitting via Ajax\n" + err );	
			}
			
			// Return False
			return false;
		} );
	
	// Event Handler: Submit Btn
	$( '#jq_submit_recommend_btn' ).click(
		function()
		{
			// Validate
			if( validateRecommendationForm() )
				$( '#recommend_form' ).submit();
			
			// Default Return
			return false;
		} );
	
	// Use Contacts Button
	$( '#jq_use_contacts_btn' ).click(
		function()
		{
			// Determine what to do
			if( user_logged_in )
			{
				// Hide Submit Btn
				$( '.btn_1', '#jq_lightbox_recommend' ).hide();
				
				// Hide this and show contacts
				$( '#jq_manual_contacts_here' ).hide( 'fast',
					function()
					{
						show_contacts_steps();
					} );
			}
			else
			{
				// Remember action
				action_selected = 'recommend';
				$( '.jq_cancel_recommend_btn' ).click();
				$( '.jq_initiate_login_btn' ).click();
			}
			
			// Always return false
			return false;
		} );
	
	// Show Contacts
	var show_contacts_steps =
		function()
		{
			// Show Loading
			$( '#jq_loading_bar_for_contacts' ).slideDown( 'fast' );
			
			// Load via AJAX
			$( '#jq_load_contact_here' ).load( 'http://' + location.host + ajax_prefix + ajax_contacts + user_kiosk_id,
				function()
				{
					// Show Result
					$( '#jq_loading_bar_for_contacts' ).slideUp( 'slow',
						function()
						{
							// Initiate and add height to Contact Box (if needed)
							$( '.jq_icontacts' ).initIContactBox( 'to' );
							$( '.jq_econtacts' ).initIContactBox( 'to' );
							$( '#jq_load_contact_here' ).slideDown( 'normal',
								function()
								{
									// Show Submit Btn
									$( '.btn_1', '#jq_lightbox_recommend' ).show();
									
									// Check to see if any contacts, ifnot, slide manual down
									if( $( '#jq_no_contacts' ).val() == 'true' )
									{
										// Hide "Use Contacts" btn
										$( '#jq_manual_contacts_here > .radio_detalles' ).hide();
										// Slide down
										$( '#jq_manual_contacts_here' ).slideDown();
									}
									else
									{
										// Tab action: Econtacts
										$( '#jq_econtacts_tab' ).click(
											function()
											{
												// Show This tab and select it
												$( '.jq_econtacts' ).show();
												$( '#jq_econtacts_tab' ).parent().addClass( 'act' );	// DEPRECATED - active
												
												// Hide Other Tab and unselect tab
												$( '.jq_icontacts' ).hide();
												$( '#jq_icontacts_tab' ).parent().removeClass( 'act' );	// DEPRECATED - active
												
												// Return
												return false;
											} );
										
										// Tab action: Icontacts
										$( '#jq_icontacts_tab' ).click(
											function()
											{
												// Show This tab and select it
												$( '.jq_icontacts' ).show();
												$( '#jq_icontacts_tab' ).parent().addClass( 'act' );	// DEPRECATED - active
												
												// Hide Other Tab and unselect tab
												$( '.jq_econtacts' ).hide();
												$( '#jq_econtacts_tab' ).parent().removeClass( 'act' );	// DEPRECATED - active
												
												// Return
												return false;
											} );
									}
								} );
						} );
				} );		
		}
		
	// Hide All embedded error fields
	$( '.field_error_msg' ).parent().parent().hide();

	// Validate Recommendatation Form
	var validateRecommendationForm =
		function()
		{
			// Declare Local Variables
			var valid = true;
			
			// Determine which path to take
			if( typeof $( '#to' ).val() != 'undefined' )
			{
				// Must select from contacts
				$( '#recommend_type' ).val( 'contacts' );	// Modify value for server
				
				if( $( '#to' ).html() == '' ) /*val()*/
				{
					$( '#jq_contacts_error_field' ).show();
					valid = false;
				}
				else
					$( '#jq_contacts_error_field' ).hide();	// Remove Error
			}
			else
			{
				// Must select from manual
				$( '#recommend_type' ).val( 'manual' );	// Modify value for server
				
				if( typeof $( '#recommend_sender_kiosk_id' ).val() == 'undefined' )
				{
					// Name
					if( $( '#recommend_sender_name' ).val() == '' )
					{
						$( '#jq_sender_name_error_field' ).show();
						valid = false;
					}
					else
						$( '#jq_sender_name_error_field' ).hide();	// Remove Error
					
					// Email
					if( $( '#recommend_sender_email' ).val() == '' )
					{
						$( '#jq_sender_email_error_field .field_error_msg' ).html( 'requerido' );
						$( '#jq_sender_email_error_field' ).show();
						valid = false;
					}
					else if( !$( '#recommend_sender_email' ).isValidEmail() )
					{
						$( '#jq_sender_email_error_field .field_error_msg' ).html( 'invalido' );
						$( '#jq_sender_email_error_field' ).show();
						valid = false;
					}
					else
						$( '#jq_sender_email_error_field' ).hide();	// Remove Error
				}
				
				// To
				if( $( '#recommend_to' ).val() == '' )
				{
					$( '#jq_recommend_to_error_field' ).show();
					valid = false;
				}
				else
					$( '#jq_recommend_to_error_field' ).hide();	// Remove Error
			}
			
			
			// Subject
			if( $( '#recommend_subject' ).val() == '' )
			{
				$( '#jq_subject_error_field' ).show();
				valid = false;
			}
			else
				$( '#jq_subject_error_field' ).hide();	// Remove Error
			
			// Message
			if( $( '#recommend_message' ).val() == '' )
			{
				$( '#jq_message_error_field' ).show();
				valid = false;
			}
			else
				$( '#jq_message_error_field' ).hide();	// Remove Error
			
			// Return
			return valid;	
		}

// END OF INIT RECOMMENDATION FUNCTIONS
	
} );
