/***********************
*	Plots of Interest
***********************/
function TogglePOI(plotid, action, elementid, newClass)
{
	// If an element id has been passed in, look at its text and see what to do. Use a call back function to change its text on success.
	if (typeof elementid != 'undefined')
	{
		var myObj = new Object;
		myObj.plotid = plotid;
		myObj.action = action;
		myObj.elementid = elementid;
		myObj.newClass = newClass;
		if(action == "remove")
		{
			var req = Spry.Utils.loadURL("GET", "/utilities/saleswsproxy.cfm?method=removePOI&pid=" + plotid, true, TogglePOICallback ,{ userData: myObj });
		}
		else
		{
			var req = Spry.Utils.loadURL("GET", "/utilities/saleswsproxy.cfm?method=addPOI&pid=" + plotid, true, TogglePOICallback ,{ userData: myObj });
		}
	}
	// If an element id hasn't been passed in, assume it's an addPOI from a cookie.
	else
	{
		var req = Spry.Utils.loadURL("GET", "/utilities/saleswsproxy.cfm?method=addPOI&pid=" + plotid, true);
	}
}

function TogglePOICallback(req)
{
	if(typeof document.getElementById(req.userData.elementid) != 'undefined')
	{
		document.getElementById(req.userData.elementid).className = req.userData.newClass;
	}
}

function addPOICookie(plotid, redirectURL)
{
	// Create a cookie with the DOI request and redirect to requested URL.
	DP_Cookies.set("addPOI", plotid, 5);
	if(typeof redirectURL != 'undefined') window.location.href = redirectURL;
}

function comparePlots() 
{
	var total = 0;
	for(var i = 0; i < document.plots.plotids.length; i++)
	{
		if(document.plots.plotids[i].checked)
		{
			total = total + 1;
		}
	}
	if(total > 3)
	{
		alert("Only 3 properties can be compared at one time, please re-select and try again.");
	}
	else if(total < 2)
	{
		alert("Please select at least 2 properties to compare.");
	}
	else
	{
		document.plots.submit();
	}
}

function selectAllPlots()
{
	if(typeof document.plots.plotids.length != 'undefined')
	{
		for(var i = 0; i < document.plots.plotids.length; i++)
		{
			document.plots.plotids[i].checked = true;
		}
	}
	else if(typeof document.plots.plotids != 'undefined')
	{
		document.plots.plotids.checked = true;
	}
}

function selectNoPlots()
{
	if(typeof document.plots.plotids.length != 'undefined')
	{
		for(var i = 0; i < document.plots.plotids.length; i++)
		{
			document.plots.plotids[i].checked = false;
		}
	} 
	else if(typeof document.plots.plotids != 'undefined')
	{
			document.plots.plotids.checked = false;
	}
}

function addPlots()
{
	var thisPlotId = "";
	var thisElementId = "";
	var plotIDList = "";
	var plotids = new Array();
	
	if(typeof document.plots.plotids.length == 'undefined')
	{
		plotids.push(document.plots.plotids);
	}
	else
	{
		plotids = document.plots.plotids;
	}
	
	// If the user is logged in, call the webservice for each plot.
	if(DP_Cookies.get('LOGGEDIN') == "true")
	{
		for(var i = 0; i < plotids.length; i++)
		{
			if(plotids[i].checked)
			{
				thisPlotId = plotids[i].value;
				thisElementId = "r" + plotids[i].value.replace(/ /g, "_");
				TogglePOI(thisPlotId, "add", thisElementId, "saved");
			}
		}
	}
	else
	{
		for(var i = 0; i < plotids.length; i++)
		{
			if(plotids[i].checked)
			{
				plotIDList = listAppend(plotIDList, plotids[i].value);
			}
		}
		addPOICookie(plotIDList, loginURL);
	}
	
	// This is the little counter that tells you how many plots of interest you saved.
	if(typeof POICountText != 'undefined')
	{
		POICountText.loadContent('utilities/display/dsp_POICountText.cfm');
	}
}

function removePlots(refreshPage)
{
	var refreshPage = (refreshPage == null) ? false : refreshPage;
	var thisPlotId = "";
	var thisElementId = "";
	
	if(DP_Cookies.get('LOGGEDIN') == "true")
	{
		if(typeof document.plots.plotids.length != 'undefined')
		{
			for(var i = 0; i < document.plots.plotids.length; i++)
			{
				if(document.plots.plotids[i].checked)
				{
					thisPlotId = document.plots.plotids[i].value;
					thisElementId = "r" + document.plots.plotids[i].value.replace(/ /g, "_");
					TogglePOI(thisPlotId, "remove", thisElementId, "");
				}
			}
		}
		else if(typeof document.plots.plotids != 'undefined')
		{
			thisPlotId = document.plots.plotids.value;
			thisElementId = "r" + document.plots.plotids.value.replace(/ /g, "_");
			TogglePOI(thisPlotId, "remove", thisElementId, "");
		}
		// This is the little counter that tells you how many plots of interest you saved.
		if(typeof POICountText != 'undefined')
		{
			setTimeout("POICountText.loadContent('utilities/display/dsp_POICountText.cfm')",500);
		}
		if(refreshPage)
		{
			setTimeout("location.reload(true)",500);
		}
	}
	
	// If the user isn't logged in, redirect them to the relevant page.
	else
	{
		window.location.href = loginURL;
	}
}

function createBrochure()
{
	var plotIDList = "";
	
	if(typeof document.plots.plotids.length == 'undefined')
	{
		document.plots.plotids.push(document.plots.plotids.value);
	}
	
	for(var i = 0; i < document.plots.plotids.length; i++)
	{
		if(document.plots.plotids[i].checked)
		{
			plotIDList = listAppend(plotIDList, document.plots.plotids[i].value);
		}
	}
	
	if(DP_Cookies.get('LOGGEDIN') == "true")
	{
		window.location.href = 'utilities/brochure.cfm?plotids=' + plotIDList;
	}
	else
	{
		addPOICookie(plotIDList, loginURL);
	}
}

/******************************
*	Developments of Interest
******************************/
function ToggleDOI(sitecode, thisid, saveText, removeText, refreshPage)
{
	// If an element id has been passed in, look at its text and see what to do. Use a call back function to change its text on success.
	if (typeof thisid != 'undefined')
	{
		var myObj = new Object;
		myObj.thissitecode = sitecode;
		myObj.thisid = thisid;
		myObj.saveText = saveText;
		myObj.removeText = removeText;
		if (typeof refreshPage != 'undefined') myObj.refreshPage = refreshPage;
		if(document.getElementById(thisid).innerHTML.indexOf("Remove") >= 0)
		{
			var req = Spry.Utils.loadURL("GET", "/utilities/saleswsproxy.cfm?method=removeDOI&sc=" + sitecode, true, ToggleDOICallback, {userData: myObj});
		}
		else
		{
			var req = Spry.Utils.loadURL("GET", "/utilities/saleswsproxy.cfm?method=addDOI&sc=" + sitecode, true, ToggleDOICallback, {userData: myObj});
		}
	}
	// If an element id hasn't been passed in, assume it's an addDOI from a cookie.
	else
	{
		var req = Spry.Utils.loadURL("GET", "/utilities/saleswsproxy.cfm?method=addDOI&sc=" + sitecode, true);
	}
}

function ToggleDOICallback(req)
{
	if(document.getElementById(req.userData.thisid).innerHTML.indexOf("Remove") >= 0)
	{
		document.getElementById(req.userData.thisid).innerHTML = req.userData.saveText;
	}
	else
	{
		document.getElementById(req.userData.thisid).innerHTML = req.userData.removeText;
	}
	// This is the little counter that tells you how many plots of interest you saved.
	if(typeof POICountText != 'undefined')
	{
		POICountText.loadContent('utilities/display/dsp_POICountText.cfm');
	}
	// This refreshes the My Berkeley panel when a link is clicked.
	if(typeof myPortfoliopanel != 'undefined')
	{
		myPortfoliopanel.loadContent('template/controllers/myportfolio/portfolio.cfm');
	}
	// Refresh the whole page if required.
	if((typeof req.userData.refreshPage != 'undefined') && req.userData.refreshPage)
	{
		window.location.reload();
	}
}

function addDOICookie(sitecode, redirectURL)
{
	// Create a cookie with the DOI request and redirect to requested URL.
	DP_Cookies.set("addDOI", sitecode, 5);
	if(typeof redirectURL != 'undefined') window.location.href = redirectURL;
}