var evalText = "var wolf = {}";
try{if(wolf)evalText = "";}catch(e){}
eval(evalText);


function using(sLibraryName, libraryWindow)
{
    syncLibraries();
	var objTemp 
	
	if(libraryWindow == null)
	{
	    libraryWindow  = top;
	}
	//try{objTop = top.wct.adam.window}catch(e){}
	
	var library = eval("libraryWindow." + sLibraryName)

	if (!library)
	{
		if(!libraryWindow.libraries)
		{
			libraryWindow.libraries = new Array();
		}

		if(!libraryWindow.libraries[sLibraryName])
		{
			libraryWindow.libraries[sLibraryName] = eval("libraryWindow.cls" + sLibraryName + ".toString()");     
		}

		try
		{
			eval("libraryWindow." + sLibraryName + " = new libraryWindow.cls" + sLibraryName + "(libraryWindow)")
		}
		catch(e)
		{
		
			eval("libraryWindow." + sLibraryName + " = new cls" + sLibraryName + "(libraryWindow)")
		}

	}
}

function syncLibraries()
{
	try
	{
	    if(top.libraries) return;
	    
	    var dialogArguments = window.dialogArguments;
	    if(dialogArguments == null)
	    {
	        if(top.opener.dialog)
	        {
	            dialogArguments = top.opener.dialog.arguments;
	        }
	    }
	    
	    // this assumes that the toip window has been passed as dialog argument
	    var openerWindowTop = dialogArguments;
	    
	    //this assumes that a WCT object has been passed as dialog argument
	    var workflowContext = dialogArguments;
	    
	    top.libraries = openerWindowTop.libraries;
	    
	    if(!top.libraries)
	    {
		    top.libraries = workflowContext.adam.libraries;
	    }
	    
	    if(!top.libraries)
	    {
		    return;
	    }
	    
	    for (var x in top.libraries) 
	    {
		    eval("top.cls" + x.toString() + " = " + top.libraries[x.toString()])
	    }   
	    
	}
	catch(e){}
}

//load xml prototypes
loadPrototypes();
function loadPrototypes()
{
    if(!window.addEventListener) return;

    //safari
    
    if(navigator.userAgent.indexOf("Safari") > 0)
    {
        HTMLElement.prototype.__setAttribute = HTMLElement.prototype.setAttribute;
        HTMLElement.prototype.setAttribute = function(attrName, newVal) 
        {  
            var prevVal = this.getAttribute(attrName);  
            this.__setAttribute(attrName, newVal);  
            newVal = this.getAttribute(attrName);  
            if (newVal != prevVal)  
            {    
                var evt = document.createEvent("MutationEvent");    
                evt.initMutationEvent("DOMAttrModified", true, false, this, prevVal || "", newVal || "", attrName, (prevVal == null) ? evt.ADDITION : evt.MODIFICATION );
                this.dispatchEvent(evt);  
            } 
        }    
    }        

    //add the load XML functiontothe Dom Document
    Document.prototype.loadXML = function(strXML)
    {
        var domParser = new DOMParser();

        var objDoc = domParser.parseFromString(strXML, "text/xml");

	    _copyDocuments(objDoc, this)
        
        updateReadyState(this,4)

    }

    Node.prototype.__defineGetter__("xml", _Node_getXML);

    Document.prototype.readyState           = "0";
    Document.prototype.onreadystatechange   = null;
    Document.prototype.parseError           = 0;

    XMLDocument.prototype.selectSingleNode      = _selectSingleNode;
    XMLDocument.prototype.selectNodes           = _selectNodes;
    
    //this is done because while cloning firefox removes event listners. This required onload to be trapped for readystate change every time a clone is requested.
    XMLDocument.prototype.cloneNodeFunction  = XMLDocument.prototype.cloneNode;
    XMLDocument.prototype.cloneNode          = _cloneNode;
    
    //for safari
    if(XMLDocument.prototype.load == null)
    {
        XMLDocument.prototype.load = _load;
    }
    
    
    Element.prototype.__defineGetter__("text", _element_getText);
    Element.prototype.__defineSetter__("text", _element_setText);
    Element.prototype.__defineGetter__("xml", _Node_getXML);
    
    Element.prototype.selectSingleNode = function(sXpath, XPathResultType, resultValue)  
    {
        if(this.ownerDocument.selectSingleNode)
        {
            return this.ownerDocument.selectSingleNode(sXpath, this, XPathResultType, resultValue);
        }
    }
    
    Element.prototype.insertAdjacentElement = function(where,parsedNode)
    {
        switch (where.toLowerCase())
        {
            case 'beforebegin':
	            this.parentNode.insertBefore(parsedNode,this)
	            break;
            case 'afterbegin':
	            this.insertBefore(parsedNode,this.firstChild);
	            break;
            case 'beforeend':
            
	            this.appendChild(parsedNode);
	            break;
            case 'afterend':
	            if (this.nextSibling) 
	                this.parentNode.insertBefore(parsedNode,this.nextSibling);
	            else 
	                this.parentNode.appendChild(parsedNode);
	            break;
            }
    }
    
    Element.prototype.insertAdjacentHTML = function(where,htmlStr)
    {
        var r = this.ownerDocument.createRange();
        r.setStartBefore(this);
        
        var parsedHTML = r.createContextualFragment(htmlStr);
        this.insertAdjacentElement(where, parsedHTML)
    }
    
    Element.prototype.selectNodes = function(sXpath)  
    {
        if(this.ownerDocument.selectNodes)
        {
            return this.ownerDocument.selectNodes(sXpath, this);
        }
    }
    
    function _copyDocuments(sourceDoc, targetDoc)
    {
        while (targetDoc.hasChildNodes())
        {
	        targetDoc.removeChild(targetDoc.lastChild);
	    }
    
        for (var i=0; i < sourceDoc.childNodes.length; i++) 
        {
            var objImportedNode = targetDoc.importNode(sourceDoc.childNodes[i], true);
            targetDoc.appendChild(objImportedNode);
        }
        
    }
    
    function _load(url) 
    {
	    var req = new XMLHttpRequest();
	    //req.onreadystatechange = _Document_onload;
        req.open("GET", url, false);
	    req.send("");
	    
	    _copyDocuments(req.responseXML, this)
	    //this = req.responseXML;
    }    
    
    function _Node_getXML() 
    {
        
        //create a new XMLSerializer
        var objXMLSerializer = new XMLSerializer();
        
        //get the XML string
        var strXML = objXMLSerializer.serializeToString(this);
        
        //return the XML string
        return strXML;
    }

    function _element_getText()
    {
        return this.textContent;
    }
    
    function _element_setText(value)
    {
        this.textContent = value;
    }
    
    function _selectSingleNode(sXPath, xmlDocument, XPathResultType, resultValue)
    {

        //ORDERED_NODE_SNAPSHOT_TYPE
        //FIRST_ORDERED_NODE_TYPE
        //UNORDERED_NODE_ITERATOR_TYPE
        //ORDERED_NODE_ITERATOR_TYPE
        //ORDERED_NODE_SNAPSHOT_TYPE
        //ANY_UNORDERED_NODE_TYPE
        
        if(!xmlDocument) xmlDocument = this;
        if(!XPathResultType) XPathResultType = XPathResult.FIRST_ORDERED_NODE_TYPE;
        if(!resultValue) resultValue = "singleNodeValue";
        
        var oEvaluator = new XPathEvaluator();

        var oResult = oEvaluator.evaluate(sXPath, xmlDocument, nsResolver, XPathResultType, null);
     
        if (oResult != null) 
        {
            return eval("oResult." + resultValue);
        } 
        else 
        {
            return null;
        }
    }
    
    function _selectNodes(xPath, xmlDocument)
    {
        if(!xmlDocument) xmlDocument = this;
     
        var oEvaluator = new XPathEvaluator();

        var oResult = oEvaluator.evaluate(xPath, xmlDocument, nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
        var aNodes = new Array();

        if (oResult != null) 
        {
            var oElement = oResult.iterateNext();
            while(oElement) 
            {
                aNodes.push(oElement);
                oElement = oResult.iterateNext();
            }
        }
        return aNodes;
    }

    function _cloneNode(value)
    {
        var XmlDom = this.cloneNodeFunction(value);
        if(XmlDom == null)
        {
            XmlDom = document.implementation.createDocument("","", null);
        }
        
        XmlDom.addEventListener("load", _Document_onload, false);
        
        return XmlDom;
    }
    
 
     function nsResolver(sPrefix)
    {
        switch(sPrefix)
        {
            case "soap":
                return "http://schemas.xmlsoap.org/soap/envelope/";
            case "xsi":
                return "http://www.w3.org/2001/XMLSchema-instance" ;
            case "xsd":
                return "http://www.w3.org/2001/XMLSchema" ;
            case "xsl":
                return "http://www.w3.org/1999/XSL/Transform" ;
            case "transformiix":
                return "http://www.mozilla.org/TransforMiix" ;

        }
        return null;    
    }

 
    //function traps the onload and updatres the error message before it updates the readystate
    function _Document_onload()
    {
        handleOnLoad(this)
    }

    //updates the parsererror so that if any IE code is trying to identify an error it can find one.
    function handleOnLoad(xmlDomDoc) 
    {
        if (!xmlDomDoc.documentElement || xmlDomDoc.documentElement.tagName == "parsererror")
        {
            xmlDomDoc.parseError = 9886120856;
        }
        updateReadyState(xmlDomDoc, 4);
    }

    //finally updates ready state and calls the eventhandler.
    function updateReadyState(xmlDomDoc, readyState) 
    {
        if(xmlDomDoc.readyState == null)
        {
            xmlDomDoc.readyState = readyState;
        
            if (xmlDomDoc.onreadystatechange != null && typeof(xmlDomDoc.onreadystatechange) == "function")
            {
                xmlDomDoc.onreadystatechange();
            }
        }
    }
  
}