// All of these functions relate to dynamically adding a property to a list or RFP

function addTo(property_id)
{
	if ( property_id )
	{
		if (!xmlh_lock)
		{
			xmlh_lock = true;
			xmlh_queryPost("/ajax/createAddToFlopup.php", "property_id="+property_id, showAddToFlopup);
		}
	}
}


function showAddToFlopup(response)
{
	var data = eval('(' + response + ')');

	if ( data['success'] )
	{
		var oDiv = document.getElementById("flopup_add_to_content");
		oDiv.innerHTML = data["html"];
		showFlopup("flopup_add_to");
		setPosition(document.getElementById("flopup_add_to"));
	}
	else
	{
		alert(data["msg"]);
	}
	xmlh_lock = false;
}


// called when user selects an rfp to add a property to in the add-to flopup
function addToRfp(rfp_id, property_id)
{
	if ( property_id )
	{
		if (!xmlh_lock)
		{
			xmlh_lock = true;
			xmlh_queryPost("/ajax/addPropToRfp.php", "property_id="+property_id+"&rfp_id="+rfp_id, addToRfpDone);
		}
	}
}


// callback from addPropToRfp()
function addToRfpDone(response)
{
	var data = eval('(' + response + ')');
	xmlh_lock = false;

	if ( !data["success"] )
	{
		alert(data["msg"]);
		xmlh_lock = false;
		return;
	}


	// if added property to active rfp, make "In RFP" graphic appear.  Refresh progress panel. 
	if ( data["rfp_id"] == data["active_rfp_id"] )
	{
		var oDiv = document.getElementById("add_to_rfp_"+data["property_id"]);
		if ( oDiv ) oDiv.className = "in_rfp";

		if ( typeof refreshPanelContents == 'function')
		{ 
			refreshPanelContents(data["property_id"]);
			
			if(!(navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i))) 
			{ 
				openPanel();
			}			
		}
	}
           
	// make sure panel list is refreshed
	if ( typeof refreshPanelList == 'function') refreshPanelList(false);


	if ( data["activate_rfp"] )
	{
		// If no rfp was active and user added to an rfp, then that rfp is now active.  Update panel.
		var oDiv = document.getElementById("list_name");
		if ( oDiv ) oDiv.innerHTML = "<a href='/planners/rfpView.php?rfp_id="+data["rfp_id"]+"'>"+data["name"]+"</a> (<span id='panel_num_items'>"+data["num_items"]+'</span>)';
	}
	else if ( data["rfp_id"] == data["active_rfp_id"] )
	{
		// If edited active rfp, then update num items in panel (if it exists)
		var oDiv = document.getElementById("panel_num_items")
		if ( oDiv ) oDiv.innerHTML = data['num_items'];
	}

	updateNextButtons(data['next_link']);

	// Show Added flopup if not empty, close in 2 seconds
	if ( data['html'] )
	{
		document.getElementById("flopup_add_to_content").innerHTML = data['html'];
		setTimeout("closeFlopup('flopup_add_to');", 2000)
	}
	else closeFlopup();
}


// called when user selects a list to add a property to in the add-to flopup
function addToList(list_id, property_id)
{
	// list id is not mandatory.  if 0, add to list in session
	if ( property_id )
	{
		if (!xmlh_lock)
		{
			xmlh_lock = true;
			xmlh_queryPost("/ajax/addPropToList.php", "property_id="+property_id+"&list_id="+list_id, addToListDone);
		}
	}
}


// callback from addToList()
function addToListDone(response)
{
	var data = eval('(' + response + ')');
	xmlh_lock = false;

	if ( !data["success"] )
	{
		alert(data["msg"]);
		return;
	}

	if ( data["activate_list"] || data["list_id"] == data["active_list_id"] )
	{
		var oDiv = document.getElementById("in_rfp_"+data["property_id"]);
		if ( oDiv ) oDiv.className = "in_rfp";

		if ( typeof refreshPanelContents == 'function')
		{
			refreshPanelContents(data["property_id"]);
			openPanel();
		}
	}

	// make sure panel list is refreshed
	if ( typeof refreshPanelList == 'function') refreshPanelList(false);

	// Show Added flopup if not empty, close in 2 seconds
	if ( data['html'] )
	{
		document.getElementById("flopup_add_to_content").innerHTML = data['html'];
		setTimeout("closeFlopup('flopup_add_to');", 2000)
	}
	else closeFlopup();
}

// This function requires two outside variables:
//   is_list_empty
//   is_signed_in
function validateNewList()
{
	if ( gForm_lock ) return;
	else gForm_lock = true;

	var errors = new ErrorList();

	var oForm			= document.getElementById("form_new_list");
	var list_name		= getInputValue(oForm["list_name"]);
	var property_id		= getInputValue(oForm["property_id"]);

	clearInputErrors(oForm);

	if ( list_name == '' ) setError(oForm["list_name"], errors, "The list's name cannot be blank\n");

	if ( errors.getNumErrors() )
	{
		errors.showErrors();
		gForm_lock = false;
	}
	else
	{
		if (!xmlh_lock)
		{
			if ( !is_signed_in && !is_list_empty )
			{
				if ( !confirm("Creating a new list will erase your existing list.  You can only save multiple lists by signing in.  Do you want to create a new list?") )
				{
					return
				}
			}
			xmlh_lock = true;
			xmlh_queryPost("/ajax/createListWithProp.php", "list_name="+list_name+"&property_id="+property_id, createNewListDone);
		}
	}
}


function createNewListDone(response)
{
	var data = eval('(' + response + ')');
	xmlh_lock = false;

	if ( data['success'] )
	{
		document.getElementById("flopup_add_to_content").innerHTML = data["html"];
		is_list_empty = false;

		if ( data["activate_list"] || data["list_id"] == data["active_list_id"] )
		{
			if ( typeof refreshPanelContents == 'function')
			{ 
				refreshPanelContents();
				openPanel();
			}
		}

		// make sure panel list is refreshed
		if ( typeof refreshPanelList == 'function') refreshPanelList(false);

		setTimeout("closeFlopup('flopup_add_to');", 2000)
	}
	else
	{
		alert(data["msg"]);
	}
	gForm_lock= false;
}


// Updates any of the various next buttons that may be on the page
function updateNextButtons(next_link)
{
	// show any next buttons
	if ( next_link )
	{
		// change link on top search next button
		var oInput = document.getElementById("btn_next_1");
		if ( oInput )
		{
			oInput.style.display = "block";
			oInput.onclick = function() { window.location.href = next_link; }
		}
	
		// change link on bottom search next button
		var oInput = document.getElementById("btn_next_2");
		if ( oInput )
		{
			oInput.style.display = "block";
			oInput.onclick = function() { window.location.href = next_link; }
		}

		// change link on panel next button
		var oLink = document.getElementById("panel_next_link");
		if ( oLink )
		{
			oLink.href = next_link;
		}
	}
}
