/**
 * JavaScript functions specific to the application form page.
 * 
 * @package    Webster_Network_Framework
 * @subpackage Public
 * @version    $Id$
 * @author     Webster Network
 * @copyright  Copyright (c) 2002-2009, Webster Network
 * @link       http://www.webster-network.com/
 */

var numDocuments = 1;

/**
 * Updates the shipping address text fields based on the value selected in the
 * "Shipping address same as address above?" field.
 * 
 * @param  void
 * @return void
 */
function updateShippingAddress()
{
	// Default field values for "no" selection
	shippingAddress = '';
	shippingCity    = '';
	shippingState   = '';
	shippingZipCode = '';
	
	// Update field values for "yes" selection
	if (document.application_form.shipping_address_same_as_operator[0].checked)
	{
		shippingAddress = document.application_form.operator_address.value;
		shippingCity    = document.application_form.operator_city.value;
		shippingState   = document.application_form.operator_state.selectedIndex;
		shippingZipCode = document.application_form.operator_zip_code.value;
	}
	
	// Set field values based on "yes" or "no" selection
	document.application_form.shipping_address.value       = shippingAddress;
	document.application_form.shipping_city.value          = shippingCity;
	document.application_form.shipping_state.selectedIndex = shippingState;
	document.application_form.shipping_zip_code.value      = shippingZipCode;
	
	return;
}

/**
 * All supporting document file fields start off hidden except for
 * the first one.
 *
 * @param  void
 * @return void
 */
function initializeDocumentFields()
{
	for (i = 2; i <= 5; i++)
	{
		hidePanel("file_" + i);
	}
	
	return;
}

/**
 * Adds another supporting document file field.  Only five fields are
 * available, once this limit has been reached the add link is removed.
 * 
 * @param  void
 * @return void
 */
function addDocumentField()
{
	if (numDocuments < 5)
	{
		numDocuments++;
		
		$("input#file_" + numDocuments).fadeIn("slow");
	}
	
	if (numDocuments >= 5)
	{
		hidePanel("add_document_field_link");
	}
	
	return;
}

/**
 * Updates the total cost amount and message lines whenever a main manual
 * checkbox is clicked (selected or deselected).
 * 
 * @param  void
 * @return void
 */
function updateTotalCost()
{
	cost    = 0.0;
	message = '';
	
	/*
	 * Determine the total cost based on the checkboxes that are selected.
	 */
	
	if ((document.application_form.manuals_main_both.checked) ||
		((document.application_form.manuals_main_rvsm.checked) &&
	     (document.application_form.manuals_main_international.checked)))
	{
		cost    = document.application_form.manual_both_cost.value;
		message = 'RVSM Manual: $' + document.application_form.manual_rvsm_cost.value + '<br />' +
				  'International Operations Manual: $' + document.application_form.manual_international_cost.value;
	}
	else if (document.application_form.manuals_main_rvsm.checked)
	{
		cost    = document.application_form.manual_rvsm_cost.value;
		message = 'RVSM Manual: $' + document.application_form.manual_rvsm_cost.value;
	}
	else if(document.application_form.manuals_main_international.checked)
	{
		cost    = document.application_form.manual_international_cost.value;
		message = 'International Operations Manual: $' + document.application_form.manual_international_cost.value;
	}
	
	cost = '<span class="emphasize">Total Amount Due: $' + cost + '</span>';
	
	document.getElementById('total_cost_amount').innerHTML  = cost;
	document.getElementById('total_cost_message').innerHTML = message;
    
	return;
}

/**
 * Hides all of the payment type panels.
 * 
 * @param  void
 * @return void
 */
function hidePaymentTypePanels()
{
	hidePanel('payment_type_panel_credit_card');
	hidePanel('payment_type_panel_check');
	hidePanel('payment_type_panel_arrange_payment');
	
	return;
}

/**
 * Shows the payment type panel based on the selection of the "Payment Type"
 * drop down.
 * 
 * @param  void
 * @return void
 */
function launchPaymentTypePanel()
{
	/*
	 * Hide any payment type panels that are already showing.
	 */
	
	hidePaymentTypePanels();
	
	/*
	 * Show the new payment type panel based on the selected option of the
	 * payment type drop down.
	 */
	
	selectedIndex = document.application_form.payment_type.selectedIndex;
	
	switch (document.application_form.payment_type.options[selectedIndex].value)
	{
		case 'credit_card':
			showPanel('payment_type_panel_credit_card');
			
			break;
		case 'check':
			showPanel('payment_type_panel_check');
			
			break;
		case 'arrange_payment':
			showPanel('payment_type_panel_arrange_payment');
			
			break;
	}
	
	return;
}