﻿function addValidation(ctlId, tipId, tipText, showOk, defaultText, inputFocusClass, inputErrorClass, tipNormalClass, tipErrorClass, tipCorrectClass, customValidation) {
    if (!ctlId || document.getElementById(ctlId) == null) return;
    if (!tipId || document.getElementById(tipId) == null) return;

    if (typeof (showOk) == 'undefined') showOk = true;

    tipText = IsNull(tipText, '');
    defaultText = IsNull(defaultText, '');
    inputFocusClass = IsNull(inputFocusClass, 'iTextNormal');
    tipNormalClass = IsNull(tipNormalClass, 'normal');
    tipCorrectClass = IsNull(tipCorrectClass, 'correct');
    tipErrorClass = IsNull(tipErrorClass, 'error');
    inputErrorClass = IsNull(inputErrorClass, 'iTextError');

    var ctl = $('#' + ctlId);
    var tip = $('#' + tipId);
    var requiredString = "此项为必填！";

    if (!IfNull(defaultText) && ctl.val() == '') {
        ctl.val(defaultText);
        ctl.css('color', '#ccc');
    }

    ctl.focus(function (e) {
        unfocusAll($('#content'), inputFocusClass);
        if (!IfNull(defaultText) && ctl.val() == defaultText) {
            ctl.val('');
        }
        ctl.css('color', '#000');
        var tipv = $.trim(tip.html());
        if (tipv == '' || tipv == '&nbsp;' || tipv == requiredString) {

            ctl.addClass(inputFocusClass);
            tip.removeClass();
            tip.addClass(tipNormalClass);
            tip.html(tipText);
        }
    });

    ctl.blur(function (e) {
        ctl.removeClass(inputFocusClass);
        if (IfNull($.trim(ctl.val()))) {
            if (!IfNull(defaultText)) {
                ctl.val(defaultText);
                ctl.css('color', '#ccc');
            }
            tip.removeClass();
            tip.empty();
        }
        else if (customValidation) {
            if (!customValidation(ctl, tip)) {
                ctl.addClass(inputErrorClass);
                tip.removeClass();
                tip.addClass(tipErrorClass);

            }
            else {
                ctl.removeClass(inputErrorClass);
                tip.empty();
                tip.removeClass();
                if (showOk)
                    tip.addClass(tipCorrectClass);
            }
        }
        else {
            var ctlToValidate = document.getElementById(ctlId);
            var correctClass;
            if (!showOk)
                tipCorrectClass = '';
            ClientValidate(e, ctlToValidate, tip, tipCorrectClass, tipErrorClass, inputErrorClass);
        }
    });
}

function ClientValidate(event, targetedControl, tip, tipCorrectClass, tipErrorClass, inputErrorClass) {
    var input = $(targetedControl);
    var isvalid = true;
    var vals;
    if (typeof (targetedControl.Validators) != "undefined") {
        vals = targetedControl.Validators;
    }
    else {
        if (targetedControl.tagName.toLowerCase() == "label") {
            targetedControl = document.getElementById(targetedControl.htmlFor);
            vals = targetedControl.Validators;
        }
    }
    if (vals !== null&&typeof(vals)=="array") {
        var i;
        for (i = 0; i < vals.length; i++) {
            ValidatorValidate(vals[i], null, event);
            if (!vals[i].isvalid) {
                isvalid = false;
                input.addClass(inputErrorClass);
                tip.html(vals[i].innerHTML);
                tip.removeClass();
                tip.addClass(tipErrorClass);
            }
        }
    }
    if (isvalid) {
        input.removeClass(inputErrorClass);
        tip.html('&nbsp;');
        tip.removeClass();
        tip.addClass(tipCorrectClass);
    }
    try {
        ValidatorUpdateIsValid();
    } catch (e) { }
}

function unfocusAll(c, fc) {
    c = IsNull(c, document);
    if (!IfNull(c) && !IfNull(fc)) {
        $(c).find('.area .list .con input').each(function (i) {
            $(this).removeClass(fc);
        });
    }
}
function IsNull(o, d) {
    if (typeof (o) == 'undefined' || o.length == 0) {
        if (d) {
            o = d;
        }
    }
    return o;
}
function IfNull(o) {
    if (typeof (o) == 'undefined' || o.length == 0) {
        return true;
    }
    return false;
}
function ISG(containerId, cbAllId, buttonsArray, alertStr, confirmStr) {
    $('#' + cbAllId).click(function () { $('#' + containerId + ' input:checkbox[id!=' + cbAllId + ']').each(function (i) { this.checked = $('#' + cbAllId).attr('checked'); }) });
    $(buttonsArray).each(function () {
        $('#' + this).click(function () {
            if ($('#' + containerId + ' input:checkbox[checked][id!=' + cbAllId + ']').length == 0) {
                if (alertStr != '') {
                    alert(alertStr && alertStr.length > 0 ? alertStr : '您还没有选择任何项目！');
                }
                return false;
            }
            if (confirmStr != '')
                return confirm(confirmStr && confirmStr.length > 0 ? confirmStr : '您确定要删除所选项目吗?');
            return true;
        });
    });
}
function ISGV(containerId, vCtl) {
    var v = '';
    $('#' + containerId + ' tr td input:checkbox').each(function () {
        if (this.checked) {
            var ctl = $(this).parent().find('input:hidden');
            v = v + ctl.val() + ',';
        }
    });
    v = v.replace(/,,/g, ',').replace(/,$/g, '');
    $('#' + vCtl).val(v);
}
function ISC(containerId, vCtl, cbs) {
    var v = '';
    $('#' + containerId + ' input:checkbox').each(function () {
        if (this.checked) {
            var ct1 = $(this).parent().parent().find('input:hidden');
            v = v + ct1.val() + ',';
           }
    });
    v = v.replace(/,,/g, ',').replace(/,$/g, '');
    $('#' + vCtl).val(v);
     }
function AddDefaultEvent(txt, btnid) {
    $(txt).keydown(function (e) { var evt = window.event | e; if (e.keyCode == 13) { if (btnid && document.getElementById(btnid)) document.getElementById(btnid).click(); return false; } });
}
function ValidateInputLen(txtId, maxLength, alertMess) {
    if ($('#' + txtId + '').val().Trim().replace(/[^\x00-\xff]/g, '**').length > maxLength) {

        alert(alertMess);
        return false;
    }
    return true;
}
