/*
 * SimpleModal Basic Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2010 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: basic.js 254 2010-07-23 05:14:44Z emartin24 $
 */

jQuery(function ($) {
	// Load dialog on page load
	//$('#basic-modal-content').modal();

	// Load dialog on click
	$('.modal-form').click(function (e) {
		$('#basic-modal-content').modal();

		return false;
	});
	
	var contact = {
		message: null,
		
		init: function() {
			
			$.get("data/contact.php", function(data){
				$('.contact-cancel').after(data);
			});
			
			$('.contact-send').click(function (e) {
				e.preventDefault();
				// validate form
				if (contact.validate()) {
					$.ajax({
						url: 'assets/data/contact.php',
						data: $('#basic-modal-content form').serialize() + '&action=send',
						type: 'post',
						cache: false,
						dataType: 'html',
						success: function (data) {
							$('.success').text(data);
							$('.modal-content').fadeOut(300, function() {
								$('.success').fadeIn(100).delay(2000).fadeOut(100, function() {
									$.modal.close();
								});
							});
						},
						error: contact.error
					});
				}
				else {
					console.log(contact.message);
					contact.error();
				}
			});
		},
		
		error: function() {
			$('#simplemodal-container .error').text(contact.message);
		},
		
		validate: function () {
			contact.message = '';
			if (!$('.modal-content #contact-name').val() || $('.modal-content #contact-name').val()=="Name") {
				contact.message += 'Name is required. ';
			}
	
			var email = $('.modal-content #contact-email').val();
			if (!email || email == "Email") {
				contact.message += 'Email is required. ';
			}
			else {
				if (!contact.validateEmail(email)) {
					contact.message += 'Email is invalid. ';
				}
			}
	
			if (contact.message.length > 0) {
				return false;
			}
			else {
				return true;
			}
		},
		
		validateEmail: function (email) {
			var at = email.lastIndexOf("@");
	
			// Make sure the at (@) sybmol exists and  
			// it is not the first or last character
			if (at < 1 || (at + 1) === email.length)
				return false;
	
			// Make sure there aren't multiple periods together
			if (/(\.{2,})/.test(email))
				return false;
	
			// Break up the local and domain portions
			var local = email.substring(0, at);
			var domain = email.substring(at + 1);
	
			// Check lengths
			if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
				return false;
	
			// Make sure local and domain don't start with or end with a period
			if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
				return false;
	
			// Check for quoted-string addresses
			// Since almost anything is allowed in a quoted-string address,
			// we're just going to let them go through
			if (!/^"(.+)"$/.test(local)) {
				// It's a dot-string address...check for valid characters
				if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
					return false;
			}
	
			// Make sure domain contains only valid characters and at least one period
			if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
				return false;	
	
			return true;
		},
		showError: function () {
			$('#contact-container .contact-message')
				.html($('<div class="contact-error"></div>').append(contact.message))
				.fadeIn(200);
		}
	};
	
	contact.init();
});
