/**
 * @author Eduard
 */

 var FastContact = {};
 FastContact.sent = false;
 FastContact.submit = function() {
 	if (FastContact.sent) {
		return;
	}
 	if (!FastContact.validateForm()) {
		return;
	}
	var callback = {
		success: function(o) {
			document.body.style.cursor = 'auto';
			try {
				var res = YAHOO.lang.JSON.parse(o.responseText);
			}
			catch(e) {
				alert(e.message);
				return;
			}
			if (res.error == 1) {
				var fobj = document.getElementById("fast_contact_form");
				fobj.fast_contact_phone.disabled = true;
				fobj.fast_contact_name.disabled = true;
				fobj.fast_contact_email.disabled = true;
				alert("פניתך התקבלה. נחזור בהקדדם האפשרי. תודה.");
			}
			else if (res.message) {
				alert(res.message);
				return;
			}
			else {
				alert("SERVER ERROR");
			}
		},
		failure: function() {
			document.body.style.cursor = 'auto';
			alert("SERVER ERROR");
			return;
		}
	}
	YAHOO.util.Connect.setForm("fast_contact_form");
	YAHOO.util.Connect.asyncRequest("POST", FastContact.uri, callback);
	document.body.style.cursor = 'wait';
	FastContact.sent = true;
 }
 
 FastContact.validateForm = function() {
 	var fobj = document.getElementById("fast_contact_form");
	if (fobj.fast_contact_name.value == '') {
		alert("נא להזין שדה חובה - שם");
		fobj.fast_contact_name.focus();
		return false;
	}
	if (fobj.fast_contact_phone.value == '') {
		alert("נא להזין שדה חובה - טלפון");
		fobj.fast_contact_phone.focus();
		return false;
	}
	if (fobj.fast_contact_phone.value.match(/[^\d+-]/) != null) {
		alert("נא להזין מס` טלפון תקין");
		fobj.fast_contact_phone.focus();
		return false;
	}
	if (fobj.fast_contact_email.value == '') {
		alert("נא להזין שדה חובה - אימייל");
		fobj.fast_contact_email.focus();
		return false;
	}
	var ar = fobj.fast_contact_email.value.split('@');
    if (ar.length != 2) {
        alert(" כתובת אימייל לא תקין");
		fobj.fast_contact_email.focus();
		return false;
    }
    var p = /[^a-zA-Z0-9.!#$%&'*\/=?^_`{|}~+-]/; //allowed chars in local part
    if (ar[0].length == 0 || ar[1].length == 0 || 
		ar[0].match(/^\./)!=null ||	ar[0].match(/\.$/)!=null || 
		ar[0].match(/\.\./)!=null || ar[0].match(p) != null || 
		ar[1].match(/[^a-zA-Z0-9._-]/) != null) 
	{ 
		alert(" כתובת אימייל לא תקין");
		fobj.fast_contact_email.focus();
		return false;
	}
	return true;
 }

