
var userLoggedIn = false;

function loginPopup()
{
	email = document.getElementById('loginPopupFormEmail').value;
	password = document.getElementById('loginPopupFormPassword').value;

	dcsMultiTrack('WT.si_n', 'DAVLogin', 'WT.si_x', '1');
	document.body.style.cursor = 'wait';
	new Ajax.Request("" + "/mdm/loginPopup.do", {
		asynchronous: true,
		method: "get",
		parameters: "email=" + email + "&password=" + password + "&fromOverlay=true",
		onSuccess: function(request)
		{
			var object = JSON.parse(request.responseText);
			if (object == null)
			{
				alert("An error occurred during login.");
				return false;
			}

			if (object.nocomplete != null)
			{
				//document.getElementById("loginPopupFormEmail").value = email;
				//document.getElementById("loginPopupFormPassword").value = password;
				//document.getElementById("loginPopupForm").submit();
				//return false;
				//document.location = "/mdm/login.do?email=" + email + "&password=" + password;
				var myForm = document.createElement("form");
        myForm.method="post" ;
        myForm.action = "/mdmLogin/login.do" ;
				var _email = document.createElement("input") ;
				_email.setAttribute("name", "email") ;
				_email.setAttribute("id","email");
				_email.setAttribute("value", email);
				var _password = document.createElement("input") ;
				_password.setAttribute("name", "password") ;
				_password.setAttribute("id","password");
				_password.setAttribute("value", password);
				myForm.appendChild(_email);
				myForm.appendChild(_password);
				document.body.appendChild(myForm) ;
				myForm.submit() ;
			}

			else if (object.errors == null || object.errors.length == 0)
			{
				//var firstName = object.firstName;

				userLoggedIn = true;
				if (document.getElementById("loginPopupRedirectURL").value != '')
				{
					window.open(document.getElementById("loginPopupRedirectURL").value, 'coupon');
					if (document.getElementById("registerDiv") != null)
					{
						document.getElementById("registerDiv").style.display = 'none';
						document.getElementById("logoffImg").style.display = 'block';
					}
					enableScreen();
					document.getElementById('loginPopupDiv').style.display='none';
				}

				else
				{
					if (document.getElementById("loginPopupRedirectJS").value != '')
						eval(document.getElementById("loginPopupRedirectJS").value);

					if (document.getElementById("registerDiv") != null)
					{
						document.getElementById("registerDiv").style.display = 'none';
						document.getElementById("logoffImg").style.display = 'block';
					}
					enableScreen();
					document.getElementById('loginPopupDiv').style.display='none';
				}
			}
			else
			{
				var errorList = "";
				for (i=0; i<object.errors.length; i++)
					errorList += object.errors[i] + "\n";

				alert(errorList);
				return false;
			}

		},
		onFailure: function(request)
		{
			alert(request.statusText);
			alert("We're currently experiencing technical problems on this website; please try again later. We apologize for any inconvenience.");
			return false;
		},
		onComplete: function(request)
		{
				document.body.style.cursor = 'default'
		}

	});

	return true;
}
