/*
**  Common javascript validations
**
**  How to include this file:
**
**  </head>
**  <script src="include/validations.js">
**  </script>
**
*/

var _console = null;
var _gDisplayAlert = true;
var _gAlertCounter = 0;

function debug(msg)
{
	if (_console == null || _console.closed)
	{
		_console = window.open("","console","width=600,height=300,scrollbars,resizable");
		_console.document.open("text/plain");
	}

	_console.focus();
	_console.document.writeln(msg);
}

/*
**  This function checks for an integer value made
**  up of only digits 0-9.
*/
function isInt (textObj)
{
   var newValue = textObj.value;
   var len = newValue.length;
   var c = "0";

   for (var i = 0; i < len; i++)
   {
     c = newValue.charAt(i);
     if (c < "0" || c > "9")
     {
	if (_gDisplayAlert)
		alert("This Field Contains Invalid Characters.\nOnly digits 0 through 9 are valid."); 
        return false;
     }	
   }
   
   return true;
}

/*
**  Check for a valid email format
**  Parameter: email is a string object
*/

function isValidEmail (email) 
{ 
   var emailStr = email.value;
   var checkTLD=0; 
   var knownDomsPat=/ ^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|us)$/; 
   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) 
   { 
      alert("The Email Address Is Invalid"); 
      return false; 
   } 

   var user=matchArray[1]; 
   var domain=matchArray[2]; 

   for (i=0; i < user.length; i++) 
   { 
      if (user.charCodeAt(i)>127) 
      { 
         alert("The Username Contains Invalid Characters."); 
         return false; 
      } 
   } 

   for (i=0; i < domain.length; i++) 
   { 
      if (domain.charCodeAt(i)>127) 
      { 
         alert("The Domain Name Contains Invalid Characters."); 
         return false; 
      } 
   } 

   if (user.match(userPat) == null) 
   { 
      alert("The Username Is Invalid."); 
      return false; 
   } 

   var IPArray=domain.match(ipDomainPat); 

   if (IPArray != null) 
   { 
      for (var i=1; i <= 4; i++) 
      { 
         if (IPArray > 255) 
	 { 
            alert("The Destination IP Address Is Invalid."); 
            return false; 
         } 
      } 

      return true; 
   } 

   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 Is Invalid."); 
         return false; 
      } 
   } 

   if (checkTLD && domArr[domArr.length-1].length != 2 && 
       domArr[domArr.length-1].search(knownDomsPat) == -1) 
      { 
          alert("The Domain Name Extension Is Invalid"); 
          return false; 
      } 

   if (len < 2) 
   { 
      alert("The Address Is Missing A Hostname."); 
      return false; 
   }

   return true;
} 

function isMoney(node)
{
	var str = node.value;
	var numPattern = /^\s*\d+(\.\d{2})?\s*$/;
	var moneyPattern = /^\s*\d{1,3}(,\d{3})*(\.\d{2})?\s*$/;

	if (node == null)
		return false;

	if (node.type != 'text')
		return false;

	if (str.length == 0)
		return true;

	if (str.match(numPattern) != null)
		return true;

	if (str.match(moneyPattern) != null)
	{
		str.replace(/,/g,"");
		node.value = str;
		return true;
	}

	if (_gDisplayAlert)
	{
		// if (_gAlertCounter == 0)
			alert("The number is not valid.");
		_gAlertCounter++;
	}
	node.focus();
	node.value = "";
	return false;
}

function isPercent(node)
{
	var str = node.value;
	var numPattern = /^\s*\d{1,3}(\.\d{0,3})?\s*$/;

	if (node == null)
		return false;

	if (node.type != 'text')
		return false;

	if (str.length == 0)
		return true;

	if (str.match(numPattern) != null)
		return true;

	if (_gDisplayAlert)
	{
		// if (_gAlertCounter == 0)
			alert("The number is not valid.");
		_gAlertCounter++;
	}
	node.focus();
	node.value = "";
	return false;
}

function isNumber(node)
{
	var str = node.value;
	var numPattern = /^\s*\d*(\.\d*)?\s*$/;

	if (node == null)
		return false;

	if (node.type != 'text')
		return false;

	if (str.length == 0)
		return true;

	if (str.match(numPattern) != null)
		return true;

	if (_gDisplayAlert)
	{
		alert("The number is not valid.");
	}
	node.focus();
	node.value = "";
	return false;
}

function verifyCancel()
{
	var msg = "Warning: This will cancel your session and return to the home page.\n";
	msg += "Do you want to continue?";

	return confirm(msg);
}

function verifyCheckbox(nodeList)
{
	for (var i=0; i < nodeList.length; i++)
	{
		if (nodeList[i].checked)
			return true;
	}
	return false;
}

function verifyPage(fields)
{
	var	spacePattern = /^\s*$/;
	_gDisplayAlert = false;

	// Verify that we have some fields to check
	if (!fields)
	{
		_gDisplayAlert = true;
		_gAlertCounter = 0;
		return true;
	}

	var elementArray = fields.split(',');

	if (elementArray.length == 0)
	{
		_gDisplayAlert = true;
		_gAlertCounter = 0;
		return true;
	}

	// For each named item, check that it has a value entered.
	for (var i=0; i < elementArray.length; i++)
	{
		var nodeName = elementArray[i];
		var nodeValue = "";
		var nodeList = document.getElementsByName(nodeName);
		if (nodeList.length > 0)
		{
			// Checkbox type is actually an array of checkboxes which may be checked/not checked
			if (nodeList[0].type == 'checkbox')
			{
				if (!verifyCheckbox(nodeList))
				{
					alert('You must check at least one (1) box');
					_gDisplayAlert = true;
					_gAlertCounter = 0;
					return false;
				}
				continue;
			}
			else if (nodeList[0].type == 'text' && nodeList[0].onblur != null)
			{
				if (!nodeList[0].onblur())
				{
					_gDisplayAlert = true;
					_gAlertCounter = 0;
					return false;
				}
			}

			nodeValue = nodeList[0].value;
		}
		if (nodeValue.length == 0 || nodeValue.match(spacePattern))
		{
			nodeList[0].focus();
			alert('This field is required');
			_gDisplayAlert = true;
			_gAlertCounter = 0;
			return false;
		}
	}

	_gAlertCounter = 0;
	_gDisplayAlert = true;
	return true;
}
