	//XML Group of functions - all functions related to XML
	
	function xml_selectSingleNode(contextNode, elementPath) {
	  if(window.ActiveXObject) {
	    return contextNode.selectSingleNode(elementPath);
	  } else {
	    var xpe = new XPathEvaluator();
	    var nsResolver = xpe.createNSResolver( contextNode.ownerDocument == null ? contextNode : contextNode.ownerDocument.documentElement);
	    var results = xpe.evaluate(elementPath,contextNode,nsResolver,XPathResult.FIRST_ORDERED_NODE_TYPE, null);
	    return results.singleNodeValue; 
	  }
	}
	
	function xml_selectNodes(contextNode, elementPath) {
	  if(window.ActiveXObject) {
	    return contextNode.selectNodes(elementPath);
	  } else {
	    var xpe = new XPathEvaluator();
	    var nsResolver = xpe.createNSResolver( contextNode.ownerDocument == null ? contextNode.documentElement : contextNode.ownerDocument.documentElement);
	    var results = xpe.evaluate(elementPath,contextNode,nsResolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	    var a=new Array();
	    for (var i=0; i<results.snapshotLength; i++ ) {
	      a[i]=results.snapshotItem(i);
	    }
	    return a; 
	  }
	}
	
	function xml_setText(contextNode, elementPath, v) {
	  var n=xml_selectSingleNode(contextNode, elementPath);
	  var t;
	  
	  if (!n) { return ""; }
	
	  if (n.nodeType==2) {
	    n.nodeValue=v;
	  } else {
	    t=xml_selectSingleNode(n, "text()");
	  
	    if (t==null) {
	      if (contextNode.ownerDocument == null) return;
	      n.appendChild(contextNode.ownerDocument.createTextNode(v));
	    } else {
	      t.nodeValue=v;
	    }
	  }
	}
	
	function xml_getText(contextNode, elementPath) {
	  var n=xml_selectSingleNode(contextNode, elementPath);
	  var t;
	  
	  if (!n) { return ""; }
	  if (n.nodeType==2) {
	    t=n;
	  } else {
	    t=xml_selectSingleNode(n, "text()");
	  }
	  
	  if (t==null) {
	    return "";
	  } else {
	    return t.nodeValue;
	  }
	}
	
	function xml_getXml(contextNode)
	{
	  if (contextNode.xml) {
	    return contextNode.xml;
	  } else {
	    var xmlser=new XMLSerializer();
	    return xmlser.serializeToString(contextNode);
	  }
	}
	
	function xml_getXmlDom() {
		var xmldom;
		try {
			xmldom = new ActiveXObject("Microsoft.XMLDOM");
	  	xmldom.setProperty("SelectionLanguage","XPath");
		} catch (e) {
			try {
				xmldom = document.implementation.createDocument("","",null);
			} catch (e) {
				xmldom=null;
			}
		}
		return xmldom;
	}
	
	function xml_getXmlHttp() {
		var xmlhttp;
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				try {
					xmlhttp = new XMLHttpRequest();
				} catch (e) {
					xmlhttp=null;
				}
			}
		}
		return xmlhttp;
	}
	
	function xml_blockingDataGet(url) {
		var xmlhttp = xml_getXmlHttp();
		if (url.indexOf("?")>-1) {
			url+='&unique='+Number(new Date());
		} else {
			url+='?unique='+Number(new Date());
		}
		xmlhttp.open("GET", url, false);
		xmlhttp.send("");
		
	  var xmlobj=null;
	  
	  try {
	    xmlobj=xml_getXmlDom();
	    xmlobj.loadXML(xmlhttp.responseText);
	  } catch (e) {
	    try {
	      var oParser = new DOMParser();
	      xmlobj = oParser.parseFromString(xmlhttp.responseText, "text/xml");
	    } catch (e) {
	    }
	  }
	  
	  return xmlobj;
	}
	
	//HTML Group functions - all functions that manipulate HTML code or the browser DOM
	
	function html_blockingDataGet(url) {
		var xmlhttp = xml_getXmlHttp();
		if (url.indexOf("?")>-1) {
			url+='&unique='+Number(new Date());
		} else {
			url+='?unique='+Number(new Date());
		}
		xmlhttp.open("GET", url, false);
		xmlhttp.send("");
		
	  return xmlhttp.responseText;
	}
	
	function html_encode(s)
	{
	  s=s.replace('<','&lt;').replace('>','&gt;').replace('&','&amp;');
	
	  return s;
	}
	
	function html_getClientWidth()
	{
	  return document.documentElement.clientWidth;
	}
	function html_getClientHeight()
	{
	  return document.documentElement.clientHeight;
	}
	
	//Palettes  -  all functions relating to virtual (DIV based) popups
	
	var palettes=new Array();
	
	function palette(domobj, nstylingdivs, nmode)
	{
	  //INTERNAL STATE
	  this.servercomponent='';      //the program on the server that supplies data for the panel (parameters in querystring)
	  this.updatefrequency=0;       //just fetch new data regardless each X seconds (0 is never)
	  this.checkfrequency=0;        //run a check command to see if data has changed - if changed, then fetch (0 is never)
	  if (domobj==null) {
	    this.parentobject=document.getElementsByTagName('BODY')[0];
	  } else {
	    this.parentobject=domobj;   //the object under which a new child shall be appended
	  }
	  this.innerdiv=null;           //the top level div that is created when the palette is shown
	  this.className='CMSPalette';  //the class name attached to the top level div 
	  this.id='';                   //the id attached to the top level div 
	  this.caption="Palette";
	  this.tickstillcheck=0;
	  this.tickstillupdate=0;
	  this.stylingdivs=(nstylingdivs ? nstylingdivs : 0);
	  this.xmlobj=null;
	  this.responsetext='<p>Loading...</p>';
	  this.contentdiv=null;
	  this.captiondiv=null;
	  this.mode=nmode;
	
	  //EVENTS
	  this.onredraw=_palettes_defaultredraw;
	  this.ontick=null;             //something to do every one second tick e.g. custom positioning 
	
	  //FUNCTIONS  
	  this.open=_palettes_open;
	  this.close=_palettes_close;
	
	  if (this.mode=='html') {
	    this.refreshcontent=_palettes_refreshhtmlcontent;
	  } else if (this.mode=='iframe') {
	    this.refreshcontent=function () { this.responsetext='<iframe name="CMSContentIframe" src="'+this.url+'"></iframe>'; this.onredraw(); }
	  } else {
	    this.refreshcontent=_palettes_refreshcontent;
	  }
	  this.setcaption=function (cap) { this.caption=html_encode(cap); if (this.captiondiv) { this.captiondiv.innerHTML=this.caption; } }
	  
	  return this;
	}
	
	//helper function to create a palette and add it to the list 
	function palettes_create(domobj, stylingdivs, nmode)
	{
	  return (palettes[palettes.length]=new palette(domobj, stylingdivs, nmode));
	}
	
	function palettes_close(e)
	{
	  if (window.event) {
	    window.event.srcElement.parentNode.palette.close();
	  } else if (e) {
	    e.target.parentNode.palette.close();
	  }
	}
	
	//open will create the divs for the palette if they don't already exist and will fetch the xml document
	//from the server. The xml document will be passed to the redraw function.
	function _palettes_open()
	{
	  if (!this.innerdiv) {
	    this.innerdiv=document.createElement("DIV");
			this.parentobject.appendChild(this.innerdiv);
			if (this.className!='') { this.innerdiv.className = this.className; }
			if (this.id!='') { this.innerdiv.id = this.id; }
	    this.innerdiv.palette=this;
	
	    var stylingdiv;
	    
	    for (var sd=0; sd<this.stylingdivs; sd++) {
	      stylingdiv=document.createElement("DIV");
			  this.innerdiv.appendChild(stylingdiv);
	      stylingdiv.className="CMSStyling"+(sd+1);
	    }
	
	    this.contentdiv=document.createElement("DIV");
		  this.innerdiv.appendChild(this.contentdiv);
	    this.contentdiv.className="CMSContent";
	
	    stylingdiv=document.createElement("DIV");
	    stylingdiv.innerHTML='Close';
		  this.innerdiv.appendChild(stylingdiv);
	    stylingdiv.className="CMSClose";
	    stylingdiv.onclick=palettes_close;
	
	    this.captiondiv=document.createElement("DIV");
		  this.innerdiv.appendChild(this.captiondiv);
	    this.captiondiv.className="CMSCaption";
	    var text=document.createTextNode(this.caption);
	    this.captiondiv.appendChild(text);
	  }
	  
	  this.refreshcontent();
	}
	
	//remove the divs from the dom for this palette
	function _palettes_close()
	{
	  this.innerdiv.parentNode.removeChild(this.innerdiv);
	  this.innerdiv=null;
	  this.contentdiv=null;
	  this.captiondiv=null;
	}
	
	//fills the window with a serialization of the xml code downloaded
	function _palettes_defaultredraw()
	{
	  if (this.mode == 'html' || this.mode == 'iframe') {
	    this.contentdiv.innerHTML=this.responsetext;
	  } else {
	    if (this.xmlobj) {
	      this.contentdiv.innerHTML="The XML is<br><br>"+xml_getXml(this.xmlobj).replace(/</g, '&lt;');
	    } else {
	      this.contentdiv.innerHTML="The XML Object is empty.";
	    }
	  }
	}
	
	function _palettes_refreshcontent()
	{
	  if (!this.servercomponent) { return; }
	  
	  this.xmlobj=xml_blockingDataGet(this.servercomponent);
	
	  if (this.onredraw) { this.onredraw(); }
	}
	
	function _palettes_refreshhtmlcontent()
	{
	  if (!this.url) { return; }
	  
	  this.responsetext=html_blockingDataGet(this.url);
	
	  if (this.onredraw) { this.onredraw(); }
	}
	
	function palettes_initialise()
	{
	}
	
	//Initialisation
	var _CMSOnloadList=new Array();
	
	function CMSAddOnloadFunc(func) {
	  _CMSOnloadList[_CMSOnloadList.length]=func;
	}
	function CMSOnloadHandler() {
	  for (var i=0;i<_CMSOnloadList.length;i++) {
	    var func=_CMSOnloadList[i];
	    func();
	  }
	}
	
	CMSAddOnloadFunc(palettes_initialise);
	
	window.onload = CMSOnloadHandler;




	function pagePalette(url,caption,className)
	{
		var cppalette=null;
		cppalette=palettes_create(document.getElementById('stdbody'), 0, 'iframe');
		cppalette.className=className;
		cppalette.url=url;
		cppalette.caption=caption;
		cppalette.open();
		cppalette.innerdiv.style.width=(html_getClientWidth()-50)+'px';
		cppalette.innerdiv.style.height=(html_getClientHeight()-70)+'px';
	}

	function autoPalettes() {
		if (!document.getElementsByTagName) { return; }
		
		if (navigator.appVersion.indexOf("MSIE")!=-1)
		{
			var temp=navigator.appVersion.split("MSIE");
			var version=parseFloat(temp[1]);
			if (version<7.0) { return; }
		}

		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("target")) {
				var target=anchor.getAttribute("target");
				if ((target!='_top') && (target!='_blank') && (target!='cp_external') && (target!='hostedwebsite') && (target!='mailmessageview')) {
					var poptitle='';
					if ((anchor.innerText) && (anchor.innerText!='')) poptitle=anchor.innerText;
					if (poptitle=='') {
						var img=anchor.getElementsByTagName("img");
						if (img.length>0) {
							img=img[0];
							if (img.getAttribute("alt")) {
								poptitle=img.getAttribute("alt");
							}
							if ((poptitle=='') && (img.getAttribute("title"))) {
								poptitle=img.getAttribute("title");
							}
						}
						if (poptitle=='') poptitle='Details';
					}
					var hrefaddon;
					if (anchor.getAttribute("href").indexOf("?")) {
						hrefaddon="&palette=Y"
					} else
					{	hrefaddon="?palette=Y" }
					anchor.onclick=new Function ("pagePalette('" + anchor.getAttribute("href") + hrefaddon + "','" + poptitle + "','bodypalette');");
					anchor.target = "";
					anchor.href="javascript:void(0);";
				}
			}
		}
		
		pageforms = document.getElementsByTagName("form");
		for (var i=0; i<pageforms.length; i++) {
			var pageform = pageforms[i];
			if (pageform.getAttribute("target")) {
				var target=pageform.getAttribute("target");
				if ((target!='_top') && (target!='_blank') && (target!='cp_external') && (target!='hostedwebsite') && (target!='mailmessageview')) {
					var poptitle='Details';
					pageform.onsubmit=new Function ("pagePalette('about:blank','" + poptitle + "','bodypalette');");
					pageform.target = "CMSContentIframe";
				}
			}
		}
	}

