if (typeof dynamo == "undefined") { 
  var dynamo = new Object();
}
  
if (typeof serverDynamoURL == "undefined") {
  var serverDynamoURL = "dynamo";
}  

if (typeof showResultsInPlace == "undefined") {
  var showResultsInPlace = true;
}
  
if (typeof hideSearchBoxOnLoad == "undefined") {
  var hideSearchBoxOnLoad = false;
}

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

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

  dynamo.searchBoxInterface.prototype = {
    // function to handle the click of the "continue" button
    proceed: function(formElement) {
      //document.location = loginPage;
      // 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['formItemType'];
        myForm.item_type_id.value = formElement.value;

        parameters = "lma_blank=nothing";
        //parameters = "";
        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, " ");
          
          parameters = parameters + "&" + myField.name + "=" + myField.value;
        }
         
         
        // provided there are no errors, submit the form (via POST)
        if (errors == 0) { 
          //alert(parameters);
          search.ajax.postAjaxRequest(rootPath + "dynamo/?module=listing_service&component=search_page-ajax&bypass=1", parameters, search.processRender, "txt");
        }
      }      
    },
    
    // 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_search_form'];
        parameters = "lmaaction=Search";
        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, " ");
          
          parameters = parameters + "&" + myField.name + "=" + myField.value; 
        }

        // provided there are no errors, submit the form (via POST)
        if (errors == 0) { 
          if (showResultsInPlace && document.getElementById("dynamo_listings_search_results_table")) {
            this.renderSubmit(parameters);
          } else {
            //alert(parameters);
            window.open(rootPath + "dynamo/?module=listing_service&component=search_page-ajax&source=popwindow&bypass=1&" + parameters,"Results","scrollbars=1,width=600,height=550");
          }
        }
      }
    },    
    
    // function to handle the click of the "register" button
    renderSubmit: function(parameters) {
		// provided there are no errors, submit the form (via POST)
		if (errors == 0) { 
			//alert(parameters);
			search.ajax.postAjaxRequest(rootPath + "dynamo/?module=listing_service&component=search_page-ajax&bypass=1", parameters, search.processRenderSearch, "txt");
		}
    },
    
	createAjaxObject: function () {
		var httpRequest = false;

		// Mozilla, Safari, etc 
		if (window.XMLHttpRequest) {
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType) {
				httpRequest.overrideMimeType('text/xml');
			}

		// Internet Explorer
		} else if (window.ActiveXObject) {
			try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					// do nothing
				}
			}
		}
		return httpRequest;
	},   
	
	evaluate: function(data){
		if(data == 0){
			//set form "stop looking" property to "true"
			return;
		}

		data = data.replace(/\n|\r/g,'');
		data = data.replace(/<html>.*?<\/head>/,'');
		data = data.replace(/.*?<p class=g>/,'<p class=g>');
		data = data.replace(/<div id=navbar class=n><table border=0 cellpadding=0 width=1% cellspacing=0 align=center>.*/g,'');
		var element = document.createElement('div');
		element.innerHTML = data;

		var divTags = document.getElementById('search_results');

		divTags.innerHTML = divTags.innerHTML + element.innerHTML;
		
	interval = window.setInterval('document.preloadNextPage()',50);
	},		
	
	getNextPage: function(parameters) {
	  search.ajax.postAjaxRequest(rootPath + "dynamo/?module=listing_service&component=search_page-ajax&bypass=1", parameters, search.processRenderSearch, "txt");
	  window.scrollTo(0, 0);
	},

    printListing: function(myListingId) {
     window.open(rootPath + "dynamo/?module=listing_service&component=print_window&bypass=1&listing_id=" + myListingId,"Print","width=166,height=166"); 
		},
		
    emailListing: function(myListingId) {     
      window.open(rootPath + "dynamo/?module=listing_service&component=email_window&bypass=1&listing_id=" + myListingId,"Email","scrollbars=1,width=380,height=278"); 
		},		

    // open the show listings full view
    changeListingView: function(myListingId) {
      var myResultViewDiv = document.getElementById("listing_result_" + myListingId);

      serverDynamoURL = serverDynamoURL.replace('dynamo//','');

 	  var myLoadingHTML = "<table cellpadding='0' cellspacing='0' width='100%'><tr><td class='wrapperBoxTopView'>Loading...</td></tr>";
	  myLoadingHTML = myLoadingHTML + "<tr><td class='wrapperBoxBottomView' height='92px' align='center'><table cellpadding='2' cellspacing='0' width='100%' border='0'>";
	  myLoadingHTML = myLoadingHTML + "<tr><td align='center'><img src='" + serverDynamoURL  + "/_config/@listing_service/images/loading.gif' alt='loading...' /></td></tr></table></td></tr></table>";

	  myResultViewDiv.innerHTML = myLoadingHTML;

      if (myResultViewDiv.className == "minimized") {
        //alert("max it");
                
        myResultViewDiv.className = "maximized";
        dynamo.searchBoxInterfaceObj.listingID = myListingId;
        
        parameters = "module=listing_service"; 
        parameters = parameters + "&component=view"; 
        parameters = parameters + "&listing_id=" + myListingId; 
        parameters = parameters + "&bypass=1"; 

				// recreate ajax object to defeat cache problem in IE
				myNewAjaxObj = this.createAjaxObject(); 

				// further attempt to defeat cache problem in IE
				//var parameters = parameters + "&cache=" + new Date().getTime()

				// if the object exists, then GET request
				if (myNewAjaxObj) {			
					myNewAjaxObj.onreadystatechange = function(){
						if(myNewAjaxObj.readyState == 4){
						  //alert("x: " + dynamo.searchBoxInterfaceObj.listingID);
						  var myResultViewDiv = document.getElementById("listing_result_" + dynamo.searchBoxInterfaceObj.listingID);
							myResultViewDiv.innerHTML = myNewAjaxObj.responseText;

							//this.listingID = "";
						}
					}
					//alert(parameters);
					//myNewAjaxObj.open('GET', "./dynamo/?" + parameters, true);
					//myNewAjaxObj.open('GET', rootPath + "/dynamo/?" + parameters, true);
					myNewAjaxObj.open('GET', rootPath + "dynamo/?" + parameters, true);
					myNewAjaxObj.send(null);
				}        
      } else {
        //alert("min it");
        
        myResultViewDiv.className = "minimized";  
        dynamo.searchBoxInterfaceObj.listingID = myListingId;
        
        parameters = "module=listing_service"; 
        parameters = parameters + "&component=search_page-ajax"; 
        parameters = parameters + "&listing_id=" + myListingId; 
        parameters = parameters + "&lmaaction=Search"; 
        parameters = parameters + "&searchListings=GetResult"; 
        parameters = parameters + "&bypass=1"; 

				// recreate ajax object to defeat cache problem in IE
				myNewAjaxObj = this.createAjaxObject(); 

				// further attempt to defeat cache problem in IE
				var parameters = parameters + "&cache=" + new Date().getTime()

				// if the object exists, then GET request
				if (myNewAjaxObj) {			
					myNewAjaxObj.onreadystatechange = function(){
						if(myNewAjaxObj.readyState == 4){
						  //alert("x: " + dynamo.searchBoxInterfaceObj.listingID);
						  var myResultViewDiv = document.getElementById("listing_result_" + dynamo.searchBoxInterfaceObj.listingID);
							myResultViewDiv.innerHTML = myNewAjaxObj.responseText;
							
							//this.listingID = "";
						}
					}
					//alert("dynamo/?" + parameters);
					//myNewAjaxObj.open('GET', "./dynamo/?" + parameters, true);
					//myNewAjaxObj.open('GET', rootPath + "/dynamo/?" + parameters, true);
					myNewAjaxObj.open('GET', rootPath + "dynamo/?" + parameters, true);
					myNewAjaxObj.send(null);
				}              
        
            
      }
    },
        
    // render the "registration" form based on information retrieved via AJAX
    processRender: function() {
      var myAjax     = dynamo.searchBoxInterfaceObj.ajax.ajaxObj;
      var myFileType = dynamo.searchBoxInterfaceObj.ajax.fileType;

      if (myAjax.readyState == "4") {     
        if (myAjax.status == 500 && dynamo.searchBoxInterfaceObj.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_listings_search_table");
              //if (!document.getElementById("dynamo_listings_search_table")) {
							//	myTargetDiv = document.getElementById("dynamo_listing_results_search_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;
				
				//while (mycount < (mycchilds -1)) {
				//  myTargetDiv.removeChild(childNodes[mycount]);
				//  mycount = mycount + 1;
				//}
				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');
        }
      } 
    },
    
    // render the "registration" form based on information retrieved via AJAX
    processRenderSearch: function() {
      var myAjax     = dynamo.searchBoxInterfaceObj.ajax.ajaxObj;
      var myFileType = dynamo.searchBoxInterfaceObj.ajax.fileType;

      if (myAjax.readyState == "4") {
  //alert(myAjax.status);
  //alert(dynamo.searchBoxInterfaceObj.debugMode);
        if (myAjax.status == 500 && dynamo.searchBoxInterfaceObj.debugMode == false) {
        //alert('e');
          alert(myAjax.responseText);
        //alert('f');
        } else if (myAjax.status == 200 || window.location.href.indexOf("http") == -1) {
          if (myFileType == "txt") {
            if (myAjax.responseText != "") {
              var myTargetDiv = document.getElementById("dynamo_listings_search_results_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('g');
          alert(myAjax.status + languageParam["dynamo_error-retrieving_response"]);
          //alert('h');
        }
      } 
    },    
    
    // handle initialization
    initialize: function() {    
      if (!hideSearchBoxOnLoad) {
      this.ajax.getAjaxRequest(rootPath + "dynamo/", "module=listing_service&component=search_page-ajax&cmd=render&bypass=1", this.processRender, "txt");
      }
    } 
  }

  dynamo.searchBoxInterfaceObj = new dynamo.searchBoxInterface();
  search = dynamo.searchBoxInterfaceObj;
}
