function ddlAdGroups_onChange()
{
	populateAdGroupsDDL(0)
}

function ddlAds_onChange()
{
	populateAreaDDL(0);
}

function ddlArea_onChange()
{
	populateCityDDL(0);
}

function ddlCity_onChange()
{
	setChkAdjZips()
}


function populateAdGroupsDDL(response)
{	
	ddlAdGroups1 = document.getElementById('ddlAdGroups');
	ddlAds1 = document.getElementById('ddlAds');
	ddlAds1.style.width = '';
	
	switch(response)
	{
		case 0 : 
			url = 'http://' + getServiceHost() + 'get.asmx/GetAdsByAdgID?adgid=' + ddlAdGroups1.value;
			loadXMLDoc(url);
			break;
			
		default :
			aryResponse = response.split('||');
			ddlAds1.options.length = aryResponse.length;

			var intTemp = 0;
			
			if (ddlAds1.options.length > 1) 
			{
				intTemp = 1;
				ddlAds1.options[0] = new Option('', -1, false, false);
			}

			for (r=intTemp; r<aryResponse.length+intTemp; r++)
			{
				aryResponse2 = aryResponse[r-intTemp].split('|');
				ddlAds1.options[r] = new Option(aryResponse2[0] + ' (' + aryResponse2[1] + ')', aryResponse2[2], false, false);
			}
			
			document.getElementById('ddlArea').options.length = 0;
			document.getElementById('ddlCity').options.length = 0;
			
			if (ddlAds1.options.length == 1) populateAreaDDL(0);
			
			break;
	}
}

function populateAreaDDL(response)
{
	var ddlAds1 = document.getElementById('ddlAds');	
	var ddlArea1 =  document.getElementById('ddlArea');
	ddlArea1.style.width = '';
	
	switch(response)
	{
		case -1 :
			break;
		
		
		case 0 :
			url = 'http://' + getServiceHost() + 'get.asmx/Areas_service?v=' + VEND_ID_SERVICE + '&adid=' + ddlAds1.value + '&c=0&s=0&r=' + REGION_ID_SERVICE + '&z=0';
			//alert(url);
			loadXMLDoc(url);
			break;
		
		
		default :
			aryResponse = response.split('||');
			ddlArea1.options.length = aryResponse.length;
			stickyIndex = 0;
			
			var intTemp = 0;
			
			if (ddlArea1.options.length > 1) 
			{
				intTemp = 1;
				ddlArea1.options[0] = new Option('', -1, false, false);
			}
			
			for (r=intTemp; r<aryResponse.length+intTemp; r++)
			{
				aryResponse2 = aryResponse[r-intTemp].split('|');
	
				
				if (DDL_AREA_OPTION) 
				{
					if (aryResponse2[2] == DDL_AREA_OPTION.value) 
					{
						stickyIndex = r
					}
				}		
				
				ddlArea1.options[r] = new Option(aryResponse2[0] + ' (' + aryResponse2[1] + ')', aryResponse2[2], false, false);
			}
			
		
			if (ddlArea1.options.length == 1) 
			{
				populateCityDDL(0);
			}
			else 
			{
				//document.getElementById('ddlCity').options.length = 0;
				ddlArea.options[aryResponse.length+1] = new Option('All', 0, false, false);
				ddlArea.selectedIndex = aryResponse.length+1;
				populateCityDDL(0);
			}
			


	}
}

function populateCityDDL(response)
{
	var ddlAds1 = document.getElementById('ddlAds');
	var ddlArea =  document.getElementById('ddlArea');
	var ddlCity =  document.getElementById('ddlCity');	
	
	ddlCity.style.width = '';
	
	var areaID
	if (ddlArea.value > -1) areaID = ddlArea.value * 1;
	else areaID = ddlArea[1].value * 1;

	switch(response)
	{
		case -1 :
		
			break;
			
		case 0 :
			url = 'http://' + getServiceHost() + 'get.asmx/Cities_service?v=' + VEND_ID_SERVICE + '&adid=' + ddlAds1.value + '&c=0&s=0&r=' + REGION_ID_SERVICE + '&a=' + ddlArea.value + '&z=0'
			//alert(url);
			loadXMLDoc(url);
			break;
			
		default :
			aryResponse = response.split('||');
			stickyIndex = 0;
			ddlCity.options.length = aryResponse.length+1;
			intTemp = 1

			for (r=intTemp; r<aryResponse.length+intTemp; r++)
			{
				aryResponse2 = aryResponse[r-intTemp].split('|');
				cityName = aryResponse2[0];
				cityValue = cityName;

				ddlCity.options[r] = new Option(cityName + ' (' + aryResponse2[1] + ')', cityValue, false, false);
				
				if (DDL_CITY_OPTION)
				{
					if (DDL_CITY_OPTION.text.substring(0,DDL_CITY_OPTION.text.indexOf("(")-1) == cityName)
					stickyIndex = r;
				}
			}
			
			ddlCity.options[aryResponse.length+1] = new Option('All', 0, false, false);
			ddlCity.selectedIndex = aryResponse.length+1;
			
			if ((QueryString("ddlCity") != null) && (SET_AUTO_RESULTS == 1))
			{
				SET_AUTO_RESULTS = 0;
				ddlCity.value = Replace(QueryString('ddlCity'),'+',' ');
				document.getElementById('ddlDays').value = QueryString("ddlDays");
				
				if (QueryString("chkAdjZips") == 'on') document.getElementById('chkAdjZips').checked = true;
				if (QueryString("chkNewConst") == 'on') document.getElementById('chkNewConst').checked = true;
				if (QueryString("chkOpens") == 'on') document.getElementById('chkOpens').checked = true;
	
				if (QueryString("format") == 'List') btnGrid_onClick()
				if (QueryString("format") == 'Map') btnMap_onClick()
			}
			
			break;
	}
}


function ddlAds_onFocus()
{
	if (document.getElementById('ddlAdGroups') != null)
	{
		if (document.getElementById('ddlAdGroups').value == -1) 
		{
			alert('Please select a Category');
			document.getElementById('ddlAdGroups').focus();
		}
	}
}

function ddlArea_onFocus()
{
	
	if (document.getElementById('ddlAdGroups') != null)
	{
		if (document.getElementById('ddlAdGroups').value == -1)
		{
			alert('Please select a Category');
			document.getElementById('ddlAdGroups').focus();
		}
		else
		{
			if (document.getElementById('ddlAds').value == -1) 
			{
				alert('Please select a Category');
				document.getElementById('ddlAds').focus();
			}
		}
	}
	else
	{
		if (document.getElementById('ddlAds').value == -1) 
		{
			alert('Please select a Category');
			document.getElementById('ddlAds').focus();
		}
	}
}


function ddlCity_onFocus()
{
	if (document.getElementById('ddlAdGroups') != null)
	{
		if (document.getElementById('ddlAdGroups').value == -1)
		{
			alert('Please select a Category');
			document.getElementById('ddlAdGroups').focus();
		}
		else
		{
			if (document.getElementById('ddlAds').value == -1) 
			{
				alert('Please select a Category');
				document.getElementById('ddlAds').focus();
			}
			else
			{
				if (document.getElementById('ddlArea').value == -1) 
				{
					alert('Please select an Area');
					document.getElementById('ddlArea').focus();
				}
			}
		}
	}
	else
	{
		if (document.getElementById('ddlAds').value == -1) 
		{
			alert('Please select a Category');
			document.getElementById('ddlAds').focus();
		}
		else
		{
			if (document.getElementById('ddlArea').value == -1) 
			{
				alert('Please select an Area');
				document.getElementById('ddlArea').focus();
			}
		}
	}
}

function setDivMap()
{
	var div = document.getElementById('divMap');
	div.style.display = 'none';
}


function setSpanResults(value)
{
	var ddlCity = document.getElementById('ddlCity');
	var span = document.getElementById('spanResults');
	
	
	value = value * 1
	var strFinalText
	var value2 = 0;
	var valueDiff = 0;
	var tempStr = ddlCity.options[ddlCity.selectedIndex].text
	
	if (tempStr.indexOf('(') > 0)
	{
		var start = tempStr.indexOf('(');
		var end = tempStr.indexOf(')');
		value2 = tempStr.substring(start+1,end) * 1
		valueDiff = value-value2
		
		if (valueDiff < 0)
		{
			value2 = 0;
		}		
	}
	
	
	//alert(value + '   ' + value2 + '    ' + valueDiff)
	if (value==0) strFinalText = 'There are no matches to your search request.'
		
	if (value == 1 && value2 == 0) 
		strFinalText = 'There is <b>1</b> match to your search request.'
	else
	{
		if (value == 1 && value2 == 1)
			strFinalText= 'There is <b>1</b> match in ' + ddlCity.value + '.'
		else
		{
			if (value2 == 0) 
				strFinalText = 'There are <b>' + value + '</b> matches to your search request.'
			else
			{
				if (value2 == 1 && valueDiff == 1) 
					strFinalText= 'There is <b>1</b> match in ' + ddlCity.value + ' and <b>1</b> additional match in ZIP codes associated with ' + ddlCity.value + '.'
				else
				{
					if (value2 == 1 && valueDiff > 1) 
						strFinalText= 'There is <b>1</b> match in ' + ddlCity.value + ' and <b>' + valueDiff + '</b> additional matches in ZIP codes associated with ' + ddlCity.value + '.'
					else
					{
						if (value2 > 1 && valueDiff == 0)
							strFinalText= 'There are <b>' + value2 + '</b> matches in ' + ddlCity.value + '.'
						else
						{
							if (value2 > 1 && valueDiff > 1)
								strFinalText= 'There are <b>' + value2 + '</b> matches in ' + ddlCity.value + ' and <b>' + valueDiff + '</b> additional matches in ZIP codes associated with ' + ddlCity.value + '.'
							else
							{
								if (value2 > 1 && valueDiff == 1)
									strFinalText= 'There are <b>' + value2 + '</b> matches in ' + ddlCity.value + ' and <b>1</b> additional match in ZIP codes associated with ' + ddlCity.value + '.'
							}
						}
					}
				}
			}
			
		}	
	}

	span.innerHTML = strFinalText
}	

function setChkAdjZips()
{
	
	ddl = document.getElementById('ddlCity');
	chk = document.getElementById('chkAdjZips');
	
	if ((ddl.value == 0) || (ddl.value == 1)) 
	{
		chk.checked = false
		chk.disabled = true
	}
	else 
	{	
		chk.disabled = false
	}
}

function resetSearchGrid()
{
	
	var divGrid =  document.getElementById('divGrid');
	var span = document.getElementById('spanResults');
	
	if (divGrid != null)
	{	
		divGrid.innerHTML = '';
		span.innerHTML= '';
	}
}

function btnMap_onClick()
{
	var divGrid = document.getElementById('divGrid');
	var divMap = document.getElementById('divMap');
	var divGoogleMap = document.getElementById('map');
	var divBlank = document.getElementById('divBlank');
	var ddlCity = document.getElementById('ddlCity');
	var img = document.getElementById('imgYoSearch');
	
	
	if (ddlCity.value == -1)
	{
		alert('Please select a city / neighborhood');
		ddlCity.focus();	
	}
	else
	{
		divGrid.innerHTML = '';
		divBlank.innerHTML = '';
		divBlank.style.display = 'none';
		divMap.style.display = 'block';	
		
		getMap(0);
		
		if ((ddlCity.value == '0') || (ddlCity.value == 'Chicago')) 
		{
			GMAP_ZOOM_LEVEL = 5;
		}
		else
		{
			if (REGION_ID_SERVICE == 1045)
			{
				GMAP_ZOOM_LEVEL = 3;
			}
		}
	}
}

function setSubscribeLinkRSS(p_strUrl)
{
//called by xhr_service.js[getResults()] and map.js[getMap()]
	try {
		var strSubscribeLinkRSS = 'subscribeRSS';

		if (document.getElementById(strSubscribeLinkRSS) != null)
		{
			var strUrlIndex = p_strUrl.indexOf('?v');
			var strUrlRSS = 'http://wibiti.com/rss/rss.aspx' + p_strUrl.substring(strUrlIndex, p_strUrl.length);

			document.getElementById(strSubscribeLinkRSS).href = strUrlRSS;
			document.getElementById(strSubscribeLinkRSS).target = '_blank';
		}
	} catch(e) {}
}

function getResults(response)
{
	var ddlAds = document.getElementById('ddlAds');
	var ddlDays = document.getElementById('ddlDays');
	var ddlArea = document.getElementById('ddlArea');
	var ddlCity = document.getElementById('ddlCity');
	var chkAdjZips = document.getElementById('chkAdjZips');
	var divGrid = document.getElementById('divGrid');
	
	var opens = 0;
	var newconst = 0;
	var min = 0
	var max = 0
	var adjzips = 0;
	if (chkAdjZips.checked) adjzips = 1;

	var tempID = 0;
	
	if (response == 0)
	{

		var url = 'http://' + getServiceHost() + 'get.asmx/getSearchResults_service?v=' + VEND_ID_SERVICE + '&p=' + tempID + '&c=0&s=0&r=' + REGION_ID_SERVICE + '&a=' + ddlArea.value + '&n=' + ddlCity.value + '&z=0' + '&min=' + min + '&max=' + max + '&o=' + opens + '&az=' + adjzips + '&nc=' + newconst + '&d=' + ddlDays.value + '&qs=0' + '&adID=' + ddlAds.value
		if (HP_SORT_STATE) url += HP_SORT_STATE
		else url += '&col=0&dir=0'
		
		loadXMLDoc(url);
		setSubscribeLinkRSS(url);
	}
	else
	{
		if (response == 'empty') 
		{
			setSpanResults(0);
			divGrid.innerHTML = '';
		}
		else
		{
			HPURL_LIST = '';
			DDL_AREA_OPTION = ddlArea.options[ddlArea.selectedIndex]
			aryResponse = response.split('||');
			var intAryIndexBegin = 0;
			
			html = '<table id="tblHomePages" cellpadding="0" cellspacing="0" border="0" width="100%">'
			html += build_divGrid_Header(tempID);
			
			for (r=intAryIndexBegin; r<aryResponse.length; r++)
			{
				html += build_divGrid_Row(r, tempID, aryResponse[r]);
			}
			
			html += '</table>'
			
			setSpanResults(aryResponse.length);
			divGrid.innerHTML = html;
		}
	}
}

function build_divGrid_Header(parentID)
{
	Header = '<tr class="stdDGHeader" valign="bottom">'
	Header += '<th style="width: 60px;">&nbsp;</th>'
	Header += '<th style="width: 300px;">Location</th>'
	Header += '</tr>'
	
	return Header;
}

function build_divGrid_Row(rownum, parentID, rowData)
{
	var ddlSubs = document.getElementById('ddlSubs');
	
	aryRows = rowData.split('|');
	hpURL = aryRows[0];	
	hpURL = '"' + aryRows[1] + '"';
	imagepath = buildImagePath(aryRows[2]);
	imagewidth = aryRows[3] * 1
	imageheight = aryRows[4] * 1
	address = aryRows[5];
	unit = aryRows[6];
	city = aryRows[7];
	neighborhood = aryRows[8];
	state = aryRows[9];
	zip = aryRows[10];
	hpdtitle = aryRows[11];
	hptitle = aryRows[12];
	hpcopy = aryRows[13];		
	
	//set variable for list of hpURLs
	imgOnClick = 'openHomePage(' + hpURL + ');'
	
	
	//----------------------------------------------
	//start row
	//----------------------------------------------
	row = '<tr class="stdDGText" valign="top">'
	
	//----------------------------------------------
	//image column
	//----------------------------------------------
	
	row += '<td rowspan="2" align="center"><img src="' + imagepath + '" onclick=' + imgOnClick + ' width="60" style="cursor:hand;"/></td>'
	
	

	//----------------------------------------------
	//location column
	//----------------------------------------------
	row += '<td class="stdDGText">'
	
	//if (catID == 1151)
	//{
	//	row +=		'<a class="lnkBlue" href=javascript:' + imgOnClick + '>'
	//	row +=		hptitle
	//	row +=		'</a><br />'
	//	row +=		address + ' ' + unit  + '<br />'
	//}
	//else
	//{
		row +=		'<a class="lnkBlue"  href=javascript:void(0); onclick=' + imgOnClick + '>'
		row +=		address + ' ' + unit
		row +=		'</a><br />'
	//}
	
	if (neighborhood != '0') row +=	neighborhood + '<br />' 
	row +=		city + ', ' + state + '  ' + zip
	row += '</td>'
	row += '</tr>'
			

	
	//----------------------------------------------
	//headline and copy
	//----------------------------------------------
	row += '<tr valign="top">'
	row += '<td colspan="3" class="stdDGText2">'
	
	if ((hpdtitle.length > 1) && (hpcopy.length == 1))
		row += '<div class="divGridTitle">' + hpdtitle + '</div><div class="divGridCopy"><a class="lnkBlue" href=javascript:void(0); onclick=' + imgOnClick + '>More info</a></div>'
		
	if ((hpdtitle.length == 1) && (hpcopy.length > 1))
		row += '<div class="divGridCopy">' + hpcopy + '...</div><div class="divGridCopy"><a href=javascript:void(0); class="lnkBlue" onclick=' + imgOnClick + '>More info</a></div>'
		
	if ((hpdtitle.length > 1) && (hpcopy.length > 1))
		row += '<div class="divGridTitle">' + hpdtitle + '</div><div class="divGridCopy">' + hpcopy + '...</div><div class="divGridCopy"><a class="lnkBlue" href=javascript:void(0); onclick=' + imgOnClick + '>More info</a></div>'
		
	if ((hpdtitle.length == 1) && (hpcopy.length == 1))
		row += '<div class="divGridCopy"><a class="lnkBlue" href=javascript:void(0); onclick=' + imgOnClick + '>More info</a></div>'
				
	return row
}

function btnGrid_onClick()
{
	var div = document.getElementById('divMap');
	var div2 = document.getElementById('divBlank');
	var span = document.getElementById('spanResults')
	var ddlCity = document.getElementById('ddlCity');
	
	if (ddlCity.value == -1)
	{
		alert('Please select a city / neighborhood');
		ddlCity.focus();	
	}
	else
	{
		getResults(0); 
		div.style.display = 'none';
		div2.style.display = 'none';
		span.display='visible';
	}
}


