function ValidateNewPortfolio(f)
{

	var strId = f.portfolioid.value;
	strId = strId.toLowerCase();
	f.portfolioid.value = strId;
	if (strId.length < 3) {
		alert("The portfolio ID must be at least 3 characters long");
		f.portfolioid.focus();
		return false;
	}
	if (strId.length > 20) {
		alert("The portfolio ID cannot be more than 20 characters long");
		f.portfolioid.focus();
		return false;
	}
	for (var i=0; i <strId.length; i++) {
		var c = strId.substr(i, 1);
		if ( (c >= "a" && c <= "z") || (c >= "0" && c <= "9") || c == "-" || c == "_") {
			// Okay
		} else {
			alert("The portfolio ID can only contain letters, numbers, underscores (_) and hyphens (-). Spaces are not allowed.");
			f.portfolioid.focus();
			return false;
		}
	}

	if (!f.portfoliotitle.value) {
		alert("You must give the portfolio a title");
		f.portfoliotitle.focus();
		return false;
	}

	f.btnSubmit.disabled = true;
	var strPost = GetFormContents(f);
	XMLTransmission("POST", glbSiteRoot + "_CreatePortfolio.aspx", strPost, OnCreatePortfolio, strId);
	
	return false;

}

function OnCreatePortfolio(objXML, PortfolioId)
{
	if (IsXmlHttpResponseSuccess(objXML)) {
		alert("The portfolio has been created");
		Goto(glbSiteRoot + "users/" + PortfolioId + "/stats");
	} else {
		HandleXmlHttpResponse(objXML);
	}
	document.NewPortfolioForm.btnSubmit.disabled = false;
}


function DeletePortfolio()
{
	if (!confirm("Are you sure that you want to delete this portfolio?")) return false;
	DeletePortfolio2();
}

function DeletePortfolio2()
{
	var strPost = "portfolioid=" + glbUserId;
	XMLTransmission("POST", glbSiteRoot + "_DeletePortfolio.aspx", strPost, OnDeletePortfolio);
}

function OnDeletePortfolio(objXML)
{
	if (IsXmlHttpResponseSuccess(objXML)) {
		alert("The portfolio has been deleted");
		Goto(glbSiteRoot + "EditProfile.aspx");
	} else {
		HandleXmlHttpResponse(objXML);
	}
}



function RemoveConstituent(AccountId)
{
	if (ctConstituents < 2) {
		if (!confirm("Removing the only constituent from the portfolio will DELETE the portfolio.\r\n\r\nAre you sure you want to do this?")) return false;
		DeletePortfolio2();
	} else {
		if (!confirm("Are you sure you want to remove this account from the portfolio?")) return false;
		var strPost = "portfolioid=" + glbUserId + "&accountid=" + AccountId;
		XMLTransmission("POST", glbSiteRoot + "_RemoveFromPortfolio.aspx", strPost, OnRemoveFromPortfolio, AccountId);
	}
}

function OnRemoveFromPortfolio(objXML, AccountId)
{
	if (IsXmlHttpResponseSuccess(objXML)) {
		var elem = document.getElementById("pmem_" + AccountId);
		if (elem) elem.style.display = "none";
		ctConstituents--;
	} else {
		HandleXmlHttpResponse(objXML);
	}
}

function AddToPortfolio(PortfolioId)
{
	var strPost = "portfolioid=" + PortfolioId + "&accountid=" + glbUserId;
	XMLTransmission("POST", glbSiteRoot + "_AddToPortfolio.aspx", strPost, OnAddToPortfolio, PortfolioId);
}

function OnAddToPortfolio(objXML, PortfolioId)
{
	if (IsXmlHttpResponseSuccess(objXML)) {
		alert("The account has been added to this portfolio");
		var elem = document.getElementById("padd_" + PortfolioId);
		if (elem) elem.disabled = true;
	} else {
		HandleXmlHttpResponse(objXML);
	}
}

function ValidateExistingPortfolio(f)
{

	var strId = f.portfolioid.value;
	strId = strId.toLowerCase();
	f.portfolioid.value = strId;
	if (strId.length < 3) {
		alert("The portfolio ID must be at least 3 characters long");
		f.portfolioid.focus();
		return false;
	}
	if (strId.length > 20) {
		alert("The portfolio ID cannot be more than 20 characters long");
		f.portfolioid.focus();
		return false;
	}
	for (var i=0; i <strId.length; i++) {
		var c = strId.substr(i, 1);
		if ( (c >= "a" && c <= "z") || (c >= "0" && c <= "9") || c == "-" || c == "_") {
			// Okay
		} else {
			alert("The portfolio ID can only contain letters, numbers, underscores (_) and hyphens (-). Spaces are not allowed.");
			f.portfolioid.focus();
			return false;
		}
	}

	if (!f.portfoliotitle.value) {
		alert("You must give the portfolio a title");
		f.portfoliotitle.focus();
		return false;
	}

	f.btnSubmit.disabled = true;
	var strPost = GetFormContents(f);
	XMLTransmission("POST", glbSiteRoot + "_ModifyPortfolio.aspx", strPost, OnModifyPortfolio, strId);
	
	return false;
}


function OnModifyPortfolio(objXML, PortfolioId)
{
	if (IsXmlHttpResponseSuccess(objXML)) {
		alert("The portfolio details have been updated");
		Goto(glbSiteRoot + "users/" + PortfolioId + "/portfolio");
	} else {
		HandleXmlHttpResponse(objXML);
	}
	document.ExistingPortfolioForm.btnSubmit.disabled = false;
}

