// holds an instance of XMLHttpRequest
var login_popup_xmlHttp = login_popup_createXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function login_popup_createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var login_popup_xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    login_popup_xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
 
    for (var i=0; i<XmlHttpVersions.length && !login_popup_xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        login_popup_xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!login_popup_xmlHttp)
      alert("Error creating the XMLHttpRequest object.");
  else 
      return login_popup_xmlHttp;
}

function submit_login(login_action,login_username,login_password,remember_username)
{
    if ( login_username != '' || login_action == 'logout' )
    {
        document.getElementById('login_status').innerHTML = "<font size=1 color=blue>Logging in...</font> ";
        login_popup_connect("login_popup.php?username="+login_username+"&password="+login_password+"&action="+login_action+"&remember="+remember_username)
    }
}

function login_popup_connect(url_string)
{
    if (login_popup_xmlHttp)
    {
        // try to connect to the server
        try
        {
            // initiate reading a file from the server
            login_popup_xmlHttp.open("GET", url_string, true);
            login_popup_xmlHttp.onreadystatechange = login_popup_handleRequestStateChange;
            login_popup_xmlHttp.send(null);
        }
        // display the error in case of failure
        catch (e)
        {
            alert("Can't connect to server:\n" + e.toString());
        }
    }
}

// function called when the state of the HTTP request changes
function login_popup_handleRequestStateChange() 
{
    // when readyState is 4, we are ready to read the server response
    if (login_popup_xmlHttp.readyState == 4) 
    {
        // continue only if HTTP status is "OK"
        if (login_popup_xmlHttp.status == 200) 
        {
            try
            {
                // do something with the response from the server
                login_popup_handleServerResponse();
            }
            catch(e)
            {
                // display error message
                alert("Error reading the response: " + e.toString());
            }
        } 
        else
        {
            // display status message
            alert("There was a problem retrieving the data:\n" + login_popup_xmlHttp.statusText);
        }
    }
}

// handles the response received from the server
function login_popup_handleServerResponse()
{
    var login_successful = false;
    // read the message from the server
    var xmlResponse = login_popup_xmlHttp.responseXML;
    // catching potential errors with IE and Opera
    if (!xmlResponse || !xmlResponse.documentElement)
        throw("Invalid XML structure:\n" + login_popup_xmlHttp.responseText);
    // catching potential errors with Firefox
    var rootNodeName = xmlResponse.documentElement.nodeName;
    if (rootNodeName == "parsererror") throw("Invalid XML structure");
    // obtain the XML's document element
    xmlRoot = xmlResponse.documentElement;  

    resultNode = xmlRoot.getElementsByTagName("result");
    actionNode = xmlRoot.getElementsByTagName("action");
    response_actionNode = xmlRoot.getElementsByTagName("response_action");
    email_address = xmlRoot.getElementsByTagName("email_address");
    returned_email_address = '';
    if ( email_address.length > 0 && email_address.item(0).firstChild )
    {
        returned_email_address = ' '+email_address.item(0).firstChild.data;
    }
    if ( actionNode.length > 0 && actionNode.item(0).firstChild )
    {
        if ( actionNode.item(0).firstChild.data == 'login' )
        {
            if ( resultNode.length > 0 && resultNode.item(0).firstChild )
            {
                if ( resultNode.item(0).firstChild.data == 'success' )
                {
                    login_successful = true;
                }    
                else if ( resultNode.item(0).firstChild.data == 'confirmation_required' )
                {
                    document.getElementById('login_status').innerHTML = "<font size=1 color=blue>Your account email has not yet been confirmed.<br/> Please check your email and respond to the confirmation link. Click <a href='resend_confirmation.php'><b>here</b></a> to resend your confirmation email.</font> ";
                }
                else
                {
                    document.getElementById('login_status').innerHTML = "<font size=1 color=red>Your login was unsuccessful.<br/> Please try again using your email address.</font>";
                }
            }
        }
        else if ( actionNode.item(0).firstChild.data == 'logout' )
        {
            if ( resultNode.length > 0 && resultNode.item(0).firstChild )
            {
                if ( resultNode.item(0).firstChild.data == 'success' )
                {
                    if ( document.getElementById('login_link') != null )
                        document.getElementById('login_link').innerHTML = "<a href=\"#\" onclick=\"open_LoginPopup_dialog(); return false;\">Sign In</a> or <a href=\"register.php\">Register</a>";
                   if ( document.getElementById('welcome_text') != null )
                         document.getElementById('welcome_text').innerHTML = "You have been successfully logged out of the system.";
                }    
            }
        }
    }
    if ( response_actionNode.length > 0 && response_actionNode.item(0).firstChild )
    {
        if ( response_actionNode.item(0).firstChild.data == 'reload' )
        {
            //alert($.fn.colorbox.element());
            if ( login_successful )
                jump_to_page($.fn.colorbox.element());       
            else
                location.reload(true);
        }
    }
    
}



