

function loadContentPage(pageName) {

	var content = document.getElementById("content");
	
	content.innerHTML = "";

	if (!YAHOO.BTL.container.wait) {

		// Initialize the temporary Panel to display while waiting for external content to load

		YAHOO.BTL.container.wait = 
				new YAHOO.widget.Panel("wait",  
												{ width: "240px", 
												  fixedcenter: true, 
												  close: false, 
												  draggable: false, 
												  modal: true,
												  visible: false
												} 
											);

		YAHOO.BTL.container.wait.setHeader("Loading, please wait...");
		YAHOO.BTL.container.wait.setBody("<img src='images/rel_interstitial_loading.gif' />");
		YAHOO.BTL.container.wait.render(document.body);

	}

	// Define the callback object for Connection Manager that will set the body of our content area when the content has loaded
	var callback = {
		success : function(o) {
			content.innerHTML = o.responseText; //.replace("<!--","").replace("-->","");
			content.style.visibility = "visible";
			YAHOO.BTL.container.wait.hide();
			MAINCONTENT = pageName;	
			onMainContentChange.fire({name: pageName});								
		},
		failure : function(o) {
			content.innerHTML = o.responseText;
			content.style.visibility = "visible";
			content.innerHTML = "<p><b>Connection Error haiku:</b></p><p>You step in the stream,<br>but the water has moved on.<br>This page is not found.";
			YAHOO.BTL.container.wait.hide();
		}
	}
	  
	// Show the Panel
	YAHOO.BTL.container.wait.show();
	
	// Connect to our data source and load the data
	var conn = YAHOO.util.Connect.asyncRequest("GET", "services/content.cfm?content=" + pageName + "&r=" + new Date().getTime(), callback);


}



function procResponseText(myText,tag) {
	
	var innerResponse ="";
	
	if(myText.indexOf("<"+tag+">")>-1 && myText.indexOf("</"+tag+">")>-1) {
		innerResponse = myText.substring(myText.indexOf("<"+tag+">")+(tag.length)+2,myText.indexOf("</"+tag+">"));
	} else {
		innerResponse="";
	}	
	
	return innerResponse;
}	


function serverTalk(talkTo, call, param, responseFunction) {
	
	// Define the callback object for Connection Manager that will set the body of our content area when the content has loaded
	var callbackObj = {
		success : function(o) {
			myText = procResponseText(o.responseText,"response")
			if( myText != "" ) {
				responseFunction( myText );
			} else {
				responseFunction("no response");
			}
	
		},
		failure : function(o) {
			responseFunction(o.responseText);
		}
	}

	// Connect to our data source and load the data
	var conn = YAHOO.util.Connect.asyncRequest("GET", "services/" + talkTo + ".cfm?call=" + call + "&param=" + param + "&r=" + new Date().getTime(), callbackObj);		
}


function doLinkClick(pageToOpen) {	
	//This call will trigger the History state change handler which does UI update
	try { 
		  YAHOO.util.History.navigate( "app", pageToOpen ); 
	} catch ( e ) { 
		loadContentPage(pageToOpen);
	} 			
}






function validateSignupForm(theForm) {
	
	var allGood = true;
	
	if(theForm.emailAddress.value == "")  { 
		YAHOO.util.Dom.addClass('emailaddress','textfieldRequiredState');			
		allGood = false;
	}				
	else
		YAHOO.util.Dom.removeClass('emailaddress','textfieldRequiredState');
		
	if(theForm.username.value == "")  { 
		YAHOO.util.Dom.addClass('usernamearea','textfieldRequiredState');			
		allGood = false;
	}				
	else
		YAHOO.util.Dom.removeClass('usernamearea','textfieldRequiredState');
		
	if(theForm.dob_day.value == "" || theForm.dob_month.value == "" || theForm.dob_year.value == "")  { 
		YAHOO.util.Dom.addClass('dateofbirth','selectRequiredState');			
		allGood = false;
	}				
	else
		YAHOO.util.Dom.removeClass('dateofbirth','selectRequiredState');
	
	if(theForm.firstname.value == "")  { 
		YAHOO.util.Dom.addClass('firstname','textfieldRequiredState');			
		allGood = false;
	}				
	else
		YAHOO.util.Dom.removeClass('firstname','textfieldRequiredState');
	
	if(theForm.lastname.value == "")  { 
		YAHOO.util.Dom.addClass('lastname','textfieldRequiredState');			
		allGood = false;
	}				
	else
		YAHOO.util.Dom.removeClass('lastname','textfieldRequiredState');
		
	return allGood;
}







function signupFormSubmit() {

	var div = document.getElementById('btlcontent_copy_signupSuccess');
	var formObject = document.getElementById('formsignup');
	
	if (validateSignupForm(formObject)) 
	{
			var handleSuccess = function(o){
				if(o.responseText !== undefined){
					
					var mytext = procResponseText(o.responseText,"response");
					if(mytext != "" && mytext != "true") {				
						//if there was an issue, tell the user					
						div.innerHTML = mytext;
					} 
					else {
						//once the user has been created clear the form	
						formObject.reset();
					}
					//for testing div.innerHTML = o.responseText;
					YAHOO.BTL.container.panelSignupSuccess.show();				
				}
			}
			
			var handleFailure = function(o){
				if(o.responseText !== undefined){
					alert("There was an error while trying to sign you up. Please send us an email (our address is at the bottom of the page) so we can fix it!")				
				}
			}
			
			var callback =
			{
			  success:handleSuccess,
			  failure: handleFailure
			};
	
			
			YAHOO.util.Connect.setForm(formObject);
			var cObj = YAHOO.util.Connect.asyncRequest('GET', 'services/user.cfm?call=add&param=form' + "&r=" + new Date().getTime(), callback);
	}
	
	
}


function validateContactForm(theForm) {
	
	var allGood = true;
	
	if(theForm.contactEmail.value == "")  { 
		YAHOO.util.Dom.addClass('contactEmailArea','textfieldRequiredState');			
		allGood = false;
	}				
	else
		YAHOO.util.Dom.removeClass('contactEmailArea','textfieldRequiredState');
	
	if(theForm.contactName.value == "")  { 
		YAHOO.util.Dom.addClass('contactNameArea','textfieldRequiredState');			
		allGood = false;
	}				
	else
		YAHOO.util.Dom.removeClass('contactNameArea','textfieldRequiredState');
	
	if(theForm.contactMessage.value == "")  { 
		YAHOO.util.Dom.addClass('contactMessageArea','textfieldRequiredState');			
		allGood = false;
	}				
	else
		YAHOO.util.Dom.removeClass('contactMessageArea','textfieldRequiredState');
		
	return allGood;
}



function contactFormSubmit() {

	var div = document.getElementById('contactFormServerResponse');
	var formObject = document.getElementById('contactForm');
	
	if (validateContactForm(formObject)) 
	{

			var handleSuccess = function(o){
				if(o.responseText !== undefined){
					
					var mytext = procResponseText(o.responseText,"response");
					if(mytext != "" && mytext != "true") {				
						//if there was an issue, tell the user					
						alert(mytext);
						formObject.reset();
					} 
					else {
						//once the user has been created clear the form	
						div.innerHTML = o.responseText;
						
					}
			
				}
			}
			
			var handleFailure = function(o){
				if(o.responseText !== undefined){
					alert("There was an error while trying to submit the form. Please send us an email (our address is at the bottom of the page) so we can fix it!")				
				}
			}
			
			var callback =
			{
			  success:handleSuccess,
			  failure: handleFailure
			};	
			
			YAHOO.util.Connect.setForm(formObject);
			var cObj = YAHOO.util.Connect.asyncRequest('GET', 'services/sendEmailContact.cfm?call=add&param=form' + "&r=" + new Date().getTime(), callback);
	}	
}

function validateChatForm(theForm) {
	
	var allGood = true;
	
	if(theForm.chatEmail.value == "")  { 
		YAHOO.util.Dom.addClass('chatEmailArea','textfieldRequiredState');			
		allGood = false;
	}				
	else
		YAHOO.util.Dom.removeClass('chatEmailArea','textfieldRequiredState');
	
	if(theForm.chatUsername.value == "")  { 
		YAHOO.util.Dom.addClass('chatUsernameArea','textfieldRequiredState');			
		allGood = false;
	}				
	else
		YAHOO.util.Dom.removeClass('chatUsernameArea','textfieldRequiredState');
		
	return allGood;
}


function chatFormSubmit() {

	var formObject = document.getElementById('chatForm');
	
	if (validateChatForm(formObject)) 
	{
			var handleSuccess = function(o){
				if(o.responseText !== undefined){
					
					var mytext = procResponseText(o.responseText,"response");
					if(mytext != "" && mytext != "true") {				
						//if there was an issue, tell the user					
						alert(mytext);
						formObject.reset();
					} 
					else {
						alert(o.responseText);						
					}			
				}
			}
			
			var handleFailure = function(o){
				if(o.responseText !== undefined){
					alert("There was an error while trying to submit the form. Please send us an email (our address is at the bottom of the page) so we can fix it!")				
				}
			}
			
			var callback =
			{
			  success:handleSuccess,
			  failure: handleFailure
			};	
			
			YAHOO.util.Connect.setForm(formObject);
			var cObj = YAHOO.util.Connect.asyncRequest('GET', 'services/sendEmailChat.cfm?call=add&param=form' + "&r=" + new Date().getTime(), callback);
	}	
}
	


function initHistoryTracker() {

	var myBookmarkedState = YAHOO.util.History.getBookmarkedState( "app" );		
	//if there is no bookmarked state, assign the default state:
	var myHistoryInitialState = myBookmarkedState || MAINCONTENT;
	
	function myHistoryStateChangeHandler( state ) { 
		loadContentPage(state);
	} 
	
	YAHOO.util.History.register( "app", myHistoryInitialState, myHistoryStateChangeHandler );
	
	YAHOO.util.History.onLoadEvent.subscribe( function() {
		var myHistoryCurrentState = YAHOO.util.History.getCurrentState( "app" );
		loadContentPage(myHistoryCurrentState);
	} );

}


function initLinks() {
	
	var ids = ["link_home","link_authorchat","link_bits","link_comp","link_newbies","link_contact"];
	
	function doNavClick(e) {

		//remove the link_ prefix from the element id so we're
		//just passing the page name
		var pageName = this.id.split("_");
		doLinkClick(pageName[1]);					 
	}  
	
	YAHOO.util.Event.addListener(ids, "click", doNavClick);
}

