var myClass = {}

//show or hidde object;
myClass.doShowHidden = function(sObj, visibility) {
    if ((typeof sObj) == 'string') sObj = document.getElementById(sObj);
    if (!sObj) return;

    if (visibility == undefined) {
        if (sObj.style.display == 'none') {
            sObj.style.display = '';
        }
        else {
            sObj.style.display = 'none';
        }
    }
    else {
        if (visibility == 'hidden' || visibility == 'none') {
            sObj.style.display = 'none';
        }
        else {
            sObj.style.display = '';
        }
    }
}

//return an rand string;
myClass.getRandStr = function(len) {
    if (!len) len = 5;

    var s1 = '';

    for (var i = 0; i < len; i++) {
        s1 += Math.floor(Math.random() * 10);
    }

    return s1;
}

//add function to sEvent;
myClass.addFunc = function(sEvent, newFunc, sort) {
    if (sort !== 0) sort = 1;

    if (eval('typeof(' + sEvent + ') !== "function"')) {
        eval(sEvent + '= newFunc');
    }
    else {
        eval('var oldFunc = ' + sEvent);

        var s = '';
        if (sort == 0) {
            s += sEvent + '=function(){';
            s += 'newFunc();';
            s += 'return oldFunc();';
            s += '}';
        }
        else {
            s += sEvent + '=function(){';
            s += 'oldFunc();';
            s += 'return newFunc();';
            s += '}';
        }

        eval(s);
    }
}


//add function to sEvent;
myClass.addFuncNew = function(sObj, sEvent, newFunc, sort) {
    if ((typeof sObj) == "string") sObj = document.getElementById(sObj);
    if (!sObj) return;

    if (sort !== 0) sort = 1;

    if (eval('typeof(sObj.' + sEvent + ') !== "function"')) {
        eval('sObj.' + sEvent + '= newFunc');
    }
    else {
        eval('var oldFunc = sObj.' + sEvent);

        //取得 oldFunc 的实体部分；
        var oldFuncBody = oldFunc.toString().replace(/t\his(\W)/gi, 'thisObj$1');
        var oldFuncBodyStart = oldFuncBody.indexOf('{') + 1;
        var oldFuncBodyEnd = oldFuncBody.lastIndexOf('}');
        oldFuncBody = oldFuncBody.substring(oldFuncBodyStart, oldFuncBodyEnd);
        oldFuncBody = 'var oldFunc = function(thisObj){' + oldFuncBody + '}';

        //取得 newFunc 的实体部分；
        var newFuncBody = newFunc.toString().replace(/t\his(\W)/gi, 'thisObj$1');
        var newFuncBodyStart = newFuncBody.indexOf('{') + 1;
        var newFuncBodyEnd = newFuncBody.lastIndexOf('}');
        newFuncBody = newFuncBody.substring(newFuncBodyStart, newFuncBodyEnd);
        newFuncBody = 'var newFunc = function(thisObj){' + newFuncBody + '}';

        eval(oldFuncBody);
        eval(newFuncBody);

        var s = '';

        if (sort == 0) {
            s += 'sObj.' + sEvent + '=function(){';
            s += 'newFunc(this);';
            s += 'return oldFunc(this);';
            s += '}';
        }
        else {
            s += 'sObj.' + sEvent + '=function(){';
            s += 'oldFunc(this);';
            s += 'return newFunc(this);';
            s += '}';
        }

        eval(s);
    }
}


//return a ajaxRequest;
myClass.getAjaxRequest = function(url, callback, msg, param) {
    if (msg) msg();

    var ajaxRequest = null;

    if (typeof (XMLHttpRequest) !== 'undefined') {
        ajaxRequest = new XMLHttpRequest();
    }
    else {
        ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
        if (!ajaxRequest) ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
    }

    if (callback) {
        if (!param) {
            ajaxRequest.open('get', url, true);
            ajaxRequest.onreadystatechange = function() { myClass.getAjaxResult(ajaxRequest, callback); }
            ajaxRequest.send(null);
        }
        else {
            ajaxRequest.open('post', url, true);
            ajaxRequest.onreadystatechange = function() { myClass.getAjaxResult(ajaxRequest, callback); }
            ajaxRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=GB2312');
            ajaxRequest.send(param);
        }
    }
    else {
        if (!param) {
            ajaxRequest.open('get', url, false);
            ajaxRequest.send();
        }
        else {
            ajaxRequest.open('post', url, false);
            ajaxRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=GB2312');
            ajaxRequest.send(param);
        }

        return ajaxRequest.responseText;
    }
}

myClass.getAjaxResult = function(request, callback, event) {
    if (request.readyState !== 4) return;
    if (request.status == 200) {
        callback(request.responseText);
    }
    else {
        try {
            var value = request.responseText;
            callback(value);
        }
        catch (e) {
            callback(e.message);
        }
    }
}

//reset textbox value;
myClass.resetText = function(sObj, focusStyle, defaultStyle) {
    if ((typeof sObj) == 'string') sObj = document.getElementById(sObj);
    if (!sObj) return; if (sObj.type !== 'text') return;

    sObj.onfocus = function() {
        if (this.value == this.defaultValue) {
            this.value = "";
            if (focusStyle) this.className = focusStyle;
        }
    }

    sObj.onblur = function() {
        if (this.value == "") {
            this.value = this.defaultValue;
            if (defaultStyle) this.className = defaultStyle;
        }
    }
}

//add class for object;
myClass.addClass = function (sObj, newClass) {
    if ((typeof sObj) == 'string') sObj = document.getElementById(sObj);
    if (!sObj) return;

    if (!sObj.className) {
        sObj.className = newClass;
    }
    else {
        var newClassName = sObj.className;
        newClassName += " " + newClass;
        sObj.className = newClassName;
    }
}

//insert newElement after targetElement
myClass.inserAfter = function (newElement, targetElement) {
    var parent = targetElement.parentNode;

    if (parent.lastChild == targetElement) {
        parent.appendChild(newElement);
    }
    else {
        parent.insertBefore(newElement, targetElement.nextSibling);
    }
}

myClass.showMsg = function (sObj, msg, event) {
    if ((typeof sObj) == 'string') sObj = document.getElementById(sObj);
    if (!sObj) return;

    var msgDiv = document.getElementById(sObj.id + "_opStatus");
    if (!msgDiv) {
        msgDiv = document.createElement("div");
        msgDiv.id = sObj.id + "_opStatus";
        sObj.appendChild(msgDiv);
    }

    if (msg !== '') {
        myClass.doShowHidden(msgDiv.id, "visibility");

        if (event) {
            try {
                var sStyle = { backgroundColor: "#FFFFFF", border: "1px solid #FF0000", width: "200px", fontSize: "20px", position: "absolute", height: "40px" };
                myClass.setStyle(msgDiv, sStyle);
                msgDiv.setAttribute("align", "center");
                msgDiv.style.left = (document.body.scrollLeft + event.clientX) - 120;
                msgDiv.style.top = (document.body.scrollTop + event.clientY) - 30;
            }
            catch (e) { }
        }

        msgDiv.innerHTML = msg;
    }
    else {
        msgDiv.innerHTML = '';
        myClass.doShowHidden(msgDiv.id, "hidden");
    }
}

myClass.setStyle = function (sObj, _style) {
    if ((typeof sObj) == 'string') sObj = document.getElementById(sObj);
    if (!sObj) return;

    for (var key in _style) {
        sObj.style[key] = _style[key];
    }
}

// if obj is on enter key down , do func
myClass.enterKey = function (sObj, func) {
    if ((typeof sObj) == 'string') sObj = document.getElementById(sObj);
    if (!sObj) return;

    sObj.onkeydown = function() {
        if (!event) event = window.event;
        if (event.keyCode == 13) {
            func(event);
            return false;
        }
    }
}

myClass.CPos = function (x, y) {
    this.x = x;
    this.y = y;
}

//get control Pos
myClass.GetObjPos = function (sObj) {
    if ((typeof sObj) == 'string') sObj = document.getElementById(sObj);
    
    var target = sObj;
    var pos = new myClass.CPos(target.offsetLeft, target.offsetTop);

    var target = target.offsetParent;
    while (target) {
        pos.x += target.offsetLeft;
        pos.y += target.offsetTop;

        target = target.offsetParent
    }

    return pos;
}


