var orderBy = "price";
var orderDir = "DESC";
var currentPage = 1;

var listing_source = "";

var selected_home_features= "";
var selected_comm_features= "";



function Listing(property_id,agent_id){
	this.property_id = property_id;
	this.agent_id = agent_id;
	
	this.city = "";
	this.prop_type = "";
	this.bedroom = "";
	
	this.bathroom = "";
	this.garage = "";
	this.footage = "";
	this.pool = "";
	this.spa = "";

	this.price = "";
	this.brokerlogo_sm = "";
	this.listing_photo = "";
	this.keyword = "";

	this.clear_data = function() {
		this.property_id = "";
		this.agent_id = "";

		this.city = "";
		this.prop_type = "";
		this.bedroom = "";

		this.bathroom = "";
		this.garage = "";
		this.footage = "";
		this.pool = "";
		this.spa = "";

		this.price = "";
		this.brokerlogo_sm = "";
		this.listing_photo = "";
		this.keyword = "";
	
	}

	this.copy_data = function(obj) {
		this.property_id = obj.property_id;
		this.agent_id = obj.agent_id;

		this.city = obj.city;
		this.prop_type = obj.prop_type;
		this.bedroom = obj.bedroom;

		this.bathroom = obj.bathroom;
		this.garage = obj.garage;
		this.footage = obj.footage;
		this.pool = obj.pool;
		this.spa = obj.spa;

		this.price = obj.price;
		this.brokerlogo_sm = obj.brokerlogo_sm;
		this.listing_photo = obj.listing_photo;
		this.keyword = obj.keyword;
	
	}

}

var listingArrayIndex = 0;
var listingArray = new Array(10);

for(var i=0; i<11; i++) {
	listingArray[i] = new Listing("","");
}



var seletedListingArrayIndex = 0;
var selectedListingArray = new Array(20);

for(var i=0; i<20; i++) {
	selectedListingArray[i] = new Listing("","");
}


function ImageLoadFailed(obj) {
	obj.src = "http://www.househunt.org/images/no-photo-01.jpg";
	//window.event.srcElement.style.display = "None";
}



function resize_logo() {

	if(img_brokerlogo.height>80)
		img_brokerlogo.height = 80;
		
}



function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}

function genSearchOption(home_feature,comm_feature) {

	var sch_opt_content = '';
	
	sch_opt_content += '<TABLE style="width:744px; text-align:left;">';
	
	if(home_feature != "--") {

		sch_opt_content += '<TR>';
		sch_opt_content +=  '<TD ALIGN="left" COLSPAN="5" class="grytxtbld11" height=30>Home Features</TD>';

		var arr_home_feature = home_feature.split(",");
		cnt_cell =0;
		for(var i = 0; i < arr_home_feature.length; i++){

			if(home_feat[arr_home_feature[i]] == undefined) continue;

			if(cnt_cell==0) sch_opt_content += '</TR><TR>\n' ;

			cnt_cell++;
			sch_opt_content += '<TD><INPUT TYPE="checkbox" NAME="home_feature" VALUE="'+
					arr_home_feature[i] + '"';

			if(selected_home_features.indexOf(arr_home_feature[i])>=0) sch_opt_content += ' CHECKED ';
					
			sch_opt_content	+= '>' + home_feat[arr_home_feature[i]]+'</TD>';

			if(cnt_cell==5) cnt_cell=0 ;


		}
		sch_opt_content += '</TR>\n';
	}
	sch_opt_content += '<input type="hidden" name="home_feature">';

	
	if(comm_feature != "--") {

		sch_opt_content += '<TR>';
		sch_opt_content +=  '<TD ALIGN="left" COLSPAN="5" class="grytxtbld11" height=30>Community Features</TD>';

		var arr_comm_feature = comm_feature.split(",");
		cnt_cell =0;
		for(var i = 0; i < arr_comm_feature.length; i++){

			if(comm_feat[arr_comm_feature[i]] == undefined) continue;

			if(cnt_cell==0) sch_opt_content += '</TR><TR>\n' ;

			cnt_cell++;
			sch_opt_content += '<TD><INPUT TYPE="checkbox" NAME="comm_feature" VALUE="'+
					arr_comm_feature[i] + '"';

			if(selected_comm_features.indexOf(arr_comm_feature[i])>=0) sch_opt_content += ' CHECKED ';
					
			sch_opt_content	+= '>' + comm_feat[arr_comm_feature[i]]+'</TD>';

			if(cnt_cell==5) cnt_cell=0 ;


		}
		sch_opt_content += '</TR>\n';
	}

	sch_opt_content += '<input type="hidden" name="comm_feature">';

	sch_opt_content += '</TABLE>';

	document.getElementById("search_option2").innerHTML = sch_opt_content;


}



function setOrder(str) {
	//alert(str);
	var order_str = str.split(" ");
	orderBy = order_str[0];
	orderDir = order_str[1];
	goToPage(currentPage);
}

function setOrderBy(str) {
	if(orderBy != str) {
		orderBy = str;
	} else {
		if(orderDir == 'ASC')
			orderDir = 'DESC';
		else
			orderDir = 'ASC';
	}
	
	goToPage(currentPage);
	
}


function genListingHeader(str) {

	var list_rec= str.split("\t");
	var listingTemplate;

	getStyleObject('listing_header').visibility = 'visible';
	
	listingTemplate = 	'<table width=100% cellspacing=0 cellpadding=0 border=0 style="table-layout:fixed"><tr style="height:25px;">'+
				'<td class="prop_checked"></td>'+

				'<td style="color:#ffffff;font-weight:bold;" class="city">'+list_rec[4]+'</td>' +
				'<td style="color:#ffffff;font-weight:bold;" class="prop_type">'+list_rec[5]+'</td>' +
				'<td style="color:#ffffff;font-weight:bold;" class="bedroom">'+list_rec[6]+'</td>' +
				'<td style="color:#ffffff;font-weight:bold;" class="bathroom">'+list_rec[7]+'</td>' +
				'<td style="color:#ffffff;font-weight:bold;" class="garage">'+list_rec[8]+'</td>' +
				'<td style="color:#ffffff;font-weight:bold;" class="footage">'+list_rec[9]+'</td>' +
				'<td style="color:#ffffff;font-weight:bold;" class="pool">'+list_rec[10]+'</td>' +
				'<td style="color:#ffffff;font-weight:bold;" class="spa">'+list_rec[11]+'</td>' +
				'<td style="color:#ffffff;font-weight:bold;" class="price">'+list_rec[12]+'</td>' +
				
			'</tr></table>';

	listingTemplate += '<div id="popup_photo"  class="shadowbox3" style="z-index: 10; visibility:hidden; position:absolute; left:420px; top:300px;"></div>';

	return listingTemplate;

}

function genListingRow(i,obj)
{



	var listingRow = 	'<div style="background-color:white;" class="prop_row_outside"><div id="prop_row'+i+'" class="prop_row"  onclick="toggleAddListing('+i+',1);" onMouseOver="m_over(this,'+i+');this.style.cursor=\'pointer\';"  onMouseOut="m_leave(this,'+i+')">'+

				'<table style="width:742px; table-layout:fixed;"><tr><td class="prop_checked">'+
				'<input type="checkbox" id="prop_chkbox'+i+'"value="'+obj.agent_id+':'+obj.property_id+'"  name="prop_list"  onclick="toggleAddListing('+i+',0);" />' +
				'</td>' +

				'<td class="city">'+obj.city+'</td>' +
				'<td class="prop_type">'+obj.prop_type+'</td>' +
				'<td class="bedroom">'+obj.bedroom +'</td>' +
				'<td class="bathroom">'+obj.bathroom+'</td>' +
				'<td class="garage">'+obj.garage+'</td>' +
				'<td class="footage">'+obj.footage+'</td>' +
				'<td class="pool">'+obj.pool+'</td>' +
				'<td class="spa">'+obj.spa+'</td>' +
				'<td class="price">'+obj.price+'</td>' +
				
			'</tr></table></div></div>';
	return listingRow;
}

function toggleAddListing(i,chkbox)
{
	var cnt=0;
	var is_removed = false;
	var myobj;
	
	
	if(document.all) {
		chkbox_obj = document.all['prop_chkbox'+i];
		row_obj = document.all['prop_row'+i];
	} else {
		chkbox_obj = document.getElementById('prop_chkbox'+i);
		row_obj = document.getElementById('prop_row'+i);
	}



	if(chkbox==0) {
		if(chkbox_obj.checked)
			chkbox_obj.checked=false;
		else
			chkbox_obj.checked=true;
		return;
	}
	
	if(chkbox_obj.checked) {
		chkbox_obj.checked = false;
		row_obj.style.backgroundColor = rowColor;	
		
		removeListing(listingArray[i].property_id, listingArray[i].agent_id);

	} else {
		if(addListing(listingArray[i])== 1) {
			chkbox_obj.checked = true;
			row_obj.style.backgroundColor = rowHiColor;
		}
		
	}


}

function addListing(obj)
{

	if(seletedListingArrayIndex>=maxSelected) {
		alert("You have reached the limit of " + maxSelected+ " selections.\n\nPlease complete the form below, in order to view detials of selected properties.");
		return 0;
	}

	selectedListingArray[seletedListingArrayIndex].copy_data(obj);
	seletedListingArrayIndex++;
	
	isListingRemove = false;
	updateListing();
	return 1;
	
}


function removeListing(property_id,agent_id)
{

	//alert(property_id+','+agent_id);

	var removedListingIndex = 100;
	for(var j=0; j<seletedListingArrayIndex; j++) {
		if((agent_id == selectedListingArray[j].agent_id) && (property_id == selectedListingArray[j].property_id))
			removedListingIndex = j;
	}

	for( var j=removedListingIndex; j<seletedListingArrayIndex-1; j++) {
		selectedListingArray[j].copy_data(selectedListingArray[j+1]);
	}
	

	//document.getElementById('selected_listing'+(seletedListingArrayIndex-1)).innerHTML = '';

	seletedListingArrayIndex--;

	isListingRemove = true;
	updateListing();
}


function updateListing() {
	var mn_slistings = "";
	for(var i=0; i<seletedListingArrayIndex; i++) {
		if(i>0)
			mn_slistings += ',';

		mn_slistings += selectedListingArray[i].agent_id+':'+selectedListingArray[i].property_id;
		
		showSelectedListing(i);
		
	}
	
	if(isListingRemove)
		selectedListingArray[seletedListingArrayIndex].clear_data;


	document.frm_visitor.mn_slistings.value = mn_slistings;
	
	if (document.all){
		// IE CODE
		objCount = document.getElementById('listing_count');
		
		objCount.innerText = "Please Fill Out the Above Form to View Details of Your Selected " + seletedListingArrayIndex + " Listing(s).";
	}else{
		// MOZILLA & NETSCAPE CODE
		document.getElementById("listing_count").childNodes[0].nodeValue = "Please Fill Out the Above Form to View Details of Your Selected " + seletedListingArrayIndex + " Listing(s).";
	}
	
	//alert(document.frm_visitor.mn_slistings.value);
}

function updateSelectedRows()
{

	for (i=0; i<totalListingsCurPage; i++) {
		if(document.frm_visitor.mn_slistings.value.indexOf(document.getElementById("prop_chkbox"+i).value)>-1) {
		
			document.getElementById("prop_chkbox"+i).checked = true;
			document.getElementById("prop_row"+i).style.backgroundColor = rowHiColor;


		} else {
			document.getElementById("prop_chkbox"+i).checked = false;
			document.getElementById("prop_row"+i).style.backgroundColor = rowColor;
		}
	}

}

function clearSelection()
{
	
	for(var i=0; i<seletedListingArrayIndex; i++) {
		selectedListingArray[i].clear_data();
		document.getElementById('selected_listing'+i).innerHTML = '';
	}
	seletedListingArrayIndex=0;
	document.frm_visitor.mn_slistings.value="";
}

function showSelectedListing(i) {

	return;
	
	getStyleObject('selected_listing'+i).visibility = 'visible';

	document.getElementById('selected_listing'+i).innerHTML = 
	
		'<div style="height:134px; width:162px; padding:2px; border:#666666 1px solid; background-color:#ffffff;">'+
		
		'<div style="width:162px; height:100px; overflow:hidden; background-color:#ffffff"><img src="'+selectedListingArray[i].listing_photo+'" width=162 OnError="ImageLoadFailed(this)"></div>' +

		'<table width=162 bgcolor=#ffffff><tr>'+
			'<td style="padding-top:2px;padding-left:3px;padding-right:3px;font-weight:bold;text-align:left;">'+selectedListingArray[i].city+'</td>' +
			'<td style="padding-top:2px;text-align:right;padding-right:3px;font-weight:bold;">'+selectedListingArray[i].price+'</td>' +
		'</tr></table>'+
		'<table width=162 bgcolor=#ffffff><tr>'+
			'<td style="padding-bottom:2px;padding-left:3px;padding-right:3px;text-align:left;">'+selectedListingArray[i].bedroom+' bd, '+selectedListingArray[i].bathroom+' ba, '+selectedListingArray[i].footage+' sf</td>' +
			'<td style="padding-bottom:2px;padding-left:3px;padding-right:3px;text-align:right;"><a class="removelnk" href="javascript:removeListing(\''+selectedListingArray[i].property_id+'\',\''+selectedListingArray[i].agent_id+'\');updateSelectedRows();">remove</span></td>'+
		'</tr></table>' +
		'</div>';
		
		
}



function showPhoto(n) {

	var prop_wininfo = '<div  class="shadowcontent" style="padding:3px;border:#000000 1px solid;top: -6px; left: -5px; background-color:#ffffff;">' +
		'<div  class="img_frm2"><img src="'+listingArray[n].listing_photo+'" width="320"  OnError="ImageLoadFailed(this)"></div>' +
		'<table width=320 bgcolor=#ffffff>'+
		'<tr>'+
			'<td style="padding-top:5px;padding-left:3px;padding-right:3px;font-weight:bold;text-align:left;">'+listingArray[n].city+'</td>' +
			'<td style="padding-top:5px;text-align:right;font-weight:bold;">'+listingArray[n].price+'</td>' +
		'</tr>'+
		'<tr>'+
			'<td style="padding-bottom:5px;padding-left:3px;padding-right:3px;text-align:left;">'+listingArray[n].bedroom+' beds, '+listingArray[n].bathroom+' baths, '+listingArray[n].footage+' sqft</td>' +
			'<td style="padding-bottom:5px;padding-left:3px;padding-right:3px;"></td>'+
		'</tr>'+
		'</table></div>';

	document.getElementById("popup_photo").innerHTML = prop_wininfo;

	if(document.body.clientWidth) {
		var winW = document.body.clientWidth;
		var winH = document.body.clientHeight;

		winW = parseInt(winW-800)/2 + 180;

		getStyleObject('popup_photo').left = winW+'px';
	}

	getStyleObject('popup_photo').visibility = 'visible';

}

function hidePhoto() {
	getStyleObject('popup_photo').visibility = 'hidden';
}

function m_over(obj,i) {
	obj.style.border = '#FF9900 1px solid';
}

function m_leave(obj,i) {
	obj.style.border = '#ffffff 1px solid';
}

function showFeatureListing() {
	if(hasFeatureListing && showFeatureListingWindow) {
		var prop_wininfo = '<div  class="shadowcontent" style="padding:3px;border:#000000 1px solid;top: -6px; left: -5px; background-color:#ffffff;">' +

			'<table width=320 bgcolor=#000000><tr><td style="color:#ffffff; text-align:left; font-weight:bold;padding:3px;">Featured \'Bank Owned\' Property</td>'+
			'<td style="color:#ffffff; text-align:right; font-weight:bold;padding:3px;"><span style="color:#ffffff;font-weight:bold;" onClick="hideFeatureListing();" onMouseOver="this.style.cursor=\'pointer\';">X</span></td></tr></table>'+

			'<div  class="img_frm2"><img src="'+featureListing.listing_photo+'" width="320"  OnError="ImageLoadFailed(this)"></div>' +
			'<table width=320 bgcolor=#ffffff>'+
			'<tr>'+
				'<td style="padding-top:5px;padding-left:3px;padding-right:3px;font-weight:bold;text-align:left;">'+featureListing.city+'</td>' +
				'<td style="padding-top:5px;text-align:right;font-weight:bold;">'+featureListing.price+'</td>' +
			'</tr>'+
			'<tr>'+
				'<td style="padding-bottom:5px;padding-left:3px;padding-right:3px;text-align:left;">'+featureListing.bedroom+' beds, '+featureListing.bathroom+' baths, '+featureListing.footage+' sqft</td>' +
				'<td style="padding-bottom:5px;padding-left:3px;padding-right:3px;"></td>'+
			'</tr>'+
			'</table>';

		if(totalListings>1) {
		
			prop_wininfo += '<table width=320 bgcolor=#000000><tr><td style="color:#000000; background-color:#dddddd; text-align:left; font-weight:bold;padding:3px;padding-bottom:10px;">'+
					'There are <font style="color=#ff0000;font-weight:bold;">'+(totalListings-1)+'</font> more "'+uniqueProperty+'" properties found in this area.' +
					'</td></tr>'+

					'<tr><td style="color:#000000; background-color:#dddddd; text-align:left; font-weight:bold;padding:3px;padding-bottom:10px;">'+
					'You can select more properties from the list. And then please enter information on this page below to access the full detail on this featured property and others<br/>' +
					'</tr></table>';

		} else {

			prop_wininfo += '<table width=320 bgcolor=#000000><tr><td style="color:#000000; background-color:#dddddd; text-align:left; font-weight:bold;padding:3px;padding-bottom:10px;">'+
					'Please enter information on this page below to access the full detail on this properties and others<br/>' +
					'</td></tr></table>';
		}


		prop_wininfo += '<table width=320 bgcolor=#000000><tr><td style="color:#000000; background-color:#ffffff; text-align:center; font-weight:bold;padding:3px;">'+
				'<a style="color:#000000;" href="javascript:hideFeatureListing();">close window</a>' +
				'</td></tr></table>';

		prop_wininfo += '</div>';

		document.getElementById("popup_photo").innerHTML = prop_wininfo;

		if(document.body.clientWidth) {
			var winW = document.body.clientWidth;
			var winH = document.body.clientHeight;

			winW = parseInt(winW-800)/2 + 180;

			getStyleObject('popup_photo').left = winW+'px';
			getStyleObject('popup_photo').top = '300';
		}

		getStyleObject('popup_photo').visibility = 'visible';
	
		showFeatureListingWindow = false;
	}
}

function hideFeatureListing() {
	getStyleObject('popup_photo').visibility = 'hidden';
}



function showPageNav(num_records,rec_no) {
	var page_no=0;
	var page_nav_html = "";
	var max_nav = 11;
	var page_max = 0;
	var page_list = new Array("","","","","","","","","","","","","","","");
	
	var mid_pos = (max_nav+1)/2;
	
	page_no = (rec_no/10)+1;
	page_max = Math.ceil(num_records/10);
	


	document.getElementById("total_listings").innerHTML =
			'Your search found '+ num_records +' homes'; 

	if(page_max<max_nav)
		max_nav = page_max 
	
	if(page_no<=max_nav) {
		for(var i=1; i<=max_nav; i++) {

			//alert(page_no +'=='+ i);
			if(page_no == i) {
				page_list[i-1] = createPageLnk(i,0);
			} else {
				page_list[i-1] = createPageLnk(i,1);
			}
			

		}
	}
	
	if(page_max>max_nav) {
		page_list[max_nav-2] = '<span class="currentPage">...</span>';
		page_list[max_nav-1] = createPageLnk(page_max,1);
	}

	if(page_no>=6) {

		page_list[0] = createPageLnk(1,1);
		page_list[1] = '<span class="currentPage">...</span>';

		for(i=2; i<mid_pos; i++) {
			page_list[i] = createPageLnk(page_no-(mid_pos-i)+1,1);
		}

		page_list[mid_pos-1] = createPageLnk(page_no,0);

		for(i=mid_pos; i<max_nav-2; i++) {
			page_list[i] = createPageLnk(page_no+i-mid_pos+1,1);
		}


		page_list[max_nav-2] = '<span class="currentPage">...</span>';
		page_list[max_nav-1] = createPageLnk(page_max,1);



	}
	
	if(page_no >= (page_max-4) && page_max>max_nav) {
		for(i=max_nav; i>1; i--) {
			var page_idx = page_max-max_nav+i;
			if(page_no == page_idx) {
				page_list[i-1] = createPageLnk(page_idx,0);
			} else {
				page_list[i-1] = createPageLnk(page_idx,1);
			}
		
		
		}
		page_list[1] = '<span class="currentPage">...</span>';

	}


	
	for(var i=0; i<max_nav; i++) {
		//alert(i+'='+page_list[i]);
		page_nav_html +=  page_list[i] + '&nbsp;&nbsp;';

	}


	var previous_page = "";
	if(page_no>1) {
		
		previous_page = '<span class="page_number_btn" onclick="goToPage('+(page_no-1)+');"  onMouseOver="this.style.backgroundColor=\'#FFE978\';this.style.cursor=\'pointer\';" onMouseOut="this.style.backgroundColor=\'#ffffff\';">&nbsp;&laquo; Previous&nbsp;</span>&nbsp;&nbsp;';
	}


	var next_page = "";
	if(page_no<page_max) {
		next_page = '<span class="page_number_btn" onclick="goToPage('+(page_no+1)+');" onMouseOver="this.style.backgroundColor=\'#FFE978\';this.style.cursor=\'pointer\';" onMouseOut="this.style.backgroundColor=\'#ffffff\';">&nbsp;Next &raquo;&nbsp;</span>';
	}

	page_nav_html = previous_page + page_nav_html + next_page;
	
	document.getElementById("page_number").innerHTML = 'Page '+ currentPage + ' of '+ page_max +  '&nbsp;&nbsp;&nbsp;' +previous_page + next_page;

	document.getElementById("page_nav_bot").innerHTML = page_nav_html;
	
	getStyleObject('top_listing_header').visibility = 'visible';
	getStyleObject('page_nav_bot').visibility = 'visible';



}

function createPageLnk(page_no,status) {
	var page_lnk = "";
	if(status == 1) {
		page_lnk = '<span class="page_number" onclick="goToPage('+page_no+');" onMouseOver="this.style.backgroundColor=\''+selPageHiColor+'\';this.style.cursor=\'pointer\';" onMouseOut="this.style.backgroundColor=\''+selPageColor+'\';">'+page_no+'</span>';
	} else {
		page_lnk = '<span class="currentPage">' + page_no + '</span>';
	}
	
	return page_lnk;
}


function handleHttpResponse() {


  if (http.readyState == 4) {
  

			hideLoadingMsg()

			totalListings = parseInt(http.responseXML.getElementsByTagName("listing_summary")[0].getAttribute("num_records"));
			
			if(totalListings==0 && hasFeatureListing)
				totalListings = 1;
			
			showPageNav(totalListings,parseInt(http.responseXML.getElementsByTagName("listing_summary")[0].getAttribute("rec_no")));

			//document.getElementById('debug2').innerText = http.responseXML.getElementsByTagName("debug")[0].getAttribute("sql");
			//alert(http.responseXML.getElementsByTagName("debug")[0].getAttribute("sql"));

			
			column_header = http.responseXML.getElementsByTagName("column_header");

			listings = http.responseXML.getElementsByTagName("listing");

			var cur_listing_source = http.responseXML.getElementsByTagName("listing_source")[0].firstChild.data
			
			if(listing_source != cur_listing_source && cur_listing_source !="--") {

				listing_source = cur_listing_source;
				document.getElementById("disclaimer").innerHTML = http.responseXML.getElementsByTagName("disclaimer")[0].firstChild.data;
				genSearchOption(http.responseXML.getElementsByTagName("home_feature")[0].firstChild.data,http.responseXML.getElementsByTagName("comm_feature")[0].firstChild.data);
			
			}
			
			


			
			if(listings.length>0 || hasFeatureListing) {
				var listing_content= '';

				document.getElementById("listing_header").innerHTML = genListingHeader(column_header[0].firstChild.data);

				var listingIndex =0;

				if(currentPage==1) {

					if(hasFeatureListing) {
						
						var uniquePropertyWithQuote = '';
						if(uniqueProperty != '')
							uniquePropertyWithQuote = '"' + uniqueProperty + '"';
						
						listing_content = '<div style="background-color:#EFEFF4; width:744px; color:#000000;font-weight:bold;padding:3px;text-align:left;">&nbsp;&nbsp;Featured '+uniquePropertyWithQuote+' Property</div>';

						listingArray[listingIndex].copy_data(featureListing);
						listingRow = genListingRow(listingIndex,listingArray[listingIndex]);
						listing_content += listingRow;
						listingIndex++;


												
						if(uniqueProperty != '') {
							var keyword = document.searchform.keyword[document.searchform.keyword.selectedIndex].value;

							if(listings.length>0) {
								if(uniqueProperty == keyword) 
									if(listings.length>1)
										listing_content += '<div style="background-color:#EFEFF4; width:744px; color:#000000;font-weight:bold;padding:3px;text-align:left;">&nbsp;&nbsp;More result on "'+keyword+'" Properties</div>';
								else
									listing_content += '<div style="background-color:#EFEFF4; width:744px; color:#000000;font-weight:bold;padding:3px;text-align:left;">&nbsp;&nbsp;Search result on "'+keyword+'" Properties</div>';

							} else {
								listing_content += '<div style="background-color:#EFEFF4; width:744px; color:#000000;font-weight:bold;padding:3px;text-align:left;">&nbsp;&nbsp;No listing found for "'+keyword+'" Properties</div>';
							}
						} else {
							if(listings.length>0) {
								listing_content += '<div style="background-color:#EFEFF4; width:744px; color:#000000;font-weight:bold;padding:3px;text-align:left;">&nbsp;&nbsp;Search result from your search criteria above</div>';

							} else {
								listing_content += '<div style="background-color:#EFEFF4; width:744px; color:#000000;font-weight:bold;padding:3px;text-align:left;">&nbsp;&nbsp;No listing found from your search criteria</div>';
							}
						}
					}
				}




				for (var i = 0; i < listings.length; i++) {
				
					var list_rec= listings[i].firstChild.data.split("\t");


					if(hasFeatureListing) {
						if(featureListing.agent_id==list_rec[2] && featureListing.property_id==list_rec[1])
							continue;
					}

					listingArray[listingIndex].property_id = list_rec[1];
					listingArray[listingIndex].agent_id = list_rec[2];
					listingArray[listingIndex].city = list_rec[4];
					listingArray[listingIndex].prop_type = list_rec[5];
					listingArray[listingIndex].bedroom = list_rec[6];
					listingArray[listingIndex].bathroom = list_rec[7];
					listingArray[listingIndex].garage = list_rec[8];
					listingArray[listingIndex].footage = list_rec[9];
					listingArray[listingIndex].pool = list_rec[10];
					listingArray[listingIndex].spa = list_rec[11];
					listingArray[listingIndex].price = list_rec[12];
					listingArray[listingIndex].brokerlogo_sm = list_rec[13];
					listingArray[listingIndex].listing_photo = list_rec[3];


					listingRow = genListingRow(listingIndex,listingArray[listingIndex]);
					listing_content += listingRow;

					listingIndex++;
				}
				
				totalListingsCurPage = listingIndex;


				document.getElementById("listing_content").innerHTML = listing_content;

				updateListing();
				updateSelectedRows();
				
				showFeatureListing();
				
				
				
			} else {
				getStyleObject('top_listing_header').visibility = 'hidden';
				getStyleObject('listing_header').visibility = 'hidden';
				getStyleObject('page_nav_bot').visibility = 'hidden';
			
				document.getElementById("listing_content").innerHTML =
				
					'<table><tr><td class="blktxtbld11" style="text-align:left;">'+

					'<br/><br/>No properties found. Please change your criteria.</b><br/><br/><br/>'+

					'Even though no properties matched my criteria,'+
					'<br/>'+
					'please send listings on homes/condos in the surrounding areas.</br/></br/>'+

					'<input type=checkbox name=notify value=yes checked>&nbsp;'+

					'Yes, I am entering my information on the next screen.&nbsp;&nbsp;&nbsp;&nbsp;<i>Thank you!</i>'+

					'<br><br><br>'+

					'</td></tr></table>';

				
				
			}
			
			isNewSearch = false;

  }

}

function readData(url) {

  http.open("GET", url, true);
  http.onreadystatechange = handleHttpResponse;
  http.send(null);

}

function goToPage(page_no) {


	showLoadingMsg();

	currentPage = page_no;

	var rec_no = (page_no-1)*10;
	
	//document.getElementById('debug1').innerText = query_url + '&rec=' + rec_no;

	if(isNewSearch) {
		readData(query_url + '&rec=' + rec_no + '&order_by='+ orderBy + '&order_dir='+ orderDir);
		//document.getElementById('debug2').innerText = query_url + '&rec=' + rec_no + '&order_by='+ orderBy + '&order_dir='+ orderDir;

	} else {
		readData(query_url + '&rec=' + rec_no + '&cnt_recs=' +totalListings + '&order_by='+ orderBy + '&order_dir='+ orderDir);
		//document.getElementById('debug2').innerText = query_url + '&rec=' + rec_no + '&cnt_recs=' +totalListings + '&order_by='+ orderBy + '&order_dir='+ orderDir;
	}
	
}

function showLoadingMsg() {
	if(document.body.clientWidth) {
		var winW = document.body.clientWidth;
		var winH = document.body.clientHeight;

		winW = parseInt(winW*0.4);

		getStyleObject('loader_msg').width = winW+'px';
	}
	getStyleObject('loader_msg').visibility = 'visible';
}

function hideLoadingMsg() {
	getStyleObject('loader_msg').visibility = 'hidden';
}

function init() {

	showLoadingMsg();
	submitSearch();
}

function newSearch() {
	if(c == 'vis'){
		self.location='#ol_result';
	}
	submitSearch();

}

function submitSearch() {

	showLoadingMsg();

	isNewSearch = true;
	
	selected_home_features = '';
	if(document.searchform.home_feature != null) {

		for(var i=0; i<document.searchform.home_feature.length; i++) {
				if(document.searchform.home_feature[i].checked) {
					if(selected_home_features != '') 
						selected_home_features += ',';
					selected_home_features += document.searchform.home_feature[i].value;
				}
		}
	}

	selected_comm_features = '';
	if(document.searchform.comm_feature != null) {
	
		for(var i=0; i<document.searchform.comm_feature.length; i++) {
				if(document.searchform.comm_feature[i].checked) {
					if(selected_comm_features != '') 
						selected_comm_features += ',';
					selected_comm_features += document.searchform.comm_feature[i].value;
				}
		}
	}

	query_url = searchUrl +
			'&terr_d='+ document.searchform.terr_d.value +
			'&state='+ document.searchform.state.value +
			'&type='+ document.searchform.type.value +
			'&bedroom1='+ document.searchform.bedroom1.value +
			'&bathroom1='+ document.searchform.bathroom1.value +
			'&price='+ document.searchform.price1.value + ','+ document.searchform.price2.value+
			'&footage1='+ document.searchform.footage1.value +
			'&footage2='+ document.searchform.footage2.value +
			'&lotrange='+ document.searchform.lotrange.value +
			'&year_built='+ document.searchform.year_built.value +
			'&keyword='+ document.searchform.keyword.value +
			zipPara +
			'&home_feature='+ selected_home_features+
			'&comm_feature='+ selected_comm_features;


	if(document.searchform.days.checked) {
		query_url += '&days='+ document.searchform.days.value;
	}
	
	//document.getElementById('debug1').innerText = query_url;

	//alert(query_url);	
	
	
	if(resetSelection) {
		clearSelection();
	}

	document.frm_visitor.minprice.value = document.searchform.price1.value;
	document.frm_visitor.maxprice.value = document.searchform.price2.value;
	document.frm_visitor.cityname.value = document.searchform.terr_d.value;

	document.frm_visitor.prop_type.value = document.searchform.type.value;
	document.frm_visitor.min_br.value = document.searchform.bedroom1.value;
	document.frm_visitor.min_ba.value = document.searchform.bathroom1.value;

	document.frm_visitor.keyword.value = document.searchform.keyword.value;
	
	
	if (document.all){
		// IE CODE
		objCount = document.getElementById('listing_count');
		objCount.innerText="You have selected 0 Listings."
	}else{
		// MOZILLA & NETSCAPE CODE
		document.getElementById("listing_count").childNodes[0].nodeValue="You have selected 0 Listings."
	}


	goToPage(1);
	
	
	
}

function getHTTPObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return false;
}

var http = getHTTPObject();

var c = 'invis';
var ids = new Array('id1', 'id2'); // IDs of your <div>s

function doSwap() {
	if(c == 'vis'){
		c = 'invis'
		document.getElementById('search_option_lnk').innerHTML = '&raquo; Show More Search Options';
	} else {
		c = 'vis';
		document.getElementById('search_option_lnk').innerHTML = '&laquo; Hide More Search Options';
	}

	//c == 'vis' ? c = 'invis' : c = 'vis';
	
	document.getElementById('search_option').className = c;
}

function checkForm() {
	if(totalListings>0 && document.frm_visitor.mn_slistings.value == '') {	
		alert('Please select at least one property.');
		return false;
	} else {
		return true;
	}
	
}


function formCheck(formobj){
var fieldRequired = Array("mn_slistings", "v_fname", "v_lname", "v_email", "v_ephone");
var fieldDescription = Array("Select At Least One Property", "First Name", "Last Name", "Email Address", "Phone");
var alertMsg = "Please complete the following required fields:\n\n";	
var l_Msg = alertMsg.length;	
for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
if (obj.type == null){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){ alertMsg += " - " + fieldDescription[i] + "\n"; }
continue;
}
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){ alertMsg += " - " + fieldDescription[i] + "\n"; }
break;
case "select-multiple":
if (obj.selectedIndex == -1){ alertMsg += " - " + fieldDescription[i] + "\n"; }
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){ alertMsg += " - " + fieldDescription[i] + "\n"; }
break;
case "hidden":
if (totalListings > 0 && obj.value == "" || obj.value == null){ alertMsg += " - " + fieldDescription[i] + "\n"; }
break;
default:
}}}
if (alertMsg.length == l_Msg){
return true;
}
else{ alert(alertMsg); 
return false;
}}


function show_user_agreement(listing_source) {
	if(checkForm()) {
		var accept=true;
		confirmWin=open('http://househunt.org/user_agreement/'+listing_source+'.html','confirmWindow','scrollbars=yes,toolbar=no,location=no');
		confirmWin.focus();
		return accept;
	} else {
		return false;
	}
}

