var formIdF= "form1"; //Id of the form for featured Search
var formIdS= "form3"; //Id of the form for general Search
var idivSearch= "apDiv140"; //Id of the div for general Search results
var classFeatConDiv = "featCon";
var classFeatTxtDiv = "featTxt";
var classFeatImage = "imgFeat";

var searRow = '<tr>'
				+'<td bgcolor="#e8ecff" style="width:115px;"><img class="imgRental" style="margin:1px;height:84px;width:113px;" src=""></td>'
				+'<td bgcolor="#e8ecff" style="font-size: 12px;">'
					+'<table border="0"><tbody>'
						+'<tr>'
							+'<td style="font-size: 12px; font-weight: bold;" class="rowTitle">Title</td>'
						+'</tr>'
						+'<tr>'
							+'<td style="font-size: 12px;" class="rowAddr">Addr</td>'
						+'</tr>'					
						+'<tr>'
							+'<td style="font-size: 12px;" class="rowCost">Cost</td>'
						+'</tr>'
						+'<tr>'
							+'<td style="font-size: 12px;" class="rowInfo">Rest</td>'
						+'</tr>'
					+'</tbody></table>'
				+'</td>'
				+'<td bgcolor="#e8ecff" style="font-size: 12px; text-align: center; color: rgb(255, 0, 0);"><p class="rowInq"></p><p class="rowID"></p></td>'
              +'</tr>';
			  
var blnkRow = '<tr><td>&nbsp;</td><td style="font-size: 12px;">&nbsp;</td><td style="font-size: 12px;">&nbsp;</td></tr>';		
var searTable = "";

$(document).ready(function(){
	
	//When Page inistialises, Display the Featured ADs
	_getFeatures();
	
	//When Page inistialises, Display all of the non-Featured ADs
	_getSearches();
	
	//When Featured ADs are searched
	$("#"+formIdF).submit(function(){
		_getFeatures();
		return false;
	});	
	
	//When Searched ADs
	$("#"+formIdS).submit(function(){
		_getSearches();
		return false;
	});
});

function _getFeatures(){
	$("."+classFeatConDiv).hide();
	$.ajax({
		type: "POST",
		url: "search.php",
		data:$("#"+formIdF).serialize()+"&action=featureds",
		cache: false,
		dataType: "xml",
		success: function(x){
			//For each returned feature			
			$("feat",x).each(function(i){
				var id=$("id",this).text();
				var conDiv = $("."+classFeatConDiv).eq(i);
				var txtDiv = $("."+classFeatTxtDiv).eq(i);
				var imgDiv = $("."+classFeatImage).eq(i);
				
				conDiv.show();//The container div is initially hidden
				
				//Address of the property
				txtDiv.text($("addr",this).text());
				txtDiv.append("<br/>"+$("city",this).text()+", "+$("state",this).text());
				
				//Details link
				txtDiv.append("<br/><a target='_self' style=\"font-size: 12px; text-align: center; color:#0000FF;\" href='detailed.php?id="+$("id",this).text()+"'>Details</a>");

				//Image for the ad
				imgDiv.attr("src","getimage.php?file="+$("file",this).text()+"&typerandval="+ Math.random());
			});				
		}
	});
}

function _getSearches(){
	searTable = $("#"+idivSearch+" table");
	searTable.empty();
	$.ajax({
		type: "POST",
		url: "search.php",
		data:$("#"+formIdS).serialize()+"&action=searchRental",
		cache: false,
		dataType: "xml",
		success: function(x){
			//For each search returned
			$("rent",x).each(function(i){
				
				//Add a table row
				searTable.append(blnkRow);
				var zr=searTable.append(searRow);
				$(".imgRental:last",searTable).attr("src","getimage.php?file="+$("file",this).text()+"&typerandval="+ Math.random());
				$(".rowTitle:last",zr).text($("title",this).text());				
				$(".rowAddr:last",zr).text($("addr",this).text());				
				
				var rentTxt="";
				if($("dayC",this).text()!=0){
					rentTxt = "$"+$("dayC",this).text()+" per Day";
				}
		
				if($("dayM",this).text()!=0){
					if(rentTxt!=""){rentTxt += ", ";}	
					rentTxt += "$"+$("dayM",this).text()+" per Month";
				}
				$(".rowCost:last",zr).text(rentTxt);		
								
				$(".rowInq:last",zr).append("<a target='_self' style=\"font-size: 12px; text-align: center; color:#0000FF;\" href='detailed.php?id="+$("id",this).text()+"'>Details</a>");				
				
				var atxt="";
				var txtBed = $("bed",this).text();
				if(txtBed!=0){
					if(txtBed==1){ txtBed="1 bedroom"; } else { txtBed =txtBed+" bedrooms"; }
					atxt=txtBed;
				}			
				
				var txtBath = $("bath",this).text();
				if(txtBath==1){ txtBath="1 bathroom"; } 
				if(txtBath==1.5){ txtBath="1&half  bathrooms"; } 
				if(txtBath==2){ txtBath="2 bathrooms"; } 
				if(txtBath==2.5){ txtBath="2&half bathrooms"; } 
				if(txtBath==3){ txtBath="3 bathrooms"; } 
				if(txtBath==3.5){ txtBath="3&half bathrooms"; }
				if(txtBath==4){ txtBath="4 bathrooms"; } 
				if(atxt!=""){atxt += ", ";}	
				atxt +=txtBath;
				
				var txtPark = $("park",this).text();
				if(txtPark==1){ txtPark="Garage"; }
				if(txtPark==2){ txtPark="Carport"; }
				if(txtPark==3){ txtPark="Street"; }
				atxt +=", "+txtPark;
				
				var txtPet = $("pet",this).text();
				if(txtPet==1){ txtPet="No pets"; }
				if(txtPet==2){ txtPet="Dogs allowed"; }
				if(txtPet==3){ txtPet="Cats allowed"; }
				if(txtPet==4){ txtPet="Pet Approval Required"; }
				atxt +=", "+txtPet;
				
				$(".rowInfo:last",zr).text(atxt);								
			});				
		}
	});
}


