// Global.js
// version 0.0.4
// author:      Michel Carroll
// modified:    2005-11-28


var pageUrl = this.location.href.indexOf("?")==-1?this.location.href:this.location.href.substring(0,this.location.href.indexOf("?"))

var _appRoot = ""
var frameRef=null

function getFrame(ref){
    aPth=ref.split(".")
    pth="top"
    for(var i=0;i<aPth.length;i++){
        pth+=".frames["+aPth[i]+"]"
    }
    return eval(pth)
}

function getApp(){
    frameRef=getFrameId(window)
    par=parent
    while(par._App==null){
        frameRef=getFrameId(par)+"."+frameRef
        if(par==top)return null
        par=par.parent;
    }
    par.App.registerFrame(frameRef,location.pathname)
    return par.App
}

var App = getApp()

if(App!=null) window.onfocus=function(){App.setFocus()}

function getFrameId(win){
    win=(win==null?window:win)
    if(window==top){
        return "top"
    }
    for(var i=0;i<win.parent.frames.length;i++){
        if(win.parent.frames[i]==win)return i
    }
    return null
}

if (typeof(Array.prototype.toSource)=='undefined'){ 

	function a_toSource() {
        var _s=""
        for(var i=0;i<this.length;i++){
            if(typeof this[i]=="string"){_s+=",'"+this[i]+"'"}
            else if(typeof this[i]=="object"){_s+=","+this[i].toSource()}
            else _s+=","+this[i]
        }
        return "["+_s.substring(1)+"]"
	}
	Array.prototype.toSource = a_toSource;
}


function Action(fn,args){
    var _x=this
    if(fn.indexOf("(")!=-1){
        brkt=fn.replace(/[^(]+\(([^)]*)\)/,"$1")
        args=[brkt]
        fn=fn.replace(/\(.*\)/,"")
    }
	args=(args==null?[]:args)
    this.fn=fn
    this.args=args
    this.run=function(){
		strRun=_x.fn+"("+_x.args.toSource()+")"
		try{eval(strRun)}catch(e){}
    }
}

function getDomPos(el){
    var p=el.parentNode
    for(var i=0;i<p.childNodes.length;i++){
        if(p.childNodes[i]==el)return i
    }
    return null
}

function getNthElKid(el,ord){
    var i = 0
    var el = el.firstChild
    while(el && el.nodeType != 1 && i<ord){
        el = el.nextSibling
        i++
    }
    return el
}

function getNextElSib(el){
    do{el = el.nextSibling}while(el && el.nodeType != 1)
    return el
}

function getPrevElSib(el) {
    do{el = el.previousSibling}while(el && el.nodeType != 1)
    return el
}

function get1stElKid(el){
    var el = el.firstChild
    while(el && el.nodeType != 1)el = el.nextSibling
    return el
}

function getEl(id){
    return document.getElementById(id)
}

function getAncestor(el,tag){
    while(el.tagName.toLowerCase()!=tag){
        if(el.parentNode.tagName.toLowerCase()=="body")return null
        el=el.parentNode
    }
    return el
}

function deriveEl(el){
    if(typeof(el)=="object"){
        el=el[0]
    }
    if(typeof(el)=="string"){
        el=getEl(el)
    }
    return el
}

    function HTMLEscape(str){
        return str.replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;")
    }


function StringBuilder(){
	this.arr = new Array()
}

StringBuilder.prototype.Add = function(str){
	this.arr[this.arr.length]=str
}

StringBuilder.prototype.AddLine = function(str){
	this.arr[this.arr.length]=str+"\n"
}

StringBuilder.prototype.Empty = function(){
	this.arr = new Array()
}

StringBuilder.prototype.toString = function(){
	str=this.arr.join("")
	this.arr=null
	return str
}

// test functions
function alertWindow(msg){
    w=window.open("","wwin","height=300px,width=300px,toolbar=no,resizable=yes,scrollbars=yes")
    w.document.write("<html><head><title>Alert window</title><style>body{font-family:tahoma;font-size:11px;color:firebrick}</style></head><body>"+msg.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/&gt;&lt;/g,"&gt;<br>&lt;")+"</body></html>")
}

function transAndAppend(hNdId,squash,prams,xmlDoc,xslDoc,debug){
    var hNd = document.getElementById(hNdId)
    
    if(document.implementation.createDocument){
        if(squash){
            while(hNd.childNodes.length>0){
                hNd.removeChild(hNd.lastChild)
            }
        }
	    var xslP = new XSLTProcessor()
	    xslP.importStylesheet(xslDoc)
	    for(var i=0;i<prams.length;i++){
		    xslP.setParameter(null,prams[i][0],prams[i][1])
	    }
	    var frag = xslP.transformToFragment(xmlDoc, document);
	    if(debug){
	        alertWindow(getXmlSource(frag))
	    }
	    hNd.appendChild(frag)
    }else{
	    if(prams!=null){
		    for(var i=0;i<prams.length;i++){
			    el = xslDoc.selectSingleNode("//xsl:param[@name='"+prams[i][0]+"']")
			    if(el!=null)el.setAttribute("select", "'"+prams[i][1]+"'")
		    }
	    }
	    var strHTML =  xmlDoc.transformNode(xslDoc)
	    if(squash){
	        hNd.innerHTML = strHTML
	    }else{
	        hNd.insertAdjacentHTML("beforeEnd",strHTML)
	    }
	    if(debug){
	        alertWindow(strHTML)
	    }
    }
}

function XmlDoc(nd){
    w3 = (document.implementation && document.implementation.createDocument?true:false)
    if(typeof(nd)=="string"){
	    if(w3){
	        var parser = new DOMParser()
            xDoc = parser.parseFromString(nd, "text/xml")
	    }else{
		    xDoc = new ActiveXObject("MSXML2.DOMDocument")//("MSXML2.DOMDocument.4.0")
		    xDoc.loadXML(nd)
	    }
    }else{
        if(w3){
	        xDoc = document.implementation.createDocument("", "", null);
        }else{
	        xDoc = new ActiveXObject("MSXML2.DOMDocument")//("MSXML2.DOMDocument.4.0")
        }
        if(nd!=null){
            if(w3){
                xNd = xDoc.importNode(nd,true);
                xDoc.appendChild(xNd)
            }else{
                xDoc.loadXML(nd.xml)
            }
        }
    }
    return xDoc
}

function getNodeSet(xdoc,context,path){
	try{
		nsr=xdoc.createNSResolver(xdoc.documentElement)
		xpres = xdoc.evaluate(path,context,nsr,7, null)
		cln=new Array(xpres.snapshotLength)
		for(var i=0;i<xpres.snapshotLength;i++){
			cln[i]=xpres.snapshotItem(i)
		}
	}catch(e){
		cln=context.selectNodes(path)
	}
	return cln
}

function getNode(xdoc,context,path){
	try{
		nsr=xdoc.createNSResolver(xdoc.documentElement)
		xpres = xdoc.evaluate(path,context,nsr,9, null)
		return xpres.singleNodeValue
	}catch(e){
		return context.selectSingleNode(path)
	}
}

function hNodeToXmlDoc(el,xslDoc){
	sXml = innerXHTML(el)
	if(document.implementation && document.implementation.createDocument){
	    var xslP = new XSLTProcessor()
	    xslP.importStylesheet(xslDoc)
	    var parser = new DOMParser()
        tmpDoc = parser.parseFromString(sXml, "text/xml")
	    xdoc = xslP.transformToDocument(tmpDoc)
	}else{
		tmpDoc = new ActiveXObject("MSXML2.DOMDocument")//("MSXML2.DOMDocument.4.0")
		xdoc = new ActiveXObject("MSXML2.DOMDocument")//("MSXML2.DOMDocument.4.0")
		tmpDoc.loadXML(sXml)
		sNew = tmpDoc.transformNode(xslDoc)
	    //alert(sNew)
	    xdoc.loadXML(sNew)
	}
	tmpDoc=null
	return xdoc
}

function XHTMLNodeToXmlDoc(el,xslDoc){
	sXml = innerXHTMLValid(el)
	if(document.implementation && document.implementation.createDocument){
	    var xslP = new XSLTProcessor()
	    xslP.importStylesheet(xslDoc)
	    var parser = new DOMParser()
        tmpDoc = parser.parseFromString(sXml, "text/xml")
	    xdoc = xslP.transformToDocument(tmpDoc)
	}else{
		tmpDoc = new ActiveXObject("MSXML2.DOMDocument")//("MSXML2.DOMDocument.4.0")
		xdoc = new ActiveXObject("MSXML2.DOMDocument")//("MSXML2.DOMDocument.4.0")
		tmpDoc.loadXML(sXml)
		sNew = tmpDoc.transformNode(xslDoc)
	    //alert(sNew)
	    xdoc.loadXML(sNew)
	}
	tmpDoc=null
	return xdoc
}


function xslTransform(xmlDoc,xslDoc,prams){ 
	if(typeof(xmlDoc)=="string"){
	    xmlDoc = new XmlDoc(xmlDoc)
	}
	if(document.implementation && document.implementation.createDocument){
	    var xslP = new XSLTProcessor()
	    xslP.importStylesheet(xslDoc)
	    if(prams!=null){
	        for(var i=0;i<prams.length;i++){
		        xslP.setParameter(null,prams[i][0],prams[i][1])
	        }
	    }
	    xdoc = xslP.transformToDocument(xmlDoc)
	}else{
	    if(prams!=null){
		    for(var i=0;i<prams.length;i++){
			    el = xslDoc.selectSingleNode("//xsl:param[@name='"+prams[i][0]+"']")
			    if(el!=null)el.setAttribute("select", "'"+prams[i][1]+"'")
		    }
	    }
		xdoc = new ActiveXObject("MSXML2.DOMDocument")//("MSXML2.DOMDocument.4.0")
		sXml = xmlDoc.transformNode(xslDoc)
	    xdoc.loadXML(sXml)
	}
	return xdoc
}

function xslTransformText(xmlDoc,xslDoc,prams){ 
	if(typeof(xmlDoc)=="string"){
	    xmlDoc = new XmlDoc(xmlDoc)
	}
	if(document.implementation && document.implementation.createDocument){
	    var xslP = new XSLTProcessor()
	    xslP.importStylesheet(xslDoc)
	    if(prams!=null){
	        for(var i=0;i<prams.length;i++){
		        xslP.setParameter(null,prams[i][0],prams[i][1])
	        }
	    }
	    xdoc = xslP.transformToDocument(xmlDoc)	   
	    if(xdoc.documentElement.firstChild.nodeValue != null)
	    { 
	         sXml = xdoc.documentElement.firstChild.nodeValue
	    }
	    else
	    {
	        sXml = getXmlSource(xdoc)
	    }
	}else{
	    if(prams!=null){
		    for(var i=0;i<prams.length;i++){
			    el = xslDoc.selectSingleNode("//xsl:param[@name='"+prams[i][0]+"']")
			    if(el!=null)el.setAttribute("select", "'"+prams[i][1]+"'")
		    }
	    }
		
		sXml = xmlDoc.transformNode(xslDoc)
	    
	}
	return sXml	
}

function getXmlSource(xdoc){
    var txt
    if(xdoc==null){
        txt=""
    }else if(document.implementation && document.implementation.createDocument){
        serializer = new XMLSerializer()
        txt = serializer.serializeToString(xdoc)
    }else{
       txt = xdoc.xml
    }
    return txt.replace(/[\n\r]/g,"")
}

function grabNode(docTrg, ndSrc, deep){
      var ndTrg
      if(!document.all){
          ndTrg = docTrg.importNode(ndSrc,deep)
      }else{
          if(ndSrc.nodeType == 1){
                ndTrg = docTrg.createElement(ndSrc.nodeName)
                for(var i = 0; i < ndSrc.attributes.length; i++){
                      ndTrg.setAttribute(ndSrc.attributes[i].name, ndSrc.attributes[i].value)
                }
          }else if(ndSrc.nodeType == 3){
                ndTrg = docTrg.createTextNode(ndSrc.nodeValue)
          }
            
          if(deep && ndSrc.hasChildNodes()){
                for(var i=0; i<ndSrc.childNodes.length; i++){
                      if(ndSrc.childNodes[i].nodeType!=8){ndTrg.appendChild(grabNode(docTrg,ndSrc.childNodes[i], true))}
                }
          }
      }
      return ndTrg
}

function setValue(el){
	if(el.tagName.toLowerCase()=="select"){
	    el.getElementsByTagName("option")[el.selectedIndex].setAttribute("selected","selected")
	}else{
	    el.setAttribute("value",el.value)
	}
}
