﻿//清空所有節點-purge(document.body);
//function purge(d) {
//    var a = d.attributes, i, l, n;
//    if (a) {
//        l = a.length;
//        for (i = 0; i < l; i += 1) {
//            n = a[i].name;
//            if (typeof d[n] === 'function') {
//                d[n] = null;
//            }
//        }
//    }
//    a = d.childNodes;
//    if (a) {
//        l = a.length;
//        for (i = 0; i < l; i += 1) {
//            purge(d.childNodes[i]);
//        }
//    }
//}
//===========================================================================
// Array.remove
//===========================================================================
Array.prototype.remove = function(from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};
//===========================================================================
// Firefox
//===========================================================================
//outerHTML for firefox
if (typeof (HTMLElement) != "undefined" && !window.opera) {
    HTMLElement.prototype.__defineGetter__("outerHTML", function() {
        var a = this.attributes, str = "<" + this.tagName, i = 0; for (; i < a.length; i++)
            if (a[i].specified)
            str += " " + a[i].name + '="' + a[i].value + '"';
        if (!this.canHaveChildren)
            return str + " />";
        return str + ">" + this.innerHTML + "</" + this.tagName + ">";
    });
    HTMLElement.prototype.__defineSetter__("outerHTML", function(s) {
        var r = this.ownerDocument.createRange();
        r.setStartBefore(this);
        var df = r.createContextualFragment(s);
        this.parentNode.replaceChild(df, this);
        return s;
    });
}
//XmlHttp cross browser
function getXmlHttp() {
    var A = null;
    try {
        A = new ActiveXObject("Msxml2.XMLHTTP")
    } catch (e) {
        try {
            A = new ActiveXObject("Microsoft.XMLHTTP")
        } catch (oc) {
            A = null
        }
    }
    if (!A && typeof XMLHttpRequest != "undefined") {
        A = new XMLHttpRequest()
    }
    return A
}
//XmlDom cross browser
function getXmlDom() {
    if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLDOM");
    }
    else {
        if (document.implementation && document.implementation.createDocument) {
            Document.prototype.loadXML = function(sXml) {
                var oParser = new DOMParser();
                var _xmlDom = oParser.parseFromString(sXml, "text/xml");
                while (this.firstChild) {
                    this.removeChild(this.firstChild);
                }
                for (var i = 0; i < _xmlDom.childNodes.length; i++) {
                    var oNewNode = this.importNode(_xmlDom.childNodes[i], true);
                    this.appendChild(oNewNode);
                }
            }
            return document.implementation.createDocument("", "", null);
        }
    }
}
//XmlDom.xml for firefox
function getXml(doc) {
    if (doc.xml)
        return doc.xml;
    else {
        try {
            return (new XMLSerializer).serializeToString(doc);
        }
        catch (e) {
        }
    }
}
//===========================================================================
// Cookie
//===========================================================================
////取得Cookie值
//function GetCookieVal(offset) {
//    var endstr = document.cookie.indexOf(";", offset);
//    if (endstr == -1)
//        endstr = document.cookie.length;
//    return unescape(document.cookie.substring(offset, endstr));
//}
////設定Cookie
//function SetCookie(name, value) {
//    var expdate = new Date();
//    var argv = SetCookie.arguments;
//    var argc = SetCookie.arguments.length;
//    var expires = (argc > 2) ? argv[2] : null;
//    var path = (argc > 3) ? argv[3] : null;
//    var domain = (argc > 4) ? argv[4] : null;
//    var secure = (argc > 5) ? argv[5] : false;
//    if (expires != null) expdate.setTime(expdate.getTime() + (expires * 1000));
//    document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expdate.toGMTString()))
//+ ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain))
//+ ((secure == true) ? "; secure" : "");
//}
////刪除Cookie
//function DelCookie(name) {
//    var exp = new Date();
//    exp.setTime(exp.getTime() - 1);
//    var cval = GetCookie(name);
//    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
//}
////取得Cookie值
//function GetCookie(name) {
//    var arg = name + "=";
//    var alen = arg.length;
//    var clen = document.cookie.length;
//    var i = 0;
//    while (i < clen) {
//        var j = i + alen;
//        if (document.cookie.substring(i, j) == arg)
//            return GetCookieVal(j);
//        i = document.cookie.indexOf(" ", i) + 1;
//        if (i == 0) break;
//    }
//    return null;
//}

function GetServerUrl() {
    return location.href.substring(0, location.href.lastIndexOf("/")) + "/";
}

//===========================================================================
// Provides a Dictionary object for client-side java scripts
//===========================================================================
function Lookup(key) {
    try { return (this[key]); }
    catch (e) { return null; }
}

function Index(n) {

    try { return (this[this.Keys[n]]); }
    catch (e) { return null; }
}

function Delete() {
    for (c = 0; c < Delete.arguments.length; c++) {
        this[Delete.arguments[c]] = null;
    }
    // Adjust the keys (not terribly efficient)
    var keys = new Array()
    for (var i = 0; i < this.Keys.length; i++) {
        if (this[this.Keys[i]] != null)
            keys[keys.length] = this.Keys[i];
    }
    this.Keys = keys;
}

function DeleteAt() {
    for (c = 0; c < Delete.arguments.length; c++) {
        this[this.Keys[Delete.arguments[c]]] = null;
    }
    // Adjust the keys (not terribly efficient)
    var keys = new Array()
    for (var i = 0; i < this.Keys.length; i++) {
        if (this[this.Keys[i]] != null)
            keys[keys.length] = this.Keys[i];
    }
    this.Keys = keys;
}

function Add() {
    for (c = 0; c < Add.arguments.length; c += 2) {
        // Add the property
        this[Add.arguments[c]] = Add.arguments[c + 1];
        // And add it to the keys array
        this.Keys[this.Keys.length] = Add.arguments[c];
    }
}

function Clear() {
    for (var i = 0, length = this.Keys.length; i < length; i++) {
        this[this.Keys[i]] = null;
    }
    this.Keys = new Array();
}

function Dictionary() {
    this.Add = Add;
    this.Lookup = Lookup;
    this.Delete = Delete;
    this.Index = Index;
    this.Clear = Clear;
    this.Keys = new Array();
}