﻿$(function() {
var name = $("[name=name]"),
	from = $("[name=from]"),
	subject = $("[name=subject]"),
	message = $("[name=message]"),
	allFields = $([]).add(name).add(from).add(subject).add(message),
	tips = $(".validateTips");

tips.hide();
$("fieldset").show();

function checkLength(o,n,min,max) {
	if ( o.val().length > max || o.val().length < min ) {
		o.addClass('ui-state-error');
		return false;
	} else {
		return true;
	}
}

function checkRegexp(o,regexp,n) {
	if ( !( regexp.test( o.val() ) ) ) {
		o.addClass('ui-state-error');
		return false;
	} else {
		return true;
	}
}

$("#contactUs").dialog({
	autoOpen: false,
	height: 450,
	width: 600,
	modal: true,
	resizable : false,
	buttons: {
		'إلغاء': function() {
			$(this).dialog('close');
		},
		'إرسال': function() {
			var bValid = true;
			allFields.removeClass('ui-state-error');
		
			bValid = bValid && checkLength(name,"name",3,1000);
			bValid = bValid && checkLength(from,"from",3,1000);
			bValid = bValid && checkLength(subject,"subject",1,10);
			bValid = bValid && checkLength(message,"message",3,100000);

			if (bValid) {
				jQuery.post('../app/mail/contact.php',
					{
						name : $("[name=name]").val(),
						from : $("[name=from]").val(),
						subject : $("[name=subject]").val(),
						message : $("[name=message]").val()
					},
					function() {
						$("#contactUs table").hide();
						tips.html("لقد توصلنا برسالتك، نشكرك على المشاركة، ونرحب دائما بمشاركاتك ومقترحاتك وملاحظاتك.");
						tips.css("margin-top", "60px");
						tips.show();
						setTimeout(function() {$("#contactUs").dialog('close');}, 2000);
					}
				);
				
			} else {
				tips.text("المرجو ملء المعلومات الضرورية");
				tips.show();
			}
		}
	},
	open :function() {
		tips.hide();
		$("#contactUs table").show();
		$(".ui-button").trigger("mouseover");
		$(".ui-button").trigger("mouseleave");
	},
	close: function() {
		allFields.val('').removeClass('ui-state-error');
	}
});

$('.contactUsAction').click(function() {
	$('#contactUs').dialog('open');
});

});
