function emailDialog(lang) {	
	// Language variables
	var dialogTitle = (lang == "english") ? "Email a Friend" : "Dites-le &#xE0; un ami";
	var errorEmailMsg = (lang == "english") ? "Invalid email address" : "Addresse courriel invalide";
	var errorReqMsg = (lang == "english") ? "This field is required" : "Champ obligatoire";
	//Captcha Input
	var captchaMessageInput = "<input type=\"text\" id=\"captcha-response\" name=\"j_captcha_response\">";
	$("body").append("<div id=\"dialog\" title=\"" + dialogTitle + "\"></div>");
	$.ui.dialog.defaults.bgiframe = true;
	$(function() {
		$.ajax({
			url: "email-form",
			cache: false,
			success: function(html){
				
				$("#dialog").append(html);
				$('#emailReturnMsgSuccess').hide();
				$('#emailReturnMsgFail').hide();
				//Force Reload JCaptcha Image
				d = new Date();
			    $("#captcha").attr("src", "/gm/jcaptcha?"+d.getTime());
				$("#dialog").dialog({ 
					buttons: {
						"Send": function() { $("#EmailForm").submit(); },
						"Cancel": function() { $(this).dialog("close"); }
					},
					close: function(event, ui) {
						$("#dialog").remove(); // remove dom elements upon closing the dialog
					},
					modal:true, 
					width:700,
					zIndex:9999
				});
				if(lang == "french") { // 
					$('#dialog').dialog('option', 'buttons', { 
						"Envoyer": function() { $("#EmailForm").submit(); },
						"Annuler": function() { $(this).dialog("close"); }
					});	
				}
				$("#EmailForm").validate({
					messages: {
						from_name: { required: errorReqMsg },
						to_email_1: { email: errorEmailMsg, required: errorReqMsg },
						to_email_2: { email: errorEmailMsg },
						to_email_3: { email: errorEmailMsg },
						to_email_4: { email: errorEmailMsg }
					},
					submitHandler: function(html) {
						$.ajax({
							type: "GET",
							data: "from_name=" + $("#from_name").val() + "&j_captcha_response=" + $("#captcha-response").val() + "&to_email_1=" + $("#to_email_1").val() + "&to_email_2=" + $("#to_email_2").val() + "&to_email_3=" + $("#to_email_3").val() + "&to_email_4=" + $("#to_email_4").val() + "&url=" + location.href,
							url: "/gm/"+lang+"/sendemail",
							dataType: "html",
							success: function(data, textStatus) {
							//it could be other way to check return value
							if(data.length>10&&data.length<50)
							{
								//Force Reload JCaptcha Image
							   d = new Date();
							   $("#captcha").attr("src", "/gm/jcaptcha?"+d.getTime());
                               //Empty input value and other error message							   
							   $("#captchaMessage").html(captchaMessageInput);
							   //Add new error message
							   $("#captchaMessage").append(data).css("color", "red");
							}
								
							else{	
								$("#EmailForm").hide();
								$("#dialog").dialog('destroy');
								$('#emailReturnMsgSuccess').show();
								$("#dialog").dialog({
									buttons: {
										"OK": function() { $(this).dialog("close"); }
									},
									close: function(event, ui) {
										$("#dialog").remove(); // remove dom elements upon closing the dialog
									},
									modal:true,
									zIndex:1500000
								});}
							},
							error: function(XMLHttpRequest, textStatus, errorThrown) {
								$("#EmailForm").hide();
								$("#dialog").dialog('destroy');
								$('#emailReturnMsgFail').hide();
								$("#dialog").dialog({
									buttons: {
										"OK": function() { $(this).dialog("close"); }
									},
									close: function(event, ui) {
										$("#dialog").remove(); // remove dom elements upon closing the dialog
									},
									modal:true,
									zIndex:1500000
								});
							}
						});
					}
				});
			}
		}); 
	});
}

