function isWhitespace(str)
{
  try
  {
    var regex = /^\s*$/;
    return regex.test(str);
  }
  catch(er)
  {
    return true;
  }

}

function validateEmail(str)
{
  try
  {
    var regex = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    return regex.test(str);
  }
  catch(er)
  {
    return true;
  }
}


function validateUSPhone(str) {
  var regex = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
  return regex.test(str);
}

function isDate (value)
{
  return (!isNaN (new Date (value).getYear () ) ) ;
}

function isMoney(value)
{
  var m = parseFloat(value);
  if (isNaN(m)) return false;
  if (Math.round(m * 100) / 100 != m) return false;
  return true;
}

function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function isDecimal(str)
{
  return (str.search(/^([0-9]+\.)?[0-9]+$/) != -1);
}

function isInteger(str)
{
	if (!isNumeric(str)) return false;
  return (str.search(/^\-?[0-9]+$/) != -1);
}

function isPositiveInteger(str)
{
  if (!isNumeric(str)) return false;
  return (str.search(/^[0-9]+$/) != -1);
}

function isHref(href)
{
  try
  {
    var regex = /^http(s)?:\/\/((\d+\.\d+\.\d+\.\d+)|(([\w-]+\.)+([a-z,A-Z][\w-]*)))(:[1-9][0-9]*)?(\/([\w-.\/:%+@&=]+[\w- .\/?:%+@&=]*)?)?(#(.*))?$/i;
    return regex.test(href);
  }
  catch(er)
  {
    return true;
  }
}

/*  ================================================================
    Credit card verification functions
    Originally included as Starter Application 1.0.0 in LivePayment.
    20 Feb 1997 modified by egk:
           changed naming convention to initial lowercase
                  (isMasterCard instead of IsMasterCard, etc.)
           changed isCC to isCreditCard
           retained functions named with older conventions from
                  LivePayment as stub functions for backward
                  compatibility only
           added "AMERICANEXPRESS" as equivalent of "AMEX"
                  for naming consistency
    ================================================================ */


/*  ================================================================
    FUNCTION:  isCreditCard(st)

    INPUT:     st - a string representing a credit card number

    RETURNS:  true, if the credit card number passes the Luhn Mod-10
		    test.
	      false, otherwise
    ================================================================ */

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()



/*  ================================================================
    FUNCTION:  isVisa()

    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid VISA number.

	      false, otherwise

    Sample number: 4111 1111 1111 1111 (16 digits)
    ================================================================ */

function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}  // END FUNCTION isVisa()




/*  ================================================================
    FUNCTION:  isMasterCard()

    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid MasterCard
		    number.

	      false, otherwise

    Sample number: 5500 0000 0000 0004 (16 digits)
    ================================================================ */

function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isMasterCard()





/*  ================================================================
    FUNCTION:  isAmericanExpress()

    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid American
		    Express number.

	      false, otherwise

    Sample number: 340000000000009 (15 digits)
    ================================================================ */

function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isAmericanExpress()


/*  ================================================================
    FUNCTION:  isDiscover()

    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Discover
		    card number.

	      false, otherwise

    Sample number: 6011000000000004 (16 digits)
    ================================================================ */

function isDiscover(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) && (first4digs == "6011"))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isDiscover()



function validateGenericForm(form)
{
  var continueValidation = true;
  $('*:input[rel~="required"]', form).each(function () {
    if ((this.type == 'radio') || (this.type == 'checkbox'))
    {
      if ($('input:' + this.type + '[name="' + this.name + '"]:checked').size() == 0)
      {
        alert($(this).attr('title') + ' is required.');
        $(this).focus();
        return continueValidation = false;
      }
    } else
    if (isWhitespace($(this).val()) || ($(this).val() == null))
    {
      alert($(this).attr('title') + ' is required.');
      $(this).focus();
      return continueValidation = false;
    }
  });

  if (!continueValidation) return false;
	
  $('*[rel~="+positiveinteger"]', form).each(function () {
    var v = $(this).val();
    var i = parseInt(v);
    if (isNaN(i))
    {
      alert($(this).attr('title') + ' is required to be a valid number.');
      $(this).focus();
      return continueValidation = false;
    }
    if (i <= 0)
    {
      alert($(this).attr('title') + ' is required to be greater than 0.');
      $(this).focus();
      return continueValidation = false;
    }
  });
	
  if (!continueValidation) return false;

  $('*[rel~="+integer"]', form).each(function () {
    var v = $(this).val();
    var i = parseInt(v);
    if (isNaN(i))
    {
      alert($(this).attr('title') + ' is required to be a valid number.');
      $(this).focus();
      return continueValidation = false;
    }
    if (i < 0)
    {
      alert($(this).attr('title') + ' is required to be greater than or equal to 0.');
      $(this).focus();
      return continueValidation = false;
    }
  });

  if (!continueValidation) return false;

  $('*[rel~="email"]', form).each(function () {
    var v = $(this).val();
    if (!validateEmail(v) && !(isWhitespace(v)))
    {
      alert($(this).attr('title') + ' is required to be a valid email address.');
      $(this).focus();
      return continueValidation = false;
    }
  });

  if (!continueValidation) return false;

  $('*[rel~="date"]', form).each(function () {
    var v = $(this).val();
    if (!isDate(v) && !(isWhitespace(v)))
    {
      alert($(this).attr('title') + ' is required to be a valid date.');
      $(this).focus();
      return continueValidation = false;
    }
  });

  if (!continueValidation) return false;

  $('*[rel~="href"]', form).each(function () {
    var v = $(this).val();
    if (!isHref(v) && !(isWhitespace(v)))
    {
      alert($(this).attr('title') + ' is required to be a valid Website Address.\n\nInclude http://');
      $(this).focus();
      return continueValidation = false;
    }
  });
	
	if (!continueValidation) return false;

  $('*[rel~="phone"]', form).each(function () {
    var v = $(this).val();
    if (!validateUSPhone(v) && !(isWhitespace(v)))
    {
      alert($(this).attr('title') + ' is required to be a valid US Phone Number.\n\nFormat: XXX-XXX-XXXX');
      $(this).focus();
      return continueValidation = false;
    }
  });

  return continueValidation;
}
