if (typeof dynamo == "undefined") { 
  var dynamo = new Object();
}

if (typeof protectedAreaRoot == "undefined") { 
  var protectedAreaRoot = "dynamo/_config/sub_controller.asp";
}
  
if (typeof dynamo.loginBoxInterface == "undefined") {
  dynamo.loginBoxInterface = function() {
    this.ajax = new luckymarble.ajaxUtil();
    this.debugMode = false;
    
    var oldonload = window.onload;

    if (typeof window.onload != 'function') {
      window.onload = function() {
        access.initialize();
      }
    } else {
      window.onload = function() {
        if (oldonload) {
          oldonload();
        }
        access.initialize();
      }
    }
  }

  dynamo.loginBoxInterface.prototype = {
    // handle when user clicks on "forgot password" link
    getPassword: function() {
      // in the event that this is on a local system, alert the user
      if (window.location.href.indexOf("http")) {
        alert(languageParam["dynamo_not_active"]);
 
      // else handle the parameters from the form
      } else {
        myForm       = document.forms['dynamo_listings_login_form'];
        myLoginField = myForm['dynamo_listings_login'];
        myLoginValue = myLoginField.value;
    
        // if the user has not specified their login id, then alert them what to do
        if (myLoginValue == "") {
          alert(languageParam["login_box-forgot_password_message"]);
          
        // submit the request to have the login information emailed to the user
        } else {
          this.ajax.getAjaxRequest(rootPath + "dynamo/", "module=contributor&component=login_box-ajax&cmd=forgot_password&bypass=1&login="+myLoginValue, this.processGetPassword, "txt");
        }
      }    
    },
    
    // handle when a user clicks on the "login" button
    login: function () {
      // in the event that this is on a local system, alert the user
      if (window.location.href.indexOf("http")) {
        alert(languageParam["dynamo_error-not_active"]);
      
      // else handle the parameters for the form
      } else {
        myForm       = document.forms['dynamo_listings_login_form'];
        myLoginField = myForm['dynamo_listings_login'];
        myLoginValue = myLoginField.value;
        //alert(myLoginValue);
    
        myPasswordField = myForm['dynamo_listings_password'];
        myPasswordValue = myPasswordField.value;
        //alert(myPasswordValue);
                    
        this.ajax.getAjaxRequest(rootPath + "dynamo/", "module=contributor&component=login_box-ajax&cmd=login&bypass=1&login="+myLoginValue+"&password="+myPasswordValue, this.processGetPost, "txt");
      }
    },
    
    // handle when a response is received for the "send password" request
    processGetPassword: function() {
      var myAjax     = dynamo.loginBoxInterfaceObj.ajax.ajaxObj;
      var myFileType = dynamo.loginBoxInterfaceObj.ajax.fileType;
  
      if (myAjax.readyState == "4") {
        if (myAjax.status == 500 && dynamo.loginBoxInterfaceObj.debugMode == true) {
          alert(myAjax.responseText);
        } else if (myAjax.status == 200 || window.location.href.indexOf("http") == -1) {
          if (myFileType == "txt") {
            if (myAjax.responseText != "") {
              alert(myAjax.responseText);
            }
          } 
        } else  {
          alert(myAjax.status + languageParam["dynamo_error-retrieving_response"]);
        }
      } 
    },

    // handle when a response is received for the "login" request
    processGetPost: function() {
      var myAjax     = dynamo.loginBoxInterfaceObj.ajax.ajaxObj;
      var myFileType = dynamo.loginBoxInterfaceObj.ajax.fileType;
  
      if (myAjax.readyState == "4") {
        if (myAjax.status == 500 && dynamo.loginBoxInterfaceObj.debugMode == true) {
          alert(myAjax.responseText);
        } else if (myAjax.status == 200 || window.location.href.indexOf("http") == -1) {
          if (myFileType == "txt") {
            if (myAjax.responseText != "") {
              // if the response is "Logged In" then direct to the main access page
              var msg = new String(myAjax.responseText);
              
              var pos = msg.indexOf("Logged In")
              if (pos >= 0) {
                var additionalParams = msg.substr(10, msg.length);
                //alert(additionalParams);
                
                document.location = rootPath + protectedAreaRoot + "?module=contributor&component=manage-listings&" + additionalParams;
              
              // if the user is not logged in, then tell them why not
              } else {
                alert(myAjax.responseText);
              }
            }
          } 
        } else  {
          alert(myAjax.status + languageParam["dynamo_error-retrieving_response"]);
        }
      } 
    },
    
    // handle when a response is received for the "auto login" request
    processAutoLogin: function() {
      var myAjax     = dynamo.loginBoxInterfaceObj.ajax.ajaxObj;
      var myFileType = dynamo.loginBoxInterfaceObj.ajax.fileType;
      
      if (myAjax.readyState == "4") {
       if (myAjax.status == 500 && dynamo.loginBoxInterfaceObj.debugMode == true) {
          alert(myAjax.responseText);
        } else if (myAjax.status == 200 || window.location.href.indexOf("http") == -1) {
          if (myFileType == "txt") {
            
            // if there is a response
            if (myAjax.responseText != "") {
              // if received the "Logged In" message
              var msg = new String(myAjax.responseText);
              
              var pos = msg.indexOf("Logged In")
              if (pos >= 0) {
                var additionalParams = msg.substr(10, msg.length);
               // alert(additionalParams);
                
                

           
                // if the "auto login" parameter is set then direct to the access component
                if (typeof autoLogin != "undefined" && autoLogin == true){
                  document.location = rootPath + protectedAreaRoot + "?" + additionalParams;
                                  
                  // if we are not told to "auto login" then send a request for the "login information" for the "Logged In User" information box            
                } else {
                  access.ajax.getAjaxRequest(rootPath + "dynamo/", "module=contributor&component=login_box-ajax&cmd=get_login_info&bypass=1", access.renderLoggedInBox, "txt");
                }

              // else, we are not logged in and should DISPLAY the log in box
              } else {
                access.showLoginBox();
              }
              
            // else, nothing was retrieved, so we should DISPLAY the log in box
            } else {
              access.showLoginBox();
            }
          }
        } else  {
          alert(myAjax.status + languageParam["dynamo_error-retrieving_response"]);
        }
      } 
    },
    
    // show the login box
    showLoginBox: function() {
      var myLoginBox = document.getElementById("dynamo_listings_loginbox");          
      
      // brand the box
      access.brand(); 
      
      // display the box         
      myLoginBox.style.display = "block";     
    },
    
    // handle when a response is received for the "render logged in box"
    renderLoggedInBox: function() {
	  var myAjax     = dynamo.loginBoxInterfaceObj.ajax.ajaxObj;
      var myFileType = dynamo.loginBoxInterfaceObj.ajax.fileType;
     
      if (myAjax.readyState == "4") {
        if (myAjax.status == 500 && dynamo.loginBoxInterfaceObj.debugMode == true) {
          alert(myAjax.responseText);
        } else if (myAjax.status == 200 || window.location.href.indexOf("http") == -1) {
          if (myFileType == "txt") {
            if (myAjax.responseText != "") {
              // DEVELOPER NOTE!
              // The code block may cause issues with the png-image script
              // and may need to be modified so as not to assign text directly to the
              // DOM via innerHTML assignment
          
     		  var myBGBox       = document.getElementById("dynamo_listings_loginbox");
     		  myBGBox.className = "dynamo_listings_statusbox";

              var myTextLabel       = document.getElementById("dynamo_listings_loginbox_top_text_label");
              myTextLabel.innerHTML = languageParam["login_box-logged_in_box_title"];
              
              var myTextArea       = document.getElementById("dynamo_listings_login_middle");
              myTextArea.innerHTML = "<center>" + myAjax.responseText + "<br><br><a href='dynamo/?module=contributor&component=login_box-ajax&bypass=1&openw=list' target='_blank'>" + languageParam["login_box-launch_title"] + "</a><br><br><a href='dynamo/?module=contributor&component=login_box-ajax&cmd=exitb'>" + languageParam["login_box-logged_in_box_logout_link_text"] + "</a><br></center>";

              var myLoginBox = document.getElementById("dynamo_listings_component_container");          
              var myLoginBox = document.getElementById("dynamo_listings_loginbox");          
      
              // brand the box
              access.brand(); 
              
              // display the box
              myLoginBox.style.display = "block";
            } 
          } 
        } else  {
          alert(myAjax.status + languageParam["dynamo_error-retrieving_response"]);
        }
      } 
    },

    // handle initialization sequence of the login box
    initialize: function() {   
      // in the event that this is on a local system, show the login box
      if (window.location.href.indexOf("http")) {
        this.showLoginBox();
        
      // otherwise, if we are live, then attempt to determine if we are logged in (to show or re-render the box)
      } else {
        access.ajax.getAjaxRequest(rootPath + "dynamo/", "module=contributor&component=login_box-ajax&cmd=login&bypass=1&try=1", access.processAutoLogin, "txt");
      }
    },  
    
    // brand the box
    brand: function() {
      
      var myForm = document.getElementById('dynamo_listings_login_bottom');
      var newDiv = document.createElement("div");
      var newText = document.createTextNode("powered by ");
      newDiv.appendChild(newText);
      
      var newLinkText = document.createTextNode("dynamo");
      var newLink     = document.createElement("a");
      newLink.setAttribute("href", "http://www.i3dthemes.com/");
      
      newLink.setAttribute("target", "_blank");
      newLink.setAttribute("class", "dynamo_brand");
      newLink.setAttribute("className", "dynamo_brand");
      newLink.appendChild(newLinkText);
      newDiv.appendChild(newLink);
      newDiv.setAttribute('class', 'dynamo_brand');
      newDiv.setAttribute('className', 'dynamo_brand');
      myForm.appendChild(newDiv);  
    }
  }

  dynamo.loginBoxInterfaceObj = new dynamo.loginBoxInterface();
  access = dynamo.loginBoxInterfaceObj;
}