﻿   
var LOGIN_IN_PROGRESS = false;

function submitLogin() {   
    var _request = new XMLHttpRequest();
    _request.open('POST', 'http://drupal.metalseconomics.com/meg/LoginDataHandler.php', true);
    _request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    
    var userName = document.getElementById("UserName");
    var userPwd = document.getElementById("UserPass");

    _request.onreadystatechange = function() {
        var _return = Response.processResponse(_request);
        if(_return != "") {
            handleLogin(_return);
        }
    }
    if(LOGIN_IN_PROGRESS == false) {
       LOGIN_IN_PROGRESS = true;
        _request.send('toDo=authenticateUser&userName='+ userName.value +
                '&password=' + userPwd.value);  
    }   
}


function handleLogin(resp) {

    if(resp != "") {
	alert(resp);
         LOGIN_IN_PROGRESS = false;
         var args = resp.split('|');
         if(args[0] == 'AUTH_SUCCESS') {
            //perform redirect
	        top.location=args[1];
         }
         else {
            //document.getElementById("UserLoginErrorDiv").innerHTML = "Your User ID or password are incorrect.";
            parent.mainFrame.location.href="login_error.html";
             var enterImage = document.getElementById("enterImage");
            var loadingImage = document.getElementById("loadingImage");
            enterImage.style.display = "inline";
            loadingImage.style.display = "none";
         }
     }
}
