﻿// Validators should return an object which contains
// valid bool and error message if not valid            

// Check a field is not empty
var isNotEmptyValidator = function(field) {
	var isValid = false;
	if(field.is('[@type=radio]'))
		isValid = (field.is(':checked'));
	else if(field.is('select'))
		isValid = (field[0].selectedIndex != 0);
	else
		isValid = (field.attr('value') != '' && field.attr('value').match(/[^\s]/gi));
	return { valid: isValid, message: '&laquo; You must fill in this field' };
};

// Check a field is a valid DD/MM/YYYY date
var isDateValidator = function(field) {
	if(field.attr('value') == '') return { valid: true, message: '' };
	var dateRegex = /\d{2}\/\d{2}\/\d{4}/gi;
	return { valid: (field.attr('value').match(dateRegex)), message: '&laquo; Must be a valid date in DD/MM/YYYY format' };
};

// Check if a field contains a valid email address
var isEmailValidator = function(field) {
	if(field.attr('value') == '') return { valid: true, message: '' };
	var emailRegex = /[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|edu|coop)\b/gi;
	return { valid: (field.attr('value').match(emailRegex)), message: '&laquo; Must be a valid email address' };
};

// Do Luhn (MOD 10) check on credit card number field
// Also strips all spaces and non numeric characters
var isCreditCardNumber = function(field) { 
    var val = field.attr('value');
    if(val == '') return { valid: true, message: '' };
	if(val.length < 13 || val.length > 19) return { valid: false, message: '&laquo; Card number must be between 13 and 19 digits' };
	if(val == '0000000000000000') return { valid: false, message: '&laquo; Card number cannot be all zeros' };
	var invalidRegex = /[^0-9]/g;
  if(val.match(invalidRegex)) return { valid: false, message: '&laquo; Card number must only contain numbers, with no spaces' };

  var n = val.replace(/\D/g, '').split('');
	var total = 0, digit = 0;
	var count = 1;
	for (var i=n.length-1; i >= 0; i--) 
	{
	    digit = parseInt(n[i], 10);
	    if (count % 2 == 0) 
	    { 
	      digit *= 2; 
	      if (digit > 9) 
	      { 
	        digit = (digit % 10) + 1; 
	      }
	    }
	    total += digit;
	    count++;
	}

	// If the total mod 10 equals 0, the number is valid
	return (total % 10 == 0) ? { valid: true, message: '' } : { valid: false, message: '&laquo; Card number is not valid - please check' };
};

// Check the issue number is either numeric or empty
var isIssueValidator = function(field) {
	if(field.attr('value') == '') return { valid: true, message: '' };
	var issueRegex = /\d+/g;
	return { valid: !isNaN(field.attr('value')), message: '&laquo; Issue number can only contain numbers' };
};

// Check CV2 number is a 3 or 4 digit numeric value
var isCV2Validator = function(field) {
  if ($('#CardType option:selected').text() == 'American Express')
  {
	  return { valid: (!isNaN(field.attr('value')) && field.attr('value').length == 4) , message: '&laquo; Security code must be a 4 digit number' };
  } else {
	  return { valid: (!isNaN(field.attr('value')) && field.attr('value').length == 3), message: '&laquo; Security code must be a 3 digit number' };
  }
}

var matchesValidator = function(field) { 
    // console.log(field.attr('value') + '-' + );

    var compare = field.attr('id').replace(/Confirm/gi, '');

    if(field.attr('value') == '') return { valid: true, message: '' };
    if(field.attr('value') == $('#' + compare).attr('value'))
        return { valid: true, message: '' };
    else
        return { valid: false, message: '&laquo; Must match ' + compare };
}

var isCustomerNameValidator = function(field) {
	if(field.attr('value') == '') return { valid: true, message: '' };
	var nameRegex = /[a-zA-Z0-9\\.]+/g;
    if (field.val().match(nameRegex)) 
        return { valid: true, message: '' };
    else
        return { valid: false, message: '&laquo; Please correct this value' };
}

var isAddressValidator = function(field) {
	if(field.attr('value') == '') return { valid: true, message: '' };
	var addressRegex = /[a-zA-Z0-9\\.\\-]+/g;
    if (field.val().match(addressRegex)) 
        return { valid: true, message: '' };
    else
        return { valid: false, message: '&laquo; Please correct this value' };
}

var isPostcodeValidator = function(field) {
	if(field.attr('value') == '') return { valid: true, message: '' };
	var postcodeRegex = new RegExp("[a-zA-Z0-9\\s]+");
    if (field.val().match(postcodeRegex) == field.val()) 
    {
        return { valid: true, message: '' };
    }
    else
    {
        return { valid: false, message: '&laquo; Please correct this value' };
    }
}

