$(document).ready(function(){
	// Set up the cookies and interstitial:
	if( undefined == $.cookie('views') && undefined == $.cookie('accepted_privacy_policy') ){
		// Set the cookie:
		$.cookie('views', '1', { expires: 3, path: '/', domain: 'indianaintern.net' });
	} else{
		if(parseInt($.cookie('views')) < 3){
			// Increment the cookie:
			$.cookie('views', (parseInt($.cookie('views')) + 1), { expires: 3, path: '/', domain: 'indianaintern.net' });
		}
	
		// Determine if we should show the interstitial:
		if(parseInt($.cookie('views')) === 3 && undefined == $.cookie('action')){
			$.cookie('views', 1, { expires: 14, path: '/', domain: 'indianaintern.net' });
			//This will default the cookie action  to "shown" and the interstitial will not be shown again for 14 days
			//This implies the user exited the form in some way other than a standard option (such as refreshing the page or visiting another page)
			$.cookie('action', 'shown', { expires: 30, path: '/', domain: 'indianaintern.net' });
			// Show the interstitial:
			var interstitial = $('#feedback-interstitial').dialog({
				dialogClass: "module",
				draggable: false,
				modal: true,
				resizable: false,
				width: '550',
				open: function() {
					try {
						pageTracker._trackEvent("Interstitial", "Show");
					} catch(err) {}
					// If user does something other than click outside, click close, or click no thanks (ie. submit feedback), show again in 30 days
					$.cookie('action', 'viewed', { expires: 30, path: '/', domain: 'indianaintern.net' });
					$('.ui-widget-overlay').click(function() {
						// If clicked outside interstitial, show again in 14 days
						$(interstitial).dialog("close");
						$.cookie('action', 'clickbehind', { expires: 30, path: '/', domain: 'indianaintern.net' });
						try {
							pageTracker._trackEvent("Feedback Interstitial", "Click Behind");
						} catch(err) {}
						return false;
					});
					// if no thanks is clicked, show again in 180 days
					$(".module .close_modal").click(function() {
						$(interstitial).dialog("close");
						$.cookie('action', 'nothanks', { expires: 30, path: '/', domain: 'indianaintern.net' });
						try {
							pageTracker._trackEvent("Feedback Interstitial", "No Thanks");
						} catch(err) {}
						return false;
					});
					// if close is clicked, show again in 14 days
					$(".module .ui-dialog-titlebar-close").click(function() {
						$(interstitial).dialog("close");
						$.cookie('action', 'close', { expires: 30, path: '/', domain: 'indianaintern.net' });
						try {
							pageTracker._trackEvent("Feedback Interstitial", "Close");
						} catch(err) {}
						return false;
					});
				}
			
			});
		}
	} 
	
	$('#ajax_feedback_form').submit(function() {
		// reset
		$('#feedback-interstitial .details').html('<h3>The following fields must be corrected before your message is sent:</h3>');
		
		var errors = false;
		
		var emailAddress = $("#ajax_feedback_form #email-address").val();
		var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		var validEmail = emailAddress.match(re);
	    if (emailAddress == "") {
	  		$('#feedback-interstitial .details').append('<div class="validation-error-detail">The Email address field is required.</div>');
			$('#email-address-label').addClass('error');
	  		errors = true;
		} else if (validEmail == null) {
			$('#feedback-interstitial .details').append('<div class="validation-error-detail">The Email address is in an incorrect format.</div>');
			$('#email-address-label').addClass('error');
	  		errors = true;
		}
		
		var message = $("#ajax_feedback_form #message").val();
		if (message == "") {
	  		$('#feedback-interstitial .details').append('<div class="validation-error-detail">The Message field is required.</div>');
			$('#ajax_feedback_form #message-label').addClass('error');
	  		errors = true;
		}
		
		// var image_verification = $("#ajax_feedback_form #image_verification").val();
		// if (image_verification == "") {
		// 	  		$('#feedback-interstitial .details').append('<div class="validation-error-detail">The Image Verification field is required.</div>');
		// 	$('#ajax_feedback_form #image_verification-label').addClass('error');
		// 	  		errors = true;
		// }

		var verification = $("#ajax_feedback_form #verification").val();
		if (verification == "") {
	  		$('#feedback-interstitial .details').append('<div class="validation-error-detail">The Verification field is required.</div>');
			$('#ajax_feedback_form #verification-label').addClass('error');
	  		errors = true;
		}
		
		if (errors) {
			$('#feedback-interstitial .message').show();
			return false;
		} else {
			$('#feedback-interstitial .message').hide();
		}
		
		var mode      = $("#ajax_feedback_form #mode").val();
		var isAjax    = $("#ajax_feedback_form #ajax").val();
		var page      = $("#ajax_feedback_form #page").val();

		var dataString = 'email-address=' + emailAddress + '&message='+ message + '&page=' + page + '&mode=' + mode + '&ajax=' + isAjax + '&verification=' + verification;
	    $.ajax({  
			type: "POST",  
			url: "/contact",  
			data: dataString,  
			success: function(html) {
				if (html == 'captcha_error') {
					$('#feedback-interstitial .details').html('<h3>The following fields must be corrected before your message is sent:</h3>');
					$('#feedback-interstitial .details').append('<div class="validation-error-detail">The Image Verification field must match the image.</div>');
					$('#feedback-interstitial .message').show();
				} else {
					$('#feedback-interstitial .feedback_content').fadeOut(500, function(){
						$('.ajax_feedback_success').fadeIn(500);
					});
				}
			}
	    });
		return false;
	});
	
});
