String.prototype.trim = function () {
  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};
Object.extendprototype = function(dest, src) {
  for (prop in src) {
    dest[prop] = src[prop];
  }
  return dest;
};
Function.prototype.Inherits = function(parent)
{
	this.prototype = new parent();
	this.prototype.constructor = this;
};
Object.prototype.Inherits = function(parent)
{
	if( arguments.length > 1 )
	{
		parent.apply( this, Array.prototype.slice.call( arguments, 1 ) );
	}
	else
	{
		parent.call( this );
	}
};
function apply(methodInfo) {
    var retObj = methodInfo.method.apply(methodInfo.owner, methodInfo.args);
    methodInfo.ResetArguments();
    return retObj;
};
function eventCallback(_1, _2) {
return function(evt) {
_2.apply(_1, arguments);
}
};
function callback(_1, _2, _3) {
return function() {
_2.apply(_1, _3);
}
};
function MethodInfo() {
    var args = MethodInfo.arguments;
    this.owner = args[0];
    this.method = args[1];
    this.args = Array.prototype.slice.call(args, 2);
};

MethodInfo.prototype = {
SetArguments : function() {
this.tmpArgs = this.args;
this.args = Array.prototype.slice.call(this.SetArguments.arguments, 0);
this.args = this.args.concat(this.tmpArgs);
},
ResetArguments : function() {
if (this.tmpArgs != null)
this.args = this.tmpArgs;
}
};

function Rect() {
var args = Rect.arguments;
this.x = 0;
this.y = 0;
this.w = 0;
this.h = 0;
if (args.length > 0)
this.x = args[0];
if (args.length > 1)
this.y = args[1];
if (args.length > 2)
this.w = args[2];
if (args.length > 3)
this.h = args[3];
};

Rect.prototype = {
AddPosition : function(rect2) {
this.x += rect2.x;
this.y += rect2.y;
},
Clone : function() {var ret = new Rect();ret.x = this.x;ret.y = this.y;ret.w = this.w;ret.h = this.h;return ret;},ToString : function() {return "Rect";}};

function XmlEngine() {};
XmlEngine.prototype = {
    CreateXmlDocument : function(startNode) {
        engine.xmlCount++;
        var doc = null;
        if (window.ActiveXObject) {
            doc = new ActiveXObject("Msxml2.DOMDocument");
            doc.setProperty("SelectionLanguage", "XPath");
        } else if (document.implementation) {
            doc = document.implementation.createDocument("", "", null);
        } else {
	        var e = new Exception(engine.CONST.EX_SEV_CRITICAL, NO_XML_SUPPORT, "Your browser does not support Xml parsing.");
	        throw e;
        }

        if (startNode != null)
            doc.appendChild(doc.createElement(startNode));

        return doc;
    },
    
    LoadXml : function(strData) {
        if (window.ActiveXObject) {
            var xmlDoc = this.CreateXmlDocument();
            if (!xmlDoc.loadXML(strData)) {
	            var e = new Exception(engine.CONST.EX_SEV_MODERATE, XML_PARSING_FAILED, "Error parsing xml data.");
	            throw e;
            }
            return xmlDoc;
        } else {
            var oParser = new DOMParser();
            return oParser.parseFromString(strData, "text/xml");
        }       
    },
    
    SelectSingleNode : function(doc, strNodePath) {
        if (window.ActiveXObject) {
            return doc.selectSingleNode(strNodePath);
        } else {
            var oResult = null;
            var xpth_res = null;
            if (doc.ownerDocument) {
                xpth_res = doc.ownerDocument.evaluate(strNodePath, doc, null, XPathResult.ANY_TYPE, null);
            } else {
                xpth_res = doc.evaluate(strNodePath, doc, null, XPathResult.ANY_TYPE, null);
            }
            if (xpth_res.resultType == XPathResult.UNORDERED_NODE_ITERATOR_TYPE || xpth_res.resultType == XPathResult.ORDERED_NODE_ITERATOR_TYPE) {
                oResult = xpth_res.iterateNext();
            } else if (xpth_res.resultType == XPathResult.ANY_UNORDERED_NODE_TYPE || xpth_res.resultType == XPathResult.FIRST_ORDERED_NODE_TYPE) {
                oResult = xpth_res.singleNodeValue;
            }
            return oResult;
        }
    },
    
    SelectNodes : function(doc, strNodePath) {
        if (window.ActiveXObject) {
            return doc.selectNodes(strNodePath);
        } else {
            var oResult = new Array();
            var xpth_res = null;
            if (doc.ownerDocument) {
                xpth_res = doc.ownerDocument.evaluate(strNodePath, doc, null, XPathResult.ANY_TYPE, null);
            } else {
                xpth_res = doc.evaluate(strNodePath, doc, null, XPathResult.ANY_TYPE, null);
            }

            if (xpth_res.resultType == XPathResult.UNORDERED_NODE_ITERATOR_TYPE || xpth_res.resultType == XPathResult.ORDERED_NODE_ITERATOR_TYPE) {
                var tmpNode = xpth_res.iterateNext();
                while (tmpNode) {
                    oResult[oResult.length] = tmpNode;
                    tmpNode = xpth_res.iterateNext();
                }
                return oResult;
            }
            return null;
        }
    },
    
    RemoveAll : function(node) {
        this.RemoveNodeChildren(node);
        this.RemoveNodeAttributes(node);
    },
    
    RemoveNodeAttributes : function(node) {
        while (node.attributes.length > 0) {
            node.removeAttributeNode(node.attributes[0]);
        }
    },
    
    RemoveNodeChildren : function(node) {
        while (node.hasChildNodes()) {
            node.removeChild(node.firstChild);
        }
    },
    
    RemoveAllChildren : function(doc, strNodePath) {
        if (strNodePath != null) {
            var tmpNode = this.SelectSingleNode(doc, strNodePath);
            if (tmpNode != null) {
                while (tmpNode.hasChildNodes()) {
                    tmpNode.removeChild(tmpNode.firstChild);
                }
            }
        } else {
            if (doc != null) {
                while (doc.hasChildNodes()) {
                    doc.removeChild(doc.firstChild);
                }
            }
        }
    },
    
    ApplyToDocumentNode : function(doc, domNode) {
        var startNode = null;
        if (arguments.length != 3) {
            startNode = doc.firstChild;
        } else {
            startNode = arguments[2];
        }
        this.ApplyXmlNodeToDocumentNode(domNode, document, startNode);
    },
    
    ApplyXmlNodeToDocumentNode : function(domNode, domDocument, node) {
        if (node.nodeType == 1) {
            var element = domDocument.createElement(node.nodeName);
            for (var i = 0; i < node.attributes.length; i++) {
                var attrib = domDocument.createAttribute(node.attributes[i].nodeName);
                attrib.nodeValue = node.attributes[i].nodeValue;
                element.attributes.setNamedItem(attrib);
            }
            domNode.appendChild(element);
            for (var i = 0; i < node.childNodes.length; i++) 
                this.ApplyXmlNodeToDocumentNode(element, domDocument, node.childNodes[i]);
        } else if (node.nodeType == 3) {
            var textNode = domDocument.createTextNode(node.nodeValue); 
            domNode.appendChild(textNode);
        }
    },
    
    PerformXslt : function(xslDoc, xmlDataNode) {
        if (window.ActiveXObject) {
            var retObj = engine.PrepareXsltResult();
            xmlDataNode.transformNodeToObject(xslDoc, retObj);
            return retObj;
        } else if (document.implementation.createDocument) {
            var xsltProcessor = new XSLTProcessor();
            xsltProcessor.importStylesheet(xslDoc);
            return xsltProcessor.transformToDocument(xmlDataNode);
        }
    },

    GetAttribute : function(node, attribute, defaultVal) {
        var retVal = null;
        var tmpAttrib = node.attributes.getNamedItem(attribute);
        if (tmpAttrib != null)
            retVal = tmpAttrib.nodeValue;
        if (retVal == null && defaultVal != null) {
            retVal = defaultVal;
        }
        return retVal;
    },

    GetTextNode : function(node) {
        if (window.ActiveXObject) {
            return node.text;
        } else {
            return node.textContent;
        }
        return null;
    },

    SetTextNode : function(node, val) {
        if (window.ActiveXObject) {
            node.text = val;
        } else {
            node.textContent = val;
        }
    },

    ToString : function() {
        return "Xml";
    }    
};

function PortalEngine() {
	// Browser check
	var ua = navigator.userAgent;
	this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	this.isGecko = ua.indexOf("Gecko") != -1;
	this.isSafari = ua.indexOf("Safari") != -1;
	this.isOpera = ua.indexOf("Opera") != -1;
	this.isMac = ua.indexOf("Mac") != -1;
	this.isNetscape = ua.indexOf("Netscape") != -1;
	this.isWinCE = ua.indexOf("Windows CE") != -1;
	this.isLoaded = false;
	this.isDebug = false;
	this.ControlIds = new Array();
	this.Controls = new Object();
	this.Xml = new XmlEngine();
};

PortalEngine.prototype = {
    Start: function() {
        this.isLoaded = true;
        for (var i=0; i<this.ControlIds.length; i++) {
            var tmpCtrl = null;
            var evalStr = "tmpCtrl = new " + this.ControlIds[i] + "();";
            eval(evalStr);
            this.Controls[tmpCtrl.id] = tmpCtrl;
        }
        //execute onload
        for (var i=0; i<this.ControlIds.length; i++) {
            this.Controls[this.ControlIds[i]].OnLoad();
        }
    },
    
    SubmitForm : function(form) {
	    var theForm = document.forms[form];
        if (!theForm) {
            theForm = document.form1;
        }
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
            theForm.submit();
        }
    },
    
    GetControl : function(id) {
        return this.Controls[id];
    },
    
	GetElement : function(elementId) {
	    if (document.getElementById) {
          return document.getElementById(elementId);
	    } else {
		    // pocket pc workaround
		    return document.forms[0][elementId];
	    }
	  return null;
	},

    GetEventSource : function(evtObj) {
        var obj = null;
        if (window.event) {
            return evtObj.srcElement;
        } else {
            return evtObj.target;
        }
    },
    
    ElementContains : function(sE, dE) {
        while(dE) {
            if (dE == sE)
                return true;
            dE = dE.parentNode;
        }
        return false;
    },

    ElementCoords : function(obj, toparent, withScroll) {
        var iY = 0;
        var iX = 0;
        var iW = obj.offsetWidth;
        var iH = obj.offsetHeight;
        if (withScroll == null)
            withScroll = false;
        while(obj != null && obj != toparent) {
            iY += obj.offsetTop;
            iX += obj.offsetLeft;
            if (withScroll) {
                iY -= obj.scrollTop;
                iX -= obj.scrollLeft;
            }
            obj = obj.offsetParent;
        }
        return new Rect(iX, iY, iW, iH);
    },

	AddEvent : function(o, n, h) {
	    if (o != null) {
		    if (o.attachEvent) {
			    o.attachEvent("on" + n, h);
		    } else if (o.addEventListener) {
			    o.addEventListener(n, h, false);
		    } else {
		    }
	    }
	},

	RemoveEvent : function(o, n, h) {
	    if (o != null) {
		    if (o.detachEvent) {
			    o.detachEvent("on" + n, h);
		    } else if (o.addEventListener) {
			    o.removeEventListener(n, h, false);
		    } else {
		    }
	    }
	},
	
	AddEventWithCallback : function(o, n, i, f) {
  	    this.AddEvent(o, n, eventCallback(i, f));
	},
	
	RemoveEventWithCallback : function(o, n, i, f) {
	    this.RemoveEvent(o, n, eventCallback(i, f));
	},
	
	RegisterControl : function(id) {
	    this.ControlIds.push(id);
	},
	
    OpenUrl : function(url) {
        window.location.href = url;
    },

    ToString : function() {
        return "PortalEngine";
    }
};
var engine = new PortalEngine();
engine.AddEventWithCallback(window, "load", engine, engine.Start);

function PortalControl(id) {
};

Object.extendprototype(PortalControl.prototype, {
    AddEventToPattern : function(parent, tagName, eventName, patternId, owner, method) {
        oColl = parent.getElementsByTagName(tagName);
        for (var i=0; i<oColl.length; i++) {
            var obj = oColl[i];
            var oId = obj.id;
            if (oId != null && oId.substr(0, patternId.length) == patternId) {
                //attach event
                engine.AddEventWithCallback(obj, eventName, owner, method);
            }
        }
    },
    ApplyToNodeWithRemove : function(xml, container) {
        while (container.hasChildNodes())
            container.removeChild(container.firstChild);
        if (xml != null && xml.firstChild != null)
            engine.Xml.ApplyToDocumentNode(xml, container);
    }
});
function XmlRequest(path) {
    if (path==null)
        path = "XmlRequests.ashx";
    this.xmlRequestPath = path;
    this.xmlRequestDoc = null;
    this.xmlHttp = null;
    this.callbackMethod = null;
    this.controlId = null;
    this.controlType = null;
};
XmlRequest.prototype = {
    SetCallback : function(method) {
        this.callbackMethod = method;
    },
    
    Instantiate : function() {
        if (window.ActiveXObject) {
            this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } else {
            this.xmlHttp = new XMLHttpRequest()
        }
    },

    // parameters
    // 1: procedure name
    // 2: control name (=definitionid)
    // 3: definitionparameter
    // 4: execution type (SQL or NET)
    // ... parameter list (name, value)
    CreateRequestDocument : function() {
        var oArgs = arguments;
        this.xmlRequestDoc = engine.Xml.CreateXmlDocument();
        var rootEl = this.xmlRequestDoc.createElement("procedure");
        this.xmlRequestDoc.appendChild(rootEl);
        rootEl.setAttribute("name", oArgs[0]);
        rootEl.setAttribute("ct", oArgs[1]);
        rootEl.setAttribute("did", oArgs[2]);
        rootEl.setAttribute("dp", oArgs[3]);
        rootEl.setAttribute("type", oArgs[4]);
        rootEl.setAttribute("portal", "true");
        var paramsEl = this.xmlRequestDoc.createElement("parameters");
        rootEl.appendChild(paramsEl);
        for (var i=0; i<(oArgs.length-5)/2; i++) {
            var paramEl = this.xmlRequestDoc.createElement("param");
            paramEl.setAttribute("name", oArgs[i*2+5]);
            var value = oArgs[i*2+6];
            if (value.nodeName) {
                if (value.nodeName == "#document") {
                    paramEl.appendChild(value.firstChild.cloneNode(true));
                } else {
                    paramEl.appendChild(value.cloneNode(true));
                }
            } else {
                var paramVal = this.xmlRequestDoc.createTextNode(value.toString());
                paramEl.appendChild(paramVal);
            }
            paramsEl.appendChild(paramEl);
        }
    },

    MakeRequest : function() {
        this.Instantiate();
        this.xmlHttp.onreadystatechange = callback(this, this.InternalHandler, arguments);
        this.xmlHttp.open("POST", this.xmlRequestPath, true);
        if (this.xmlRequestDoc != null) {
            this.xmlHttp.send(this.xmlRequestDoc);
        }
        else {
            this.xmlHttp.send(null);
        }
    },
    
    InternalHandler : function() {
        if (this.xmlHttp.readyState == 4) {
            if (this.callbackMethod != null) {
                var tmpArgs = Array.prototype.slice.call(this.InternalHandler.arguments);
                tmpArgs.splice(0, 0, this.xmlHttp.responseXML);
                this.callbackMethod.args = tmpArgs;
                apply(this.callbackMethod);
            }
        }
    },
    
	ToString : function() {
		return "XmlRequest";
	}
};