function Require(name)
{
    var elem = $('[name="'+name+'"]');
    alert(elem.length);
    return false;
}



$(document).ready(function() {    

function log(msg)
{
    $('#dbg').append('<div>'+msg+"</div>");
}
function isVisible(v)
{
    var o = v;
    while(v != null && v.length >= 1 && v[0].tagName != 'BODY')
    {
        
        if (v.css('display') == 'none' || v.css('visibility') == 'hidden')
        {
            /*log('do not validate '+o.attr('name'));*/
            return false;
        }
        v = v.parent();
    }
    return true;
}

document.firstError = null;
function Validate(elem)
{
    var valid = true;
    var d = GetMetaData(elem);
    if (d.name != null) {
        var names = d.name.split('&');
        $('.validateFailed').hide();
        $(names).each(function(idx,val) {
            var v = $('[name="'+val+'"]');
            if (isVisible(v) || d.always)
            {
                if (v.attr('type') == 'checkbox' || v.attr('type')=='radio')
                {
                    var count = 0;
                    v.each(function() { if (this.checked) count++;});
                    if (d.count) {
                        valid = valid && (count == d.count);
                    }
                    else{
                        valid = valid && (count > 0);
                    }
                }
                else if ($.trim(v.val()).length==0) {
                    valid = false;
                }
                if (d.length != null && d.length != '')
                {
                    valid = valid && v.val().length >= d.length;
                }
                if (d.match != null && d.match != '')
                {
                    var other = $('[name="'+d.match+'"]');
                    valid = valid && $(other).val() == v.val();
                }
                if (!valid && document.firstError == null)
                {
                    document.firstError = v;
                }
                if (!valid && !document.errFocus && d.onFocus)
                {
                    try { eval(d.onFocus); document.errFocus = d;} catch(e){}
                }
                
            }
        });
    }
    if (d.eval != null)
    {
        try {
            valid = eval(d.eval);
        } catch(e) { valid=false;}
    }
    return valid;
    
    
}

$('.validate').submit(function() {
    var form = $(this);
    var required = form.find('.require');
    var valid = true;
    document.firstError = null;
    document.errFocus = null;
    required.each(function() {
        
        if (!Validate(this)) {
            valid = false;
            $(this).show();
        }
        else
        {
            $(this).hide();
        }
    });
    
    var custom = form.find('.validation');
    custom.each(function() {
            var d = GetMetaData($(this));
            var func = d.eval;
            if (!eval(func))
            {
                $(this.show());
                valid = false;
            }
            else
            {
                $(this).hide();
            }
    });
        if (!valid) {
        $('.validateFailed').show();
        if (document.firstError != null)
        {
            $(document.firstError).focus();
        }
        if ($('.validateFailed').length > 0) {
            $(window).scrollTop($('.validateFailed').position().top);
        }
    }
    
    return valid;
});

}); // document.ready


function GetMetaData(e)
{
    var data = e== null ? "" : e.className;
    var vals = {};
    var s = data.indexOf('{');
    var e = data.indexOf('}');
    while (s>= 0 && e > s)
    {
        var metadata = data.slice(s+1,e);
        ParseNameValueList(metadata,vals);
        data = data.slice(e+1);
        s = data.indexOf('{');
        e = data.indexOf('}');
    }
    return vals;
    
}

function ParseNameValueList(list, result)
{
    var vals = list.split(',');
    
    $(vals).each(function(idx,text){
        var nv = text.split(":");
        if (nv.length>1)
        {
            var name = nv[0];
            var val = nv[1];
            if (val[0] == '"')
            {
                var eq = val.lastIndexOf('"');
                if (eq > 0)
                {
                    val = val.slice(1,eq);
                }
            }
            if (val[0] == "'")
            {
                var eq = val.lastIndexOf("'");
                if (eq > 0)
                {
                    val = val.slice(1,eq);
                }
            }
            if (name == "function")
            { // function is invalid as a name in IE
                name = "validateFunction";
            }
            eval("result."+name+" = val");
        }
    });
    return result;
}