//<![CDATA[
 
////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS FOR INITIAL PAGE LOAD
////////////////////////////////////////////////////////////////////////////////////////////////
// used to identify and log in users
// loggedin is used to identify property tracker accounts
// signed is used to verify that user has completed manadatory registration form
var qs;
var siteid = '9';
var default_mls_region = '3';
var userid = '';
var siusername = '';
var sipassword = '';
var loggedin = false;
var signedin = true;
var regionselected = false;
var numSearches = 0;
var numListings = 0;

// used in save searches
var searchid;
var quicksearchid;
var newonly;
var favs;
var mls;
var mlsregionid;

// used for processing saved listing after login
var savelistingid = '';
var savemlsregionid = '';
var savefrom = '';
var savemarker = '';

// These are used in loading the login / register containers
var whichContainer_welcome = 'welcome';
var whichContainer_results = 'results';

var saveToFavs_map = 'map';
var saveToFavs_mls = 'mls';
var saveToFavs_detail = 'detail';
var saveToFavs_search = 'searchform';

dojo.addOnLoad(function(){	
	loadUser();							
});

function loadUser() {	
	// Retrieve user info from cookies
	if (readCookie('siUserID')) {userid = readCookie('siUserID')} else {userid = ''}
	if (readCookie('siID')) {siusername = readCookie('siID');} else {siusername = '';}
	if (readCookie('siPW')) {sipassword = readCookie('siPW');} else {sipassword = '';}
	
	// Check if user is logged in
	if (readCookie('siLoggedIn')) {
		if (readCookie('siLoggedIn') == 'true') {
			if ((userid.length >= 1) && (siusername.length >= 1) && (sipassword.length >= 1)) {loggedin = true; signedin = true} else {loggedin = false}
		}
	}
	
	if (signedin == false) {
		if (readCookie('siSignedIn') == 'true') {
			signedin = true;
		}
	}
}

function full_search_from_favorites(searchType) {
	dojo.byId('userid').value = userid;
	dojo.byId('searchid').value = '';
	dojo.byId('favs').value = 'true';
	full_search(searchType);	
}

function run_saved_search(searchid, searchType) {
	dojo.byId('userid').value = userid;
	dojo.byId('searchid').value = searchid;
	dojo.byId('favs').value = 'false';
	full_search(searchType);	
}

function sidebarUpdateSavedListingText(num) {
	var numSavedListings = dojo.byId('numSavedListings');
	var showSavedListings = dojo.byId('showSavedListings');
	var noSavedListings = dojo.byId('noSavedListings');
	
	if (num == 1) {
		dojo.event.browser.clean(numSavedListings);
		numSavedListings.innerHTML = 'You currently have 1 saved listing.';
		dojo.style.show(showSavedListings);
		dojo.style.hide(noSavedListings);
	} else if (num >=2) {
		dojo.event.browser.clean(numSavedListings);
		numSavedListings.innerHTML = 'You currently have ' + num + ' saved listings.';
		dojo.style.show(showSavedListings);
		dojo.style.hide(noSavedListings);
	} else {
		dojo.event.browser.clean(numSavedListings);
		numSavedListings.innerHTML = 'You do not currently have any saved listings. <a href="javascript:newSearch();">Click here to search the area listings</a>.<br /><br />Once you save listings, you can email or request more information on all of your saved listings at once.';
		dojo.style.hide(showSavedListings);
		dojo.style.show(noSavedListings);
	}
}

function sidebarUpdateSavedSearchesText(num) {
	var numSavedSearches = dojo.byId('numSavedSearches');
	var runSavedSearches = dojo.byId('runSavedSearches');
	var viewAllSearches = dojo.byId('viewAllSearches');
	var noSavedSearches = dojo.byId('noSavedSearches');	
	
	if (num == 1) {
		dojo.event.browser.clean(numSavedSearches);
		numSavedSearches.innerHTML = 'You have 1 saved search.';
		dojo.style.hide(noSavedSearches);
		dojo.style.show(runSavedSearches);
	} else if (num >=2) {
		dojo.event.browser.clean(numSavedSearches);
		numSavedSearches.innerHTML = 'You have ' + num + ' saved searches.';
		dojo.style.hide(noSavedSearches);
		dojo.style.show(runSavedSearches);
		
	} else {
		dojo.event.browser.clean(numSavedSearches);
		numSavedSearches.innerHTML = 'You do not currently have any saved searches.';
		dojo.style.show(noSavedSearches);
		dojo.style.hide(runSavedSearches);
	}
}

function logout_automatic() {
		eraseCookie('siID');
		eraseCookie('siPW');
		eraseCookie('siUserID');
		eraseCookie('siLoggedIn');
		
		// clear user data from javascript variables
		userid = '';
		siusername = '';
		sipassword = '';
		
		// Also need to remove user id from search form hidden fields
		if (dojo.byId('userid')) {
			dojo.byId('userid').value = '';
		}
		
		if (dojo.byId('searchid')) {
			dojo.byId('searchid').value = '';
		}
		
		loggedin = false;
}

function showVirtualTour(theURL) {
	window.open(theURL, 'virtualtour', 'width=600, height=600, resizable=yes, scrollbars=yes, toolbar=no, menubar=no')
}

////////////////////////////////////////////////////////////////////////////////////////////////
// FULL SEARCH FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////
// Runs the search and populates search results and map results tabs
function full_search(searchType) {	
	// Put form data into querystring and form URL to pass to XML map results	
	var formAction;
	if (searchType == '1') {
		formAction = 'search_form.asp';
	} else if (searchType == '2') {
		formAction = 'mls.asp';
	} else if (searchType == '3') {
		formAction = 'mls_maps.asp';
	} else if (searchType == '4') {
		formAction = 'mls_detail.asp';
	} else if (searchType == '5') {
		formAction = 'property_tracker.asp';
	} else {
		formAction = '';
	}
	
	document.frmSearch.action = formAction;
	
	document.frmSearch.submit();
}

// Clears the quicksearchid form field and runs the full search
function full_search_from_form(searchType) {
	clearHiddenFieldsFromSearchForm();	
	full_search(searchType);
}

// Clears the search form to run a new search
function newSearch() {
	dojo.byId('userid').value = userid;
	dojo.byId('searchid').value = '';	
	dojo.byId('favs').value = 'false';
	dojo.byId('hidaction').value = 'newsearch';
	full_search('1');
}

// Switches back to the search tab
function reviseSearch() {
	dojo.byId('hidaction').value = '';
	dojo.byId('searchid').value = '';
	full_search('1');
}

function show_save_for_current_search() {
	dojo.byId('userid').value = userid;
	dojo.byId('favs').value = 'false';
	dojo.byId('hidaction').value = 'currentsavedsearch';
	full_search('1');
}

// Loads Detail view for listing from Search Results or Map Results
function showDetailPage(listing_id) {	
	dojo.byId('detoffset').value='';
	dojo.byId('listingid').value=listing_id;
	full_search('4');	
}

function processSidebarLogin(whichContainer) {
	
	dojo.require("dojo.validate");
	dojo.require("dojo.validate.check");
	dojo.require("dojo.validate.datetime");
	dojo.require("dojo.validate.us");
	dojo.require("dojo.validate.web");	
	
	var bindArgs = {
	sync: true,
	url: 'pages/login_process.asp',
	method: 'post',
	content: {
		email: dojo.byId('sidebarloginemail').value,
		password: dojo.byId('sidebarloginpassword').value,
		siteid: siteid
	},
	error: function(type, data, evt){
		alert('An error occurred and we could not send your request.  Please try again.');	
	},
	load: function(type, data, evt){
		var returnVal = parseFloat(data);
		
		if (returnVal <= 0) { // incorrect username or password
			dojo.style.show(dojo.byId('sidebar_login_incorrect'));
		} else { // Login was successful -- 			
			//  set the javascript variables to logged in
			userid = returnVal;
			loggedin = true;
			siusername = dojo.byId('sidebarloginemail').value;
			sipassword = dojo.byId('sidebarloginpassword').value;
			if (dojo.byId('userid')) {
				dojo.byId('userid').value = userid;
			}			
			
			// If they checked the Remember Me box, then set cookies
			if (document.frmSidebarLogin.sidebarloginremember.checked) {
				createCookie('siUserID',userid,365);
				createCookie('siID',siusername,365);
				createCookie('siPW',sipassword,365);
				createCookie('siLoggedIn','true',365);
			} else {
				createCookie('siUserID',userid,1);
				createCookie('siID',siusername,1);
				createCookie('siPW',sipassword,1);
				createCookie('siLoggedIn','true',1);				
			}
			
				
			// If we started on the list results page, then reload that page to
			//  show any existing favorites
			if (savefrom == saveToFavs_mls) {
				full_search('2');
			} else if (savefrom == saveToFavs_map) {
				full_search('3');				
			} else if (savefrom == saveToFavs_search) {
				full_search('1');
			} else {
				full_search('0');
			}
		}
		
	},
	mimetype: 'text/html'
	};	
	
	// Validate the form contents before submitting
	var valProfile = {
		// filters change the field value and are applied before validation.
		trim: ["sidebarloginemail", "sidebarloginpassword"],
		uppercase: [],
		lowercase: [],
		ucfirst: [],
		digit: [],
		required: ["sidebarloginemail", "sidebarloginpassword"],
		dependancies:    {},
		constraints: {
			"sidebarloginemail" : [dojo.validate.isEmailAddress]
		},
		confirm: {}
	};

	var valResults = dojo.validate.check(dojo.byId('frmSidebarLogin'), valProfile);	
	
	if (valResults.isSuccessful()) {	
		dojo.io.bind(bindArgs);
	} else if (valResults.hasMissing()) {
		alert('Please enter both your email address and your password.');
		return;
	} else if (valResults.isInvalid('sidebarloginemail')) {
		alert('Please enter a valid email address.');
		dojo.byId('sidebarloginemail').focus();	
		return;
	}	

}

function processLogin(whichContainer) {
	
	dojo.require("dojo.validate");
	dojo.require("dojo.validate.check");
	dojo.require("dojo.validate.datetime");
	dojo.require("dojo.validate.us");
	dojo.require("dojo.validate.web");	
	
	dojo.byId('loginsubmit').value = 'Please Wait...';
	dojo.byId('loginsubmit').style.width = '120px';		
	
	var bindArgs = {
	sync: true,
	url: 'pages/login_process.asp',
	method: 'post',
	content: {
		email: dojo.byId('email').value,
		password: dojo.byId('password').value,
		siteid: siteid
	},
	error: function(type, data, evt){
		alert('An error occurred and we could not send your request.  Please try again.');
		dojo.byId('loginsubmit').value = 'Login';
		dojo.byId('loginsubmit').style.width = '80px';	
	},
	load: function(type, data, evt){
		var returnVal = parseFloat(data);
		
		if (returnVal <= 0) { // incorrect username or password
			dojo.style.show(dojo.byId('login_incorrect'));
			dojo.byId('loginsubmit').value = 'Login';
			dojo.byId('loginsubmit').style.width = '80px';				
		} else { // Login was successful -- 			
			//  set the javascript variables to logged in
			userid = returnVal;
			loggedin = true;
			siusername = dojo.byId('email').value;
			sipassword = dojo.byId('password').value;
			if (dojo.byId('userid')) {
				dojo.byId('userid').value = userid;
			}			
			
			// If they checked the Remember Me box, then set cookies
			if (document.frmLogin.loginremember.checked) {
				createCookie('siUserID',userid,365);
				createCookie('siID',siusername,365);
				createCookie('siPW',sipassword,365);
				createCookie('siLoggedIn','true',365);
			} else {
				createCookie('siUserID',userid,1);
				createCookie('siID',siusername,1);
				createCookie('siPW',sipassword,1);
				createCookie('siLoggedIn','true',1);				
			}
			
			// Now, it gets tricky
			// If they started the login process by clicking a Save to Favorites button,
			// we need to go back to the search results and show that listing as saved.
			// BUT, we also need to update the search results and map results pages to 
			// show ALL of their saved listings.
			if (whichContainer != 'results') { // They started from the property tracker home page
				savelistingid = '';
				savemlsregionid = '';
				savefrom = '';
				
				loadHomePage();
			} else { // otherwise....
				// If there is a listing to save, save it
				if ((savelistingid.length >= 1) && (savemlsregionid.length >= 1)) {
					var returnVal = saveListing(savelistingid, savemlsregionid, 'save');
				}
				
				// If we started on the list results page, then reload that page, and, once it's
				//   been reloaded, scroll down to the just-saved listing.
				if (savefrom == saveToFavs_mls) {
					dojo.byId('listingid').value = savelistingid;
					full_search('2');

				} else if (savefrom == saveToFavs_map) {
					
					if (returnVal >= 1) {
						dojo.byId('listingid').value = savelistingid;
						full_search('3');						
					} else {
						var coords = cumulativeOffset(dojo.byId('map'));
						window.scrollTo(0, coords[1]-20);							
						
						dojo.event.browser.clean(dojo.byId('map_message'));
						dojo.byId('map_message').innerHTML = "An error occurred (" + returnVal + ") and the listing could not be saved. Please try again."
						dojo.lfx.highlight(dojo.byId('map_message'), '#FFFF99', 800).play();			
						var anim4 = dojo.lfx.fadeHide(dojo.byId('map_message'), 500);
						var composed = dojo.lfx.chain(anim4)
						composed.play(8000);			
					}						

					savelistingid = '';
					savemlsregionid = '';
					savefrom = '';
				} else if (savefrom == saveToFavs_detail) {
					if (returnVal >= 1) {
						var coords = cumulativeOffset(dojo.byId('detail_message'));
						window.scrollTo(0, coords[1]-220);
						
						// change the button from Save Listing to Listing Saved
						dojo.event.browser.clean(dojo.byId('detSaveListing'));
						dojo.byId('detSaveListing').innerHTML = "<a href=\"javascript:removeFromFavorites('" + savelistingid + "', '" + savemlsregionid + "', 'detail',0);\"><img src=\"res/images/listings/det_listing_saved.gif\" border=\"0\" alt=\"Listing Saved - click to un-save\"></a>";
		
						// wipe in the notice that the listing has been saved, and then fade it out
						dojo.event.browser.clean(dojo.byId('login_register_results'));
						dojo.byId('login_register_results').innerHTML = '<fieldset style="width: 540px;"><legend>Success!</legend><strong>This listing has been saved.</strong> To view your Favorites, click the Property Tracker tab at the top.</fieldset>';
						dojo.lfx.highlight(dojo.byId('detail_message'), '#FFFF99', 800).play();
						
						var anim3 = dojo.lfx.fadeHide(dojo.byId(dojo.byId('detail_message')), 500);
						var composed2 = dojo.lfx.chain(anim3)
						composed2.play(8000);
					} else {
						var coords = cumulativeOffset(dojo.byId('detail_message'));
						window.scrollTo(0, coords[1]-220);						
						
						// wipe in the notice that the listing could not be saved, and then fade it out
						dojo.style.hide(dojo.byId('detail_message'));
						dojo.event.browser.clean(dojo.byId('detail_message'));
						dojo.byId('detail_message').innerHTML = '<fieldset style="width: 540px;"><legend>Save Listing</legend><strong>This listing could not be saved.</strong> Please make sure that you are logged in and try again.</fieldset>';
						var anim1 = dojo.lfx.wipeIn(dojo.byId('detail_message'), 500)
						var anim2 = dojo.lfx.highlight(dojo.byId('detail_message'), '#FFFF99', 800);
						var composed1 = dojo.lfx.chain(anim1, anim2);
						composed1.play(500);
						
						var anim3 = dojo.lfx.fadeHide(dojo.byId('detail_message'), 500);
						var composed2 = dojo.lfx.chain(anim3)
						composed2.play(8000);					
					}
					
					savelistingid = '';
					savemlsregionid = '';
					savefrom = '';					
				} else if (savefrom == 'searchform') {
					dojo.event.browser.clean(dojo.byId('searchform_message'));
					dojo.byId('searchform_message').innerHTML = '<fieldset style="width: 540px;"><legend>Login</legend><strong>Success!</strong> You are now logged in.  Please wait a moment while we load the form so that you can save the search.</fieldset><br /><br />';
					dojo.lfx.highlight(dojo.byId('searchform_message'), '#FFFF99', 800).play();
					var anim1 = dojo.lfx.wipeOut(dojo.byId('searchform_message'), 500);
					var anim2 = dojo.lfx.wipeIn(dojo.byId('savethissearch'), 500);
					var anim3 = dojo.lfx.highlight(dojo.byId('savethissearch'), '#FFFF99', 800).play();
					var composed1 = dojo.lfx.chain(anim1, anim2, anim3);
					composed1.play(4000);

					savelistingid = '';
					savemlsregionid = '';
					savefrom = '';					
					
				} else if (savefrom == 'unsubscribe') {
					dojo.event.browser.clean(dojo.byId('searchform_message'));
					dojo.byId('searchform_message').innerHTML = '<fieldset style="width: 540px;"><legend>Login</legend><strong>Success!</strong> You are now logged in.  Please wait a moment while we unsubcribe you from listing alerts for this search.</fieldset>';
					dojo.lfx.highlight(dojo.byId('searchform_message'), '#FFFF99', 800).play();
					
					setTimeout('unsubscribeSavedSearch()',4000);

					savelistingid = '';
					savemlsregionid = '';
					savefrom = '';					
				}
			}
		}
		
	},
	mimetype: 'text/html'
	};	
	
	// Validate the form contents before submitting
	var valProfile = {
		// filters change the field value and are applied before validation.
		trim: ["email", "password"],
		uppercase: [],
		lowercase: [],
		ucfirst: [],
		digit: [],
		required: ["email", "password"],
		dependancies:    {},
		constraints: {
			"email" : [dojo.validate.isEmailAddress]
		},
		confirm: {}
	};

	var valResults = dojo.validate.check(dojo.byId('frmLogin'), valProfile);	
	
	if (valResults.isSuccessful()) {	
		dojo.byId('loginsubmit').value = 'Please Wait...';
		dojo.byId('loginsubmit').style.width = '120px';	
		dojo.io.bind(bindArgs);
	} else if (valResults.hasMissing()) {
		alert('Please enter both your email address and your password.');
		dojo.byId('loginsubmit').value = 'Login';
		dojo.byId('loginsubmit').style.width = '80px';			
		return false;
	} else if (valResults.isInvalid('email')) {
		alert('Please enter a valid email address.');
		dojo.byId('email').focus();
		dojo.byId('loginsubmit').value = 'Login';
		dojo.byId('loginsubmit').style.width = '80px';			
		return false;
	}	

}

function retrievePassword() {
	dojo.require("dojo.validate");
	dojo.require("dojo.validate.check");
	dojo.require("dojo.validate.datetime");
	dojo.require("dojo.validate.us");
	dojo.require("dojo.validate.web");	
	
	var lost_password_container = dojo.byId('lost_password_container');
	var orig_contents = lost_password_container.innerHTML;
	
	var bindArgs = {
	url: 'pages/retrieve_password.asp',
	method: 'post',
	content: {
		email: dojo.byId('forgotemail').value,
		siteid: siteid
	},
	error: function(type, data, evt){
		alert('An error occurred and we could not retrieve your password.  Please try again.');
		dojo.byId('retrievesubmit').value = 'Retrieve Password';
		dojo.byId('retrievesubmit').style.width = '140px';	
	},
	load: function(type, data, evt){
		var returnVal = parseFloat(data);
		if (returnVal == -2) {
			dojo.style.show(dojo.byId('retrieve_not_found'));
			dojo.byId('retrievesubmit').value = 'Retrieve Password';
			dojo.byId('retrievesubmit').style.width = '140px';			
		} else if (returnVal <= 0) {
			dojo.style.show(dojo.byId('retrieve_error'));
			dojo.byId('retrievesubmit').value = 'Retrieve Password';
			dojo.byId('retrievesubmit').style.width = '140px';			
		} else {
			dojo.style.hide(lost_password_container);
			dojo.style.show(dojo.byId('retrieve_success'));
		}
		
	},
	mimetype: 'text/html'
	};	
	
	// Validate the form contents before submitting
	var valProfile = {
		// filters change the field value and are applied before validation.
		trim: ["forgotemail"],
		uppercase: [],
		lowercase: [],
		ucfirst: [],
		digit: [],
		required: ["forgotemail"],
		dependancies: {},
		constraints: {
			"forgotemail" : [dojo.validate.isEmailAddress]
		},
		confirm: {}
	};

	var valResults = dojo.validate.check(dojo.byId('forgotpw'), valProfile);	
	
	if (valResults.isSuccessful()) {
		dojo.byId('retrievesubmit').value = 'Please Wait...';
		dojo.io.bind(bindArgs);
	} else if (valResults.hasMissing()) {
		alert('Please enter an email address.');
		dojo.byId('forgotemail').focus();		
		return false;
	} else if (valResults.isInvalid('forgotemail')) {
		alert('Please enter a valid email address.');
		dojo.byId('forgotemail').focus();
		return false;
	}	
}

function processRegister(whichContainer) {
	
	dojo.require("dojo.validate");
	dojo.require("dojo.validate.check");
	dojo.require("dojo.validate.datetime");
	dojo.require("dojo.validate.us");
	dojo.require("dojo.validate.web");	
	
	dojo.byId('register_already_taken').style.display = 'none';
	dojo.byId('register_error').style.display = 'none';	
	
	var bindArgs = {
	url: 'pages/register_process.asp',
	method: 'post',
	formNode: dojo.byId('frmRegister'),
	error: function(type, data, evt){
		alert('An error occurred and we could not complete your registration.  Please try again.');
		dojo.byId('registersubmit').value = 'Submit';
		dojo.byId('registersubmit').style.width = '80px';	
	},
	load: function(type, data, evt){
		var returnVal = parseFloat(data);
		if (returnVal == -2) {
			dojo.byId('register_already_taken').style.display = '';
			dojo.byId('registersubmit').value = 'Submit';
			dojo.byId('registersubmit').style.width = '80px';				
		} else if (returnVal <= 0) {
			dojo.byId('register_error').style.display = '';
			dojo.byId('registersubmit').value = 'Submit';
			dojo.byId('registersubmit').style.width = '80px';				
		} else {
			userid = returnVal;
			loggedin = true;
			siusername = dojo.byId('registeremail').value;
			sipassword = dojo.byId('registerpassword').value;
			if (dojo.byId('userid')) {
				dojo.byId('userid').value = userid;
			}			
			
			// If they checked the Remember Me box, then set cookies
			createCookie('siUserID',userid,365);
			createCookie('siID',siusername,365);
			createCookie('siPW',sipassword,365);
			createCookie('siLoggedIn','true',365);


			// Now, it gets tricky
			// If they started the login process by clicking a Save to Favorites button,
			// we need to go back to the search results and show that listing as saved.
			// BUT, we also need to update the search results and map results pages to 
			// show ALL of their saved listings.
			if (whichContainer != 'results') { // they started from the property tracker page
				loadHomePage();
				savelistingid = '';
				savemlsregionid = '';
				savefrom = '';			
			} else { // otherwise....
				// If there is a listing to save, save it
				if ((savelistingid.length >= 1) && (savemlsregionid.length >= 1)) {
					var returnVal = saveListing(savelistingid, savemlsregionid, 'save');
				}
				
				// If we started on the list results page, then change the listing to saved.
				if (savefrom == saveToFavs_mls) {
					if (returnVal >= 1) {
						dojo.event.browser.clean(dojo.byId('savelisting' + savelistingid));
						dojo.byId('savelisting' + savelistingid).innerHTML = '<a href="javascript: removeFromFavorites(\'' + savelistingid + '\', \'' + savemlsregionid + '\', \'mls\',0);" class="summary"><img src="res/images/listings/button_listingsaved.gif" border="0" alt="Listing Saved - Click to Remove" /></a>';
						dojo.event.browser.clean(dojo.byId('listing_message' + savelistingid));
						dojo.byId('listing_message' + savelistingid).innerHTML = '<fieldset style="width: 540px;"><legend>Success!</legend><strong>You are now registered and this listing has been saved.</strong><br />To view your Favorites, click the Property Tracker tab at the top.</fieldset>';
						dojo.lfx.highlight(dojo.byId('listing_message' + savelistingid), '#FFFF99', 800).play();
						var anim4 = dojo.lfx.fadeHide(dojo.byId('listing_message' + savelistingid), 500);
						var composed = dojo.lfx.chain(anim4)
						composed.play(8000);
					} else {
						dojo.event.browser.clean(dojo.byId('listing_message' + savelistingid));
						dojo.byId('listing_message' + savelistingid).innerHTML = '<fieldset style="width: 540px;"><legend>Save to Favorites</legend><strong>You are now registered, but an error occurred (\'' + returnVal + '\') and the listing could not be saved. Please try again.'
						dojo.lfx.highlight(dojo.byId('savelistingid' + savelistingid), '#FFFF99', 800).play();			
						var anim4 = dojo.lfx.fadeHide(dojo.byId('savelistingid' + savelistingid), 500);
						var composed = dojo.lfx.chain(anim4)
						composed.play(8000);			
					}
					
				} else if (savefrom == saveToFavs_map) {
					
					if (returnVal >= 1) {
						// Show the favorites icon
						if (savemarker != '') {showFavIcon(savemarker)};
						savemarker = '';
						
						//Update the Save To Favorites text in the popup
						dojo.event.browser.clean(dojo.byId('maplisting' + savelistingid));
						dojo.byId('maplisting' + savelistingid).innerHTML = '<a href="javascript: removeFromFavorites(' + savelistingid + ', ' + mlsregionid + ', saveToFavs_map, ' + whichMarker + ');" class="popup_link">Listing Saved</a>|';
						
						//Scroll back up to view the map
						var coords = cumulativeOffset(dojo.byId('map'));
						window.scrollTo(0, coords[1]-20);	
						
						// Change the map message to show them that they've registered successfully and the
						//   listing has been saved
						dojo.event.browser.clean(dojo.byId('map_message'));
						dojo.byId('map_message').innerHTML = '<fieldset><legend>Success!</legend><strong>Your registration is complete, and this listing has been saved.</strong> To view your Favorites and access all your Property Tracker features, click the Property Tracker tab at the top.</fieldset>';
						dojo.lfx.highlight(dojo.byId('map_message'), '#FFFF99', 1000).play();
						var anim4 = dojo.lfx.fadeHide(dojo.byId('map_message'), 500);
						var composed = dojo.lfx.chain(anim4)
						composed.play(10000);
						
						// increment the number of saved listings on the property tracker home page
						numListings = numListings + 1;
					} else {
						dojo.event.browser.clean(dojo.byId('listing_message' + savelistingid));
						dojo.byId('map_message').innerHTML = "An error occurred (" + returnVal + ") and the listing could not be saved. Please try again."
						dojo.lfx.highlight(dojo.byId('map_message'), '#FFFF99', 800).play();			
						var anim4 = dojo.lfx.fadeHide(dojo.byId('map_message'), 500);
						var composed = dojo.lfx.chain(anim4)
						composed.play(8000);			
					}					
					
					savelistingid = '';
					savemlsregionid = '';
					savefrom = '';					
					
				} else if (savefrom == saveToFavs_detail) {
					if (returnVal >= 1) {
						var coords = cumulativeOffset(dojo.byId('detail_message'));
						window.scrollTo(0, coords[1]-220);
						
						// change the button from Save Listing to Listing Saved
						dojo.event.browser.clean(dojo.byId('detSaveListing'));
						dojo.byId('detSaveListing').innerHTML = "<a href=\"javascript:removeFromFavorites('" + savelistingid + "', '" + savemlsregionid + "', 'detail',0);\"><img src=\"res/images/listings/det_listing_saved.gif\" border=\"0\" alt=\"Listing Saved - click to un-save\"></a>";
		
						// wipe in the notice that the listing has been saved, and then fade it out
						dojo.event.browser.clean(dojo.byId('login_register_results'));
						dojo.byId('login_register_results').innerHTML = '<fieldset style="width: 540px;"><legend>Success!</legend><strong>You are now registered, and this listing has been saved to your Favorites.</strong><br />To view your Favorites, click the Property Tracker tab at the top.</fieldset>';
						dojo.lfx.highlight(dojo.byId('detail_message'), '#FFFF99', 800).play();
						
						var anim3 = dojo.lfx.fadeHide(dojo.byId(dojo.byId('detail_message')), 500);
						var composed2 = dojo.lfx.chain(anim3)
						composed2.play(8000);
						
						if (dojo.byId('savelisting' + savelistingid)) {
							dojo.event.browser.clean(dojo.byId('savelisting' + savelistingid));
							dojo.byId('savelisting' + savelistingid).innerHTML = '<a href="javascript: removeFromFavorites(\'' + savelistingid + '\', \'' + savemlsregionid + '\', \'mls\',0);" class="summary"><img src="res/images/listings/button_listingsaved.gif" border="0" alt="Listing Saved - Click to Remove" /></a>';
						}
					} else {
						var coords = cumulativeOffset(dojo.byId('detail_message'));
						window.scrollTo(0, coords[1]-220);						
						
						// wipe in the notice that the listing could not be saved, and then fade it out
						dojo.style.hide(dojo.byId('detail_message'));
						dojo.event.browser.clean(dojo.byId('detail_message'));
						dojo.byId('detail_message').innerHTML = '<fieldset style="width: 540px;"><legend>Save Listing</legend><strong>This listing could not be saved.</strong><br />Please make sure that you are logged in and try again.</fieldset>';
						var anim1 = dojo.lfx.wipeIn(dojo.byId('detail_message'), 500)
						var anim2 = dojo.lfx.highlight(dojo.byId('detail_message'), '#FFFF99', 800);
						var composed1 = dojo.lfx.chain(anim1, anim2);
						composed1.play(500);
						
						var anim3 = dojo.lfx.fadeHide(dojo.byId('detail_message'), 500);
						var composed2 = dojo.lfx.chain(anim3)
						composed2.play(8000);					
					}
					
					savelistingid = '';
					savemlsregionid = '';
					savefrom = '';
				} else if (savefrom == 'searchform') {
					dojo.event.browser.clean(dojo.byId('searchform_message'));
					dojo.byId('searchform_message').innerHTML = '<fieldset style="width: 540px;"><legend>Restier for Property Tracker</legend><strong>Success!</strong> You are now registered -- just click the Property Tracker tab at the top to access the full features.  Please wait a moment while we load the form so that you can save the search.</fieldset>';
					dojo.lfx.highlight(dojo.byId('searchform_message'), '#FFFF99', 800).play();
					var anim1 = dojo.lfx.wipeOut(dojo.byId('searchform_message'), 500);
					var anim2 = dojo.lfx.wipeIn(dojo.byId('savethissearch'), 500);
					var anim3 = dojo.lfx.highlight(dojo.byId('savethissearch'), '#FFFF99', 800).play();
					var composed1 = dojo.lfx.chain(anim1, anim2, anim3);
					composed1.play(6000);

					savelistingid = '';
					savemlsregionid = '';
					savefrom = '';					
					
				}
			}			
		}
		
	},
	mimetype: 'text/html'
	};	
	
	// Validate the form contents before submitting
	var valProfile = {
		// filters change the field value and are applied before validation.
		trim: ["registeremail", "registerfirstname", "registerlastname", "registerpassword", "registerpasswordconfirm"],
		uppercase: [],
		lowercase: [],
		ucfirst: ["registerfirstname", "registerlastname"],
		digit: [],
		required: ["registeremail", "registerfirstname", "registerlastname", "registerpassword", "registerpasswordconfirm", "registerphone"],
		dependancies:    {},
		constraints: {
			"registeremail" : [dojo.validate.isEmailAddress]
		},
		confirm: {}
	};

	var valResults = dojo.validate.check(dojo.byId('frmRegister'), valProfile);	
	
	if (valResults.isSuccessful()) {
		if (dojo.byId('registerpassword').value == dojo.byId('registerpasswordconfirm').value) {
			dojo.byId('registersubmit').value = 'Processing Info...';
			dojo.byId('registersubmit').style.width = '145px';	
			dojo.io.bind(bindArgs);
		} else {
			alert('The passwords did not match.');
			dojo.byId('registerpasswordconfirm').focus();
			return false;
		}
	} else if (valResults.hasMissing()) {
		alert('Please enter all of the required fields.');
		return false;
	} else if (valResults.isInvalid('registeremail')) {
		alert('Please enter a valid email address.');
		dojo.byId('registeremail').focus();
		return false;
	}
	
}

// Handles Save to Favorites for list results, map results, and detail pages
// Because must take into account content in all three areas when making changes to any one
function saveToFavorites(listingid, mlsregionid, whichPage, whichMarker) {
	var returnVal;
	
	savemarker = whichMarker;
		
	if (!(loggedin)) {
		showLoginRegisterButtons(listingid, mlsregionid, whichPage);
	} else {
		// Determine which page we came from: listing results, map results, or detail page.
		//   We will need to update listing markers across all pages at one time.
		if (whichPage == saveToFavs_mls) {
			dojo.html.setOpacity(dojo.byId('listing_message' + listingid), 1);
			dojo.event.browser.clean(dojo.byId('listing_message' + listingid));
			dojo.byId('listing_message' + listingid).innerHTML = '<img src="res/images/loading.gif" align="absmiddle"> Saving Listing Info...';
			dojo.lfx.wipeIn(dojo.byId('listing_message' + listingid), 300).play();
			returnVal = saveListing(listingid, mlsregionid, 'save');
			if (returnVal >= 1) {
				dojo.event.browser.clean(dojo.byId('savelisting' + listingid));
				dojo.byId('savelisting' + listingid).innerHTML = '<a href="javascript: removeFromFavorites(\'' + listingid + '\', \'' + mlsregionid + '\', \'mls\',0);" class="summary"><img src="res/images/listings/button_listingsaved.gif" border="0" alt="Listing Saved - Click to Remove" /></a>';
				dojo.event.browser.clean(dojo.byId('listing_message' + listingid));
				dojo.byId('listing_message' + listingid).innerHTML = '<fieldset style="width: 540px;"><legend>Success!</legend><strong>This listing has been saved.</strong> To view your Favorites, click the Property Tracker tab at the top.</fieldset>';
				dojo.lfx.highlight(dojo.byId('listing_message' + listingid), '#FFFF99', 800).play();
				var anim4 = dojo.lfx.fadeHide(dojo.byId('listing_message' + listingid), 500);
				var composed = dojo.lfx.chain(anim4)
				composed.play(8000);
				
				// increment the number of saved listings on the property tracker home page
				numListings = numListings + 1;
			} else {
				dojo.event.browser.clean(dojo.byId('listing_message' + listingid));
				dojo.byId('listing_message' + listingid).innerHTML = "An error occurred (" + returnVal + ") and the listing could not be saved. Please try again."
				dojo.lfx.highlight(dojo.byId('listing_message' + listingid), '#FFFF99', 800).play();			
				var anim4 = dojo.lfx.fadeHide(dojo.byId('listing_message' + listingid), 500);
				var composed = dojo.lfx.chain(anim4)
				composed.play(8000);			
			}
		} else if (whichPage == saveToFavs_map) {
			dojo.html.setOpacity(dojo.byId('map_message'), 1);
			returnVal = saveListing(listingid, mlsregionid, 'save');
			if (returnVal >= 1) {
				
				if (whichMarker != '') {
					showFavIcon(whichMarker);
				} else {
					showFavIcon(0);	
				}
				
				var coords = cumulativeOffset(dojo.byId('map'));
				window.scrollTo(0, coords[1]-20);				
				
				dojo.event.browser.clean(dojo.byId('maplisting' + listingid));
				dojo.byId('maplisting' + listingid).innerHTML = '<a href="javascript: removeFromFavorites(' + listingid + ', ' + mlsregionid + ', saveToFavs_map, ' + whichMarker + ');" class="popup_link">Listing Saved</a>|';
				dojo.event.browser.clean(dojo.byId('map_message'));
				dojo.byId('map_message').innerHTML = '<fieldset><legend>Success!</legend><strong>This listing has been saved.</strong> To view your Favorites, click the Property Tracker tab at the top.</fieldset>';
				dojo.lfx.wipeIn(dojo.byId('map_message'), 800).play();
				dojo.lfx.highlight(dojo.byId('map_message'), '#FFFF99', 800).play();
				var anim4 = dojo.lfx.fadeHide(dojo.byId('map_message'), 500);
				var composed = dojo.lfx.chain(anim4)
				composed.play(8000);
				
				// increment the number of saved listings on the property tracker home page
				numListings = numListings + 1;
			} else {
				dojo.event.browser.clean(dojo.byId('map_message'));
				dojo.byId('map_message').innerHTML = "An error occurred (" + returnVal + ") and the listing could not be saved. Please try again."
				dojo.lfx.highlight(dojo.byId('map_message'), '#FFFF99', 800).play();			
				var anim4 = dojo.lfx.fadeHide(dojo.byId('map_message'), 500);
				var composed = dojo.lfx.chain(anim4)
				composed.play(8000);			
			}
			
		} else if (whichPage == saveToFavs_detail) {
			returnVal = saveListing(listingid, mlsregionid, 'save');
			if (returnVal >= 1) {
				// change the button from Save Listing to Listing Saved
				dojo.event.browser.clean(dojo.byId('detSaveListing'));
				dojo.byId('detSaveListing').innerHTML = '<a href="javascript:removeFromFavorites(\'' + listingid + '\', \'' + mlsregionid + '\', \'detail\',0);"><img src="res/images/listings/det_listing_saved.gif" border="0" alt="Listing Saved - click to un-save"></a>';

				// wipe in the notice that the listing has been saved, and then fade it out
				dojo.byId('detail_message').style.display = 'none';
				dojo.html.setOpacity(dojo.byId('detail_message'),1);
				dojo.event.browser.clean(dojo.byId('detail_message'));
				dojo.byId('detail_message').innerHTML = '<fieldset style="width: 540px;"><legend>Success!</legend><strong>This listing has been saved.</strong> To view your Favorites, click the Property Tracker tab at the top.</fieldset>';
				var anim1 = dojo.lfx.wipeIn(dojo.byId('detail_message'), 500)
				var anim2 = dojo.lfx.highlight(dojo.byId('detail_message'), '#FFFF99', 800);
				var composed1 = dojo.lfx.chain(anim1, anim2);
				composed1.play(500);
				
				var anim3 = dojo.lfx.fadeHide(dojo.byId('detail_message'), 500);
				var composed2 = dojo.lfx.chain(anim3)
				composed2.play(8000);
				
				// increment the number of saved listings on the property tracker home page			
				numListings = numListings + 1;
			} else {
				// wipe in the notice that the listing could not be saved, and then fade it out
				dojo.byId('detail_message').style.display = 'none';
				dojo.html.setOpacity(dojo.byId('detail_message'),1);
				dojo.event.browser.clean(dojo.byId('detail_message'));
				dojo.byId('detail_message').innerHTML = '<fieldset style="width: 540px;"><legend>Save Listing</legend><strong>This listing could not be saved.</strong> Please make sure that you are logged in and try again.</fieldset>';
				var anim1 = dojo.lfx.wipeIn(dojo.byId('detail_message'), 500)
				var anim2 = dojo.lfx.highlight(dojo.byId('detail_message'), '#FFFF99', 800);
				var composed1 = dojo.lfx.chain(anim1, anim2);
				composed1.play(500);				
			}			
		}
	}
}

// Handles Remove from Favorites for list results, map results, and detail pages
function removeFromFavorites(listingid, mlsregionid, whichPage, whichMarker) {
	var returnVal;
		
	if (!(loggedin)) {
		showLoginRegisterButtons(listingid, mlsregionid, whichPage);
	} else {
		
		// Determine which page we came from: listing results, map results, or detail page.
		//   We will need to update listing markers across all pages at one time.
		if (whichPage == saveToFavs_mls) {
			dojo.html.setOpacity(dojo.byId('listing_message' + listingid),1);
			dojo.event.browser.clean(dojo.byId('listing_message' + listingid));
			dojo.byId('listing_message' + listingid).innerHTML = '<img src="res/images/loading.gif" align="absmiddle"> Removing Listing from Favorites...';
			dojo.lfx.wipeIn(dojo.byId('listing_message' + listingid), 300).play();
			returnVal = saveListing(listingid, mlsregionid, 'remove');
			if (returnVal >= 1) {
				dojo.event.browser.clean(dojo.byId('savelisting' + listingid));
				dojo.byId('savelisting' + listingid).innerHTML = '<a href="javascript: saveToFavorites(\'' + listingid + '\', \'' + mlsregionid + '\', \'mls\',0);" class="summary"><img src="res/images/listings/button_savelisting.gif" border="0" alt="Save This Listing to Your Favorites" /></a>';
				dojo.event.browser.clean(dojo.byId('listing_message' + listingid));
				dojo.byId('listing_message' + listingid).innerHTML = '<fieldset style="width: 540px;"><legend>Success!</legend><strong>This listing has been removed from your Favorites.</strong>&nbsp;&nbsp;To view your Favorites, click the Property Tracker tab at the top.</fieldset>';
				dojo.lfx.highlight(dojo.byId('listing_message' + listingid), '#FFFF99', 800).play();
				var anim4 = dojo.lfx.fadeHide(dojo.byId('listing_message' + listingid), 500);
				var composed = dojo.lfx.chain(anim4)
				composed.play(8000);
				
				// increment the number of saved listings on the property tracker home page
				numListings = numListings - 1;
			} else {
				dojo.event.browser.clean(dojo.byId('listing_message' + listingid));
				dojo.byId('listing_message' + listingid).innerHTML = "An error occurred (" + returnVal + ") and the listing could not be removed. Please try again."
				dojo.lfx.highlight(dojo.byId('listing_message' + listingid), '#FFFF99', 800).play();			
				var anim4 = dojo.lfx.fadeHide(dojo.byId('listing_message' + listingid), 500);
				var composed = dojo.lfx.chain(anim4)
				composed.play(8000);			
			}
		} else if (whichPage == saveToFavs_map) {
			dojo.html.setOpacity(dojo.byId('map_message'), 1);
			returnVal = saveListing(listingid, mlsregionid, 'remove');
			if (returnVal >= 1) {
				
				if (whichMarker != '') {
					removeFavIcon(whichMarker);
				} else {
					removeFavIcon(0);	
				}
				
				var coords = cumulativeOffset(dojo.byId('map'));
				window.scrollTo(0, coords[1]-20);				
				
				dojo.event.browser.clean(dojo.byId('maplisting' + listingid));
				dojo.byId('maplisting' + listingid).innerHTML = '<a href="javascript: saveToFavorites(' + listingid + ', ' + mlsregionid + ', saveToFavs_map, ' + whichMarker + ');" class="popup_link">Save to Favorites</a>|';
				dojo.event.browser.clean(dojo.byId('map_message'));
				dojo.byId('map_message').innerHTML = '<fieldset><legend>Success!</legend><strong>This listing has been removed from your Favorites.</strong> To view your Favorites, click the Property Tracker tab at the top.</fieldset>';
				dojo.lfx.wipeIn(dojo.byId('map_message'), 800).play();
				dojo.lfx.highlight(dojo.byId('map_message'), '#FFFF99', 800).play();
				var anim4 = dojo.lfx.fadeHide(dojo.byId('map_message'), 500);
				var composed = dojo.lfx.chain(anim4)
				composed.play(8000);
				
				// increment the number of saved listings on the property tracker home page
				numListings = numListings - 1;
			} else {
				dojo.event.browser.clean(dojo.byId('map_message'));
				dojo.byId('map_message').innerHTML = "An error occurred (" + returnVal + ") and the listing could not be removed. Please try again."
				dojo.lfx.highlight(dojo.byId('map_message'), '#FFFF99', 800).play();			
				var anim4 = dojo.lfx.fadeHide(dojo.byId('map_message'), 500);
				var composed = dojo.lfx.chain(anim4)
				composed.play(8000);			
			}
			
		} else if (whichPage == saveToFavs_detail) {
			returnVal = saveListing(listingid, mlsregionid, 'remove');
			if (returnVal >= 1) {
				// change the button from Save Listing to Listing Saved
				dojo.event.browser.clean(dojo.byId('detSaveListing'));
				dojo.byId('detSaveListing').innerHTML = '<a href="javascript:saveToFavorites(\'' + listingid + '\', \'' + mlsregionid + '\', \'detail\',0);"><img src="res/images/listings/next_steps_buttons_23.gif" border="0" alt="Save Listing to Your Favorites"></a>';

				// wipe in the notice that the listing has been saved, and then fade it out
				dojo.byId('detail_message').style.display = 'none';
				dojo.html.setOpacity(dojo.byId('detail_message'),1);
				dojo.event.browser.clean(dojo.byId('detail_message'));
				dojo.byId('detail_message').innerHTML = '<fieldset style="width: 540px;"><legend>Success!</legend><strong>This listing has been removed from your Favorites.</strong>&nbsp;&nbsp;To view your Favorites, click the Property Tracker tab at the top.</fieldset>';
				var anim1 = dojo.lfx.wipeIn(dojo.byId('detail_message'), 500);
				var anim2 = dojo.lfx.highlight(dojo.byId('detail_message'), '#FFFF99', 800);
				var composed1 = dojo.lfx.chain(anim1, anim2);
				composed1.play(500);
				
				var anim3 = dojo.lfx.fadeHide(dojo.byId('detail_message'), 500);
				var composed2 = dojo.lfx.chain(anim3)
				composed2.play(8000);
				
				// check for the listing on the results page. 
				// if found, update that from Save Listing to Listing Saved as well.
				if (dojo.byId('savelisting' + listingid)) {
					dojo.event.browser.clean(dojo.byId('savelisting' + listingid));
					dojo.byId('savelisting' + listingid).innerHTML = '<a href="javascript: saveToFavorites(\'' + listingid + '\', \'' + mlsregionid + '\', \'mls\',0);" class="summary"><img src="res/images/listings/button_savelisting.gif" border="0" alt="Save This Listing to Your Favorites" /></a>';
				}
				
				// increment the number of saved listings on the property tracker home page
				numListings = numListings - 1;			
			} else {
				// wipe in the notice that the listing could not be saved, and then fade it out
				dojo.byId('detail_message').style.display = 'none';
				dojo.html.setOpacity(dojo.byId('detail_message'),1);
				dojo.event.browser.clean(dojo.byId('detail_message'));
				dojo.byId('detail_message').innerHTML = '<fieldset style="width: 540px;"><legend>Save Listing</legend><strong>This listing could not be removed.</strong> Please make sure that you are logged in and try again.</fieldset>';
				var anim1 = dojo.lfx.wipeIn(dojo.byId('detail_message'), 500);
				var anim2 = dojo.lfx.highlight(dojo.byId('detail_message'), '#FFFF99', 800);
				var composed1 = dojo.lfx.chain(anim1, anim2);
				composed1.play(500);
				
				var anim3 = dojo.lfx.fadeHide(dojo.byId('detail_message'), 500);
				var composed2 = dojo.lfx.chain(anim3)
				composed2.play(8000);					
			}			
		}
	}
}

function saveListing(listingid, mlsregionid, action) {
	var returnVal;
	returnVal = -2;
	
	if (action != 'remove') {action = 'save'}
	
	var bindArgs = {
	url: 'pages/save_listing.asp',
	method: 'post',
	sync: true,
	content: {
		action: action, 
		listingid: listingid,
		mlsregionid: mlsregionid,
		userid: userid,
		siteid: siteid
	},
	error: function(type, data, evt){
		returnVal = -1;
	},
	load: function(type, data, evt){
		returnVal = parseFloat(data);
	},
	mimetype: 'text/html'
	};
	
	// Run the XHR Requests
	dojo.io.bind(bindArgs);		
	
	return returnVal;
}




function showLoginRegisterButtons(listingid, mlsregionid, whichPage)	{
	
	var divContainer;
	
	// Need to see which content dive we're dealing with so that we know where
	//  to load the login / register buttons
	if (whichPage == 'mls') { // List Results Page
		divContainer = dojo.byId('listing_message' + listingid);
	} else if (whichPage == 'detail') { // Detail Page
		divContainer = dojo.byId('detail_message');				
	} else if (whichPage == 'map') { // Map Results Page
		divContainer = dojo.byId('map_message');
	} else if ((whichPage == 'searchform') || (whichPage == 'unsubscribe')) {
		divContainer = dojo.byId('searchform_message');
	}
	
	// If the login / register buttons are alreayd open, then hide them
	if ((savelistingid == listingid) && (savemlsregionid == mlsregionid) && (savefrom == whichPage)) {
		dojo.lfx.fadeHide(divContainer,600).play();
		savelistingid = '';
		savemlsregionid = '';
		savefrom = '';
		return;
	} else if ((savelistingid.length != 0) && (savemlsregionid.length != 0)) {
		hideLoginRegisterButtons(savelistingid, savemlsregionid);
	}	
	
	// Store the listing ID so that we can still add the listing to favorites after
	//  login or register
	savelistingid = listingid;
	savemlsregionid = mlsregionid;
	savefrom = whichPage;
	
	divContainer.style.display = '';
	if (whichPage == 'mls') {
		var coords = cumulativeOffset(dojo.byId('listing'+listingid));
		divContainer.style.display = 'none';
		window.scrollTo(0, coords[1]-140);
		dojo.event.browser.clean(divContainer);
		divContainer.innerHTML='<img src="res/images/loading_big.gif" align="absmiddle"> <span style="font-size: 12px; font-weight: bold;">Loading...</span>';
		dojo.html.setOpacity(divContainer,1);
	} else if (whichPage == 'map') {
		var coords = cumulativeOffset(dojo.byId('map'));
		window.scrollTo(0, coords[1]+75);		
		dojo.event.browser.clean(divContainer);
		divContainer.innerHTML='<img src="res/images/loading_big.gif" align="absmiddle"> <span style="font-size: 12px; font-weight: bold;">Loading...</span>';
		dojo.html.setOpacity(divContainer,1);		
	} else if (whichPage == 'detail') {
		dojo.event.browser.clean(divContainer);
		divContainer.innerHTML='<img src="res/images/loading_big.gif" align="absmiddle"> <span style="font-size: 12px; font-weight: bold;">Loading Form...</span>';
		divContainer.style.display = '';
		dojo.html.setOpacity(divContainer,1);	
		
		var coords = cumulativeOffset(dojo.byId(divContainer));
		window.scrollTo(0, coords[1]-140);
		divContainer.style.display = 'none';
	} else if ((whichPage == 'searchform') || (whichPage == 'unsubscribe')) {
		dojo.event.browser.clean(divContainer);
		divContainer.innerHTML='<img src="res/images/loading.gif" align="absmiddle"> <span style="font-size: 12px; font-weight: bold;">Loading, please wait...</span>';
		divContainer.style.display = '';
		dojo.html.setOpacity(divContainer,1);
	}
	
	var bindArgs = {
	url: 'pages/choose_login_register.asp',
	method: 'get',	
	content: {
		page: whichPage
	},
	error: function(type, data, evt){
		alert('An error occurred and we could not retrieve the login / register form.  Please try again.');
		dojo.event.browser.clean(divContainer);
		divContainer.innerHTML='';
		divContainer.style.display = 'none';
		savelistingid = '';
		savemlsregionid = '';		
	},
	load: function(type, data, evt){
		dojo.event.browser.clean(divContainer);
		divContainer.innerHTML=data;

		var anim1 = dojo.lfx.wipeIn(divContainer,500);
		var anim2 = dojo.lfx.highlight(divContainer, '#FFFF99', 800);
		var composed = dojo.lfx.chain(anim1, anim2)
		composed.play();				
	},
	mimetype: 'text/html'
	};
	
	dojo.io.bind(bindArgs);	

}

function hideLoginRegisterButtons(listingid, mlsregionid){
	savelistingid = '';
	savemlsregionid = '';
	savefrom = '';

	// Load the div asking user to log in or register
	if (dojo.byId('listing_message'+listingid)) {
		dojo.lfx.fadeHide(dojo.byId('listing_message'+listingid),1000).play();
		dojo.event.browser.clean(dojo.byId('listing_message'+listingid));
		dojo.byId('listing_message'+listingid).innerHTML = '';
	}
	
}

function showLoginOrRegisterForm(actionType, whichContainer) {	
	// Need this first part to distinguish between the Property Tracker Welcome page 
	// and the search results pages.  We don't want to have the login form open on both 
	// pages at once, because of overlapping div names.
	
	var buttons_container = dojo.byId('buttons_container_' + whichContainer);
	var login_register = dojo.byId('login_register_' + whichContainer);
	var whichPage;
	
	if (actionType == 'login') {whichPage = 'pages/login.asp';} else {whichPage = 'pages/register.asp'}
	
	//Reset opacity from having been faded out before.
	dojo.html.setOpacity(login_register,1);
	
	// Set the bind arguments to retrieve map data
	var bindArgs = {
	url: whichPage,	
	error: function(type, data, evt){
		alert('An error occurred and we could not load the Login Form . \n\n Please make sure that you are using an up-to-date browser and that you have javascript and cookies fully enabled.');
	},
	content: {
		container: whichContainer
	},
	load: function(type, data, evt){
		dojo.event.browser.clean(login_register);
		login_register.innerHTML=data;	
		dojo.style.hide(buttons_container);
		var anim4 = dojo.lfx.wipeIn(login_register, 500);
		var composed = dojo.lfx.chain(anim4)
		composed.play();		
	},
	mimetype: 'text/html'
	};

	// Run the XHR Requests
	dojo.io.bind(bindArgs);		
}

function hideLoginOrRegisterForm(whichContainer) {
	// For the welcome page, whichContainer is empty string.
	// For the mls, maps and detail pages, it's set to _2 -- this prevents overlapping div names.
	var buttons_container = dojo.byId('buttons_container_' + whichContainer);
	var login_register = dojo.byId('login_register_' + whichContainer);	
	
	if (buttons_container && login_register) {
		dojo.html.setOpacity(buttons_container,1);
		dojo.lfx.wipeOut(login_register, 300).play();
		dojo.style.show(buttons_container);
	}
	
	
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function noSpam(user,domain) {
locationstring = "mailto:" + user + "@" + domain;
window.location = locationstring;
}

function cumulativeOffset(element) {

	var valueT = 0, valueL = 0;
	
	do {
	  valueT += element.offsetTop  || 0;
	  valueL += element.offsetLeft || 0;
	  element = element.offsetParent;
	} while (element);
	
	return [valueL, valueT];

}

////////////////////////////////////////////////////////////////////////////////////////////////
// QUERYSTRING FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////
function getQuerystring() {
  FORM_DATA = new Object();
  separator = ',';
  query = '' + this.location;
  qu = query
  query = query.substring((query.indexOf('?')) + 1);
  
    // Keep everything after the question mark '?'.
  if (query.length < 1) { return false; }  // Perhaps we got some bad data?
  keypairs = new Object();
  numKP = 1;
  
  while (query.indexOf('&') > -1) {
    keypairs[numKP] = query.substring(0,query.indexOf('&'));
    query = query.substring((query.indexOf('&')) + 1);
    numKP++;
  }
  
  keypairs[numKP] = query;
  
  for (i in keypairs) {
    keyName = keypairs[i].substring(0,keypairs[i].indexOf('='));
      // Left of '=' is name.
    keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
      // Right of '=' is value.
    while (keyValue.indexOf('+') > -1) {
      keyValue = keyValue.substring(0,keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
        // Replace each '+' in data string with a space.
    }
	
    keyValue = unescape(keyValue);
	// Unescape non-alphanumerics
	
	if (keyValue.length == 0) keyValue = '';
	
    if (FORM_DATA[keyName]) {
      FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
        // Object already exists, it is probably a multi-select input,
        // and we need to generate a separator-delimited string
        // by appending to what we already have stored.
    } else {
      FORM_DATA[keyName] = keyValue;
        // Normal case: name gets value.
    }
  }
  return FORM_DATA;
}

////////////////////////////////////////////////////////////////////////////////////////////////
// COOKIE FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return '';
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}
//]]>