/**
 * AS4SomersetRequestForSupportWidget
 * proves form emailing functionality to the Somerset request for support form
 */

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_request_for_support_form").click(function()
	{
		if(!confirm("Are you sure you wish to send this form?"))
		{
			return false
		}

		AS4SomersetRequestForSupportWidget.getInstance().sendForm();
	});
});


var AS4SomersetRequestForSupportWidget = Class.create({


	emailTo: null,

	formData: null,

	sendForm: function()
	{
       var errFlag_checkbox = false;
       var errFlag = false;
       var errFlag_code= false;
       var alert_text= "";
       var code;
       
       jQuery("#send_request_for_support_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_confirm").attr("value"))
				{
                    errFlag_code=true;
                    alert("The security code entered does not match the required code. Please try again.");
					jQuery("#send_request_for_support_form").attr("value", "Send");
                    return false;
				}

                else
                    {
        

					//put the form data in to a nice array

					var strFormData = "";

					jQuery("#request_for_support_form select,#request_for_support_form input, #request_for_support_form textarea").each(function(i, ele)
					{
						
						
                        if (ele.type=="checkbox" )
                            {
                                if (!ele.checked)
                                    {
                                        strFormData += ""+ele.id+"="+"No"+":::"
                                    }
                                    else
                                        {
                                            strFormData += ""+ele.id+"="+"Yes"+":::"
                                        }
                            }
                            else
                            {
                               	//strFormValue = ele.value.replace(",", 'com;');
                               	strFormData += ""+ele.id+"="+ ele.value+":::"
                            }
                    strFormData = strFormData.replace(/&/, 'amp;');         
                    });

                    // Agree the policy

                    jQuery("input#agree").each(function(i,ele)
                    {
                        if(!ele.checked)
                            {
                                errFlag_checkbox=true;
                            }
                    });
                    
					//now check that all of the mandatory field have been completed

					jQuery("form#request_for_support_form input.mandatory, form#request_for_support_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_checkbox == true)
                        {
                            alert_text=alert_text+"Please tick the box to agree our policy. \n";
                        }
                    if(errFlag == true)
                        {
                            alert_text=alert_text+"Please complete all requested details. Missing fields have been highlighted. \n";
                        }
                    if(errFlag_code == true)
                        {
                            alert_text=alert_text+"The security code entered does not match the required code. Please try again. \n";
                        }
                    if(errFlag_checkbox == true||errFlag == true||errFlag_code == true)
                        {
                            alert(alert_text);
                            
                            jQuery("#send_request_for_support_form").attr("value", "Send");
                            
                            return false;
                        }
                    } 

                    // now send the form

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

	},

    onFormError: function()
	{

	},

	onFomSend: function()
	{

	}



});



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

	return AS4SomersetRequestForSupportWidget.instance;
}

