validate_fields = [];

function validate(f, fields) {
	if (typeof(fields) == 'undefined') {
		var fields = validate_fields;
	}
	for (var i in fields) {
		//alert(i +": " +f.elements[i]);
		var e = f.elements[i];
		var v = getvalue(e);
		var t = typeof(v);
		if (t == 'undefined' || t == 'null' || isempty(v) || (typeof(e.onfocus) == "function" && typeof(e.resetted) == "undefined")) {
			alert(fields[i]);
			return false;
		}
	}
	return true;
}

function isempty(s) {
	if (s instanceof Array) {
		return !s.length;
	} else {
		var c, v = s.split('');
		while (c = v.shift()) {
			if (c != ' ' && c != '\n' && c != '\r' && c != '\t') {
				return false;
			}
		}
		return true;
	}
}

function getvalue(f) {
	var t = f.type;
	switch (t) {
		case 'text':
		case 'hidden':
		case 'textarea':
		case 'password':
			return f.value;
		case 'select-one':
			return (f.selectedIndex == -1) ? '' : f[f.selectedIndex].value;
		case 'select-multiple':
			var ret = [], o = f.options;
			for (var i = 0; i < o.length; i++) {
				if (o[i].selected) {
					ret.push(o[i].value);
				}
			}
			return ret;
		default:
			location.href = 'mailto:lpini@officinedigitali.it?subject=completare%20script&body=' +t;

	}
	return "";
}
