function isFactor(value, parcel) {
    for (var i = 0; i <= value; i++ ) {
        var f = value / i;
        if (f === Math.floor(f) && i == parcel) {
            return true;
        }
    }
    return false;
}

function emailValidation(value) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(value);
}

function validate($input) {
    var isValid;
    var value = $input.val();
    var $icon = $('#' + $input.attr('id') + '-icon');
    var $hint = $('#' + $input.attr('id') + '-hint');
    if (value.length > 0) {
        switch ($input.attr('id')) {
            case 'username' : isValid = value.length >= 6 && value.length <= 20 && value != $('#password').val(); break;
            case 'password' : isValid = value.length >= 6 && value.length <= 20 && value != $('#username').val(); break;
            case 'password2' : isValid = $('#password').data('isValid') && value == $('#password').val(); break;
            case 'cvr' : isValid = value.length == 8 && !isNaN(value); break;
            case 'company' :
            case 'firstName' :
            case 'lastName' :
            case 'city' : isValid = value.length > 0 && value.length <= 50; break;
            case 'address' : isValid = value.length > 0 && value.length <= 100; break;
            case 'zipcode' :
            case 'phone' :
            case 'fax' : isValid = value.length > 0 && !isNaN(value); break;
            case 'email' : isValid = value.length <= 50 && emailValidation(value); break;
            default : isValid = value.length > 0;
        }
        $icon.removeClass().addClass('icon ' + (isValid ? 'tick' : 'warning'));
        if (isValid) {
            $hint.fadeOut();
        } else {
            $hint.fadeIn();
        }
    } else {
        isValid = false;
        $icon.removeClass().addClass('icon' + ($input.hasClass('required') ? ' asterisk' : ''));
        $hint.fadeOut();
    }
    $input.data('isValid', isValid);
}

jQuery(function($){

    $('form.validate input.required, form.validate input.validate').each(function(){
        var $input = $(this);
        var iconId = $input.attr('id') + '-icon';
        var hintId = $input.attr('id') + '-hint';
        $input.after(
            $('<div class="icon' + ($input.hasClass('required') ? ' asterisk' : '') + '"></div>').attr('id', iconId),
            $('<span class="hint"></span>').attr('id', hintId).html($input.attr('title')).css('display', 'none')
        ).keyup(function(){
            $('form.validate input.required, form.validate input.validate').each(function(){
                validate($(this));
            });
        });
        if ($input.val().length > 0) {
            $input.trigger('keyup');
        }
    });

    $('form.validate').submit(function(){
        var formIsValid = true;
        $(this).find('input.required').each(function(){
            validate($(this));
            if (!$(this).data('isValid')) {
                formIsValid = false;
            }
        });
        if (!formIsValid) {
            $('#submitHint').fadeIn();
        }
        return formIsValid;
    });

    $('table.products tbody tr:odd, table.cart tbody tr:odd').addClass('odd');
    $('table tbody tr').hover(function(){
        $(this).addClass('hover');
    }, function(){
        $(this).removeClass('hover');
    });
});