/**
 * AS4SomersetWebForumMessageWidget
 * proves form emailing functionality to the Somerset website forum message forum
 */

jQuery.noConflict();

jQuery(document).ready(function()
{
	var charMap = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	var charsincaptcha = 7;
	
	var captcha = "";
	
	for(i=0;i<charsincaptcha;i++)
	{
		position = Math.floor(Math.random()*62)
		
		captcha += charMap.charAt(position);
	}
	

	jQuery("#send_website_forum_message_form").click(function()
	{
		if(!confirm("Are you sure you wish to send this form?"))
		{
			return false
		}

		AS4SomersetWebForumMessageWidget.getInstance().sendForm();
	});
});


var AS4SomersetWebForumMessageWidget = Class.create({


	emailTo: null,

	formData: null,

	sendForm: function()
	{

		var errFlag = false;

		jQuery("#send_website_forum_message_form").attr("value", "Sending");
		
        jQuery.ajax(
		{
			type: "POST",
			url: "/channel/get_captcha",
			data: "",
			success: function(msg)
			{
				var jsonT = eval('('+ msg +')');

				if(jsonT.capcha_code != jQuery("#captcha_confirm1").attr("value"))
				{
					alert("The security code entered does not match the required code. Please try again.");

					jQuery("#send_website_forum_message_form").attr("value", "Send");
					
					return false;
				}
                else
                    {

                        //put the form data in to a nice array

                        var strFormData = "";

                        jQuery("#website_forum_message_form input, #website_forum_message_form textarea ").each(function(i, ele)
                        {
                            strFormData += ""+ele.id+"="+ele.value+","
                        });


                        //now check that all of the mandatory field have been completed

                        jQuery("form#website_forum_message_form input.mandatory, form#website_forum_message_form textarea.mandatory").each(function(i, ele)
                        {
                            jQuery("#"+ele.id).css("background", "#FFFFFF");

                            if(ele.value == "")
                            {
                                errFlag = true;

                                jQuery("#"+ele.id).css("background", "#FFDFE0");
                            }
                        });

                        if(errFlag == true)
                        {
                            alert("Please supply requested details. Missing fields have been highlighted.");
							
                            jQuery("#send_website_forum_message_form").attr("value", "Send.");
                            
                            return false;
                        }
                    }
				// now send the form

				jQuery.ajax(
				{
					type: "POST",
					url: "/channel/send_website_forum_message_form",
					data: "strFormData="+strFormData,
					success: function(msg)
					{jQuery("form#website_forum_message_form input, form#website_forum_message_form textarea").each(function(i, ele)
                        {
                            if (ele.type!="button")
                                {
                                    ele.disabled = "true";
                                }
                        });
      
                        jQuery("form#website_forum_message_form input[type='reset']").hide(1);
						jQuery("#send_website_forum_message_form").hide(1);
						jQuery("#sent_display").show();
					}
				});
            }
        })

	},

	onFormError: function()
	{

	},

	onFomSend: function()
	{

	}



});



// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Static method to retrieve controller from channel_widgets_id
AS4SomersetWebForumMessageWidget.getInstance = function(channelWidgetsId)
{
	if(!AS4SomersetWebForumMessageWidget.instance)
		AS4SomersetWebForumMessageWidget.instance = new AS4SomersetWebForumMessageWidget();

	return AS4SomersetWebForumMessageWidget.instance;
}

