// JavaScript Document
function isNumeric(string, ignoreWhiteSpace) 
{
	if (string.search) 
	{
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
	}
	return true;
}

function isAlphaNumeric(string, ignoreWhiteSpace) 
{
	if (string.search) 
	{
		if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) return false;
	}
	return true;
}

function isEmail(address) 
{
	if (address != '' && address.search) 
	{
		if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
		else return false;
	}
	var domain = address.substring(address.indexOf('@') + 1);
	if (domain.indexOf('.') == -1) return false;
	if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) return false;
	return true;
}

function isValidName(string)
{
	var re = /^([a-zA-Z])+$/;
	if(!string.match(re))
		return false;
	else
		return true;
}
function isURL(argvalue)
{
	if(argvalue.indexOf("http://")<0 && argvalue.indexOf("https://")<0)
	{
		argvalue = "http://" + argvalue
	}

	if (argvalue.indexOf(" ") != -1)
		return false;
	else if (argvalue == "http://")
	    return false;
	else if (argvalue.indexOf("http://") > 0)
		return false;
	argvalue = argvalue.substring(7, argvalue.length);

	if (argvalue.indexOf(".") == -1)
		return false;
	else if (argvalue.indexOf(".") == 0)
	    return false;
	else if (argvalue.charAt(argvalue.length - 1) == ".")
	    return false;
	
	if (argvalue.indexOf("/") != -1) 
	{
		argvalue = argvalue.substring(0, argvalue.indexOf("/"));
		if (argvalue.charAt(argvalue.length - 1) == ".")
		{
			return false;
		}			
	}

	if (argvalue.indexOf(":") != -1) 
	{
		if (argvalue.indexOf(":") == (argvalue.length - 1))
		{
			return false;
		}
	    else if (argvalue.charAt(argvalue.indexOf(":") + 1) == ".")
		{
			return false;
			argvalue = argvalue.substring(0, argvalue.indexOf(":"));
			if (argvalue.charAt(argvalue.length - 1) == ".")
			{
				return false;
			}
		}
	}
	return true;
}

function strtrim(inputString) 
{
	if (typeof inputString != "string") 
	{
		return inputString; 
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") 
	{
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ")
	{
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1)
	{
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; 
}

function autotab(original,destination)
{
	if(IgnoreKeys())
		return;
	
	if(document.selection.createRange().text.length != 0)
		return;

	if(original.getAttribute && original.value.length == original.getAttribute("maxlength"))
	{
		destination.focus()
	}
}

function IgnoreKeys()
{
	// Ignore Home and End keys
	if(event.keyCode == 35 || event.keyCode == 36) //home and end
		return true;

	if(event.keyCode == 9) // Shift tab
		return true;
		
	if(event.keyCode == 37 || event.keyCode == 38 || 
		event.keyCode == 39 || event.keyCode == 40) // Arrow keys
		return true;
		
	if(event.keyCode == 46) //Delete
		return true;
		
	if(event.keyCode == 16) // Shift
		return true;				
		 	
	if(event.keyCode == 8) // Back
		return true;				

	return false;			

}
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

