﻿var ztbmngr = new function() {
    this.items = new Object();
    this.csss = new Object();
    this.o = null;
    this.ct = function(o, e) { if (!this.items[o.id]) return; if (o.value == this.items[o.id]) { o.value = ''; o.className = this.csss[o.id].v; } }
    this.ft = function(o, e) { if (!this.items[o.id]) return; if (o.value == '') { o.value = this.items[o.id]; o.className = this.csss[o.id].h; } }
    this.i = function(oid, dv, hcss) {
        if (!this.items[oid]) this.items[oid] = dv;
        var lo = document.getElementById(oid); if (!lo) return;
        if (!this.csss[oid]) this.csss[oid] = { v: lo.className, h: (hcss == '' ? lo.className : (hcss.charAt(0)=='+' ? lo.className.concat(hcss.replace('+',' ')) : hcss)) };
        AddEvent(lo, "focus", (function(e) { this.ct(lo, e); }).bind(this));
        AddEvent(lo, "blur", (function(e) { this.ft(lo, e); }).bind(this));
        AddEvent(lo, "focusout", (function(e) { return false; }).bind(this));
        if (lo.value == '') { lo.value = dv; lo.className = this.csss[lo.id].h; }
    }
    this.u = function() { this.items = null; this.o = null; }
    AddEvent(window, "unload", this.u);

    this.resize = function(textarea) {
        var hiddenTextArea = textarea.cloneNode(true);
        hiddenTextArea.value = textarea.value;
        hiddenTextArea.style.visibility = 'hidden';
        textarea.parentNode.appendChild(hiddenTextArea);
        hiddenTextArea.style.height = '1px';
        //IE Fix -> trigger recalculate of scrollHeight
        var dummy = hiddenTextArea.clientHeight;
        textarea.style.height = (hiddenTextArea.scrollHeight + 20) + 'px';
        textarea.parentNode.removeChild(hiddenTextArea);
        textarea.style.overflow = (textarea.scrollHeight > parseInt(textarea.style.maxHeight)) ? 'auto' : 'hidden';
    };
    this.grow = function(textarea) {
        if (textarea) {
            AddEvent(textarea, "keyup", (function() { this.resize(textarea); }).bind(this));
            this.resize(textarea);
        }
    }
} ();


var detect = navigator.userAgent.toLowerCase();

// Keep user from entering more than maxLength characters
function doKeyPress(obj, evt) {
    maxLength = obj.getAttribute("maxlength");
    var e = window.event ? event.keyCode : evt.which;
    if ((e == 32) || (e == 13) || (e > 47)) { //IE
        if (maxLength && (FixNewLine(obj.value) > maxLength - 1)) {
            if (window.event) {
                window.event.returnValue = null;
            } else {
                evt.cancelDefault;
                return false;
            }
        }
    }
}
function doKeyUp(obj, sr) {
    maxLength = obj.getAttribute("maxlength");
    if (maxLength && FixNewLine(obj.value) > maxLength) {
        obj.value = obj.value.substr(0, maxLength);
    }
    //sr = obj.getAttribute("showremain");
    if (sr) {
        document.getElementById(sr).innerHTML = maxLength - FixNewLine(obj.value);
    }
    resizeHeightToContent(obj);
}

function setText(sr, obj) {
    maxLength = obj.getAttribute("maxlength");
    if (sr) {
        document.getElementById(sr).innerHTML = maxLength - FixNewLine(obj.value);
        resizeHeightToContent(obj);
    }
}

// Cancel default behavior and create a new paste routine
function doPaste(obj) {
    maxLength = obj.getAttribute("maxlength");
    if (maxLength) {
        if ((window.event) && (detect.indexOf("safari") + 1 == 0)) { //IE
            var oTR = obj.document.selection.createRange();
            var iInsertLength = maxLength - FixNewLine(obj.value) + oTR.text.length;
            try {
                var sData = window.clipboardData.getData("Text").substr(0, iInsertLength);
                oTR.text = sData;
            }
            catch (err) {
            }
            if (window.event) { //IE                
                window.event.returnValue = null;
            } else {
                //not IE
                obj.value = obj.value.substr(0, maxLength);
                setText(obj.getAttribute("spanid"), obj);
                return false;
            }
        }
        setText(obj.getAttribute("spanid"), obj);
    }
}

function FixNewLine(txt) {
    /*if (txt.indexOf('\r\n') != -1) { }
    else if (txt.indexOf('\r') != -1) { txt = replace(txt,/\r/g, "\r\n"); }
    else if (txt.indexOf('\n') != -1) { txt = replace(txt, /\n/g, "\r\n"); }
    else { }*/
    //txt = replace(txt, '\r', '\r\n');
    //txt = replace(txt, '\n', '\r\n');
    return txt.length;
}

function replace(input, search, replacement) {
    while (input.indexOf(search) > -1) {
        input = input.replace(search, replacement);
    }
    return input;
}
