function isBlank(str) {
	if (str.search(/\S/) == -1) {
		return true;
	} else {
		return false;
	}
}

function isInteger(str) {
	if (str.search(/^\d+$/) != -1) {
		return true;
	} else {
		return false;
	}
}

// Returns true if given year is a leap year. Input should be an integer.
function isLeapYear(intYear) {
	var ret = false;
	
	if (intYear % 4 == 0) {
		ret = true;
		if (intYear % 100 == 0 && !(intYear % 400 == 0)) {
			ret = false;
		}
	}
	
	return ret;
}

// Returns number of days in given month. Input month should be an integer in range 1 to 12.
// Input year should be an integer.
function daysInMonth(intMonth, intYear) {
	var days = 31;
	if (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) {
		days = 30;
	} else if (intMonth == 2) {
		if ( isLeapYear(intYear) ) {
			days = 29;
		} else {
			days = 28;
		}
	}
	return days;
}

// removes leading & trailing whitespace
function trim(str) {
	return str.replace(/^\s+/, '').replace(/\s+$/, '');
}

// add a css class to an element (ensuring it isn't added twice)
function addCSSClass(element, strClass) {
	removeCSSClass(element, strClass);
	element.className = element.className + ' ' + strClass;
}

// remove a css class from an element (if it's there)
function removeCSSClass(element, strClass) {
	element.className = element.className.replace(new RegExp(strClass), '');
	element.className = element.className.replace(/\s+/, ' ');
	element.className = trim(element.className);
}

// validates the correct setting of a set of date boxes,
// 		with names suffixed with 'Day', 'Month' and 'Year'.
// The field name is used in error messages.
function blnValidateDate(strDatePrefix, strFieldName) {
	var hasError = false;
		
	var dayInput   = document.getElementById(strDatePrefix + "Day");
	var monthInput = document.getElementById(strDatePrefix + "Month");
	var yearInput  = document.getElementById(strDatePrefix + "Year");
	
	// clear previous error conditions
	removeCSSClass(dayInput, 'error');
	removeCSSClass(yearInput, 'error');
	removeCSSClass(monthInput, 'error');
	
	var intDay   = 0;
	var intMonth = 0;
	var intYear  = 0;
		
	if (!isInteger(trim(yearInput.value))) {
		hasError = true;
		errorStr = errorStr + strFieldName + ': year should be a number.\n\n';
		addCSSClass(yearInput, 'error');
	} else {
		intYear = parseInt(yearInput.value, 10);
	}
		
	if (!isInteger(trim(monthInput.value))) {
		hasError = true;
		errorStr = errorStr + strFieldName + ': month should be a number.\n\n';
		addCSSClass(monthInput, 'error');
	} else {
		intMonth = parseInt(monthInput.value, 10);
		if (intMonth < 1 || intMonth > 12) {
			hasError = true;
			errorStr = errorStr + strFieldName + ': month should be in range 1 to 12.\n\n';
			addCSSClass(monthInput, 'error');
		}
	}
		
	if (!isInteger(trim(dayInput.value))) {
		hasError = true;
		errorStr = errorStr + strFieldName + ': day should be a number.\n\n';
		addCSSClass(dayInput, 'error');
	} else {
		intDay = parseInt(dayInput.value, 10);
		if (intDay < 1 || intDay > daysInMonth(intMonth, intYear)) {
			hasError = true;
			errorStr = errorStr + strFieldName + ': the entered day is not valid in the entered month.\n\n';
			addCSSClass(dayInput, 'error');
		}
	}
	return hasError;
}	

function blnValidateNotBlank(strElementId, strFieldName) {
	var hasError = false;
	var elTextBox = document.getElementById(strElementId);
	removeCSSClass(elTextBox, 'error');
	if (isBlank(elTextBox.value)) {
		hasError = true;
		addCSSClass(elTextBox, 'error');
		errorStr += strFieldName + ' must not be blank.\n\n';
	}
	
	return hasError;
}

function blnValidatePositiveInteger(strElementId, strFieldName, blnPermitBlank) {
	var hasError = false;
	var elTextBox = document.getElementById(strElementId);
	removeCSSClass(elTextBox, 'error');
	if (blnPermitBlank && isBlank(elTextBox.value)) {
		// do nothing
	}
	else {
		if (trim(elTextBox.value).search(/^\d+$/) == -1) {
			hasError = true;
			addCSSClass(elTextBox, 'error');
			errorStr += strFieldName + ' must be a positive number.\n\n';
		}
	}
	
	return hasError;
}

function blnValidatePercentage(strElementId, strFieldName, blnPermitBlank) {
	var hasError = false;
	var elTextBox = document.getElementById(strElementId);
	removeCSSClass(elTextBox, 'error');
	if (blnPermitBlank && isBlank(elTextBox.value)) {
		// do nothing
	}
	else {
		if (trim(elTextBox.value).search(/^\d+$/) == -1 || parseInt(elTextBox.value, 10) > 100) {
			hasError = true;
			addCSSClass(elTextBox, 'error');
			errorStr += strFieldName + ' must be a positive integer not greater than 100.\n\n';
		}
	}

	return hasError;
}