var bNeighb1CanBeOn = false;
var bNeighb2CanBeOn = false;

function cpt_neighb0_change()
{
	fldN = document.getElementById( 'neighborhood' );

	if( fldN.selectedIndex > 0 )
	{
		bNeighb1CanBeOn = true;

		document.getElementById( 'add_another_1' ).style.color = "#3A8BB3";
	}
	else
	{
		bNeighb1CanBeOn = false;
		toggleAddNeighb1( false );
		toggleAddNeighb2( false );

		document.getElementById( 'add_another_1' ).style.color = "#bbbbbb";
	}
}

function cpt_neighb1_change()
{
	fldN = document.getElementById( 'neighborhood_1' );

	if( fldN.selectedIndex > 0 )
	{
		bNeighb2CanBeOn = true;

		document.getElementById( 'add_another_2' ).style.color = "#3A8BB3";
	}
	else
	{
		bNeighb2CanBeOn = false;

		document.getElementById( 'add_another_2' ).style.color = "#bbbbbb";
	}
}

function showNeighb1()
{
	toggleAddNeighb1( bNeighb1CanBeOn );
}

function showNeighb2()
{
	toggleAddNeighb2( bNeighb2CanBeOn );
}

function toggleAddNeighb1( turnOn )
{
	var myElem = document.getElementById( 'extra_neighb_block_1' );

	if( turnOn )
	{
		myElem.style.display = 'block';
	}
	else
	{
		myElem.style.display = 'none';
		document.getElementById( 'add_another_1' ).style.color = "#bbbbbb";
		hideNewNeighb1();
		hideNewNeighb2();
	}
}

function toggleAddNeighb2( turnOn )
{
	var myElem = document.getElementById( 'extra_neighb_block_2' );

	if( turnOn )
	{
		myElem.style.display = 'block';
	}
	else
	{
		myElem.style.display = 'none';
		document.getElementById( 'add_another_2' ).style.color = "#bbbbbb";
		hideNewNeighb2();
	}
}

function hideNewNeighb()
{
	try {

		var h1 = document.getElementById( 'neighbhide' );
		var h2 = document.getElementById( 'neighbhide2' );
		var old_neigh = document.getElementById( 'neighbhide_old' );
		var myInp = document.getElementById( 'newneighb' );

		myInp.value = '';
		h1.style.display = 'none';
		h2.style.display = 'none';
		old_neigh.style.display = 'block';

		bNeighb1CanBeOn = false;
		bNeighb2CanBeOn = false;

		showNeighb1()
		showNeighb2()
	} catch( ig ) {}
}

function cityChange()
{
	var state = document.getElementById( 'tm_state' );
	var city = document.getElementById( 'tm_city' );

	clearNeighborhoods();

	if( city.selectedIndex > 0 && city.options[city.selectedIndex].value != strJsNewActionToken )
	{
		loadXMLDoc_repop( "../CsnPop?state=" + state.value + "&city=" + city.value ) ;
	}
	else if( city.selectedIndex > 0 )
	{
		showNewCity();
		showNewNeighb();
	}
}


function syncOptions()
{
	n0 = document.getElementById( 'neighborhood' );
	n1 = document.getElementById( 'neighborhood_1' );
	n2 = document.getElementById( 'neighborhood_2' );

	n1.options.length = 0;
	n2.options.length = 0;

	for( i=0; i<n0.options.length; i++ )
	{
		n1.options[i] = new Option();
		n1.options[i].text  = n0.options[i].text;
		n1.options[i].value = n0.options[i].value;

		n2.options[i] = new Option();
		n2.options[i].text  = n0.options[i].text;
		n2.options[i].value = n0.options[i].value;
	}
}

function populateNeighborhoods_callback()
{
	syncOptions();
}

function neighbChange1()
{
	var seltag = document.getElementById( 'neighborhood_1' );
	var fk = document.getElementById( 'neighborhood_fk_1' );

	if( seltag.options[seltag.selectedIndex].value == strJsNewActionToken )
	{
		fk.value = "-1";
		showNewNeighb1();
	}
	else
	{
		fk.value = seltag.options[seltag.selectedIndex].value;
	}
}

function neighbChange2()
{
	var seltag = document.getElementById( 'neighborhood_2' );
	var fk = document.getElementById( 'neighborhood_fk_2' );

	if( seltag.options[seltag.selectedIndex].value == strJsNewActionToken )
	{
		fk.value = "-1";
		showNewNeighb2();
	}
	else
	{
		fk.value = seltag.options[seltag.selectedIndex].value;
	}
}

function showNewNeighb1()
{
	var h1 = document.getElementById( 'neighbhide_1' );
	var h2 = document.getElementById( 'neighbhide2_1' );
	var old_neigh = document.getElementById( 'neighbhide_old_1' );

	h1.style.display = 'block';
	h2.style.display = 'block';
	old_neigh.style.display = 'none';
}

function showNewNeighb2()
{
	var h1 = document.getElementById( 'neighbhide_2' );
	var h2 = document.getElementById( 'neighbhide2_2' );
	var old_neigh = document.getElementById( 'neighbhide_old_2' );

	h1.style.display = 'block';
	h2.style.display = 'block';
	old_neigh.style.display = 'none';
}

function hideNewNeighb1()
{
	try {
		var h1 = document.getElementById( 'neighbhide_1' );
		var h2 = document.getElementById( 'neighbhide2_1' );
		var old_neigh = document.getElementById( 'neighbhide_old_1' );
		var myInp = document.getElementById( 'newneighb_1' );

		myInp.value = '';
		h1.style.display = 'none';
		h2.style.display = 'none';
		old_neigh.style.display = 'block';
	} catch( ig ) {}
}

function hideNewNeighb2()
{
	try {
		var h1 = document.getElementById( 'neighbhide_2' );
		var h2 = document.getElementById( 'neighbhide2_2' );
		var old_neigh = document.getElementById( 'neighbhide_old_2' );
		var myInp = document.getElementById( 'newneighb_2' );

		myInp.value = '';
		h1.style.display = 'none';
		h2.style.display = 'none';
		old_neigh.style.display = 'block';
	} catch( ig ) {}
}

function checkForm()
{
	var lease_24 = document.getElementById( 'opt_mlt_2_years' )
	var lease_12 = document.getElementById( 'opt_mlt_1_year' )
	var lease_06 = document.getElementById( 'opt_mlt_6_months' )
	var lease_18 = document.getElementById( 'opt_mlt_18_months' )
	var lease_09 = document.getElementById( 'opt_mlt_9_months' )
	var lease_mm = document.getElementById( 'opt_mlt_month_to_month' )
	var lease_ne = document.getElementById( 'opt_mlt_any' )

	var price = document.getElementById( 'price' )

/*
	var state = document.getElementById( 'tm_state' )
	var city = document.getElementById( 'tm_city' )
	var new_city = document.getElementById( 'newcity' )
	var new_city_hide = document.getElementById( 'cityhide' );

	if( !state.value )
	{
		showNiceError( state, "Please pick the state your property is in." );
		return false;
	}

	if( !city.value || city.value == '~~*~~' )
	{
		if( new_city_hide.style.display == 'block' )
		{
			if( !new_city.value )
			{
				showNiceError( new_city, "Please enter the name of the city your property is in." );
				return false;
			}
		}
		else
		{
			showNiceError( city, "Please enter the name of the city your property is in." );
			return false;
		}
	}
*/
	var zip = document.getElementById( 'zip' );
	var tm_city = document.getElementById( 'tm_city' );
	var tm_state = document.getElementById( 'tm_state' );
	var lat = document.getElementById( 'lat' );
	var lon = document.getElementById( 'lon' );

	if( !tm_city.value || !tm_state.value || !lat.value || !lon.value )
	{
		showPinkErr( "", zip, "You need to specify the zip code that the property you want to rent is in." );
		return false;
	}

	if
	(
		!lease_24.checked &&
		!lease_12.checked &&
		!lease_06.checked &&
		!lease_18.checked &&
		!lease_09.checked &&
		!lease_mm.checked &&
		!lease_ne.checked
	) {
		showPinkErr( "", lease_18, "You need to pick at least one kind of lease you'd like a tenant for." );
		return false;
	}

	try {
		var nPrice = price.options[seltag.selectedIndex].value;
		nPrice = (+nPrice);

		if( !nPrice || nPrice < 50 )
		{
			showPinkErr( "", price, "Please enter the tenant's maximum rent budget." );
			return false;
		}
	} catch(ig) {}

	return true;
}


function showPinkErr( jumpName, msgNode, msg )
{
	setNiceTitleProps( "/aptsrentals/js/lt_pink_ptr2.png", "/aptsrentals/js/char_x.gif", "eviltextCell", "nicetable" );
	showNiceError( msgNode, msg );

	jump( ""+jumpName );

	return false;
}

function showBlueMsg( jumpName, msgNode, msg )
{
	setNiceTitleProps( "/aptsrentals/js/lt_blue_ptr2.gif", "/aptsrentals/js/char_x.gif", "helptextCell", "nicetable" );
	showNiceError( msgNode, msg );

	jump( ""+jumpName );

	return false;
}

var CHECKMODE_UNCHECKED = 0;
var CHECKMODE_BAD = 1;
var CHECKMODE_GOOD = 2;

var __addr_checkmode = CHECKMODE_UNCHECKED;

function cpdAddrOnChange(){
	closeAllOpenHelps();
	__addr_checkmode = CHECKMODE_UNCHECKED;
}

function lookup_addr()
{
	var l_addr  	= document.getElementById( 'loc_streetaddr' );
	var l_state 	= document.getElementById( 'tm_state' );
	var l_city  	= document.getElementById( 'tm_city' );
	var l_zip	= document.getElementById( 'loc_zip' );

	var url = "../AddrLookup/?m=CPD&q=" + escape( l_addr.value );
	url += "," + escape( l_state.value );
	url += "," + escape( l_city.value );
	url += "," + escape( l_zip.value );

	/// execute the Ajax call, which will asynchronously call addrFinisher() when it's done.
	AjaxRequest.get(
	  {
	    'url': ''+url
	    ,'onSuccess':function(req){ addrFinisher(req); }
	    ,'onError':function(req){ alert('Error!\n onAddrLookup() :(' );}
	  }
	);
}

function addrFinisher( req )
{
	var mySubFrm = document.getElementById( 'myCpdForm' );

	try
	{
		var xmlDoc = req.responseXML;

		var arLoc = xmlDoc.documentElement.getElementsByTagName("location");
		var arErr = xmlDoc.documentElement.getElementsByTagName("error");

		var latNode = document.getElementById( 'loc_lat' );
		var lonNode = document.getElementById( 'loc_lon' );

		if( arLoc[0] )
		{
			latNode.value  = arLoc[0].getAttribute("lat");
			lonNode.value  = arLoc[0].getAttribute("lon");

			__addr_checkmode = CHECKMODE_GOOD;
		}
		else
		{
			__addr_checkmode = CHECKMODE_BAD;
		}
/*
		else if( arErr[0] )
		{
			showPinkErr( "", addrEntryBttn, ""+arErr[0].getAttribute("msg") );
		}
		else
		{
			showPinkErr( "", addrEntryBttn, "We could not find that address.  Please check it again." );
		}
*/
	}
	catch( e )
	{
		alert( "addrf err: " + e );
		__addr_checkmode = CHECKMODE_BAD;
	}

	if( checkCpdForm() )
		mySubFrm.submit();
}


function checkCpdForm()
{
	var l_addr  	= document.getElementById( 'loc_streetaddr' );
	var l_state 	= document.getElementById( 'tm_state' );
	var l_city  	= document.getElementById( 'tm_city' );
	var l_new_city	= document.getElementById( 'newcity' );
	var l_zip	= document.getElementById( 'loc_zip' );

	if( !l_addr.value )
	{
		return showPinkErr( "targ_topmost", l_addr, "You did not enter an address." );
	}

	if( !l_state.value )
	{
		return showPinkErr( "targ_topmost", l_state, "You did not choose a valid state." );
	}

	if( !l_city.value /*&& !l_new_city.value*/ )
	{
		return showPinkErr( "targ_topmost", l_city, "You did not enter a city." );
	}

	if( !l_zip.value )
	{
		return showPinkErr( "targ_topmost", l_zip, "You did not enter a zip code." );
	}

	// Once we've verified there's actually stuff in the addr fields, *then* verify the place actually exists.
	//
	if( __addr_checkmode == CHECKMODE_UNCHECKED )
	{
		lookup_addr();
		return false;
	}
	else if( __addr_checkmode == CHECKMODE_BAD )
	{
		return showPinkErr( "targ_topmost", l_addr, "This address, city, state, and/or zip cannot be found in our maps.  Please double-check to see that you entered the correct data.  If you did, COPY COPY COPY (contact admin option???)." );
		return false;
	}

	var rtype1 = document.getElementById('rental_type_single_family_house');
	var rtype2 = document.getElementById('rental_type_townhouse_condo');
	var rtype3 = document.getElementById('rental_type_garage_apartment_cottage');
	var rtype4 = document.getElementById('rental_type_duplex_fourplex');
	var rtype5 = document.getElementById('rental_type_apartment');
	var rtype6 = document.getElementById('rental_type_room_for_rent_roommate');

	if( !rtype1.checked && !rtype2.checked && !rtype3.checked && !rtype4.checked && !rtype5.checked && !rtype6.checked )
	{
		return showPinkErr( "targ_rentaltype", rtype1, "You did not choose a rental type." );
	}


	var mlt1 = document.getElementById('opt_mlt_any');
	var mlt2 = document.getElementById('opt_mlt_month_to_month');
	var mlt3 = document.getElementById('opt_mlt_9_months');
	var mlt4 = document.getElementById('opt_mlt_18_months');
	var mlt5 = document.getElementById('opt_mlt_6_months');
	var mlt6 = document.getElementById('opt_mlt_1_year');
	var mlt7 = document.getElementById('opt_mlt_2_years');

	if( !mlt1.checked && !mlt2.checked && !mlt3.checked && !mlt4.checked && !mlt5.checked && !mlt6.checked && !mlt7.checked )
	{
		return showPinkErr( "targ_mlt", mlt1, "You did not choose any desired lease terms." );
	}

	var beds1 = document.getElementById('rental_0');
	var beds2 = document.getElementById('rental_1');
	var beds3 = document.getElementById('rental_2');
	var beds4 = document.getElementById('rental_3');
	var beds5 = document.getElementById('rental_4');
	var beds6 = document.getElementById('rental_5');
	var beds7 = document.getElementById('rental_6');

	if( beds1 && beds2 && beds3 && beds4 && beds5 && beds6 && beds7 )
	{
		if( !beds1.checked && !beds2.checked && !beds3.checked && !beds4.checked && !beds5.checked && !beds6.checked && !beds7.checked )
		{
			return showPinkErr(
				"targ_beds",
				document.getElementById('bed_err_post'),
				"You did not select the number of bedrooms your unit has."
			);
		}
	}

	var bath1 = document.getElementById('baths_1');
	var bath2 = document.getElementById('baths_15');
	var bath3 = document.getElementById('baths_2');
	var bath4 = document.getElementById('baths_25');
	var bath5 = document.getElementById('baths_3');
	var bath6 = document.getElementById('baths_35');
	var bath7 = document.getElementById('baths_4');

	if( bath1 && bath2 && bath3 && bath4 && bath5 && bath6 && bath7 )
	{
		if( !bath1.checked && !bath2.checked && !bath3.checked && !bath4.checked && !bath5.checked && !bath6.checked && !bath7.checked )
		{
			return showPinkErr(
				"targ_beds",
				document.getElementById('bath_err_post'),
				"You did not select the number of bathrooms your unit has."
			);
		}
	}

	var price = document.getElementById('price');
	var sqrft = document.getElementById('square_footage');
	var deposit = document.getElementById('deposit');

	var nPrice = 0;
	try {
		nPrice = parseInt( price.value );
	} catch(ig){}

	var nSqft = 0;
	try {
		nSqft = parseInt( sqrft.value );
	} catch(ig){}

	var nDpst = 0;
	try {
		nDpst = parseInt( deposit.value );
	} catch(ig){}

/*
	if( price.value == "" ){
		return showPinkErr( "targ_price", price, "You did not give a valid price." );
	}
*/
/*
	if( !nPrice )
	{
		return showPinkErr( "targ_price", price, "You did not give a valid price." );
	}

	if( !nSqft )
	{
		return showPinkErr( "targ_price", sqrft, "You did not give a valid amount of square feet." );
	}
*/

	if( !nDpst )
	{
		return showPinkErr( "targ_price", deposit, "You did not give a valid deposit." );
	}

	var c_name = document.getElementById('contact_name');
	var c_mail = document.getElementById('contact_email');
	var c_phon = document.getElementById('contact_phone');

	if( !c_name.value )
	{
		return showPinkErr( "targ_contact", c_name, "You did not give a contact name." );
	}

	if( !Validate_Email_Address(c_mail.value) )
	{
		return showPinkErr( "targ_contact", c_mail, "You did not give a valid contact email." );
	}

	if( !c_phon.value )
	{
		return showPinkErr( "targ_contact", c_phon, "You did not give a contact phone number." );
	}

	return true;
}







function checkAptsRentalsForm(){
	var l_addr  	= document.getElementById( 'loc_streetaddr' );
	var l_state 	= document.getElementById( 'tm_state' );
	var l_city  	= document.getElementById( 'tm_city' );
	var l_new_city	= document.getElementById( 'newcity' );
	var l_zip	= document.getElementById( 'loc_zip' );

	if( !l_state.value ){
		return showPinkErr( "targ_topmost", l_state, "You did not choose a valid state." );
	}

	if( !l_city.value /*&& !l_new_city.value*/ ){
		return showPinkErr( "targ_topmost", l_city, "You did not enter a city." );
	}

	// Crazy complex verification... makes my mind hurt >_<
	////////
	if( document.getElementById("rental_type_apartment").checked == true ){
		var complexList  = document.getElementById("complexList");
		var selectedComplex = complexList.options[complexList.selectedIndex].value;

		if( selectedComplex == "none" ){
			return showPinkErr( "targ_topmost", complexList, "You must select an apartment complex." );
		}
		else if( selectedComplex == "-1" ){
			var newComplexField  = document.getElementById("newComplexField");
			if( newComplexField.value == "" ){
				return showPinkErr( "targ_topmost", newComplexField, "You must type in the apartment complex name." );
			}

			l_addr = document.getElementById( 'loc_streetaddr2' );
			if( !l_addr.value ){
				return showPinkErr( "targ_topmost", l_addr, "You did not enter an address." );
			}

			l_zip = document.getElementById( 'loc_zip2' );
			if( !l_zip.value ){
				return showPinkErr( "targ_topmost", l_zip, "You did not enter a zip code." );
			}
		}
	}
	else{
		if( !l_addr.value ){
			return showPinkErr( "targ_topmost", l_addr, "You did not enter an address." );
		}
		if( !l_zip.value ){
			return showPinkErr( "targ_topmost", l_zip, "You did not enter a zip code." );
		}
	}

	var rtype1 = document.getElementById('rental_type_single_family_house');
	var rtype2 = document.getElementById('rental_type_townhouse_condo');
	var rtype3 = document.getElementById('rental_type_garage_apartment_cottage');
	var rtype4 = document.getElementById('rental_type_duplex_fourplex');
	var rtype5 = document.getElementById('rental_type_apartment');
	var rtype6 = document.getElementById('rental_type_room_for_rent_roommate');

	if( !rtype1.checked && !rtype2.checked && !rtype3.checked && !rtype4.checked && !rtype5.checked && !rtype6.checked ){
		return showPinkErr( "targ_rentaltype", rtype1, "You did not choose a rental type." );
	}


	var mlt1 = document.getElementById('opt_mlt_any');
	var mlt2 = document.getElementById('opt_mlt_month_to_month');
	var mlt3 = document.getElementById('opt_mlt_9_months');
	var mlt4 = document.getElementById('opt_mlt_18_months');
	var mlt5 = document.getElementById('opt_mlt_6_months');
	var mlt6 = document.getElementById('opt_mlt_1_year');
	var mlt7 = document.getElementById('opt_mlt_2_years');

	if( !mlt1.checked && !mlt2.checked && !mlt3.checked && !mlt4.checked && !mlt5.checked && !mlt6.checked && !mlt7.checked ){
		return showPinkErr( "targ_mlt", mlt1, "You did not choose any desired lease terms." );
	}

	var unitCount = document.myCpdForm.unit_count.value;
	for(var i=0; i<unitCount; i++){
		var price = document.myCpdForm['price_'+i];
		if( price.value == "" ){
			return showPinkErr( "targ_price_"+i, price, "You did not give a price." );
		}

		var beds = document.myCpdForm['beds_'+i];
		for(var j=0; j<document.myCpdForm['beds_'+i].length; j++){
			if( document.myCpdForm['beds_'+i][j].checked ) beds = null;
		}
		if( beds != null ) return showPinkErr("targ_beds_"+i, document.getElementById('bed_err_post_'+i), "You did not select the number of bedrooms your unit has." );

		var baths = document.myCpdForm['baths_'+i];
		for(var j=0; j<document.myCpdForm['baths_'+i].length; j++){
			if( document.myCpdForm['baths_'+i][j].checked ) baths = null;
		}
		if( baths != null ) return showPinkErr("targ_beds_"+i, document.getElementById('bath_err_post_'+i), "You did not select the number of bathrooms your unit has." );
	}

	var c_phon = document.getElementById('contact_phone');
	var c_name = document.getElementById('contact_name');
	var c_name_last = document.getElementById('contact_name_last');
	var c_mail = document.getElementById('contact_email');

	if( !c_name.value ){
		return showPinkErr( "targ_contact", c_name, "You did not give a contact name." );
	}

	if( c_name.value.length < 3 || c_name_last.value.length < 3 ){
		return showPinkErr( "targ_contact", c_name, "There's a 3 character minimum for first and last names" );
	}

	if( !Validate_Email_Address(c_mail.value) ){
		return showPinkErr( "targ_contact", c_mail, "You did not give a valid contact email." );
	}

	if( !c_phon.value ){
		return showPinkErr( "targ_contact", c_phon, "You did not give a contact phone number." );
	}

	var stripNonDigits = new RegExp("\\D", "g");
	if( c_phon.value.replace(stripNonDigits, "").length != 10 ){
		return showPinkErr( "targ_contact", c_phon, "Phone number must have 10 digits. (###) ###-####" );
	}

	return true;
}