var rootURL = "/";
var browserName=navigator.appName;
var browserAgent=navigator.userAgent.toLowerCase();
var browserVersion=navigator.appVersion;

//BEGIN ADD OF STYLESHEET FIXERS
if (browserAgent.indexOf('Safari')>0) {
	document.write("<link rel=stylesheet type=text/css href=" + rootURL + "css/saf.css>");
}
if (browserName=="Netscape" || browserName=="Opera") {
	document.write("<link rel=stylesheet type=text/css href=" + rootURL + "css/ff.css>");
}
//END ADD OF STYLESHEET FIXERS

$(document).ready(function(){

	/*FIXES ALL PAGE HEADERS AND NAVS AND FORM LIST ITEMS TO HAVE A BREAK AFTER*/
	$("input:checkbox").addClass("nob")
	$("input:radio").addClass("nob")

  /*FIX ALL HR'S TO HAVE THE PROPER ATTRIBUTES*/
  $("hr").attr("noshade","true");
  $("hr").attr("size","1");
  $("hr").attr("color","#e8e8e8");

  /*CREATE TABS FOR HOMEPAGE AND FUNDPAGE*/
  $('#wrapper_Tabs_A > ul').tabs();
  $('#wrapper_Tabs_B > ul').tabs();

  /*STYLE LABEL CELLS FOR FUND TABLES*/
  $("table#fundfacts td:even").addClass("label")
  $("table#fundfeatures td:even").addClass("label")

  /*MATCH HEIGHTS OF INVESTOR AND ROI BOXES ON HOMEPAGE*/
  if (parseInt($("#wrapper_InvestorSuggest").height()) > parseInt($("#wrapper_Calculator").height())) {
  	$("#wrapper_Calculator").height($("#wrapper_InvestorSuggest").height());
  } else {
  	$("#wrapper_InvestorSuggest").height($("#wrapper_Calculator").height());
  }

  /*IF IE THEN PUSH ROI AND INVESTOR BOXES DOWN*/
  if (browserName!="Netscape" && browserName!="Opera") {
  	//$("#wrapper_MoreTools").css("padding-top","24px")
  }

  /*SETS CURRENT NAVS FOR SUBMENU ITEMS SINCE THEY ARE IN DROP FILES AND CANNOT USE VBSCRIPT*/
  setCurrentSubNav();

  /*SET HIGHLIGHTING COLORS ON TABLE LISTINGS*/
  //$(".tbl_FundListing tr:even").addClass("even")
  //$(".tbl_FundListing tr:totalrow").addClass("totalrow")
  $(".tbl_NavHistoryListing tr:even").addClass("even")
  $(".tbl_DistributionListing tr:even").addClass("even")
  $(".tbl_FullListing tr:even").addClass("even")
  $(".tbl_FullListing tr td:even").addClass("label")
  $(".tbl_ManagerListing tr:even").addClass("even")

	/*CONTACT US FORM CLICKS AND VALIDATION*/
	$("form#contactus").validate({
		rules:{
			comments:"required"
		},
		errorPlacement: function(error, element) {
			if (element.attr("class").indexOf("radio")>-1) {
				error.insertBefore(element);
			}
		}
	});

	if($("input#advisorradio").attr("checked")) {
		$(".advisor_yes").show();
	}

	$("input#advisorradio").click(function(){
		$(".advisor_yes").show();
	});

	$("input#privateradio").click(function(){
		$(".advisor_yes").hide();
	});

});

function printPage() {
	window.print();
}

function clearField(obj,val) {
	if((obj).value==val) {
		(obj).value='';
	} else if((obj).value=='') {
		(obj).value=val;
	}
}

function navsRedir(obj) {
	document.location.href = ($(obj).val());
}

function extLink() {
	alert('You are now leaving the Brompton website.  Please be aware Brompton cannot attest the accuracy of information provided by websites accessed via this link.')
}

function setCurrentSubNav() {
	var inFunds = document.location.href.indexOf("/funds/")
  if (inFunds && document.location.href.indexOf("/overview/")>0) {
  	$("#subNavOverview").addClass("on")
  	$("#subNavReorgOverview").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/fund_features/")>0) {
  	$("#subNavFundFeatures").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/distribution/")>0) {
  	$("#subNavDistributions").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/nav_history/")>0) {
  	$("#subNavNavHistory").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/portfolio/")>0) {
  	$("#subNavPortfolio").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/performance/")>0) {
  	$("#subNavPerformance").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/press_releases/")>0) {
  	$("#subNavNewsReleases").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/tax_information/")>0) {
  	$("#subNavTaxInfo").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/financial_reporting/")>0) {
  	$("#subNavFinancialReporting").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/manager/")>0) {
  	$("#subNavManager").addClass("on");
		$("#subNavAdministrator").addClass("on");
  }
  if (inFunds && document.location.href.indexOf("/portfolio_manager/")>0) {
  	$("#subNavPortfolioManager").addClass("on")
  	$("#subNavOptionsAdvisor").addClass("on")
  	$("#subNavHighstreet").addClass("on")
		$("#subNavInvestManager").addClass("on");
  }
  if (inFunds && document.location.href.indexOf("/prospectus/")>0) {
  	$("#subNavProspectus").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/corporate_governance/")>0) {
  	$("#subNavCorporateGovernance").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/investment_objectives/")>0) {
  	$("#subNavInvestmentObjectives").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/investment_guidelines/")>0) {
  	$("#subNavInvestmentGuidelines").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/tax-deferred/")>0) {
  	$("#subNavTaxDeferred").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/drip_program/")>0) {
  	$("#subNavDripProgram").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/preferred_securities/")>0) {
  	$("#subNavPreferredSecurities").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/recent_news/")>0) {
  	$("#subNavRecentNews").addClass("on")
  }
  if (inFunds && document.location.href.indexOf("/portfolio_mgmt_team/")>0) {
  	$("#subNavPortfolioManagementTeam").addClass("on")
  }

  if (inFunds && readCookie('advisor')) {
  	$("#subNavInvestmentAdvisors").addClass("on")
  }
  if (inFunds && !readCookie('advisor')) {
  	$("#subNavInvestmentAdvisors").addClass("off")
  }
}

function openFund(obj) {
	if (obj.selectedIndex != 0) {
		document.location.href=obj.options[obj.selectedIndex].value;
	}
}

function redirect_user(url)
{
	window.location = "/roi/" + url
}

function RefreshForm(mode)
{
  var form = window.document.frmROIcalc;
  var thisPage = String(window.location);
  var page = "default.asp";

  if (thisPage.charAt(thisPage.length - 1) == "/")
      //thisPage = thisPage + "default.asp";
		thisPage += page;

  //remove querystring parameters from general form submission
  if (thisPage.indexOf("?") > 0)
      thisPage = thisPage.substr(0, thisPage.indexOf("?"));

  switch (mode)
  {
    case "FUND":
        //resubmit to the containing page
        form.action = thisPage;
        form.submit();
        //break;
    case "IPO":
	   if (document.getElementById("frmROIcalc").elements["ipo"].checked)
	   {
			document.getElementById("price").disabled=true
			document.getElementById("dd").disabled=true
			document.getElementById("mm").disabled=true
			document.getElementById("yyyy").disabled=true

			document.getElementById("frmROIcalc").elements["price"].value = document.getElementById("frmROIcalc").elements["price_temp"].value;
	      document.getElementById("frmROIcalc").elements["dd"].selectedIndex = document.getElementById("frmROIcalc").elements["dd_temp"].value;
			document.getElementById("frmROIcalc").elements["mm"].selectedIndex = document.getElementById("frmROIcalc").elements["mm_temp"].value;
			document.getElementById("frmROIcalc").elements["yyyy"].selectedIndex = document.getElementById("frmROIcalc").elements["yyyy_temp"].value;

	   	document.getElementById("frmROIcalc").elements["price_temp"].value = document.getElementById("frmROIcalc").elements["priceipo"].value;
	      document.getElementById("frmROIcalc").elements["dd_temp"].value = document.getElementById("frmROIcalc").elements["ddipo"].selectedIndex;
			document.getElementById("frmROIcalc").elements["mm_temp"].value = document.getElementById("frmROIcalc").elements["mmipo"].selectedIndex;
			document.getElementById("frmROIcalc").elements["yyyy_temp"].value = document.getElementById("frmROIcalc").elements["yyyyipo"].selectedIndex;

	   }
	   else
	   {
	   	document.getElementById("frmROIcalc").elements["price_temp"].value = document.getElementById("frmROIcalc").elements["price"].value;
	      document.getElementById("frmROIcalc").elements["dd_temp"].value = document.getElementById("frmROIcalc").elements["dd"].selectedIndex;
			document.getElementById("frmROIcalc").elements["mm_temp"].value = document.getElementById("frmROIcalc").elements["mm"].selectedIndex;
			document.getElementById("frmROIcalc").elements["yyyy_temp"].value = document.getElementById("frmROIcalc").elements["yyyy"].selectedIndex;

	      document.getElementById("price").disabled=false
	      document.getElementById("dd").disabled=false
			document.getElementById("mm").disabled=false
			document.getElementById("yyyy").disabled=false

	      //reset the values for these items on the form
	      document.getElementById("frmROIcalc").elements["price"].value = "";
	      document.getElementById("frmROIcalc").elements["dd"].value = "";
			document.getElementById("frmROIcalc").elements["mm"].value = "";
			document.getElementById("frmROIcalc").elements["yyyy"].value = "";
      	break;
    	}
  }
}

function getPrint() {
	document.getElementById("getPrint").innerHTML = window.opener.document.getElementById("emailDiv").innerHTML;
}

function checkForm() {
	if (document.getElementById('fromName').value=='' || document.getElementById('toName').value=='')
	{
		alert('Please fill in all fields')
	} else {
		if (checkEmail(document.getElementById('toEmail'))) {
			setEmail('emailDiv')
		} else {
			alert('Please enter a properly formatted email address')
		}
	}
}

function checkEmail(obj) {
//CHECK EMAIL ADDRESS VALIDITY
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.value)){
		return (true)
	}
	return (false)
}

function setEmail(contentDiv) {
	document.getElementById("contentEmail").value=document.getElementById(contentDiv).innerHTML;
	//alert(document.getElementById("contentEmail").value);
	document.emailFriend.submit();
}

function toggleEmailRoi() {
	$("#emailFields").toggle();
}

function toggleArchive(year) {
	$("#tbl" + year).toggle();
}

function toggleLinks(obj) {
	$("#" + obj).toggle();
}

function openWindow2(arg1){
  window.open (arg1,"edit","status=no,toolbars=no,menubar=no,location=no,directories=no,scrollbars=yes,width=650,height=600,resizable=yes")
}

function openWindow(arg1){
  window.open (arg1,"edit","status=no,toolbars=no,scrollbars=yes,width=450,height=500,resize=no")
}

function popup(argPage)
{
  hWnd = window.open(argPage, "manager", "status=no,toolbars=no,scrollbars=yes,width=450,height=500,resizable=no");
  hWnd.moveTo(160,120);
  hWnd.focus();
}

function closeWindow() {
 window.close(self)
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function validateForm(){
	if (document.tempform.Agree.checked) {
  	var formType = get_radio_value();
  	if (formType == 'Investor') {
  		if(document.tempform.Name.value.length == 0) {
  			alert("Please enter your Name.");
      	document.tempform.Name.focus();
      	return false;
  		} else {
  			return true;
  		}
  	}
  	else if (formType == 'InvestmentAdvisor') {
  		if(document.tempform.AdvisorName.value.length == 0 || document.tempform.Firm.value.length == 0) {
  			alert("Please enter your Name and/or Firm.");
      	document.tempform.Name.focus();
      	return false;
  		} else {
  			return true;
  		}
  	}
  	else {
  		alert("Please choose a type.");
  		return false;
  	}
  }
  else {
  	alert("Please agree to the disclaimer.");
  	return false;
  }
}

function toggleType(obj) {
	document.getElementById('InvestorDiv').style.display='none';
	document.getElementById('InvestmentAdvisorDiv').style.display='none';
	document.getElementById(obj.value + 'Div').style.display='block';
}

function get_radio_value()
{
	for (var i=0; i < document.tempform.InvestType.length; i++)
   {
   if (document.tempform.InvestType[i].checked)
      {
      var rad_val = document.tempform.InvestType[i].value;
      return rad_val
      }
   }
}

function jumpTo() {
	location=document.jump.menu.options[document.jump.menu.selectedIndex].value;
}