/* Subversion: $Id: check.js 69 2005-12-02 05:22:35Z rhorton $ */

// returns true if a string is all whitespace (space, tab, newline); false otherwise
function isBlank(s) {
	var c;
	for (var i = 0; i < s.length; i++) {
		c = s.charAt(i);
		if (( c != ' ') && (c != '\n') && (c != '\t')) { return false; }
	}
	return true;
}


function isYear(value, min, max) {

	if (matchesPattern(value, "^\\d\\d\\d\\d$")) {

		if (arguments[1] != null) {
			flag = value < min;
		}
		if (arguments[2] != null) {
			flag = flag || value > max;
		}

		return ! flag;

	} else {
		return false;
	}
}

function isMonth() {
	return isNaturalNumber(value) && value >= 1 && value < 13;
}

function isDay() {
	return isNaturalNumber(value) && value >= 1 && value <= 31;
}


function isHour(value) {
	return isNaturalNumber(value, true) && value >= 0 && value < 24;
}

function isMinute(value) {
	return isNaturalNumber(value, true) && value >= 0 && value < 60;
}

function isSecond(value) {
	return isNaturalNumber(value, true) && value >= 0 && value < 60;
}

function is3Digits(value) {
	return matchesPattern(value, "^\\d\\d\\d$");
}

function is4Digits(value) {
	return matchesPattern(value, "^\\d\\d\\d\\d$");
}

// returns true if a string can be interpreted as an email address; false otherwise
function isEmail(value) {
//			  /.*@.*\..*/
	var p = /.*@.*\..*/;
	return matchesPattern(value, ".*@.*\\..*");
}

// returns true if a string can be intrepreted as a date (YYYY/MM/DD); false otherwise
function isDate(value) {
	return (matchesPattern(value, "^(2[0-9]{3})/((0[1-9])|(1[12]))/(0[1-9]|([12][0-9])|(3[0-1]))$"));
}

// Check that a Canadian postal code is valid
function isPostalCode(postalcode) {
   if (postalcode.search) {
      postalcode = removeSpaces(postalcode);
      if (postalcode.length == 6 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;
      else if (postalcode.length == 7 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]-\d[a-zA-Z]\d$/) != -1) return true;
      else return false;
   }
   return true;
}

// Remove all spaces from a string
function removeSpaces(string) {
   var newString = '';
   for (var i = 0; i < string.length; i++) {
      if (string.charAt(i) != ' ') newString += string.charAt(i);
   }
   return newString;
}

function isInteger(value, zero) {
	if (zero) {
		return matchesPattern(value, "^-?[0-9]+$");
	} else {
		return matchesPattern(value, "^-?0?[1-9]{1}[0-9]*$");
	}
}

function isNaturalNumber(value, zero) {
    if (zero) {
        return matchesPattern(value, "^[0-9]*$");	
    } else {
        return matchesPattern(value, "^\\+?0?[1-9]{1}(\\d)*$");
    }
}

function isNumber(value) {
    return matchesPattern(value, "^-?[0-9]+\\.?[0-9]*$");
}

// note: argument shouldn't have "$" prepended to the front
function isCurrency(value, negative) {

	if (negative) {
		if (isInteger(value)) {
			return true;
		}
		return matchesPattern(value, "^-?[0-9]+\\.[0-9]{1,2}$");
	} else {
		if (isNaturalNumber(value, true)) {
			return true;
		}
		return matchesPattern(value, "^[0-9]+\\.[0-9]{1,2}$");
	}
}

function isQuarter(value) {
	return (isInteger(value) && value >= 1 && value <= 16);
}

function isSection(value) {
	return (isInteger(value) && value >=1 && value <= 36);
}

function isTownship(value) {
	return (isInteger(value) && value >= 1 && value <= 126);
}

function isRange(value) {
	return (isInteger(value) && value >= 1 && value <= 30);
}

function isMeridian(value) {
	return (isInteger(value) && value >= 2 && value <= 6);
}

// a function used to abstract the functionality of pattern matching
function matchesPattern(value, pattern) {
	var p = new RegExp(pattern);
	return p.test(value);
}
