function isValidEmail(value) {
	if (value==null || value=="")	return false;
	if (value.indexOf(" ")>=0)		return false;

	var state = 1;
	var code, username='', domain='', amountOfDot = 0;
	for (var i=0; i<value.length; i++) {
		code = value.charAt(i);
		if (state==1) {
			if (	code == "<" || code == ">" 
					|| code == "(" || code == ")"	) return false;
			else if (	code == "@"	)
				if (username == '') return false;
				else state = 3;
			username += code;
		}
		else if (state==3) {
			if (	(code >= "0" && code <= "9")
					|| (code >= "A" && code <= "Z")
					|| (code >= "a" && code <= "z")
					|| code == "_"
					|| code == "-"
				) ;
			else if (code == ".")
				if (domain == '' || domain.charAt(domain.length-1) == '.') return false;
				else amountOfDot++;
			else return false;
			domain += code;
		}
	}
	if (state != 3) return false;
	if (domain == '' || domain.charAt(domain.length-1) == '.') return false;
	if (amountOfDot <1) return false;
	return true;
}

//------------------------------------------------------------------------
function isValidText(obj) {
	if(obj == null) return false;
	var value = Trim(obj.value);
	var len = value.length;
	if(len == 0 && value == "") { 
		obj.value = "";
		return false;
	}
	return true;
}

//------------------------------------------------------------------------
function checkNumber(numfrom,numto,num,obj){
	var a, st;
	st= obj.value;
	for (var i=0;i< st.length; i++){
		if (st.charAt(i) < '0' || st.charAt(i)>'9'){
		if (st.charAt(i) != '.') {
			alert("Invalid charater! Only digits are accepted.");
			obj.focus();
			return 1;
		} }
	}
		if ( eval(obj.value)< numfrom || eval(obj.value)> numto ){
		alert("Invalid value! Must be between " + numfrom + " - " + numto + ".");
		obj.focus();
		return 1;
	}		
	return 0;
}

//------------------------------------------------------------------------
function isValidDate1(o1,o2,o3){
	var Month=o1.value, Day=o2.value, Year=o3.value;
	var DaysPerMonth, msg;
	switch (eval(Month)) {
		case 4:
		case 6:
		case 9:
		case 11: { DaysPerMonth=30; msg="30 days"; break; }
		case 2:  { 
			if ((Year%4 == 0) && ((Year%100 != 0) || (Year%400 == 0))) {
				DaysPerMonth=29; msg="29 days (leap year)"; }
			else { DaysPerMonth=28; msg="28 days"; }
			break; }
		default: { DaysPerMonth=31; msg="31 days"; break; }
	}
	if (Day.length!=0 && (eval(Day)>eval(DaysPerMonth))) {
		//alert("Invalid day value. This month only has " + msg);
		//o1.focus();
		return false;
	}
	return true;
}

//------------------------------------------------------------------------
function isValidDate2(obj) {
	var strDate=obj.value;
	var unicode, findtype=0;
	
	for (var i=0;i<strDate.length; i++) {
		unicode=strDate.charCodeAt(i);
		if (unicode<48 || unicode>57) { // not from 0-9
			if (unicode!=47) { // not  "/"
				alert("Invalid charater: '" + strDate.charAt(i) + "' (code: " + unicode + ")");
				obj.focus();
				return 0;
			}
		}
	}

	var s=obj.value;
	if (s.length!=0 && s.indexOf("/")==-1) {
		alert("Invalid date syntax. Please follow: mm/dd/yyyy");
		obj.focus();
		return 0;
	}

	var elem=s.split("/");
	if (s.length!=0 && (elem[0].length==0 || eval(elem[0])<1 || eval(elem[0])>12)) {
		alert("Invalid month value. Must be between 1-12.");
		obj.focus();
		return 0;
	}

	var DaysPerMonth, msg;
	switch (eval(elem[0])) {
		case 4:
		case 6:
		case 9:
		case 11: { DaysPerMonth=30; msg="30 days"; break; }
		case 2:  { 
			if ((elem[2]%4 == 0) && ((elem[2]%100 != 0) || (elem[2]%400 == 0))) {
				DaysPerMonth=29; msg="29 days (leap year)"; }
			else { DaysPerMonth=28; msg="28 days"; }
			break; }
		default: { DaysPerMonth=31; msg="31 days"; break; }
	}

	if (s.length!=0 && (elem[1].length==0 || eval(elem[1])<1 || eval(elem[1])>eval(DaysPerMonth))) {
		alert("Invalid day value. This month only has " + msg);
		obj.focus();
		return 0;
	}

	if (s.length!=0 && (elem[2].length==0 || eval(elem[2])<1900 || eval(elem[2])>3000)) {
		alert("Invalid year value. Must be between 1900-3000");
		obj.focus();
		return 0;
	}
	
	var indate = new Date(elem[2], elem[0], elem[1]);
	return indate;
}

//------------------------------------------------------------------------
function checkLen(obj,n) {
	if (obj.value.length>n) {
		alert("You have exceeded the total number of characters (" + n + ").\nThe string will be truncated at its end.");
		obj.value = obj.value.substring(0,n);
	}
}

function isNumeric(value) {
	if(value == "" || isNaN(value) || value.indexOf('E') >= 0 || value.indexOf('e') >= 0) return false;
	else if(value < 0) return false;
	return true;
}

function Trim(str) {	
	if(str == null || str.length == 0) return "";
	while (str.charCodeAt(0) <= 32)
	{
		str = str.substr(1);
	}

	while (str.charCodeAt(str.length - 1) <= 32)
	{
		str = str.substr(0, str.length - 1);
	}
	return str;
}
