(function(){
	var loader = new YAHOO.util.YUILoader();
	JSModules.registerWithYUILoader(loader);
	loader.loadOptional = true;
	loader.combine = true;
	loader.require(["connection","stringvalidation", "formvalidation","stwprogressdialog","stwalertdialog","json","stwyuidialog","button"]);
	loader.insert();

	loader.onSuccess = function()
	{
		//Create buttons
		YAHOO.util.Dom.addClass(document.body, "yui-skin-sam");
		Contact.initButtons();
	};
})();

var DEBUG = false;

var Contact = {

	wait: null,
	sendButton: null,

	resetForm:function()
	{
		var form = document.getElementById("contactform");
		if (Session.id && Session.id!="")
		{
			form.name.value=Session.User.getName();
			form.username.value=Session.User.getUserName();
			form.company.value=Session.User.getAccountName();
			form.email.value=Session.User.getEmail();
			form.phone.value=Session.User.getAccountInfo().phone;
		}
		else
		{
			form.name.value="";
			form.username.value="";
			form.company.value="";
			form.email.value="";
			form.phone.value="";
		}
		form.message.value="";
	},

	submitMessage:function()
	{
		var form = document.getElementById("contactform");

		var data = {
			name: form.name.value,
			username:form.username.value,
			company: form.company.value,
			phone: form.phone.value,
			name: form.name.value,
			email: form.email.value,
			subject:form.subject.value,
			message: form.message.value
		};
		if (this.validateForm(form))
		{
			//Display loading dialog
			Contact.wait = new STWProgressDialog("Your message is being sent...");
			Contact.wait.show();

			var url = "/aboutus/contact_server.php?responsetype=json&sid=";
			var post = "request="+YAHOO.lang.JSON.stringify(data);

			var callback =
			{
				success: function(o)
				{
					var response = YAHOO.lang.JSON.parse(o.responseText);
					if (!response.error)
					{

						Contact.handleResponse("Your Message Has Been Sent to:","Thank you for contacting us.<br/>We will get back to you shortly.<p><b><u>Text of message sent:</u></b><br>"+o.responseText);
					}
					else
					{
						var error = response.error.description;
						var code = error.code;
						Contact.handleResponse("Error","There was an error submitting your message:<br><br/>"+error+"<p>Please email support@lat49.com<br/>or call 604-656-6303 for support.");
					}
				},
				failure: function(o)
				{
					Contact.handleResponse("Error","There was an error submitting your message.<br/>Please email support@lat49.com<br/>or call 604-656-6303 for support.");				}
			};
			YAHOO.util.Connect.asyncRequest('POST', url, callback, post);

		}

	/* 2279 old legacy approach
				var request = new STWRequest(this.messageSuccess,this.messageFailure);
				request.createXMLRPCRequest("contact.sendMessage",[form.name.value,form.username.value,form.company.value,form.email.value,form.phone.value,form.subject.value,form.message.value]);
	*/
	},

	validateForm:function(form)
	{
		if (DEBUG){ alert('validation has been disabled because debugs are on'); return true; }
		var valid = FormValidation.name(form,"nameerror");
		valid = FormValidation.email(form,"emailerror") && valid;
		valid = FormValidation.generic("!StringValidation.isEmpty",form.message.value,"messageerror","Required") && valid;
		return valid;
	},

/* 2279
	messageSuccess:function(request,response)
	{
		Contact.handleResponse("Message Sent","Thank you for contacting us.<br/>We will get back to you shortly.");
	},

	messageFailure:function(request,response)
	{
		Contact.handleResponse("Error","There was an error submitting your message.<br/>Please email support@lat49.com<br/>or call 604-656-6303 for support.");
	},
*/
	handleResponse:function(title,msg)
	{
		if(Contact.wait)
			Contact.wait.hide();
		var dialog = new STWAlertDialog(title, msg);
		this.resetForm();
	},

	initButtons: function(){
		Contact.sendButton = new YAHOO.widget.Button("sendButton", {disabled:true});
	},

	enableSendButton:function()
	{
		if (DEBUG){ Contact.sendButton.set("disabled",false); return; }
		var contactform = document.getElementById("contactform");
		var filledF = (!StringValidation.isWhitespace(contactform.name.value) && !StringValidation.isWhitespace(contactform.email.value) && !StringValidation.isWhitespace(contactform.message.value) ? true : false);
		Contact.sendButton.set("disabled",!filledF);
		/*var disbtn = document.getElementById("nextbtndis");
		var enbtn = document.getElementById("nextbtnen");
		disbtn.style.display = !filledF ? "" : "none";
		enbtn.style.display = filledF ? "" : "none";*/
	},
	disableSendButton:function()
	{
		Contact.sendButton.set("disabled",true);
		/*(var disbtn = document.getElementById("nextbtndis");
		var enbtn = document.getElementById("nextbtnen");
		disbtn.style.display = "";
		enbtn.style.display = "none";*/
	}
}