﻿function HasContent(htmlData) {
    var _ph = $('<span></span>').html(htmlData).text();
    return _ph.replace(/^\s*|\s*$/g, '').length > 0;
}
function submitenter(myfield, e, act) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13) {
        if (typeof (act) == 'function') act();
        return false;
    }
    else
        return true;
}
function twPanelInstance (e,o) {
    e = this.checkReplace($BK(e));
    var panelElm = new bkElement('DIV').setStyle({ width: 100 + '%' }).appendBefore(e);
    this.setPanel(panelElm);
    return this.addInstance(e, o);
}
function twValidate(fieldset, data, fldToValidate) {
    $('.invalidInput', fieldset).toggleClass('invalidInput', false);
    _data = data || fieldset.values();
    for (var i = 0, fields = $('input,select,textarea', fieldset), arr = fldToValidate.split(','), field; field = arr[i]; i++)
        if (!_data[field]) { fields.filter('[name="' + field + '"]').toggleClass('invalidInput', true); }
    return $('.invalidInput', fieldset).length == 0;
};
function twValidateBlock(block) {
    var valid = true;
    $('input[type="text"].mandatoryField,textarea.mandatoryField', block).removeClass('invalidInput').each(function (i, item) {
        $(item).toggleClass('invalidInput', ($(item).val() || '').isEmpty());
    });
    valid = (($('.invalidInput', block).length) == 0);
    if (!valid) $('.invalidInput:first', block).focus();
    return valid;
}
function twNString(dCode,lng, dn) {
    var ds = new String(dn);
    return (ds.length == 1 || (ds.length >= 2 && ds[ds.length - 2] != '1')) ? mlDict[dCode][lng][parseInt(ds.substr(ds.length - 1, 1))] : mlDict[dCode][lng][0];
};
function twInitBase(p) {
    $(':text', p).val('');
    $('textarea', p).val('');
    $(':checkbox', p).removeAttr('checked');
    $(':radio', p).removeAttr('checked');
    $('select', p).val('');
};


/* pads PadChar before ContentToSize
*/
function LPad(ContentToSize, PadLength, PadChar) {
    var PaddedString = ContentToSize.toString();
    for (var i = PaddedString.length + 1; i <= PadLength; i++) {
        PaddedString = PadChar + PaddedString;
    }
    return PaddedString;
}
/* gets source of the event cross-browser
*/
function getSourceElement(event) {
    return event.srcElement ? $(event.srcElement) : event.originalTarget ? $(event.originalTarget) : undefined;
}
/* drops off exeeding chars in textareas
*/
function imposeMaxLength(Object, MaxLen) {
    return (Object.value.length <= MaxLen);
}
function imposeInt(evt, obj) {
    return imposeNum(evt, obj, new RegExp('^\\d+$'));
}
function imposeDec(evt, obj) {
    return imposeNum(evt, obj, new RegExp('^\\d+(\\.(\\d{2}))?$'));
}
function imposeIntPercent(evt, obj) {
    return imposeNum(evt, obj, new RegExp('^(100|(([1-9][0-9])|[0-9]))$'));
}
function imposeNum(evt,obj,re) {
    var charCode = (evt.which) ? evt.which : window.event.keyCode;

    if (charCode <= 13) {
        if (obj.value.length == 0) {
            $(obj).unbind('blur').bind('blur', function() { $(obj).val(0).trigger('change'); });
        } 
        return true;
    }
    else {
        var test = re.test(obj.value);
        if (!test) {
            if (obj.value.length == 0) {
                $(obj).unbind('blur').bind('blur', function() { $(obj).val(0).trigger('change'); })
            } else obj.value = obj.value.substr(0, obj.value.length - 1);
        } else $(obj).unbind('blur');
        return test;
    }
}
/* apply tweed fine tunes
*/
(function($) {
    $.values.defaults.useSelectValue = true;
    $.values.defaults.uncheckedValue = false;
    $.values.set.input = function(work, opts) {
        var type = this.type.toLowerCase();
        if (type == 'checkbox' || type == 'radio') {
            this.checked = ((this.value == "true" ? true : this.value == "false" ? false : this.value) == work.value); /*2.n*/
            if (!this.checked && work.values) {
                this.checked = false;
                var has = this.value, vals = work.values;
                for (var i = 0, m = vals.length; i < m; i++) {
                    if (has == vals[i]) {
                        this.checked = true;
                        break;
                    }
                }
            }
        } else {
            this.value = work.value;
        }
    };
    $.values.get.input = function(opts) {
        if (opts.uncheckedValue !== true &&
                        (this.type == 'checkbox' || this.type == 'radio') && !this.checked) {
            return opts.uncheckedValue;
        }
        return this.value == 'true' ? true : this.valueOf == 'false' ? false : this.value; /*2.n*/
    };
/*
    $('a').die('mouseover').die('mouseout')
        .live('mouseover', function() { 
            if ($(this).css('color') != '') $(this).attr('baseColor',$(this).css('color'));
            $(this).css({'color':'red','cursor':'pointer'}); 
        })
        .live('mouseout', function(){
            $(this).css({'color':$(this).attr('baseColor'),'cursor':''});
        });
*/
})(jQuery);

(function ($) {
    $.fn.customFadeIn = function (speed, callback) {
        $(this).fadeIn(speed, function () {
            if (!$.support.opacity)
                $(this).get(0).style.removeAttribute('filter');
            if (callback != undefined)
                callback();
        });
    };
    $.fn.customFadeOut = function (speed, callback) {
        $(this).fadeOut(speed, function () {
            if (!$.support.opacity)
                $(this).get(0).style.removeAttribute('filter');
            if (callback != undefined)
                callback();
        });
    };
    $.fn.customFadeTo = function (speed, to, callback) {
        return this.animate({ opacity: to }, speed, function () {
            if (to == 1 && jQuery.browser.msie)
                this.style.removeAttribute('filter');
            if (jQuery.isFunction(callback))
                callback();
        });
    };
})(jQuery);