err = 0;
errc = ['OK'];
errc[1] = 'Please verify the zip code (5 or 9 digits).';
errc[2] = 'Please enter the telephone number, including area code.';
errc[3] = 'Please check the expiration date: four digits, '
+ 'e.g., 0709 is the correct date for July, 2009.';
errc[4] = 'This card is not working; can you check the card number again?';
errc[5] = 'Please do not leave this field blank.';
errc[6] = 'Please fill in the email address.';
errc[7] = 'Please check the expiration date: apparently it is expired.';
errc[8] = 'Please check the expiration date: it is not valid.';
errc[9] = 'Please complete the entry.';
errc[10] = 'Please enter a numerical value (may include an exponent e+N).';
errc[11] = 'Please enter a complete URL.';
errc[12] = 'Please make a selection.';
errc[13] = 'Por favor, introduzca un número mayor que cero.';
errc[14] = 'Por favor, introduzca una carpeta válida.';
errc[15] = 'Por favor, seleccione una opción.';
errc[16] = 'El día debe estar comprendido entre 1 y 31.';
errc[17] = 'El mes debe estar comprendido entre 1 y 12.';
errc[18] = 'El año debe estar comprendido entre -inf y +inf.';
errc[19] = 'La fecha debe tener el formato dd/mm/aaaa y ser válida.';

function date(s) {	// Comprueba que la fecha es valida
	// TODO: add different date format to validate
	var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var reDate = /(\d{1,2})\/(\d{1,2})\/(\d{4})/;
	err = 19;
	if (reDate.test(s)) {
		var tmp = reDate.exec(s);
		var day = parseInt(tmp[1],10);
		var month = parseInt(tmp[2],10);
		var year = parseInt(tmp[3],10);

		if (!day || !month || !year)
			return false;

		if (year/4 == parseInt(year/4))
			monthLength[1] = 29;

		if (day > monthLength[month-1])
			return false;

		monthLength[1] = 28;

		var now = new Date();
		now = now.getTime(); //NN3

		var dateToCheck = new Date();
		dateToCheck.setYear(year);
		dateToCheck.setMonth(month-1);
		dateToCheck.setDate(day);
		var checkDate = dateToCheck.getTime();

		var futureDate = (now < checkDate);
		var pastDate = (now > checkDate);

		err = 0;
		return s;
	}
	return 0;
}

function year(s) { // Comprueba que el año está entre -inf y +inf (que es un número)
	err = isNaN(s) ? 18 : 0;
	return [err ? 0 : s];
}

function month(s) { // Comprueba que el mes está entre 1 y  12
	err=(1<=s&&s<=12)?0:17;
	return [err ? 0 : s];
}

function day(s) { // Comprueba que el día está entre 1 y 31
	err=(1<=s&&s<=31)?0:16;
	return [err ? 0 : s];
}

function select(s,args) { // Comprueba que se haya seleccionado un valor
	var dflt=args['dflt'];

	err=(s.value==dflt)?15:0;
	return [err ? 0 : s.value];
}

function rutaSana(s,args) {	// Comprueba que la ruta especificada sea válida
	var sub=args['sub'];
	var abs=args['abs'];
	var file=args['file'];
	var re=new RegExp();

	s=trimwts(s);
	
	if (s=='') {
		err=14;
		return 0;
	}

	if (abs==1) {
		if (sub==1) {
			re=/^\/?[0-9a-zA-Z_\-\/]*[\/]$/;
		} else {
			re=/^\/?[0-9a-zA-Z_\-]*[\/]$/;
		}
	} else if (sub==1) {
		if (sub==1) {
			re=/^[0-9a-zA-Z_\-][0-9a-zA-Z_\-\/]*[\/]$/;
		} else {
			re=/^[0-9a-zA-Z_\-]+[\/]$/;
		}
	} else if (file==1) {
		re=/^[0-9a-zA-Z_.\-]+$/;
	}
	if (re.test(s)) { return 1; }
	else { err=14; return 0; }
}

function radiob(buttonGroup) {	// Comprueba que se haya seleccionado un botón de un grupo
   err=0;
   name=buttonGroup.name;
   els=document.getElementsByName(name);
   if (els[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<els.length; i++) {
         if (els[i].checked) {
            return 1;
         }
      }
   } else {
      if (els.checked) { return 1; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   err = 12;
   return 0;
}

function nwts(s) { // no white space
	return s.replace(/\s+/g, '');
}

function trimwts(s) { // trim white space
	return s.replace(/^\s+|\s+$/g, '');
}

function posnumber(s) {	// Cualquier número > 0
	err = (nwts(s)>0) ? 0: 13;
	return [err ? 0 : s];
}

function number(s) { // any number, including exponent
	s = nwts(s)-0;
	err = isNaN(s) ? 10 : 0;
	return [err ? 0 : s];
}

function zip(s) { // zip code
	var a = nwts(s).match(/\d{5}(-?\d{4}){0,1}/g);
	err = a != null && a.length ? 0 : 1;
	return a;
}

function tel(s) { // telephone, with area code + opt prefixes
	var a = s.replace(/\D+/g, '-');
	//a = a.match(/(\d+-?)*(\d{3}-?){2}\d{4}/g);
	a = a.match(/(\d+-?)*(\d{3}-?){2}\d+/g);
	err = a != null && a.length ? 0 : 2;
	return a;
}

/* this code is copied from
http://www.codelib.net/home/jkm/checksum.js */
function checksum(s) { // thanks to daniel_amor@hp.com for AMEX specs
	var p=0, e=8, t=0, c=[], r=0, l=0, i;
	if (s.length != 16) {
		t = 1;
		e = s.length == 13 && 6 || s.length == 15 && 7;
	}
	for (i=p; i<e; i++)
		r += (c[i] = s.charAt(i*2+t) * 2) > 9
		? Math.floor(c[i]/10 + c[i]%10)
		: c[i];
	for (i=p; i<e+t; i++) l += s.charAt(i*2+1-t)-0;
		l += r;
	return e && l%10 == 0;
}

function cardno(s) {
	s = s.replace(/\D+/g, '');
	err = checksum(s) ? 0 : 4;
	return [s];
}

function text(s) {
	s = trimwts(s);
	err = s.length ? 0 : 5;
	return [s];
}

function words(s) {
	s = trimwts(s);
	err = /\s/.test(s) ? 0 : 9;
	return [s];
}

function email(s) {
	a = s.match(/\S+@([-\w]+\.)+\w+/g);
	err = a != null && a.length ? 0 : 6;
	return a;
}

function url(s) {
	a = s.match(/\w{2,}:\/{2}([-\w]+\.)+\w+\S*/g);
	err = a != null && a.length ? 0 : 11;
	return a;
}

function expires(s) {
	s = nwts(s);
	var m = new Date()
	var m_year = m.getFullYear()%100, m_month = m.getMonth();
	if (s.length != 4 || isNaN(s))
		err = 3;
	else {
		s_month = s.substring(0,2)-0;
		s_year = s.substring(2, 4)-0;
		if (m_year > 70 && s_year < 30) s_year += 100;
			/* "Let your great grandson worry about that." */
			if (s_year < m_year || s_year > m_year + 6) err = 8;
			else if (s_year == m_year && !(s_month > m_month)) err = 7;
			else err = 0;
		}
	return [s];
}

function valid(element, check, args) {
	if (element.type == 'text' || element.type == 'textarea') {
		return check(element.value, args);
	} else {
		return check(element, args);
	}
}

present_element = null, present_error = null;
function validate(form, list) {
	for (i=0; i<form.elements.length; i++) {
		var element = form.elements[i];
		var n = element.name, out;
		if (list[n] && list[n].verify) {
			out = valid(element, list[n].verify, list[n].args);
			if (err && (list[n].force || present_element != element || present_error != err)) {
				present_error = err;
				present_element = element;
				alert(list[n].message || errc[err]);
				element.focus();
				return false;
			} else if (!err && list[n].filter)
				element.value = out.join(', ');
		}
	}
	return true;
}
