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.';


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

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);
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 checkbox(s) {
err = s.checked ? 0 : 31;
return s;
}

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) {
if (element.type == 'text' || element.type == 'textarea') {
return check(element.value);
} else return check(element);
}

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);
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;
}
