/**
 * These should be GLOBAL javascript functions that are used on ALL our rfi forms.
 * No vert/horiz specific code here. 
 * Don't put any direct-execution code here!
 *
 * This code relies on the prototype.js library.
 * Don't go trying to use this without FIRST including that!
 *
 * This file was coded against prototype 1.5.1.1. 
 *
 * @author		dustin
 */

/**
 * Global-scope variables that are needed in processing. Please document their usage!
 */

// Controls the lightbox form element target for the left hand side of lightbox
var lb_left_target = false;

// Controls the lightbox form element target for the right hand side of lightbox
var lb_right_target = false;

// Stores the lightbox element itself. In future land, we might want an array or hash, but
// for now we only need the one, since we'll only have the one modal box
var lb_handler = false;

 
/**
 * Checks if an xmlhttp object is currently in-progress
 * i.e, still talking on network or waiting for response
 *
 * @author		dustin
 */
function callInProgress(xmlhttp)
{
	// If something async has happened and xmlhttp is no longer valid, it's no longer in progress
	if ( !xmlhttp )
		return false;
		
	switch ( xmlhttp.readyState )
	{
		case 1:
		case 2:
		case 3:
			return true;
		break;

		default:
			return false;
		break;
	}
}


/**
 * Hides the ajax activity indicator...
 *
 * @author		dustin
 */
function hideActivity()
{
	$('activityIndicator').hide();
}


/**
 * Shows the "edit address" form on the lightbox
 *
 * @author		dustin
 */
function lightboxAddressShowEdit()
{
	$('lightboxedit').hide();
	$('lb_left_text').hide();
	$('lb_left_edit').show();
	$('lb_address_left').checked = true;
	showModalBox();
}


/**
 * Does the lightbox address processing when they hit "continue"
 *
 * @author		dustin
 */
function lightboxAddressProcess()
{
	if ( $('lb_address_left').checked )
	{
		if ( $('lb_left_edit').visible() )
		{
			$('rfiForm').elements['data[address]'].value = $F('lb_address_address');
			
			if ( $('lb_address_address2') )
				$('rfiForm').elements['data[address2]'].value = $F('lb_address_address2');			
			
			$('rfiForm').elements['data[city]'].value = $F('lb_address_city');
			$('rfiForm').elements['data[postal]'].value = $F('lb_address_postal');
			$('rfiForm').elements['data[st]'].value = $F('lb_address_st');
		}
	}
	else if ( $('lb_address_right').checked )
	{
		$('rfiForm').elements['data[address]'].value = $F('lb_fixed_address');
		$('rfiForm').elements['data[address2]'].value = $F('lb_fixed_address2');
		$('rfiForm').elements['data[city]'].value = $F('lb_fixed_city');
		$('rfiForm').elements['data[postal]'].value = $F('lb_fixed_postal');
		$('rfiForm').elements['data[st]'].value = $F('lb_fixed_st');
	}
	
	hideModalBox();
	addressVerified = true;
	// something may have changed in the geo, so we have to re-run validation...
	validateForm();
}

/**
 * Shows the editing forms, and gets rid of the "edit numbers" button
 *
 * @author		dustin
 */
function lightboxPhoneShowEdit()
{
	$('lb_left_text').hide();
	$('lb_left_edit').show();

	if ( lb_right_target )
	{
		$('lb_right_text').hide();
		$('lb_right_edit').show();
	}
	
	$('lightboxedit').hide();
	showModalBox();
}

/**
 * Processes the edit phone form. If the numbers changed, update the form and re-submit.
 * Otherwise, just re-submit
 *
 * Either way, set the "phonevalidated" to true.
 *
 * @author		dustin
 */
function lightboxPhoneProcess()
{
	if ( lb_left_target == '' )
		lb_left_target = 'data[phone1]';
		
	if ( lb_right_target == '' )
		lb_right_target = 'data[phone2]';
	
	// Should we be updating?
	if ( $('lb_left_edit').visible )
		$('rfiForm').elements[lb_left_target].value = $F('lb_left_edit');
		
	if ( $('lb_right_edit') && $('lb_right_edit').visible() )
		$('rfiForm').elements[lb_right_target].value = $F('lb_right_edit');
	
	// Now hide lightbox and re-run form.
	phoneVerified = true;
	hideModalBox();
	doVerify();
}



/**
 * Shows the Modal Box
 *
 * @author		dustin
 * @requires 	leightbox's version of lightbox.js, modified by dustin (lightbox_MOD.js)
 */
function showModalBox()
{
	lb_handler = new lightbox('lightbox');
	lb_handler.activate();
}


/**
 * Hides the Modal Box
 *
 * @author		dustin
 * @requires	leightbox's version of lightbox.js, modified by dustin (lightbox_MOD.js)
 */
function hideModalBox()
{
	if ( lb_handler && lb_handler.deactivate )
	{
		lb_handler.deactivate();
		lb_handler = false;
	}
	else
	{
		throw("couldn't find lightbox handler");
	}
}