function validate_name(val) {
    var re = /^[a-zą-’\. ]+$/i;
    return val == '' || re.test(val);
}

function validate_email(val) {
    var re = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return val == '' || re.test(val);
}

function validate_phone(val) {
    var re = /^(\+|\d)[0-9() \-]+$/;
    return val == '' || re.test(val);
}

function enable_form(form) {
    var valid = true;
    jQuery('input[type=text], textarea', form).each(function() {
        var el = jQuery(this);
        if (el.hasClass('error')) {
            valid = false;
            return false;
        }
        if (el.hasClass('v_required') && (this.value == '' || this.value == jQuery.data(this, 'defval'))) {
            valid = false;
            return false;
        }
    });
	var btn = jQuery('input[type=submit].v_can_disable', form);
	if (btn) {
		if (valid) {
			btn.removeClass('disabled');
			btn.removeAttr('disabled');
		} else {
			btn.addClass('disabled');
			btn.attr('disabled', 'disabled');
		}
	}
}

function applyDefaultValue(el, val) {
    if (el.value == '')
		el.value = val;
    el.onfocus = function() {
        if (this.value == val) {
            this.value = '';
        }
    };
    el.onblur = function() {
        if (this.value == '') {
            this.value = val;
            jQuery(this).removeClass('error');
        }
    };
    jQuery.data(el, 'defval', val);
}

jQuery(function() {
    jQuery('form.v_form input, form.v_form textarea').keyup(function() {
        var el = jQuery(this);
        var valid;
		if (el.hasClass('v_name')) {
			valid = validate_name(this.value);
		} else if (el.hasClass('v_email')) {
            valid = validate_email(this.value);
        } else if (el.hasClass('v_phone')) {
            valid = validate_phone(this.value);
        } else {
            valid = true;
        }
        if (valid) el.removeClass('error');
        else el.addClass('error');
		enable_form(el.parents('form:first'));
    });
    
    jQuery('form.v_form input[type=submit]').click(function() {
        var form = jQuery(this).parents('form:first');
        var isValid = true;
        jQuery('input, textarea', form).each(function() {
            var val = this.value;
            var el = jQuery(this);
            if (val == jQuery.data(this, 'defval')) val = '';
            if (el.hasClass('v_required') && val == '') {
                isValid = false;
			} else if (el.hasClass('v_name') && !validate_name(val)) {
				isValid = false;
            } else if (el.hasClass('v_email') && !validate_email(val)) {
                isValid = false;
            } else if (el.hasClass('v_phone') && !validate_phone(val)) {
                isValid = false;
            }
			if (isValid) {
				el.removeClass('error');
			} else {
				el.addClass('error');
				return false; // break
			}
        });
        
        if (isValid) {
            // clear all default values
            jQuery('input', form).each(function() {
                if (this.value == jQuery.data(this, 'defval')) this.value = '';
            });
        }
        return isValid;
    });
});
