/*
 * jQuery form validation class
 *
 * author:  Dariusz Pobożniak
 * website: http://pobozniak.pl
 */


$(function() {
    Form.validate();
})

var Form = {
    formClass  : '.handiform',
    errorClass : 'error',
    isError    : false,
    
    validate : function() {
        $(this.formClass).submit(function() {
            var $form = $(this);
            Form.isError = false;
            $form.find('input[type=hidden]').each(function() {
    			if ($(this).val().substr(-1) == '*') {
                    var $hidId = $(this).attr('name').substr(1,2);
                    var $hidType = $(this).val().split('-')[0];
                    
                    switch ($hidType) {
                        case 'text'     : Form.checkText($hidId); break;
                        case 'textarea' : Form.checkText($hidId); break;
                        case 'file'     : Form.checkText($hidId); break;
                        case 'select'   : Form.checkText($hidId); break;
                        case 'radio'    : Form.checkRadio($hidId); break;
                        case 'checkbox' : Form.checkRadio($hidId); break;
                    }
                }
            });
            if (Form.isError === true) {
                return false;
            }
        })
    },
    
    checkText : function($id) {
        if (jQuery.trim($('[name="f'+$id+'"]', this.formClass).val()) == '') {
            Form.isError = true;
            $('[name="f'+$id+'"]', this.formClass).parent().addClass(this.errorClass);
        } else {
            $('[name="f'+$id+'"]', this.formClass).parent().removeClass(this.errorClass);
        }
    },
    
    checkRadio : function($id) {
        if ($('[name^="f'+$id+'"]:checked', this.formClass).val() == undefined) {
            Form.isError = true;
            $('[name^="f'+$id+'"]', this.formClass).parent().parent().parent().addClass(this.errorClass);
        } else {
            $('[name^="f'+$id+'"]', this.formClass).parent().parent().parent().removeClass(this.errorClass);
        }
    }
}
