//<![CDATA[
		   
dojo.addOnLoad(function(){
	if (loggedin) {loadHomePage()} else {loadWelcomePage()}
});

function loadWelcomePage() {
	
	var property_tracker_container = dojo.byId('property_tracker_container_inner');
	dojo.event.browser.clean(property_tracker_container);
	property_tracker_container.innerHTML='<div style="margin-top: 20px; margin-bottom: 40px;  font-size: 12px; font-weight: bold;"><img src="res/images/loading_big.gif" align="absmiddle"><strong> Loading Property Tracker....</strong></div>';

	
	// Set the bind arguments to retrieve map data
	var bindArgs = {
	url: 'pages/welcome.asp',	
	error: function(type, data, evt){
		alert('An error occurred and we could not initiate the Property Tracker . \n\n Please make sure that you are using an up-to-date browser and that you have javascript and cookies fully enabled.');
		dojo.event.browser.clean(property_tracker_container);
		property_tracker_container.innerHTML='<div style="padding: 25px 0px 0px 15px; font: bold 12px tahoma;">Error: Could Not Load Page</div>';
	},
	load: function(type, data, evt){
		dojo.event.browser.clean(property_tracker_container);
		property_tracker_container.innerHTML=data;		
	},
	mimetype: 'text/html'
	};

	// Run the XHR Requests
	dojo.io.bind(bindArgs);	
	
}

function loadHomePage() {
	
	var property_tracker_container = dojo.byId('property_tracker_container_inner');
	dojo.event.browser.clean(property_tracker_container);
	property_tracker_container.innerHTML='<div style="margin-top: 20px; margin-bottom: 40px;  font-size: 12px; font-weight: bold;"><img src="res/images/loading_big.gif" align="absmiddle"><strong> Loading Property Tracker....</strong></div>';
	
	// Set the bind arguments to retrieve map data
	var bindArgs = {
	url: 'pages/home.asp',
	method: 'post',
	content: {
		userid: userid,
		siteid: siteid
	},	
	error: function(type, data, evt){
		alert('An error occurred and we could not load your Property Tracker home page. \n\n Please make sure that you are using an up-to-date browser and that you have javascript and cookies fully enabled.');
		dojo.event.browser.clean(property_tracker_container);
		property_tracker_container.innerHTML='<div style="padding: 25px 0px 0px 15px; font: bold 12px tahoma;">Error: Could Not Load Page</div>';
	},
	load: function(type, data, evt){
		dojo.event.browser.clean(property_tracker_container);
		property_tracker_container.innerHTML=data;
		
		// get the javascript to update the count of number of favorites and number of searches
		var script = property_tracker_container.getElementsByTagName('script')[0];
		eval(script.innerHTML);			
	},
	mimetype: 'text/html'
	};

	// Run the XHR Requests
	dojo.io.bind(bindArgs);		
	
}

function logout() {
	
	if (confirm('Are you sure you want to logout?')) {
		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;
		
		// reload search results (if necessary) to remove saved listings
		loadWelcomePage();
	}
}

function show_save_for_new_search() {
	dojo.byId('userid').value = userid;
	dojo.byId('searchid').value = '';
	dojo.byId('favs').value = 'false';
	dojo.byId('hidaction').value = 'newsavedsearch';
	full_search('1');
}

function load_saved_search_for_edit(searchid, userid) {	
	dojo.byId('userid').value = userid;
	dojo.byId('searchid').value = searchid;
	dojo.byId('favs').value = 'false';
	dojo.byId('hidaction').value = 'editsavedsearch';
	full_search('1');
}

function updateSavedListingText(num) {
	var numSavedListings = dojo.byId('numSavedListings');
	var savedListingsActions = dojo.byId('savedListingsActions');
	
	if (num == 1) {
		dojo.event.browser.clean(numSavedListings);
		numSavedListings.innerHTML = 'You currently have 1 saved listing.';
		savedListingsActions.style.display = '';
	} else if (num >=2) {
		dojo.event.browser.clean(numSavedListings);
		numSavedListings.innerHTML = 'You currently have ' + num + ' saved listings.';
		savedListingsActions.style.display = '';
	} 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.';
		savedListingsActions.style.display = 'none';
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
// PROFILE SAVED LISTING FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////

function profile_request_saved_listings_info(userid){
	var savedListingsContainer = dojo.byId('savedListingsContainer');
	var savedListingsRequest = dojo.byId('savedListingsRequest');
	
	dojo.html.setOpacity(savedListingsContainer,1);
	dojo.html.setOpacity(savedListingsRequest,1);		
	
	dojo.event.browser.clean(savedListingsRequest);
	savedListingsRequest.innerHTML='<fieldset><legend>Request More Info about Your Saved Listings</legend><img src="/res/images/loading_big.gif" align="absmiddle"> <span style="font-size: 12px; font-weight: bold;">Loading Form...</span></fieldset>';

	dojo.style.hide(savedListingsContainer);
	dojo.lfx.wipeIn(savedListingsRequest, 500).play();

	var bindArgs = {
	url: 'pages/request_more_info.asp',
	method: 'get',
	content: {
		requestuserid: userid
	},		
	error: function(type, data, evt){
		alert('An error occurred and we could not retrieve the request more info form.  Please try again.');
	},
	load: function(type, data, evt){
		savedListingsRequest.innerHTML=data;			
	},
	mimetype: 'text/html'
	};
	
	dojo.io.bind(bindArgs);
}

function profile_request_saved_listings_send(){
	
	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 savedListingsContainer = dojo.byId('savedListingsContainer');
	var savedListingsRequest = dojo.byId('savedListingsRequest');
	
	dojo.html.setOpacity(savedListingsContainer,1);
	dojo.html.setOpacity(savedListingsRequest,1);	
	
	var bindArgs = {
	url: 'pages/request_more_info.asp',
	method: 'post',
	formNode: dojo.byId('requestmoreinfo'),
	error: function(type, data, evt){
		alert('An error occurred and we could not send your request.  Please try again.');
	},
	load: function(type, data, evt){
		dojo.event.browser.clean(savedListingsRequest);
		savedListingsRequest.innerHTML='<fieldset><legend>Request More Info about Your Saved Listings</legend>' + data + '</fieldset>';			
		
		dojo.lfx.highlight(savedListingsRequest, '#FFFF99', 800).play();
		var anim2 = dojo.lfx.fadeHide(savedListingsRequest, 500);
		var anim3 = dojo.lfx.fadeShow(savedListingsContainer,500);
		var composed = dojo.lfx.chain(anim2, anim3)
		composed.play(5000);
	},
	mimetype: 'text/html'
	};	
	
	// Validate the form contents before submitting
	var valProfile = {
		// filters change the field value and are applied before validation.
		trim: ["requestName", "requestEmail"],
		uppercase: [],
		lowercase: [],
		ucfirst: ["requestName"],
		digit: [],
		required: ["requestName", "requestEmail", "requestContactPreference", "requestTODPreference"],
		dependancies:    {},
		constraints: {
			"requestEmail" : [dojo.validate.isEmailAddress]
		},
		confirm: {}
	};

	var valResults = dojo.validate.check(dojo.byId('requestmoreinfo'), valProfile);
	
	if (valResults.isSuccessful()) {	
		dojo.byId('requestsubmit').value = 'Sending Data...';
		dojo.byId('requestsubmit').style.width = '125px';	
		dojo.io.bind(bindArgs);
	} else if (valResults.hasMissing()) {
		alert('Please enter all of the required fields.');
		return false;
	} else if (valResults.isInvalid('requestEmail')) {
		alert('Please enter a valid email address.');
		dojo.byId('requestEmail').focus();
	}
}

// Cancels any / all detail request forms
function profile_request_saved_listings_cancel(){
	var savedListingsContainer = dojo.byId('savedListingsContainer');
	var savedListingsRequest = dojo.byId('savedListingsRequest');
	
	dojo.html.setOpacity(savedListingsContainer,1);
	dojo.html.setOpacity(savedListingsRequest,1);
	
	var anim1 = dojo.lfx.fadeHide(savedListingsRequest, 500);
	var anim2 = dojo.lfx.fadeShow(savedListingsContainer, 500);
	var composed = dojo.lfx.chain(anim1, anim2)
	composed.play();
}

////////////////////////////////////////////////////////////////////////////////////////////////
// PROFILE SAVED LISTING FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////

function profile_email_saved_listings(userid){
	var savedListingsContainer = dojo.byId('savedListingsContainer');
	var savedListingsRequest = dojo.byId('savedListingsRequest');
	
	dojo.html.setOpacity(savedListingsContainer,1);
	dojo.html.setOpacity(savedListingsRequest,1);		
	
	dojo.event.browser.clean(savedListingsRequest);
	savedListingsRequest.innerHTML='<fieldset><legend>Email Your Saved Listings</legend><img src="/res/images/loading_big.gif" align="absmiddle"> <span style="font-size: 12px; font-weight: bold;">Loading Form...</span></fieldset>';

	dojo.style.hide(savedListingsContainer);
	dojo.lfx.wipeIn(savedListingsRequest, 500).play();

	var bindArgs = {
	url: 'pages/email_to_friend.asp',
	method: 'get',
	content: {
		requestuserid: userid
	},		
	error: function(type, data, evt){
		alert('An error occurred and we could not retrieve the email form.  Please try again.');
	},
	load: function(type, data, evt){
		dojo.event.browser.clean(savedListingsRequest);
		savedListingsRequest.innerHTML=data;			
	},
	mimetype: 'text/html'
	};
	
	dojo.io.bind(bindArgs);
}

function profile_email_saved_listings_send(){
	
	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 savedListingsContainer = dojo.byId('savedListingsContainer');
	var savedListingsRequest = dojo.byId('savedListingsRequest');
	
	dojo.html.setOpacity(savedListingsContainer,1);
	dojo.html.setOpacity(savedListingsRequest,1);	
	
	var bindArgs = {
	url: 'pages/email_to_friend.asp',
	method: 'post',
	formNode: dojo.byId('emaillisting'),
	error: function(type, data, evt){
		alert('An error occurred and we could not send your request.  Please try again.');
	},
	load: function(type, data, evt){
		dojo.event.browser.clean(savedListingsRequest);
		savedListingsRequest.innerHTML='<fieldset><legend>Email Your Saved Listings</legend>' + data + '</fieldset>';			
		
		dojo.lfx.highlight(savedListingsRequest, '#FFFF99', 800).play();
		var anim2 = dojo.lfx.fadeHide(savedListingsRequest, 500);
		var anim3 = dojo.lfx.fadeShow(savedListingsContainer,500);
		var composed = dojo.lfx.chain(anim2, anim3)
		composed.play(5000);
	},
	mimetype: 'text/html'
	};	
	
	// Validate the form contents before submitting
	var valProfile = {
		// filters change the field value and are applied before validation.
		trim: ["requestName", "requestEmail", "recipientName", "recipientEmail", "requestComments"],
		uppercase: [],
		lowercase: [],
		ucfirst: ["requestName", "recipientName"],
		digit: [],
		required: ["requestName", "requestEmail", "recipientName", "recipientEmail"],
		dependancies: {},
		constraints: {
			"requestEmail" : [dojo.validate.isEmailAddress],
			"recipientEmail" : [dojo.validate.isEmailAddress]
		},
		confirm: {}
	};

	var valResults = dojo.validate.check(dojo.byId('emaillisting'), valProfile);
	
	if (valResults.isSuccessful()) {	
		dojo.byId('requestsubmit').value = 'Sending Data...';
		dojo.byId('requestsubmit').style.width = '125px';	
		dojo.io.bind(bindArgs);
	} else if (valResults.hasMissing()) {
		alert('Please enter all of the required fields.');
		return false;
	} else if (valResults.isInvalid('requestEmail')) {
		alert('Please enter a valid email address for yourself.');
		dojo.byId('requestEmail').focus();
	} else if (valResults.isInvalid('recipientEmail')) {
		alert('Please enter a valid email address for the recipient.');
		dojo.byId('recipientEmail').focus();		
	}
}

function profile_email_saved_listings_cancel() {
	var savedListingsContainer = dojo.byId('savedListingsContainer');
	var savedListingsRequest = dojo.byId('savedListingsRequest');
	
	dojo.html.setOpacity(savedListingsContainer,1);
	dojo.html.setOpacity(savedListingsRequest,1);
	
	var anim1 = dojo.lfx.fadeHide(savedListingsRequest, 500);
	var anim2 = dojo.lfx.fadeShow(savedListingsContainer, 500);
	var composed = dojo.lfx.chain(anim1, anim2)
	composed.play();
}

////////////////////////////////////////////////////////////////////////////////////////////////
// PROFILE SAVED SEARCH FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////

function profile_view_saved_searches(userid) {
	var savedSearchesContainer = dojo.byId('savedSearchesContainer');
	var savedSearchesView = dojo.byId('savedSearchesView');
	
	dojo.html.setOpacity(savedSearchesContainer,1);
	dojo.html.setOpacity(savedSearchesView,1);
	
	dojo.event.browser.clean(savedSearchesView);
	savedSearchesView.innerHTML='<fieldset><legend>View Your Saved Searches</legend><img src="res/images/loading.gif" align="absmiddle"> <span style="font-size: 12px; font-weight: bold;">Loading Form...</span></fieldset>';

	dojo.style.hide(savedSearchesContainer);
	dojo.lfx.wipeIn(savedSearchesView, 500).play();

	var bindArgs = {
	url: 'pages/saved_searches.asp',
	method: 'get',
	content: {
		requestuserid: userid
	},		
	error: function(type, data, evt){
		alert('An error occurred and we could not retrieve your saved searches.  Please try again.');
	},
	load: function(type, data, evt){
		dojo.event.browser.clean(savedSearchesView);
		savedSearchesView.innerHTML=data;			
	},
	mimetype: 'text/html'
	};
	
	dojo.io.bind(bindArgs);
}

function profile_delete_saved_search(searchid, userid) {
	
	var savedSearchesView = dojo.byId('savedSearchesView');
	var savedSearchesMessage = dojo.byId('savedSearchesMessage');	
	
	dojo.html.setOpacity(savedSearchesView,1);
	dojo.html.setOpacity(savedSearchesMessage,1);	
	
	var bindArgs = {
	url: 'pages/saved_searches.asp',
	method: 'post',
	content: {
		requestuserid: userid,
		searchid: searchid,
		action: 'delete'
	},	
	error: function(type, data, evt){
		alert('An error occurred and we could not delete the saved search.  Please try again.');
	},
	load: function(type, data, evt){
		var returnVal = parseFloat(data);
		
		if (returnVal >= 1) {
			// Updated the saved listings text in the main saved searches container div.
			numSearches = numSearches - 1;
			if (numSearches <= -1) {numSearches = 0};
			updateSavedSearchesText(numSearches);
			
			//Remove the deleted search row from the table
			var deletedSearch = dojo.byId('savedSearch'+searchid);
			dojo.lfx.fadeHide(deletedSearch, 500).play();
			
			//Display the message that the search was deleted successfully.
			dojo.event.browser.clean(savedSearchesMessage);
			savedSearchesMessage.innerHTML='The search was deleted successfully.';
			savedSearchesMessage.style.display = '';
			dojo.lfx.highlight(savedSearchesMessage, '#FFFF99', 800).play();
			var anim2 = dojo.lfx.fadeHide(savedSearchesMessage, 500);
			var composed = dojo.lfx.chain(anim2)
			composed.play(5000);
		} else {
			//Display the message that the search could not be deleted.
			dojo.event.browser.clean(savedSearchesMessage);
			savedSearchesMessage.innerHTML='An error occurred (' + returnVal + ') and the search could not be deleted.';
			savedSearchesMessage.style.display = '';
			dojo.lfx.highlight(savedSearchesMessage, '#FFFF99', 800).play();
			
			var anim2 = dojo.lfx.fadeHide(savedSearchesMessage, 500);
			var composed = dojo.lfx.chain(anim2)
			composed.play(5000);			
		}
	},
	mimetype: 'text/html'
	};	
	
	dojo.io.bind(bindArgs);
}

function profile_close_saved_searches() {
	var savedSearchesContainer = dojo.byId('savedSearchesContainer');
	var savedSearchesView = dojo.byId('savedSearchesView');
	
	dojo.html.setOpacity(savedSearchesContainer,1);
	dojo.html.setOpacity(savedSearchesView,1);
	
	var anim1 = dojo.lfx.fadeHide(savedSearchesView, 500);
	var anim2 = dojo.lfx.fadeShow(savedSearchesContainer, 500);
	var composed = dojo.lfx.chain(anim1, anim2)
	composed.play();
}

function updateSavedSearchesText(num) {
	var numSavedSearches = dojo.byId('numSavedSearches');
	var savedSearchesActions = dojo.byId('savedSearchesActions');
	
	if (num == 1) {
		dojo.event.browser.clean(numSavedSearches);
		numSavedSearches.innerHTML = 'You currently have 1 saved search.';
		savedSearchesActions.style.display = '';
	} else if (num >=2) {
		dojo.event.browser.clean(numSavedSearches);
		numSavedSearches.innerHTML = 'You currently have ' + num + ' saved searches.';
		savedSearchesActions.style.display = '';
	} else {
		dojo.event.browser.clean(numSavedSearches);
		numSavedSearches.innerHTML = 'You do not currently have any saved searches. <a href="javascript:newSavedSearch();">Click here to add one</a>.';
		savedSearchesActions.style.display = 'none';
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////
// PROFILE UPDATE PROFILE FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////

function profile_update(userid) {
	var profileContainer = dojo.byId('profileContainer');
	var profileUpdate = dojo.byId('profileUpdate');
	
	dojo.html.setOpacity(profileContainer,1);
	dojo.html.setOpacity(profileUpdate,1);
	
	dojo.event.browser.clean(profileUpdate);
	profileUpdate.innerHTML='<fieldset><legend>Update Your Profile</legend><img src="res/images/loading_big.gif" align="absmiddle"> <span style="font-size: 12px; font-weight: bold;">Loading Profile...</span></fieldset>';

	dojo.style.hide(profileContainer);
	dojo.lfx.wipeIn(profileUpdate, 500).play();

	var bindArgs = {
	url: 'pages/register.asp',
	method: 'get',
	content: {
		requestuserid: userid,
		action: 'edit'
	},		
	error: function(type, data, evt){
		alert('An error occurred and we could not retrieve your profile information.  Please try again.');
	},
	load: function(type, data, evt){
		dojo.event.browser.clean(profileUpdate);
		profileUpdate.innerHTML=data;
	},
	mimetype: 'text/html'
	};
	
	dojo.io.bind(bindArgs);
}

function profile_update_send(){
	
	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 profileContainer = dojo.byId('profileContainer');
	var profileUpdate = dojo.byId('profileUpdate');
	
	dojo.html.setOpacity(profileContainer,1);
	dojo.html.setOpacity(profileUpdate,1);
	
	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 {
			siusername = dojo.byId('registeremail').value;
			sipassword = dojo.byId('registerpassword').value;
		
			// Update cookies
			createCookie('ckUsername',siusername,365);
			createCookie('ckPassword',sipassword,365);
			
			dojo.event.browser.clean(profileUpdate);
			profileUpdate.innerHTML='<fieldset><legend>Update Your Profile</legend><strong>Success!  Your profile was updated successfully.</strong><br><br><a href="javascript: profile_update_cancel();">Close</a></fieldset>';

			dojo.lfx.highlight(profileUpdate, '#FFFF99', 800).play();
			var anim2 = dojo.lfx.fadeHide(profileUpdate, 500);
			var anim3 = dojo.lfx.fadeShow(profileContainer,500);
			var composed = dojo.lfx.chain(anim2, anim3)
			composed.play(8000);			
		}
	},
	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;
	}
}

function profile_update_cancel() {
	var profileContainer = dojo.byId('profileContainer');
	var profileUpdate = dojo.byId('profileUpdate');
	
	dojo.html.setOpacity(profileContainer,1);
	dojo.html.setOpacity(profileUpdate,1);
	
	var anim1 = dojo.lfx.fadeHide(profileUpdate, 500);
	var anim2 = dojo.lfx.fadeShow(profileContainer, 500);
	var composed = dojo.lfx.chain(anim1, anim2)
	composed.play();
}

//]]>