/**
 * Input validation functions
 *
 * @author Nick Nettleton / nick@plumdigitalmedia.com
 * @copyright 2005
 * @package UI
 * @version 0.1
 */

 /**
  * Trims a string
  */
String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, '') ;
}

/**
 * Tells whether a string is a valid email
 */
String.prototype.is_valid_email = function()
{
	var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/ ;
	return re.test(this) ;
}

/**
 * Tells whether a string is a valid credit card number
 * *** todo: check we've got the latest version here against dropsend
 */
String.prototype.is_valid_card_number = function()
{
	var number = this.replace(/\s+/g, '') ;

	//replace if contains non-numbers
	if(number.match(/\D/)){
		return false ;
	}

	// convert to array and reverse the number
	number = number.split('').reverse().join('') ;

	// loop through the number one digit at a time
	// double the value of every second digit (starting from the right)
	// concatenate the new values with the unaffected digits
	var digits = '';
	for(var i = 0; i < number.length; i++){
		digits += '' + ((i%2) ? number.charAt(i) * 2 : number.charAt(i)) ;
	}

	// add all of the single digits together
	var sum = 0 ;
	for (var i = 0; i < digits.length; i++){
		sum += (digits.charAt(i) * 1) ;
	}
	//alert(sum) ;

	// valid card numbers will be transformed into a multiple of 10
	return (sum % 10) ? false : true ;
}