function renewSession() {
var keepworking = confirm("Your session will time out in 5 minutes.\n If you wish to continue working, click OK, then save your work.");
	if(!keepworking) {
		document.location.href="http://www.ddsuite.org/";
	}
}
function sessionEnded() {
	alert("Your session has ended. Please log in again to continue using the DD Suite.");
	document.location.href="http://www.ddsuite.org/login.php";
}
setTimeout("renewSession()", 35*60*1000);
setTimeout("sessionEnded()", 40*60*1000);

function autoSave(input_el) {
var autosaveOn = false;
    if (!autosaveOn)
    {
        autosaveOn = true;
		$.post("../reports/ppr_2.php", $('#ppr2').serialize());
	}
}

function get(el) {
	return document.getElementById(el);
}
function asc(char) {
	return char.charCodeAt(0);
}
function createEl(el) {
	return document.createElement(el);
}
function getStyle(elem, name) {
	if (elem.style[name]) {
		return elem.style[name];
	} else if (elem.currentStyle) {
		return elem.currentStyle[name];
	} else if (document.defaultView && document.defaultview.getComputedStyle) {
		name = name.replace(/([A-Z])/g, "-$1");
		name = name.toLowerCase();
		var s = document.defaultView.getComputedStyle(elem, "");
		return s && s.getPropertyValue(name);
	} else {
		return null;
	}
}

function calcCostsWrapper() {
	return calcCosts(this);
}

function calcCounMatchFundsWrapper() {
	return calcCounMatchFunds(this);
}
function chr(ascii) {
	return String.fromCharCode(ascii);
}

function inlinehelp(divtoshow) {
var divelement;
	divelement = document.getElementById(divtoshow)
	divelement.style.display = 'inline';
}

function fitToContent(id, maxHeight)
{
   var text = id && id.style ? id : document.getElementById(id);
   if ( !text )
      return;

   var adjustedHeight = text.clientHeight;
   if ( !maxHeight || maxHeight > adjustedHeight )
   {
      adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
      if ( maxHeight )
         adjustedHeight = Math.min(maxHeight, adjustedHeight);
      if ( adjustedHeight > text.clientHeight )
         text.style.height = adjustedHeight + "px";
   }
}

window.onload = function() {
	calcBudgetTotals(); 
	var textareas = document.getElementsByTagName("TEXTAREA");
	var func =
	function() {
      fitToContent( this, 1000 )
    };
	for(i=0; i < textareas.length; i++) {
		fitToContent(textareas[i], 1000);
		if (window.addEventListener) {
			textareas[i].addEventListener("keyup", func, false);
		} else {
			textareas[i].attachEvent("onclick", func);
		}
	}
}

function resizetextarea(str_textarea) {
	var textarea = get(str_textarea);
	textwidth = textarea.cols;
	textheight = textarea.rows;
	totalvis = textwidth * textheight;
	if (textarea.value.length > totalvis - 2) {
		textarea.rows = textheight + 1;
	}
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function pleasewait() {
var divelement;
var plswaitdiv;
plswaitdiv = "plswait";
divelement = document.getElementById("plswait");
	if (divelement.style.display == "none") {
		divelement.style.display = '';
		}
}

function showhiderow(pm_id) {
	var t_area = get(pm_id);
	var disp_link_id = pm_id.substring(0, pm_id.length - 4)+'_sh';
	var disp_link = get(disp_link_id);
	if(null == disp_link) {
		var disp_link_id = pm_id.substring(0, pm_id.length - 6)+'_sh_'+pm_id.substring(pm_id.length -1, pm_id.length);
		var disp_link = get(disp_link_id);
	}
	if (t_area.style.display == "none") {
		t_area.style.display = "";
		disp_link.innerHTML = "[-]";
	} else {
		t_area.style.display = "none";
		disp_link.innerHTML = "[+]";
	}	
}

function splitObligation(cbox, nofa, orgid) {
		var oblSplitRow2 = get(nofa+'_'+orgid+'_2');
		var oblSplitRow3 = get(nofa+'_'+orgid+'_3');
		var oblSplit2 = get(nofa+'_'+orgid+'_2_amt');
		var oblSplit3 = get(nofa+'_'+orgid+'_3_amt');			
	if(cbox.checked) {
		oblSplitRow2.style.display = "";
		oblSplitRow3.style.display = "";
		oblSplit2.name=oblSplit2.id;
		oblSplit3.name=oblSplit3.id;
	} else {
		oblSplitRow2.style.display = "none";
		oblSplitRow3.style.display = "none";
		oblSplit2.name="";
		oblSplit3.name="";
	}
}


function unhideUserRow(userid) {
	var tbl_details = get(userid+'_tbl');
	if (tbl_details.style.display=='none') {
		tbl_details.style.display='inline';
	} else {
		tbl_details.style.display='none';
	}
}
		
function showAddOrg() {
	var orgselect = get('orgid');
	orgselect.selectedIndex = -1;
	document.getElementById('addOrg').style.display = 'inline';
	document.getElementById('addOrg2').style.display = 'inline';
	document.getElementById('chooseOrg').style.display = 'none';
}
function hideAddOrg() {
	document.getElementById('addOrg').style.display = 'none';
	document.getElementById('addOrg2').style.display = 'none';
	document.getElementById('chooseOrg').style.display = 'inline';
}
function updateDocStatus(doc, status, nofa, flocation, conttype, bname) {
	var ifrm = get('doc_frame');
	switch(status) {
		case "s":
			ifrm.src = "gdoc_update.php?status_i="+status+"&doc_i="+doc+"&nofa_i="+nofa;
			break;
		default:
			ifrm.src = "gdoc_update.php?status_i="+status+"&doc_i="+doc+"&bname="+bname+"&nofa_i="+nofa+"&flocation="+flocation+"&conttype="+conttype;
	}
	setTimeout("window.location.reload(true)", 1.5*1000);
}

function calcEmpBkdn(input, userid) {
	var aoes = new Array('em','ed','ho','he','ch','re','tr','cs','qa','dsa','gm');
	var rt=0;
	var tot=0;
	var ti = get('tot_'+userid);
	for (i=0; i<aoes.length; i++) {
		rt = get(aoes[i]+'_'+userid);
		rtval = parseInt(rt.value.replace(/[^0-9.]/g,""));
		if(!isNaN(rtval)) {
			tot += rtval;
			rt.value=rtval+'%';		
		} else {
			rt.value='0%';
		}
	}
	if(tot>100) {
		ti.style.backgroundColor='red';
		ti.value=tot+'%';
	} else {
		ti.style.backgroundColor='';
		ti.value=tot+'%';
	}
}
		
function remNofaSects(row) {
	var el=get(row);
	el.parentNode.removeChild(el);
	var count = get('ns_count');
	count.value -= 1;
}

function unlockOther(fld, other) {
	if(typeof(fld)!='object') {
		fld = get(fld);
	}
	var other = get(other);
	if (fld.selected || fld.value==1) {
		other.disabled = false;
	} else {
		other.disabled = true;
		other.value = "";
	}
}

function total_bk_pms(pmid) {
	var idd = get(pmid+'_idd').value;
	var fm = get(pmid+'_fm').value;
	var otr = get(pmid+'_otr').value;
	var tot = get(pmid+'_tot');
	
	if(isNaN(parseInt(idd))) {
		idd=0;
	} else {
		idd = parseInt(idd);
	}
	if(isNaN(parseInt(fm))) {
		fm=0;
	} else {
		fm = parseInt(fm);
	}
	if(isNaN(parseInt(otr))) {
		otr=0;
	} else {
		otr = parseInt(otr);
	}

	tot.value = idd + fm + otr;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////                                BUDGET FUNCTIONS                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function calcPPRExpendituresTotals() {
	var flds = document.getElementsByTagName('input');
	var j=0;
	var fldck = "";
	var aoe = new Array();
	var sums = new Array();
	var rtsumspartb=0;
	var rtsumsmatch=0;
	var aesumspartb=0;
	var aesumsmatch=0;
	for (i=0; i<flds.length; i++) {
		var fld_ids = flds[i].name.split('_');
		if(fldck != fld_ids[0] && fld_ids[0].length > 0 && fld_ids[0].length < 4) {
			aoe[j]=fld_ids[0];
			sums[fld_ids[0]]=0;
			fldck = fld_ids[0];
			j++;
		}
		if(fld_ids[0].length > 0 && fld_ids[0].length <4) {
			switch(fld_ids[0]) {
				case 'sc':
				case 'ds':
				case 'os':
				case 'pa':
				case 'uc':
				case 'np':
				case 'ot':
					if(fld_ids[1] == 'partb') {
						rtsumspartb += (isNaN(parseFloat(flds[i].value.replace(/[^0-9.]/g, ''))))?0:parseFloat(flds[i].value.replace(/[^0-9.]/g, ''));
					} else if (fld_ids[1] == 'match') {
						rtsumsmatch += (isNaN(parseFloat(flds[i].value.replace(/[^0-9.]/g, ''))))?0:parseFloat(flds[i].value.replace(/[^0-9.]/g, ''));
					}
				break;
				case 'em':
				case 'ed':
				case 'ho':
				case 'he':
				case 'ch':
				case 're':
				case 'tr':
				case 'qa':
				case 'cs':
				case 'gm':
				case 'dsa':
					if(fld_ids[1] == 'partb') {
						aesumspartb += (isNaN(parseFloat(flds[i].value.replace(/[^0-9.]/g, ''))))?0:parseFloat(flds[i].value.replace(/[^0-9.]/g, ''));
					} else if (fld_ids[1] == 'match') {
						aesumsmatch += (isNaN(parseFloat(flds[i].value.replace(/[^0-9.]/g, ''))))?0:parseFloat(flds[i].value.replace(/[^0-9.]/g, ''));
					}
				break;
			}
				
			sums[fld_ids[0]] += (isNaN(parseFloat(flds[i].value.replace(/[^0-9.]/g, ''))))?0:parseFloat(flds[i].value.replace(/[^0-9.]/g, ''));
			flds[i].value = (isNaN(parseFloat(flds[i].value.replace(/[^0-9.]/g, ''))))?formatCurrency(0):formatCurrency(parseFloat(flds[i].value.replace(/[^0-9.]/g, '')));
		}
	}
	for(i=0; i<aoe.length; i++) {
		get(aoe[i]+'_total').value = formatCurrency(sums[aoe[i]]);
	}
	get('rt_partb_total').value = formatCurrency(rtsumspartb);
	get('rt_match_total').value = formatCurrency(rtsumsmatch);
	get('rt_total_total').value = formatCurrency(rtsumspartb + rtsumsmatch);
	get('ae_partb_total').value = formatCurrency(aesumspartb);
	get('ae_match_total').value = formatCurrency(aesumsmatch);
	get('ae_total_total').value = formatCurrency(aesumspartb + aesumsmatch);
}
															 
		
			
function calcExpenses(fld, fringe) {
	var fldname = fld.id;
	var secttypeser = fldname.split("_", 4);
	var section = secttypeser[0];
	var type = secttypeser[1];
	var serial = secttypeser[3];
	var pr_cf = get(section + "_pr_cf_" + serial);//Prior Council Funds
	var pr_mf = get(section + "_pr_mf_" + serial);//Prior Match
	var cp_cf = get(section + "_cp_cf_" + serial);//Current Period Council Funds
	var cp_mf = get(section + "_cp_mf_" + serial);//Current Period Match
	var cum_cf = get(section + "_cum_cf_" + serial);//Cumulative
	var cum_mf = get(section + "_cum_mf_" + serial);//Cumulative

	prcf_val = parseFloat(pr_cf.value.replace(/[^0-9.]/g,""));
	if (isNaN(prcf_val)) {
		prcf_val = 0;
	}
	prmf_val = parseFloat(pr_mf.value.replace(/[^0-9.]/g,""));
	if (isNaN(prmf_val)) {
		prmf_val = 0;
	}
	cpcf_val = parseFloat(cp_cf.value.replace(/[^0-9.]/g,""));
	if (isNaN(cpcf_val)) {
		cpcf_val = 0;
	}
	cpmf_val = parseFloat(cp_mf.value.replace(/[^0-9.]/g,""));
	if (isNaN(cpmf_val)) {
		cpmf_val = 0;
	}
	cumcf_val = prcf_val + cpcf_val;
	cummf_val = prmf_val + cpmf_val;
	cum_cf.value = formatCurrency(cumcf_val);
	cum_mf.value = formatCurrency(cummf_val);
	cp_cf.value = formatCurrency(cpcf_val);
	cp_mf.value = formatCurrency(cpmf_val);
	calcExpensesTotals(section, fringe);
}

function calcExpensesTotals(section, fringe) {
	var pr_cf_total = get(section + "_pr_cf_total");
	var pr_mf_total = get(section + "_pr_mf_total");
	var cp_cf_total = get(section + "_cp_cf_total");
	var cp_mf_total = get(section + "_cp_mf_total");
	var cum_cf_total = get(section + "_cum_cf_total");
	var cum_mf_total = get(section + "_cum_mf_total");
	var pr_counfunds = 0;
	var pr_matchfunds = 0;
	var cp_counfunds = 0;
	var cp_matchfunds = 0;
	var cum_counfunds = 0;
	var cum_matchfunds = 0;
	var lineids = get(section + "_lineids");
	var inputs = lineids.value.split("|");
	for(i=0; i<inputs.length; i++) {
		if(inputs[i]) {
			pr_counfunds += parseFloat(get(section + '_pr_cf_' + inputs[i]).value.replace(/[^0-9.]/g,""));
			pr_matchfunds += parseFloat(get(section + '_pr_mf_' + inputs[i]).value.replace(/[^0-9.]/g,""));
			cp_counfunds += parseFloat(get(section + '_cp_cf_' + inputs[i]).value.replace(/[^0-9.]/g,""));
			cp_matchfunds += parseFloat(get(section + '_cp_mf_' + inputs[i]).value.replace(/[^0-9.]/g,""));
			cum_counfunds += parseFloat(get(section + '_cum_cf_' + inputs[i]).value.replace(/[^0-9.]/g,""));
			cum_matchfunds += parseFloat(get(section + '_cum_mf_' + inputs[i]).value.replace(/[^0-9.]/g,""));
		}
	}
	if(fringe==1) {
		fringe = true;
	} else {
		fringe = false;
	}
	if (fringe) { 
		var pr_cf_sub = get(section + "_pr_cf_sub");
		var pr_mf_sub = get(section + "_pr_mf_sub");
		var cp_cf_sub = get(section + "_cp_cf_sub");
		var cp_mf_sub = get(section + "_cp_mf_sub");
		var cum_cf_sub = get(section + "_cum_cf_sub");
		var cum_mf_sub = get(section + "_cum_mf_sub");

		pr_cf_sub.value = formatCurrency(pr_counfunds);
		pr_mf_sub.value = formatCurrency(pr_matchfunds);
		cp_cf_sub.value = formatCurrency(cp_counfunds);
		cp_mf_sub.value = formatCurrency(cp_matchfunds);
		cum_cf_sub.value = formatCurrency(cum_counfunds);
		cum_mf_sub.value = formatCurrency(cum_matchfunds);
		
		var pr_cf_fringe = get(section + "_pr_cf_fringe");
		var pr_mf_fringe = get(section + "_pr_mf_fringe");
		var cp_cf_fringe = get(section + "_cp_cf_fringe");
		var cp_mf_fringe = get(section + "_cp_mf_fringe");
		var cum_cf_fringe = get(section + "_cum_cf_fringe");
		var cum_mf_fringe = get(section + "_cum_mf_fringe");
		
		var pr_cf_fringe_val = parseFloat(pr_cf_fringe.value.replace(/[^0-9.]/g,""));
		var pr_mf_fringe_val = parseFloat(pr_mf_fringe.value.replace(/[^0-9.]/g,""));
		var cp_cf_fringe_val = parseFloat(cp_cf_fringe.value.replace(/[^0-9.]/g,""));
		var cp_mf_fringe_val = parseFloat(cp_mf_fringe.value.replace(/[^0-9.]/g,""));
		var cum_cf_fringe_val = pr_cf_fringe_val + cp_cf_fringe_val;
		var cum_mf_fringe_val = pr_mf_fringe_val + cp_mf_fringe_val;
		
		var total_pr_cf = pr_counfunds + pr_cf_fringe_val;
		var total_pr_mf = pr_matchfunds + pr_mf_fringe_val;
		var total_cp_cf = cp_counfunds + cp_cf_fringe_val;
		var total_cp_mf = cp_matchfunds + cp_mf_fringe_val;
		var total_cum_cf = cum_counfunds + cum_cf_fringe_val;
		var total_cum_mf = cum_matchfunds + cum_mf_fringe_val;
		
		pr_cf_fringe.value = formatCurrency(pr_cf_fringe_val);
		pr_mf_fringe.value = formatCurrency(pr_mf_fringe_val);
		cp_cf_fringe.value = formatCurrency(cp_cf_fringe_val);
		cp_mf_fringe.value = formatCurrency(cp_mf_fringe_val);
		cum_cf_fringe.value = formatCurrency(cum_cf_fringe_val);
		cum_mf_fringe.value = formatCurrency(cum_mf_fringe_val);
		
		pr_cf_total.value = formatCurrency(total_pr_cf);
		pr_mf_total.value = formatCurrency(total_pr_mf);
		cp_cf_total.value = formatCurrency(total_cp_cf);
		cp_mf_total.value = formatCurrency(total_cp_mf);
		cum_cf_total.value = formatCurrency(total_cum_cf);
		cum_mf_total.value = formatCurrency(total_cum_mf);
		
	} else {
		pr_cf_total.value = formatCurrency(pr_counfunds);
		pr_mf_total.value = formatCurrency(pr_matchfunds);
		cp_cf_total.value = formatCurrency(cp_counfunds);
		cp_mf_total.value = formatCurrency(cp_matchfunds);
		cum_cf_total.value = formatCurrency(cum_counfunds);
		cum_mf_total.value = formatCurrency(cum_matchfunds);
	}
	calcBudgetTotals();
}

function calcBudgetTotals()
{
	var totals = document.getElementsByName("totals");
	for (i=0; i<totals.length; i++)
	{
		totals[i].value = 0;
	}
	var inputs = document.getElementsByTagName("input");
	for (i=0; i<inputs.length; i++)
	{
		var x = inputs[i].id.split("_");
		var num_splits = x.length-1;
		if(x[num_splits]=="total" && x[0]!="budget")
		{
			var bt = get("budget_"+x[1]+"_"+x[2]+"_total");
			bt.value = parseFloat(bt.value.replace(/[^0-9\.]/g,"")) + parseFloat(inputs[i].value.replace(/[^0-9\.]/g,""));
		}
		
	}
	for (i=0; i<totals.length; i++)
	{
		totals[i].value = formatCurrency(totals[i]);
	}	
}


function calcCosts(fld, it, fr) {
var fldname = fld.id;
var value = fld.value;
var secttypeser = fldname.split("_", 3);
var section = secttypeser[0];
var type = secttypeser[1];
var serial = secttypeser[2];
if(it == "Rt") {
	var rate = get(section + "_rate_" + serial);
	var units = get(section + "_units_" + serial);
	var time = get(section + "_time_" + serial);
	var projcosts = get(section + "_projcosts_" + serial);

	rateval = rate.value.replace(/[^0-9\.]/g,"");
	rateval = rateval - 0;
	if (isNaN(rateval)) {
		rateval = 0;
	}
	unitval = units.value.replace(/[^0-9\.]/g,"");
	unitval = unitval - 0;
	if (isNaN(unitval)) {
		unitval = 0;
	}
	timeval = time.value.replace(/[^0-9\.]/g,"");
	timeval = timeval - 0;
	if (isNaN(timeval)) {
		timeval = 0;
	}
	rate.value = formatCurrency(rateval);
	units.value = unitval;
	projcosts.value = formatCurrency(rateval * unitval * timeval);
	var counfunds = get(section + "_counfunds_" + serial);	
	var matchfunds = get(section + "_matchfunds_" + serial);
	counval = parseFloat(counfunds.value.replace(/[^0-9\.]/g, ""));
	matchval = parseFloat(matchfunds.value.replace(/[^0-9\.]/g, ""));

	if ((counval + matchval) > (unitval * rateval * timeval)) {
		counval = unitval * rateval * timeval;
		matchval = 0;
		counfunds.value = formatCurrency(counval);
		matchfunds.value = formatCurrency(matchval);
	} else {
		calcTotals(section, fr);
	}
} else {
	var rate = get(section + "_rate_" + serial);
	var units = get(section + "_units_" + serial);
	var projcosts = get(section + "_projcosts_" + serial);

	rateval = rate.value.replace(/[^0-9\.]/g,"");
	rateval = rateval - 0;
	if (isNaN(rateval)) {
		rateval = 0;
	}
	unitval = units.value.replace(/[^0-9\.]/g,"");
	unitval = unitval - 0;
	if (isNaN(unitval)) {
		unitval = 0;
	}
	rate.value = formatCurrency(rateval);
	units.value = unitval;
	projcosts.value = formatCurrency(rateval * unitval);
	var counfunds = get(section + "_counfunds_" + serial);	
	var matchfunds = get(section + "_matchfunds_" + serial);
	counval = parseFloat(counfunds.value.replace(/[^0-9\.]/g, ""));
	matchval = parseFloat(matchfunds.value.replace(/[^0-9\.]/g, ""));

	if ((counval + matchval) > (unitval * rateval)) {
		counval = unitval * rateval;
		matchval = 0;
		counfunds.value = formatCurrency(counval);
		matchfunds.value = formatCurrency(matchval);
	} else {
		calcTotals(section, fr);
	}
}
}

function calcTotals(section, fringe) {
	var re = "/^" + section + "/";
	var sectprojtotal = get(section + "_projcosts_total");
	var sectcountotal = get(section + "_counfunds_total");
	var sectmatchtotal = get(section + "_matchfunds_total");
	var sectelementcount = get(section + "_counter");
	var projcosts = 0;
	var counfunds = 0;
	var matchfunds = 0;
	var inputs = document.getElementsByTagName("input");
	for(i=0; i<inputs.length; i++) {
		if(inputs[i].id) {
			var sect_line = inputs[i].id.split("_");
			if(sect_line[0]==section && sect_line[1]=="counfunds" && sect_line[2]!="total" && sect_line[2]!="subtotal" && sect_line[2]!="fringe") {
				if(!isNaN(parseFloat(inputs[i].value.replace(/[^0-9\.]/g, "")))) {
					counfunds += parseFloat(inputs[i].value.replace(/[^0-9\.]/g, ""));
				}
			} else if (sect_line[0]==section && sect_line[1]=="matchfunds" && sect_line[2]!="total" && sect_line[2]!="subtotal" && sect_line[2]!="fringe") {
				if(!isNaN(parseFloat(inputs[i].value.replace(/[^0-9\.]/g, "")))) {
					matchfunds += parseFloat(inputs[i].value.replace(/[^0-9\.]/g, ""));
				}
			}
		}
	}
	projcosts = counfunds + matchfunds;
	if(fringe==1) {
		fringe = true;
	} else {
		fringe = false;
	}


	if (fringe) {
		var sectprojsubtotal = get(section+"_projcosts_subtotal");
		var sectcounsubtotal = get(section+"_counfunds_subtotal");
		var sectmatchsubtotal = get(section+"_matchfunds_subtotal");
		var projectfringe = get(section+"_projcosts_fringe");
		var counfringe = get(section+"_counfunds_fringe");
		var matchfringe = get(section+"_matchfunds_fringe");

		sectprojsubtotal.value = formatCurrency(projcosts);
		sectcounsubtotal.value = formatCurrency(counfunds);
		sectmatchsubtotal.value = formatCurrency(matchfunds);
		if (!isNaN(parseFloat(projectfringe.value.replace(/[^0-9\.]/g, "")))) {
			projcosts += parseFloat(projectfringe.value.replace(/[^0-9\.]/g, ""));
		}
		if (!isNaN(parseFloat(counfringe.value.replace(/[^0-9\.]/g, "")))) {
			counfunds += parseFloat(counfringe.value.replace(/[^0-9\.]/g, ""));
		}
		if (!isNaN(parseFloat(matchfringe.value.replace(/[^0-9\.]/g, "")))) {
			matchfunds += parseFloat(matchfringe.value.replace(/[^0-9\.]/g, ""));
		}
		sectprojtotal.value = formatCurrency(projcosts);
		sectcountotal.value = formatCurrency(counfunds);
		sectmatchtotal.value = formatCurrency(matchfunds);
	} else {
		sectprojtotal.value = formatCurrency(projcosts);
		sectcountotal.value = formatCurrency(counfunds);
		sectmatchtotal.value = formatCurrency(matchfunds);
	}
}

function calcFringe(section) {
	var projectfringe = get(section+"_projcosts_fringe");
	var counfringe = get(section+"_counfunds_fringe");
	var matchfringe = get(section+"_matchfunds_fringe");
	var counfringeval = 0;
	var matchfringeval = 0;
	var projfringe = 0;
	if (!isNaN(parseFloat(counfringe.value.replace(/[^0-9\.]/g, "")))) {
		counfringeval += parseFloat(counfringe.value.replace(/[^0-9\.]/g, ""));
	}
	if (!isNaN(parseFloat(matchfringe.value.replace(/[^0-9\.]/g, "")))) {
		matchfringeval += parseFloat(matchfringe.value.replace(/[^0-9\.]/g, ""));
	}
	counfringe.value = formatCurrency(counfringeval);
	matchfringe.value = formatCurrency(matchfringeval);
	projfringe = counfringeval + matchfringeval;
	projectfringe.value = formatCurrency(projfringe);
	calcTotals(section, 1);
}

function calcCumulative(pm) {
	var prior = get(pm+"_prior");
	var curr = get(pm+"_curr");
	var cum = get(pm+"_cum");
	cum.value = parseInt(prior.value) + parseInt(curr.value);
}

function calcCounMatchFunds(fld, fr, inputtype) {
var fldname = fld.name;
var value = fld.value;
var secttypeser = fldname.split("_", 3);
var section = secttypeser[0];
var type = secttypeser[1];
var serial = secttypeser[2];
var projcosts = get(section + "_projcosts_" + serial);
var counfunds = get(section + "_counfunds_" + serial);
var matchfunds = get(section + "_matchfunds_" + serial);
switch(inputtype) {
	case "Rt":
	case "Ru":
		counval = parseFloat(counfunds.value.replace(/[^0-9\.]/g,""));
		if (isNaN(counval)) {
			counval = 0;
		}
		matchval = parseFloat(matchfunds.value.replace(/[^0-9\.]/g,""));
		if (isNaN(matchval)) {
			matchval = 0;
		}
		projcostval = parseFloat(projcosts.value.replace(/[^0-9\.]/g,""));
		if (projcostval == 0 || isNaN(projcostval)) {
			if(isNaN(matchval)) {
				matchval = 0;
			}
			if(isNaN(counval)) {
				counval = 0;
			}
			matchfunds.value = formatCurrency(matchval);
			counfunds.value = formatCurrency(counval);
			projcostval = matchval + counval;
			projcosts.value = formatCurrency(projcostval);
			return true;
		}
		if (counval>projcostval || matchval>projcostval) {
			alert("You may not request an amount greater than the project cost.");
			return false;
		}
		if (type=="matchfunds") {
			matchfunds.value = formatCurrency(matchval);
			counfunds.value = formatCurrency(projcostval - matchval);
		} else if (type=="counfunds") {
			counfunds.value = formatCurrency(counval);
			matchfunds.value = formatCurrency(projcostval - counval);
		}
		calcTotals(section, fr);
		break;
	case "Pc":
		counval = parseFloat(counfunds.value.replace(/[^0-9\.]/g,""));
		if (isNaN(counval)) {
			counval = 0;
		}
		matchval = parseFloat(matchfunds.value.replace(/[^0-9\.]/g,""));
		if(isNaN(matchval)) {
			matchval = 0;
		}
		if(isNaN(counval)) {
			counval = 0;
		}
		matchfunds.value = formatCurrency(matchval);
		counfunds.value = formatCurrency(counval);
		projcostval = matchval + counval;
		projcosts.value = formatCurrency(projcostval);
		calcTotals(section, fr);
		break;		
	}
}


function calcfunds() {

var totalcosts = document.getElementById("totalprojcosts");
var counfunds = document.getElementById("counfunds").value;
var matchfunds = document.getElementById("matchfunds").value;
var counfundsinput = document.getElementById("counfunds");
var matchfundsinput = document.getElementById("matchfunds");
var counfundspercent = get("council_percent");
var counfundspercentraw = "";
var matchpercent = get("match_percent");
var matchpernentraw = "";
var re = /[^0-9]/;
counfundsreturn = "";
if (re.test(counfunds)) {
	for (var i=0; i<counfunds.length; i++) {
		if ((counfunds.charCodeAt(i) >=48 && counfunds.charCodeAt(i) <=57) || (counfunds.charCodeAt(i)==46)) {
		counfundsreturn = counfundsreturn + counfunds.charAt(i);
		}
	}
	counfunds = counfundsreturn;
	counfundsinput.value = formatCurrency(counfundsreturn);
	}else{
	counfundsinput.value = formatCurrency(counfunds);
}
matchfundsreturn = "";
if (re.test(matchfunds)) {
	for (var i=0; i<matchfunds.length; i++) {
		if ((matchfunds.charCodeAt(i) >=48 && matchfunds.charCodeAt(i) <=57) || (matchfunds.charCodeAt(i)==46)) {
		matchfundsreturn = matchfundsreturn + matchfunds.charAt(i);
		}
	}
	matchfunds = matchfundsreturn;
	matchfundsinput.value = formatCurrency(matchfundsreturn);
	} else {
	matchfundsinput.value = formatCurrency(matchfunds);
	}

if (isNaN(counfunds) || isNaN(matchfunds) || (counfunds=="") || (matchfunds=="")) {
	totalcosts.value = "";
	}
	else {
	subtotal = parseFloat(counfunds) + parseFloat(matchfunds);
	totalcosts.value = formatCurrency(subtotal.toFixed(2));
	matchpercentraw = (matchfunds/subtotal) * 100;
	matchpercent.value =  matchpercentraw.toFixed(0) + "%";
	counfundspercentraw = (counfunds/subtotal) * 100;
	counfundspercent.value =  counfundspercentraw.toFixed(0) + "%";
	}
}

function addBudgetElements(section, fr) {
	var insertAtRow = get(section + "_insert");
	var counter = get(section + "_counter");
	var counterval = counter.value;
	var rows = document.getElementsByTagName("tr");
	for(i=0; i<rows.length; i++) {
		var ids = rows[i].id.split("_");
		if(ids[0]==section) {
			var row = get(rows[i].id).cloneNode(true);
			break;
		}
	}
	row.id = section+"_row_"+counterval;
	insertAtRow.parentNode.insertBefore(row, insertAtRow);
	var budgel = updateBudgetElementIds(row, counter.value, fr);
	counter.value ++;
	var lineids = get(section+'_lineids');
	if(lineids) {
		lineids.value = lineids.value + counter.value + '|';
	}		
}

function updateBudgetElementIds(node, c, fr) {
	var next = parseInt(c) + 1;
	nodeId = node.id;
	idels = nodeId.split("_");
	if(idels.length==4) {
		idels[3]=next;
	} else {
		idels[2]=next;
	}
	if(node.id) {
		node.id = idels.join("_");
		if(idels[2]=='cf' || idels[2]=='mf') {
			node.value = "$0.00";
		} else {
			node.value = "";
		}
		if (node.nodeValue) {
			node.nodeValue = "";
		} else if(idels[1]=="remlink") {
			el_row = idels[0]+'_row_'+idels[2];
			node.removeAttribute('onclick');
			var func = function() {
					var rem = remBudgetElements(node.id, fr);
				};
			if (window.addEventListener) {
				node.addEventListener("click", func, false);
			} else {
				node.attachEvent("onclick", func);
			}
		}
	}
			
	if(node.name) {
		node.name = node.id;
	}
	var children = node.childNodes;
	for (var i=0; i<children.length; i++) {
		if(children[i].nodeType==1) {
			nodeId += updateBudgetElementIds(children[i], c, fr);
		}
	}
	return nodeId;
}

function remBudgetElements(sect_line, fringe) {
	var section_row_line = sect_line.split("_");
	var section = section_row_line[0];
	var item_count = get(section+'_counter');
	var item_count_val = parseInt(item_count.value);
	if(item_count_val > 1) {
		var el = get(section_row_line[0]+'_row_'+section_row_line[2]);
		el.parentNode.removeChild(el); 
		var next_line_item = parseInt(section_row_line[2]);	
		for(i=next_line_item+1; i<=item_count_val; i++) {
			item_number = i;
			prev_item_number = i-1;
			var n = get(section+'_row_'+item_number);
			var updated = updateBudgetElements(n, section, item_number, prev_item_number, fringe);
		}
		item_count.value = item_count_val - 1;
		calcTotals(section, fringe);
		var mfdInput = get('mfd');
		mfdInput.value += sect_line+",";
	}
}
function updateBudgetElements(n, section, item_number, prev_item_number, fringe) {
	var nodeId = n.id;
	var nodeIdVals = nodeId.split("_");
	var newNodeId = "";
	line_number = prev_item_number;
	if (nodeIdVals[2] == item_number) {
		nodeIdVals[2] = line_number;
		n.name = nodeIdVals.join("_");
		n.id = nodeIdVals.join("_");
		if(n.id == section+'_remlink_'+line_number) { //obj_2_act_D_rem_link WAS obj_2_act_E_rem_link
			n.removeAttribute('onclick');
			var sect_line = section+"_row_"+line_number;
			var func = function() { 
					var removed = remBudgetElements(n.id, fringe);
			}
			if (window.addEventListener) {
				n.addEventListener("click", func, false);
			} else {
				n.attachEvent("onclick", func);
			}
		}			
	}
	var children = n.childNodes;
	for (var i=0; i < children.length; i++) {
		if (children[i].nodeType == 1) {
			nodeId += updateBudgetElements(children[i], section, item_number, prev_item_number);
		}
	}
	return nodeId;
}

function updateBudgElementIds(node, section, curr, prev) {
	var nodeId = node.id;
	var section = section.split("_"); 
	var nodeIdVals = nodeId.split("_");
	var newNodeId = "";
	if (nodeIdVals.length >= 3) {
		if (nodeIdVals[3] == curr) {
			nodeIdVals[3] = prev_act_letter;
			n.name = nodeIdVals.join("_");
			n.id = nodeIdVals.join("_");
			if (n.id == o + '_act_' + prev_act_letter) {
				n.value = prev_act_letter;
			}
			if(n.id == o+'_act_'+prev_act_letter+'_rem_link') { //obj_2_act_D_rem_link WAS obj_2_act_E_rem_link
				var func = function() { 
					var el = get(o + '_act_' + prev_act_letter + '_table'); //obj_2_act_D_rem_link - new name of this table
					el.parentNode.removeChild(el); 
					var act_count = get(o+'_act_count');
					var act_count_val = act_count.value;
					var last_act_letter = parseInt(act_count_val)+65;
					for(i=asc(prev_act_letter)+1; i<last_act_letter; i++) {
						act_letter = chr(i); //obj_act_E
						prev_act_letter = chr(i-1);
						var n = get(o+'_act_'+act_letter+'_table');
						var updated = updateActLetters(n, o, act_letter, prev_act_letter);
					}
					act_count.value = act_count_val - 1;
				};
				if (window.addEventListener) {
					n.addEventListener("click", func, false);
				} else {
					n.attachEvent("onclick", func);
				}
			}			
		}
	}
	var children = n.childNodes;
	for (var i=0; i < children.length; i++) {
		if (children[i].nodeType == 1) {
			nodeId += updateActLetters(children[i], o, curr_letter, prev_act_letter);
		}
	}
	return nodeId;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////                               FORMAT FUNCTIONS                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function replacewithformatted(fld) {
	fld.value = formatPhone(fld.value);
}

function formatCurrency(fld) {
var newcurr="";
var currlen;
if (typeof fld == "object") {
	var curr = fld.value.replace(/[^0-9.]/g, '');		
	curr = parseFloat(curr);
} else  {
	var curr = parseFloat(fld);
} 
if (isNaN(curr)) {
	return "";
}
var currstr = curr+"";
if (currstr.indexOf('.')>-1) {
	decprecision = currstr.length-currstr.indexOf('.')-1;
} else {
	decprecision = 2;
}

if(decprecision < 2) {
	decprecision = 2;
}
curr = curr.toFixed(decprecision);
	for (var i=curr.length-1; i>=0; i--) {
		if (((curr.length-decprecision+1-i)%3==0) && (i < curr.length - (decprecision+1)-3)) {
		newcurr = curr.charAt(i) + ","  + newcurr;
		} else {
		newcurr = curr.charAt(i) + newcurr;
		}
	}
	newcurr = "$"+newcurr;
	fld.value = newcurr;
	return newcurr;
}
function formatPhone(strng) {
var strippedphone = strng.replace(/[\D]/g, '');
	if (strippedphone.length == 10) {
		var formattedphone = "(" + strippedphone.substring(0,3) + ")" + strippedphone.substring(3,6) + "-" 
			+ strippedphone.substring(6, strippedphone.length);
	} else {
		if (strippedphone.length == 7) {
			var formattedphone = strippedphone.substring(0,3) + "-" + strippedphone.substring(3, strippedphone.length);
		} else {
			if (strippedphone.length > 10) {
				var formattedphone = "(" + strippedphone.substring(0,3) + ")" + strippedphone.substring(3,6) + "-" 
			+ strippedphone.substring(6, 10) + " x." + strippedphone.substring(10, strippedphone.length);
				} else {
					var formattedphone = strng;
				}
			}
		}
	return formattedphone;
}

function copyMailAddr(checkbox) {
	var fromflds;
	var toflds;
	var fromfld;
	var tofld;
	var i;
	
	fromflds = new Array();
	fromflds[0] = "p1_addr1";
	fromflds[1] = "p1_addr2";
	fromflds[2] = "p1_ste_floor";
	fromflds[3] = "p1_city";	fromflds[4] = "p1_state";
	fromflds[5] = "p1_zip";
	fromflds[6] = "p1_zip4";
	
	toflds = new Array();
	toflds[0] = "m1_addr1";
	toflds[1] = "m1_addr2";
	toflds[2] = "m1_ste_floor";
	toflds[3] = "m1_city";
	toflds[4] = "m1_state";
	toflds[5] = "m1_zip";
	toflds[6] = "m1_zip4";
	
	if (checkbox.checked) {
		for (i = 0; i <= 6; ++i) {
			fromfld = get(fromflds[i]);
			tofld = get(toflds[i]);
			tofld.value = fromfld.value;
		}
	} else {
		for (i=0; i<=6; ++i) {
			tofld = get(toflds[i]);
			tofld.value = "";
		}
	}
}
			
function sameAs(checkbox) {
var prefixes = checkbox.name.split("_");
var i;

var infoflds = new Array();
infoflds[0] = "_fname";
infoflds[1] = "_title";
infoflds[2] = "_phone";
infoflds[3] = "_fax";
infoflds[4] = "_email";
infoflds[5] = "_lname";

var fromprefix = prefixes[0];
var toprefix = prefixes[1];

if (checkbox.checked) {
	for (i = 0; i<infoflds.length; ++i) {
		var fromfldname = fromprefix + infoflds[i];
		var tofldname = toprefix + infoflds[i];
		var fromfld = document.getElementById(fromfldname);
		var tofld = document.getElementById(tofldname);
		tofld.value = fromfld.value;
	}
} else {
	for (i=0; i<infoflds.length; ++i) {
		var tofldname = toprefix + infoflds[i];
		var tofld = document.getElementById(tofldname);
		tofld.value = "";
	}
}
}

function app_mods(checkbox) {
	var ckboxname = checkbox.name;
	var rowid = ckboxname + '_mods_row';
	var textid = 'mods_'+ckboxname;
	var row = get(rowid);
	var text_area = get(textid);
	
	if(checkbox.checked) {
		row.style.display = '';
	} else {
		row.style.display = 'none';
		text_area.innerHTML = "";
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////                                VALIDATION FUNCTIONS                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function validateEmail(email) { 
	var addr_spec = "/^[A-Za-z0-9]*@[A-Za-z0-9]*$/";
	var strToMatch = email.value;
	if (strToMatch.match(addr_spec)) {
		return true;
	} else {
		return false;
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////                                ADD/REMOVE ACTIVITY/OBJECTIVE FUNCTIONS                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function remActs(act) {
	var el = get(act); 
	el.parentNode.removeChild(el); 
	var obj_act = act.split("_");
	var o = obj_act[0]+'_'+obj_act[1];
	var next_act_letter = obj_act[3];	
	var act_count = get(o+'_act_count');
	var act_count_val = act_count.value;
	var last_act_letter = parseInt(act_count_val)+65;
	for(i=asc(next_act_letter)+1; i<last_act_letter; i++) {
		act_letter = chr(i);
		prev_act_letter = chr(i-1);
		var n = get(o+'_act_'+act_letter+'_table');
		var updated = updateActLetters(n, o, act_letter, prev_act_letter);
	}
	act_count.value = act_count_val - 1;
}

function remAttachments(i, filename) {
	var att_link = get('link_att_'+i);
	var delAttach = get('delAttach');
	var delFiles = get('delFiles');
	var inputCell = get('td_att_'+i);
	var descCell =get('td_att_'+i+'_desc');
	var remCell = get('td_rem_'+i);
	delAttach.value += 'att'+i+"|"
	delFiles.value += filename + "|";
	var fInput = document.createElement("input");
	fInput.name = "att"+i;
	fInput.id = "att"+i;
	fInput.size = "80";
	fInput.type = "file";
	inputCell.appendChild(fInput);
	inputCell.colspan = 4;
	var fInputdesc = document.createElement("input");
	fInputdesc.name = "att"+i+"_desc";
	fInputdesc.id = "att"+i+"_desc";
	fInputdesc.size = "40";
	fInputdesc.type = "text";
	descCell.innerHTML="";
	descCell.appendChild(fInputdesc);
	att_link.parentNode.removeChild(att_link);
	remCell.parentNode.removeChild(remCell);	
}
function addActs(obj) {
	//var obj_num = obj.substr(4, 6);
	var act_count = get(obj + '_act_count');
	act_count_val = parseInt(act_count.value);
	var act_letter = chr(65);
	var next_act_letter = chr(act_count_val + 65);
	var actTemplate = get(obj + '_act_' + act_letter + '_table').cloneNode(true);
	act_count.value = act_count_val + 1;
	var ins_location = get(obj + '_acts_ins_before');
	var parentEl = ins_location.parentNode;
	ins_location.parentNode.insertBefore(actTemplate, ins_location);
	//Update act letters
	var updateIds = updateActElementIds(actTemplate, obj, act_letter, next_act_letter);
}

function updateActElementIds(n, o, curr_letter, next_act_letter) {
	var nodeId = n.id;
	var obj_num = o.split("_"); 
	if (obj_num[1] < 10) {
		obj_id_len = 11;
	} else {
		obj_id_len = 12;
	}
	var nodeIdVals = nodeId.split("_");
	var newNodeId = "";
	if (nodeIdVals.length >= 3) {
		if (nodeIdVals[3] == curr_letter) {
			nodeIdVals[3] = next_act_letter;
			for (i=0; i< nodeIdVals.length; i++) {
				newNodeId += nodeIdVals[i] + '_';
			}
			n.name = newNodeId.substr(0, newNodeId.length -1);
			n.id = newNodeId.substr(0, newNodeId.length -1);
			if (n.id == o + '_act_' + next_act_letter + '_start' || n.id == o + '_act_' + next_act_letter + '_end' || n.id == o + '_act_' + next_act_letter + '_staff') {
				n.value = "";
			}			
			if (n.id == o + '_act_' + next_act_letter) {
				n.value = next_act_letter;
			}
			if (n.id == o + '_act_' + next_act_letter + '_desc') {
				n.innerHTML = "";
			}				
			if (n.id == o + '_act_' + next_act_letter + '_rem_link') {
				if(n.style.display == "none") {
					n.style.display = 'inline';
				}
				var func = function() { 
					var el = get(o + '_act_' + next_act_letter + '_table'); 
					el.parentNode.removeChild(el); 
					var act_count = get(o+'_act_count');
					var act_count_val = act_count.value;
					var last_act_letter = parseInt(act_count_val)+65;
					for(i=asc(next_act_letter)+1; i<last_act_letter; i++) {
						act_letter = chr(i);
						prev_act_letter = chr(i-1);
						var n = get(o+'_act_'+act_letter+'_table');
						var updated = updateActLetters(n, o, act_letter, prev_act_letter);
					}
					act_count.value = act_count_val - 1;
				};
				if (window.addEventListener) {
					n.addEventListener("click", func, false);
				} else {
					n.attachEvent("onclick", func);
				}
			}
		}
	}
	var children = n.childNodes;
	for (var i=0; i < children.length; i++) {
		if (children[i].nodeType == 1) {
			nodeId += updateActElementIds(children[i], o, curr_letter, next_act_letter);
		}
	}
	return nodeId;
}

function updateActLetters(n, o, curr_letter, prev_act_letter) {
	var nodeId = n.id;
	var obj_num = o.split("_"); 
	var nodeIdVals = nodeId.split("_");
	var newNodeId = "";
	if (nodeIdVals.length >= 3) {
		if (nodeIdVals[3] == curr_letter) {
			nodeIdVals[3] = prev_act_letter;
			n.name = nodeIdVals.join("_");
			n.id = nodeIdVals.join("_");
			if (n.id == o + '_act_' + prev_act_letter) {
				n.value = prev_act_letter;
			}
			if(n.id == o+'_act_'+prev_act_letter+'_rem_link') { //obj_2_act_D_rem_link WAS obj_2_act_E_rem_link
				var func = function() { 
					var el = get(o + '_act_' + prev_act_letter + '_table'); //obj_2_act_D_rem_link - new name of this table
					el.parentNode.removeChild(el); 
					var act_count = get(o+'_act_count');
					var act_count_val = act_count.value;
					var last_act_letter = parseInt(act_count_val)+65;
					for(i=asc(prev_act_letter)+1; i<last_act_letter; i++) {
						act_letter = chr(i); //obj_act_E
						prev_act_letter = chr(i-1);
						var n = get(o+'_act_'+act_letter+'_table');
						var updated = updateActLetters(n, o, act_letter, prev_act_letter);
					}
					act_count.value = act_count_val - 1;
				};
				if (window.addEventListener) {
					n.addEventListener("click", func, false);
				} else {
					n.attachEvent("onclick", func);
				}
			}			
		}
	}
	var children = n.childNodes;
	for (var i=0; i < children.length; i++) {
		if (children[i].nodeType == 1) {
			nodeId += updateActLetters(children[i], o, curr_letter, prev_act_letter);
		}
	}
	return nodeId;
}

function addObjs() {
	var obj_count = get('obj_count');
	var obj_count_value = obj_count.value;
	var obj_clone = get('obj_0').cloneNode(true);
	var ins_before = get('obj_insert_before');
	var parentEl = ins_before.parentNode;
	parentEl.insertBefore(obj_clone, ins_before);
	obj_clone.style.display = 'inline';
	var updateIds = updateObjElementIds(obj_clone, obj_count_value);
	obj_count.value = parseInt(obj_count.value) + 1;
}

function updateObjElementIds(node, curr) {
	var next = parseInt(curr)+1;
	nodeId = node.id;
	idels = nodeId.split("_");
	if(idels[0]=="obj") {
		idels[1]=next;
	}
	var obj = idels[0]+'_'+idels[1];
	if(node.id) {
		node.id = idels.join("_");
		if(idels[3]=="Acts") {
			node.removeAttribute('onclick');
			var func = function() { 
				var newObj = addActs(obj);
			};
			if (window.addEventListener) {
				node.addEventListener("click", func, false);
			} else {
				node.attachEvent("onclick", func);
			}				
		} else if (idels[2]=="remlink") {
			node.removeAttribute('onclick');
			var func = function() {
				var removed = remObj('obj_'+next);
				};
			if (window.addEventListener) {
				node.addEventListener("click", func, false);
			} else {
				node.attachEvent("onclick", func);
			}
		} else if (idels[3]=="PMs") {
			node.removeAttribute('onclick');
			var func = function() {
					var newPM = addPMs(obj);
				};
			if (window.addEventListener) {
				node.addEventListener("click", func, false);
			} else {
				node.attachEvent("onclick", func);
			}
		} else if(idels[2]=="number") {
			node.value = next;
		} else if(idels[4]=="pms") {
			node.removeAttribute('onchange');
			var func = function() {
					var limited = limitsafmotr(node.id);
				};
			if (window.addEventListener) {
				node.addEventListener("click", func, false);
			} else {
				node.attachEvent("onclick", func);
			}
		}
	}
			
	if(node.name) {
		node.name = node.id;
	}
	var children = node.childNodes;
	for (var i=0; i<children.length; i++) {
		if(children[i].nodeType==1) {
			nodeId += updateObjElementIds(children[i], curr);
		}
	}
	return nodeId;
}
function remObj(obj) {
	var el = get(obj); 
	el.parentNode.removeChild(el); 
	var obj_act = obj.split("_");
	var next_obj = parseInt(obj_act[1]);	
	var obj_count = get('obj_count');
	var obj_count_val = obj_count.value;
	var last_obj = parseInt(obj_count_val);
	for(i=next_obj+1; i<=last_obj; i++) {
		curr = i;
		prev = i-1;
		var n = get('obj_'+i);
		var updated = updateObjNumbers(n, curr, prev);
	}
	obj_count.value = obj_count_val - 1;
}

function updateObjNumbers(n, curr_number, prev_obj_number) {
	var nodeId = n.id;
	var nodeIdVals = nodeId.split("_");
	var newNodeId = "";
	nodeIdVals[1] = prev_obj_number;
	if(n.name) {
		n.name = nodeIdVals.join("_");
	}
	if(n.id) {
		n.id = nodeIdVals.join("_");
	}
	if (n.id == 'obj_' + prev_obj_number + '_number') {
		n.value = prev_obj_number;
	}
	if(n.id == 'obj_'+prev_obj_number+'_remlink') { //obj_2_act_D_rem_link WAS obj_2_act_E_rem_link
		n.removeAttribute('onclick');
		var func = function() { 
			var removed = remObj('obj_'+prev_obj_number);
			};
		if (window.addEventListener) {
			n.addEventListener("click", func, false);
		} else {
			n.attachEvent("onclick", func);
		}
	}			
	var children = n.childNodes;
	for (var i=0; i < children.length; i++) {
		if (children[i].nodeType == 1) {
			nodeId += updateObjNumbers(children[i], curr_number, prev_obj_number);
		}
	}
	return nodeId;
}

function updateObjElementIds_old(n, o, next, curr) {
	var nodeId = n.id;
	var obj_num = o.split("_"); 
	var nodeIdVals = nodeId.split("_");
	var newNodeId = "";
	if (nodeIdVals.length >= 1) {
		if (nodeIdVals[1] == 1) {
			nodeIdVals[1] = next;
			for (i=0; i< nodeIdVals.length; i++) {
				newNodeId += nodeIdVals[i] + '_';
			}
			n.name = newNodeId.substr(0, newNodeId.length -1);
			n.id = newNodeId.substr(0, newNodeId.length -1);
			if (n.id == o + '_pm_' + next + '_rem_link') {
				if (window.addEventListener) {
					var func = function() { var el = get(o + '_pm_' + next); el.parentNode.removeChild(el);};
					n.addEventListener("click", func, false);
				} else {
					n.attachEvent("onclick", func);
				}
			}
		}
	}
	var children = n.childNodes;
	for (var i=0; i < children.length; i++) {
		if (children[i].nodeType == 1) {
			nodeId += updateObjElementIds(children[i], o, next, curr);
		}
	}
	return nodeId;
	
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////                                ADD USER INPUT ELEMENTS FUNCTIONS                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function addUserInputs(obj, ib, count) {
	var uitemplate = get(obj).cloneNode(true);
	var uicount = get(count);
	var curr = parseInt(uicount.value);
	var next = curr + 1;
	uicount.value = next;
	var UIIds = updateUIElementIds(uitemplate, obj, next, curr, count);
	var refChild = get(ib);
	parentEl = refChild.parentNode;
	parentEl.insertBefore(uitemplate, refChild);
}

function updateUIElementIds(n, o, next, curr, count) {
	var nodeId = n.id;
	var obj_num = o.split("_"); 
	var nodeIdVals = nodeId.split("_");
	var newNodeId = "";
	if(nodeIdVals[0]=="rpt") {
		nodeIdVals[2] = parseInt(nodeIdVals[2])+curr;
	} else if (nodeIdVals[0]=="budg") {
		nodeIdVals[2] = next + 'new';
	} else {
		nodeIdVals[2] = next;
	}
	newNodeId = nodeIdVals.join("_");
	if (n.name != "" && n.id != "") {
		n.name = newNodeId;
		n.id = newNodeId;
	}
	if (nodeIdVals[0]=="per" && nodeIdVals[1]=="num") {
		n.innerHTML="<strong>"+next+"</strong>";
	}
	if (n.innerHTML=="[x]") {
		n.removeAttribute('onclick');
		var tr = n.parentNode.parentNode.id;
		var uitype = tr.split("_");
		if(uitype[0]=='nofa') {
			var func = function() { var el = get(tr); el.parentNode.removeChild(el); var uicount = get('ns_count'); uicount.value -= 1;};
		} else if(uitype[0]=='rpt') {
			var func = function() { var el = get(tr); el.parentNode.removeChild(el); var uicount = get('rd_count'); uicount.value -= 1;};
		} else if(uitype[0]=='user') {
			var func = function() { var el = remUserInputs(tr, 'ui_count', 'mfd');};
		} else {
			var func = function() { var el = get(tr); el.parentNode.removeChild(el); var uicount = get('ui_count'); uicount.value -= 1;};
		}
		if (window.addEventListener) {
			n.addEventListener("click", func, false);
		} else {
			n.attachEvent("onclick", func);
		}
	} else {
		if(n.innerHTML=="Period 1") {
			n.innerHTML = "Period "+next;
		}
	}
	if (n.style.display == "none") {
		n.style.display = '';
	}
	if (nodeIdVals[1] == "group") {
		n.selectedIndex = -1;
	} else {
		if (n.nodeName != "OPTION" && next>curr) {
			n.value = "";
		}
	}
	var children = n.childNodes;
	for (var i=0; i < children.length; i++) {
		if (children[i].nodeType == 1) {
			nodeId += updateUIElementIds(children[i], o, next, curr);
		}
	}
	return nodeId;
}

function remUserInputs(tr, uic, mfd) {
	var el = get(tr); 
	if(typeof mfd!="undefined") {
		var uiid = tr.split('_');
		if(uiid[0]=="user") {
			var emailid = 'staff_email_'+uiid[2];
			var budgid = get(emailid).value;
		} else {
			var budgid = uiid[2];
		}
		var el_mfd = get(mfd);
		el_mfd.value += budgid+',';
	}
	var trid = parseInt(uiid[2])+1;
	var uicount = get(uic);	
	if(uiid[0]!='exp' && uiid[0]!='new') {
		for(i=trid; i<=parseInt(uicount.value); i++) {
			var trnode = get(uiid[0]+'_'+uiid[1]+'_'+i);
			var obj = tr;
			var prev = i-1;
			var curr = i;
			var count = uicount;
			var updatedEl = updateUIElementIds(trnode, obj, prev, curr, count);
		}
	}
	el.parentNode.removeChild(el); 
	uicount.value --;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////                   PERFORMANCE MEASURE FUNCTIONS                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function addPMs(obj) {
	var pmtemplate = get('obj_0_pm_1').cloneNode(true);
	if(pmtemplate.style.display == "none") {
		pmtemplate.style.display = '';
	}
	var pmcount = get(obj + '_pm_count');
	var curr = parseInt(pmcount.value);
	var next = curr + 1;
	pmcount.value = next;
	var pmIds = updatePmElementIds(pmtemplate, obj, next, curr);
	var pm_src_row = document.createElement("tr");
	var pm_src_cell = document.createElement("td");
	var pm_src_ta = document.createElement("textarea");
	pm_src_row.id = obj + '_pm_' + next + '_src_rw';
	pm_src_cell.colSpan = '7';
	pm_src_ta.id = obj + '_pm_' + next + '_src';
	pm_src_ta.name = obj + '_pm_' + next + '_src';
	pm_src_ta.style.height = "5em";
	pm_src_ta.style.width = "58em";
	pm_src_ta.style.display = 'none';
	pm_src_cell.appendChild(pm_src_ta);
	pm_src_row.appendChild(pm_src_cell);
	
	var refChild = get(obj + '_pm_insert_before');
	parentEl = refChild.parentNode;
	parentEl.insertBefore(pmtemplate, refChild);
	parentEl.insertBefore(pm_src_row, refChild);
	return(pm_src_row);
}

function updatePmElementIds(n, o, next, curr) {
	var nodeId = n.id;
	var obj_num = o.split("_"); 
	var nodeIdVals = nodeId.split("_");
	var newNodeId = "";
	if(nodeId=='obj_0_pm_1') {
		nodeID = 'obj_'+obj_num[1]+'_pm_'+next+'_src_rw';
		n.id = nodeID;
	}
	if (nodeIdVals.length >= 3) {
		if (nodeIdVals[3] == 1) {
			nodeIdVals[3] = next;
			nodeIdVals[1] = obj_num[1];
			for (i=0; i< nodeIdVals.length; i++) {
				newNodeId += nodeIdVals[i] + '_';
			}
			n.name = newNodeId.substr(0, newNodeId.length -1);
			n.id = newNodeId.substr(0, newNodeId.length -1);
			var pfx = o + '_pm_' + next;
			if (n.id == pfx + '_pms') {
				n.selectedIndex = -1;
				var func = function() { var limited = limitsafmotr(n.id); };
				if (window.addEventListener) {
					n.addEventListener("change", func, false);
				} else {
					n.attachEvent("onchange", func);
				}				
			}
			if (n.id == pfx + '_pr') {
				n.value = "";
			}
			if (n.id == pfx + '_sa') {
				n.value = "";
			}
			if (n.id == pfx + '_fm') {
				n.value = "";
			}
			if (n.id == pfx + '_ot') {
				n.value = "";
			}				
			if (n.id == pfx + '_rem_link') {
				var func = function() { var el = get(pfx+'_src_rw'); el.parentNode.removeChild(el); var pm_el = get(pfx); pm_el.parentNode.removeChild(pm_el); var count=get(o+'_pm_count'); count.value--; };
				if (window.addEventListener) {
					n.addEventListener("click", func, false);
				} else {
					n.attachEvent("onclick", func);
				}
			}
			if (n.id == pfx + '_sh') {
				n.removeAttribute("onclick");
				n.onclick = null;
				var func = function() { var t_area = get(pfx + '_src'); var disp_link = get(pfx + '_sh'); if (t_area.style.display == "none") { t_area.style.display = 'inline'; disp_link.innerHTML = "-"; } else { t_area.style.display = 'none'; disp_link.innerHTML = "+"; } }				
				if (window.addEventListener) {
					n.addEventListener("click", func, false);
				} else {
					n.attachEvent("onclick", func);
				}
			}
			
		}
	}
	var children = n.childNodes;
	for (var i=0; i < children.length; i++) {
		if (children[i].nodeType == 1) {
			nodeId += updatePmElementIds(children[i], o, next, curr);
		}
	}
	return nodeId;
	
}

function remPMs(row, obj) {
	var pmrow = get(row);
	if(obj) {
		var pmcount = get('obj_'+obj+'_pm_count');
		var pmcountval = pmcount.value;
		pmcount.value = pmcountval - 1;
	}
	pmrow.parentNode.removeChild(pmrow);
}

function limitsafmotr(pmsel) {
	var pmsel = get(pmsel);
	var sel_id = pmsel.id;
	var sel_vals = sel_id.split("_");
	var obj_num = sel_vals[1];
	var pm_num = sel_vals[3];
	var sa = get("obj_" + obj_num + "_pm_" + pm_num + "_sa");
	var fm = get("obj_" + obj_num + "_pm_" + pm_num + "_fm");
	var ot = get("obj_" + obj_num + "_pm_" + pm_num + "_ot");
	if(null == sa || null==fm || null==ot) {
		var sa = get("obj_" + obj_num + "_pm_" + pm_num + "_sa_"+sel_vals[5]);
		var fm = get("obj_" + obj_num + "_pm_" + pm_num + "_fm_"+sel_vals[5]);
		var ot = get("obj_" + obj_num + "_pm_" + pm_num + "_ot_"+sel_vals[5]);
	}
	var selected = pmsel.value;	
	
	switch (selected) {
		case "EM01":
		case "ED01":
		case "ED02":
		case "ED03":
		case "ED04":
		case "ED05":
		case "HO01":
		case "HO02":
		case "HE01":
		case "CH01":
		case "RE01":
		case "TR01":
		case "QA01":
		case "QA08":
		case "QA09":
		case "CS01":
			sa.disabled = false;
			fm.value = "";
			fm.disabled = true;
			ot.value = "";
			ot.disabled = true;
			break;
		case "ED13":
			sa.value = "";
			sa.disabled = true;
			fm.disabled = false;
			ot.value = "";
			ot.disabled = true;
			break;
		case "EM10":
		case "ED14":
		case "HO11":
		case "HE08":
		case "CH08":
		case "RE08":
		case "TR08":
		case "QA06":
		case "QA07":
		case "QA11":
		case "CS09":
			sa.disabled = false;
			fm.disabled = false;
			ot.disabled = false;
			break;
		default:
			sa.value = "";
			sa.disabled = true;
			fm.value = "";
			fm.disabled = true;
			ot.disabled = false;
			break;
	}
}

function showPerNarr(per) {
	var num_pers = get("num_pers").value;
	var all_p = get("per_0");
	var spn_all = get("spn0");
	if(per == 0) {
		all_p.style.display = '';
		spn_all.style.borderTop = 'medium solid #999999';
		spn_all.style.borderLeft = 'medium solid #999999';
		spn_all.style.borderRight = 'medium solid #999999';		
		spn_all.style.borderBottom = 'thin solid #DDDDDD';
		for (var i=1; i<=num_pers; i++) {
			var el = get("per_"+i);
			var spn = get("spn"+i);
			el.style.display = 'none';
			spn.style.borderTop = '2px solid #CCCCCC';
			spn.style.borderLeft = '2px solid #CCCCCC';
			spn.style.borderRight = '2px solid #CCCCCC';							
			spn.style.borderBottom = 'medium solid #999999';
		}
	} else {
		all_p.style.display = 'none';
		spn_all.style.borderTop = '2px solid #CCCCCC';
		spn_all.style.borderLeft = '2px solid #CCCCCC';
		spn_all.style.borderRight = '2px solid #CCCCCC';						
		spn_all.style.borderBottom = 'medium solid #999999';
		for (var i=1; i<=num_pers; i++) {
			var el = get("per_"+i);
			var spn = get("spn"+i);
			if (i == per) {
				el.style.display = '';
				spn.style.borderTop = 'medium solid #999999';
				spn.style.borderLeft = 'medium solid #999999';
				spn.style.borderRight = 'medium solid #999999';
				spn.style.borderBottom = 'thin solid #DDDDDD';
			} else {
				el.style.display = 'none';
				spn.style.borderTop = '2px solid #CCCCCC';
				spn.style.borderLeft = '2px solid #CCCCCC';
				spn.style.borderRight = '2px solid #CCCCCC';				
				spn.style.borderBottom = 'medium solid #999999';
			}
		}
	}
}

function addCAPMs(per) {
	var pmtemplate = get('obj_0_pm_0_per_0').cloneNode(true);
	if(pmtemplate.style.display == "none") {
		pmtemplate.style.display = '';
	}
	var pmcount = get('per_'+per + '_pm_count');
	var curr = parseInt(pmcount.value);
	var next = curr + 1;
	pmcount.value = next;
	var pmIds = updateCAPmElementIds(pmtemplate, per, next, curr);
	var pm_src_row = document.createElement("tr");
	var pm_src_cell = document.createElement("td");
	var pm_src_ta = document.createElement("textarea");
	pm_src_row.id = 'obj_1_pm_' + next + '_src_rw_' + per;
	pm_src_cell.colSpan = '7';
	pm_src_ta.id = 'obj_1_pm_' + next + '_src_' + per;
	pm_src_ta.name = 'obj_1_pm_' + next + '_src_' + per;
	pm_src_ta.style.height = "5em";
	pm_src_ta.style.width = "58em";
	pm_src_ta.style.display = 'none';
	pm_src_cell.appendChild(pm_src_ta);
	pm_src_row.appendChild(pm_src_cell);
	
	var refChild = get('per_' + per + '_pm_insert_before');
	parentEl = refChild.parentNode;
	parentEl.insertBefore(pmtemplate, refChild);
	parentEl.insertBefore(pm_src_row, refChild);
	return(pm_src_row);
}

function updateCAPmElementIds(n, o, next, curr) {
	var nodeId = n.id;
	var per_num = o; 
	var nodeIdVals = nodeId.split("_");
	var newNodeId = "";
	if(nodeId=='obj_0_pm_0_per_0') {
		nodeID = 'obj_1_pm_'+next+'_per_'+per_num;
		n.id = nodeID;
	}
	if (nodeIdVals.length >= 3) { //obj_0_pm_0
		if (nodeIdVals[3] == 0) {
			nodeIdVals[3] = next;
			nodeIdVals[1] = 1;
			nodeIdVals[5] = per_num;
			newNodeId = nodeIdVals.join("_");
			n.name = newNodeId;
			n.id = newNodeId;
			var pfx = 'obj_1_pm_' + next;
			if (n.id == pfx + '_pms_'+per_num) {
				n.selectedIndex = -1;
				n.onchange = null;
				n.removeAttribute("onchange");
				n.removeAttribute("change");
				var func = function() { var limited = limitsafmotr(n.id); };
				if (window.addEventListener) {
					n.addEventListener("change", func, false);
				} else {
					n.attachEvent("onchange", func);
				}				
			}
			if (n.id == pfx + '_pr_'+per_num) {
				n.value = "";
			}
			if (n.id == pfx + '_sa_'+per_num) {
				n.value = "";
			}
			if (n.id == pfx + '_fm_'+per_num) {
				n.value = "";
			}
			if (n.id == pfx + '_ot_'+per_num) {
				n.value = "";
			}				
			if (n.id == pfx + '_remlink_'+per_num) {
				var func = function() { var el = get(pfx+'_src_rw_'+per_num); el.parentNode.removeChild(el); var pm_el = get(pfx+'_per_'+per_num); pm_el.parentNode.removeChild(pm_el); var count=get('per_'+per_num+'_pm_count'); count.value--; }; 
				if (window.addEventListener) {
					n.addEventListener("click", func, false);
				} else {
					n.attachEvent("onclick", func);
				}
			}
			if (n.id == pfx + '_sh_'+per_num) {
				n.removeAttribute("onclick");
				n.onclick = null;
				var func = function() { var t_area = get(pfx + '_src_' + per_num); var disp_link = get(pfx + '_sh'); if (t_area.style.display == "none") { t_area.style.display = 'inline'; disp_link.innerHTML = "-"; } else { t_area.style.display = 'none'; disp_link.innerHTML = "+"; } }				
				if (window.addEventListener) {
					n.addEventListener("click", func, false);
				} else {
					n.attachEvent("onclick", func);
				}
			}
			
		}
	}
	var children = n.childNodes;
	for (var i=0; i < children.length; i++) {
		if (children[i].nodeType == 1) {
			nodeId += updateCAPmElementIds(children[i], per_num, next, curr);
		}
	}
	return nodeId;
	
}
