var images = "";
var minImages = 0;
var currentImages = 0;
var maxImages = 0;
var agtUrl = "";
var agtUrl2 = "";

if(typeof(aid) != "undefined" && aid !="" && aid !=0 && aid > 0)
{
	agtUrl = "?frm=agt&aid="+aid;
	agtUrl2 = "&frm=agt&aid="+aid;
}

function loadCountyCitySchoolPops()
{
	// Load County into Dropdown box
	/*qs="loadtype=county";
	var url  = "/help/c21vjf/loadLandingPageData.php";
	ajaxRequest(url,qs,function(reqobj){var varResponse = trim(reqobj.responseText);
		if(varResponse != "-NA-")
		{	*/
			//var countyData = varResponse.split("^^");
			var countyData = new Array("Dutchess","Fairfield","Putnam","Westchester");
			var select = document.getElementById('county');
			select.options[select.options.length] = new Option("Any", "");
			for(i=0;i<countyData.length;i++)
			{
				keyVal = countyData[i];
				if(keyVal == "Fairfield")
					keyVal = "Connecticut";

				if(countyData[i] == "Ulster" || countyData[i] == "Orange" || countyData[i] == "Columbia")
					keyVal = countyData[i] + " County";

				if(countyData[i] != "" && countyData[i] != "Other")
				select.options[select.options.length] = new Option(countyData[i], keyVal);
			}
			select.options[0].selected="true";
/*		}
	});
*/


	qs="loadtype=city";
	var url  = "/help/c21vjf/loadLandingPageData.php";
	ajaxRequest(url,qs,function(reqobj){var varResponse = trim(reqobj.responseText);
		if(varResponse != "-NA-")
		{	
			var countyData = varResponse.split("^^");
			var select = document.getElementById('searchCounty');
			//select.options[select.options.length] = new Option("Any", "");
			for(i=0;i<countyData.length;i++)
			{
				if(countyData[i] != "" && countyData[i] != "Other")
				select.options[select.options.length] = new Option(countyData[i], countyData[i]);
			}
			select.options[0].selected="true";
		}
	});

	// Load New Listing Property Box
	qs="loadtype=newlistings"+agtUrl2;
	var url  = "/help/c21vjf/loadLandingPageData.php";
	ajaxRequest(url,qs,function(reqobj){var varResponse = trim(reqobj.responseText);
		if(varResponse != "-NA-")
		{
			minImages = 0;
			currentImages = 0;
			images = varResponse.split('|^^|');
			maxImages = images.length;
			document.getElementById('currentImageTag').innerHTML=images[currentImages];
		}
	});
}

function loadCityOnCounty()
{
	$("school").disabled=true;
	$("searchCounty").disabled=false;
	ClearOptionsFast('searchCounty');
	ClearOptionsFast('school');

	if($('citytown').checked)
	{
		
		// Load City into Dropdown box
		if($('county').value!="")
			qs="loadtype=countycity&countybase="+$('county').value;
		else
			qs="loadtype=city";
		
		var url  = "/help/c21vjf/loadLandingPageData.php";
		ajaxRequest(url,qs,function(reqobj){var varResponse = trim(reqobj.responseText);
			if(varResponse != "-NA-")
			{	
				var countyData = varResponse.split("^^");
				var select = document.getElementById('searchCounty');
				//select.options[select.options.length] = new Option("Any", "");
				for(i=0;i<countyData.length;i++)
				{
					if(countyData[i] != "" && countyData[i] != "Other")
					select.options[select.options.length] = new Option(countyData[i], countyData[i]);
				}
				select.options[0].selected="true";
			}
		});
	}
}

function loadSchooOnCity()
{
	ClearOptionsFast('searchCounty');
	ClearOptionsFast('school');

	if($("county").value == "" && $('schooldistrict').checked)
	{
		$("school").disabled=false;
		$("searchCounty").disabled=true;

		var countyData = new Array("Ardsley","Arlington","Beacon","Bedford","Blind Brook","Brewster","Briarcliff Manor","Bronxville","Byram Hills","Carmel","Chappaqua","Cortland","Croton Harmon","Dobbs Ferry","Dover","Eastchester","Edgemont","Elmsford","Garrison","Greenburgh","Haldane","Harrison","Hastings-on-hudson","Hendrick Hudson","Hyde Park","Irvington","Katonah Lewisboro","Lakeland","Lewisboro","Mahopac","Mamaroneck","Millbrook","Mt Kisco","Mt Pleasant","Mt Vernon","New Castle","New Rochelle","North Castle","North Salem","Northeast Central","Ossining","Pawling","Peekskill","Pelham","Pine Plains","Pleasantville","Pocantico Hills","Port Chester","Poughkeepsie","Pound Ridge","Putnam Valley","Red Hook","Rhinebeck","Rhinecliff Union Free","Rye City","Rye Neck","Rye Town","Scarsdale","Somers","Spackenkill","Tarrytown","Tuckaho","Valhalla","Wappingers","Webutuck","White Plains","Yonkers","Yorktown");
		
		if(typeof(countyData) != "undefined")
		{
			var select = document.getElementById('school');
			//select.options[select.options.length] = new Option("Any", "");
			for(i=0;i<countyData.length;i++)
			{
				if(countyData[i] != "" && countyData[i] != "Other")
				select.options[select.options.length] = new Option(countyData[i], countyData[i]);
			}
			select.options[0].selected="true";
		}

		return;
	}
	else if($('schooldistrict').checked)
	{
		$("school").disabled=false;
		$("searchCounty").disabled=true;

		if($('county').value=="Dutchess")
			var countyData = new Array("Arlington","Beacon","Carmel","Dover","Haldane","Hyde Park","Millbrook","Northeast Central","Pawling","Pine Plains","Poughkeepsie","Red Hook","Rhinebeck","Rhinecliff Union Free","Spackenkill","Wappingers","Webutuck");
		else if($('county').value=="Putnam")
			var countyData = new Array("Brewster","Carmel","Garrison","Haldane","Lakeland","Mahopac","North Salem","Pawling","Putnam Valley","Wappingers");
		else if($('county').value=="Westchester")
			var countyData = new Array("Ardsley","Bedford","Blind Brook","Briarcliff Manor","Bronxville","Byram Hills","Chappaqua","Cortland","Croton Harmon","Dobbs Ferry","Eastchester","Edgemont","Elmsford","Greenburgh","Harrison","Hastings-on-hudson","Hendrick Hudson","Irvington","Katonah Lewisboro","Lakeland","Lewisboro","Mamaroneck","Mt Kisco","Mt Pleasant","Mt Vernon","New Castle","New Rochelle","North Castle","North Salem","Ossining","Peekskill","Pelham","Pleasantville","Pocantico Hills","Port Chester","Pound Ridge","Putnam Valley","Rye City","Rye Neck","Rye Town","Scarsdale","Somers","Tarrytown","Tuckaho","Valhalla","White Plains","Yonkers","Yorktown");
		if(typeof(countyData) != "undefined")
		{
			var select = document.getElementById('school');
			//select.options[select.options.length] = new Option("Any", "");
			for(i=0;i<countyData.length;i++)
			{
				if(countyData[i] != "" && countyData[i] != "Other")
				select.options[select.options.length] = new Option(countyData[i], countyData[i]);
			}
			select.options[0].selected="true";
		}
	}
}

function loadSchooOnCounty()
{
	ClearOptionsFast('school');
	// Load School into Dropdown box
	qs="loadtype=countyschool&countybase="+$('county').value;
	var url  = "/help/c21vjf/loadLandingPageData.php";
	ajaxRequest(url,qs,function(reqobj){var varResponse = trim(reqobj.responseText);
		if(varResponse != "-NA-")
		{	
			var countyData = varResponse.split("^^");
			var select = document.getElementById('school');
			select.options[select.options.length] = new Option("Any", "");
			for(i=0;i<countyData.length;i++)
			{
				if(countyData[i] != "" && countyData[i] != "Other")
				select.options[select.options.length] = new Option(countyData[i], countyData[i]);
			}
			select.options[0].selected="true";
		}
	});
}

function ClearOptionsFast(id)
{
	var selectObj = document.getElementById(id);
	var selectParentNode = selectObj.parentNode;
	var newSelectObj = selectObj.cloneNode(false); // Make a shallow copy
	selectParentNode.replaceChild(newSelectObj, selectObj);
}

//loadCountyCitySchoolPops();

function gotoMap4C21VJF(moreOpt)
{
	var moreOptFlag=0;
	var url = "/Search/"+agtUrl;
	if(moreOpt == "1")
	{
		moreOptFlag=1;
		setSearchCookie("moreOpt",moreOpt);
		
		if(agtUrl == "")
			url="/Search/?noSearch=1";
		else
			url= url + "&noSearch=1";
		
		location.href = url;
		return;
	}

	if($('mlsno').value=="" || $('mlsno').value == "MLS#")
	{
		if($('citytown').checked)
		{
			if ( $("searchCounty").value == "" && moreOptFlag != "1")
			{
					alert ("Please Select City/Town.");
					return;
			}

			setSearchCookie("setSearchCookie","");
			setSearchCookie("searchCriteria","");
			
			if(!moreOptFlag)
			{
				var getGeoAddress = doGeoCode($("searchCounty").value,'');
				if(!getGeoAddress)
					return;

				var city  = getGeoAddress['city'];
				var state = getGeoAddress['state'];
				var zip = getGeoAddress['zip'];
				setSearchCookie("zip",zip);
				setSearchCookie("city",city);
				setSearchCookie("state",state);
			}
		}

		if($('schooldistrict').checked)
		{
			if ( $("school").value == "")
			{
					alert ("Please Select School District.");
					return;
			}
		}

		if($("school").value != "" && $('schooldistrict').checked)
			setSearchCookie("SchDst",$("school").value);
		
		if($("county").value != "" && $('schooldistrict').checked)
			setSearchCookie("County",$("county").value);
		
		url = "/Search/"+agtUrl;

		removeSearchCookie('subNhoodId');
		removeSearchCookie('subNhood');
		removeSearchCookie('neighborhood');

		
		if(moreOpt == "mapsearch")
			url = "/Search/?fromPage=mapView"+agtUrl2;
		else
			setSearchCookie("moreOpt","1");	
		
		if(validateRSCriteriaNew())
		{
			if($('chkNewListing') && $('chkNewListing').checked) 
			{
				setSearchCookie("searchCriteria","newListing");
			}
			if($('chkRentals') && $('chkRentals').checked) 
			{
				setSearchCookie("searchType","'Residential Rental','Rentals'");
			}
			location.href = url;
		}
	}
	else
	{
		qs="mlsno="+ $("mlsno").value + "&forAlert=1&theme="+bkTheme;
		var url  = "/app/dashboard/mlsSearch.php";
		ajaxRequest(url,qs,function(reqobj){var varResponse = trim(reqobj.responseText);
			if (varResponse == "0")
			{
				alert("MLS Number Not found.");
				$("mlsno").value = "";
				$("mlsno").focus();
				return;
			}
			else
			{		
				setSearchCookie('city','');
				setSearchCookie('state','');
				setSearchCookie('zip','');
				setSearchCookie("mlsno",$('mlsno').value);
				
				url = "/search/"+agtUrl;
				if(moreOpt == "mapsearch")
					url = "/Search/?fromPage=mapView"+agtUrl2;
				location.href = url;
			}
		});
	}
}


/*function gotoMap4C21VJF(moreOpt)
{
	var moreOptFlag=0;
	var url = "/Search/"+agtUrl;
	if($('mlsno').value=="" || $('mlsno').value == "MLS#")
	{
		if ( $("cityStZip").value == "" || $F("cityStZip").toLowerCase() == "required" || $F("cityStZip") == "Enter Neighborhood, City or ZIP code")
		{
			if (moreOpt != "moreOpt") {
				alert ("Please Enter Neighborhood, City or ZIP code");
				$("cityStZip").value = "Enter Neighborhood, City or ZIP code";
				$("cityStZip").focus();
				return;
			}
			else
				moreOptFlag=1;
		}

		setSearchCookie("setSearchCookie","");
		setSearchCookie("searchCriteria","");

		if($('selVal').value == "NBR")
		{
			var nbrName = trim($F("cityStZip")).replace('&','|and|');
			nbrName = nbrName.replace('/','__');
			removeSearchCookie('city');
			removeSearchCookie('state');
			removeSearchCookie('zip');
			removeSearchCookie('subNhoodId');
			removeSearchCookie('subNhood');
			setSearchCookie("neighborhood",trim($F("cityStZip")));
			setSearchCookie("nbrIndex","1");
			url = "/Search/"+agtUrl;
		}
		else if($F('selVal').indexOf("SNBR") != -1)
		{
			removeSearchCookie('city');
			removeSearchCookie('state');
			removeSearchCookie('zip');
			removeSearchCookie('neighborhood');
			var sNbrID = $F('selVal').split('_');							
			getPolygonFrmSNbr(sNbrID[1]);
			if(getSearchCookie('area') != "")
			{
				setSearchCookie("subNhoodId",sNbrID[1]);
				setSearchCookie("subNhood",$F("cityStZip"));
			}	
		}
		else
		{
			if(!moreOptFlag)
			{
				var getGeoAddress = doGeoCode($("cityStZip").value,'');
				if(!getGeoAddress)
					return;

				var city  = getGeoAddress['city'];
				var state = getGeoAddress['state'];
				var zip = getGeoAddress['zip'];
				setSearchCookie("zip",zip);
				setSearchCookie("city",city);
				setSearchCookie("state",state);
				url = "/Search/"+agtUrl;
				
				removeSearchCookie('subNhoodId');
				removeSearchCookie('subNhood');
				removeSearchCookie('neighborhood');
			}
		}
		
		if(moreOpt == "moreOpt") 
			setSearchCookie("moreOpt","1");
		else if(moreOpt == "mapsearch")
		{
			if(agtUrl)
				url = "/Search/"+agtUrl+"&drawSearch=1";
			else
				url = "/Search/?drawSearch=1";
		}
		else
			removeSearchCookie("moreOpt");

		if(validateRSCriteriaNew())
		{
			if($('chkNewListing') && $('chkNewListing').checked) 
			{
				setSearchCookie("searchCriteria","newListing");
			}
			if($('chkRentals') && $('chkRentals').checked) 
			{
				setSearchCookie("searchType","'Residential Rental','Rentals'");
			}
			location.href = url;
		}
	}
	else
	{
		qs="mlsno="+ $("mlsno").value + "&forAlert=1&theme="+bkTheme;
		var url  = "/app/dashboard/mlsSearch.php";
		ajaxRequest(url,qs,function(reqobj){var varResponse = trim(reqobj.responseText);
			if (varResponse == "0")
			{
				alert("MLS Number Not found.");
				$("mlsno").value = "";
				$("mlsno").focus();
				return;
			}
			else
			{		
				setSearchCookie('city','');
				setSearchCookie('state','');
				setSearchCookie('zip','');
				setSearchCookie('subNhoodId','');
				setSearchCookie('subNhood','');
				setSearchCookie('neighborhood','');
				setSearchCookie("mlsno",$('mlsno').value);
				location.href = "/Search/"+agtUrl;
			}
		});
	}
}*/

function validateRSCriteriaNew()
{
	if($F('minprice') != "" || $F('maxprice') != "")
	{
		if(isNaN($F('minprice').replace(/,/g,'')) || isNaN($F('maxprice').replace(/,/g,'')))
		{
			alert("Please enter valid price range");
			$('minprice').value = '';
			$('maxprice').value = '';
			$('minprice').focus();
			return false;
		}
	}

	var minP = Number(money2num($('minprice').value));	
	var maxP = Number(money2num($('maxprice').value));
	if(minP<0 || maxP<0)
	{
		$('minprice').className  = 'textboxRed';
		alert("Please enter valid price range");
		$('minprice').value = '';
		$('maxprice').value = '';
		$('minprice').focus();
		return false;
	}

	if((minP>maxP) && (minP != "" && maxP != ""))
	{
		('minprice').className  = 'textboxRed';
		alert("Please enter valid price range");
		$('minprice').value = '';
		$('maxprice').value = '';
		$('minprice').focus();
		return false;
	}	
	
	if (maxP == "0")
	{
		$('maxprice').value = '';
		maxP = "";
	}
	if (minP == "0")
	{
		$('minprice').value = '';
		minP = "";
	}
	
	if( maxP != "" && minP != "" && parseInt(maxP) <= parseInt(minP) )
	{
			//$('minprice').className  = 'textboxRed';
			$('minprice').focus();
			alert("Maximum price should be greater than minimum price.");
			$("maxprice").value = "";
			$('minprice').value = "";			
			return false;
	}

	setSearchCookie("minPrice",minP);
	setSearchCookie("maxPrice",maxP);			
	
	setSearchCookie("searchCriteria","");
	return true;
}

function previousImage()
{
	if(currentImages > minImages && minImages != maxImages)	
		currentImages--;
	else	
		currentImages = maxImages-1;
	document.getElementById('currentImageTag').innerHTML=images[currentImages];
}

function nextImage()
{
	if(currentImages < maxImages-1 && minImages != maxImages)
		currentImages++;
	else
		currentImages = 0;
	document.getElementById('currentImageTag').innerHTML=images[currentImages];
}

setInterval("nextImage()",5000);

function showhideImgNavigation(show)
{
	if(show==1 && maxImages > 1)
	{
		$("imgPrevious").show();
		$("imgNext").show();
	}
	else
	{
		$("imgPrevious").hide();
		$("imgNext").hide();
	}
}

if($("imgPrevious"))$("imgPrevious").hide();
if($("imgNext"))$("imgNext").hide();


function serCSZOnFocus()
{
	if($F('cityStZip') == '')
		$('cityStZip').value = 'Enter Neighborhood, City or ZIP code';

	else if($F('cityStZip') == 'Enter Neighborhood, City or ZIP code')
		$('cityStZip').value = '';
}

function clearSelVal(event)
{
	if(event && event.keyCode != 13)
	{
		$('selVal').value='';			
	}
}

function exploreEnter(event)
{
	if(event && event.keyCode == 13)
	{			
		gotoMap();				
	}
}

function moreSearch()
{
	setSearchCookie("moreOpt","1");
	
	setSearchCookie("searchType","'<?php echo $typeStr; ?>'");

	if(validateRSCriteriaNew())
	{	
		if(agtUrl !="")
			location.href = "/Search/"+agtUrl+"&noSearch=1";
		else
			location.href = "/Search/?noSearch=1";
	}
}
	/*YAHOO.example.BasicRemote = function()
	{
		// Use an XHRDataSource
		var oDS = new YAHOO.util.XHRDataSource("/getCity.php?getCityNbr=1");
			oDS.connMethodPost = true;


		// Set the responseType
		oDS.responseType = YAHOO.util.XHRDataSource.TYPE_TEXT;
		// Define the schema of the delimited results
		oDS.responseSchema =
		{
			recordDelim: "\n",
			fieldDelim: "\t"
		};

		// Enable caching
		oDS.maxCacheEntries = 180;
		// Instantiate the AutoComplete
		var oAC = new YAHOO.widget.AutoComplete("cityStZip", "myContainer", oDS);
		oAC.maxResultsDisplayed = 20;
		//for IE6 BUG
		oAC.useIFrame = true;
		oAC.generateRequest = function()
		{
			matchCont = '';
			return "city="+$('cityStZip').value;
		};
		oAC.resultTypeList = false;
		oAC.formatResult = function(oResultData, sQuery, sResultMatch)
		{
			var sKey = sResultMatch;
			var sKeyRemainder = sKey.substr(sQuery.length);
			var aMarkup = ["<div id=\'data\' class=\'yui-skin-sam\'>","<span style=\'font-weight:bold\'>", sQuery, "</span>", sKeyRemainder, "<span style=\'color:red;display:none;\'>"+ oResultData[1]+"</span></div>"];
			matchCont +='~!@'+oResultData;
			return (aMarkup.join(""));
		};
		//define your itemSelect handler function:
		var itemSelectHandler = function(Event) 
		{			
			if($('selVal')){$('selVal').value="";}
			var myArr= $("cityStZip").value.split("&nbsp;");							
			$("cityStZip").value=myArr[0];
			if(typeof(myArr[1])!="undefined" && myArr[1]!="")
			{	
				$('myAutoComplete').style.marginTop = "0px";
				var re = new RegExp("<span style='display:none;'>([a-zA-Z0-9_]+)<\/span>");
				var m = re.exec(myArr[1]);				
				if (m != null){if($('selVal')){$('selVal').value=m[1];}}				
			}
		};
		var dataReturnEventon = function()
		{			
			if(trim(matchCont) != "")
			{	
				matchContArr = matchCont.split('~!@');						
				if(matchContArr.length > 0 && trim($F('selVal')) == "" )
				{	
					var matchContStr = matchContArr[(matchContArr.length)-1];
					var matchContStrArr = matchContStr.split('&nbsp;');
					$('cityStZip').value = matchContStrArr[0];
					var re = new RegExp("<span style='display:none;'>([a-zA-Z0-9_]+)<\/span>");
					var m = re.exec(matchContStrArr[1]);
					if (m != null){if($('selVal')){$('selVal').value=m[1];}}
				}
			}			
		}
		//subscribe your handler to the event, assuming
		//you have an AutoComplete instance myAC:
		oAC.itemSelectEvent.subscribe(itemSelectHandler);
		oAC.textboxBlurEvent.subscribe(dataReturnEventon);
	}();*/
