$(document).ready(function() {
	var dialogHtml = '<div class="validateTips"></div><br/><table width="437" style="text-align:left"><tr><td><label>Your Name*</label></td><td><input type="text" size="35" id="emailName" tabindex="1"/></td></tr><tr><td><label>Return Email*</label></td><td><input type="text" size="35" id="emailReturnEmail" tabindex="2"/></td></tr><tr><td><label>Subject</label></td><td><input type="text" size="35" id="emailSubject" tabindex="3"/></td></tr><tr><td colspan="2"><textarea id="emailMessage" style="width:400px" rows="6" tabindex="4"></textarea><br/><br/></td></tr><tr><td style="font-weight:normal">* Required fields</td><td align="right"><input type="image" value="Send Message" id="sendSiteButton" src="images/contact-form-send-btn.png" style="margin-right:30px"/></td></tr></table>';
	
	var $dialog = $('<div class="popupForm"></div>')
		.html(dialogHtml)
		.dialog({
			autoOpen: false,
			title: '',
			height: 350,
			width: 460,
			modal: true
		});
		
	var sendName = $("#emailName"), 
	sendEmail = $("#emailReturnEmail"),
	subject = $("#emailSubject"),
	message = $("#emailMessage"), 
	
	allFields = $([]).add(sendName).add(sendEmail).add(subject).add(message),
	tips = $(".validateTips");
	
		
	function checkLength( o, n, min, max )
	{
		if ( o.val().length > max || o.val().length < min )
		{
			o.addClass( "ui-state-error" );
			updateTips( "Length of " + n + " must be between " +
				min + " and " + max + "." );
			return false;
		}
		else return true;
	}

	function checkRegexp( o, regexp, n )
	{
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass( "ui-state-error" );
			updateTips( n );
			return false;
		}
		else return true;
	}
	
	function updateTips(t)
	{
		tips
			.text( t )
			.addClass( "ui-state-highlight" );
		setTimeout(function() {
			tips.removeClass( "ui-state-highlight", 1500 );
		}, 500 );
	}

	$('.opener').click(function() {
		$dialog.dialog('open');
		return false;
	});
	
	$('#sendSiteButton').click(function() {
		var bValid = true;
		allFields.removeClass("ui-state-error");
		bValid = bValid && checkLength(sendName, "Your Name", 3, 16 );
		
		
		bValid = bValid && checkRegexp(sendName, /^[a-z]([a-z\ ])+$/i, "Name may consist of a-z and spaces" );
		
		
		bValid = bValid && checkRegexp(sendEmail, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Your email address is not valid. eg. username@example.com");
		
		
		
		bValid = bValid && checkLength(message, "your Personal Message", 3, 300 );
		

		if ( bValid ) {
								
			$.post('/ajax/main.php', {sender_name:$('#emailName').val(), sender_email:$('#emailReturnEmail').val(), subject:$('#emailSubject').val(), message:$('#emailMessage').val(), action:'send_contact_message'}, function(data) {
				//var theirName = $('#sendSiteFriendsName').val()
			  //$dialog.html('<h3>Thanks, your message has been sent to '+theirName+'</h3>');
			alert('Thanks, your message has been sent to Franc.')
			$dialog.html(dialogHtml);
			$dialog.dialog('close');
			});
		}
		
	});
});
