

/*
 *	Global Functions
 */

// Returns true if string contains only whitespace characters
function isblank(s) {

	for(var i = 0; i < s.length; i++) {

		var c = s.charAt(i);

		if(c != '\n' && c != '\t') {

			return false;
		}
	}
	return true;
}

// Returns true if string contains only numeric characters
function isnumeric(s)
{
	return !isNaN(parseFloat(s));
}

// Returns reverse of a string
function reverse(s) {

	var rev = new String;

	var i = s.length;
	
	while(i--) {

		rev += s.charAt(i);
	}
	
	return rev;
}

function formatdate(obj, delKey, dir) {

	if(obj.value.length < 10) {

		if(delKey != 9) { //tab

			if(delKey != 8 && delKey != 46 && delKey != 16 && !(delKey > 36 && delKey < 41)) { //if the delete, backspace, shift, are not the keys that caused the keyup event.

				var fieldLen = obj.value.length;

				if((delKey >= 48 && delKey <= 57) || (delKey >= 96 && delKey <= 105)) {

					if(fieldLen == 2 || fieldLen == 5) {

						obj.value = obj.value + "/";
					}
				}
				else {

					if(dir == "up") {

						if(obj.value.length == 0) {

							obj.value = "";
						}
						else {

							obj.value = obj.value.substring(0, obj.value.length - 1);
						}
					}
				}
				obj.focus();
			}
		}
		else {

			if(dir == "down") {

				checkdate(obj.value);
			}
		}
	}
}

// Check the date syntax
function checkdate(s) {

	var days = new Array(31,29,31,30,31,30,31,31,30,31,30,31);

	if(s.length != 10) {

		return false;
	}

	var mon = s.substring(0, 2);	// month
	var s1 = s.substring(2, 3);	// '/'
	var day = s.substring(3, 5);	// day
	var s2 = s.substring(5, 6);	// '/'
	var yr = s.substring(6, 10);	// year

	if(mon < 1 || mon > 12 || day < 1 || day > 31 || yr < 1900) {

		return false;
	}

	if(day > days[mon]){

		return false;
	}

	if(mon == 2) {

		var g = parseInt(yr/4);

		if(isNaN(g)) {

			return false;
		}

		if(day == 29 && (g != (yr/4))) {

			return false;
		}
	}

	return true;
}

/*
// Returns true if string matches the date pattern
function verifyre(regex, s) {

	return regex.test(s);
}

// Returns true if string matches the zip pattern
function verifyzip(s) {

	var exp = /\d[0-9]{5}(-[0-9]{4})/;

	return exp.test(s);
}

// Returns true if string matches the phone pattern
function verifyphone(s) {

	var shortUS = /\\(\\d{3}\\)\\d{3}-\\d{4}(x\\d{1,4}/;
	var longUS = /1-\\d{3}-\\d{3}-\\d{4}(x\\d{1,4}/;
	var intl = /\\+\\d{1,3}-\\d{1,4}-\\d{1,8}(x\\d{1,4}/;

	return exp.test(shortUS) || exp.test(longUS) || exp.test(intl);
}
*/

// Returns true if string matches the date pattern
function verifydate(s) {

	var exp = /\d{1,2}\/\d{1,2}\/\d{4}/;

	return exp.test(s) && checkdate(s);
}

// Returns true if string matches the email pattern
function verifyemail(s) {

	//var exclude = /[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	//var check = /@[\w\-]+\./;
	//var checkend = /\.[a-zA-Z]{2,3}$/;

	return s.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1;
}

// Returns true if string matches the amount pattern
function verifyamt(s) {

	return s.search(/^\d*\.?\d{0,2}$/) != -1;
}

// Returns true if string matches the credit card number pattern
function verifycard(s) {

	var rev = reverse(s);
	
	var total = new Number(0);
	
	for(var i = 0; i < rev.length; i += 1) {

		var temp = 0;

		if(i % 2) {

			temp = rev.substr(i, 1) * 2;

			if(temp >= 10) {

				var split = new String(temp);
				temp = parseInt(split.substr(0, 1)) + parseInt(split.substr(1, 1));
			}
		}
		else {

			temp = rev.substr(i, 1);
		}	
		total += parseInt(temp); 
	}
	return total % 10;
}

/*
 *	Verify a field for emptiness
 */

function isempty(e) {

	return (e.value == null) || (e.value == "") || isblank(e.value);
}