// COMMON VALIDATION / FORMATTING ROUTINES:
//
// isEmpty(strData)											Returns true if strData is an empty string.
// isWhitespace(strData)								True if string strData is empty or whitespace.
// isLetter(strData)										True if string strData is all English letters. 
// isDigit(strData)											True if string strData is all digits.
// isRealNumber(strData)								True if string strData is all digits with or without a '.'.
// isDigitWithDecimal(strData)					True if string strData is digit with less than one Period
// isAlphabeticWithWhiteSpace(strData)  True if string strData is all letters and spaces.
// isAlphanumeric (strData)							True if string strData is all letters and numbers only.
// stripToNumbers(strData)							Returns strData with all non-digits removed.
// trimString
// ctrlExists
// getSearchParameter
// Rounder
// checkNumber													para: withDecimal : True/False

var whitespace = " \t\n\r";
var tmkSeparator = ":TMKSEPARATOR:";
				
function isEmpty(strData) {
	return ((strData == null) || (strData.length == 0));
}
		
function isWhitespace(strData) {
	var iCtr, cTemp;

	if (isEmpty(strData))
		return true;

  for (iCtr = 0; iCtr < strData.length; iCtr++) {   
		var cTemp = strData.charAt(iCtr);

		if (whitespace.indexOf(cTemp) == -1)
			return false;
  }
  return true;
}
		
function isLetter(strData) {
	for (iCtr = 0; iCtr < strData.length; iCtr++) {
		cDigit = strData.charAt(iCtr);
	  if (! (((cDigit >= "a") && (cDigit <= "z")) || ((cDigit >= "A") && (cDigit <= "Z"))))
		return false;
	}
	return true;
}

function CheckDigit(sender, args) {
	if (args.Value==null) {
		args.IsValid = false;
		return;
	}
	for (iCtr = 0; iCtr < args.Value.length; iCtr++) {
		cDigit = args.Value.charAt(iCtr);
	  if ((cDigit < "0") || (cDigit > "9")) {
			args.IsValid = false;
			return;
		}
	}
	
	args.IsValid = true;
	return;
}

function isDigit(strData) {
	if (strData==null) return false;
	for (iCtr = 0; iCtr < strData.length; iCtr++) {
		cDigit = strData.charAt(iCtr);
	  if ((cDigit < "0") || (cDigit > "9"))
			return false;
	}
	return true;
}

function isRealNumber(strData) {
	if (strData==null) return false;
	var bPeriod = false;
	for (i = 0; i < strData.length; i++) {
		cDigit = strData.charAt(i);
	  if ((cDigit < "0") || (cDigit > "9")) {
			if (cDigit!=".") {
				if (! ((i==0) && (cDigit=="-"))) {
					return false;
				}
			}
			else {
				if (! bPeriod)
					bPeriod = true;
				else
					return false;
			}
		}
	}
	return true;
}
	
function isDigitWithDecimal(strData) {
	var bPeriod = false;
	if (strData==null) return false;
	for (iCtr = 0; iCtr < strData.length; iCtr++) {
		cChar = strData.charAt(iCtr);
		if (cChar == ".") {
			if (bPeriod) 
				return false;
			else
				bPeriod = true;
		}
	  else if ((cChar < "0") || (cChar > "9"))
			return false;
	}
	return true;
}

function isAlphabeticWithWhiteSpace(strData) {
	var iCtr, cTemp;

	if (isEmpty(strData)) 
		if (isAlphabeticWithWhiteSpace.arguments.length == 1)
			return false;
	else
		return (isAlphabeticWithWhiteSpace.arguments[1] == true);

	for (iCtr = 0; iCtr < strData.length; iCtr++) {
		cTemp = strData.charAt(iCtr);
    if (! isLetter(cTemp) && ! isWhitespace(cTemp))
			return false;
  }
    
  return true;
}

function isAlphanumeric(strData) {
	var iCtr,	cTemp;

	if (isEmpty(strData)) 
		if (isAlphanumeric.arguments.length == 1)
			return false;
	else
		return (isAlphanumeric.arguments[1] == true);

	for (iCtr = 0; iCtr < strData.length; iCtr++) {
		cTemp = strData.charAt(iCtr);
		if (! (isLetter(cTemp) || isDigit(cTemp)))
			return false;
  }

   return true;
}

function stripToNumbers(strData) {
	var iCtr, cDigit, strNumber;

	if (isEmpty(strData))
		return;
					
	strNumber = "";

	for (iCtr = 0; iCtr < strData.length; iCtr++) {
		cDigit = strData.charAt(iCtr);
	  if (isDigit(cDigit))
			strNumber += cDigit;
	}			
	return strNumber;
}

function CInt(Num) {
		if (Num == null) return null;
		if (!isNaN(Num)) return Num;
		var re1= /^0*/;
		Num = Num.replace(re1,"");
		return parseInt(Num);
}

function getSearchParameter(parameter, def)
{
	//	This method checks the search parameters at the end of the url,
	//	and returns a value if found, or a default value if not found.
	//	this enables information to be passed to the page via url
  
  url = new String(top.location)
  search = new String(url.substring(url.indexOf("?") + 1, url.length))
  search = search.split("&")
  for (i = 0; i < search.length; i++)
  {
	pair = new String(search[i])
	index = pair.indexOf("=")
	if (index > 0 && index < pair.length)
	{
		check = pair.substring(0, index)
		if (parameter == check)
			return pair.substring(index + 1, pair.length)
	}
  }
  return def
}

function trimString(theStr) {
  if (!theStr) theStr = "";  //ensure its not null
  theStr = theStr.replace(/^\s*/,""); //strip leading
  theStr = theStr.replace(/\s+$/,""); //strip trailing
  return theStr;
}

function ctrlExists(searchForm, ctrlName) {
	var i = 0;

	// how many elements are there in this form
    var numElements = searchForm.elements.length;
	var foundCtrl = false;

	// loop through until we either find our control or
	// run out of elements
	while ((i < numElements) && (!(foundCtrl))) {
		foundCtrl = (searchForm.elements[i].name == ctrlName);
		i++;
	}

	return foundCtrl;
}

function ascii_value(c)
{
   // restrict input to a single character
   c = c . charAt (0);
        
   // loop through all possible ASCII values
   var i;
   for (i = 0; i < 256; ++ i)
   {
	// convert i into a 2-digit hex string
      var h = i . toString (16);
      if (h . length == 1)
         h = "0" + h;
                        
         // insert a % character into the string
         h = "%" + h;
                
         // determine the character represented by the escape code
         h = unescape (h);
                
         // if the characters match, we've found the ASCII value
         if (h == c)
         	break;
   }
   return i;
}

function Rounder(val,decs) {
	var multiplier = 10
	for (var i = 1; i < decs; ++i)
	  multiplier = multiplier * 10
	if (decs==0)
	  return Math.round(val)
	else 
		return PadDecimal((Math.round(val * multiplier))/multiplier, decs)
}

function PadDecimal(val, decs) {
  var strVal = val.toString()
  var decimal = strVal.indexOf(".")

  if (decimal == -1) {
    wholeLength = strVal.length
    strVal += "."
  }
  else
    wholeLength = decimal

  newLength = wholeLength + eval(decs) + 1
  if (newLength > strVal.length) {
    var padTotal = newLength - strVal.length
    for (var i = 1; i <= padTotal; i++) 
			strVal += "0"
    }
  return strVal
}

function checkNumber(withDecimal) {
	// set keycode to 0 if no 0-9 or return 
	if ((window.event.keyCode < 48) || (window.event.keyCode > 57)) {
		if (withDecimal) {
			if (window.event.keyCode!=46) { window.event.keyCode=0; }
		}
		else if (window.event.keyCode!=13) { window.event.keyCode=0; }
	}
}
