﻿var loginDialog = {
	speed: "normal",
	animate: false,
	animateLogin: true,
	open: function(dialog) {
		if (loginDialog.animate) {
			dialog.wrap.css({ overflow: 'hidden' });
			dialog.overlay.fadeIn(loginDialog.speed, function() {
				dialog.data.show();
				dialog.container.slideDown(loginDialog.speed, function() {
					dialog.wrap.css({ overflow: 'visible' });
					loginDialog.show(dialog);
				});
			});
		} else {
			dialog.overlay.show();
			dialog.data.show();
			dialog.wrap.css({ overflow: 'visible' });
			dialog.container.show();
			loginDialog.show(dialog);
		}
	},
	show: function(dialog) {
		$("input:first", this).focus();
	},
	close: function(dialog) {
		if (loginDialog.animate) {
			dialog.container.slideUp(loginDialog.speed, function() {
				dialog.data.hide();
				dialog.overlay.fadeOut(loginDialog.speed, function() {
					$.modal.close();
				});
			});
		} else {
			dialog.container.hide();
			dialog.data.hide();
			dialog.overlay.hide();
			$.modal.close();
		}
	}
};
$(function() {
	var $loginForm = $("#loginForm");
	$loginForm.unbind();
	var $loginFormModal = $loginForm.modal({
		appendTo: "form",
		focus: true,
		persist: true, // don't clone data
		containerCss: {
			borderWidth: '0',
			borderStyle: 'none',
			width: '400px',
			height: 'auto'
		},
		dataCss: {
			width: '376px'
		},
		close: false,
		escClose: false,
		overlayClose: false,
		onOpen: loginDialog.open,
		onClose: loginDialog.close
	});
	$("a.showLogin", $loginForm).unbind('click').click(function(e) {
		e.preventDefault();
		$(this).unbind('click').click(function(ev) { ev.preventDefault(); });
		if (loginDialog.animateLogin) {
			$("#userLoginForm", $loginForm).slideDown("normal", function() {
				$loginFormModal.dialog.container.height($loginFormModal.dialog.data.height());
				$loginFormModal.setPosition();
				$("input:first", this).focus();
			});
		} else {
			$("#userLoginForm", $loginForm).show();
			$loginFormModal.dialog.container.height($loginFormModal.dialog.data.height());
			$loginFormModal.setPosition();
			$("input:first", this).focus();
		}
	});
});
function loginCompleted(loginResult) {
	if (loginResult.success) {
		$("#userLoginForm input:text[id$=loginEmailBox]").val("");
		$("#userLoginForm input:password[id$=loginPasswordBox]").val("");
		$("#loginForm span[id$=guestITList] input:radio").each(function() { $(this).attr("checked", false); });
		$("#loginForm span[id$=guestCountryList] input:radio").each(function() { $(this).attr("checked", false); });
		$.modal.close();

		if (typeof (Sys.WebForms) !== 'undefined') {
			var prm = Sys.WebForms.PageRequestManager.getInstance();
			var panelId = "loginFormUpdatePanel";
			for (var i = 0; i < prm._updatePanelIDs.length; i++) {
				if (prm._updatePanelIDs[i].match(panelId + "$")) {
					panelId = prm._updatePanelIDs[i];
					break;
				}
			}
			__doPostBack(panelId, "");
		} else {
			__doPostBack("", "");
		}
	} else {
		alert("Sorry, the email or password was incorrect.");
	}
}
function loginFailed(request, textStatus, error) {
	alert("Oops, there was an error contacting the server. Please reload the page and try again.");
	alert("error:message = " + error + "\nerror:statusCode = " + textStatus);
}
function loginClick() {
	var email = $("input:text[id$=loginEmailBox]").val();
	var password = $("input:password[id$=loginPasswordBox]").val();
	var params = '{"email":"' + email + '","password":"' + password + '"}';
	$.ajax({
		type: "POST",
		url: basePath + "/Login.aspx/LoginUser",
		data: params,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(result) {
			// ASP.NET 2.0
			if (result.d === undefined) {
				loginCompleted(result);
			}
			// ASP.NET 3.5
			else {
				loginCompleted(result.d);
			}
		},
		error: loginFailed
	});
	return false;
}
function guestLoginClick() {
	var institutionType = $("span[id$=guestITList] input:checked").val();
	var country = $("span[id$=guestCountryList] input:checked").val();
	var params = '{"institutionType":' + institutionType + ',"country":' + country +'}';
	$.ajax({
		type: "POST",
		url: basePath + "/Login.aspx/LoginGuest",
		data: params,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(result) {
			// ASP.NET 2.0
			if (result.d === undefined) {
				loginCompleted(result);
			}
			// ASP.NET 3.5
			else {
				loginCompleted(result.d);
			}
		},
		error: loginFailed
	});
	return false;
}
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

