/**
 * Common functions
 */

// clear the default value or set it back on blur
function init_doclear() {
	jQuery('.doclear').focus(function() {
		if (this.value == this.defaultValue) {
			this.select();
		}
	}).blur(function() {
		if (this.value == '') {
			this.value = this.defaultValue;
		}
	});
}

// add a class inputError for the mandatory fields
function init_checkclear() {
	jQuery('.checkclear').submit(function(){
		// empty fields with default valuke
		jQuery(this).find('.doclear').each(function(){
			if (jQuery(this).hasClass('doclear') && this.value == this.defaultValue) {
				this.value = '';
			}
		});
		// get and check mandatory fields
		var ret = true;
		var f = this; // keep the form available
		var m = jQuery(this).find('input[name=mandatory]:first').val();
		var ma = [];
		if (m != undefined && m != '') {
			// loop the mandatory fields
			ma = m.split(',');
			jQuery(ma).each(function(){
				// remove inputError class
				jQuery(f).find(':input[name=' + this + ']').removeClass('inputError');
				// check if empty
				if (jQuery(f).find(':input[name=' + this + ']').val() == '') {
					jQuery(f).find(':input[name=' + this + ']').addClass('inputError');
					ret = ret && false;
				}
			});
			// focus on first error
			jQuery(f).find('.inputError:first').focus();
		}
		return ret;
	});
}

