function showme(favorite_id) {
	var fav = document.getElementById('show_me_'+favorite_id);
	var toggle = 1;
	var msg = "Added to Favorites to Tour.";
	if(fav.checked == false) {
		toggle = 0;
		msg = "Removed from Favorites to Tour.";
	}
	var url = "favorites_showme.php?id=" + favorite_id + "&toggle=" + toggle;
	var handleSuccess = function(){
		alert(msg);
	}
	var handleFailure = function(){
		msg = "An error occurred. Please try again!";
		alert(msg);
	}
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure
	};
	YAHOO.util.Connect.asyncRequest('GET', url, callback);
	
	return true;
}

function addFavoriteComment(favorite_id) {
	var div = document.getElementById('add_comment_div'+favorite_id);
	//Start by emptying the div (in case the form is already displayed)
	if (div.hasChildNodes()) {
		while(div.childNodes.length >= 1) {
			div.removeChild(div.firstChild);
		}
	}
	//Change the "Add a comment" link to something else
	var commentlink = document.getElementById('add_comment_link'+favorite_id);
	commentlink.innerHTML = "Enter your Note below...";
	//Add a textbox to type the comment in
	var textbox = document.createElement('textarea');
	textbox.setAttribute("name","comment_textarea");
	textbox.setAttribute("id", "text_"+favorite_id);
	textbox.setAttribute("cols","50");
	textbox.setAttribute("rows","5");
	textbox.setAttribute("wrap","PHYSICAL");
	textbox.setAttribute("fav_id",favorite_id);
	div.appendChild(textbox);
	//Add a submit button
	var button = document.createElement("button");
//	button.setAttribute("type","submit");
	button.onclick = function() {
		return addComment(favorite_id);
	};
	var clickme = document.createTextNode("Submit Note");
	button.appendChild(clickme);
	button.setAttribute("name","submit");
	//Need a line break
	var br = document.createElement("br");
	div.appendChild(br);
	div.appendChild(button);
	return false;	
}
function addComment(favorite_id) {
	var textarea = document.getElementById("text_"+favorite_id);
	var comment = textarea.value;
	if(comment == '') {
		alert("Please enter a Note");
		return false;
	}
	
	var div = document.getElementById('add_comment_div'+favorite_id);
//	var commentlink = document.getElementById('add_comment_link'+favorite_id);
	var commentlink = document.getElementById('favorite_comments_'+favorite_id);

	var url = "addcomment.php";
	var handleSuccess = function(response){
			//Remove the existing HTML
/*			if (div.hasChildNodes()) {
				while(div.childNodes.length >= 1) {
					div.removeChild(div.firstChild);
				}
			}			
*/
//			commentlink.innerHTML = '<span class="errormessage">Crib Note added successfully! <a href="javascript:location.reload(true);">Reload this page</a> to view new comments.</span>';
			commentlink.innerHTML = response.responseText;
	}
	var handleFailure = function(){
		commentlink.innerHTML = '<span class="errormessage">Error adding Note. Please try again.</span>';
	}
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure
	};
	formdata = document.getElementById("comment_form_"+favorite_id);
	YAHOO.util.Connect.setForm(formdata);
	var request = YAHOO.util.Connect.asyncRequest('POST', url, callback);
	
	return false;	
}

function addToFavorites(mls_num, results_page, is_session) {
	if(is_session == 1) {
		//	var url = "addsessionfavorites.php?mls_num=" + mls_num;
		alert("Please login to save your Favorite listings");
		return;
	} else {
		var url = "addfavorites.php?mls_num=" + mls_num;
	}
	var div = document.getElementById('fav_reply');
	if(div == null) {
		div = document.getElementById("fav_reply_" + mls_num);
	}
	var handleSuccess = function(){
//		if(results_page != 0) {
//			div.innerHTML = '<span class="errormessage">Added to <a href="favorites.php">Favorites</a>.</span>';
//		} else {
			alert("Added to Favorites! Now you can add notes!");
			history.go(0);
//		}
	}
	var handleFailure = function(){
		div.innerHTML = '<span class="errormessage">Error with favorites. Please try again.</span>';
	}
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure
	};
	var request = YAHOO.util.Connect.asyncRequest('GET', url, callback);
	
	return false;	
}

function addToClientFavorites(mls_num, client, results_page) {
//	var client = document.clientsform.clients.options[document.clientsform.clients.selectedIndex].value;
//	alert("add client fav " + client);
	var url = "addfavorites.php?mls_num=" + mls_num + '&client=' + client;
	var div = document.getElementById('fav_reply');
	if(div == null) {
		div = document.getElementById("fav_reply_" + mls_num);
	}
	var handleSuccess = function(){
//		if(results_page != 0) {
//			div.innerHTML = '<span class="errormessage">Added to <a href="favorites.php?clientid=' + client + '">client favorites</a>!</span>';
//		} else {
			alert("Added to Favorites! Now you can add notes!");
			history.go(0);
//		}
	}
	var handleFailure = function(){
		div.innerHTML = '<span class="errormessage">Error with favorites. Please try again.</span>';
	}
	var callback =
	{
	  success:handleSuccess,
	  failure: handleSuccess
	};
	var request = YAHOO.util.Connect.asyncRequest('GET', url, callback);
	
	return false;	
}

function removeFromFavorites(mls_num) {
	var url = "removefavorites.php?mls_num=" + mls_num;
	var favorites_div = document.getElementById("tr_mlsnum_" + mls_num);
	var div = document.getElementById('fav_reply');
	if(div == null) {
		div = document.getElementById("fav_reply_" + mls_num);
	}

	var handleSuccess = function(){
		div.innerHTML = '<span class="errormessage"><a href="javascript:location.reload(true);"><img src="http://4114homes.com/omaha/images/reload_page.gif" border="0"/></a></span>';
		if(favorites_div != null) {
			//This works in Firefox but not IT
			favorites_div.innerHTML = '<td colspan="3" class="errormessage"><a href="javascript:location.reload(true);"><img src="http://4114homes.com/omaha/images/reload_page.gif" border="0"/></a></td>';
		} 
	}
	var handleFailure = function(){
			favorites_div.innerHTML = '<td colspan="3" class="errormessage">Error connecting to server. Please reload this page.</td>';
	}
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure
	};
	var request = YAHOO.util.Connect.asyncRequest('GET', url, callback);
	
	return false;	
}

function addToHomepage(mls_num, agent_id) {
	var url = "addhomepage.php?mls_num=" + mls_num + '&agent=' + agent_id;
	var div = document.getElementById('homepage_span');
	var handleSuccess = function(){
			div.innerHTML = '<span class="errormessage">Added to <a href="index.php">homepage</a>!</span>';
	}
	var handleFailure = function(){
		div.innerHTML = '<span class="errormessage">An error occurred. Refresh this page and try again.</span>';
	}
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure
	};
	var request = YAHOO.util.Connect.asyncRequest('GET', url, callback);
	
	return false;	
}
function removeFromHomepage(mls_num, agent_id) {
	var url = "removehomepage.php?mls_num=" + mls_num + '&agent=' + agent_id;
	var div = document.getElementById('homepage_span');
	var handleSuccess = function(){
			div.innerHTML = '<span class="errormessage">Removed from <a href="index.php">homepage</a>!</span>';
	}
	var handleFailure = function(){
		div.innerHTML = '<span class="errormessage">An error occurred. Refresh this page and try again.</span>';
	}
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure
	};
	var request = YAHOO.util.Connect.asyncRequest('GET', url, callback);
	
	return false;	
}

