﻿$(document).ready(function() {
	$("#dialog").jqm(
    {
    	modal: true
    });

	$("#btnLogin").click(function() {
		var email = $("#txtLoginEmail").val();
		var password = $("#txtLoginPassword").val();
		$.ajax({
			type: "POST",
			url: "Services/FormService.asmx/Authenticate",
			data: "{'email' : '" + email + "','password' : '" + password + "'}",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function(msg) {
				if (msg.d != "") {
					$("#dialog").jqmHide();
					window.location = window.location + "&uid=" + msg.d;
				}
				else {
					$("#loginFailed").text("Login failed");
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				var error = XMLHttpRequest;
			}

		});
	});
});


function CheckEmail(id) {
	var email = $("#" + id).val();
	if (email != "") {
		var regularExpression = new RegExp("(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)", "i");
		if (email.match(regularExpression)) {
			$.ajax({
				type: "POST",
				async: false,
				cache: false,
				url: "Services/FormService.asmx/IsUserExist",
				data: "{'email' : '" + email + "'}",
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				success: function(msg) {
					if (msg.d) {
						$("#txtLoginEmail").val($("#" + id).val());
						$("#dialog").jqmShow();
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					var error = XMLHttpRequest;
				}

			});
		}
	}
}