/**
 * AS4SomersetNQTVacanciesWidget
 * proves form emailing functionality to the Somerset NQT vacancies 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_nqt_vacancies_form").click(function()
	{
		if(!confirm("Are you sure you wish to send this form?"))
		{
			return false
		}
    AS4SomersetNQTVacanciesWidget.getInstance().sendForm();
		//if (AS4SomersetNQTVacanciesWidget.getInstance().checkForm())
           // document.nqt_vacancies_form.submit();
	});
    
});


var AS4SomersetNQTVacanciesWidget = Class.create({


	emailTo: null,

	formData: null,

	sendForm: function()
	{
       var errFlag = false;
		jQuery("#send_nqt_vacancies_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_confirm2").attr("value"))
				{
                    errFlag_code=true;
                    alert("The security code entered does not match the required code. Please try again.");
                    jQuery("#send_nqt_vacancies_form").attr("value", "Send");
                    return false;
				}

                else
                    {
       
					//put the form data in to a nice array

					var strFormData = "";

					jQuery("#nqt_vacancies_form input, #nqt_vacancies_form textarea").each(function(i, ele)
					{
                       strFormData += ""+ele.id+"="+ele.value+","
                    });

					//now check that all of the mandatory field have been completed

					jQuery("form#nqt_vacancies_form input.mandatory, form#nqt_vacancies_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 complete all requested details. Missing fields have been highlighted.");
                            jQuery("#send_nqt_vacancies_form").attr("value", "Send");
                            return false;
                        }
                    }  
                    jQuery.ajax(
					{
					type: "POST",
					url: "/channel/send_website_forum_message_form",
					data: "strFormData="+strFormData,
					success: function(msg)
					{jQuery("form#nqt_vacancies_form input, form#nqt_vacancies_form textarea").each(function(i, ele)
                        {
                            if (ele.type!="button")
                                {
                                    ele.disabled = "true";
                                }
                        });
      
                        jQuery("form#nqt_vacancies_form input[type='reset']").hide(1);
						jQuery("#send_nqt_vacancies_form").hide(1);
						jQuery("#sent_display").show();
					}
				});
            }
        })
	},
   
    onFormError: function()
	{

	},

	onFomSend: function()
	{

	}



});



// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Static method to retrieve controller from channel_widgets_id
AS4SomersetNQTVacanciesWidget.getInstance = function(channelWidgetsId)
{
	if(!AS4SomersetNQTVacanciesWidget.instance)
		AS4SomersetNQTVacanciesWidget.instance = new AS4SomersetNQTVacanciesWidget();

	return AS4SomersetNQTVacanciesWidget.instance;
}

