if (typeof dynamo == "undefined") { 
  var dynamo = new Object();
}
  
if (typeof dynamo.registerInterface == "undefined") {
  dynamo.registerInterface = function() {
    this.ajax = new luckymarble.ajaxUtil();
    this.debugMode = false;
    var oldonload = window.onload;

    if (typeof window.onload != 'function') {
      window.onload = function() {
        register.initialize();
      }
    } else {
      window.onload = function() {
        if (oldonload) {
          oldonload();
        }
        register.initialize();
      }
    }
  }

  dynamo.registerInterface.prototype = {
    // function to handle the click of the "continue" button
    proceed: function() {
      document.location = loginPage;
    },
    
    // function to handle the click of the "register" button
    submit: 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 from the form
      } else {
        myForm     = document.forms['dynamo_listings_registration_form'];
        parameters = "cmd=add";
        errors     = 0;
        
        for (var i = 0; i < myForm.elements.length; i++) {
          var myField      = myForm.elements[i];
          var myFieldValue = myField.value;
          var myFieldName  = myField.name.replace(/_/gi, " ");
          
          // handle case of "agree to terms" checkbox
          if (myFieldName == "agree") {
            if (myField.checked) {
              parameters = parameters + "&" + myField.name + "=1";
            
            // alert the user that the checkbox must be checked, then break out of this loop
            } else {
              alert(languageParam["registration_page-agree_to_terms_alert"]);
              errors = 1;

              parameters = parameters + "&" + myField.name + "=0";
              break;
            }
          } else {
            parameters = parameters + "&" + myField.name + "=" + myField.value; 
          }
        }
                 
        // provided there are no errors, submit the form (via POST)
        if (errors == 0) { 
          register.ajax.postAjaxRequest(rootPath + "dynamo/?module=contributor&component=register_page-ajax&bypass=1", parameters, register.processRender, "txt");
        } else {
          //alert(errors);
        }
      }
    },
    
    // render the "registration" form based on information retrieved via AJAX
    processRender: function() {
      var myAjax     = dynamo.registerInterfaceObj.ajax.ajaxObj;
      var myFileType = dynamo.registerInterfaceObj.ajax.fileType;
  
      if (myAjax.readyState == "4") {
        if (myAjax.status == 500 && dynamo.registerInterfaceObj.debugMode == true) {
          alert(myAjax.responseText);
        } else if (myAjax.status == 200 || window.location.href.indexOf("http") == -1) {
          if (myFileType == "txt") {
            if (myAjax.responseText != "") {
              var myTargetDiv = document.getElementById("dynamo_listings_user_registration_info_table");
              var myNewDiv    = document.createElement("div");
              var childNodes = myTargetDiv.childNodes;
              
              // assign response text to a separate "document" not attached to the main DOM.
              // This must be done otherwise there will be a confilict with png-image handler script
              myNewDiv.innerHTML = myAjax.responseText;

              // clear any previous html from where the form would be
              if (childNodes.length > 0) {
                myTargetDiv.removeChild(childNodes[0])
              }
             
              // attach the new response
              myTargetDiv.appendChild(myNewDiv);
            }
          } 
        } else  {
          alert(myAjax.status + languageParam["dynamo_error-retrieving_response"]);
        }
      } 
    },
    
    // handle initialization
    initialize: function() {    
      this.ajax.getAjaxRequest(rootPath + "dynamo/", "module=contributor&component=register_page-ajax&cmd=render&bypass=1", this.processRender, "txt");
    } 
  }

  dynamo.registerInterfaceObj = new dynamo.registerInterface();
  register = dynamo.registerInterfaceObj;
}