if (typeof dynamo == "undefined") { 
  var dynamo = new Object();
}
  
if (typeof rootPath == "undefined") {
  var rootPath = "./";
}  

if (typeof formId == "undefined") {
  var formId = "";
} 

function processFileUploads() {
  document.getElementById('dynamo_form_handler_form').target = 'upload_itarget';
  document.forms['dynamo_form_handler_form'].submit();
}

if (typeof dynamo.formBoxInterface == "undefined") {
  dynamo.formBoxInterface = function() {
    this.ajax = new luckymarble.ajaxUtil();
    this.debugMode = true;
    var oldonload = window.onload;

    if (typeof window.onload != 'function') {
      window.onload = function() {
        formHandler.initialize();
      }
    } else {
      window.onload = function() {
        if (oldonload) {
          oldonload();
        }
        formHandler.initialize();
      }
    }
  }

  dynamo.formBoxInterface.prototype = {
    // render the "registration" form based on information retrieved via AJAX
    processRender: function() {
      var myAjax     = dynamo.formBoxInterfaceObj.ajax.ajaxObj;
      var myFileType = dynamo.formBoxInterfaceObj.ajax.fileType;

      if (myAjax.readyState == "4") {     
        if (myAjax.status == 500 && dynamo.formBoxInterfaceObj.debugMode == true) {
          //alert('a');
          alert(myAjax.responseText);
		  //alert('b');          
        } else if (myAjax.status == 200 || window.location.href.indexOf("http") == -1) {
          if (myFileType == "txt") {
            if (myAjax.responseText != "") {
              var myTargetDiv = document.getElementById("dynamo_form_information_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
				//alert("x: " + childNodes.length);
				
				var mycount = 0;
				var mycchilds = childNodes.length;
				
				if (childNodes.length > 0) {
					myTargetDiv.removeChild(childNodes[0])
				}

				// attach the new response
				myTargetDiv.appendChild(myNewDiv);		
              }
          } 
        } else  {
          //alert(myAjax.status + languageParam["dynamo_error-retrieving_response"]);
          //alert('c');
          alert(languageParam["dynamo_error-retrieving_response"]);
          //alert('d');
        }
      } 
    },
    
    // handle form submission
    submit: function(pagePosition) {   
      myForm     = document.forms['dynamo_form_handler_form'];
      parameters = "cmd=save&formId=" + formId + "&pagePosition=" + pagePosition;
      errors     = 0;
      
      processFileUploads();
          
      for (var i = 0; i < myForm.elements.length; i++) {
        var myField      = myForm.elements[i];
        var myFieldValue = myField.value;
        var myFieldName  = myField.name.replace(/_/gi, " ");

        parameters = parameters + "&" + myField.name + "=" + escape(myField.value.replace(/&/gi, "%26")); 
      }

      this.ajax.postAjaxRequest(rootPath + "dynamo/?module=form_handler&component=form_page-ajax&bypass=1", parameters, this.processRender, "txt");
    },
    
    next: function(pagePosition) {   
      myForm     = document.forms['dynamo_form_handler_form'];
      parameters = "cmd=next&formId=" + formId + "&pagePosition=" + pagePosition;
      errors     = 0;
      
      processFileUploads();
          
      for (var i = 0; i < myForm.elements.length; i++) {
        var myField      = myForm.elements[i];
        var myFieldValue = myField.value;
        var myFieldName  = myField.name.replace(/_/gi, " ");

        parameters = parameters + "&" + myField.name + "=" + escape(myField.value.replace(/&/gi, "%26")); 
      }

      this.ajax.postAjaxRequest(rootPath + "dynamo/?module=form_handler&component=form_page-ajax&bypass=1", parameters, this.processRender, "txt");
    },
            
        
    // handle initialization
    initialize: function() {    
      this.ajax.postAjaxRequest(rootPath + "dynamo/?module=form_handler&component=form_page-ajax&bypass=1", "formId=" + formId, this.processRender, "txt");
    } 
  }

  dynamo.formBoxInterfaceObj = new dynamo.formBoxInterface();
  formHandler = dynamo.formBoxInterfaceObj;
}