function isCreditCard(st) {
  st = Trim(st);
  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--;
  }
  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
} 


function isVisa(cc)
{
  cc = Trim(cc);
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}  


function isMasterCard(cc)
{
  cc = Trim(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;

}


function isAmericanExpress(cc)
{
  cc = Trim(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;

} 


function isDiscover(cc)
{
   cc = Trim(cc);
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) && (first4digs == "6011"))
    return isCreditCard(cc);
  return false;

} 

function isCardMatch (cardType, cardNumber)
{
   cardType = Trim(cardType);
   cardNumber = Trim(cardNumber);   
   cardType = cardType.toUpperCase();
   var doesMatch = true;

   if ((cardType == "VISA") && (!isVisa(cardNumber)))
      doesMatch = false;
   if ((cardType == "MASTERCARD") && (!isMasterCard(cardNumber)))
      doesMatch = false;
   if ( ( (cardType == "AMEX") || (cardType == "AMERICAN EXPRESS") )
                && (!isAmericanExpress(cardNumber))) doesMatch = false;
   if ((cardType == "DISCOVER") && (!isDiscover(cardNumber)))
      doesMatch = false;
   return doesMatch;

}  

/* dotmarketing formchecker created 03/05/02 
	List of Functions and their :-- descriptions follows...
lengthValid(fieldname, required field length, error alert message)
		:-- checks value of field has a length at least as long as the required field length.
selectValid(fieldname, error alert message)
		:-- checks that any but the first drop-down option is selected in an HTML select box.
numberValid(fieldname, error alert message)
		:-- checks that field value is numerical.
emailValid(fieldname, required field length, error alert message)
		:-- checks email length is at least required field length.  Also checks that field
		:-- value contains the "@" and "." characters.
radioValid(fieldname, number of radio buttons to check, error alert message)
		:-- checks that at least one of the radio buttons with the field name are checked.
otherboxValid(name of 'other' radio button,name of 'other' field, required field length, error alert message)		
		:-- checks that if the appropriate radio button is checked, the 'please specify other' text box is also completed.
checkboxRadio(name of checkbox field, name of radio field, number of radios to check, error alert message)		
		:-- checks that if the checkbox is checked, at least one of the radio buttons is checked.

	Example of Usage: Create a function with IF statement containing instances of NOT TRUE subfunctions below. Finish the function by the form submission.
E.G.-	function submitValid() {
			form=document.formName;
			if(!radioValid(form.event,2,"an event type")){
			}else if(!lengthValid(form.first_name,2,"first name")){
			}else if(!lengthValid(form.last_name,2,"last name")){
			}else{
				form.submit();
			}
		}
*/

//function to validate by length	
function lengthValid(field, len, text) {
    text = Trim(text);
	if (field.value.length < len)
  	{
 	  	alert("Please enter a valid " + text + ".");
		field.focus();
		return false;
	}else{
		return true;
	}
}

//function to validate select drop-downs
function selectValid(field, text) {
    text = Trim(text);
	if (field[0].selected)
  	{
 	  	alert("Please select a " + text + ".");
		field.focus();
		return false;
	}else{
		return true;
	}
}

//function to validate numerical fields
function numberValid(field, text) {
    text = Trim(text);
	if (isNaN(field.value))
  	{
 	  	alert("Please enter a valid " + text + ".");
		field.focus();
		return false;
	}else{
		return true;
	}
}

//function to validate email
function emailValid(mailfield, len, text) {
    text = Trim(text);
	if(mailfield.value.length < len || mailfield.value.indexOf("@") < 1 || mailfield.value.indexOf(".") < 2)
	{
	    alert("Please enter a valid " + text + ".");
		mailfield.focus();
		return false;
	}else{
		return true;
	}
}

//function to validate at least 1 radio button is checked	
function radioValid(field, radios, text) {
    text = Trim(text);
	radios=radios-1;
	var varChecked=false;
	for(i=0;i<=radios;i++)
	{
		if(field[i].checked)
		{
			varChecked=true;
		}
	}
	if (varChecked==false)
  	{
 	  	alert("Please select " + text + ".");
		field[0].focus();
		return false;
	}else{
		return true;
	}
}

//function to validate "other box" is not empty if checked	
function otherboxValid(otherradio,otherfield,len,text) {
    text = Trim(text);
	if (otherradio.checked && (otherfield.value.length<len))
  	{
 	  	alert("Please specify the " + text + ".");
		otherfield.focus();
		return false;
	}else{
		return true;
	}
}

//function to validate radio checked if checkbox checked	
function checkboxRadio(checkfield, radiofield, radios, text) {
    text = Trim(text);
	if(checkfield.checked)
	{	
		radioValid(radiofield, radios, text);
	}
}


/*
==================================================================
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));
}
 

