/**
 * AS4OCEAN2012JoinUsWidget
 * proves form emailing functionality to the OCEAN2012 jpin us 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);
	}
	
	// create a captcha graphic
	
	
	jQuery("#send_join_us_form").click(function()
	{
		if(!confirm("Are you sure you wish to send this form to OCEAN2012?"))
		{
			return false
		}
		
		AS4OCEAN2012JoinUsWidget.getInstance().sendForm();
	});
});


var AS4OCEAN2012JoinUsWidget = Class.create({
	
		
	emailTo: null,
	
	formData: null,
	
	sendForm: function()
	{
		
		var errFlag = false;
		
		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"))
				{
					alert("The security code entered does not match the required code. Please try again!");
					
					return false;
				}
				else
				{
					//put the form data in to a nice array
				
					var arrFormFields = new Array();
					var arrFormData = new Array();
					var strFormData = "";
					
					jQuery("#join_us_form input, #join_us_form textarea ").each(function(i, ele)
					{
						
						strFormData += ""+ele.id+"="+ele.value+"|:|";
					});
									
					strFormData = encodeURI(strFormData);					
					
					//because the above doesn't seem to work
					
					strFormData = strFormData.replace(/&/, 'amp;');
					
							
					//first check the checkboxes have been ticked
			
					jQuery("input#support, input#abide, input#cooperation_protocol, input#statement_of_purpose").each(function(i, ele)
					{
						if(!ele.checked)
						{
							alert("Please tick the confirmation boxes highlighted on the form.");
							
							jQuery("#"+ele.id).prev().css("color", "red");
							
							return false;
						}
					});
					
					//now check that all of the mandatory field have been completed
					
					jQuery("form#join_us_form input.mandatory, form#join_us_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 fill in all mandatory fields. Missing fields have been highlighted.");
						
						return false;
					}
				}
				
				//jsonFormData = JSON.sttringify
				
				
				
				// now send the form 
				
				jQuery.ajax(
				{
					type: "POST",
					url: "/channel/send_join_us_form",
					data: "strFormData="+strFormData,
					success: function(msg)
					{
						alert("Thank you for your OCEAN2012 application.");						
					}
				});
				
			}
		});
		
		
	},
	
	
	
	onFormError: function()
	{
		
	},
	
	onFomSend: function()
	{
		
	}


	
});



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

function utf8_encode(string)
{
	string = string.replace(/\r\n/g,"\n");

	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}
	
	return utftext;


}

function utf8_decode(utftext) 
{
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}

