﻿var xmlHttp
var propertyArray = new Array();
var sortFld = ""; 
var fill = "";
var page = 1;
var sortDir=0;

function updatemap(str){
//	    document.getElementById("hourglass").style.visibility = "visible";
//	    document.getElementById("hourglass").style.display = "inline";

	if (str.length==0){ document.getElementById("txtCount").innerHTML="Theres is nothing to show";
  		return;
  	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){	alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url="ajax/getpins.php";
	url=url+"?base="+str;
	url=url+"&sid="+Math.random();
	//alert(url);
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function GetXmlHttpObject(){
	var xmlHttp=null;
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e){
  		// Internet Explorer
  		try{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
 		}
  		catch (e){
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return xmlHttp;
}

function stateChanged() { 
	if (xmlHttp.readyState==4){ 
	//alert(xmlHttp.responseText);
	document.getElementById("txtList").innerHTML="";
	document.getElementById("loading").innerHTML="";
	map.DeleteAllShapes();
	
	var pin = eval('(' + xmlHttp.responseText + ')');
		if(pin.house.length>0){
			$cnt=1;
			if(propertyArray.length>0) { propertyArray.length=0;}
			for ($i = 0; $i < pin.house.length; $i++) {
				if(pin.house[$i].lat!=0 && pin.house[$i].lon!=0) { // no lat long data found
				//alert(pin.house[$i].adno);
					propertyArray[propertyArray.length] = new createProperty(
						$cnt,
						pin.house[$i].adno,
						pin.house[$i].baseid,
						pin.house[$i].agentid,
						pin.house[$i].agent_cur,
						pin.house[$i].agentname,
						pin.house[$i].pcsmbr,
						pin.house[$i].rltrpix,
						pin.house[$i].r_pixw,
						pin.house[$i].r_pixh,
						pin.house[$i].brkrpix,
						pin.house[$i].b_pixw,
						pin.house[$i].b_pixh,						
						pin.house[$i].realtor,
						pin.house[$i].broker,
						pin.house[$i].brokerage,
						pin.house[$i].r_hp,
						pin.house[$i].r_bp,						
						pin.house[$i].price,
						pin.house[$i].rental,
						pin.house[$i].beds,
						pin.house[$i].baths,
						pin.house[$i].year,
						pin.house[$i].homepix,
						pin.house[$i].pixw,
						pin.house[$i].pixh,
						pin.house[$i].sqft,
						pin.house[$i].acres,
						pin.house[$i].address1,
						pin.house[$i].city,
						pin.house[$i].zip,
						pin.house[$i].lat,
						pin.house[$i].lon,
						pin.house[$i].dist						
					)

					pinClass = (pin.house[$i].agentid==0 && pin.house[$i].adno.indexOf("nls")==-1)? 'pinFSBO':'pinRealtor';
					var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(pin.house[$i].lat, pin.house[$i].lon));
					shape.SetCustomIcon("<div class='"+pinClass+"'><div class='text'>"+$cnt+"</div></div>");    
					map.AddShape(shape);
					
					/**** BEGIN PRELOAD HOME IMAGES ******
					*******************************/
					//alert(propertyArray[$i].adno);
					if(pin.house[$i].homepix != "" ){	// THEN LET'S PRELOAD PICTURES!
						 if (document.images)
						   {
							 //alert(propertyArray[$i].adno.indexOf("nls"));
							 	if(propertyArray[$i].adno.indexOf("nls")<0) {
									propertyArray[$i].homepix= new Image();
							 		propertyArray[$i].homepix.src='http://www.MilitaryFSBO.com/adpics/'+propertyArray[$i].adno + '.jpg?'+stamp;    		
								}else{
									//propertyArray[$i].homepix= new Image();
							 		//propertyArray[$i].homepix.src=pin.house[$i].homepix+'?'+stamp;  	
								}
									

							 //alert('after');
							 //propertyArray[$i].homepix.src='adpics/'+propertyArray[$i].adno + '.jpg?'+stamp;  		
						   }
					}
					/**** END PRELOAD HOME IMAGES ******
					*******************************/

					$cnt++;
				}
			}	
			// *****  BUILDS THE propertyArray OBJECT AND THEN PUTS IT ON THE PAGE WITH sorter() ***********
//alert(sortDir);
//sortDir=(sortFld)? ((sortDir)? 0:1):0;
//alert(sortDir);
			sorter(sortFld,1);
		}else{	// Returned ZERO homes
			alert('Sorry!\n\nWe found no homes meeting your search criteria.\nPlease consider refining your search and try again.');
		}
		document.getElementById("homesCount").innerHTML	= pin.house.length;
	}
}

createProperty = function(ID,ADNO,BASEID,AGENTID,AGENT_CUR,AGENTNAME,PCSMBR,RLTRPIX,R_PIXW,R_PIXH,BRKRPIX,B_PIXW,B_PIXH,REALTOR,BROKER,BROKERAGE,R_HP,R_BP,PRICE,RENTAL,BEDS,BATHS,YEAR,HOMEPIX,PIXW,PIXH,SQFT,ACRES,ADDRESS1,CITY,ZIP,LAT,LON,DIST){
    this.id = ID;
    this.adno = ADNO;
    this.baseid = BASEID;
    this.agentid = AGENTID;
    this.agent_cur = AGENT_CUR;
    this.agentname = AGENTNAME;
    this.pcsmbr = PCSMBR;
    this.rltrpix = RLTRPIX;
    this.r_pixw = R_PIXW;
    this.r_pixh = R_PIXH;
    this.brkrpix = BRKRPIX;
    this.b_pixw = B_PIXW;
    this.b_pixh = B_PIXH;
	this.realtor = REALTOR;
	this.broker = BROKER;
	this.brokerage = BROKERAGE;
	this.r_hp = R_HP;
	this.r_bp = R_BP;
	this.price = PRICE;
	this.rental = RENTAL;
    this.beds = BEDS;
    this.baths = BATHS;
    this.year = YEAR;
    this.homepix = HOMEPIX;
    this.pixw = PIXW;
    this.pixh = PIXH;
    this.sqft = SQFT;
    this.acres = ACRES;
    this.address1 = ADDRESS1;
    this.city = CITY;
    this.zip = ZIP;
    this.lat = LAT;
    this.lon = LON;
    this.dist = DIST;
}


function sorter(fld,newlist) {
	switch (fld) {
	case "adno":
		var adno = function(a, b){return (b.adno < a.adno) - (a.adno < b.adno); };
		if(sortFld=="adno") {
			if(newlist==1) { sortDir=(sortDir==1)? 0:1; propertyArray.sort(adno);}
			if(sortDir==0) {propertyArray.reverse(adno);sortDir=1;} else {propertyArray.sort(adno);sortDir=0;}
		} else {
			propertyArray.sort(adno);sortDir=0;
		}			
		sortFld = "adno";
		break;
	case "city":
		var city = function(a, b){return (b.city < a.city) - (a.city < b.city); };
		if(sortFld=="city") {
			if(newlist==1) { sortDir=(sortDir==1)? 0:1; propertyArray.sort(city);}
			if(sortDir==0) {propertyArray.reverse(city);sortDir=1;} else {propertyArray.sort(city);sortDir=0;}
		} else {
			propertyArray.sort(city);sortDir=0;
		}			
		sortFld = "city";
		break;
	case "price":
		var price = function(a,b){return (a.price - b.price);};
		if(sortFld=="price") {
			if(newlist==1) { sortDir=(sortDir==1)? 0:1; propertyArray.sort(price);}
			if(sortDir==0) {propertyArray.reverse(price);sortDir=1;} else {propertyArray.sort(price);sortDir=0;}
		} else {
			propertyArray.sort(price);sortDir=0;
		}			
		sortFld = "price";
		break;
	case "beds":
		var beds = function(a, b){return (b.beds < a.beds) - (a.beds < b.beds); };
		if(sortFld=="beds") {
			if(newlist==1) { sortDir=(sortDir==1)? 0:1; propertyArray.sort(beds);}
			if(sortDir==0) {propertyArray.reverse(beds);sortDir=1;} else {propertyArray.sort(beds);sortDir=0;}
		} else {
			propertyArray.sort(beds);sortDir=0;
		}			
		sortFld = "beds";
		break;
	case "baths":
		var baths = function(a, b){return (b.baths < a.baths) - (a.baths < b.baths); };
		if(sortFld=="baths") {
			if(newlist==1) { sortDir=(sortDir==1)? 0:1; propertyArray.sort(baths);}
			if(sortDir==0) {propertyArray.reverse(baths);sortDir=1;} else {propertyArray.sort(baths);sortDir=0;}
		} else {
			propertyArray.sort(baths);sortDir=0;
		}			
		sortFld = "baths";
		break;
	case "sqft":
		var sqft = function(a, b){return (a.sqft - b.sqft); };
		if(sortFld=="sqft") {
			if(newlist==1) { sortDir=(sortDir==1)? 0:1; propertyArray.sort(sqft);}
			if(sortDir==0) {propertyArray.reverse(sqft);sortDir=1;} else {propertyArray.sort(sqft);sortDir=0;}
		} else {
			propertyArray.sort(sqft);sortDir=0;
		}			
		sortFld = "sqft";
		break;
	case "acres":
		var acres = function(a, b){return (a.acres - b.acres); };
		if(sortFld=="acres") {
			if(newlist==1) { sortDir=(sortDir==1)? 0:1; propertyArray.sort(acres);}
			if(sortDir==0) {propertyArray.reverse(acres);sortDir=1;} else {propertyArray.sort(acres);sortDir=0;}
		} else {
			propertyArray.sort(acres);sortDir=0;
		}			
		sortFld = "acres";
		break;
	case "dist":
		var dist = function(a, b){return (a.dist - b.dist); };
		if(sortFld=="dist") {
			if(newlist==1) { sortDir=(sortDir==1)? 0:1; propertyArray.sort(dist);}
			if(sortDir==0) {propertyArray.reverse(dist);sortDir=1;} else {propertyArray.sort(dist);sortDir=0;}
		} else {
			propertyArray.sort(dist);sortDir=0;
		}			
		sortFld = "dist";
		break;
	default:
		var id = function(a, b){return (b.id < a.id) - (a.id < b.id);};
		if(sortFld=="id") {
			if(newlist==1) { sortDir=(sortDir==1)? 0:1; propertyArray.sort(id);}  // Reason we do this is if a new list, but already had sorted, we need to resort new list before we try to reverse sort
			if(sortDir==0) {propertyArray.reverse(id);sortDir=1;} else {propertyArray.sort(id);sortDir=0;}
		} else {
			propertyArray.sort(id);sortDir=0;
		}			
		sortFld = "id";
		break;
	}
	buildTable(1);
}  

function stripZeros(fld) {
	fld2=fld;
	for(var i= fld.lastIndexOf("."); i<fld.length; i++) {
		if (fld2.substring(fld2.length-1,fld2.length)==0) {
			fld2=fld2.substring(0,fld2.length-1);
		} else if (fld2.substring(fld2.length-1,fld2.length)==".") {
			fld2=fld2.substring(0,fld2.length-1);
			return fld2;
		} else {
			return fld2;
		}
	}
	return fld2;
}

  
function buildTable(page) {
lpg = 10; // Listings Per Page
nxtPage = (page*1+1);
start = (page*lpg-lpg);
end = (start+lpg>propertyArray.length-1)?propertyArray.length-1:start+lpg-1;
pages = Math.ceil(propertyArray.length/lpg);

sortI = (sortFld=="id")? "class=\"sortClassSelected\"":"class=\"sortClass\"";
sortC = (sortFld=="city")? "class=\"sortClassSelected\"":"class=\"sortClass\"";
sortP = (sortFld=="price")? "class=\"sortClassSelected\"":"class=\"sortClass\"";
sortB = (sortFld=="beds")? "class=\"sortClassSelected\"":"class=\"sortClass\"";
sortBa = (sortFld=="baths")? "class=\"sortClassSelected\"":"class=\"sortClass\"";
sortS = (sortFld=="sqft")? "class=\"sortClassSelected\"":"class=\"sortClass\"";
sortAc = (sortFld=="acres")? "class=\"sortClassSelected\"":"class=\"sortClass\"";
sortAd = (sortFld=="dist")? "class=\"sortClassSelected\"":"class=\"sortClass\"";
sortA = (sortFld=="adno")? "class=\"sortClassSelected\"":"class=\"sortClass\"";


pgCtr= "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><form name=\"badform\" id=\"badform\">"
		+"<tr class=\"small\">"
		+"<td style=\"font-size:11px;\" align=\"left\" class=\"white\">"
		+"<a href=\"getadlist.php?base="+varBasenum+"\" class=\"small\">View homes by List</a></td>"
		+"<td style=\"font-size:11px;\" align=\"right\" class=\"white\">";

pgCtr+=(page>1)?"<a href=\"#\" onclick=\"buildTable('"+(page-1)+"'); return false;\" style=\"text-decoration:none;font-size:11px;\">&lt;&lt;Back</a> &nbsp;&nbsp;":"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;";

pgCtr+="Listing <b>"+(start+1)+"-"+(end+1)+"</b> of <b>"+propertyArray.length+" </b>Total";
pgCtr+=(pages>1 && page!=pages)?"<a href=\"#\" onclick=\"buildTable('"+nxtPage+"'); return false;\" style=\"text-decoration:none;font-size:11px;\"> &nbsp;&nbsp;Next&gt;&gt;</a>":"";

if(pages>1) {
	pgCtr+="&nbsp;&nbsp;&nbsp;&nbsp;Go to Page <select name='pager' onchange=\"javascript:buildTable(this.value); return false;\" style=\"font-size:9px;\">";
	for ($i = 1; $i<=pages; $i++) {
		selected = (page==$i)? "Selected":"";
		pgCtr+="<option value="+$i+" "+selected+">"+$i;
	}
	pgCtr+="</select>";
}
pgCtr = pgCtr	+"</td></form></tr></table>";




pgCtr += "<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">"
		+"<tr><td colspan=\"8\" align=\"right\" class=\"white\">";





//  *******************************************************************************************

text_id 	= (sortFld=='id')? ((sortDir==0)? 'ID▼':'ID▲'):'ID';
text_city 	= (sortFld=='city')? ((sortDir==0)? 'City▼':'City▲'):'City';
text_price 	= (sortFld=='price')? ((sortDir==0)? 'Price/Rent▼':'Price/Rent▲'):'Price/Rent';
text_beds 	= (sortFld=='beds')? ((sortDir==0)? 'Beds▼':'Beds▲'):'Beds';
text_baths 	= (sortFld=='baths')? ((sortDir==0)? 'Baths▼':'Baths▲'):'Baths';
text_sqft 	= (sortFld=='sqft')? ((sortDir==0)? 'Sq.Ft.▼':'Sq.Ft.▲'):'Sq.Ft.';
text_acres 	= (sortFld=='acres')? ((sortDir==0)? 'Acres▼':'Acres▲'):'Acres';
text_dist 	= (sortFld=='dist')? ((sortDir==0)? 'Miles to Base▼':'Miles to Base▲'):'Miles to Base';
text_adno 	= (sortFld=='adno')? ((sortDir==0)? 'Adno▼':'Adno▲'):'Adno';
	fill = pgCtr	+"</td></tr>"
		+"<tr style=\"font-weight:bold;\" bgcolor=\"#DDDDDD\"  bordercolor=\"#dddddd\">"
		+"<th width=\"23\"><a "+sortI+" href=\"#\" onclick=\"sorter('id');return false;\">"+text_id+"</a></th>"
		+"<th><a "+sortC+" href=\"#\" onclick=\"sorter('city');return false;\">"+text_city+"</a></th>"
		+"<th><a "+sortP+" href=\"#\" onclick=\"sorter('price');return false;\">"+text_price+"</a></th>"
		+"<th><a "+sortB+" href=\"#\" onclick=\"sorter('beds');return false;\">"+text_beds+"</a></th>"
		+"<th><a "+sortBa+" href=\"#\" onclick=\"sorter('baths');return false;\">"+text_baths+"</a></th>"
		+"<th><a "+sortS+" href=\"#\" onclick=\"sorter('sqft');return false;\">"+text_sqft+"</a></th>"
		+"<th><a "+sortAd+" href=\"#\" onclick=\"sorter('dist');return false;\">"+text_dist+"</a></th>"
		+"<th><a "+sortA+" href=\"#\" onclick=\"sorter('adno');return false;\">"+text_adno+"</a></th>"
		+"</tr>";




	for ($i = start; $i <= end; $i++) {
$priceShow = stripStuff(propertyArray[$i].price);
$acresShow = (propertyArray[$i].acres==0)?'-':stripZeros(propertyArray[$i].acres);
$commuteShow = (propertyArray[$i].dist==0)?'-':stripZeros(propertyArray[$i].dist);
$sqftShow = (propertyArray[$i].sqft==0)?'-':propertyArray[$i].sqft+"'";

		pinClass = (propertyArray[$i].agentid==0 && propertyArray[$i].adno.indexOf("nls")==-1)? 'pinList_FSBO':'pinList_Realtor';
		var ss = "onclick='mapObj.iconPress("+($i+1)+",\"house\","+propertyArray[$i].lat+","+propertyArray[$i].lon+")'";
		fill = fill +"<tr style=\"cursor:hand;\" onclick=\"javascript:popWinAd('getad.php?adno="+propertyArray[$i].adno+"')\"; align=\"center\" "+ss+" onmouseover=\"this.className='yellowbg';mapPopup.hidePopup();mapObj.iconPress("+($i+1)+",'house',"+propertyArray[$i].lat+","+propertyArray[$i].lon+");\" onmouseout=\"mapPopup.hidePopup();this.className='stdbg"+($i %2)+"';\" class=\"stdbg"+($i %2)+"\">"	
			+"<td "+sortI+"><div class='"+pinClass+"'><div class='text'>"+propertyArray[$i].id+"</div></div></td>"
			+"<td "+sortC+">"+propertyArray[$i].city+"&nbsp;</td>";
			if(propertyArray[$i].rental==1) {
				fill=fill +"<td "+sortP+">("+formatCurrency($priceShow)+" Rent)&nbsp;</td>";
			}else{
				fill=fill +"<td "+sortP+">"+formatCurrency($priceShow)+"&nbsp;</td>";
			}

		fill = fill +"<td "+sortB+">"+propertyArray[$i].beds+"&nbsp;</td>"
			+"<td "+sortBa+">"+stripZeros(propertyArray[$i].baths)+"&nbsp;</td>"
			+"<td "+sortS+">"+$sqftShow+"&nbsp;</td>"
			+"<td "+sortAc+">"+$commuteShow+"&nbsp;</td>"
			+"<td "+sortA+">"+propertyArray[$i].adno.replace('nls',"")+"&nbsp;</td>"
			+"</tr>";
			
			if($i=='dontshow') fill = fill +"<tr align=\"center\" onmouseover=\"this.className='yellowbg';mapPopup.hidePopup();mapObj.iconPress("+($i+1)+",'house',"+propertyArray[$i].lat+","+propertyArray[$i].lon+");\" onmouseout=\"mapPopup.hidePopup();this.className='spotlite';\" class=\"spotlite\">"
			+ "<td colspan=8 onclick=\"javascript:popWinAd('getad.php?adno="+propertyArray[$i].adno+"')\"; style=\"padding:5px;\"><img src=\"" + propertyArray[$i].homepix + "\" width=\""+(propertyArray[$i].pixw*.8)+"\" height=\""+(propertyArray[$i].pixh*.8)+"\" style=\"border:solid 1px #cccccc;float:left;\">"
			+ "<img src=\"" + propertyArray[$i].rltrpix + "\" width=\""+(propertyArray[$i].r_pixw*.53)+"\" height=\""+(propertyArray[$i].r_pixh*.53)+"\" style=\"border:solid 1px #cccccc;float:right;\">"
			+ "<table border=0 width=596 style=\"border:solid 1px #999999;\">"
			+ "<tr><td rowspan=2><img src=\"images/spacer.gif\" width=1 height=90></td>"
			+ "<td colspan=2 style=\"font-family:Georgia, Times New Roman, Times, serif; font-size:24px;\">Spotlight Ad</td></tr>"
			+ "<tr><td style=\"font-family: Arial, Helvetica, sans-serif; font-size: 14px; \"><b>"+propertyArray[$i].address1 + "</b><br>"
			+ propertyArray[$i].city+" "+propertyArray[$i].zip+"</td>"
			+ "<td style=\"font-family: Arial, Helvetica, sans-serif; font-size: 14px; \">"
			+ "Listed by:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>"
			+ "<b>"+propertyArray[$i].agentname + "</b><br>"
			+ propertyArray[$i].brokerage
			+ "</td></tr></table>"
			+ "</td></tr>";


	} fill = fill + '</table>';
	document.getElementById("txtList").innerHTML	= fill;
	
			//+"<td "+sortI+"><a href='getad.php?adno="+propertyArray[$i].adno+"'><div class='"+pinClass+"'><div class='text'>"+propertyArray[$i].id+"</div></div></a></td>"
			//+"<td "+sortI+"><a href='getad.php?adno="+propertyArray[$i].adno+"'><div class='"+pinClass+"'><div class='text'>"+propertyArray[$i].id+"</div></div></a></td>"


}

