
// Show/Hide/ChkHidden/SetText
function SetElm(elmid, elm) {
	if (elm==null && elmid!=null && elmid!="") elm = document.getElementById(elmid);
	return elm;
}
function HideControl(elmid, elm) {
	elm = SetElm(elmid, elm);
	if (elm && elm.style) elm.style.display = "none";
}
function ShowControl(elmid, elm) {
	elm = SetElm(elmid, elm);
	if (elm && elm.style) elm.style.display = "";
}
function IsHidden(elmid, elm) {
	elm = SetElm(elmid, elm);
	return (elm && elm.style && elm.style.display.toString().toLowerCase()=="none");
}
function BoldText(elmid, elm) {
	elm = SetElm(elmid, elm);
	if (elm && elm.style) elm.style.fontWeight = "bold";
}
function UnBoldText(elmid, elm) {
	elm = SetElm(elmid, elm);
	if (elm && elm.style) elm.style.fontWeight = "";
}
function IsVisible(elmid, elm) {return !IsHidden(elmid, elm);}
function SetInnerHtml(elmid, elm, html) {
	elm = SetElm(elmid, elm);
	if (elm && elm.innerHTML) elm.innerHTML = html;
}


// toggle visibility of specified object and adjust image accordingly
function toggleVisibility (eltid,imgid) {
	var elt = document.getElementById(eltid);
	var img = document.getElementById(imgid);

	if (!elt) return;

	if (elt.style.display == "none") {
		elt.style.display = "";
		if (img) img.src = "/images/minus.gif";
	}
	else {
		elt.style.display = "none";
		if (img) img.src = "/images/plus.gif";
	}
}

// toggle visibility of specified objects named eltidprefix1,eltidprefix2,eltidprefix3...
function toggleVisibilityEnum(eltidprefix,imgid) {
	var elt, i = 1, img = document.getElementById(imgid);
	while (elt = document.getElementById(eltidprefix+i)) {
		// find the custom account table and expand image
		if (!elt) return;
		if (elt.style.display == "none") {
			elt.style.display = "";
			if (img) img.src = "/images/minus.gif";
		}
		else {
			elt.style.display = "none";
			if (img) img.src = "/images/plus.gif";
		}
		i++;
	}
}

//Set one control within the list to be visible
//Hide all other controls
function setVisibility(eltIdList, visibleId) {
	var elt;
	for (var i = 0; i < eltIdList.length; i++) {
		elt = document.getElementById(eltIdList[i]);
		if (!elt) return;
		elt.style.display = 'none';
		elt.style.width = 0;
	}
	elt = document.getElementById(visibleId);
	elt.style.display = '';
	elt.style.width = '';
}

var preloadimg;
function toggleTemplateRowVisibility(me, eltidprefix, eltidsuffix, startindex, finishindex, imgid) {
	var mesrc = me.src;
	if (!mesrc || mesrc=="") return;
/*
	if (mesrc.indexOf("none")!=-1) {
		alert("You currently have no subaccounts for this account.\nPlease click on the + button to start adding subaccounts.");
		return;
	}
*/
	var displayvalue = "";
	if (mesrc.indexOf("open")!=-1) displayvalue = "none";

	// preload image of the open/close folder
	var img = document.getElementById(imgid);
	if (!preloadimg) {
		preloadimg = new Image();
		preloadimg.src = (img.src.indexOf("open")!=-1)?img.src.replace("open","close"):img.src.replace("close","open");
	}

/*
	var subaccttbody = me.parentNode.parentNode.parentNode.nextSibling; // =>TBODY
	subaccttbody.style.display = (subaccttbody.style.display=="none")?"":"none";
*/
	var startnode = document.getElementById(eltidprefix + (startindex-1) + eltidsuffix);
	var finishnode = document.getElementById(eltidprefix + (finishindex-1) + eltidsuffix);

	if (!startnode || !finishnode) return;

	var currentnode = startnode;

	    // un/hide the subaccount rows
	    while (currentnode) {
	        currentnode = currentnode.nextSibling;
	        if (currentnode && currentnode.nodeName == "TR")
	                currentnode.style.display = displayvalue;
	        if (currentnode == finishnode) break;
	    }


	// toggle the image, if any (plus to minus and vice versa)
	if (img) img.src = (img.src.indexOf("open")!=-1)?img.src.replace("open","close"):img.src.replace("close","open");

	// adjust document height
	document.body.style.height='200%';
}

function toggleRowVisibility(me, eltidprefix, eltidsuffix, startindex, finishindex, imgid) {
	var pageNumber = document.getElementById("pageno").innerHTML;
	//bug fix to catch if you are annualizing, don't want to delete the last row because it is actaully a Non-Financial Account.
	if(pageNumber){
		if(pageNumber.indexOf("b of ") != -1) return;
	}
    var mesrc = me.src;
    if (!mesrc || mesrc == "") return;
    /*
    if (mesrc.indexOf("none")!=-1) {
    alert("You currently have no subaccounts for this account.\nPlease click on the + button to start adding subaccounts.");
    return;
    }
    */
    var displayvalue = "";
    if (mesrc.indexOf("open") != -1) displayvalue = "none";

    // preload image of the open/close folder
    var img = document.getElementById(imgid);
    if (!preloadimg) {
        preloadimg = new Image();
        preloadimg.src = (img.src.indexOf("open") != -1) ? img.src.replace("open", "close") : img.src.replace("close", "open");
    }

    /*
    var subaccttbody = me.parentNode.parentNode.parentNode.nextSibling; // =>TBODY
    subaccttbody.style.display = (subaccttbody.style.display=="none")?"":"none";
    */
    var startnode = document.getElementById(eltidprefix + (startindex - 1) + eltidsuffix);
    var finishnode = document.getElementById(eltidprefix + (finishindex - 1) + eltidsuffix);

    if (!startnode || !finishnode) return;

    var currentnode = startnode;

    // un/hide the subaccount rows
    while (currentnode) {
        currentnode = currentnode.nextSibling;
        if (currentnode && currentnode.nodeName == "TR")
            currentnode.style.display = displayvalue;
        if (currentnode == finishnode) {
            finishnode.style.display = "none";
            break;
        }
    }


    // toggle the image, if any (plus to minus and vice versa)
    if (img) img.src = (img.src.indexOf("open") != -1) ? img.src.replace("open", "close") : img.src.replace("close", "open");

    // adjust document height
    document.body.style.height = '200%';
}




function toggleTemplateRowVis(eltidprefix, eltidsuffix, startindex, finishindex, imgid) {
	var elt;
	var img = document.getElementById(imgid);

	for (var i = startindex; i < finishindex; i++) {
		elt = document.getElementById(eltidprefix + i + eltidsuffix);
		if (elt.style.display == "none") {
			elt.style.display = "";
			if (img) img.src = "/images/minus.gif";
		}
		else {
			elt.style.display = "none";
			if (img) img.src = "/images/plus.gif";
		}
	}
}

function toggleNPOAccountLabels(bShow) {
	if (!document.getElementsByTagName) return;
	var npoElems = document.getElementsByTagName("span");
	for (var i=0; i < npoElems.length; i++)
		if (npoElems[i].className=="NPOUNDIVIDED")
			npoElems[i].style.display = (bShow)?"":"none";
}


// Sets the opacity of an HTML element
function setOpacity(elm, value) {
	elm.style.opacity = value/10;
	elm.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function showCalculatorMessage() {
	alert("For example: We will automatically calculate expressions such as 35897.75 + 45983.50 * 1.5 and return the value 104,873 when you move off the cell.");
}
function showSubaccountMessage() {
	alert("New! To add many subaccounts at once, choose the number of subaccounts from the dropdown below and click 'Add subaccount(s):'.");
}
function showPrior1Message() {
	var context = (document.location.href.toString().toLowerCase().indexOf(".co.uk") != -1)?"UK":"US";

	if (context == "US")
		alert("By \"Prior 1\" we mean the period prior to the current period. Prior 2 would be two periods prior to the current period, and so on.\n\nFor example, if you are entering in annual data and your current period end date is 06/30/2006, Prior 1 would mean the 12 months data from 07/01/2004 to 06/30/2005.");
	else
		alert("By \"Prior 1\" we mean the period prior to the current period. Prior 2 would be two periods prior to the current period, and so on.\n\nFor example, if you are entering in annual data and your current period end date is 30/06/2006, Prior 1 would mean the 12 months data from 01/07/2004 to 30/06/2005.");
}
