
function validateRegistration (formname) {
var reg_email, reg_password, reg_title, reg_firstname, reg_surname, reg_address, reg_town, reg_country, reg_telephone;
var reg_card_holder, reg_card_type, reg_card_number, reg_sec_code, reg_edate_month, reg_eyear_year;

var emailPat = /^(.+)@(.+)$/
var matchArray = formname.email_address.value.match(emailPat)

	reg_email = formname.email_address.value;
	if (formname.user_password) {
		reg_password = formname.user_password.value;
	}
	if (formname.reg_title) {
		reg_title = formname.reg_title.value;
	}
	if (formname.firstname) {
		reg_firstname = formname.firstname.value;
		if (reg_firstname.length <= 2) {
			alert("Your firstname is too short or empty.")
			formname.firstname.focus();
			return false;
		}
	}
	if (formname.surname) {
		reg_surname = formname.surname.value;
		if (reg_surname.length <= 2) {
			alert("Your surname is too short or empty.")
			formname.surname.focus();
			return false;
		}
	}
	if (formname.address1) {
		reg_address = formname.address1.value;
		if (reg_address.length <= 5) {
			alert("Your address is too short or empty.")
			formname.address1.focus();
			return false;
		}
	}
	if (formname.town) {
		reg_town = formname.town.value;
		if (reg_town.length <= 3) {
			alert("Your town / city is too short or empty.")
			formname.town.focus();
			return false;
		}
	}
	if (formname.telephone) {
		reg_telephone = formname.telephone.value;
		if (reg_telephone.length <= 2) {
			alert("Your telephone number is too short or empty.")
			formname.telephone.focus();
			return false;
		}
	}

	if (reg_email.length <= 7) {
		alert("Your email address is too short or empty.")
		formname.email_address.focus();
		return false;
	}
    if (matchArray==null) {
        alert("Email address seems incorrect (check @ and .'s)");
        formname.email_address.focus();
        return false;
    }

	if (formname.user_password) {
		if (reg_password.length <= 3) {
			alert("Your password is too short or empty.")
			formname.user_password.focus();
			return false;
		}
	}

	if (formname.registration) {
		if (validateCard (formname) == false) {
			return false;
		}
	}

//	this.disabled=true;
	return true;

}

function validateBidForm (formname) {
var reg_password, reg_email

	if (formname.email_address) {
		reg_email = formname.email_address.value;
	}
	if (reg_email.length <= 7) {
		alert("Your email address is too short or empty.")
		formname.email_address.focus();
		return false;
	}

	if (formname.user_password) {
		reg_password = formname.user_password.value;
	}
	if (formname.user_password) {
		if (reg_password.length <= 3) {
			alert("Your password is too short or empty.")
			formname.user_password.focus();
			return false;
		}
	}

	return true;
}


function validateCard(form){
	var card_holder, card_type, card_issue_number, card_cvc, card_smonth, card_syear, card_emonth, card_eyear, card_number;
	var current_year, current_month;
	var excludedCard = false;

	current_year = form.current_year.value;
	current_month = form.current_month.value;
	card_holder = form.card_holder.value;
	card_type = form.card_type.value;
	card_issue_number = form.card_issue.value;
	card_cvc = form.sec_code.value;
	card_smonth = form.vdate_month.value;
	card_syear = form.vdate_year.value;
	card_emonth = form.edate_month.value;
	card_eyear = form.edate_year.value;
	card_number = form.card_number.value;

	var hyphenrx = /-/g;
	var spacerx = / /g;	

	var new_card_number = card_number.replace(hyphenrx, "");
	new_card_number = new_card_number.replace(spacerx, "");

	if (card_holder.length >= 3) {
		if (isNaN(card_holder) == false) {
			alert("Cardholder's name cannot be numeric.")
			form.card_holder.focus();
			return false;
		}
	}
	else if (card_holder.length < 3) {
		alert("Cardholder's name is too short or empty.")
		form.card_holder.focus();
		return false;
	}
		
	if (card_type == 'Switch / Solo') {
		excludedCard = true;
	}
	if (card_type == 'Delta') {
		excludedCard = true;
	} 
	
	if (excludedCard == false) {
		if (checkCreditCard (new_card_number,card_type)) {
		} 
		else {
			alert (ccErrors[ccErrorNo]);
			//form.card_type.select();
			form.card_type.focus();
			return false;
		}
	}

	if (excludedCard == true) {
		if (new_card_number.length == 0) {
			alert("No card number provided");
			form.card_number.focus();
			return false;
		}
		else if (new_card_number.length < 16) {
			alert("Card number has an inappropriate number of digits");
			form.card_number.focus();
			return false;
		}
		if (isNaN(new_card_number) == true) {
			alert("Card number must be numeric")
			form.card_number.focus();
			return false;
		}
	}

	if (card_issue_number.length != 0) {
		if (isNaN(card_issue_number) == true) {
			alert("Issue number must be numeric")
			form.card_issue.focus();
			return false;
		}
	}

	if (card_cvc.length != 0) {
		if (isNaN(card_cvc) == true) {
			alert("Security code must be numeric")
			form.sec_code.focus();
			return false;
		}
		if (card_cvc.length < 3) {
			alert("Security code must be 3 or 4 digits")
			form.sec_code.focus();
			return false;
		}
	}

/*
	if ((card_syear.length != 0) && (card_syear.length != 0)) {
		if (card_syear > current_year) {
			alert("Valid from year can not be in the future");
			//form.card_syear.select();
			form.vdate_year.focus();
			return false;
		}
		if ((card_syear == current_year) && (card_smonth > current_month)) {
			alert("Valid from date can not be in the future");
			//form.card_syear.select();
			form.vdate_year.focus();
			return false;
		}
	}
	*/

	if (card_emonth.length == 0) {
		alert("Please select an expiry month")
		//form.card_emonth.select();
		form.edate_month.focus();
		return false;
	}
	else if (card_eyear.length == 0) {
		alert("Please select an expiry year")
		//form.card_eyear.select();
		form.edate_year.focus();
		return false;
	}

	if ((card_emonth.length != 0) && (card_eyear.length != 0)) {
		if (card_eyear < current_year) {
			alert("Expiry year can not be in the past");
			//form.card_eyear.select();
			form.edate_year.focus();
			return false;
		}
		if ((card_eyear == current_year) && ( Math.floor(card_emonth) < Math.floor(current_month) )) {
			alert("Expiry date can not be in the past");
			//form.card_eyear.select();
			form.edate_year.focus();
			return false;
		} 
	}
	else {
		alert("Please enter a complete expiry date");
		//form.card_eyear.select();
		form.edate_year.focus();
		return false;
	}
	this.disabled=true;
	return true;
}



/*============================================================================*/

/* Adapted by Matt Stow - 16/08/04

This routine checks the credit card number. The following checks are made:

1. A number has been provided
2. The number is a right length for the card
3. The number has an appropriate prefix for the card
4. The number has a valid modulus 10 number check digit if required

If the validation fails an error is reported.

The structure of credit card formats was gleaned from
    http://www.blackmarket-press.net/info/plastic/check_digit.htm 
where the details of other cards may also be found.

Parameters:
            cardnumber           number on the card
            cardname             name of card as defined in the card list below

Author:     John Gardner
Date:       1st November 2003

*/

/*
   If a credit card number is invalid, an error reason is loaded into the 
   global ccErrorNo variable. This can be be used to index into the global error  
   string array to report the reason to the user if required:
   
   e.g. if (!checkCreditCard (number, name) alert (ccErrors(ccErrorNo);
*/

var ccErrorNo = 0;
var ccErrors = new Array ()

ccErrors [0] = "Please select card type";
ccErrors [1] = "No card number provided";
ccErrors [2] = "Card number is in invalid format";
ccErrors [3] = "Card number is invalid";
ccErrors [4] = "Card number has an inappropriate number of digits";

function checkCreditCard (cardnumber, cardname) {

  // Array to hold the permitted card characteristics
  var cards = new Array();

  // Define the cards we support. You may add addtional card types.
  
  //  Name:      As in the selection box of the form - must be same as user's
  //  Length:    List of possible valid lengths of the card number for the card
  //  prefixes:  List of possible prefixes for the card
  //  checkdigit Boolean to say whether there is a check digit
  
  cards [0] = {name: "Visa Debit", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};  
  cards [1] = {name: "Visa Credit", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};
  cards [2] = {name: "Mastercard", 
               length: "16", 
               prefixes: "51,52,53,54,55",
               checkdigit: true};
  cards [3] = {name: "American Express (Amex)", 
               length: "15", 
               prefixes: "34,37",
               checkdigit: true};
  cards [4] = {name: "Switch / Solo", 
               length: "16,17,18", 
               prefixes: "6,7",
               checkdigit: true}; 
               
  // Establish card type
  var cardType = -1;
  for (i=0; i<cards.length; i++) {

    // See if it is this card (ignoring the case of the string)
    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
      cardType = i;
      break;
    }
  }
  
  // If card type not found, report an error
  if (cardType == -1) {
     ccErrorNo = 0;
     return false; 
  }
   
  // Ensure that the user has provided a credit card number
  if (cardnumber.length == 0)  {
     ccErrorNo = 1;
     return false; 
  }
  
  // Check that the number is numeric, although we do permit a space to occur  
  // every four digits. 
  var cardNo = cardnumber
  var cardexp = /^([0-9]{4})\s?([0-9]{4})\s?([0-9]{4})\s?([0-9]{1,4})$/;
  if (!cardexp.exec(cardNo))  {
     ccErrorNo = 2;
     return false; 
  }
    
  // Now remove any spaces from the credit card number
  cardexp.exec(cardNo);
  cardNo = RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4;
       
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    for (i = cardNo.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardNo.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 3;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.
  var LengthValid = false;
  var PrefixValid = false; 
  var undefined; 

  // We use these for holding the valid lengths and prefixes of a card type
  var prefix = new Array ();
  var lengths = new Array ();
    
  // Load an array with the valid prefixes for this card
  prefix = cards[cardType].prefixes.split(",");
      
  // Now see if any of them match what we have in the card number
  for (i=0; i<prefix.length; i++) {
    var exp = new RegExp ("^" + prefix[i]);
    if (exp.test (cardNo)) PrefixValid = true;
  }
      
  // If it isn't a valid prefix there's no point at looking at the length
  if (!PrefixValid) {
     ccErrorNo = 3;
     return false; 
  }
    
  // See if the length is valid for this card
  lengths = cards[cardType].length.split(",");
  for (j=0; j<lengths.length; j++) {
    if (cardNo.length == lengths[j]) LengthValid = true;
  }
  
  // See if all is OK by seeing if the length was valid. We only check the 
  // length if all else was hunky dory.
  if (!LengthValid) {
     ccErrorNo = 4;
     return false; 
  };   
  
  // The credit card is in the required format.
  return true;

  }
/*============================================================================*/