﻿YAHOO.namespace("thefitpro.container");

function init() 
{
	// Define various event handlers for Dialog
	var handleSubmit = function()
	{

		// ensure validation is passed
		if (this.submit())
		{
			// render loading image into footer of dialog
			this.setFooter('<center><img src="/images/loadingBar.gif" /><br />Submitting your request...</center>');
		}
	};
	
	var handleCancel = function()
	{
		this.cancel();
	};
	
	var handleSuccess = function(o)
	{
		var response = o.responseText;
		response = response.split("<!")[0];

		// render response into footer of dialog
		o.argument[0].setFooter("<span style='color: #196AA6; font-weight: bold;'>" + response + "</span>");
	};
	
	var handleFailure = function(o)
	{
		alert("Submission failed: " + o.status);
	};
		

	// Instantiate the Dialogs
	YAHOO.thefitpro.container.dialogTestimonial = new YAHOO.widget.Dialog("dialogTestimonial", 
	{ 
	  width : "600px",
	  fixedcenter : true,
	  visible : false, 
	  draggable : false,
	  modal : true,
	  constraintoviewport : true,
	  hideaftersubmit : false,
	  buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
		      { text:"Cancel", handler:handleCancel } ]
	});
	

	YAHOO.thefitpro.container.dialogComment = new YAHOO.widget.Dialog("dialogComment", 
	{ 
	  width : "600px",
	  fixedcenter : true,
	  visible : false, 
	  draggable : false,
	  modal : true,
	  constraintoviewport : true,
	  hideaftersubmit : false,
	  buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
		      { text:"Cancel", handler:handleCancel } ]
	});
							
							
	YAHOO.thefitpro.container.dialogQuestion = new YAHOO.widget.Dialog("dialogQuestion", 
	{ 
	  width : "600px",
	  fixedcenter : true,
	  visible : false, 
	  draggable : false,
	  modal : true,
	  constraintoviewport : true,
	  hideaftersubmit : false,
	  buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
		      { text:"Cancel", handler:handleCancel } ]
	});
							
	YAHOO.thefitpro.container.dialogContact = new YAHOO.widget.Dialog("dialogContact", 
	{ 
	  width : "600px",
	  fixedcenter : true,
	  visible : false, 
	  draggable : false,
	  modal : true,
	  constraintoviewport : true,
	  hideaftersubmit : false,
	  buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
		      { text:"Cancel", handler:handleCancel } ]
	});
							
	YAHOO.thefitpro.container.dialogNetwork = new YAHOO.widget.Dialog("dialogNetwork", 
	{ 
	  width : "400px",
	  fixedcenter : true,
	  visible : false, 
	  draggable : false,
	  modal : true,
	  constraintoviewport : true,
	  hideaftersubmit : false,
	  buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
		      { text:"Cancel", handler:handleCancel } ]
	});																							
							

	// Validate the entries in the form
	YAHOO.thefitpro.container.dialogComment.validate = function() 
	{
		var data = this.getData();
		
		if (!validateFormTextField(data.txtUserName, 1, 50, "Name")) return false;
		if (!isEmail(data.txtUserEmail, 1, 50, "Email Address")) return false;
		if (!validateFormTextField(data.txtUserComment, 1, 500, "Comments")) return false;

		data.txtUserComment = RemoveBad(data.txtUserComment);		
		return true;
	};
	
	YAHOO.thefitpro.container.dialogQuestion.validate = function() 
	{
		var data = this.getData();
		
		if (!validateFormTextField(data.txtUserFirstName, 1, 50, "First Name")) return false;
		if (!isEmail(data.txtUserEmail)) return false;
		if (!validateFormTextField(data.txtUserQuestion, 1, 500, "Comments")) return false;

		data.txtUserQuestion = RemoveBad(data.txtUserQuestion);		
		return true;
	};
	
	YAHOO.thefitpro.container.dialogContact.validate = function() 
	{
		var data = this.getData();
		
		if (!validateFormTextField(data.txtUserFirstName, 1, 50, "First Name")) return false;
		if (!validateFormTextField(data.txtUserLastName, 1, 50, "Last Name")) return false;
		if (!isEmail(data.txtUserEmail)) return false;
		if (!validateFormTextField(data.txtUserMessage, 1, 500, "Message")) return false;

		data.txtUserMessage = RemoveBad(data.txtUserMessage);		
		return true;
	};
	
	YAHOO.thefitpro.container.dialogTestimonial.validate = function() 
	{
		var data = this.getData();
		
		if (!validateFormTextField(data.txtUserName, 1, 50, "Name")) return false;
		if (!validateFormTextField(data.txtUserTestimonial, 1, 500, "Testimonial")) return false;

		data.txtUserMessage = RemoveBad(data.txtUserTestimonial);		
		return true;
	};	

	// Wire up the success and failure handlers
	YAHOO.thefitpro.container.dialogComment.callback = { success: handleSuccess, failure: handleFailure, argument:[YAHOO.thefitpro.container.dialogComment] };
	YAHOO.thefitpro.container.dialogQuestion.callback = { success: handleSuccess, failure: handleFailure, argument:[YAHOO.thefitpro.container.dialogQuestion] };
	YAHOO.thefitpro.container.dialogContact.callback = { success: handleSuccess, failure: handleFailure, argument:[YAHOO.thefitpro.container.dialogContact] };
	YAHOO.thefitpro.container.dialogNetwork.callback = { success: handleSuccess, failure: handleFailure, argument:[YAHOO.thefitpro.container.dialogNetwork] };
	YAHOO.thefitpro.container.dialogTestimonial.callback = { success: handleSuccess, failure: handleFailure, argument:[YAHOO.thefitpro.container.dialogTestimonial] };
	
	// Render the Dialogs
	YAHOO.thefitpro.container.dialogComment.render();
	YAHOO.thefitpro.container.dialogQuestion.render();
	YAHOO.thefitpro.container.dialogContact.render();
	YAHOO.thefitpro.container.dialogNetwork.render();
	YAHOO.thefitpro.container.dialogTestimonial.render();

	YAHOO.util.Event.addListener("lnkComment", "click", YAHOO.thefitpro.container.dialogComment.show, YAHOO.thefitpro.container.dialogComment, true);
	YAHOO.util.Event.addListener("lnkQuestion", "click", YAHOO.thefitpro.container.dialogQuestion.show, YAHOO.thefitpro.container.dialogQuestion, true);
	YAHOO.util.Event.addListener("lnkContact", "click", YAHOO.thefitpro.container.dialogContact.show, YAHOO.thefitpro.container.dialogContact, true);
	YAHOO.util.Event.addListener("lnkNetwork", "click", YAHOO.thefitpro.container.dialogNetwork.show, YAHOO.thefitpro.container.dialogNetwork, true);
	YAHOO.util.Event.addListener("lnkTestimonial", "click", YAHOO.thefitpro.container.dialogTestimonial.show, YAHOO.thefitpro.container.dialogTestimonial, true);
}

YAHOO.util.Event.onDOMReady(init);