/**
 * Copyright (c) 2006, ARC Technologies.
 * All Rights Reserved
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * File     $Id: cml.js 1274 2008-10-14 02:17:01Z emann $ <br />
 * Author   Ed Mann <ed.mann at arctechnologies dot net>
 * Last Changed By   $LastChangedBy: emann $ <br />
 * Last Changed Date $LastChangedDate: 2008-10-13 21:17:01 -0500 (Mon, 13 Oct 2008) $ <br />
 * Description:  File will call ajax action for our Commercial Listing Module
 * @package eve.php.modules.commercialListing
 */

/**
  * gloabal vars
  */
  var url        = "/index.php";
  var submit_str = "";
  var cml_type;
  var cml_status;
  var cml_prop_status;
  var cml_prop_type;
  var cml_min_tot_sf;
  var cml_max_tot_sf;
  var cml_min_acre;
  var cml_max_acre;
  var offset;
  var context;
  var action;
  var module  = "CommercialListing";
  var t;
  
function doCmlListSearch(off){
	context   = "cml_web_search";
    action    = "CommercialListingPager";
    offset    = off; 
    activateLoader();
    submit_str       = doCmlUrl();
    /**
     * Now we can do our request and request our xmlDoc
     */
    doRequest(url,doCmlSearchResult,submit_str); 
}

function activateLoader(){
	var pg_sr = document.getElementById('search_results');
	var ajax_cell = document.getElementById('cml_pager');
	pg_sr.removeChild(pg_sr.lastChild);
	ajax_cell.innerHTML = "&nbsp;";
	var img = document.createElement('img');
	img.setAttribute("src", "/themes/default/images/maloof-loader.gif");
	pg_sr.appendChild(img);
}
function doCmlSearchResult(){
	if (req.readyState == 4) {
	       if (req.status == 200) {
				xmlDoc = req.responseXML;
		  
		  /**
	       * Now we can do our request and get our xmlDoc
	       */
			var count = xmlDoc.getElementsByTagName("cml_item");
	        var ajax_cell = document.getElementById('cml_pager');
			ajax_cell.innerHTML = xmlDoc.getElementsByTagName("cml_pager").item(0).firstChild.data;
	    	var pg_sr = document.getElementById('search_results');
	   	 	pg_sr.removeChild(pg_sr.lastChild);
	   	    var tbody = document.createElement('TBODY');
	   	 	var table = document.createElement("table");
	   	 	table.setAttribute("border", '0');
	   	 	table.setAttribute("cellspacing", '10');
	   	 	var row = document.createElement("TR");
	   	 	var pass = 0;
	    	for (i = 0; i < count.length; i++){
	    		if(pass == 3){
	    			 tbody.appendChild(row);
	    			 pass = 0;
	    			 row = document.createElement("TR");
	    		 }
	    	    /**
	             * check our values coming back from the server
	             */
	             var elements = new Array("cml_id","cml_addr","cml_list_price","cml_img");
	             for(var b = 0; b < elements.length; b++){
	          var ref_name = elements[b];
	    	    /**
	             * check our values coming back from the server
	             * we can avoid using a bunch of if statements by placing our items in an array. Then we loop through each one like we are
	             * and window will make our ref_name a variable. Then below we can build our table off that. I need to find someone to explain
	             * this window[] trick to me. I read about it in  JavaScript Bible 5th Edition, but it did not give me a good answer.
	             * http://vusti.dk/monogami/questions_and_answers.asp?id=34
	             * that is where i found the question and answer, but not any detail on the answer.
	             */
	         if( count.item(i).getElementsByTagName(ref_name).item(0).firstChild ){
	             window[ref_name]  = count.item(i).getElementsByTagName(ref_name).item(0).firstChild.data;
	 	         } else {
	 	             window[ref_name] = "";
	 	         }
	        }
	       
	    var td = document.createElement("td");
	    var br = document.createElement("br");
	    var price = document.createTextNode(cml_list_price);
	        td.innerHTML = "<br /><a href=\"/property/"+cml_id+"\" >"+cml_img+"</a><br />"+cml_addr;
	        td.appendChild(br);
	        td.appendChild(price);
	        row.appendChild(td);
	pass++;
		}
	  }
	  tbody.appendChild(row)
	  table.appendChild(tbody);
	  pg_sr.appendChild(table);
	  doObjReset();
}
}
	/**
	 * doCmlUrl
	 * 
	 * Function will prepare the url that we will send to the XMLHttpRequest object.
	 * Each value that you add to the url needs to be in the form of &var_name=value;
	 * 
	 * @param {void}
	 */
	 function doCmlUrl(){
	     if(!isEmpty(module)){
	        submit_str = submit_str+"&module="+module;
	    }
	    if(!isEmpty(action)){
	        submit_str = submit_str+"&action="+action;
	    }
	    if(!isEmpty(offset)){
	        submit_str = submit_str+"&offset="+offset;
	    }
	    if(!isEmpty(context)){
	        submit_str = submit_str+"&context="+context;
	    }
	    var src_elm = new Array("search_cml_prop_county","search_cml_id", "search_cml_prop_type","search_cml_prop_status",
	            "search_cml_min_tot_sf","search_cml_max_tot_sf","search_cml_min_acre","search_cml_max_acre");
	    submit_str = buildUrl(src_elm, submit_str);
	    hash_str = buildHashUrl(src_elm);
	    setLocationHash(submit_str);
	   return submit_str;
	 } 

/**
 * do Obj Reset
 * 
 * Function will reset the Objects on this page to the default
 * @return void;
 */
 function doObjReset(){
     url                = "/index.php";
     submit_str         = "";
     cml_name           = null;
     cml_id             = null;
     cml_rec_id         = null;
     cml_city           = null;
     cml_type           = null;
     cml_status         = null;
     cml_agent          = null;
     cml_prop_status    = null;
     cml_prop_type      = null;
     cml_street_num     = null;
     cml_street_dir     = null;
     cml_street_name    = null;
     cml_street_type    = null;
     cml_min_tot_sf     = null;
     cml_max_tot_sf     = null;
     cml_min_asf        = null;
     cml_max_asf        = null;
     cml_min_price_psf  = null;
     cml_max_price_psf  = null;
     offset             = null;
     context            = null;
     module             = "CommercialListing";
     action             = null;
 }
 
 function buildTable(){
	 pg_sr = document.getElementById('search_results');
	 pg_sr.removeChild(pg_sr.lastChild);
	 var table = document.createElement("table");
	 table.setAttribute("border", '1');
	 var count = new Array("ed was here 1","ed was here 2","ed was here 3");
	 var row = document.createElement("tr");
	 pass = 0;
	 for (i = 0; i < count.length; i++){
	 if(pass == 3){
		 table.appendChild(row);
		 pass = 0;
		 var row = document.createElement("tr");
	 }
	 var td0 = document.createElement("td");
     var str1 = document.createTextNode(count[i]);
     td0.appendChild(str1);
     row.appendChild(td0);
     pass++;
	 }
	 table.appendChild(row);
	 pg_sr.appendChild(table);
 }
