var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r";
var decimalPointDelimiter = "."

var globalErrMsg = "";
var firstErrField = null;
var errPrefixMissingRequired = "Please enter value for required field: " ;
var errInvalidEmail = "Please enter an appropriate email address like user@company.com";
var errInvalidPhoneNum = "Please enter an appropriate Phone Number.";
var pEntryPrompt = "Please enter ";

var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;

function doCheckForNumber()
{
	if ((window.event.keyCode < 48 || window.event.keyCode > 57) && window.event.keyCode != 8 && window.event.keyCode != 46) 
		window.event.keyCode = 0;
}

function isEmpty(s)
{   
	return ((s == null) || (s.length == 0));
}

function isWhitespace (s)
{   
	var i;
	
	if (isEmpty(s)) return true;
	
	for (i = 0; i < s.length; i++)
	{ 
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	return true;
}

function isLetter (c)
{   
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
}

function isDigit (c)
{  
	return ((c >= "0") && (c <= "9"));
}

function isLetterOrDigit (c)
{   
	return (isLetter(c) || isDigit(c));
}

function isInteger (s)
{   
	var i;
	
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}
	return true;
}

function isFloat (s)
{   
	var i;
	var seenDecimalPoint = false;
	
	if (s == decimalPointDelimiter) return false;
	
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
		else if (!isDigit(c)) return false;
	}
	return true;
}

function isAlphabetic (s)
{   
	var i;
	for (i = 0; i < s.length; i++)
	{
 		var c = s.charAt(i);
 		if (!isLetter(c)) return false;
	}
	return true;
}

function isAlphanumeric (s)
{   
	var i;
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (! (isLetter(c) || isDigit(c) ) ) return false;
	}
	return true;
}

function stripCharsInBag(s, bag)
{   
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function checkInternationalPhone(strPhone)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function checkPhoneNum (theField,sName) 
{
	if ((theField.value==null)||(theField.value==""))
	{
		buildErrMsg(errPrefixMissingRequired + sName + ".");
		return;
	}
	if (checkInternationalPhone(theField.value)==false)
	{
		buildErrMsg(sName + " is a phone number. "+ errInvalidPhoneNum);
		return;
	}
	return;
}

function checkRequiredString (theField,sName) 
{
	if (isWhitespace(theField.value)) 
	{
		buildErrMsg(errPrefixMissingRequired + sName + ".");
		if (firstErrField == null) firstErrField = theField;
	}
}

function checkNumericField(theField,sName) 
{
	if (!isInteger(theField.value)) 
		buildErrMsg("Entered Value for " + sName + " should be numeric only.");
}

function checkRequiredDropDown (theField,sName) 
{ 
	for (var i=1; i <  theField.length; i++) 
		if ((theField.options[i].selected == true)) 
		{
			if ((theField.options[0].selected == true)) 
				theField.options[0].selected = false;
	    return;
		}
	buildErrMsg(errPrefixMissingRequired + sName + ".");  
}

function checkNotRequiredDropDown (theField,sName) 
{ 
	if ((theField.options[0].selected == true)) 
		theField.options[0].selected = false;
  return;
}

function checkDate (theField,sName,nMonth,nDay,nYear) 
{  
	if (nMonth == 1 || nMonth == 3 || nMonth == 5 || nMonth == 7 || nMonth == 8 || nMonth == 10 || nMonth == 12) 
	{
		if (nDay < 1 || nDay > 31) 
		{
			buildErrMsg(sName + " is invalid. Please re-enter. ");
	   	if (firstErrField == null) firstErrField = theField;
		}
	}
	
	if (nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11) 
	{
		if (nDay < 1 || nDay > 30) 
		{
			buildErrMsg(sName + " is invalid. Please re-enter. ");
	   	if (firstErrField == null) firstErrField = theField;
		}
	}
	
	if (nMonth == 2 ) 
	{
		if (nYear % 4 == 0) 
		{
			if (nDay < 1 || nDay > 29) 
			{
				buildErrMsg(sName + " is invalid. Please re-enter. ");
		   	if (firstErrField == null) firstErrField = theField;
			}
		}
		else
		{
			if (nDay < 1 || nDay > 28) 
			{
				buildErrMsg(sName + " is invalid. Please re-enter. ");
		   	if (firstErrField == null) firstErrField = theField;
			}
		}
	}
}

function buildErrMsg(newErrMsg) 
{
	if (globalErrMsg != "") 
		globalErrMsg += "\n";
	globalErrMsg += newErrMsg;
}

function checkDisplayError() 
{
	if (globalErrMsg == "") 
		return true;
  else 
	{
		window.alert(globalErrMsg);
		if (firstErrField != null) 
		{
			firstErrField.focus();
    	firstErrField.select();
		}
		return false;
	}  
}

function ValueInArray(arrArray,sValue) 
{
	for (var i = 0; i < arrArray.length; i++)
		if (arrArray[i] == sValue) 
			return true;
	return false;
}

function PrintArray(arrArray)
{
	for (var i = 0; i < arrArray.length; i++)
		alert(arrArray[i]);
}

function promptEntry (s)
{   
	window.status = pEntryPrompt + s;
}

function ClearForm(theForm) 
{
	for (var i = 0; i < theForm.length; i++)
	{
		var theElement = theForm.elements[i];
		
		if (theElement.type == "text" || theElement.type == "hidden") 
			theElement.value = "";
		if (theElement.type == "select-one") 
		{
			for (var j = 0; j < theElement.length; j++) 
				theElement.options[j].selected = false;
			theElement.options[0].selected = true;
			theElement.options[0].selected = false;
		}
		
		if (theElement.type == "select-multiple") 
			for (var j = 0; j < theElement.length; j++) 
				theElement.options[j].selected = false;
		
		if (theElement.type == "radio") 
			if (theElement.defaultChecked == true) 
				theElement.checked = true;
			else 
				theElement.checked = false;
	}
	return true;
}

function checkEmail (theField,sName) 
{  
	if (!isEmail(theField.value)) 
	{
		buildErrMsg(sName + " is not an email address. "+ errInvalidEmail);
		if (firstErrField == null) firstErrField = theField;
	}
}

function isEmail(EmailAddress)
{
	EmailAddress = Trim(EmailAddress);
  var emailLength = EmailAddress.length;
	if (emailLength > 0) 
	{
		var i, result=true;
    for (i=1; i<emailLength; i++) 
		{
			var hereNow = EmailAddress.charAt(i);
			if (hereNow == "@") 
			{
				var atSign = "good";
				var atPos = i;
			} 
			else if (hereNow == ".") 
			{
				var periodSign = "good";
				var periodPos = i;
			}                       
    }
		if (atSign != "good"  && periodSign != "good") 
			result = false;
		else if (atSign != "good") 
			result = false;
		else if (periodSign != "good") 
			result = false;
		if (emailLength < 5 || atPos < 0 || periodPos < 3) 
			result = false;
		if (emailLength <= periodPos+1) 
			result = false;
		if ((periodPos-atPos) == 1) 
			result = false;
	}
	return result;
}

function LTrim(str)
/**********************************************************************
PURPOSE: Remove leading blanks from our string.
IN: str - the string we want to LTrim

RETVAL: An LTrimmed string!
**********************************************************************/
{
	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;
}

function RTrim(str)
/**********************************************************************
PURPOSE: Remove trailing blanks from our string.
IN: str - the string we want to RTrim

RETVAL: An RTrimmed string!
**********************************************************************/
{
	// 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;
}

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));
}


function Len(str)
/**********************************************************************
IN: str - the string whose length we are interested in

RETVAL: The number of characters in the string
**********************************************************************/
{  
	return String(str).length;  
}

function Left(str, n)
/**********************************************************************
IN: str - the string we are LEFTing
    n - the number of characters we want to return

RETVAL: n characters from the left side of the string
**********************************************************************/
{
	if (n <= 0)     // Invalid bound, return blank string
		return "";
	else if (n > String(str).length)   // Invalid bound, return
		return str;                // entire string
	else // Valid bound, return appropriate substring
		return String(str).substring(0,n);
}

function Right(str, n)
/**********************************************************************
IN: str - the string we are RIGHTing
    n - the number of characters we want to return

RETVAL: n characters from the right side of the string
**********************************************************************/
{
	if (n <= 0)     // Invalid bound, return blank string
		return "";
	else if (n > String(str).length)   // Invalid bound, return
		return str;                     // entire string
	else 
	{ // Valid bound, return appropriate substring
		var iLen = String(str).length;
		return String(str).substring(iLen - n);
	}
}

function Mid(str, start, len)
/**********************************************************************
IN: str - the string we are LEFTing
    start - our string's starting position (0 based!!)
    len - how many characters from start we want to get

RETVAL: The substring from start to start+len
**********************************************************************/
{
	// Make sure start and len are within proper bounds
	if (start < 0 || len < 0) return "";
	
	var iEnd, iLen = String(str).length;
	if (start + len > iLen)
		iEnd = iLen;
	else
		iEnd = start + len;
	
	return String(str).substring(start,iEnd);
}

function SetGridPosition() {
  try {
    document.all["SCROLLPOSITIONX"].value = document.all["panel1"].scrollLeft;
    document.all["SCROLLPOSITIONY"].value = document.all["panel1"].scrollTop;
  }
  catch (ex) {
  }
}

function GetGridPosition() {
  try {
    document.all["panel1"].scrollLeft = document.all["SCROLLPOSITIONX"].value;
    document.all["panel1"].scrollTop = document.all["SCROLLPOSITIONY"].value;
  }
  catch (ex) {
  }
}

