var evalText = "var wolf = {}";
try{if(wolf)evalText = "";}catch(e){}
eval(evalText);


if (top.context != null) {
    //		objTop.cacheUtil    = objTop.context.cacheUtil;
    top.cache = top.context.cache;
}
else {
    top.cache = new Array();
}


var wflib;

function using(sLibraryName, libraryWindow)
{
    
	var objTemp 
	
    if(wflib != null)
    {
        libraryWindow = wflib;
        library = eval("libraryWindow." + sLibraryName);
    }
    else
    {

        if (libraryWindow == null) {
            libraryWindow = window;
	        for (var i = 0; i < 5; i++) {
	            if (libraryWindow.clsHelperLib != null) {
	                break;
	            }
	            libraryWindow = libraryWindow.parent;
            }
	        
	    }
	
	    var library;
	    wflib   = libraryWindow;

	    syncLibraries();
        //try{objTop = top.wct.adam.window}catch(e){}
    	
	    for ( var i = 0; i < 5; i++)
	    {
    	    
	        try
	        {
    	        library = eval("libraryWindow." + sLibraryName);
    	        if(library != null)
    	        {
    	            wflib   = libraryWindow;
	                break;
                }
                else
                {
                    if( eval("libraryWindow.cls" + sLibraryName + "(wflib)")  != null )
                    {
                        wflib = libraryWindow;
                    
                    }
                    libraryWindow = libraryWindow.parent;
                }
                
                
    	        
	        }
	        catch(e)
	        {
	            wflib = window;
	            break;
            }
            
        }
    }


	if (!library)
	{
	    if (!wflib.libraryDef)
		{
		    wflib.libraryDef = new Array();
		}

		if (!wflib.libraryDef[sLibraryName])
		{
		    try { wflib.libraryDef[sLibraryName] = eval("wflib.cls" + sLibraryName + ".toString()"); } catch (e) { }    
		}

		try
		{
			eval("wflib." + sLibraryName + " = new wflib.cls" + sLibraryName + "(wflib)")
		}
		catch(e)
		{

		    try { eval("wflib." + sLibraryName + " = new cls" + sLibraryName + "(wflib)") } catch (e) { }
		}

	}
}

function syncLibraries()
{
	try
	{
	    if (top.libraryDef) {

	        wflib.libraryDef = top.libraryDef;
        }
          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.libraryDef = openerWindowTop.libraryDef;

	    if (!top.libraryDef)
	    {
	        top.libraryDef = workflowContext.adam.libraryDef;
	    }

	    if (!top.libraryDef)
	    {
		    return;
	    }

		for (var x in top.libraryDef) 
	    {
	        eval("top.cls" + x.toString() + " = " + top.libraryDef[x.toString()])
	    }   
	    
	}
	catch(e){}
}

//load xml prototypes
try{if (ctr == null)ctr = 0}catch(e){ctr = 0}
ctr++
if(ctr == 1) 
{
    loadPrototypes();    
}

function loadPrototypes()
{
    if(navigator.userAgent.indexOf("MSIE") >= 0) 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);
            var df = targetDoc.createDocumentFragment()
            df.appendChild(objImportedNode)
            targetDoc.appendChild(df);
        }
        
    }


    function _load(url) 
    {
        var xmlDoc = this;
        var req = new XMLHttpRequest();

        if (xmlDoc.onreadystatechange || xmlDoc.onload) {

            req.onreadystatechange = _loadReady;
            
        }
        
        req.open("GET", url, false);
	    req.send("");
	    if (!(xmlDoc.onreadystatechange || xmlDoc.onload)) {
	        _copyDocuments(req.responseXML, xmlDoc)
	    } //this = req.responseXML;

	    function _loadReady() {
	        if (req.readyState == 4) {
                if (req.responseXML.selectSingleNode("/parsererror") != null) {
	                xmlDoc.parseError = 9886120856;
	            }
	            else {
	                xmlDoc.loadXML(req.responseXML.xml);
	            }
                
//	            alert(xmlDoc.xml)
//	            updateReadyState(xmlDoc, 4)
	        }
        }
    }    
    
    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.onreadystatechange != null  && typeof (xmlDomDoc.onreadystatechange) == "function" )
        {
            xmlDomDoc.readyState = readyState;
            xmlDomDoc.onreadystatechange();
        }
        if (xmlDomDoc.onload != null && typeof (xmlDomDoc.onload) == "function" ) {
            
            xmlDomDoc.onload();
        }

    }
  
}
