

var shipto,charge;

// non-digit characters which are allowed in credit card numbers


var creditCardDelimiters = " "



//Prompt messages
var sName="Please enter your Name."
var sCoName="Please enter your Company's Name."
var sAddress = "Please enter your Address."
var sCity = "Please enter a city."
var sState = "Please enter a state."
var sZip = "Please enter a 5 or 9 digit zip code."
var sPhone = "Please enter valid phone number."
var sEmail = "Please enter a valid email address. ex: jdoe@somewhere.com."
var sAmt = "Please enter an amount."
var sCCNum = "Please enter a valid credit card number."
var sCCName = "Please enter a name as appears on the card."
var sCCMo = "Please select the month the card expires."
var sCCYear = "Please select the year the card expires."
var sCheck = "Yes I would like mail you a check."
var iCreditCardPrefix = "This is not a valid "
var iCreditCardSuffix = " credit card number. Please reenter it now."
var sAuthorize = "You must authorize the Gamma Mu Foundation to process this credit card."
var sTransfermethod = "You must specify a means by which this transfer is to be made."
var sBeneficiary = "You must select all three consent boxes at the top of this form."



function promptUser(prompt){

    window.status = prompt;

}
function checkForm(theForm) {
	
  var x;
  var element, returnValue;

//  if (theForm.paybyCheck.checked){
//
//     check=true;
//  } 
//  else
//     check=false


  for (x=0; x < theForm.elements.length; x++) {
  element = theForm.elements[x];

  switch (element.name) {

   case "beneficiarycb1":  returnValue = checkcheckbox(element,sBeneficiary);
                           if (returnValue == false){
                                return(false);
                             }
                           break; 
   case "beneficiarycb2":
                           if (theForm.beneficiarycb2.checked){
                             break; 
                           } 
                           else
                              return(false);
                              break; 
   case "beneficiarycb3":
                           if (theForm.beneficiarycb3.checked){
                             break; 
                           } 
                           else
                              return(false);
                              break; 
   //case "paybyCheck":
   //                        if (theForm.paybyCheck.checked){
   //                          check=true;
   //                        } 
   //                        else
   //                          check=false
   //                        break; 
   case "transfermethod":         returnValue = checkNull(element,sTransfermethod);
                           if (returnValue == false){
                                return(false);
                             }
                           break; 

   case "authorize":         returnValue = checkNull(element,sAuthorize);
                           if (returnValue == false){
                                return(false);
                             }
                           break; 
   case "name":         returnValue = checkNull(element,sName);
                           if (returnValue == false){
                                return(false);
                             }
                           break;
    case "address1":      returnValue = checkNull(element,sAddress);
                                if (returnValue == false){
                                return(false);
                             }
                           break;
    case "city"   :        returnValue = checkNull(element,sCity);
                           if (returnValue == false){
                                return(false);
                             }
                           break;

    case "state":          returnValue = checkNull(element,sState);
                             if (returnValue == false){
                               return(false);
                             }
                           break;

    case "zip"     :        returnValue = checkNull(element,sZip);
                            if (returnValue == false){
                               return(false);
                            }
                            break;

    case "phone"  :         returnValue = checkNull(element,sPhone);
                              if (returnValue == false){
                                return(false);
                              }
                            break;

    case "email"    :        returnValue = checkNull(element,sEmail);
                             if (returnValue == false){
                               return(false);
                             }
                             returnValue = checkEmail(element);
                             if (returnValue == false){
                               return(false);
                             }
                             break;
    case "amount"  :         returnValue = checkNull(element,sAmt);
                              if (returnValue == false){
                                return(false);
                              }
                            break;

    //case "ccName" :   if (check!=true){
    //                    returnValue = checkNull(element,sCCName);
    //                    if (returnValue == false){
    //                    return(false);
    //                  }
    //                }
    //                break;	
    //case "ccNum"  : if (check!=true){
    //                  returnValue = checkNull(element,sCCNum);
    //                  if (returnValue == false){
    //                    return(false);
    //                 }
    //                  returnValue = checkCreditCard (theForm.elements["ccType"], theForm.elements["ccNum"]);
    //                 if (returnValue == false){
    //                    return(false);
    //                  }
    //                } 
    //                break;
    }		
  }

  window.status = "";
  return(true);          

}


//------------------------------------------------------------------------------

function checkSelect(field,prompt){

  if (field.selectedIndex == "0") {
    msg = prompt;
    alert(msg);
    field.focus();
    return(false);
   }

}

function checkcheckbox(field,prompt) {

	if (!field.checked) {
		msg = prompt;
		alert(msg);
                field.focus();
                field.select();
		return (false);
	}	
	else return (true);
}		
//------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------	
function checkNull(field,prompt) {

	if (field.value == "") {
		msg = prompt;
		alert(msg);
                field.focus();
                field.select();
		return (false);
	}	
	else return (true);
}		
//------------------------------------------------------------------------------------		
function validateChar(OKChars, field) {
	
	for (x=0; x < field.value.length; x++) {
		ch = field.value.charAt(x);
		for (y=0; y < OKChars.length; y++) {	
			if (ch == OKChars.charAt(y)) 
				break;
			if (y == OKChars.length - 1) {
				return (false);
			}	
		}		
	}
	return (true);
}		
//-------------------------------------------------------------------------------------		
function isDigit(ch) {

	if (ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4' || 
	    ch == '5' || ch == '6' || ch == '7' || ch == '8' || ch == '9')	
 		return (true);
	else
		return (false);
}
//-------------------------------------------------------------------------------------		
function isHyphenDotSpace(ch) {

	if (ch == '-' || ch == '.' || ch == ' ')	
 		return (true);
	else
		return (false);
}
//-------------------------------------------------------------------------------------		
function checkEmail(field) {
	
	// Validate all characters in the email field
	//--------------------------------------------------
	
	OKChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@.";
	returnValue = validateChar(OKChars, field);
	if (returnValue == false) {
		msg = "Email address contains invalid characters";	
		alert(msg);
		return (false);
	}
	
	// Check for a '@' and a '.' in the email field 
	//--------------------------------------------------
	
	for (x=0; x < field.value.length; x++) {
		if (field.value.charAt(x) == '@') {
			for (y=x+1; y < field.value.length; y++)			
				if (field.value.charAt(y) == '.')
					return (true);
		}
	}	
	msg = "Email address needs to be in the form:  yourname@anywhere.com";
	alert(msg);
	field.focus();
	field.select();
	return (false);

}
//------------------------------------------------------------------------------------
function checkAreaCodePrefix(field) {

	
	// Check for properly formatted area code and phone number prefix

        if (field.value.length != 3){
	  alert ("Please enter a 3 digit area code and prefix.");
          field.focus();
          field.select();
	  return (false);
        }
        else
	for (x=0; x < 3; x++) {
          ch = field.value.charAt(x);
          returnValue = isDigit (ch);
	  if (returnValue == false) {
	    alert ("Please enter digits only for the area code and prefix.");
            field.focus();
            field.select();
	    return (false);
	  }
	}		
   	return (true);
}
//-------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
function checkPhoneExt(field) {
		
	// Check for properly formatted area code and phone number prefix


        if (field.value.length != 4){
	  alert ("Please enter the last 4 digits of the phone number.");
          field.focus();
          field.select();
	  return (false);
        }
        else
	for (x=0; x < 4; x++) {
          ch = field.value.charAt(x);
          returnValue = isDigit (ch);
	  if (returnValue == false) {
	    alert ("Please enter digits only for the phone number.");
            field.focus();
            field.select();
	    return (false);
	  }
	}		
   	return (true);
}
//-------------------------------------------------------------------------------------


function checkZip(field) {

	// Check for properly formatted phone number 
	//--------------------------------------------------
	
	if (field.value.length == 5) {
		for (x=0; x < field.value.length; x++) {		
			ch = field.value.charAt(x);
			returnValue = isDigit (ch);
			if (returnValue == false) {
				zipMessage ();
                                field.focus();
                                field.select();
				return (false);
			}	
		}
		return (true);		
	}
	else {
		zipMessage ();
                field.focus();
                field.select();
		return (false);
	}
}
//------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
function checkZipext(field) {

	// Check for properly formatted phone number 
	//--------------------------------------------------
	
	if (field.value.length == 4) {
		for (x=0; x < field.value.length; x++) {		
			ch = field.value.charAt(x);
			returnValue = isDigit (ch);
			if (returnValue == false) {
				zipMessage ();
                                field.focus();
                                field.select();
				return (false);
			}	
		}
		return (true);		
	}
	else {
		zipMessage ();
                field.focus();
                field.select();
		return (false);
	}
}
//------------------------------------------------------------------------------------------
function zipMessage () {

	msg = "Zip code must be digits in the form:  XXXXX or XXXXX-XXXX";
	alert(msg);
	return (true);
}
//------------------------------------------------------------------------------------------
function phoneMessage () {

	msg = "Phone number must be in the form:  XXX-XXX-XXXX"
	alert(msg);
	return (true);
}


// 18 Feb 97 created Eric Krock
//
// (c) 1997 Netscape Communications Corporation


// CREDIT CARD DATA VALIDATION FUNCTIONS
// 
// getRadioButtonValue (radio)         Get checked value from radio button.
// checkCreditCard (radio, theField)   Validate credit card info.
// isCreditCard (st)              True if credit card number passes the Luhn Mod-10 test.
// isVisa (cc)                    True if string cc is a valid VISA number.
// isMasterCard (cc)              True if string cc is a valid MasterCard number.
// isAmericanExpress (cc)         True if string cc is a valid American Express number.
// isDinersClub (cc)              True if string cc is a valid Diner's Club number.
// isCarteBlanche (cc)            True if string cc is a valid Carte Blanche number.
// isDiscover (cc)                True if string cc is a valid Discover card number.
// isEnRoute (cc)                 True if string cc is a valid enRoute card number.
// isJCB (cc)                     True if string cc is a valid JCB card number.
// isAnyCard (cc)                 True if string cc is a valid card number for any of the accepted types.
// isCardMatch (Type, Number)     True if Number is valid for credic card of type Type.


// Get checked value from radio button.

function getRadioButtonValue (radio)
{   for (var i = 0; i < radio.length; i++)
    {   if (radio[i].checked) { break }
    }
    return radio[i].value
}




// Validate credit card info.

function checkCreditCard (radio, theField)
{   var cardType = getRadioButtonValue (radio)
    var normalizedCCN = stripCharsInBag(theField.value, creditCardDelimiters)
    if (!isCardMatch(cardType, normalizedCCN)) 
       return warnInvalid (theField, iCreditCardPrefix + cardType + iCreditCardSuffix);
    else 
    {  theField.value = normalizedCCN
       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:  isDinersClub()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Diner's
		    Club number.
		    
	      false, otherwise

    Sample number: 30000000000004 (14 digits)
    ================================================================ */

function isDinersClub(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 14) && (firstdig == 3) &&
      ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
    return isCreditCard(cc);
  return false;
}



/*  ================================================================
    FUNCTION:  isCarteBlanche()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Carte
		    Blanche number.
		    
	      false, otherwise
    ================================================================ */

function isCarteBlanche(cc)
{
  return isDinersClub(cc);
}




/*  ================================================================
    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:  isEnRoute()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid enRoute
		    card number.
		    
	      false, otherwise

    Sample number: 201400000000009 (15 digits)
    ================================================================ */

function isEnRoute(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 15) &&
      ((first4digs == "2014") ||
       (first4digs == "2149")))
    return isCreditCard(cc);
  return false;
}



/*  ================================================================
    FUNCTION:  isJCB()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid JCB
		    card number.
		    
	      false, otherwise
    ================================================================ */

function isJCB(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) &&
      ((first4digs == "3088") ||
       (first4digs == "3096") ||
       (first4digs == "3112") ||
       (first4digs == "3158") ||
       (first4digs == "3337") ||
       (first4digs == "3528")))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isJCB()



/*  ================================================================
    FUNCTION:  isAnyCard()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is any valid credit
		    card number for any of the accepted card types.
		    
	      false, otherwise
    ================================================================ */

function isAnyCard(cc)
{
  if (!isCreditCard(cc))
    return false;
  if (!isMasterCard(cc) && !isVisa(cc) && !isAmericanExpress(cc) && !isDinersClub(cc) &&
      !isDiscover(cc) && !isEnRoute(cc) && !isJCB(cc)) {
    return false;
  }
  return true;

} // END FUNCTION isAnyCard()



/*  ================================================================
    FUNCTION:  isCardMatch()
 
    INPUT:    cardType - a string representing the credit card type
	      cardNumber - a string representing a credit card number

    RETURNS:  true, if the credit card number is valid for the particular
	      credit card type given in "cardType".
		    
	      false, otherwise
    ================================================================ */

function isCardMatch (cardType, cardNumber)
{

	cardType = cardType.toUpperCase();
	var doesMatch = true;

	if ((cardType == "VISA") && (!isVisa(cardNumber)))
		doesMatch = false;
	if ((cardType == "MASTERCARD") && (!isMasterCard(cardNumber)))
		doesMatch = false;
	if ( ( (cardType == "AMERICANEXPRESS") || (cardType == "AMEX") )
                && (!isAmericanExpress(cardNumber))) doesMatch = false;
	if ((cardType == "DISCOVER") && (!isDiscover(cardNumber)))
		doesMatch = false;
	if ((cardType == "JCB") && (!isJCB(cardNumber)))
		doesMatch = false;
	if ((cardType == "DINERS") && (!isDinersClub(cardNumber)))
		doesMatch = false;
	if ((cardType == "CARTEBLANCHE") && (!isCarteBlanche(cardNumber)))
		doesMatch = false;
	if ((cardType == "ENROUTE") && (!isEnRoute(cardNumber)))
		doesMatch = false;
	return doesMatch;

}  // END FUNCTION CardMatch()




function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}




// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}











