// JavaScript Document
function NumCheckKeyPress () {
	if(window.event){
		var key = window.event.keyCode;
	}
	if (key!=13 && key!=8 && key!=9) {
		if (key<48 || key>58) return false;
		else return true;
	}
}

function RealCheckKeyPress () {
	if(window.event){
		var key = window.event.keyCode;
	}
	if (key!=13 && key!=8 && key!=9) {
		if (key<48 || key>58) {
			if (key!=46) return false;
		} else return true;
	}
}

function RealCheckKeyPressPositive () {
	if(window.event){
		var key = window.event.keyCode;
	}
	if (key!=13 && key!=8 && key!=9) {
		if (key<48 || key>58) {
			if (key!=46 && key!=45) return false;
		} else return true;
	}
}

function RealCheck (fieldstr) {
	var flag=true;
	var countdot=0;
	for (i=0; i<fieldstr.length; i++) {
		if (fieldstr.charCodeAt(i)<48 || fieldstr.charCodeAt(i)>58) {
			if (fieldstr.charCodeAt(i)!=46) {
				flag=false;
			} else {
				if (fieldstr.charCodeAt(i)==46) countdot++;
			}
		}
	}
	if (countdot>1) {
		alert ("Too much dot signs");
		flag=false;
	}
	return flag;
}

function RealPositiveCheck (fieldstr) {
	var flag=true;
	var countdot=0;
	var countneg=0;
	for (i=0; i<fieldstr.length; i++) {
		if (fieldstr.charCodeAt(i)<48 || fieldstr.charCodeAt(i)>58) {
			if (fieldstr.charCodeAt(i)!=46 && fieldstr.charCodeAt(i)!=45) {
				flag=false;
			} else {
				flag=true;
				if (fieldstr.charCodeAt(i)==46) countdot++;
				if (fieldstr.charCodeAt(i)==45) countneg++;
			}
		}
	}
	if (countdot>1) {
		alert ("Too much dot signs");
		flag=false;
	}
	if (countneg>1) {
		alert ("Too much minus signs");
		flag=false;
	}
	return flag;
}


function NumCheck (fieldstr) {
	var flag=true;
	for (i=0; i<fieldstr.length; i++) {
		if (fieldstr.charCodeAt(i)<48 || fieldstr.charCodeAt(i)>57) flag=false;
	}
	return flag;
}

function emailCheck (emailStr) {
	var result="";
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		result="Email address seems incorrect";
		return result;
	} //if (matchArray==null)
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			result="Ths username contains invalid characters.";
			return result;
   		} //if (user.charCodeAt(i)>127)
	} //for
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			result="Ths domain name contains invalid characters.";
			return result;
   		}
	}
	if (user.match(userPat)==null) {
		if (user.match(userPat)==null) {
			result="The username doesn't seem to be valid.";
			return result;
		}
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					result="Destination IP address is invalid!";
					return result;
   				}
			}
			return result;
		}
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			result="The domain name does not seem to be valid.";
			return result;
   		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		result="The address must end in a well-known domain or two letter " + "country.";
		return result;
	}
	if (len<2) {
		result="This address is missing a hostname!";
		return result;
	}
	return result;
}
