var amail = {
	emailFilter: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
	urlFilter: /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/,
	phoneFilter: /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/,
	lang: "en",
	i18n: {
		en: {
			errorRequire: "This field is required!",
			errorEmail: "Email is not valid!",
			errorUrl: "Url is not valid!",
			errorPhone: "Phone number is not valid!",
			submitSending: "Sending ...",
			submitSent: "Email sent!",
			no: "no"
		},
		fr: {
			errorRequire: "Ce champ est requis!",
			errorEmail: "Cette adresse courriel est invalide!",
			errorUrl: "Ce lien hypertext est invalide!",
			errorPhone: "Ce numéro de téléphone est invalide!",
			submitSending: "En cours d'envois ...",
			submitSent: "Courriel envoyé!",
			no: "non"
		}
	},
	hasErrors: false,
	submitButton: null,
	form: null,
	add: function (a, lang) {
		if (typeof a == "string") {
			a = document.getElementById(a)
		}
		amail.lang = lang == 'fr' ? 'fr' : 'en';
		amail.form = a;
		$(a).submit(amail.send);
	},
	send: function (event) {

		event.preventDefault();
		
		var form = $(this);
		
		amail.form = this;
		
		amail.validate();
		if (!amail.hasErrors) {
			amail.appendLabels();
			if (amail.submitButton) {
				amail.submitButton.disabled = true;
				if (amail.submitButton.tagName == "BUTTON") {
					amail.submitButton.oldValue = amail.submitButton.innerHTML;
					amail.submitButton.innerHTML = amail.i18n[amail.lang].submitSending
				} else {
					amail.submitButton.oldValue = amail.submitButton.value;
					amail.submitButton.value = amail.i18n[amail.lang].submitSending
				}
			}
			amail.appendHidden("amail", 1);
			$.post(form.attr('action'), form.serialize(), amail.ready);
		}
	},
	ready: function (a) {
		if (a.msg != undefined) {
			var div = document.getElementById("amail-msg");
			if (div == undefined) {
				div = document.createElement("div");
				div.setAttribute("id", "amail-msg");
				div.style.backgroundColor = "#E8F6FB";
				div.style.border = "1px solid #2786C2";
				div.style.padding = "5px 10px";
				div.style.color = "#2786C2";
				div.style.fontWeight = "bold";
				div.style.fontSize = "14px";
				div.style.lineHeight = "18px";
				div.onclick = function () {
					this.style.display = "none"
				};
				amail.form.insertBefore(div, amail.form.firstChild)
			}
			div.innerHTML = a.msg;
			div.style.display = "block"
		}
		if (amail.submitButton) {
			if (amail.submitButton.tagName == "BUTTON") {
				amail.submitButton.innerHTML = amail.i18n[amail.lang].submitSent
			} else {
				amail.submitButton.value = amail.i18n[amail.lang].submitSent
			}
		}
	},
	validate: function () {
		var c, a, b;
		var d = amail.form.getElementsByTagName("LABEL");
		amail.hasErrors = false;
		for (b = 0; b < amail.form.elements.length; b++) {
			c = amail.form.elements[b];
			if (/require/.test(c.className) && c.value.replace(/^\s+|\s+$/g, "") == "") {
				amail.displayError(c, amail.i18n[amail.lang].errorRequire)
			} else {
				if (/email/.test(c.className) && !amail.emailFilter.test(c.value)) {
					amail.displayError(c, amail.i18n[amail.lang].errorEmail)
				} else {
					if (/url|website/.test(c.className) && !amail.urlFilter.test(c.value)) {
						amail.displayError(c, amail.i18n[amail.lang].errorUrl)
					} else {
						if (/phone/.test(c.className) && !amail.phoneFilter.test(c.value)) {
							amail.displayError(c, amail.i18n[amail.lang].errorPhone)
						}
					}
				}
			}
		}
	},
	appendLabels: function () {
		var c, a, b;
		var d = amail.form.getElementsByTagName("LABEL");
		for (b = 0; b < amail.form.elements.length; b++) {
			c = amail.form.elements[b];
			switch (c.tagName.toLowerCase()) {
			case "input":
				switch (c.type.toLowerCase()) {
				case "checkbox":
					if (!c.checked) {
						amail.appendHidden(c.name, amail.i18n[amail.lang].no)
					}
					break;
				case "submit":
					amail.submitButton = c
				}
				break;
			case "button":
				if (c.type.toLowerCase() == "submit") {
					amail.submitButton = c
				}
			}
		}
		for (b = 0; b < d.length; b++) {
			a = d[b];
			if (a.htmlFor != "") {
				if (e = document.getElementById(a.htmlFor)) {
					amail.appendHidden("label[" + e.name + "]", a.innerHTML)
				}
			}
		}
	},
	appendHidden: function (a, c) {
		var b = document.createElement("input");
		b.type = "hidden";
		b.name = a;
		b.value = c;
		amail.form.appendChild(b)
	},
	displayError: function (a, b) {
		var f = "amail-error-" + a.name;
		var d = document.getElementById(f);
		amail.hasErrors = true;
		if (!d) {
			d = document.createElement("div");
			d.setAttribute("id", f);
			var c = amail.findPos(a);
			d.innerHTML = '<div style="display:block;position:absolute;margin:0;padding:5px 8px;top:' + (c[1] - 22) + "px;left:" + (c[0] + 20) + 'px;height:10px;background:#333;color:#fff;font-family:Helvetica, Arial, sans-serif;font-size:10px !important;line-height:10px !important;text-transform:uppercase;font-style:normal;font-weight:normal;text-align:center;vertical-align:middle;-moz-opacity:.95;filter:alpha(opacity=95);opacity:.95;-moz-border-radius:3px;-webkit-border-radius:3px;text-shadow:#000 1px 1px 1px"><span></span><b style="position:absolute;width:0;height:0;border-bottom-width:0;background:none;margin-left:-5px;left:10px;bottom:-5px;right:auto;border-top:5px solid #333;border-left:5px solid transparent;border-right:5px solid transparent;"><b style="position:absolute;width:0;height:0;border-bottom-width:0;background:none;top:-5px;left:-3px;bottom: auto;border-top:3px solid #333;border-left:3px solid transparent;border-right:3px solid transparent;"></b></b></div>';
			document.body.appendChild(d);
			a.onfocus = function () {
				document.getElementById("amail-error-" + this.name).style.display = "none"
			}
		}
		d.getElementsByTagName("SPAN")[0].innerHTML = b;
		d.style.display = "block"
	},
	findPos: function (a) {
		var b = curtop = 0;
		if (a.offsetParent) {
			do {
				b += a.offsetLeft;
				curtop += a.offsetTop
			} while (a = a.offsetParent)
		}
		return [b, curtop]
	}
};
