
var userLoggedIn = false;

function loginPopup(email, password, successFunct)
{
	//alert(email);
	dcsMultiTrack('WT.si_n', 'SD Login', 'WT.si_x', '1');
	document.body.style.cursor = 'wait';

	new Ajax.Request("" + "/mdm/loginPopupW.do", {
		asynchronous: true,
		method: "get",
		parameters: "email=" + email + "&password=" + password,
		onSuccess: function(request)
		{
			//alert(request.responseText);
			var object = JSON.parse(request.responseText);
			//alert(request.responseText);
			if (object == null)
			{
				alert("An error occurred during login.");
				return false;
			}

			else if(object.nocomplete != null)
			{
				
				//document.getElementById("loginPopupFormEmail").value = email;
				//document.getElementById("loginPopupFormPassword").value = password;
				//document.getElementById("loginPopupForm").submit();
				  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() ;

				//document.location = "/mdmLogin/login.do?email=" + email + "&password=" + password;
			}
			else if (object.errors == null || object.errors.length == 0)
			{
				
				if (object.nocomplete != null)
				{
					//document.getElementById("loginPopupFormEmail").value = email;
					//document.getElementById("loginPopupFormPassword").value = password;
					//document.getElementById("loginPopupForm").submit();
					//return false;
					//document.location = "/mdmLogin/login.do?email=" + email + "&password=" + password;
				}
				else
				{
					

					userLoggedIn = true;
					
					if (document.getElementById("loginPopupRedirectURL").value != '')
					{
						
						document.location = document.getElementById("loginPopupRedirectURL").value;
					}
					else
					{
						if (document.getElementById("loginPopupRedirectJS").value != '')
							eval(document.getElementById("loginPopupRedirectJS").value);

						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;
}
