function isValidInteger(text) {
	// trim
	text = text.replace(/^\s+/,'').replace(/\s+$/,'');
	text = text.replace(',','').replace(' ','');
	
	if (text.length > 0) {
		var i = parseInt(text);
		
		if (!isNaN(i)) {
			var s = i + '';
			if (s.length == text.length)
				if (i >= 0)
					return true;
				else
					// Negative integer
					return false;
			else
				// Not an integer
				return false;
		}
		else
			// Not numeric
			return false;
	}
	
	// Empty
	return true;
}

function getIntValue(text) {
	if (text.length > 0 && isValidInteger(text)) {
		return parseInt(text);
	}
	else {
		return -1;
	}
}

function isValidFloat(text) {
	// trim
	text = text.replace(/^\s+/,'').replace(/\s+$/,'');
	text = text.replace(',','').replace(' ','');

	if (text.length > 0) {
		var f = parseFloat(text);
		
		if (!isNaN(f)) {
			var s = f + '';
			var textTrimmed = text;
			if (text.indexOf('.') >= 0) {
				while (textTrimmed.substring(textTrimmed.length - 1, textTrimmed.length) == "0") {
					textTrimmed = textTrimmed.substring(0, textTrimmed.length - 1);
				}
			}
			
			if (textTrimmed.substring(textTrimmed.length - 1, textTrimmed.length) == ".") {
				// Remove last .
				textTrimmed = textTrimmed.substring(0, textTrimmed.length - 1)
			}

			if (s.length == textTrimmed.length)
				if (f >= 0)
					return true;
				else
					// Negative integer
					return false;
			else
				// Not an integer
				return false;
		}
		else
			// Not numeric
			return false;
	}
}

function getFloatValue(text) {
	if (text.length > 0 && isValidFloat(text)) {
		return parseFloat(text);
	}
	else {
		return -1;
	}
}

function validateIntegerTextBox(textbox) {
	if (!isValidInteger(textbox.value)) {
		alert("Please enter a valid whole number.");
		textbox.value = '';
		return false;
	}
	
	return true;
}

function validateIntegerTextBoxRange(textbox, minValue, maxValue) {
	if (!isValidInteger(textbox.value)) {
		alert("Please enter a valid whole number between " + minValue + " and " + maxValue + ".");
		textbox.value = '';
		return false;
	}
	else if (textbox.value.length > 0 && (parseInt(textbox.value) < minValue || parseInt(textbox.value) > maxValue)) {
		alert("Please enter a valid whole number between " + minValue + " and " + maxValue + ".");
		textbox.value = '';
		return false;
	}
	
	return true;
}

function validateFloatTextBox(textbox) {
	if (!isValidFloat(textbox.value)) {
		alert("Please enter a valid number.");
		textbox.value = '';
		return false;
	}
	
	return true;
}

function validateFloatTextBoxRange(textbox, minValue, maxValue) {
	if (!isValidFloat(textbox.value)) {
		alert("Please enter a valid number between " + minValue + " and " + maxValue + ".");
		textbox.value = '';
		return false;
	}
	else if (textbox.value.length > 0 && (parseFloat(textbox.value) < minValue || parseFloat(textbox.value) > maxValue)) {
		alert("Please enter a valid number between " + minValue + " and " + maxValue + ".");
		textbox.value = '';
		return false;
	}
	
	return true;
}

function checkOtherSum(text, questionId) {
	try {
		if (text.length > 0 && isValidInteger(text) && getIntValue(text) > 0) {
			document.getElementById('div_q_' + questionId).style.visibility = 'visible';
				
			if (document.getElementById('q_' + questionId).value.length == 0) {
				document.getElementById('q_' + questionId).focus();
			}
		}
		else {
			document.getElementById('div_q_' + questionId).style.visibility = 'hidden';
		}
	}
	catch(objError) {
		// MS - Put this in as it was throwing errors - May need to check!
		// alert("Error Caught: " + objError);
	}
}

function checkMatrixRank(form, questionID, rows, cols, exactlyOne) {
	for (var i = 0; i < cols; i++) {
		var selectedCount = 0;
		for (var j = 1; j <= rows; j++) {
			var question = eval("form." + "q_" + questionID + "_" + j);
			if (question[i].checked) {
				selectedCount++;
			}
		}

		if (exactlyOne && selectedCount != 1 || selectedCount > 1) {
			return false;
		}
	}
	
	return true;
}

function notSureSelected(checkbox, textQuestionId) {
	var question = document.getElementById(textQuestionId);
	if (checkbox.checked) {
		question.value = "";
		question.disabled = true;
	}
	else {
		question.disabled = false;
	}
}

function textEntered(textbox) {
	if (textbox.value.length > 0) {
		return true;
	}
	else {
		alert('Please enter a description for this textbox.');
		textbox.focus();
		return false;
	}
}

function checkEmail(emailText) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(emailText);
}
