function empty(str)
{
	for (var i = 0; i < str.length; i++)
		if (str.charAt(i) != ' ')
			return false;
	return true;
}

function trim(str)
{
	var i, j;
	for (i = 0; i < str.length; i++)
		if (str.charAt(i) != ' ')
			break;
	for (j = str.length-1; j >= 0; j--)
		if (str.charAt(j) != ' ')
			break;
	if (i <= j)
		return str.substring(i,j+1);
	return '';
}

function len(str)
{
	return str.length;
}

function checkdate(str, f) // f - date format: 1 - yyyy.mm.dd, 2 - dd/mm/yyyy, 3 - mm/dd/yyyy
{
	var x, s, s1, s2, s3, d, m, y;

	str = trim(str);
	if (str == '') return true;

	s = '/'; x = str.indexOf(s);
	if (x < 0) {s = '.'; x = str.indexOf(s);}
	if (x < 0) {s = '-'; x = str.indexOf(s);}
	if (x < 0 || x == str.length)
		return false;
	s1 = str.substring(0, x);
	x = str.indexOf(s, x+1);
	if (x < 0 ||  x+1 == str.length)
		return false;
	s2 = str.substring(s1.length+1, x);
	s3 = str.substring(x+1);

	if (f == 1) {y = s1; m = s2; d = s3}
	else if (f == 2) {d = s1; m = s2; y = s3}
	else {m = s1; d = s2; y = s3}

	return checkinteger(m) && checkrange(m, 1, 12) && checkinteger(y) && checkrange(y, 0, null) && checkinteger(d) && checkday(y, m, d);
}

function checkday(y, m, d)
{
	var maxDay = 31;
	if (m == 4 || m == 6 || m == 9 || m == 11)
		maxDay = 30;
	else if (m == 2) {
		if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
			maxDay =29;
		else
			maxDay = 28;
	}
	return checkrange(d, 1, maxDay);
}

function checkinteger(s)
{
	if (empty(s)) return true;
	if (s.indexOf(".") < 0)
		return checknumber(s);
	return false;
}

function numberrange(n, min, max)
{
	if (min != null && n < min || max != null && n > max)
		return false;
	return true;
}

function checknumber(s)
{
	s = trim(s);
	if (s == '') return true;
	var number_format = ".0123456789";
	var start_format = "+-" + number_format;
	var decimal = false;
	var x = start_format.indexOf(s.charAt(0))
	if (x < 0) return false;
	if (x == 2) decimal = true;
	for (var i = 1; i < s.length; i++) {
		x = number_format.indexOf(s.charAt(i))
		if (x < 0)
			return false;
		if (x == 0) {
			if (decimal) return false;
			decimal = true;
		}
	}
	return true;
}

function checkrange(s, min, max)
{
	if (empty(s)) return true;
	return checknumber(s) && numberrange(eval(s), min, max);
}

function checkemail(emailaddr)
{
	var i, c, emailbox, emaildomain;

	emailaddr = trim(emailaddr);
	if (emailaddr == '') return true;
	if (emailaddr.indexOf('@') < 0) {
		alert("No @ sign detected. An @ sign is part of every e-mail address.");
		return false;
	}
	if (emailaddr.charAt(emailaddr.length-1) == '@') {
		alert("An @ sign cannot be the last character of the e-mail address.");
		return false;
	}
	if (emailaddr.charAt(0) == '@') {
		alert("An @ sign cannot be the first character of the e-mail address.");
		return false;
	}
	if (emailaddr.indexOf('.') < 0) {
		alert("No period detected. An e-mail address contains at least one dot.");
		return false;
	}
	if (emailaddr.charAt(emailaddr.length-1) == '.') {
		alert("The last character of the e-mail address cannot be a dot.");
		return false;
	}
	if (emailaddr.charAt(0) == '.') {
		alert("The first character of the e-mail address cannot be a dot.");
		return false;
	}
	if (emailaddr.indexOf(',') >= 0) {
		alert("A valid e-mail address cannot contain a comma.\r\nIf you have a CompuServe account, substitute a period for the comma in your CompuServe ID,\r\nlike so: 12345.6789@compuserve.com.");
		return false;
	}
	if (emailaddr.indexOf(' ') >= 0) {
		alert("A valid e-mail address cannot contain a space.");
		return false;
	}
	if (emailaddr.indexOf('@',emailaddr.indexOf('@')+1) > 0) {
		alert("A valid e-mail address can only contain one @ sign.");
		return false;
	}
	for (i = 0; i < emailaddr.length; i++) {
		c = emailaddr.charAt(i);
		if (c < ' ' || "!#$%^&*+=|\\/:;?\"'<>(){}[]`~".indexOf(c) >= 0) {
			alert("A valid e-mail address cannot contain non-printable character or any of these characters:\r\n! # $ % ^ & * + = | \\ / : ; ? \" ' < > ( ) { } [ ] ` ~");
			return false;
		}
	}
	i = emailaddr.indexOf('@');
	emailbox = emailaddr.substring(0,i);
	emaildomain = emailaddr.substring(i+1,emailaddr.length);
	if (emaildomain.indexOf('.') < 0) {
		alert("The host portion of the e-mail address was not valid, there must be at least 2 parts to the domain (i.e. a.com).");
		return false;
	}
	c = emaildomain.substring(emaildomain.lastIndexOf('.')+1,emaildomain.length);
	if (c.length < 2 || c.length > 6) {
		alert("The top level domain of the e-mail address was not valid.");
		return false;
	}
	if (emaildomain.charAt(0) == '.') {
		alert("The first character of the host portion of the e-mail address cannot be a dot.");
		return false;
	}
	if (emailbox.charAt(emailbox.length-1) == '.') {
		alert("The last character of the mailbox portion of the e-mail address cannot be a dot.");
		return false;
	}
	return true;
}
