
//|
//|  validation functions
//|
String.prototype.validateNotBlank = function()
{
  return this.match(/\S/);
};
String.prototype.validateBlank = function()
{
  return ! this.match(/\S/);
};
String.prototype.validateNotNull = function()
{
  return this.length != 0;
};
String.prototype.validateNoSpace = function()
{
  return (! this.match(/\s/));
};
String.prototype.validateMaxValue = function(max)
{
  if (typeof max == 'number')
  {
    return (parseFloat(this) <= max);
  }
  else
  {
    return (this <= max);
  }
};
String.prototype.validateMinValue = function(min)
{
  if (typeof min == 'number')
  {
    return (parseFloat(this) >= min);
  }
  else
  {
    return (this >= min);
  }
};
String.prototype.validateMaxLength = function(length)
{
  return this.length <= length;
};
String.prototype.validateMinLength = function(length)
{
  return this.length >= length;
};
String.prototype.validateCreditCard = function()
{
  var credit_card_regex = /^([0-9][-. ]?){13,16}$/;
  return this.match(credit_card_regex);
};
String.prototype.validatePastDate = function(date)
{

};
String.prototype.validateFutureDate = function(date)
{

};
String.prototype.validateBeforeTime = function(time)
{

};
String.prototype.validateAfterTime = function(time)
{

};
String.prototype.validateInArray = function(values)
{
  for (i=0; i < values.length; i++)
  {
    if (values[i]==this) return true;
  }
  return false;
};
var countries_array = ['AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CS', 'CU', 'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TP', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UK', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW'];
String.prototype.validateInCountry = function(or_value)
{
  if (typeof or_value != 'undefined' && this==or_value)  return true;
  return this.validateInArray(countries_array);
};
var states_array = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'];
String.prototype.validateInState = function(or_value)
{
  if (typeof or_value != 'undefined' && this==or_value)  return true;
  return this.validateInArray(states_array);
};
String.prototype.validateRange = function(min, max)
{
  if (typeof min == 'number' && typeof max == 'number')
  {
    return (parseFloat(this) >= min && parseFloat(this) <= max);
  }
  else
  {
    return (this >= min && this <= max);
  }
};
String.prototype.validateRegex = function(regex)
{
  return this.match(regex);
};
String.prototype.validateCurrency = function(symbol, comma, decimal, symbol_at_start, number_between_commas)
{
  symbol = (typeof symbol=='undefined') ? '$' : symbol;
  comma = (typeof comma=='undefined') ? ',' : comma;
  decimal = (typeof decimal=='undefined') ? '.' : decimal;
  symbol_at_start = (typeof symbol_at_start=='undefined') ? true : symbol_at_start;
  number_between_commas = (typeof number_between_commas=='undefined') ? 3 : number_between_commas;

  var regex = '^'+(symbol_at_start && symbol ? symbol.escapeRegex()+'?' : '') + '([0-9]{1,'+number_between_commas+'}('+comma.escapeRegex()+'[0-9]{'+number_between_commas+'})*|[0-9]+)('+decimal.escapeRegex()+'[0-9]*)?' + (!symbol_at_start && symbol ? symbol.escapeRegex()+'?' : '') + '$';
  if (this.match(new RegExp(regex)))
  {
    return this;
  }
  else
  {
    return false;
  }
};
String.prototype.parseCurrency = function()
{
  return (this.replace(/[^0-9.]/g, '').match(/[0-9.]+/)[0]);
};
function parseCurrency(value)
{
  return value.parseCurrency();
}
String.prototype.validateEmail = function()
{
  isValid = true;
  atIndex = this.lastIndexOf('@');
  if (atIndex==-1)
  {
     isValid = false;
  }
  else
  {
    domain = this.substr(atIndex + 1);
    local = this.substr(0, atIndex);
    localLen = local.length;
    domainLen = domain.length;

    if (localLen < 1 || localLen > 64)
    {
       // local part length exceeded
       isValid = false;
    }
    else if (domainLen < 1 || domainLen > 255)
    {
       // domain part length exceeded
       isValid = false;
    }
    else if (local.match(/^\./) || local.match(/\.$/))
    {
       // local part starts or ends with "."
       isValid = false;
    }
    else if (local.match(/\.\./))
    {
       // local part has two consecutive dots
       isValid = false;
    }
    else if (domain.match(/^\./) || domain.match(/\.$/))
    {
       // domain part starts or ends with "."
       isValid = false;
    }
    else if (!domain.match(/^[A-Za-z0-9\-\.]+$/))
    {
       // character not valid in domain part
       isValid = false;
    }
    else if (domain.match(/\.\./))
    {
       // domain part has two consecutive dots
       isValid = false;
    }
    else if ( ! local.replace('\\\\', '').match(/^(\\.|[A-Za-z0-9!#%&`_=\/$'*+?^{}|~.-])+$/))
    {
       if (!local.replace('\\\\','').match(/^"(\\"|[^"])+"$/))
       {
         // character not valid in local part unless local part is quoted
         isValid = false;
       }
    }
  }
  return isValid;
}
String.prototype.validateZip = function()
{
  var zip_regex = /^[0-9]{5}(-?[0-9]{4})?$/;
  return this.match(zip_regex);
};
String.prototype.validatePostalCode = function()
{
  var postal_regex = /^[-0-9a-zA-Z ]{4,20}$/;
  return this.match(postal_regex);
};
String.prototype.validateDate = function()
{
  var date_regex_Y_m_d = /^([0-9]{4}[-.\/](1[0-2]|0?[1-9])[-.\/](3[0-1]|[0-2]?[0-9]))$/;
  var date_regex_m_d_Y = /^((1[0-2]|0?[1-9])[-.\/](3[0-1]|[0-2]?[0-9])[-.\/][0-9]{4}|)$/;
  return (this.match(date_regex_Y_m_d) || this.match(date_regex_m_d_Y));
};
String.prototype.validateTime = function()
{
  var time_regex = /^((2[0-3]|1[0-9]|0?[1-9]):([0-5]?[0-9])(:[0-5]?[0-9])? *(am|pm)?)$/i;
  return this.match(time_regex);
};
String.prototype.validateIp = function()
{
  var ip_regex = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
  return this.match(ip_regex);
};
String.prototype.validatePhoneNumber = function()
{
  var phone_number_regex = /^\+?([0-9().-]+)$/;
  var phone_number_length = this.replace(/[^0-9]/g, '').length;
  return this.match(phone_number_regex) && (phone_number_length >= 10);
}
String.prototype.validateUsPhoneNumber = function(area_code_required)
{
  area_code_required = (typeof area_code_required=='undefined') ? true : area_code_required;
  if (area_code_required)
  {
    var phone_number_regex = /^(\(?[0-9]{3}\)?-? *)([0-9]{3})-? *([0-9]{4})$/;
  }
  else
  {
    var phone_number_regex = /^(\(?[0-9]{3}\)?-? *)?([0-9]{3})-? *([0-9]{4})$/;
  }
  return this.match(phone_number_regex);
}
String.prototype.validateNumber = function(comma, decimal, number_between_commas)
{
  comma = (typeof comma=='undefined') ? ',' : comma;
  decimal = (typeof decimal=='undefined') ? '.' : decimal;
  number_between_commas = (typeof number_between_commas=='undefined') ? '3' : number_between_commas;
  var number_regex = new RegExp('^([0-9]{1,' + number_between_commas + '}(' + comma.escapeRegex() + '[0-9]{' + number_between_commas + '})*|[0-9] + )(' + decimal.escapeRegex() + '[0-9]*)?$');
  return this.match(number_regex);
};
String.prototype.parseNumber = function()
{
  if (this.match(/^[0-9.]+/)) return parseInt(this.match(/^[0-9.]+/)[0]);
  return 0;
};
String.prototype.validateDigits = function(digits, precision)
{
};
String.prototype.validateInteger = function()
{
  var contender = this.match(/[0-9]+/);
  return (contender[0] == this) ? true : false;
};


//|
//|  jQuery validate plugin
//|
if ( ! $.fn.validate )
{
  $.validate = function(form_selector, options)
  {
    $.extend($.validate, options);
    $(form_selector).submit(function()
    {
      $.validate.success = true;
      if ($.validate.before)  $.validate.before();
      $('.__validate__').trigger('validate');
      if ($.validate.after)  $.validate.after();
      
      if ($.validate.success)
      {
        if ($.validate.on_success)
        {
          this.__validate_action = $.validate.on_success;
          return this.__validate_action();
        }
        return true;
      }
      else
      {
        if ($.validate.on_errors)
        {
          this.__validate_action = $.validate.on_errors;
          return (this.__validate_action() ? true : false);
        }
        return false;
      }
    });
  };
  $.validate.before = null;
  $.validate.after = null;
  $.validate.on_success = null;
  $.validate.on_error = null;
  $.validate.on_errors = null;
  
  
  $.fn.validate = function(validator_name, options, action)
  {
    if ( typeof validator_name == 'undefined' )  return this;
    
    switch ( validator_name )
    {
      case 'min_length':
      case 'minlength':
        this.validateMinLength(options, action);
        break;
      case 'max_length':
      case 'maxlength':
        this.validateMaxLength(options, action);
        break;
      case 'number!':
        this.bind('keyup change', function(e)
        {
          current_value = $(this).val();
          var stripped_value = $(this).val().replace(/[^0-9]/g, '');
          if (current_value !== stripped_value )
          {
            $(this).val( stripped_value );
            current_value = stripped_value;
          }
        }).change();  //calls the event in case the initial data is bunk
        break;
    }
  }
  
  
  $.fn.validateMinLength = function(options, action)
  {
    var min_length = options;
    this.addClass('__validate__');
    this.bind('validate', function()
    {
      var result = $(this).val().validateMinLength(min_length);
      if ( ! result )
      {
        if (action && typeof action == 'function')
        {
          this.__validate_action = action;
          this.__validate_action('minlength');
        }
        else if ($.validate.on_error && typeof $.validate.on_error == 'function')
        {
          this.__validate_action = $.validate.on_error;
          this.__validate_action('minlength');
        }
        this.__validate_action = null;
        $.validate.success = false;
      }
    });
  }
  
  
  $.fn.validateMaxLength = function(options, action)
  {
    var max_length = options;
    this.addClass('__validate__');
    this.bind('validate', function()
    {
      var result = $(this).val().validateMaxLength(max_length);
      if ( ! result )
      {
        if (action && typeof action == 'function')
        {
          this.__validate_action = action;
          this.__validate_action('maxlength');
        }
        else if ($.validate.on_error && typeof $.validate.on_error == 'function')
        {
          this.__validate_action = $.validate.on_error;
          this.__validate_action('maxlength');
        }
        this.__validate_action = null;
        $.validate.success = false;
      }
    });
  }
  
  
}

