var MinSalary = 100;
var MaxSalary = 99999999;


/**
	Removes any whitespace from the start and end of a String.
*/
String.prototype.trim = function() {
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
};


function popup(url, name, width, height) {
	window.open(url, name, "height=" + height + ",width=" + width + ",resizable=1,scrollbars=1");
}


function popupCalc(url, name, width, height, salaryElementId, contribPctElementId, errorElementId) {
	var salaryElement = document.getElementById(salaryElementId);
	var contribPctElement = document.getElementById(contribPctElementId);
	var contribPct = contribPctElement.options[contribPctElement.selectedIndex].value;
	var errorElement = document.getElementById(errorElementId);
	if (isValidCalcSalary(salaryElement)) {
		errorElement.style.display = "none";
		popup(url + "?Salary=" + salaryElement.value + "&ContribPct=" + contribPct, name, width, height);
	}
	else {
		errorElement.style.display = "block";
	}
}

function isValidCalcSalary(salaryElement) {
	salaryElement.value = salaryElement.value.replace(/\$/g, "").replace(/,/g, "").trim();
	if (salaryElement.value != "" && /^[1-9][0-9]*$/.test(salaryElement.value)) {
		// clamp values if required
		if (salaryElement.value < MinSalary) {
			salaryElement.value = MinSalary;
		}
		else if (salaryElement.value > MaxSalary) {
			salaryElement.value = MaxSalary;
		}
		return true;
	}
	return false;
}


function addEmployeeRegisterEvents(yesButtonId, noButtonId, existingSchemeRowId) {
	var existingSchemeRow = document.getElementById(existingSchemeRowId);
	document.getElementById(yesButtonId).onclick = function() {
		var row = existingSchemeRow;
		setVisibleExistingSchemeIdRow(row, true);
	};
	document.getElementById(noButtonId).onclick = function() {
		var row = existingSchemeRow;
		setVisibleExistingSchemeIdRow(row, false);
	};
}


function setVisibleExistingSchemeIdRow(existingSchemeRow, visible) {
	existingSchemeRow.style.display = visible ? "block" : "none";
}


/**
	This pair of functions "disable" a button so it can't
	be clicked twice within the specified period.
	The button's onclick function and the form's onsubmit
	function are temporarily removed.
	To work in FF the body's onpageshow function must
	call restoreButton() manually (so the button is restored
	when the back button is clicked).
*/
function disableButton(buttonId, formId, waitText, disabledCssClass, enableDelay) {	
	/*var*/ button = document.getElementById(buttonId);
	/*var*/ oldValue = button.value;
	button.value = waitText;
	/*var*/ oldClickFunc = button.onclick;
	button.onclick = function() {};
	/*var*/ form = document.getElementById(formId);
	/*var*/ oldCssClass = button.className;
	button.className = disabledCssClass;
	/*var*/ oldSubmitFunc = form.onsubmit;
	setTimeout(function() {
		form.onsubmit = function() { return false; };
	}, 0);
	setTimeout(restoreButton, enableDelay);
}
function restoreButton() {
	if (button) {
		button.value = oldValue;
		button.onclick = oldClickFunc;
		form.onsubmit = oldSubmitFunc;
		button.className = oldCssClass;
	}
}

