
/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
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));
}

 function isNumeric(str)
// returns true if str is numeric
// that is it contains only the digits 0-9
// returns false otherwise
// returns false if empty
{
  var len= str.length;
  if (len==0)
    return false;
  //else
  var p=0;
  var ok= true;
  var ch= "";
  while (ok && p<len)
  {
    ch= str.charAt(p);
    if (('0'<=ch && ch<='9')||ch=="."||ch=="-")
      p++;
    else
      ok= false;
  }
  return ok;
}

/*
===================================================================
formatFilter(form, format, len, name)

form 	= document.formName.elementName
format 	= Use '#' to represent numbers, any char for separators. (EG. (###)###-#### or ###-##-#### )
len		= number of DIGITS that are required (Do not count separators or grouping symbols)
name	= The 'name' of the element. This is for human eyes ... not the ELEMENT name.

Recommended usage:
<input type="text" name="PHONE_NUM" onBlur="javascript:formatFilter(this, '(###)###-####', 10, Phone Number);">
===================================================================
*/
function formatFilter(form, format, len, name) {

	var input = form.value;

	if(input.length > 0) { //do not perform if empty input

		var numbers = ""; //store all the numbers here

		//process to remove non-numbers and spaces
		for(var i = 0; i < input.length; i++) {
			var c_char = input.charAt(i);
			if(!(isNaN(c_char) || c_char == " ")) numbers += c_char;
		}

		if (numbers.length < len)
		{
			alert(name+" must be "+len+" digits");
			form.focus();
		}
		
		var output = ""; //assign numbers here

		//assign numbers to chosen format
		var n = 0, i = 0;
		while(i < format.length && n < numbers.length) {
			var c_char = format.charAt(i);
			if(c_char == "#") {
				output += numbers.charAt(n++)
			} else {
				output += c_char;
			}
			i++;
		}

		form.value = output; //output to form
	}
}


//Pattern Match for valid email address
function validateEmail(theEmailAddr) {
	var pat_email = /^[-._&0-9a-zA-Z]+[@][-._&0-9a-zA-Z]+[.][._0-9a-zA-Z]+[a-zA-Z]$/;
	
	if ( pat_email.exec(theEmailAddr) == null ) {	
		return false;
	}
   	return true;
}

function stringFilter (input, filter) {
	s = input.value;
	filteredValues = filter;     // Characters stripped out
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
	var c = s.charAt(i);
	if (filteredValues.indexOf(c) == -1) returnString += c;
	}
	input.value = returnString;
}

//LUHN CHECK CC Num.
function luhnCheckCC(s) {
  var i, n, c, r, t;
  // First, reverse the string and remove any non-numeric characters.
  r = "";
  for (i = 0; i < s.length; i++) {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9)
      r = c + r;
  }
  // Check for a bad string.
  if (r.length <= 1)
    return false;
  // Now run through each single digit to create a new string. Even digits
  // are multiplied by two, odd digits are left alone.
  t = "";
  for (i = 0; i < r.length; i++) {
    c = parseInt(r.charAt(i), 10);
    if (i % 2 != 0)
      c *= 2;
    t = t + c;
  }
  // Finally, add up all the single digits in this string.
  n = 0;
  for (i = 0; i < t.length; i++) {
    c = parseInt(t.charAt(i), 10);
    n = n + c;
  }
  // If the resulting sum is an even multiple of ten (but not zero), the
  // card number is good.
  if (n != 0 && n % 10 == 0)
    return true;
  else
    return false;
}

function validateCCExpDate(mm, yy) {
	timeisit=new Date();
	realmonth=timeisit.getMonth();
	realmonth++;
	realyear=(timeisit.getYear()%100);
	
	expmonth = mm;
	expyear = yy;
	
	expmonth++;expmonth--;
	expyear++;expyear--;
	if(expyear < realyear) {			
		return false;
	}
	
	if((expmonth < realmonth) && (expyear == realyear)) {		
		return false;
	}	
	return true;
}
