/**
 * All Online Schools request info-specific Javascripts
 *
 * This file was coded against prototype 1.5.1.1
 *
 * @author		dustin
 */

var validationObject = false;


/**
 * When the person changes programs, we re-load the page.
 *
 * If you want to send the whole form data as it exists, send true as the second parameter.
 * Otherwise, to just make the basic request, send false. 
 *
 * @author		dustin
 */
function loadit(programID, useGetString)
{
	checkDupe(programID);
	
	formText = $('rfiForm').serialize();

	$('customfields').innerHTML='<strong class=\'loadingText\'>Please wait, loading...</strong>';

	// Update the Left column with info about the program
	var url = '/rfi_dynamicdata.php?programID=' + programID + '&schoolCollectionID=' + schoolCollectionID;
	new Ajax.Updater('detailsInner', url, {
		method: 'get',
		evalScripts: true
	});
	

	// Now update the fields that display for the specific school.
	url = '/rfi_customfields.php?programID=' + programID + '&schoolCollectionID=' + schoolCollectionID;
	
	var params = 'programID=' + programID;
	if ( useGetString )
		params += '&' + formText;
	
	new Ajax.Updater('customfields', url, {
		parameters: params,
		method: 'post',
		evalScripts: true,
		onComplete: function(request)
		{
			countryChange();	
		}
	});
}


/**
 * Main validation function.
 *
 * @author		dustin
 */
function validateForm()
{
	formText = $('rfiForm').serialize();
	programID = $('programID').value;
	
	url = '/rfi_runfilter.php?schoolCollectionlID=' + schoolCollectionID + '&programID=' + programID;
	
	if ( validationObject && callInProgress(validationObject.transport) )
		validationObject.transport.abort();
	
	validationObject = new Ajax.Request(url, {
		method: 'post',
		parameters: formText,
		evalScripts: true,
		
		// allow us a timeout before we send to server
		onCreate: function(request)
		{
			this.validationObjectTimeoutID = window.setTimeout( 
				function()
				{
					if ( callInProgress(request) )
					{
						failureFunc = validationObject.options.onFailure || Prototype.emptyFunction;
						failureFunc(request);
						
						//onFailure takes care of the rest...
					}
				},
				10000 // 10 seconds
			);
			
			$('activityIndicator').show();
		},
		
		onComplete: function(request)
		{
			validationObject = false;
			window.clearTimeout(this.validationObjectTimeoutID);
		},
		
		onFailure: function(request)
		{
			if ( callInProgress(request) )
			{
				request.abort();	
			}
			
			validationObject = false;
			
			alert("Due to increased server load, we could not complete your request at this time. Please wait a few seconds and try again.");	
		}
			
	});
}


/**
 * Allows verification to be done, if we want it
 *
 * @author		dustin
 */
function setDoVerification(val)
{
	if ( allowVerification )
		doVerification = val;
}


/**
 * State variables for address/phone verification
 */
var addressVerified = false;
var phoneVerified = false;
var verifyObject = false;

function doVerify()
{
	if ( !doVerification )
	{
		// we want to avoid verification for some reason... 
		$('rfiForm').submit();
		return;
	}
	
	if ( !phoneVerified )
	{
		type = 'phone';
	}
	else if ( !addressVerified )
	{
		type = 'address';
	}
	else
	{
		// We're done here.
		$('rfiForm').submit();
		return;
	}	

	var formText = $('rfiForm').serialize();
	var programID = $('programID').value;
	
	var url = '/rfi_verify.php?schoolCollectionID=' + schoolCollectionID + '&programID=' + programID + '&type=' + type;

	if ( verifyObject && callInProgress(verifyObject.transport) )
		verifyObject.transport.abort();

	verifyObject = new Ajax.Updater({success: 'lightbox'}, url, {
		method: 'post',
		parameters: formText,
		evalScripts: true,
		
		// allow us a timeout before we send to server
		onCreate: function(request)
		{
			this.verifyObjectTimeoutID = window.setTimeout( 
				function()
				{
					if ( callInProgress(request) )
					{
						// See my comments in common/g_js/requestinfo/requestinfo.js about this
						if ( this.lbhandler )
						{
							return;
						}
						
						failureFunc = verifyObject.options.onFailure || Prototype.emptyFunction;
						failureFunc(request);
							
						//onFailure takes care of the rest...
					}
				},
				10000 // 10 seconds
			);
			
			$('activityIndicator').show();
		},
		
		onComplete: function(request)
		{
			verifyObject = false;
			window.clearTimeout(this.verifyObjectRequestID);
		},
		
		onFailure: function(request)
		{		
			if ( callInProgress(request) )
			{
				request.abort();	
			}
								
			// Something failed!
			// Let's mark this one as already verified, and move on.. 
			if ( type == 'phone' )
			{
				phoneVerified = true;
			}
			else if ( type == 'address' )
			{
				addressVerified = true;	
			}
			
			verifyObject = false;
			
			// Nothing changed. Let's just go to next verify step.
			doVerify();
		}
			
	});
}



/**
 * Checks for a short-circuit dupe.
 *
 * @author		dustin
 */
function checkDupe(programID)
{
	url = '/rfi_checkdupe.php?schoolCollectionID=' + schoolCollectionID + '&programID=' + programID;
	
	new Ajax.Request(url, {
		method: 'get',
		evalScripts: true
	});	
}


/**
 * When the country changes, we have to see if they want foreign fields.
 *
 * @author		dustin
 */
function countryChange(country)
{
	formText = $('rfiForm').serialize();
	
	url = '/rfi_foreignfields.php?schoolCollectionID=' + schoolCollectionID;
	
	new Ajax.Updater('foreignfields', url, {
		method: 'post',
		parameters: formText,
		evalScripts: true
	});	
}


/**
 * Makes a popup window with the information
 *
 * @author		dustin
 */
function popUp(programID)
{
	popupwin = window.open('/rfi_popup.php?programID=' + programID + '&schoolCollectionID=' + schoolCollectionID, 'popupwin', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=491,height=300');
	popupwin.focus();
}


/**
 * Confirms region for out-of-geo filtering.
 */
function confirmRegion()
{
	$('rfiForm').elements['data[regionConfirm]'].value='1';
}
