﻿
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, '');
}
function checkInput(form, cleanError) {
    var result = true;
    var hfocus = false;
    var focus = true;
    $(form).find('span.validate').each(function() {
        var id = $(this).attr('id');
        var forid = $(this).attr('forid').toString();
        var inputStr = $('#' + forid).val().toString().trim();

        if (!cleanError) {
            if ($(this).attr('errorstring')) {
                var errorstring = $(this).attr('errorstring');
                $(this).html(errorstring);
                if (!hfocus) {
                    if (focus)
                        $('#' + forid).focus();
                    hfocus = true;
                }
                result = false;
                return;
            }
        }

        var nullable = true;
        if ($(this).attr('nullable')) {
            nullable = $(this).attr('nullable') != 'false';
        }
        var nullablestr = '*';
        if (!nullable) {
            nullablestr = $(this).attr('nullablestring');
        }
        if (!nullable && inputStr.length <= 0) {
            $(this).html(nullablestr);
            if (!hfocus) {
                if (focus)
                    $('#' + forid).focus();
                hfocus = true;
            }
            result = false;
            return;
        }

        var needregularexpression = false;
        var regularexpression = '';
        if ($(this).attr('regularexpression')) {
            regularexpression = $(this).attr('regularexpression');
            needregularexpression = regularexpression.length >= 1;
        }
        var regularexpressionstring = '**';
        if (needregularexpression) {
            regularexpressionstring = $(this).attr('regularexpressionstring');
        }
        if (needregularexpression) {
            var rx = new RegExp(regularexpression);
            if (!rx.test(inputStr)) {
                $(this).html(regularexpressionstring);
                if (!hfocus) {
                    if (focus)
                        $('#' + forid).focus();
                    hfocus = true;
                }
                result = false;
                return;
            }
        }

        var needcompareelement = false;
        if ($(this).attr('compareelementid')) {
            needcompareelement = true;
        }
        if (needcompareelement) {
            var compareelementid = $(this).attr('compareelementid');
            var comparestring = $(this).attr('comparestring');
            var compareStr = $('#' + compareelementid).val().toString().trim();
            if (inputStr != compareStr) {
                $(this).html(comparestring);
                if (!hfocus) {
                    if (focus)
                        $('#' + forid).focus();
                    hfocus = true;
                }
                result = false;
                return;
            }
        }

        $(this).html('&nbsp;');
    });

    return result;
}

$(document).ready(function() {
    $('form').each(function() {
        checkInput($(this), false);
    });
});

$(document).ready(function() {
    $('form').each(function() {
        $(this).submit(function() {
            var result = checkInput($(this), true);
            return result;
        });
    });
});
