/**********
*
* The jsp that calls this needs to print out the following lines before calling AdvSearchFormWriter(tm_can_share),
* with the proper vaue in between the ++'s.
*

out.println( "document.write('var strOptPercent = ' +" ++ "+ ';' ); ");
out.println( "document.write('strOptReviews = ' +" ++ "+ ';' ); ");
out.println( "document.write('strOptStates = ' +" ++ "+ ';' ); ");
out.println( "document.write('strRadDist = ' +" ++ "+ ';' ); ");
out.println( "document.write('strRadAddr = ' +" ++ "+ ';' ); ");
out.println( "document.write('strOptBeds = ' +" ++ "+ ';' ); ");
out.println( "document.write('strOptBaths = ' +" ++ "+ ';' ); ");
out.println( "document.write('strOptPriceMin = ' +" ++ "+ ';' ); ");
out.println( "document.write('strOptPriceMax = ' +" ++ "+ ';' ); ");
out.println( "document.write('strThisYear = ' +" ++ "+ ';' ); ");
out.println( "document.write('strNextYear = ' +" ++ "+ ';' ); ");
out.println( "document.write('hiddenDate = ' +" ++ "+ ';' ); ");

strCity
strState
*
*
*/

function errorChecker()
{
	if( !notThePast() )
		return false;
		
	var beds = document.getElementById( 'beds' ).value;
	
	if( !beds )
	{
		alert( "You need to specify how many beds you are looking for." );
		return false;
	}
	
	var lo_price = document.getElementById( 'price_bottom' ).value;
	var hi_price = document.getElementById( 'price_top' ).value;
	
	if( lo_price == "2000+" || lo_price == "2000%2B" )
	{
		lo_price = 2001;
	}
	
	if( hi_price == "2000+" || hi_price == "2000%2B" )
	{
		hi_price = 2001;
	}	
	
	if( !lo_price )
	{
		alert( "Please set your minimum desired rent." );
		return false;
	}
	
	if( !hi_price )
	{
		alert( "Please set your maximum desired rent." );
		return false;	
	}
	
	if( hi_price*1 < lo_price*1 )
	{	
		alert( "Your minimum rent cannot be greater than your maximum rent." );
		return false;
	}
	
	return true;
}

/// Currently just massages the date to not be the past.  Not really error-findy.
///
function notThePast()
{
	try {
		setYear();
	} catch(ig)  {}

	var _monthSel	= document.getElementById( "month" );
	var _daySel	= document.getElementById( "lol_day" );
	var _yearSel	= document.getElementById( "year" );

	if( !_monthSel.options[_monthSel.selectedIndex].value || !_daySel.options[_daySel.selectedIndex].value )
	{
		alert( " Please set your desired move-in date." );
		return false;
	}

	return true;
}

function setupAddr()
{
	var _addr = document.getElementById( "radius_address" );

	if( !_addr.value )
	{
		_addr.value = addrDefault;
	}
}

function selectHelper( elemSelect, myValue )
{
	if( myValue == "~~*~~" )
		myValue = '';

	try {
	
		var setFlag = false;

		for( i=0; i<elemSelect.options.length; i++ )
		{
			if( elemSelect.options[i].value == myValue )
			{
				elemSelect.selectedIndex = i;
				setFlag = true;
				continue;
			}
		}

		if( !setFlag )
			elemSelect.selectedIndex = 0;
	}
	catch( e )
	{
		//alert( "omg: "+e );
	}
}

function updateDate()
{
	var _hidden	= document.getElementById( "hidden_date" );
	var _monthSel	= document.getElementById( "month" );
	var _daySel	= document.getElementById( "lol_day" );
	var _yearSel	= document.getElementById( "year" );

	/// ditch early if there's no hidden date value.
	if( !_hidden.value )
	{
		_monthSel.selectedIndex = 0;
		_yearSel.selectedIndex = 0;
		_daySel.selectedIndex = 0;
		return;
	}

	arSplit = _hidden.value.split("-");

	myYear	= arSplit[0];
	myMonth	= arSplit[1];
	myDay	= arSplit[2];

	selectHelper( _yearSel, myYear );
	selectHelper( _monthSel, myMonth );
	selectHelper( _daySel, myDay );	
}

function toggleAddCrit()
{
	var c1 = document.getElementById( "addCrit" );
	var c2 = document.getElementById( "addCrit2" );
	var c3 = document.getElementById( "addCrit3" );
	var c4 = document.getElementById( "addCrit4" );

	var cT = document.getElementById( "expTxt" );
	
	closeAllOpenHelps();

	if( c1.style.display == "block" )
	{
		c1.style.display = "none";
		c2.style.display = "none";
		c3.style.display = "none";
		c4.style.display = "none";
		cT.innerHTML = "show more criteria";
	}
	else
	{
		c1.style.display = "block";
		c2.style.display = "block";
		c3.style.display = "block";
		c4.style.display = "block";
		cT.innerHTML = "hide criteria";
	}
}

function topFocus()
{
	var addrField = document.getElementById( 'radius_address' );
	var distField = document.getElementById( 'radius_distance' );

	if( addrField.value || distField.value )
	{
		radioBtn = document.getElementById( 'vis1' );
		zipField = document.getElementById( 'zip_code' );

		zipField.value = "";
		radioBtn.checked = true;	
	}
}

function bottomFocus()
{
	zipField = document.getElementById( 'zip_code' );

	if( zipField.value )
	{
		var radioBtn = document.getElementById( 'vis2' );
		var distField = document.getElementById( 'radius_distance' );
		var addrField = document.getElementById( 'radius_address' );

		distField.value = "";
		addrField.value = "";
		radioBtn.checked = true;
	}
}

function toggleADV()
{
	var me = document.getElementById( 'tm_advsearchform' );
	var myLink = document.getElementById( 'tm_advsearchform_toggle' );
	var myImg = document.getElementById( 'tm_advsearchform_img' );
	
	closeAllOpenHelps();
	
	if( me.style.display == "block" || !me.style.display )
	{
		me.style.display = "none";
		//myLink.innerHTML = "show advanced search";
		myImg.src = "/review/i/plus.png";
		
		tmtog_loadXMLDoc( "./Sandle/?citylist_toggle=hide" );
	}
	else
	{
		me.style.display = "block";
		//myLink.innerHTML = "hide advanced search";
		myImg.src = "/review/i/minus.png";
		
		tmtog_loadXMLDoc( "./Sandle/?citylist_toggle=show" );
	}
}

function openADV() 
{
	var me = document.getElementById( 'tm_advsearchform' );
	var myLink = document.getElementById( 'tm_advsearchform_toggle' );
	var myImg = document.getElementById( 'tm_advsearchform_img' );
	
	closeAllOpenHelps();

	if( me.style.display != "block" )
	{
		me.style.display = "block";
		//myLink.innerHTML = "hide advanced search";
		myImg.src = "/review/i/minus.png";
		
		tmtog_loadXMLDoc( "./Sandle/?citylist_toggle=show" );
	}
}

function dcCheck()
{
	var state = document.getElementById( 'adv_state' );
	
	if( state.value == "DC" )
	{
		var city = document.getElementById( 'adv_city' );
		
		city.value = "Washington";
	}
}

function tmtog_processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
            
//alert( req.responseText );
            
        } else {
        
        }
    }
}

function tmtog_loadXMLDoc(url) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = tmtog_processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = tmtog_processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function hTrigger() 
{
	var lbl = document.getElementById( 'search_area_label' );
	var txt;

	var miles = document.getElementById( 'radius_distance' ).value;

	txt = "within "+miles+" mi. of ";

	if( 
		document.getElementById( 'radius_address' ).value ||
		document.getElementById( 'loc_custom' ).value
	)
	{	
		txt += " (custom&nbsp;location) ";
	}
	else if( document.getElementById( 'zip' ).value )
	{
		txt += document.getElementById( 'zip' ).value;
	}
	else if(
		document.getElementById( 'loc_city' ).value &&
		document.getElementById( 'loc_state' ).value
	)
	{
		txt 
			+= document.getElementById( 'loc_city' ).value;
	}
	try{
	lbl.innerHTML = txt;
	}catch(err){}
/*
	/// clears the map.
	map.clearOverlays()

	groundZero = new GPoint(
		document.getElementById( 'asform_lon' ).value, 
		document.getElementById( 'asform_lat' ).value
	);

	map.centerAndZoom( groundZero, zoomLookupTable() );

	/// change the radius of the circle to whatever value is selected.
	changeDrawCircleRadius( parseFloat(document.getElementById('radius_distance').value) );

	/// draw the circle
	drawCircle( groundZero.x,groundZero.y );

	/// add the centerpoint flag.
	map.addOverlay( new GMarker(groundZero) );*/
}

function setYear()
{
	var today = new Date();
	var currentYear=today.getFullYear();
	var nCurrentMonth=today.getMonth()+1;

	var formMonth=document.getElementById( "month" );
	var formYear=document.getElementById( "year" );

	if (formMonth.selectedIndex<nCurrentMonth) 
		formYear.value=currentYear+1;
	else
		formYear.value=currentYear;
}

function AdvSearchFormWriter( tm_can_share ) {
	
	document.write('<div id=tm_advsearchform_toggle_div align=right><a id=tm_advsearchform_toggle href="javascript:toggleADV();"><img border=0 id=tm_advsearchform_img src=/review/i/minus.png></a></div><div id=tm_advsearchform><form onSubmit="return errorChecker();" method=POST action=/rate/><input type=hidden name=a value=SearchLocation><input type=hidden name=forwardmode value=tm_newsearch>');
	
	document.write('<form method=POST>');

	document.write('<table width="100%" align=center style="background-color: #DEF0F5;">');
	
	
	//maprow. -1 
	document.write('<tr>');
//	document.write('<td class=dataLabel valign=top>Search Area:</td>');
//	document.write('<td colspan="3" class=dataField>');
//	document.write('<span id=search_area_label></span>');
//	document.write('&nbsp;<a href="javascript:openChildWindow();">change</a>');
//	document.write('</td>');
//	document.write('<td align=center valign=top rowspan=4 style="padding-right: 8px;" > ');
	//document.write('<div id=g_map style=" width: 230px; height: 112px; cursor:pointer; cursor:hand; border: 1px solid #979797; position: relative; left: -10px;"></div>');
//	document.write('</td>');
	document.write('</tr>');
	
	//rec row - 2
	document.write('<tr>');
	document.write('<td class=dataLabel>Recommended:</td>');
	document.write('<td class=dataField>');
	document.write('<select name=recommended_percent id=recommended_percent>');
	document.write('<option value="">any</option>');
	document.write( strOptPercent );
	document.write('</select>');
//	document.write('</td>');
//	document.write('<td class=dataLabel><div style="position: relative; left: -50px;">Ratings:</div></td>');
//	document.write('<td class=dataField><div style="position: relative; left: -50px;">');
	document.write('   <b>Ratings:</b> ');
	document.write('<select name=recommended_reviews id=recommended_reviews>');
	document.write('<option value="">any</option>');
	document.write( strOptReviews );
	document.write('</select>');
	document.write('</td>');
	document.write('</tr>');
	
	
	// B&B row - 4
	document.write('<tr>');
	document.write('<td class=dataLabel>Beds & Baths:</td>');
	document.write('<td class=dataField>');
	document.write(' <select name=beds id=beds>');
	document.write(' <option value="">-</option> ');
	document.write( strOptBeds );
	document.write('</select>');
	document.write('<select name=baths id=baths>');
	document.write( strOptBaths );
	document.write('</select>');
	document.write('</td>');
	document.write('</tr>');
		
	// rent row - 5
	document.write('<tr>');
	document.write('<td class=dataLabel>Rent Amount:</td>');
	document.write('<td class=dataField colspan=2>');
	document.write('<select name=price_bottom id=price_bottom>');
	document.write(' <option value="">-</option> ');
	document.write( strOptPriceMin );
	document.write('</select>');
	document.write('&nbsp;');
	document.write('<span class=dataLabel>to</span>');
	document.write('&nbsp;');
	document.write('<select name=price_top id=price_top>');
	document.write(' <option value="">-</option> ');
	document.write( strOptPriceMax );
	document.write('</select>');	document.write('</td>');
	document.write('<td>');
	document.write('</td>');
	document.write('</tr>');
	
	// movein - 6
	document.write('<tr>');
	document.write('<td class=dataLabel valign=top ><span id=aster>*</span>Move in date:</td>');
	document.write('<td class=dataField colspan=2>');
	document.write('<select name=month id=month onChange="setYear();"> <option value="">Month</option><option value=01>Jan</option><option value=02>Feb</option><option value=03>Mar</option><option value=04>Apr</option><option value=05>May</option><option value=06>Jun</option><option value=07>Jul</option><option value=08>Aug</option><option value=09>Sep</option><option value=10>Oct</option><option value=11>Nov</option><option value=12>Dec</option> </select>');
	
	document.write('<select name=day id="lol_day"><option value="">Day</option><option value=01>01</option><option value=02>02</option><option value=03>03</option><option value=04>04</option><option value=05>05</option><option value=06>06</option><option value=07>07</option><option value=08>08</option><option value=09>09</option><option value=10>10</option><option value=11>11</option><option value=12>12</option><option value=13>13</option><option value=14>14</option><option value=15>15</option><option value=16>16</option><option value=17>17</option><option value=18>18</option><option value=19>19</option><option value=20>20</option><option value=21>21</option><option value=22>22</option><option value=23>23</option><option value=24>24</option><option value=25>25</option><option value=26>26</option><option value=27>27</option><option value=28>28</option><option value=29>29</option><option value=30>30</option><option value=31>31</option></select>');
	
	document.write('<input type=hidden value="" name=year id=year>');
	document.write('<input type=hidden name=hidden_date id=hidden_date value="'+hiddenDate+'" onChange=updateDate()>');
	document.write('<img src=/review/i/cal.png id="trigger" style="cursor: pointer; position: relative; top: 6px;">');
	document.write('</td>');
	document.write('<td>');
	document.write('</td>');
	document.write('<td align=center style="vertical-align: top;">');
//	document.write(' &nbsp;&nbsp;&nbsp;<input type=button style="background-color: #A73735;color: white;font-family: Tahoma, verdana, sans-serif; font-size: 10px;font-weight: bold;height: 18px; position: relative; left: -10px;" onClick=" openChildWindow(); " value="Change Search Area">');
	document.write('</td>');
	document.write('</tr>');
	
	// share - 7 
	document.write('<tr>');
	document.write('<td class=dataLabel></td>');
	document.write('<td class=dataField colspan=4 align=left>');
	document.write('<input name=can_share_to_TM id=can_share_to_TM type=checkbox  CHECKED style="position: relative; left: -3px;"> Match and share my search with individual landlords <sup><a helpbox="<img src=/review/images/jointenantmarket.gif>');
	document.write('<br>Tell landlords what you want and receive personalized offers for rentals that exactly match your criteria.');
	document.write('<br/><br/>');
	document.write('&#149;&nbsp;Good renters get even better &nbsp;&nbsp;deals!<br/>');
	document.write('&#149;&nbsp;Discover unlisted and &nbsp;&nbsp;hard-to-find properties<br/>');
	document.write('&#149;&nbsp;Save time! Rentals come to &nbsp;&nbsp;you<br/>');
	document.write('&#149;&nbsp;100% control of your &nbsp;&nbsp;personal information<br/><br/>');
	document.write('<img src=/review/images/whenlandlords.gif>"><img src=/review/i/help/blue_q.gif></a></sup>');

	document.write('<input type=hidden name=asform_lat id=asform_lat value="'+strLat+'">');
	document.write('<input type=hidden name=asform_lon id=asform_lon value="'+strLon+'">');
	document.write('<input type=hidden id=radius_distance name=radius_distance value="'+strRadDist+'">');
	document.write('<input type=hidden id=radius_address name=radius_address value="'+strRadAddr+'">');

	document.write('<input type=hidden id=zip name=zip value="'+strZip+'">');
	document.write('<input type=hidden id=loc_city name=city value="'+strCity+'">');
	document.write('<input type=hidden id=loc_state name=state value="'+strState+'">');
	document.write('<input type=hidden id="hTrigger" value="" >');
	document.write('<input type=hidden id=loc_custom name=loc_custom value="">');
	
	document.write('<input type=hidden id=error name=error value="">');

	document.write('</td>');
	document.write('</tr>');
	
	document.write('<tr>');
	document.write('<td></td>');
	document.write('<td align=left colspan=4><input type="image" alt="Search" src="/review/images/btn_go_plain.gif" name="submit" value="  Search  " id="submit" ><br /></td>');
	document.write('</tr>');
	document.write('</table></form></div>');

	Calendar.setup(
		{
			inputField  : "hidden_date",	// ID of the input field
			ifFormat    : "%Y-%m-%d",	// the date format
			button      : "trigger"		// ID of the button
		}
	);
	
	updateDate();
	
	setupAddr();
	
	setupNiceHelpByTagname( 'a' );
}
