/*
 * Copyright 2004-2006, Peter Rowntree. All Rights Reserved.
 * http://www.hdyn.com/wr/common/contact.php?addr=pr
 * does NOT require popups.js
 * needs: xml.js
 * if timers.js->SSTimer is availabe, uses it
 */
 
var g_infoOb=null;
var g_replace=true;
var g_xfrTimer=typeof SSTimer == 'undefined' ? null : new SSTimer("g_xfrTimer",lightXfr,dimXfr,1000);

function xfrCVis(vis)
{
   if(g_infoOb == null)
      return false;
   g_infoOb.style.display=vis ? "block" : "none";
   return true;
}

//arguments[1]=alternate destOb
function xfrComm(s)
{
   if(!xfrCVis(true))
      return;
   var destOb=arguments.length >= 2 && arguments[1] ? arguments[1] : g_infoOb;
   if(typeof s == 'string')
   {
      if(s == "")
         return;
      var newEl=document.createElement('div');
      newEl.innerHTML=s;
      s=newEl;
   }
   if(g_replace && destOb.firstChild != null)
   	destOb.replaceChild(s,destOb.firstChild);
   else
      destOb.appendChild(s); //IE won't append XML element, only HTML
   if(typeof destOb.scrollTop != 'undefined' && okScroll(null))
      destOb.scrollTop=32000;
   if(g_xfrTimer != null)
   	g_xfrTimer.run();
}

var g_okScroll=true;
function okScroll(ok)
{
   if(ok != null)
      g_okScroll=ok;
   return g_okScroll;
}

function infoIs(s)
{
   return xfrCVis(true) 
      && g_infoOb.firstChild != null 
      && typeof g_infoOb.firstChild.innerHTML != "undefined" 
      && g_infoOb.firstChild.innerHTML == s;
}

function lightXfr()
{
	if(g_infoOb != null)
		g_infoOb.style.backgroundColor="#FFEEDD";
}

function dimXfr()
{
	if(g_infoOb != null)
		g_infoOb.style.backgroundColor="#DDDDDD";
}

function quietTimeout(req)
{
   req.abort();
}

function sendXMLTimeout(req)
{
   req.abort();
   xfrComm("sendXML timed out.");
}

function gotError(req)
{  
   try
   {
      testError(req);
      return false;
   }
   catch(e)
   {
      xfrComm(typeof e == "string" || typeof e.message == "undefined" ? e : e.message);
   }
   return true;
}

//can take a 2nd argument: 'htmlOK=true|false' (default=false)
//=> it's OK to pass HTML thru.
function testError(req)
{
   if(req == null || typeof req.responseXML == 'undefined')
      throw "no response XML!";
   var errStr="";
   var lf="<br/>";
   if(req.status != 200)
   {
      errStr+=req.status+" "+req.statusText+lf;
      //we assume 500 gives us valid html text, even though moz now returns
      //null req.responseXML:
      if(req.status == 500)
         ;//doStdNewXML(req.responseText);
      throw errStr;
   }
   var xmlDoc=req.responseXML;
   if(xmlDoc == null)
   {
      if(req.responseText == null || req.responseText == "")
        throw "expected xml responseText, got "+(req.responseText == null ? 'null' : 'nothing')+lf;
      throw req.responseText;
   }
   var htmlOK=(arguments.length >= 2 && arguments[1]);
   var htmlNode=iElement("html",0,xmlDoc);
   if(!htmlOK && htmlNode != null)
   {
      //doStdNewXML(req.responseText);
      throw "got unexpected HTML response";
   }
   var root=xmlDoc.firstChild;
   if(root == null)  //happens when IE6 gets a parser error
   {
      if(req.responseText == null || req.responseText == "")
         throw "no XML data.";
      throw "XML parser error in: "+entitySubstitute(req.responseText);
   }
   if(root.localName == "parsererror") //gecko is much better
      throw entitySubstitute(root.textContent);
   var nset=root.getElementsByTagName("error");
   if(nset == null || nset.length < 1)
      return false;
   var len=nset.length;
   for(var i=0; i < len; i++)
      errStr+=nset.item(i).firstChild.nodeValue; 
   throw errStr;
}

function gotBody(xmlDoc)
{  
   var body=getBodyAsStr(xmlDoc);
   if(body == null || body == "")
      return false;
   xfrComm(body);
   return true;
}

//a simple endProc
function stdResponse(req)
{
   if(!gotError(req))
      gotBody(req.responseXML);
}

/**************** cooling *************
*************/
