var whitespace = " \t\n\r";
var keyExp = /^[a-zA-Z0-9_\-]*$/;
var daysInMonth = new Array();

daysInMonth[1] = 31;
daysInMonth[2] = 29;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

function leftTrim(s) {
	var i = 0;
	while (i < s.length && whitespace.indexOf(s.charAt(i)) != -1) i++;
	return s.substring(i, s.length);
}

function rightTrim(s) {
	var i = s.length;
	while (i > 0 && whitespace.indexOf(s.charAt(i - 1)) != -1) i--;
	return s.substring(0, i);
}

function trim(s) {
	return rightTrim(leftTrim(s));
}

function isEmpty(s) {
	return trim(s).length == 0;
}

function isNumeric(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK; 
	return !isNaN(s);
}

function isInteger(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
    return !isNaN(s) && s.indexOf(".") == -1;
}

function isPositive(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return !isNaN(s) && s > 0;
}

function isNegative(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return !isNaN(s) && s < 0;
}

function isNonNegative(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return !isNaN(s) && s >= 0;
}

function isNonPositive(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return !isNaN(s) && s <= 0;
}

function isInRange(s, min, max, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return !isNaN(s) && s >= min && s <= max;
}

function isYear(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return isInteger(s) && isInRange(s, 1753, 9999);
}

function isMonth(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return isInteger(s) && isInRange(s, 1, 12);
}

function isDay(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return isInteger(s) && isInRange(s, 1, 31);
}

function isDate(year, month, day) {
	if (!isYear(year) || !isMonth(month) || !isDay(day)) return false;  
    if (day > daysInMonth[month]) return false;
    if (month == 2 && day > daysInFebruary(year)) return false;
    return true;
}

function isFutureDate(year, month, day) {
	if (!isDate(year, month, day)) return false;  
	var now = new Date();
	var then = new Date(year, month - 1, day);
	if (then.getTime() < now.getTime()) return false;
	return true;
}

function isStringDate(s) {
	var datetime = s.split(" ");
	var date = datetime[0].split("/");
	return isDate(date[2], date[0], date[1]);
}

function daysInFebruary(year) {
    return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28;
}

function isChecked(el) {
	if(el.length) {
		for (var i = 0; i < el.length; i++) {
			if (el[i].checked) return true;
		}
	} else {
		if (el.checked) return true;
	}
	return false;
}

function isURL(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	s = trim(s);
	if (s.length < 8 || s.substring(0, 7) != "http://") return false;
	return true;
}

function isEmail(s, emptyOK) {
	var REG = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/;
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
		s = trim(s);
	if (s.indexOf(" ") != -1) return false;
		var i = 1;
		while (i < s.length && s.charAt(i) != "@") i++;
		if (i >= s.length || s.charAt(i) != "@") return false;
			i += 2;
		while (i < s.length && s.charAt(i) != ".") i++;
			if (i >= s.length - 1 || s.charAt(i) != ".") return false;
			if (s.match(REG))  {
			    return true;
			} //end if
			else { 
				return false;
		    } //end if	
	
	return true;
}

function isEmailList(s, field) {
	s = trim(s);
	var a = new Array();
	var cList = s.split(",");
	var sList = s.split(";");
	var rList = s.split("\r\n");
	if (cList.length > sList.length && cList.length > rList.length) a = cList;
	else if (rList.length > cList.length && rList.length > sList.length) a = rList;
	else a = sList;
	var b = removeEmptyElements(a);
	for (var i in b) {
		if (!isEmail(b[i])) return false;
	}
	if (field) field.value = b.join(";");
	return true;
}

function isZip(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	if (s.length != 5 || !isInteger(s)) return false;
	return true;
}

function isKey(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	return keyExp.test(s);
}

function isLongerThan(s, c) {
	if (s.length > c) return true;
	return false;
}

function isLength(s, c) {
	if (s.length == c) return true;
	return false;
}

function removeEmptyElements(a) {
	var b = new Array();
	for (var i in a) {
		if (!isEmpty(a[i]))
			b[b.length] = trim(a[i]);
	}
	return b;
}
