var request = GXmlHttp.create();
function selectall(divid)
{
var arr = new Array();
        arr = document.getElementsByName(divid);
		
		if(arr[0].checked==true)
		{
		 for(var i = 0; i < arr.length; i++)
         {
    	  arr[i].checked=true;
		 }
		}
		else
		{
		 for(var i = 0; i < arr.length; i++)
         {
           arr[i].checked=false;
		 }
		}
}						

function get(divid)
{
return document.getElementById(divid);	
}

function ifchecked(element)
{
return 	document.getElementById(element).checked;
}

function getcommonsearchcount()
{
	var tab=document.getElementById('activetab').value; 
	switch(tab)
	{
	case 'area':getareacount();break;
	case 'city':getcitycount();break;
	case 'county':getcountycount();break;
	case 'zip':getzipcount();break;
	case 'neighborhood':getneighborhoodcount();break;
		
	}
}

function getareacount()
{   
	var q='( '+getcommonquery()+') ';
	q1=gettabquery('area[]','MLSArea');
	if(q1!='')
	q+=' AND ('+q1+')';
	requesturl(q);
}


function getcitycount()
{
	var q='( '+getcommonquery()+') ';
	/*
	q1=gettabquery('city[]','tmlCity')
	if(q1!='')
	q+=' AND ('+q1+')';
	*/
	var q1;
	var q2 = [];
	q1 = $("#va_city").val();
	$(".va_srch_city").each(function(i) {
		q2.push(" (tmlCity = '"+$(this).html()+"' AND tmlState='VA') ");
	});

	$(".md_srch_city").each(function(i) {
		q2.push(" (tmlCity = '"+$(this).html()+"' AND tmlState='MD') ");
	});

	$(".dc_srch_city").each(function(i) {
		q2.push(" (tmlSubdivision = '"+$(this).html()+"' AND tmlState='DC') ");
	});
	
	if (q2.length > 0) {
	    q += ' AND ( '+q2.join(' OR ')+' ) ';
	}
//	alert('query is '+q);
//	return; 
	requesturl(q);
}

function getcountycount()
{
	var q='( '+getcommonquery()+') ';
    q1=gettabquery('county[]','tmlCounty');
	if(q1!='')
	q+=' AND ('+q1+')';
	requesturl(q);
}

function getzipcount()
{
	var q='( '+getcommonquery()+') ';
	q1=gettabquery('zip[]','tmlPostalCode')
	if(q1!='')
	q+=' AND ('+q1+')';
	requesturl(q);
}
function getneighborhoodcount()
{   
	var element=document.getElementById('testinput').value;
    if(element==""){
 
    }else{
	var q='( '+getcommonquery()+')  ';
	q+='AND ('+getneighborhoodquery()+')';
	requesturl(q);
	document.getElementById('testinput').value='';
	}
}

function getneighborhoodquery()
{
	 
	var query=''; 
	var oDiv = document.getElementById("myList");
	walker = document.createTreeWalker(oDiv, NodeFilter.SHOW_ELEMENT, null, false);
	while(walker.nextNode())
	{
	query+='tmlSubdivision=(\\"'+walker.currentNode.innerHTML+'\\") OR ';
	}
	return query.substring(0,query.length-3);
}


function gettabquery(mytab,column)
{
	var tabquery='';
    var arr = new Array();
    arr = document.getElementsByName(mytab);
	var chkcnt=0;
	 for(var i = 0; i < arr.length; i++)
        {
            if(arr[i].checked)
			{
			  chkcnt++;
			  if(i==0)return '';
			}
		}
	
	 for(var i = 0; i < arr.length; i++)
        {
            if(arr[i].checked && i!=0)
			{
			   tabquery+=' '+column+'="'+arr[i].value+'" ';
               tabquery+=' OR ';
			}
		}
	return tabquery.substring(0,tabquery.length-3);	
}


function getcommonquery()
{
	var myquery=null;
	
	var mydiv=get('minprice');

	var minprice= mydiv.options[mydiv.selectedIndex].value;

	mydiv=get('maxprice');
	var maxprice= mydiv.options[mydiv.selectedIndex].value;
	 
	var residential=ifchecked('residential');
	var land=ifchecked('land');
	var multifamily=ifchecked('multifamily');
	var commercial=ifchecked('commercial');
	 
	mydiv=get('beds');
	var beds= mydiv.options[mydiv.selectedIndex].value;
	
	mydiv=get('Baths');
	var Baths= mydiv.options[mydiv.selectedIndex].value;
	
	mydiv=get('yearbuilt');
	var yearbuilt= mydiv.options[mydiv.selectedIndex].value;
	
	mydiv=get('acreage');
	var acreage= mydiv.options[mydiv.selectedIndex].value;
	
	
	var garage=ifchecked('garage');
	var fireplace=ifchecked('fireplace');
	
	var keywords=document.getElementById('keywords').value;
	
	var foreclosure=ifchecked('foreclosure');
	var shortsale=ifchecked('shortsale');
	
	
	mydiv=get('status');
	var daysonmarket= mydiv.options[mydiv.selectedIndex].value;
	
	var virtualtour=ifchecked('virtualtour');
	var query='';
 
	if(beds==0)
	query='(tmlBeds IS NULL OR tmlBeds IS NOT NULL) ';
	else
	query=' tmlBeds >='+beds;
 
	if(Baths>0)
	query+=' AND tmlBaths>='+Baths;
	if(yearbuilt>0)
	query+=' AND tmlYearBuilt>='+yearbuilt;
	if(minprice>0)
	query+=' AND tmlListPrice >='+minprice;
	if(maxprice>0 & maxprice!=10000000000)
	query+=' AND tmlListPrice<='+maxprice;
	if(acreage>0)
	query+=' AND tmlLotAreaAcre >='+acreage;
	
	if(foreclosure)
	{
	  query+=' AND tmlForeclosure ="Y"'; 
	}
	if(shortsale)
	{
		query+=' AND tmlShortSale ="Yes"'; 
	}
	if(garage)
	{
		query+=' AND tmlGarage!="NULL"'; 
	}
	if(fireplace)
	{
		query+=' AND tmlFireplaces!="NULL"'; 
	}
	if(virtualtour)
	{
		query+=' AND tmlVirtualTourURL!="NULL"'; 
	}
	if(daysonmarket>0)
	{
		query+=' AND tmlDaysOnMarket<='+daysonmarket; 
	}
	
	if(residential || land || multifamily || commercial)
	{
		query+=' AND (';
	}
	if(residential)
	{
		query+='tmlPropertyType="Residential"'; 
	}
	if(land)
	{
		if(residential)
		query+=' OR tmlPropertyType="Lot-Land"'; 
		else
		query+=' tmlPropertyType="Lot-Land"'; 
	}
	if(multifamily)
	{
		if(residential || land)
		query+=' OR tmlPropertyType="Multi-Family"';
		else
		query+=' tmlPropertyType="Multi-Family"'; 
	}
	if(commercial)
	{
		if(residential || land || multifamily)
		query+=' OR tmlPropertyType="Commercial"';
		else
		query+=' tmlPropertyType="Commercial" '; 
	}
	
	if(residential || land || multifamily || commercial)
	{
		query+=' ) ';
	}
	
	if(keywords!='')
	query+=' AND `tmlInternetRemarks` LIKE ("%'+keywords+'%")';
	 
	return query;
}

function requesturl(params)
{  
 document.getElementById('topcount').innerHTML='Loading...';
 document.getElementById('bottomcount').innerHTML='<span style="font-size:14px">Loading...</span>';
    params=ReplaceAll(params,"#","%23");
	params=ReplaceAll(params,"&","%26");
    params1='getsearchcount.php?q='+params; 
    request.open("GET", params1, true);
	document.getElementById('query').value=encodeURIComponent(params); 
	request.onreadystatechange = function() {
      if (request.readyState == 4) { 
		  document.getElementById('topcount').innerHTML=request.responseText+' Properties Found';
		  document.getElementById('bottomcount').innerHTML='<span style="font-size:20px">'+request.responseText+'</span> Properties Found';
                                   }//nend if if ==4
	}
request.send(null);        
}

function ReplaceAll(Source,stringToFind,stringToReplace){
  var temp = Source;
    var index = temp.indexOf(stringToFind);
        while(index != -1){
            temp = temp.replace(stringToFind,stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;
}
 
function addElement(){
// Get the value into the input text field
var element=document.getElementById('testinput').value;
if(element==""){
 
}else{
// This is the <ul id="myList"> element that will contains the new elements
var container = document.getElementById('myList');
// Create a new <li> element for to insert inside <ul id="myList">
var new_element = document.createElement('li');
new_element.innerHTML = element;
container.insertBefore(new_element, container.firstChild);
 
}
}

$(document).ready(function(){
    $("#mls_search_btn").bind("click", function(){
	var val = $("#mls_search_text").val();
	if (val == '') {
	    alert('Please enter an MLS number');
	    return false;
	}
	else {
	    $.ajax({
		cache: false,
		url: TMPATH_ABS+'/realty/mls_search.php',
		data: 'mls_key='+val,
		type: "GET",
		success: function(ret, status1) {
		    if (ret == 'no') {
		//	alert('No listings for this key');
			$("#mls_search_msg").html('No listings for this key');
			$("#mls_search_a").click(); 
		    }
		    else if (ret == 'error') {
			$("#mls_search_msg").html('Sorry, an error occurred while processing your request');
			$("#mls_search_a").click(); 
		    }
		    else {
			window.location.href = ret;
		    }
		},
		error: function(msg, status2) {
		    alert('Could not process your request');
		}
	    });
	}
    });
    $("#mls_search_a").fancybox({});
});

