$(document).ready(function(){
	$('#trigger_contactForm').click(function(){
		var fm = $('#contactForm');
		var param = {};
		var isError = false;
		$.each($('input[required=true]', fm), function(){
			param[$(this).attr('name')] = $(this).val();
			
			if(isError == false)
			{
				if($(this).val() == '')
				{
					isError = true;
					errorObj = $(this);
				}
			}
			$(this).removeClass('required');
			
		});
		if(isError == false)
		{
			param[$('textarea', fm).attr('name')] = $('textarea', fm).val();
			if($('textarea', fm).val() == '')
			{
				isError = true;
				errorObj = $('textarea', fm);	
			}
		}
		
		
		
		if(isError == true)
		{
			alert(errorObj.attr('txt') + ' is required.');
			errorObj.addClass('required');
			return errorObj.focus();
		}

		if(validEmail($('input[name=email]', fm).val()) == false)
		{
			$('input[name=email]', fm).addClass('required');
			$('input[name=email]', fm).val('');
			return $('input[name=email]', fm).focus();
		}
		$.post("/Plg/contact/contact.php", param, function(res){
			if(res == '1')
			{
				alert('Message sent successfully!');
				location.href = location;
			}
			else
			{
				alert(res);
				$('#secure_code').val('');
				$('#secure_code').focus()
				$('#secure_img').attr('src', '/Plg/secure_code/securimage_show.php?sid='+Math.random());
			}
		});
	});
});
