// saves a listing and marks it as saved
// assumes that user is already logged in
function saveThisListing(mls_id, mls_no) {
	
	// show progress while making the ajax request
	// change word to "Saving..."
	if ($("BtnSaveListing")) { // if on the listing detail page:
		$("BtnSaveListing").update('Saving&hellip;');
	} else { // else if on the listing results page
		$("btn_save_"+mls_id+"_"+mls_no).update('Saving&hellip;');
	}
	
	// base url for the ajax request
	var ajax_url = "?";
	
	// ajax request to save the listing
	new Ajax.Request(ajax_url,
		{
			method:'get',
			parameters: {
				product: 'Web',
				controller: 'ListingSearch',
				action: 'save_this_listing',
				acnt: ACNT,
				mls_id: mls_id,
				mls_no: mls_no,
                IDXSESS: SESSION_ID
			},
			onSuccess: function(transport) {
				var description = transport.responseJSON;
				
				if (description != 'error') {
					// show that the listing has been saved
					if ($("BtnSaveListing")) { // if on the listing detail page:
						$("BtnSaveListing").replace('<span id="BtnListingSaved">Listing Saved</span>');
					} else { // else if on the listing results page
						$("btn_save_"+mls_id+"_"+mls_no).replace('<span class="btn_saved">Saved</span>');
					}
					// don't use ugly alert box??
					//alert("Listing Saved: " + description);
				} else {
					alert("You need to login/register before you can save a listing");
				}
				
				
			},
			onFailure: function() {
				// revert the progress indicator back to normal
				if ($("BtnSaveListing")) { // if on the listing detail page:
					$("BtnSaveListing").update('Save');
				} else { // else if on the listing results page
					$("btn_save_"+mls_id+"_"+mls_no).update('Save');
				}
				alert('The ajax request failed - was trying to save a listing.');			
			}
		});
}






// SAVE LISTING EVENTS
// these get attached to the page elements on page load
function attachSaveListingEvents() {
	// save listing buttons only work if you are logged in
	// (welcome div exists only if logged in)
	if (AUTHENTICATED != "") {
		$$('.btn_save_listing').each( function(s) {
			$(s).observe('click', function(event) {
				Event.stop(event); // prevent the regular link from firing
				
				// get the mls id and mls no from the id attribute of the button
				// split listing id into mls parts
				var mlsparts = s.id.split("_", 4);
				var mls_id = mlsparts[2];
				var mls_no = mlsparts[3];
				
				saveThisListing(mls_id, mls_no);
			});
			
		});
		if ($("BtnSaveListing")) {
			$("BtnSaveListing").observe('click', function(event) {
				Event.stop(event); // prevent the regular link from firing
				
				// get the mls id and mls no from the rel attribute of the button
				// split listing id into mls parts
				var mlsparts = $("BtnSaveListing").rel.split("_", 2);
				var mls_id = mlsparts[0];
				var mls_no = mlsparts[1];
				
				saveThisListing(mls_id, mls_no);
			});
		}
	}
	// else if not logged in, the save listing buttons will have an href value
	// that takes them to the registration page and shows a message why.
}

Event.observe(window, 'load', function() {
	attachSaveListingEvents();
});

