var req;
var divBody;
var cache = new Object();
var globalCntType;
var attDiv;

function sndPost(frm, act, divId, cbFunc) {
	divBody=document.getElementById(divId);

	var params = serializeFrm(frm);
	var async = false;

	if (window.XMLHttpRequest) {
	    req = new XMLHttpRequest();
		async = true;	// needed for Mozilla
	} else if (window.ActiveXObject) {
	    req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("POST", act, async);

	if (cbFunc) {
		req.onreadystatechange = eval(cbFunc);
	}
	else {
		req.onreadystatechange = callback;
	}
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send(params);
}

function callback() {

    if (req.readyState == 4) {
        if (req.status == 200) {
			var rspText = req.responseText;
			var headers = req.getAllResponseHeaders();
			divBody.innerHTML = rspText;
			setCache(divBody.id, globalCntType);
			setTabs(globalCntType);
        }
    }
}

function cbBreakdown() {
    if (req.readyState == 4) {
        if (req.status == 200) {
			var doc = req.responseXML.documentElement;
			var headers = req.getAllResponseHeaders();
			graphXmlBreakdown(doc, "bdContent");
			try {
				document.getElementById("bdDisclaimer").innerHTML = "";
				var disclaimer = doc.getElementsByTagName("disclaimer")[0].firstChild.nodeValue;
				if (disclaimer !=  null) {
					document.getElementById("bdDisclaimer").innerHTML = disclaimer;
				}
			}
			catch(e) {}

			setCache(divBody.id, globalCntType);
			if (globalCntType == "sector") setTabs(globalCntType);

			try {
				if (!hasAttribution) {
					document.getElementById("attrLink").style.display = "none";
				}
			}
			catch (e) {
			}
        }
    }
}

function cbGetBreakdown() {
    if (req.readyState == 4) {
        if (req.status == 200) {
			var rspText = req.responseText;
			var headers = req.getAllResponseHeaders();
			divBody.innerHTML = rspText;
			rtvBreakdown(document.frmBrkdn, 'breakdown');
        }
    }
}

function cbAttrib() {
    if (req.readyState == 4) {
        if (req.status == 200) {
			var rspText = req.responseText;
			var headers = req.getAllResponseHeaders();
			divBody.innerHTML = rspText;
			setCache(divBody.id, globalCntType);
        }
    }
}

function cbSW() {
    if (req.readyState == 4) {
        if (req.status == 200) {
			var rspText = req.responseText;
			var headers = req.getAllResponseHeaders();
			divBody.innerHTML = rspText;
			setCache(divBody.id, globalCntType);
			setTabs(globalCntType);
        }
    }
}

function cbFav() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var rspText = req.responseText;
			var headers = req.getAllResponseHeaders();
			
			if (window.innerHeight) {
				yPos = window.pageYOffset + 150;
			}
			else if (document.documentElement && document.documentElement.scrollTop) {
				yPos = document.documentElement.scrollTop + 150;
			}
			else if (document.body) {
				yPos = document.body.scrollTop + 150;
			}
	
			divBody.style.top = yPos + "px";
			divBody.innerHTML = rspText;
			divBody.style.visibility = "visible";

			// workaround for container div.
			try {
				var newDiv = divBody.offsetParent.removeChild(divBody);
				document.body.appendChild(newDiv);
			}
			catch (e) {
			}
			
		}
	}
}

function cbHoldingsMgmt() {

    if (req.readyState == 4) {
        if (req.status == 200) {
			var rspText = req.responseText;
			var headers = req.getAllResponseHeaders();
			divBody.innerHTML = rspText;
        }
    }
}

function serializeFrm(frm) {
	var str = "";
	
	var iMax = frm.elements.length - 1;
	
	for (var i = 0; i <= iMax ; i++) {
		if ((frm.elements[i].type == "checkbox" || frm.elements[i].type == "radio") && !frm.elements[i].checked) continue;
		str += frm.elements[i].name + "=" + frm.elements[i].value;
		if (i < iMax) str += "&";
	}
	
	return str.replace(/\&$/, "");
}

function rtvCnt(frm, act, divId, cntType) {
	if (!cache[cntType]) {
		globalCntType = cntType;
		sndPost(frm, act, divId);
	}
	else {
		restore(divId, cntType);
		setTabs(cntType);
	}
}

function rtvRankings(frm, act, divId, cntType, rType) {
	if (!cache[cntType]) {
		frm.rankingsType.value = rType;
		globalCntType = cntType;
		sndPost(frm, act, divId);
		setTabs(cntType);
	}
	else {
		restore(divId, cntType);
		setTabs(cntType);
	}
}

function rtvHoldings(frm, divId, cntType) {
	if (!cache[cntType]) {
		frm.hldType.value = cntType;
		globalCntType = cntType;
		sndPost(frm, frm.action, divId);
//		setTabs(cntType);
	}
	else {
		restore(divId, cntType);
	}
}

function rtvHoldings2(frm, divId, cntType) {
	frm.hldType.value = cntType;
	globalCntType = cntType;
	sndPost(frm, frm.action, divId, "cbHoldingsMgmt");
}

function rtvBreakdown(frm, divId, url) {
	if (url) act = url;
	else act = frm.action;

	var cntType = frm.pbType.value;
	globalCntType = cntType;
	sndPost(frm, act, divId, "cbBreakdown");
}

function getBreakdown(frm, divId, url) {
	if (url) act = url;
	else act = frm.action;

	var cntType = frm.pbType.value;
	globalCntType = cntType;
	sndPost(frm, act, divId, "cbGetBreakdown");
}

function rtvAttrib(frm, divId, url) {
	var cntType = "attrib";
	globalCntType = cntType;
	attDiv = divId;
	sndPost(frm, url, divId, "cbAttrib");
}

function rtvAttrib2(frm, divId, url) {
//	alert("frm.selKey.options[frm.selKey.selectedIndex].text=" + frm.selKey.options[frm.selKey.selectedIndex].text);
	var cntType = "attrib";
	globalCntType = cntType;
	attDiv = divId;
	frm.keyString.value = frm.selKey.options[frm.selKey.selectedIndex].text;
	sndPost(frm, url, divId, "cbAttrib");
}

function rtvSW(frm, divId, url) {
	var cntType = "sw";
	globalCntType = cntType;
	sndPost(frm, url, divId, "cbSW");
}

function addFav(frm, divId) {
	if (valFav(frm)) {
		frm.task.value = "addFav";
		frm.key.value = "mfAddFav";
		sndPost(frm, frm.getAttribute("action"), divId, "cbFav");
	}
}

function addFundFinderFav(divId) {
	with (document.fundFinder) {
		task.value = "addFavFund";
		savKey = key.value;
		key.value = "mfAddFav";
	}
	sndPost(document.fundFinder, document.fundFinder.getAttribute("action"), divId, "cbFav");
	document.fundFinder.key.value = savKey;
}

function delFav(frm, divId) {
	if (valFav(frm)) {
		frm.task.value = "deleteFavFund";
		sndPost(frm, frm.action, divId, "cbFav");
	}
}

function valFav(frm) {
	var valid = false;
	var qs = serializeFrm(frm);

	if (qs.search(/selectedFunds/) >= 0) valid = true;
	else alert("Please select funds using the checkboxes to change your Fund Watch list");
	
	return valid;
}

function getPriceHist(frm, currYear, currMonth, incepYear, incepMonth) {
	
	if(frm.selectYear.value >= currYear && frm.selectMonth.value > currMonth){
		alert("Please select a date prior to today.");
		return false;
	}

	if(frm.selectYear.value <= incepYear && frm.selectMonth.value < incepMonth){
		alert("Date selected is before inception date");
		return false;
	}

	sndPost(frm, frm.action, 'hist');
}

function setCache(id, cntType) {
	cache[cntType] = document.getElementById(id).innerHTML;
}

function restore(id, cntType) {
	document.getElementById(id).innerHTML = cache[cntType];
}

function setTabs(cntType) {
	currTab = document.getElementById(cntType + "Tab");

	if (currTab != null) {
		currTable = currTab.parentNode.parentNode.parentNode;
		
		for (c in cache) {
			try {
				tab = document.getElementById(c + "Tab");
				table = tab.parentNode.parentNode.parentNode;
	
				if (table != currTable) continue;	// only set tabs in current container table.
				
				if (c == cntType) tab.className = "head_sm_white sub_tab_sm_up";
				else tab.className = "head_sm sub_tab_sm_dn";
			}
			catch (exception) {
			}
		}
	}

}


