/*
This code module was written by Joel Candelaria for FierceBlaze/NightsMedia for use on SpringfieldNights.Com.

It requires the Yahoo User Interface AJAX Library v2.2.2.
	<script src="http://yui.yahooapis.com/2.2.2/build/yahoo/yahoo-min.js"></script> 
	<script src="http://yui.yahooapis.com/2.2.2/build/connection/connection-min.js"></script>
*/

var handleSuccess = function(o){
	// Get the div tag we are loading the result to.
	var div = document.getElementById(o.argument.div);
	// If there is response text...
	if(o.responseText !== undefined){
		// Put it in the div tag.
		div.innerHTML = o.responseText;
		// Run any scripts in the response.
		runScripts(div);
	}
}
var handleFailure = function(o){
	// Get the div tag we are loading the result to.
	var div = document.getElementById(o.argument.div);
	// If there is response text...
	if(o.responseText !== undefined){
		// Put it in the div tag.
		div.innerHTML = "<strong>HTTP Error " + o.status + ":</strong><br><em>" + o.statusText + "</em>";
	}
}



var handleSuccessForm = function(o){
	// Get the div tag we are loading the result to.
	var div = document.getElementById(o.argument.div);
	// If there is response text...
	if(o.responseText !== undefined){
		// Put it in the div tag.
		div.innerHTML = o.responseText;
		// Run any scripts in the response.		
		runScripts(div);
	}
	// Reset the form state.
	YAHOO.util.Connect.resetFormState()
}
var handleFailureForm = function(o){
	// Get the div tag we are loading the result to.
	var div = document.getElementById(o.argument.div);
	// If there is response text...
	if(o.responseText !== undefined){
		// Put it in the div tag.
		div.innerHTML = "<strong>HTTP Error " + o.status + ":</strong><br><em>" + o.statusText + "</em>";
	}
	// Reset the form state.
	YAHOO.util.Connect.resetFormState()
}



// Run the scripts in the element after we write to it.
function runScripts(element)
{	
	// Get an array of all the script tags.
	var scriptTags = element.getElementsByTagName('SCRIPT');
	
	// For each script tag in the array...
	for( var count=0; count < scriptTags.length; count++ )
	{
		// Run the content of the tag.
		eval( scriptTags[count].innerHTML );
	}
}



function loadPage(pDiv, pURL)
{
	// Cache Breaker Fix.
	pURL = cacheBreakerFix(pURL);
	
	var callback =
	{
	  success:handleSuccess,
	  failure:handleFailure,
	  argument: { div:pDiv } 
	};
	  
	var request = YAHOO.util.Connect.asyncRequest('GET', pURL, callback); 
	
	var div = document.getElementById(pDiv);
	
	div.innerHTML = '<center><img src="image/loading.gif"></center>';
}



function submitForm(pForm, pDiv, pURL)
{
	// Cache Breaker Fix.
	pURL = cacheBreakerFix(pURL);
	
	var callback =
	{
	  success:handleSuccessForm,
	  failure:handleFailureForm,
	  argument: { div:pDiv } 	  
	};
		 
	YAHOO.util.Connect.setForm(pForm);
	 
	var request = YAHOO.util.Connect.asyncRequest('GET', pURL, callback);
	
	var div = document.getElementById(pDiv);
	
	div.innerHTML = '<center><img src="image/loading.gif"></center>';
}



// This function was added to keep the pages from being cached.
function cacheBreakerFix(pURL)
{
	// Create a new data object.
	// jsTs for those of you wondering means Javascript Timestamp
	var jsTs = new Date();
	// If the url doesnt have a ?...
	if(pURL.indexOf('?') == -1)
	{
		// Add one...
		pURL += '?';
	// Else if it does then...
	} else {
		// Add a &.
		pURL += '&';		
	}
	// Return the url with the random cache breaker field added to it.
	return pURL + 'jsTs=' + jsTs.getTime();	
}