/*
 * Traduccion al espanol.
 *
 * Author: Jose Robinson - http://joserobinson.com/
 */

jQuery.extend(jQuery.validator.messages, {
  required: "Este campo es obligatorio.",
  remote: "Por favor, rellena esta campo.",
  email: "Por favor, escribe una direcci&oacute;n de correo v&aacute;lida.",
  url: "Por favor, escribe una URL v&aacute;lida.",
  date: "Por favor, escribe una fecha v&aacute;lida.",
  dateISO: "Por favor, escribe una fecha (ISO) v&aacute;lida.",
  number: "Por favor, escribe un n&uacute;mero entero v&aacute;lido.",
  digits: "Por favor, escribe s&oacute;lo d&iacute;gitos.",
  creditcard: "Por favor, escribe un n&uacute;mero de tarjeta v&aacute;lido.",
  equalTo: "Por favor, escribe el mismo valor de nuevo.",
  accept: "Por favor, escribe una valor con una extensi&oacute;n aceptada.",
  maxlength: jQuery.format("Por favor, no escribas m&aacute;s de {0} caracteres."),
  minlength: jQuery.format("Por favor, no escribas menos de {0} caracteres."),
  rangelength: jQuery.format("Por favor, escribe un valor entre {0} y {1} caracteres."),
  range: jQuery.format("Por favor, escribe un valor entre {0} y {1}."),
  max: jQuery.format("Por favor, escribe un valor igual o menor que {0}."),
  min: jQuery.format("Por favor, escribe un valor igual o mayor que {0}.")
});


/*
 * Metodos adicionales
 *
 * Author: Jose Robinson - http://joserobinson.com/
 */

jQuery.validator.addMethod("double", function(value, element) {
  return this.optional(element) || /^-?\d+(\.\d+)?$/.test(value);
}, "Por favor, escribe un valor valido.");

jQuery.validator.addMethod("phone", function(value, element) {
  return this.optional(element) || /^(\d{3}|\(\d{3}\))([-\s\.]?\d{3})([-\s\.]?\d{4})$/.test(value);
}, "Por favor, escribe un n&uacute;mero de tel&eacute;fono v&aacute;lido.");

jQuery.validator.addMethod("rnc", function(value, element) {
  return this.optional(element) || /^[0-9]{9}$/.test(value);
}, "Por favor, escribe un RNC v&aacute;lido.");

jQuery.validator.addMethod("moneyD", function(value, element) {
  return this.optional(element) || /^[1-9][0-9]*(\,?[0-9]{3})*(\.[0-9]{1,})?$/.test(value);
}, "Por favor, escribe una cantidad v&aacute;lida.");

jQuery.validator.addMethod("dateD", function(value, element) {

	var dateI = value.match(/^([0-9]{1,2})(.)([0-9]{1,2})(.)([0-9]{4})$/);

	if (dateI == null) {
		return false;
	}

	var separator = dateI[2];

	if (dateI[1].match(/^0[1-9]$/) != null) {
			dateI[1] = dateI[1].substr(1);
	}

	if (dateI[3].match(/^0[1-9]$/) != null) {
			dateI[3] = dateI[3].substr(1);
	}

	var oDate = new Date();
	oDate.setFullYear(dateI[5], (dateI[3] - 1), dateI[1]);

	var dateA = new Array();
	dateA['day'] = ""+ oDate.getDate() +"";
	dateA['month'] = ""+ (oDate.getMonth() + 1) +"";
	dateA['year'] = ""+ oDate.getFullYear() +"";

	if (dateA['day'].length == 1) {
		dateA['day'] = "0" + dateA['day'];
	}

	if (dateA['month'].length == 1) {
		dateA['month'] = "0" + dateA['month'];
	}

	$(element).blur(function() {
		$(element).val(dateA['day'] + '/' + dateA['month'] + '/' + dateA['year']);
	});

	return true;

}, "Por favor, escribe una fecha v&aacute;lida.");

jQuery.validator.addMethod("hourD", function(value, element) {

//	var hourMatch = value.match(/^([1-9]|0[1-9]|1[012])([AP]M?)$/i);
//
//	if (hourMatch != null) {
//		if (hourMatch[2].match(/^[AP]$/i) != null) {
//			hourMatch[2] = hourMatch[2] + "M";
//		}
//
//		if (hourMatch[1].match(/^[1-9]$/) != null) {
//			hourMatch[1] = "0" + hourMatch[1];
//		}
//
//		value = hourMatch[1] + ":00 " + hourMatch[2];
//
//		value = value.toUpperCase();
//
//		$(element).val(value)
//		.valid();
//
//		return true;
//	}

	return this.optional(element) || /^(0[1-9]|1[012]):([0-5][0-9])\s([AP]M)$/i.test(value);
}, "Por favor, escribe una hora v&aacute;lida.");
