// Focuses into field f, displays message s and returns false
function focus_alert(s, f) {
	f.focus();
	alert(s);
	return false;
}

// Check fields in the form
// s is warning message, f is pointer to <form>, next arguments are names of mandatory fields
// example of use: <form onSubmit="return form_correct('Fill-in all marked fields!', this, 'name', 'password');">
function form_correct(s, f) {
	for (i=2; i < arguments.length; i++) {
		if (f[arguments[i]].value == '') {
			return focus_alert(s, f[arguments[i]]);
		}
	}
	return true;
}

// Check if field matches ereg
// example of use: <input onBlur="return ereg_correct('Enter a date!', this, /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/);">
function ereg_correct(s, f, regexp) {
	if (f.value == '' || regexp.test(f.value)) {
		return true;
	}
	return focus_alert(s, f);
}

// Check if there is a number in the field
// s is warning message, f is input field, boolean negative allows negative values, d1 is number of digits before decimal point, optional d2 is maximum count of digits after decimal point (empty - none, d1 - unlimited)
// example of use: <input onBlur="return number_correct('Enter a possitive number!', this, false, 5);">
function number_correct(s, f, negative, d1, d2) {
	return ereg_correct(s, f, new RegExp('^'+ (negative ? '-?' : '') +'[0-9]{0,'+ (d1 ? d1 : '') +'}'+ (d2 ? '([.][0-9]{1,'+ d2 +'})?' : '') +'$'));
}

// Check if there is an e-mail in the field, simplified!
// example of use: <input onBlur="return email_correct('Enter an e-mail!', this);">
function email_correct(s, f) {
	return ereg_correct(s, f, /^[^ @]+@[^ @]+\.[^ @]+$/);
}

// Check if there is a URL in the field, simplified!
// example of use: <input onBlur="return email_correct('Enter a URL!', this);">
function url_correct(s, f) {
	return ereg_correct(s, f, /http:\/\/[^ \/]+\.[^ ]+/);
}

// Open image window
function img_open(filename, w, h, title) {
	with (window.open('', '', 'width=' + w + ',height=' + h)) {
		document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n<html>\n');
		if (title) {
			document.write('<head>\n<title>' + title + '</title>\n</head>\n');
		}
		document.write('\n<body leftmargin="0" topmargin="0">\n\n');
		document.write('<img src="' + filename + '" width="' + w + '" height="' + h + '" border="0">');
		document.write('\n</body>\n</html>\n');
		return true;
	}
	return false;
}

