var listingCount = "";
var attemptCount = 0;
var intervalID = 0;
var formToPost;

function getListingCount()
{
	attemptCount++;
	
	try {
		var countFrame = MM_findObj('ifrLC');
		var countCtrl = MM_findObj('txtListingCount');

		//listingCount = countFrame.document.all.countValue.innerHTML;
		if (ns4) listingCount = countFrame.document.getElementById('countValue').innerHTML;
	  else if (ie4) listingCount = countFrame.document.all.countValue.innerHTML;

		//BEGIN: this is to ALWAYS submit, regardles of whether or not the listing count is returned.
		formToPost.submit(); 
		return true;
		//END
		
		countCtrl.value = listingCount.replace(".","").replace(",","");
		//alert(countCtrl.value);
		if ((listingCount != "" || attemptCount > 4) && listingCount != "0")  //4 times .5 seconds = 2 seconds
		{
			clearInterval(intervalID); //stop the interval
			formToPost.submit();
		}
  }
	catch (err)
	{}
		
	//alert('stop loop');
}
function postSearch(searchForm)
{
	//alert('here!  quitLoop = ' + quitLoop);
	formToPost = searchForm;
	if (formToPost.id == 'SearchForm') formToPost.ProcessAction.value = 'Search';
	getListingCount();
	if (listingCount == "")
	{
    //intervalID = setInterval("getListingCount()", 500); //try every second to get the listing count
  }
  return true; //do not post the form just yet, if ListingCount is found, the form will be posted from getListingCount function
	//alert(countFrame.document.all.countValue.innerHTML);

}

function get_radio_value(c)
{
	var r = MM_findObj(c);
	if (!r)
		return r;
	else
	{
		for (var i=0; i < r.length; i++)
		{
			if (r[i].checked)
			{
					return r[i].value;
			}
		}
  }
}

function get_value(c)
{
	var r = MM_findObj(c);
	if (!r)
		return r;
	else
		return r.value;
}


function RefreshListingCounts(from) 
{		
	// Function to get all the controls varaibles and build the query string
	if (IsPageLoad) //need to have this variable declared on the page that calls this function
	                //it is used to ensure the counts do not get updated multiple times as the page loads
	{
	 // alert('counts');
	  var page;
	  try {
	  var countFrame = MM_findObj('ifrLC');
		countFrame.document.all.countValue.innerHTML = ""; //reset the text so that an incorrect number is not used in a search result total
    } catch(err) {}
	  // Done: using MM_findObj("lstListingAdded").Value instead
	  page = "ListingCounts.asp";
	  if (get_value("lstRegion"))
		  page += "?Region=" + get_value("lstRegion"); //this is the SearchCurRegion
  
	  //	page += "&Lang=" + lang;
	  page += "&From=" + from;
	  if (from == 1)
		  page += "&QuickSearch=" + from;
	  // Get ComRes from InternetSearch.asp (hidden value)
	  if (get_value("ComRes"))
		  page += "&ComRes=" + get_value("ComRes");
	  // Get ComRes from StandModule.asp (radio button)
	  if (get_radio_value("ComRes"))
		  page += "&ComRes=" + get_radio_value("ComRes");
	  if (get_value("lstTransactionType"))
		  page += "&lstTransactionType=" + get_value("lstTransactionType");
	  if (get_value("lstPropertyType"))
		  page += "&lstPropertyType=" + get_value("lstPropertyType");
	  if (get_value("lstCommPropertyType"))
		  page += "&lstCommPropertyType=" + get_value("lstCommPropertyType");
	  if (get_value("lstRegionalZone"))
		  page += "&lstRegionalZone=" + get_value("lstRegionalZone");
	  if (get_value("lstProvince"))
		  page += "&lstProvince=" + get_value("lstProvince");	  
	  if (get_value("txtCity"))
		  page += "&txtCity=" + get_value("txtCity");		  
		if (get_value("txtStreet"))
		  page += "&txtStreet=" + get_value("txtStreet");   // CR 17118	
	  if (get_value("lstLocalZone")) 
		  page += "&lstLocalZone=" +get_value("lstLocalZone");
	  if (get_value("lstRadius"))
		  page += "&lstRadius=" + get_value("lstRadius");
	  if (get_value("RadiusSearchZIP"))
		  page += "&RadiusSearchZIP=" + get_value("RadiusSearchZIP");
  		
  		
	  if (get_value("ZIP1"))
		  page += "&ZIP1=" + escape(get_value("ZIP1"));
	  if (get_value("ZIP2"))
		  page += "&ZIP2=" + escape(get_value("ZIP2"));
	  if (get_value("lstCurrency"))
		  page += "&lstCurrency=" + get_value("lstCurrency");
	  if (get_value("lstMinPrice"))
		  page += "&lstMinPrice=" + get_value("lstMinPrice");
	  if (get_value("lstMaxPrice"))
		  page += "&lstMaxPrice=" + get_value("lstMaxPrice");
	  if (get_value("lstRentalMinPrice"))
		  page += "&lstRentalMinPrice=" + get_value("lstRentalMinPrice");
	  if (get_value("lstRentalMaxPrice"))
		  page += "&lstRentalMaxPrice=" + get_value("lstRentalMaxPrice");
	  if (get_value("lstSqMeters"))
		  page += "&lstSqMeters=" + get_value("lstSqMeters");
	  if (get_value("lstFloorLevel"))
		  page += "&lstFloorLevel=" + get_value("lstFloorLevel");
	  if (get_value("lstNumOfBedrooms"))
		  page += "&lstNumOfBedrooms=" + get_value("lstNumOfBedrooms");
	  if (get_radio_value("lstListingAdded"))
		  page += "&lstListingAdded=" + get_radio_value("lstListingAdded");
	  if (get_radio_value("lstPhotos"))
		  page += "&lstPhotos=" + get_radio_value("lstPhotos");
	  if (get_radio_value("lstPageSize"))
		  page += "&lstPageSize=" + get_radio_value("lstPageSize");
	  if (get_radio_value("lstOrder"))
		  page += "&lstOrder=" + get_radio_value("lstOrder");
	  if (get_radio_value("lstActiveListings"))
		  page += "&lstActiveListings=" + get_radio_value("lstActiveListings");
	  if (get_radio_value("ComRes_Group"))
		  page += "&ComRes_Group=" + get_radio_value("ComRes_Group");

	  if (get_value("txtAvailDate")){
		  var str = get_value("txtAvailDate");
		  //alert(str.slice(0,2))
		  if (!isNaN(str.slice(0,2)))
		  {
			  page += "&txtAvailDate=" + get_value("txtAvailDate");
		  }
	  }
	  if(get_value("txtMLSID"))
		page += "&txtMLSID=" + get_value("txtMLSID");
	  
	  var el = MM_findObj("cbPrivatelyOwned");
	  if (el != undefined)
	  {
	    if (el.checked == true) page += "&cbPrivatelyOwned=1";
	  }
	  


	  //'<%Response.write(%>' + page + '<%) Response.End%>' ;
	  if (MM_findObj('ifrLC')!= null){
	  	  document.all["ifrLC"].src = page;	
	  }
	  
	  
	  //window.frames.ifrLC.src  = page;
	  
	  //var frame = MM_findObj("ifrLC");
	  ////alert(frame);
	  //if (frame == null)
	  //{
	  //  alert('Frames not supported!');
	  //}
	  //else
	  //{
	  //  frame.src = page;
	  //  //frame.location.reload();
	   // alert(frame.src);
	    
	  //}
	}
		
}
