var	errObject1 = null, errObject2 = null,
	submittedForm1 = false, submittedForm2 = false;

$(function(){
	// hide hot from honblue archive
	//$("#hfh_title").removeClass('show');
	//$("#hfh_archive").hide();

	// archive functionality
	$(".archive-title").bind("click", function() {
		if ($(this).hasClass('show')) {
			$(this).removeClass('show');
			$(this).next().slideUp();
		}
		else {
			$(this).addClass('show');
			$(this).next().slideDown();
		}
	});

	// display newsletters
	$("a.fancybox_iframe").fancybox({
		width: 818,
		height: $(window).height() - 100,
		type: 'iframe'
	});

	$("#submit_btn_1").removeAttr('disabled').removeClass('over sending');
	$("#subscribe_form1").bind("submit", function() {
		$(this).validate(form1_options);
		var valid = $(this).valid();
		if (valid && !submittedForm1) {
			$("#submit_btn_1").attr('disabled','disabled').addClass('sending').blur();
			submittedForm1 = true;
			/*
			// fail if process takes more than xx seconds
			var toTime = 15;
			errObject1 = setTimeout(function() {
				alert('Error: The form could not be submitted. Please try again.\nIf it still doesn\'t work after several tries, please contact us.');
				submittedForm1 = false;
				$("#submit_btn_1").removeAttr('disabled').removeClass('over sending');
				sendError(toTime+' second timeout reached', '#subscribe_form1');
				// check if Eloqua accessible
				var img = new Image();
				img.onerror = function() { sendError('Client cannot access Eloqua', '#subscribe_form1'); };
				img.src = 'http://now.eloqua.com/web/honblue/error-check.gif';
			}, toTime * 1000);
			*/
			return true;
		}
		return false;
	});

	$("#submit_btn_2").removeAttr('disabled').removeClass('over sending');
	$("#subscribe_form2").bind("submit", function() {
		$(this).validate(form2_options);
		var valid = $(this).valid();
		if (valid && !submittedForm2) {
			var ffname = $.trim($("#ffname").val());
			if (ffname) $("#friendname").text(ffname);
			$("#submit_btn_2").attr('disabled','disabled').addClass('sending').blur();
			submittedForm2 = true;
			/*
			// fail if process takes more than xx seconds
			var toTime = 15;
			errObject2 = setTimeout(function() {
				alert('Error: The form could not be submitted. Please try again.\nIf it still doesn\'t work after several tries, please contact us.');
				submittedForm2 = false;
				$("#submit_btn_2").removeAttr('disabled').removeClass('over sending');
				sendError(toTime+' second timeout reached', '#subscribe_form2');
				// check if Eloqua accessible
				var img = new Image();
				img.onerror = function() { sendError('Client cannot access Eloqua', '#subscribe_form2'); };
				img.src = 'http://now.eloqua.com/web/honblue/error-check.gif';
			}, toTime * 1000);
			*/
			return true;
		}
		return false;
	});
});

function form1_thankyou() {
	clearTimeout(errObject1);
	$("#subscribe_form1").slideUp('slow', function() {
		$("#form1_thankyoucontainer").slideDown('slow');
	});
};

function form2_thankyou() {
	clearTimeout(errObject2);
	$("#subscribe_form2").slideUp('slow', function() {
		$("#form2_thankyoucontainer").slideDown('slow');
	});
};

function sendError(errorMsg, form) {
	$.ajax({ url:'http://www.honblue.com/error.js', dataType:'script', data:{ message:errorMsg, url:escape(location.href), data:$(form).serializeArray() } });
};

$.validator.messages.required = '';
$.validator.messages.email = '';

var form1_options = {
	errorContainer: "div.form1_validationErrorMsg",
	rules: {
		'email': { required: true, email: true }
	}
};

var form2_options = {
	errorContainer: "div.form2_validationErrorMsg",
	rules: {
		'rfname': { required: true },
		'rlname': {	required: true },
		'remail': { required: true, email: true },
		'femail': { required: true, email: true }
	}
};
