﻿$(function() {
var name = $("[name=name]"),
	from = $("[name=from]"),
	subject = $("[name=subject]"),
	city = $("[name=city]"),
	country = $("[name=country]"),
	message = $("[name=message]"),
	captcha = $("[name=captcha]"),
	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');
		
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			bValid = bValid && checkLength(name,"name",3,1000);
			bValid = bValid && checkRegexp(from, pattern);
			bValid = bValid && checkLength(subject,"subject",1,10);
			bValid = bValid && checkLength(message,"message",3,100000);

			if (bValid) {
				jQuery.post('../services/contact/contact.php',
					{
						name : name.val(),
						from : from.val(),
						subject : subject.val(),
						city : city.val(),
						country : country.val(),
						message : message.val(),
						captcha : captcha.val()
					},
					function(data) {
						if (data.match("خطأ في رمز الحماية")) {
							tips.text("خطأ في رمز الحماية");
							captcha.addClass('ui-state-error');
							tips.css("margin-top", "");
							tips.css("margin-bottom", "");
							tips.css("font-size", "");
							tips.show();
							document.getElementById('captchaimg').src = "../services/captcha/captcha.php?code=contact&c=" + new Date().getTime();
						} else {
							$("#contactUs table").hide();
							tips.html("لقد توصلنا برسالتك، نشكرك على المشاركة، ونرحب دائما بمشاركاتك ومقترحاتك وملاحظاتك.");
							tips.css("margin-top", "60px");
							tips.css("margin-bottom", "");
							tips.css("font-size", "");
							tips.show();
							setTimeout(function() {$("#contactUs").dialog('close');}, 2000);
						}
					}
				);
				
			} else {
				tips.text("المرجو ملء المعلومات الضرورية");
				tips.css("margin-top", "");
				tips.css("margin-bottom", "");
				tips.css("font-size", "");
				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(event) {
	document.getElementById('captchaimg').src = "../services/captcha/captcha.php?code=contact&c=" + new Date().getTime();
	event.preventDefault();
	$('#contactUs').dialog('open');
});

});
