/*
insertLead.js
Author: Sean Portle
Email: sportle@redoakgroup.com
Created: 8/16/2008
Last Modified: 11/11/2008
*/

// Global session timeout variables
var sessionTimeout;
var sessionLength = 5;

// Upon page load, set session timeout
var sessionTimeout = setTimeout("keepSessionAlive()", sessionLength * 60000); 

/*
Utility function to clear the session timeout 
*/
function clearSessionTimeout()
{
	clearTimeout(sessionTimeout);
}

/*
Utility function to start the session timeout
*/
function setSessionTimeout()
{
	sessionTimeout = setTimeout("keepSessionAlive()", sessionLength * 60000); 
}

/*
Calls a dummy function in order to keep the Coldfusion session alive over periods of
user inactivity.
*/
function keepSessionAlive()
{
	// Get the cfc object      
	var keepSessionAliveCFC = new CFCs.keepSessionAlive;
	
	// Set the cfc's callback function
	keepSessionAliveCFC.setCallbackHandler(keepSessionAliveCallback);
	
	// Set the cfc's error handler function
	keepSessionAliveCFC.setErrorHandler(keepSessionAliveErrorHandler);
	
	// Clear the session timeout, we know that the user is at the computer
	clearSessionTimeout();
	
	// Ping server to keep session alive
	keepSessionAliveCFC.keepAlive();
}

/*
Call back function for the keepSessionAliveCFC.keepAlive AJAX call in the keepSessionAlive function.
*/
function keepSessionAliveCallback()
{		 
	// Set session timeout to start when we get response from server
	setSessionTimeout(); 
}

/*
Error handler function for the keepSessionAliveCFC.keepAlive AJAX call in the keepSessionAlive function.
*/
function keepSessionAliveErrorHandler()
{		 
	// Call the keep session alive function again
	keepSessionAlive();
}

/*
This function trims the leading and trailing spaces off of an input string.
*/
function trim(sString)
{
	if(sString)
	{
		if(sString.substring)
		{
			while (sString.substring(0,1) == ' ')
			{
				sString = sString.substring(1, sString.length);
			}
			while (sString.substring(sString.length-1, sString.length) == ' ')
			{
				sString = sString.substring(0,sString.length-1);
			}
		}
	}
	else
	{
		sString = '';
	}
	return sString;
}

/*
Calls a dummy function in order to keep the Coldfusion session alive over periods of
user inactivity.
*/
function isValidZip()
{
	// Get the cfc object      
	var insertLeadCFC = new CFCs.insertLead;
	
	// Get the value of the zip on the form
	var zip = trim(document.getElementById('leadzip').value);
	
	// Get the element that shows the zip code error message
	var zipErrorElement = document.getElementById('leadziperror');
	
	// Verify that the zip code is US
	if(!zip.match(/^\d{5}$/))
	{
		zipErrorElement.style.display = "block";
		return;
	}
	else if(zip.length != 5)
	{
		zipErrorElement.style.display = "block";
		return;
	}
	else
	{
		zipErrorElement.style.display = "none";
	}
	
	// Set the cfc's callback function
	insertLeadCFC.setCallbackHandler(isValidZipCallback);
	
	// Set the cfc's error handler function
	insertLeadCFC.setErrorHandler(isValidZipErrorHandler);
	
	// Clear the session timeout, we know that the user is at the computer
	clearSessionTimeout();
	
	// Ping server to keep session alive
	insertLeadCFC.isValidZip(zip);
}

/*
Call back function for the insertLeadCFC.isValidZip AJAX call in the isValidZip function.
*/
function isValidZipCallback(response)
{		 
	// Set session timeout to start when we get response from server
	setSessionTimeout(); 
	
	// Get the element that shows the zip code error message
	var zipErrorElement = document.getElementById('leadziperror');
	
	// If the zip code is valid
	if(response.VALID)
	{
		zipErrorElement.style.display = "none";
	}
	// Else the zip is not valid
	else
	{
		zipErrorElement.style.display = "block";
	}
}

/*
Error handler function for the insertLeadCFC.isValidZip AJAX call in the isValidZip function.
*/
function isValidZipErrorHandler()
{		 
	// Call the zip validation function again
	isValidZip();
}
