var submitChangeCalculator = true;
var changesSubmitted = true;
/**
 * Adds a hidden input field with the given elementName and value to the given form and submits the form
 *
 * param formName the name of the form to add to and to submit
 * param elementName the name of the element to add
 * param value the value of the element to add
 */
function addHiddenAndSubmit(formName, elementName, value) {
	if (document.getElementById) {
		var hiddenElement = document.createElement('INPUT');
		 if (document.all) { // what follows should work with NN6 but doesn't in M14
			 hiddenElement.type = 'hidden';
			 hiddenElement.name = elementName;
			 hiddenElement.value = value;
		 } else if (document.getElementById) { // so here is the NN6 workaround
			 hiddenElement.setAttribute('type', 'hidden');
			 hiddenElement.setAttribute('name', elementName);
			 hiddenElement.setAttribute('value', value);
		 }
		document.getElementById(formName).appendChild(hiddenElement);
	}
	document.getElementById(formName).submit();
}

function setMethod(methodName) {
	if (document.fundReturnForm.method.value == methodName) {
		document.fundReturnForm.method.value = null;
	} else {
		document.fundReturnForm.method.value = methodName;
	}
}

function switchCalculator(searchField,searchFieldInput, combineMonthlyButtonText,combineOnetimeInvestmentButtonText,onetimeInvestmentButtonText,monthlyInvestmentButtonText) {
	if (!submitChangeCalculator) { return; }
		
	var elm = document.getElementById(searchField);
	var periodSelect1 = document.getElementById("periodSelect1");
	var periodSelect2 = document.getElementById("periodSelect2");
	var elmInput = document.getElementById(searchFieldInput);	
	var combineMonthlyAndInitial = document.getElementById('combineMonthlyAndInitial') ;
	var buttonText = document.getElementById("Combine");
	
	var initialStartDay = document.getElementById('oneTimeInvestmentDay');
	var initialStartMonth = document.getElementById('oneTimeInvestmentMonth');
	var initialStartYear = document.getElementById('oneTimeInvestmentYear');
	var finalDay = document.getElementById('finalDay');
	var finalMonth = document.getElementById('finalMonth');
	var finalYear = document.getElementById('finalYear');
	var monthlyStartDay = document.getElementById('monthlyInvestmentStartDay');
	var monthlyStartMonth = document.getElementById('monthlyInvestmentStartMonth');
	var monthlyStartYear = document.getElementById('monthlyInvestmentStartYear');
	var monthlyEndDay = document.getElementById('monthlyInvestmentEndDay');
	var monthlyEndMonth = document.getElementById('monthlyInvestmentEndMonth');
	var monthlyEndYear = document.getElementById('monthlyInvestmentEndYear');
	
	if (elm.style.display == "block") {			
		elm.style.display = "none";
	
		if (searchField == 'monthlyInvestment') {
			buttonText.value = combineMonthlyButtonText;
		} else if (searchField == 'initialInvestment') {
			buttonText.value = combineOnetimeInvestmentButtonText;
		}
		combineMonthlyAndInitial.value = false;
		
		if (elmInput.value > 0) {
			elmInput.value = 0;
		}
		if (changesSubmitted) {
			document.getElementById('Calculate').click();
		} else {
			return false;
		}
	} else {
		changesSubmitted = false;
		elm.style.display = "block";

		if (searchField == 'monthlyInvestment') {
			buttonText.value = onetimeInvestmentButtonText;
			elmInput.value = 1000;
			monthlyStartDay.value = initialStartDay.value;
			monthlyStartMonth.value = initialStartMonth.value;
			monthlyStartYear.value = initialStartYear.value;
			monthlyEndDay.value = finalDay.value;
			monthlyEndMonth.value = finalMonth.value;
			monthlyEndYear.value = finalYear.value;
		} else if (searchField == 'initialInvestment') {
			buttonText.value = monthlyInvestmentButtonText;
			elmInput.value = 100000;
			initialStartDay.value = monthlyStartDay.value;
			initialStartMonth.value = monthlyStartMonth.value;
			initialStartYear.value = monthlyStartYear.value;
			finalDay.value = monthlyEndDay.value;
			finalMonth.value = monthlyEndMonth.value;
			finalYear.value = monthlyEndYear.value;
		}
		combineMonthlyAndInitial.value = true;
		return false;
	}
}

function setDateSearchMethod(newValue) {
	var radioObj = document.forms['fundReturnForm'].elements['dateSearchMethod'];
	var radioLength = radioObj.length;		
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function onKeyPressed(e) {
	var unicode=e.keyCode? e.keyCode : e.charCode
	if(unicode == '13') {
		submitChangeCalculator = false;
		document.getElementById('Calculate').click();
	}
}

