

// vars
var internalLink = false; // if set to true the action will be considered "not external"
//


$(document).ready(function() {

	$.fn.create_popup = function() {
		
		if(internalLink) {
			return;
		}
		
		var popup = $("div#popup");
		var message = popup.html();
		var new_url = popup.attr('data-redirect');
		//
		internalLink = true;
		location.assign(new_url);
		return message;
	};
	
	// <popup>
	$("div#popup").each(function() {
		// unless we see nopopup in the URL we will do a popup
		if (window.location.href.indexOf('nopopup') == -1) {
			window.onbeforeunload = $.fn.create_popup; //send_to_direct_order;
		}
	});
	// </popup>
	

	$("form").submit(function() {
		internalLink = true;
	});
	
	$("button").click(function(event) {
		internalLink = true;
	});
	
	$("a").click(function(event) {
		internalLink = true;
	});
	
	$("form").submit(function() {
		var validate = $(this).validationEngine('validate');

		if(this.beenSubmitted) { return false; }
		if(validate) {
			this.beenSubmitted = true;
			return true;
		}
		else {
			return false;
		}
	});

});

