/*-------------------------------------------------------------------------
  |  Function Checkvalue simply calls other ones and returns a value based on  |
  |  what it gets from its checks.  A FALSE means that the form won't be        |
  |  submitted.                                                                                      |
   -------------------------------------------------------------------------*/
   
function checkvalue(){
	var i;
	var l;
	var response;
	
	// get number of items on the form (remember that includes buttons, too!)
	l = document.forms.usereg.length;
	
	// iterate through all elements of the form and check entry validity
	for (i=0;i<l;i++){
		thevalue = document.forms.usereg.elements[i].value;
		thename = document.forms.usereg.elements[i].name;
		
		//only do something if they are not buttons
		if ((thename != "submit") && (thename != "reset")){
			//alert("Value: " + thevalue + "\nName: " + thename);
			response = validate(thename,thevalue);
			
			// if the response is false, then you can't submit the form!
			if (response==false) {
				return(false);
			}
		}
	}	
}

/*-------------------------------------------------------------------------
  |  Function Validate takes the name of the field and its value as params       |
   -------------------------------------------------------------------------*/
   
function validate(name,value){
	var response;
	//make sure it's a string - if it ain't, make it!
	value = "" + value + "";
	name = "" + name + "";
	
	
	response = isblank(name,value);
	if (response==false) {
		return(false);
	}
	else {
		response = numbercheck(name,value);
		if (response==false) {
			return(false);
		}
	}
}

function isblank(name,value) {

	if (value == "") {
		alert(name + " cannot be blank!");
		return false;
	}
	else {
		return true;
	}

}

/*-------------------------------------------------------------------------
  |  Function NumberCheck takes the name of the field and its value as params|
   -------------------------------------------------------------------------*/
   
function numbercheck(name,value){
	//set up a number of strings for testing
	var strings = new Array("Zipcode","Areacode","Credit Card Number");
	var i;
	var test;
	var response;
	
	for (i=0; i<strings.length; i++) {
		//for ease of use...
		test = "" + name.indexOf(strings[i]) + "";
		
		//if value matches test strings, then test it to be a number.
		if (test != "-1") {
		
			//if it's isNaN returns a true, then it's not a number and we have to set
			//the flage to false and alert the user of the mistake...
			if (isNaN(value)) {
				alert(name + " value must be a number!");
				return(false);
			}
			
			//if it is a number then if it's an areacode it can't be more than 3 chars longer, if it's a zipcode,
			//the limit is 5 chars.  no phone number checking here.
			else {
				if ((strings[i] == "Areacode") && (value.length != 3)) {
					alert("Area codes must be 3 digits long!");
					return(false);
					break;
				}
				
				if ((strings[i] == "Zipcode") && (value.length != 5)) {
					alert("Zip code must be 5 digits long!");
					return(false);
					break;
				}
				
				if (strings[i] == "Credit Card Number") {
					response = "" + isAnyCard(value) + "";

					if (response=="false") {
						alert("The Credit Card Number you've entered is not valid.\nPlease check the number and try again!");
						return(false);
						break;
					}
				}
			}
		}
	}
}

/*-------------------------------------------------------------------------
  |  Function PwdMatch checks to make sure that the password and its          |
  |  confirmation match.                                                                          |
   -------------------------------------------------------------------------*/
   
function pwdmatch() {
	var pwd1 = document.usereg("Password").value;
	var pwd2 = document.usereg("Password Check").value;
	
	if (pwd1 == pwd2) {
		return(true);
	}
	else {
		alert("Your passwords do not match... Please try again.");
		return(false);
	}
}




/*  ================================================================
    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:  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) && !isDiscover(cc)) {
    return false;
  }
  return true;

} // END FUNCTION isAnyCard() 