//
// This function forwards to a new page if login is successful.
// Otherwise, it shows an error message on the login lightbox
//
function showLoginLightboxResponse (t) { 
  var status = "";
  var jsonObj = eval("("+t.responseText+")");
  status = jsonObj.status;
  
  var statusMsgElement = document.getElementById("loginlightbox_status_msg");
  Element.show(statusMsgElement);
  
  if (status == "success") {
     document.getElementById('lightboxLoading').style.visibility = 'visible';
     statusMsgElement.innerHTML = " Loading...";
     window.location = jsonObj.forward;
  }
  else {
     statusMsgElement.innerHTML = jsonObj.message;
  }
  
  Windows.focusedWindow.updateHeight();  
}

//
// This function opens a popup window and reloads the current page
// if login is successful. Otherwise, it shows an error message on 
// the login lightbox
//
function showLoginLightboxResponseWithPopup (t) {
  var status = "";
  var jsonObj = eval("("+t.responseText+")");
  status = jsonObj.status;

  var statusMsgElement = document.getElementById("loginlightbox_status_msg"); 
  Element.show(statusMsgElement);
  
  if (status == "success") {
     document.getElementById('lightboxLoading').style.visibility = 'visible';
     statusMsgElement.innerHTML = " Loading...";
     window.location.reload();
     startWin(jsonObj.forward);
  }
  else {
     statusMsgElement.innerHTML = jsonObj.message; 
  }
  
  Windows.focusedWindow.updateHeight();  
}

// 
// This function retrives login information from login lightbox and submits this information for process 
//
function submitLoginLightbox(url,msg,popup) {
  
   var username = document.getElementById("lightboxUserName").value;
   var password = document.getElementById("lightboxPassword").value;
   var action = document.getElementById("lightboxAction").value;
   
	var rememberMeCheckbox = document.getElementById("lightboxRememberMe");    
   
   var rememberme = "false";
   
   if (rememberMeCheckbox.checked)
   {
      rememberme = "true";
   }

   // Display an error message if username field is empty
   if (username == undefined || username == null || trimString(username).length == 0) {
      var statusMsgElement = document.getElementById(msg);
      statusMsgElement.innerHTML = "Username is required"
      Element.show(statusMsgElement); 
      Windows.focusedWindow.updateHeight();
      return false;
   }
   
   // Display an error message if password field is empty
   if (password == undefined || password == null || trimString(password).length == 0) {
      var statusMsgElement = document.getElementById(msg);
      statusMsgElement.innerHTML = "Password is required"
      Element.show(statusMsgElement); 
      Windows.focusedWindow.updateHeight();
      return false;
   }
   
   if (popup == "true") {
      ajax = new Ajax.Request (
   		url+"&action="+action+"&userName="+username+"&password="+password+"&rememberme="+rememberme, {onComplete: showLoginLightboxResponseWithPopup}
      );
   }
   else {
      ajax = new Ajax.Request (
   		url+"&action="+action+"&userName="+username+"&password="+password+"&rememberme="+rememberme, {onComplete: showLoginLightboxResponse}
      );
   }
   
   return true;
}

//
// This function renders the login lightbox
//
function openLoginLightbox(url,id,msg,popup) {
   // Use the Dialog module of Prototype Window Class 
   // to implement login lighbox
   Dialog.confirm($(id).innerHTML,
   		{windowParameters: {className:'alphacube', width:400}, 
   		 okLabel: '&nbsp;', 
   		 cancelLabel: '&nbsp;',
   		 buttonClass: 'loginLightboxButtonClass',	
   		 ok: function(win){
   		    submitLoginLightbox(url,msg,popup);
            return false;
         },
         cancel: function(win) {
            return true;
         }
   		});
}