var userAgent = navigator.userAgent.toLowerCase(); 
var userApp = navigator.appName.toLowerCase();

function getNavValues(){
	//document.navValuesChoose.FUND_CLASS_CODE.value = document.navValuesChoose.selShareClass.options[document.navValuesChoose.selShareClass.selectedIndex].value;
	document.navValuesChoose.FUND_CLASS_CODE.value = "";
}

var aryShareClass = new Array("A","B","C","R");
var aryShareDate = new Array("10/01/2001","10/01/2001","10/01/2001","8/31/2005");
var arySharePerfDate = new Array("01/01/2002","01/02/2004","01/02/2004","01/1/2006");

function fncValidateDate(strDateForm,strNotForm){
	var badDate;
	var strShareClass;
	var strShareDate;
	var aryStrShareDate = new Array();
	var strMonth;
	var strDay;
	var strYear;
	
	if(strDateForm == document.navValuesChoose) // Daily Performance
		strShareClass = 'A';	
	else // NAV
		strShareClass = strDateForm.selShareClass.options[strDateForm.selShareClass.selectedIndex].value;	

	strMonth = strDateForm.sltMonth.options[strDateForm.sltMonth.selectedIndex].value;
	strDay = strDateForm.sltDay.options[strDateForm.sltDay.selectedIndex].value;
	strYear = strDateForm.sltYear.options[strDateForm.sltYear.selectedIndex].value;
	strDateForm.FUND_VALUE_EFFECTIVE_DATE.value = strDateForm.sltMonth.options[strDateForm.sltMonth.selectedIndex].value + "/" + strDateForm.sltDay.options[strDateForm.sltDay.selectedIndex].text + "/" + strDateForm.sltYear.options[strDateForm.sltYear.selectedIndex].text;
	
	for(var i = 0; i < aryShareClass.length; i++){
		if(aryShareClass[i] == strShareClass){
			if(strDateForm == document.navValuesChoose){ // Daily Performance
				strShareDate = aryShareDate[i];
				aryStrShareDate = strShareDate.split("/");
			}else{
				strShareDate = arySharePerfDate[i];
				aryStrShareDate = strShareDate.split("/");
			}
			break;
		}
	}
	
	aryStrShareDate[0] = parseInt(aryStrShareDate[0]);
		
	if (strYear < aryStrShareDate[2]){
		badDate = true;				
	}else if (strYear == aryStrShareDate[2]) {		 
		if (strMonth < aryStrShareDate[0]){
			badDate = true;				
		}else if (strMonth == aryStrShareDate[0]) {
			if (strDay < aryStrShareDate[1]){
				badDate = true;										 
			}					
		}
	}
	
	if(badDate == true){
		if(strNotForm != "noform"){
			alert("Performance not available until " + strShareDate + ".");
		}
		return false;
	}else{
		// is date in the future
		var today = new Date(); 
		var todayMonth = today.getMonth()+1;	
		var todayDate = today.getDate();	
		var todayYear = today.getFullYear();
		
		if (strYear == todayYear) {		
			if (strMonth >= todayMonth) {				
				if (strMonth > todayMonth) {			
					badDate = true;			
				} else {  
					if (strDay > todayDate)
						badDate = true;
				}
			}
			
			if (badDate == true) {
				if(strNotForm != "noform"){
					alert("Please enter a valid date.");
				}
				return false;		
			}else{
				return true;
			}
		}
	}		
}

function validateNav(radioObject){
	//if(document.navValuesChoose.FUND_CLASS_CODE.value == ""){
	//	alert("Please select a share class to view Daily Prices.");
	//}else{
		
		if(fncValidateDate(document.navValuesChoose) != false){
			//document.navValuesChoose.submit();
			
			var servlet = "/--/performance/mutual_fund_nav.jsp?";
			var strFundClassCode = "FUND_CLASS_CODE=";
			var strTask = "&TASK=GET_DAILY_MFUND_NAV";
			var strEffectiveDate = "&FUND_VALUE_EFFECTIVE_DATE=" + document.navValuesChoose.FUND_VALUE_EFFECTIVE_DATE.value;
			var strEffectiveMonth = "&sltMonth=" + document.navValuesChoose.sltMonth.options[document.navValuesChoose.sltMonth.selectedIndex].value;
			var strEffectiveDay = "&sltDay=" + document.navValuesChoose.sltDay.options[document.navValuesChoose.sltDay.selectedIndex].value;
			var strEffectiveYear = "&sltYear=" + document.navValuesChoose.sltYear.options[document.navValuesChoose.sltYear.selectedIndex].value;
			
			//fncLoadPerformanceUpdate("IFRM_NAV");
			document.getElementById("IFRM_NAV").height = "900px";
			document.getElementById("IFRM_NAV").src = servlet + strFundClassCode + strTask + strEffectiveDate + strEffectiveMonth + strEffectiveDay + strEffectiveYear;
			document.getElementById("IFRM_NAV").style.display = "block";
			
		}
	//}
}

// begin CONTRACT PERFORMANCE
function getContractPerf(){
	document.contractPerfChoose.FUND_CLASS_CODE.value = document.contractPerfChoose.selShareClass.options[document.contractPerfChoose.selShareClass.selectedIndex].value;
	//document.contractPerfChoose.FUND_CLASS_CODE.value = "";
}

function validateContractPerf(radioObject){

//alert(validateContractPerf());

	if(document.contractPerfChoose.FUND_CLASS_CODE.value == ""){
		alert("Please select a share class to view Performance.");
		return false;
	}else{
		if(fncValidateDate(document.contractPerfChoose) != false){
			return true;
		}
	}
}

function submitConPerf() {
	if(validateContractPerf()){		
			var frm = document.contractPerfChoose;
			var	oSelectVal = "perfShareClass=" + frm.selShareClass.options[frm.selShareClass.selectedIndex].value + "&";
			//var	oSelectVal = "perfShareClass=" + "" + "&";
			var task = "TASK=GET_MFUND_PERFORMANCE&";
			//var classCode = "FUND_CLASS_CODE=" + frm.FUND_CLASS_CODE.value + "&";
			var classCode = "FUND_CLASS_CODE=" + frm.selShareClass.options[frm.selShareClass.selectedIndex].value + "&";
			var reportPer = "REPORTING_PERIOD=0&";
			var effectiveDate = "FUND_VALUE_EFFECTIVE_DATE=" + frm.FUND_VALUE_EFFECTIVE_DATE.value + "&";						
			if (frm.selShareClass.options[frm.selShareClass.selectedIndex].text.indexOf('529') ==  -1) var is529 = "IS_529=false&";
			else var is529 = "IS_529=true&";			
			var servlet = "/--/performance/mutual_fund_performance.jsp?";
			
			var getString = servlet + oSelectVal + task + classCode + reportPer + effectiveDate + is529 + fncSetTimeStamp(); 
			//document.location.href = getString;
			//var winFeatures = "menubar=yes,resizable=yes,scrollbars=yes";								
			//nonStandard = window.open("/loading.html", "nonStandard", winFeatures);
			//nonStandard.focus();									
			
			//if(nonStandard != null){
			//	nonStandard.location.href = getString;	
			//}
			
			//fncLoadPerformanceUpdate("IFRM_DAILY");
			document.getElementById("IFRM_DAILY").height = "2375px";
			document.getElementById("IFRM_DAILY").src = getString;
			document.getElementById("IFRM_DAILY").style.display = "block";
			
	}
}	
// end CONTRACT PERFORMANCE

function fncSetTimeStamp(){
	var today = new Date();
	var timeStampMonth = today.getMonth();
	var timeStampDay = today.getDate();
	var timeStampHour = today.getHours();
	var timeStampMinute = today.getMinutes();
	var timeStampSecond = today.getSeconds();
	var strDateTimeStamp = timeStampMonth.toString() + timeStampDay.toString() + timeStampHour.toString() + timeStampMinute.toString() + timeStampSecond.toString();
	//alert(strDateTimeStamp);
	return strDateTimeStamp;
}

function strSplit(str, delimiter) {
	var arySplit = "";
	arySplit = str.split(delimiter);
	return arySplit;
}

//  date form objects
var frm;
function fncSetFrmVar(currObj) {
	frm = document.navValuesChoose;
	if (currObj.form){
		if(currObj.form.name == "contractPerfChoose"){
			frm = document.contractPerfChoose;
		}else{
			frm = document.navValuesChoose;
		}
	}
}

function fncSetDefaultDate(test){
	fncSetFrmVar(test);
	var today = new Date();
	
	frm.sltMonth.selectedIndex = today.getMonth();
	frm.sltDay.selectedIndex = today.getDate()-1;
	fncSetDefaultYears();	
	fncSetDays(frm.sltMonth);

}

function fncSetDefaultYears() {
	var i=0; 	
	var today = new Date(); 
	var xyear = today.getFullYear();	
	var minYear = 2001;
	if(frm == document.contractPerfChoose){
		minYear = 2002;
	}
	
	while(xyear >= minYear) {				
		if(xyear == today.getFullYear())	
			frm.sltYear.options[i] = new Option(xyear, xyear, true, true);
		else 						
			frm.sltYear.options[i] = new Option(xyear, xyear);		
		
		i++;	
		xyear--;		
	}		
}

function fncSetDays(objMon){
	var i, x, y, z, days=27;
	var ary30DayMon = new Array(2, 4, 6, 9, 11);
	var selectedMonth = parseInt(objMon.options[objMon.options.selectedIndex].value);
	
	fncSetFrmVar(objMon);
	
	for(i=0; i<ary30DayMon.length; i++){
		if (selectedMonth == ary30DayMon[i]) {			
			if (selectedMonth == 02) { // additional clean up for feb.
				if (fncIsLeapYear())
					days = 28;				
				for(x=frm.sltDay.length-1; x>days; x--) { 
					frm.sltDay.options[x] = null;				
				}
				
				if(fncIsLeapYear() && !frm.sltDay.options[28]) // if 29th removed and leap year add 29th							
					frm.sltDay.options[28] = new Option(29, 29);
			}
			
			if ((!frm.sltDay.options[30]) && (selectedMonth != 02)) {
				for(z=27; z<=29; z++) { // add days back to months that have less then 31 days except feb.
					if (!frm.sltDay.options[z])
						frm.sltDay.options[z] = new Option(z+1, z+1);
				}
			}
			 			
			frm.sltDay.options[30] = null;			
			break;
			
		} else if ((!frm.sltDay.options[30]) && (selectedMonth != ary30DayMon[i])) {
			for(y=27; y<=30; y++) { // add days back to months that have 31 days
				if (!frm.sltDay.options[y])
					frm.sltDay.options[y] = new Option(y+1, y+1);
			}
		}	
	}	
}

function fncIsLeapYear() {
	var z=1980;
	var today = new Date();

	while (z <= today.getFullYear()) {
		if (z == frm.sltYear.options[frm.sltYear.selectedIndex].value)
			return true;
		z += 4;		
	}
	return false;	
}

function fncSetYearInception(objProduct) {
	var i, y, aryIncepDate;

	fncSetFrmVar(objProduct);
	fncSetDefaultYears(); // reset all years first	
	
	for(i=0; i<aryProducts.length; i++) {
		if (objProduct.options[objProduct.selectedIndex].text == aryProducts[i][0]) {
			aryIncepDate = strSplit(aryProducts[i][1], "/");			

			for(y=frm.sltYear.length-1; y>0; y--) {	// remove years prevoius to product inception 				
				if (frm.sltYear.options[y].value < aryIncepDate[2]) {
					frm.sltYear.options[y] = null;								
				} 	
			}	
		} 
	}		
}

//Investment Drop Down
function fncInvestmentDropDown(){
	var frm = document.frmFundInfo;
	var selectedShare = frm.selGetShare.value;
	var aryInvestments = new Array();
	
	if (selectedShare != "") { 
		switch (selectedShare) {
			case "A" :
				aryInvestments = aryA; break;
		   	case "B" :
				aryInvestments = aryB; break;
		   	case "C" :
				aryInvestments = aryC; break;
		   	case "R" :
				aryInvestments = aryR; break;												
		} 		
		clearFundOptions(frm); // clear funds 
		for(var i = 0; i < aryInvestments.length; i++){ // reset funds
			var nmbr = i + 1;			
			frm.selGetInvestment.options[nmbr] = new Option(aryInvestments[i][1]);
			frm.selGetInvestment.options[nmbr].value = aryInvestments[i][0];
		}		
	} else 		
		clearFundOptions(frm);
}

function clearFundOptions(frm) {
	if (frm.selGetInvestment.options.length > 0) { 
		for(y = frm.selGetInvestment.options.length-1; y > 0; y--) { // remove funds from select obj 
			if (frm.selGetInvestment.options[y].index != 0)  
				frm.selGetInvestment.options[y] = null;		
		}	
	}
}

function checkInvestmentDates(incDate, termDate, selDate) {
	var inception = compareDates(incDate, selDate);				
	var termination =  compareDates(termDate, selDate);				

	if ((inception == 0 || inception == -1) && (termDate == "" || temination == 1))
		return true;
	else
		return false;
}

// returns: 0 dates are same, -1 date1 is earlier, 1 date1 is later 
function compareDates(date1, date2) {	
	// date1
	var date1Array = strSplit(date1, "/"); 
	var month1 = date1Array[0];
	var date1  = date1Array[1];
	var year1  = date1Array[2];	
	// date2
	var date2Array = strSplit(date2, "/"); 
	var month2 = date2Array[0];
	var date2  = date2Array[1];
	var year2  = date2Array[2];	
	
	if (year1 > year2) return 1;
	else if (year1 < year2) return -1;
	else if (month1 > month2) return 1;
	else if (month1 < month2) return -1;
	else if (date1 > date2) return 1;
	else if (date1 < date2) return -1;
	else return 0;
}

function evalFundList(objFundSelect) {
	if (objFundSelect.length == 1)
		alert("Please select Share Class prior to Fund.");
}

function fncIntialInvestment(){
	for(var x = 0; x < document.frmFundInfo.selGetInvestment.options.length; x++){
		if(document.frmFundInfo.selGetInvestment.options[x].value == sFund){
			document.frmFundInfo.selGetInvestment.selectedIndex = x;
		}
	}
}
function cleanUpShareDrop() {
	if (aryR.length < 1) document.frmFundInfo.selGetShare.options[4] = null; 	
	if (aryC.length < 1) document.frmFundInfo.selGetShare.options[3] = null; 
	if (aryB.length < 1) document.frmFundInfo.selGetShare.options[2] = null; 		
}

//Change Share Class for page load
function fncInitialClass(){
	cleanUpShareDrop();
	for(var x = 0; x < document.frmFundInfo.selGetShare.options.length; x++){
		if(document.frmFundInfo.selGetShare.options[x].value == strCurrentClass){
			document.frmFundInfo.selGetShare.selectedIndex = x;
		}
	}
	fncInvestmentDropDown();
}

//Handle Layers
var locationHref = window.location.href;	
locationHref = locationHref.toLowerCase();

/* when a specific layer is called in the url, 
	this will open that layer instead of the default first layer */
function fncLoadThisDiv(){
	if (locationHref.indexOf("divid") > 0) {
		var param = locationHref.indexOf("divid=");
		param = param + 6;
		var paramDivID = locationHref.slice(param);
		paramDivID = paramDivID.toUpperCase();
		if (paramDivID > "") {
			for (var n = 0; n < aryInvestmentLayers.length; n++) {
				if(paramDivID == aryInvestmentLayers[n]){
					fncGetFundInformation(aryInvestmentLinks[n]);
					break;
				}else if(paramDivID == aryInvestmentLinks[n]){
					fncGetFundInformation(aryInvestmentLinks[n]);
					break;
				}
			}
		}
	}
}

function fncGetParamValue(strUrl,strParam){
	var aryCurrentParams = strUrl.split("?");
	aryCurrentParams = aryCurrentParams[1].split("&");
	for(var i = 0; i < aryCurrentParams.length; i++){
		if(aryCurrentParams[i].indexOf(strParam) != -1){
			var arySelectedParam = aryCurrentParams[i].split("=");
			return arySelectedParam[1];
			break;
		}
	}
}

function fncBuildNavUrl(strFundView){
	var strInvestmentUrl = "/--/performance/mutual_fund_nav.jsp?FUND_CLASS_CODE=&TASK=GET_DAILY_MFUND_NAV";
	var strCurrentDate = fncGetParamValue(document.location.href,"FUND_VALUE_EFFECTIVE_DATE");
	var strEffectiveDate = "&FUND_VALUE_EFFECTIVE_DATE=" + strCurrentDate;
	var aryCurrentDate = strCurrentDate.split("/");
	var strMonth = "&sltMonth=" + aryCurrentDate[0];
	var strDay = "&sltDay=" + aryCurrentDate[1];
	var strYear = "&sltYear=" + aryCurrentDate[2];
	
	return strInvestmentUrl + strEffectiveDate + strMonth + strDay + strYear;
	
}

var aryInvestmentLayers = new Array("DIV_FUND_CALENDAR","DIV_DAILY_PERFORMANCE","DIV_VALUE_LIST");
var aryInvestmentTitles = new Array("Fund Calendar","Daily Performance","Values List");
var aryInvestmentLinks = new Array("TD_FUND_CALENDAR","TD_DAILY_PERFORMANCE","TD_VALUE_LIST");

function fncChangeNavFrameHeight(strFundView){
	if(strFundView == "TD_VALUE_LIST"){
		parent.document.getElementById("IFRM_NAV").height = "770px";
	}else{
		parent.document.getElementById("IFRM_NAV").height = "550px";
	}
}

function fncGetFundInformation(strFundView){
	if((document.frmFundInfo.selGetShare.options[document.frmFundInfo.selGetShare.selectedIndex].value > "" && document.frmFundInfo.selGetInvestment.options[document.frmFundInfo.selGetInvestment.selectedIndex].value > "") || strFundView == "TD_VALUE_LIST"){
		if(strPageUrl.indexOf("mutual_fund_nav") == -1 && strFundView == "TD_VALUE_LIST"){	//click on list option while not on nav page
			document.location.href = fncBuildNavUrl(strFundView);
			fncChangeNavFrameHeight(strFundView);
		}else{
			for(var i = 0; i < aryInvestmentLinks.length; i++){
				if(aryInvestmentLinks[i] == strFundView){		//selected fund view
					document.getElementById(aryInvestmentLinks[i]).style.textDecoration = "none";
					document.getElementById(aryInvestmentLinks[i]).style.color = "#000000";
					document.getElementById(aryInvestmentLinks[i]).style.fontWeight = "bold";
					
					if(strPageUrl.indexOf("mutual_fund_nav") == -1){	//not on nav page
						document.getElementById(aryInvestmentLayers[i]).style.display = "block";
						fncDisplayDisclaimer(i);
					}else{	//on nav page
						if(strFundView != "TD_VALUE_LIST"){
							//for MFUND_VALUE_MFUND_TA_NO, change first digit based on class, function is in mf_calendar.js
							var strDate = fncGetParamValue(document.location.href,"FUND_VALUE_EFFECTIVE_DATE=");
							strDate = strDate.split("/");
							strDate[1] = "01";
							strDate = strDate[0] + "/" + strDate[1] + "/" + strDate[2];
							var strFundUrl = "/--/performance/nav_month_frame.jsp?" + 
								"FUND_CLASS_CODE=" + document.frmFundInfo.selGetShare.options[document.frmFundInfo.selGetShare.selectedIndex].value + 
								"&FUND_VALUE_EFFECTIVE_DATE=" + strDate + 
								"&MFUND_VALUE_MFUND_TA_NO=" + fncChangeFundCode(document.frmFundInfo.selGetShare.options[document.frmFundInfo.selGetShare.selectedIndex].value,document.frmFundInfo.selGetInvestment.options[document.frmFundInfo.selGetInvestment.selectedIndex].value) + 
								"&tab_name=calendar" + 
								"&MFUND_NAV_PAGE=1" +
								"&divid=" + strFundView;
							parent.document.getElementById("IFRM_NAV").src = strFundUrl;
							//document.location.href = strFundUrl;
							fncChangeNavFrameHeight(strFundView);
						}
					}
				}else{
					document.getElementById(aryInvestmentLinks[i]).style.textDecoration = "underline";
					document.getElementById(aryInvestmentLinks[i]).style.color = "#3399CC";
					document.getElementById(aryInvestmentLinks[i]).style.fontWeight = "bold";
					if(strFundView != "new"){
						if(strPageUrl.indexOf("mutual_fund_nav") == -1){
							document.getElementById(aryInvestmentLayers[i]).style.display = "none"
						}
					}
				}
			}
		}
	}else{
		alert("Select a Share Class and Fund.");
	}
}


/* ----- dynamic iframe height ------- */
/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/
function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
  }
  return docHt;
}

function setIframeHeight(iframeName) {
  var iframeWin = window.frames[iframeName];
  var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
  if ( iframeEl && iframeWin ) {  
    iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous  
    var docHt = getDocHeight(iframeWin.document);
    // need to add to height to be sure it will all show
	//alert(docHt);
    if (docHt) iframeEl.style.height = docHt + 30 + "px";
  }
}