﻿var rootURL;

$(document).ready(function () {

	$.validator.addMethod("textOnly",
									function (value, element) {
										var result = !/[0-9]+/.test(value);
										return result;
									},
									 "Alpha Characters Only."
			  );

	$.validator.addMethod(
					"validDate",
					function (value) {
						var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
						return RegExPattern.test(value);
					},
					"Please enter a valid date"
				);

	setTimeout("UpdateMessageCenter('Your session will be closed due to inactivity in 1 minute.');", 19 * 60 * 1000);
	setTimeout("location.href = '" + rootURL + "';", 20 * 60 * 1000);

	positionStatusDisplay();

	$(window).scroll(function () {
		$('#statusDisplay').css('top', $(this).scrollTop() + "px");
	});
	$(window).resize(function () {
		positionStatusDisplay();
	});

	$("#loading").hide();
	$('#fadeBack').hide();

	$('.toggleLink').click(function () {
		$(this).next().slideToggle();

		if ($(this).text() == 'more') {
			$(this).text('less');
		} else {
			$(this).text('more');
		}
		return false;
	});

	jQuery.fn.slideFadeToggle = function (speed, easing, callback) {
		return this.animate({ opacity: 'toggle', height: 'toggle' }, speed, easing, callback);
	};

	UpdateMessageCenter();
});

function positionStatusDisplay() {
	var newLeft = $(window).width() / 2 - 200;
	$('#statusDisplay').css('left', newLeft + "px");
}

function StartLoading() {
	var fade = $('#fadeBack');
	var loading = $('#loading');
	var w = $(document);

	fade.height(w.height());
	fade.width(w.width());
	loading.height(w.height());
	loading.width(w.width());

	$('#fadeBack').show();
	$("#loading").show();
};

function StopLoading() {
	$('#fadeBack').hide();
	$("#loading").hide();
};

function registerFormSubmit(form, container, errorMsg, successHandler) {
	form.submit(function () {
		StartLoading();
		var action = form.attr("action");
		var serializedForm = form.serialize();

		$.ajax({
			type: "POST",
			data: serializedForm,
			url: action,
			success: function (html) {
				StopLoading();
				if (container != null) {
					container.html("");
					container.html(html);
				}
				if (successHandler != null)
					successHandler();
			},
			error: function (msg) {
				StopLoading();
				UpdateMessageCenter(errorMsg);
			}
		});
		return false;
	});
}

function UpdateMessageCenter(msg) {
	var msgs = '';
	
	if (typeof msg == 'string') msgs += msg + '</br>';

	$('.displayMessage').each(
		function(s) {
			if (msgs.indexOf(this.value) == -1) {
				msgs += this.value + '<br />';
			}
		});
		if (msgs != '') {
			$("#statusDisplay").html("");
			$("#statusDisplay").html(msgs);
			$("#statusDisplay").slideDown();
			setTimeout("$('#statusDisplay').fadeOut()", 4000);
		}
		$('.displayMessage').remove();
}
