var debugValidation = false;

var alphaNumStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.,_'$()-/#:?!\\@%^&*;~`+=| ";
var invalidStr = "[]<>{}\"";
var invalidStrDisc = "[]<{}\"";

function isPlainText(data){
    var tmpstr = data;
    var tmpchr;
	
    if (tmpstr.length < 1)
       return false;
	else {
		for (i=0; i < tmpstr.length; i++)
		{
			tmpchr = tmpstr.charAt(i);
			if (invalidStr.indexOf(tmpchr) > -1)
				return false;
		}
	}    
    return true;
}

function isPlainTextNull(data){
    var tmpstr = data;
    var tmpchr;
    
    for (i=0; i < tmpstr.length; i++)
    {
        tmpchr = tmpstr.charAt(i);
        if (invalidStr.indexOf(tmpchr) > -1)
            return false;
    }
    return true;
}

function isPlainTextDisc(data){
    var tmpstr = data;
    var tmpchr;
	
    if (tmpstr.length < 1)
       return false;
	else {
		for (i=0; i < tmpstr.length; i++)
		{
			tmpchr = tmpstr.charAt(i);
			if (invalidStrDisc.indexOf(tmpchr) > -1)
				return false;
		}
	}    
    return true;
}

function getFullDate(theYear, theMonth, theDay, theHour, theMin, theAmPm) {
	hourVal = theHour;
	
	if (!isHour(hourVal)){
		if (debugValidation) alert("bad hour");
		return false;
	}
	if (!isMinute(theMin)) {
		if (debugValidation) alert("bad min");
		return false;
	}
	if (!isDate(theDay, theMonth, theYear)) {
		if (debugValidation) alert("bad date");
		return false;
	}
	
	if (theAmPm==1) hourVal = eval(hourVal) + 12;

	theDate = new Date(theYear, theMonth, theDay, hourVal, theMin);
	return theDate;
}

function isDate(day, month, year) {
	max=0;

	if ((day == 0) && (month == 0) && ((year == 0) || (year.length == 0))){
	   return true;
	}
	if (isMonth(month) && isYear(year)) {
		if ((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
			max = 31;
		else if ((month==4)||(month==6)||(month==9)||(month==11))
			max = 30;
		else if ((month==2) && (((year/4)==parseInt(year/4)))){
			max = 29;
			if ((year/100)==parseInt(year/100))
				max = 28;
			if ((year/400)==parseInt(year/400))
				max = 29;
		}
		else
			max = 28;
			
	   if ((day > 0) && (day <= max))
		   return true;
	}
	return false;
}
    
function isMonth(data) {
	if (debugValidation) alert("(month) val: " + eval(data) + ", isNaN: " + isNaN(data) + ", length: " + data.length);
	if ((isNaN(data)) || (data.length == 0) || (eval(data) < 0) || (eval(data) > 12))
		return false;
    return true;
}

function isYear(data) {
    if ((isNaN(data)) || (data.length == 0)) {
        return false;
    }
    else if ((eval(data)-1000)<0){
        if (pivotYear(data))
            return true;
        else
            return false;
    }
    else
        return true;
}

function pivotYear(data){
    if ((data<=50)&&(data>=0)){
        data = eval(data)+2000;
		return true;
	}
	else if ((data<100)&&(data>50)){
		data = eval(data)+1900;
		return true;
	}
	return false
}


function isHour(hourVal) {
	if (isNaN(hourVal))
		{
		if (debugValidation) alert("hourVal is Non a Number, it is: " + hourVal);
		return false;
		}
	else if ((eval(hourVal) < 0) || (eval(hourVal) > 12) || (hourVal.length == 0))
		{
		if (debugValidation) alert("hourVal is a number, but invalid. It is: " + hourVal);
		return false;
		}
	else
		{
		return true;
		}
}

function isMinute(minVal) {
	if (isNaN(minVal))
		{
		if (debugValidation) alert("minVal is Non a Number, it is: " + minVal);
		return false;
		}
	else if ((eval(minVal) < 0) || (eval(minVal) > 59) || (minVal.length == 0))
		{
		if (debugValidation) alert("minVal is a number, but invalid. It is: " + minVal);
		return false;
		}
	else
		{
		return true;
		}
}



function isEmail(emailStr) {
	/*
	This script and many more are available free online at
	The JavaScript Source!! http://javascript.internet.com
	*/
	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */
	var checkTLD=1;

	/* The following is the list of known TLDs that an e-mail address must end with. */
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	var emailPat=/^(.+)@(.+)$/;

	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	var validChars="\[^\\s" + specialChars + "\]";

	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")";

	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

	/* The following string represents an atom (basically a series of non-special characters.) */
	var atom=validChars + '+';

	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")";

	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	/* Finally, let's start trying to figure out if the supplied address is valid. */
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		//alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			//alert("Ths username contains invalid characters.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			//alert("Ths domain name contains invalid characters.");
			return false;
		}
	}

	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		// alert("The username doesn't seem to be valid.");
		return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				// alert("Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}

	// Domain is symbolic name.  Check if it's valid.
	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) {
			// alert("The domain name does not seem to be valid.");
			return false;
		}
	}

	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		// alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
		// alert("This address is missing a hostname!");
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}



