function doFocus(obj) {
	if (obj.type == "text" || obj.type == "textarea" || obj.type == "password")
		obj.select(); 
	obj.focus();
}

function swap(img, src) {
	img.src = src;
}

var whitespace = " \t\n\r";

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, 1900, 2020);
}

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 convertMeridian(meridian) {
	if(meridian == 'AM') {
		meridian = 1;
	} else if (meridian == 'PM') {
		meridian = 2;
	}
	return meridian;
}

function dateCompare(year1, month1, day1, hour1, minute1, meridian1, 
					year2, month2, day2, hour2, minute2, meridian2) {
			
	if(hour1 == null) hour1 = 0;
	if(minute1 == null) minute1 = 0;
	if(hour2 == null) hour2 = 0;
	if(minute2 == null) minute2 = 0;
	
	if(convertMeridian(meridian1) == 2) hour1 =  parseInt(hour1) + 12;
	if(convertMeridian(meridian2) == 2) hour2 =  parseInt(hour2) + 12;
	
	var comp = null;
	if (isDate(year1, month1, day1) && isDate(year2, month2, day2)) {
		var date1 = new Date(year1, month1 - 1, day1, hour1, minute1, 0);
		var date2 = new Date(year2, month2 - 1, day2, hour2, minute2, 0);
		
		if (date1.getTime() < date2.getTime()) {
			comp = -1;
		} else if (date1.getTime() == date2.getTime()) {
			comp = 0;
		} else if (date1.getTime() > date2.getTime()) {
			comp = 1;
		}
	}
	return comp;
}

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(obj) {
	for (var i = 0; i < obj.length; i++) {
		if (obj[i].checked) return true;
	}
	return false;
}

function isEmail(s, emptyOK) {
	if (isEmpty(s)) return (emptyOK == null) ? false : emptyOK;
	s = trim(s);
    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;
   	return true;
}

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;
}

var remote=null;

function popUp(n,u,w,h,scollbar,x) {
	width = w;
	height = h;
	args="menubar=no,toolbar=no,location=no,resizable=yes,directories=no,status=no,width="+width+",height="+height;
	if (scollbar == "yes")
		args = args+",scrollbars=yes";
	remote=window.open(u,n,args);
	remote.focus();
	if (remote != null) {
  		if (remote.opener == null) 
			remote.opener = self;
	}
	if (x == 1) { return remote; }
}


