function validateText(text, bSilent) {
	if ((text != "") && ((text.indexOf("http://") >= 0) || (text.indexOf("https://") >= 0))) {
		if (!bSilent) { alert("Embedded html links are not allowed in text fields."); }
		if (!bSilent) { alert("Information not saved."); }
		return false;
	}
	return true;
}
	
function validateEmail(text, bSilent) {
	if (text != "") {
		var idxAtSymbol = text.indexOf("@");
		var iChars = "*|,\":<>[]{}`'; ()&$#%";
		for (var i=0; i < text.length; i++) {
			if (iChars.indexOf(text.charAt(i)) != -1) {
				if ((text.charAt(i) == "'") && (i < idxAtSymbol)) {
					//allow single quote in name portion of email address but not in provider portion
				} else {
					if (!bSilent) { alert("The email address entered, " + text + ", contains invalid characters and could not be sent."); }
					return false;
				}
		     }
		 }
		 if ((idxAtSymbol == -1) || (text.indexOf(".") == -1)) {
			if (!bSilent) { alert("The email address entered, " + text + ", is invalid.\n\nEmail addresses must be in the form 'name@provider.ext'"); }
			return false;
		}
	}
	return validateText(text, bSilent);
}

function validateURL(text, bSilent) {
	if (text != "") {
		if (text.indexOf("://") < 0) {
			if (!bSilent) { alert("The URL, \"" + text + "\", is invalid because it does not contain a protocol (such as \"http://\")."); }
			return false;
		}
	}
	return true;
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function validateDate(textDate, bSilent, eleName){
        var dtCh= "/";
        var minYear=1900;
        var maxYear=2100;
	var strDate = textDate;
	var daysInMonth = DaysArray(12);
	var pos1=strDate.indexOf(dtCh);
	var pos2=strDate.indexOf(dtCh,pos1+1);
	var strMonth=strDate.substring(0,pos1);
	var strDay=strDate.substring(pos1+1,pos2);
	var strYear=strDate.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	var month=parseInt(strMonth);
	var day=parseInt(strDay);
	var year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		if (!bSilent) alert("The "+(!eleName||eleName==""?"":eleName+" ")+"date format should be : mm/dd/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		if (!bSilent) alert("Please enter a valid month"+(!eleName||eleName==""?"":" for the "+eleName));
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		if (!bSilent) alert("Please enter a valid day"+(!eleName||eleName==""?"":" for the "+eleName));
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		if (!bSilent) alert("Please enter a valid 4 digit year"+(!eleName||eleName==""?"":" for the "+eleName));
		return false;
	}
	if (strDate.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(strDate, dtCh))==false){
		if (!bSilent) alert("Please enter a valid date"+(!eleName||eleName==""?"":" for the "+eleName));
		return false;
	}
        return true;
}

function validateInputDate(textFrom, textTo, bSilent) {
	var from = new Date(textFrom);
	var to	 = new Date(textTo);
	//Check Month and Day for any number out of range
	if ((textFrom.indexOf("/") > 0) && (textTo.indexOf("/") > 0)) {
		// FROM Date
		var date = textFrom;
		var year = "";
		var month = "";
		var day = "";
		year = textFrom.substring(textFrom.lastIndexOf("/")+1);
		if (year < 100) {
			if (!bSilent) alert("Please use a 4-digit year: 'mm/dd/yyyy'.");
			return false;
		}
		month = date.substring(0, date.indexOf("/"));			
		if ((month <= 0) || (month > 12)) {
			if (!bSilent) alert("The 'From' date contains an invalid month value.");
			return false;
		}
		day = date.substring(date.lastIndexOf("/"), date.indexOf("/")+1);
		if ((day <= 0) || (day > 31)) {
			if (!bSilent) alert("The 'From' date contains an invalid day value.");
			return false;
		}
		// TO Date
		date = textTo;
		year = textTo.substring(textTo.lastIndexOf("/")+1);
		if (year < 100) {
			if (!bSilent) alert("Please use a 4-digit year: 'mm/dd/yyyy'.");
			return false;
		}
		month = date.substring(0, date.indexOf("/"));			
		if ((month <= 0) || (month > 12)) {
			if (!bSilent) alert("The 'To' date contains and invalid month value.");
			return false;
		}
		day = date.substring(date.lastIndexOf("/"), date.indexOf("/")+1);
		if ((day <= 0) || (day > 31)) {
			if (!bSilent) alert("The 'To' date contains an invalid day value.");
			return false;
		}
	} else {
		if (!bSilent) alert("Please enter date in the form 'mm/dd/yyyy'.");
		return false;
	}
	//Check if the from date is less then the to date
	if (from <= to) {
		return true;
	} else {
		if (!bSilent) alert("The 'From' date must be equal to or before the 'To' date.");
		return false;
	}
}

/*
Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com)
Changes:
1.1.4: Fixed a bug where upper ASCII characters (i.e. accented letters
international characters) were allowed.

1.1.3: Added the restriction to only accept addresses ending in two
letters (interpreted to be a country code) or one of the known
TLDs (com, net, org, edu, int, mil, gov, arpa), including the
new ones (biz, aero, name, coop, info, pro, museum).  One can
easily update the list (if ICANN adds even more TLDs in the
future) by updating the knownDomsPat variable near the
top of the function.  Also, I added a variable at the top
of the function that determines whether or not TLDs should be
checked at all.  This is good if you are using this function
internally (i.e. intranet site) where hostnames don't have to 
conform to W3C standards and thus internal organization e-mail
addresses don't have to either.
Changed some of the logic so that the function will work properly
with Netscape 6.

1.1.2: Fixed a bug where trailing . in e-mail address was passing
(the bug is actually in the weak regexp engine of the browser; I
simplified the regexps to make it work).

1.1.1: Removed restriction that countries must be preceded by a domain,
so abc@host.uk is now legal.  However, there's still the 
restriction that an address must end in a two or three letter
word.

1.1: Rewrote most of the function to conform more closely to RFC 822.

1.0: Original  */
function validateEmailStrict(emailStr, bSilent) {
	var checkTLD	=1;													//verify that address ends in a two-letter country or well-known TLD
	var knownDomsPat=/^(aero|arpa|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel)$/;	//ICANN TLDs
	var emailPat	=/^(.+)@(.+)$/;										//check if the entered e-mail address fits the user@domain format
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";					//special characters NOT ALLOWED in the address. 
	var validChars	="\[^\\s" + specialChars + "\]";					//The range of characters allowed in a username or domainname.  It really states which chars aren't allowed.
	var quotedUser	="(\"[^\"]*\")";									//Applies if the "user" is a quoted string E.g. "jiminy cricket"@disney.com is legal
	var ipDomainPat	=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;//Applies for domains that are IP addresses, rather than symbolic names.  E.g. joe@[123.124.233.4] is legal
	var atom		=validChars + '+';									// String that represents an atom (basically a series of non-special characters.)
/* disallow quotedUser (to eliminate the use of double quotes which causes problems in EmailQueue)
	var word		="(" + atom + "|" + quotedUser + ")";				// String that represents one word in the typical username. For example, in john.doe@somewhere.com, john and doe are words.
*/
	var word		="(" + atom + ")";									// String that represents one word in the typical username. For example, in john.doe@somewhere.com, john and doe are words.
	var userPat		=new RegExp("^" + word + "(\\." + word + ")*$");	// The following pattern describes the structure of the user
	var domainPat	=new RegExp("^" + atom + "(\\." + atom +")*$");		// The following pattern describes the structure of a normal symbolic domain, as opposed to ipDomainPat

	// 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.
		if (!bSilent) alert("The email address entered, " + emailStr + ", is invalid.\n\nEmail addresses must be in the form 'name@provider.ext'");	//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) {
			if (!bSilent) alert("The email address username entered, " + user + ", contains invalid characters.");	//alert("Ths username contains invalid characters.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			if (!bSilent) alert("The email address domain name entered, " + domain + ", contains invalid characters.");	//alert("Ths domain name contains invalid characters.");
			return false;
	   }
	}

	if (user.match(userPat)==null) {  // user is not valid
		if (!bSilent) alert("The email address username entered, " + user + ", appears to be invalid.");	//alert("The username doesn't seem to be valid.");
		return false;
	}

/*disallow IP domains (to eliminate the use of square brackets which causes problems in EmailQueue)
	// 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) {
				if (!bSilent) alert("The Destination IP addres entered, " + IPArray + ", is invalid.");	//alert("The username doesn't seem to be valid.");
				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) {
			if (!bSilent) alert("The email address domain name entered, " + domArr + ", appears to be invalid.");	//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) {
		if (!bSilent) alert("The address must end in a known domain (.com, .org, etc.) or two letter country (.ca, .uk, etc).");
		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) {
		if (!bSilent) alert("The email address domain entered, " + emailStr + " does not have a hostname.  The address is invalid.");
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}

