//**********************************************************
// copyright Greg Hicks 2008
//
// Javascript to help with  content management
// Loads an HTML file as the innerHTML of the specified element
// after stripping off everything except the contents of the body
/*if (typeof XMLHttpRequest == "undefined")
  XMLHttpRequest = function() {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e) {};
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e) {};
    try { return new ActiveXObject("Msxml2.XMLHTTP"); }     catch(e) {};
    try { return new ActiveXObject("Microsoft.XMLHTTP"); }  catch(e) {};
 
    throw new Error("This browser does not support XMLHttpRequest or XMLHTTP.");
  };
     var xmlDoc = null ;  //handle for the XMLHttpRequest object, however we get it..
      var targetElement=null; //
*/  
      function loadFile(fileURL,targetElementID) 
      {
		if(fileURL.length==0)return;
        if (typeof window.ActiveXObject != 'undefined' ) 
        {
          xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
//          xmlDoc.onreadystatechange = processStateChange ;
        }
        else 
        {
          xmlDoc = new XMLHttpRequest();
//          xmlDoc.onload = processStateChange ;
        }
        xmlDoc.open( "GET", fileURL, false );
        xmlDoc.send( null );
        
         response= xmlDoc.responseText ;
  // trim out the contents of the <BODY> element, and use that as the innerHTML of the target
         var offset=response.indexOf("<BODY");
         if(offset<0)offset=response.indexOf("<body");
         var endOffset=response.indexOf("</BODY");
         if(endOffset<0)endOffset=response.indexOf("</body");
         response=response.slice(offset+5,endOffset); //add 5 to offset to  get rid of opening '<BODY'
         offset=response.indexOf(">")+1;		//find next element
         response=response.slice(offset);
         window.document.getElementById(targetElementID).innerHTML = response;
         
      }
 //This function processes state change events 
      function processStateChange() 
      {
/*		var response;
        if ( xmlDoc.readyState != 4 ) return ;
         response= xmlDoc.responseText ;
  // trim out the contents of the <BODY> element, and use that as the innerHTML of the target
         var offset=response.indexOf("<BODY");
         if(offset<0)offset=response.indexOf("<body");
         var endOffset=response.indexOf("</BODY");
         if(endOffset<0)endOffset=response.indexOf("</body");
        response=response.slice(offset+1,endOffset); //add one to offset to  get rid of opening '<'
         offset=response.indexOf("<");
         response=response.slice(offset);
         document.getElementById(targetElement).innerHTML=response;
*/      };
 //***************************************************************
// code for flashing one object over another object
// this sucks... needs to be an object
var opacity=0;
var flashIntervalID=0;
var flashActivated=false;
var flashInit=false;
var theSource;
var theTarget;
var flashMinOpacity;
var flashMaxOpacity;
var flashDuration;
var flashInterval;

function flash(source,target,minOpacity,maxOpacity,duration,interval)
{
	if(flashInit==true)stopFlash();
	flashInit=true;
	theSource=document.getElementById(source);
	theTarget=document.getElementById(target);
	flashMinOpacity=minOpacity;
	flashMaxOpacity=maxOpacity;
	flashDuration=duration;
	flashInterval=interval;
	doFlash();
};
function stopFlash()
{
	flashActivated=false;
	clearInterval(flashIntervalID);
	theSource.style.visibility="hidden";	
};

function superImpose(
					source,  //ID of sourse element
					target	 //ID of target element
					)
{
	var thePosition=findPos(target);
	source.style.left=thePosition[0];
	source.style.top=thePosition[1];
	source.style.height=target.style.height;
	source.style.width=target.style.width;
	source.style.visibility="visible";
	source.style.zIndex=target.style.zIndex+1;
};
function hideElements(arrayOfElements)
	{
	var i;
	for(i=0;i<arrayOfElements.length;i++)
		{
		document.getElementById(arrayOfElements[i]).style.visibility="hidden";
		};
	};
function doFlash()
{
if(flashActivated==false)
{
	superImpose(theSource,theTarget);
	opacity=flashMinOpacity;
	flashActivated=true;
	flashIntervalID=setInterval("doFlash()",flashInterval);
	
}
flashDuration-=flashInterval;
if(flashDuration<=0)
	{
	stopFlash();
	return;
	}
if(flashMinOpacity<flashMaxOpacity)
{
opacity+=10;
opacity-=flashMinOpacity;
opacity%=(flashMaxOpacity-flashMinOpacity);
opacity+=flashMinOpacity;
}
else opacity=flashMaxOpacity;

theSource.filters.alpha.opacity =opacity;
};

// Finds absolute position of an object
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}


//********************************************
// changes the position of an object

function setPosition(elementName,top,left)
	{
	if(top==undefined)top=0;
	if(left==undefined)left=0;
	document.getElementById(elementName).style.top=top;
	document.getElementById(elementName).style.left=left;
	};
//changes the size of an object
function setSize(elementName,height,width)
	{
	if(height==undefined)height=0;
	if(width==undefined)width=0;
	document.getElementById(elementName).style.height=height;
	document.getElementById(elementName).style.width=width;
	};
//sets the onclick behaviour
//Note that the element name must be the id of an element, and the function must be a reference to an actual function
function setClick(elementName,clickFunction)
	{
	
	document.getElementById(elementName).onclick=clickFunction;
	}
//sets the source of an image
function setSource(elementName,imageSrc)
	{
	document.getElementById(elementName).src=imageSrc;
	}
//changes z-index of an object to put it on top
function putOnTop(elementName)
	{
	var i;
	var max=0;
	var zIndex;
	for(i=0;i<document.body.childNodes.length;i++)
		{
		zIndex=document.body.childNodes[i].style.zIndex;
		if(zIndex>max)
			{
			max=zIndex;
			}
		}
	document.getElementById(elementName).style.zIndex=max+1;
	}
//changes z-index of an object to put it on bottom
function putOnBottom(elementName)
	{
	var i;
	var min=0;
	var zIndex;
	for(i=0;i<document.body.childNodes.length;i++)
		{
		zIndex=document.body.childNodes[i].style.zIndex;
		if(zIndex<min)
			{
			min=zIndex;
			}
		}
	document.getElementById(elementName).style.zIndex=min-1;
	}
//make a new object if it doesn't already exist.
function makeObject(objectId,objectType)
	{
	var theObject
	if((theObject=document.getElementById(objectId))==null)
		{
		if(objectType==undefined)objectType="div";
		theObject=document.createElement(objectType);
		document.body.appendChild(theObject);
		theObject.id=objectId;
		theObject.style.position="absolute";
		}
	show(objectId);
	return theObject;
	}
	
//hides the specified element
function hide(objectId)
	{
	var theObject;
	if(theObject=document.getElementById(objectId))theObject.style.visibility="hidden";
	}
//shows the specified element
function show(objectId)
	{
	var theObject;
	if(theObject=document.getElementById(objectId))theObject.style.visibility="visible";
	}

//This function creates a frame in the specified element, and puts an image in it.
function pictureFrame(elementId,top,left,width,height,imgSrc,target)
	{
	this.id=elementId;
	this.targetId=function(){return this.id+"-target";}
//make object if it doesn't exist, use it if it does
	makeObject(elementId).innerHTML=
	 "<table  class=\"generated\" >"
// top bar
	+"<tr>"
	+"<td ><img src=\"images/grey-frame-tl.gif\"></td>"
	+"<td><img src=\"images/grey-frame-h.gif\" width=\""+width+"\" height=\"10\"></td>"
	+"<td><img src=\"images/grey-frame-tr.gif\"></td>"
	+"</tr>"
// side bars and image
	+"<tr>"
	+"<td><img src=\"images/grey-frame-v.gif\" height=\""+height+"\" width=\"10\"></td>"
	+"<td  id=\""+this.targetId()+"\"></td>"
	+"<td><img src=\"images/grey-frame-v.gif\" height=\""+height+"\" width=\"10\"></td>"
	+"</tr>"
// bottom bar
	+"<tr>"
	+"<td align=\"right\"><img src=\"images/grey-frame-bl.gif\"></td>"
	+"<td><img src=\"images/grey-frame-h.gif\" width=\""+width+"\" height=\"10\"></td>"
	+"<td><img src=\"images/grey-frame-br.gif\"></td>"
	+"</tr>"
	+"</table>";
	setPosition(elementId,top,left);
	setSize(elementId,top,left);
	pictureTarget(this.targetId(),target,imgSrc);
	setClass(this.id,"generated");
	}

function setClass(parentNode,classId)
	{
	setClassByElement(document.getElementById(parentNode),classId);
	}
function setClassByElement(parent,classId)
	{
	var i;
	for(i=0;i<parent.childNodes.length;i++)
		{
		if(parent.childNodes[i].className!=undefined)
			{
			parent.childNodes[i].className="";
			parent.childNodes[i].className=classId;
			}
		setClassByElement(parent.childNodes[i],classId);
		}
	}

var pictureTargetWindow;
//puts an image and a hypertext link into the specified object, overwriting any inner HTML
//already there.
function pictureTarget(theObject,theTarget,theImage,border)
	{
	if(border==undefined)border=0;
	
	document.getElementById(theObject).innerHTML="<a onclick=\"pictureTargetWindow=window.open('"
										+theTarget
										+"','mywindow','status=0,toolbar=0,menubar=0,resizable=1,width=640,height=480');\"  >"
										+"<img src='"+theImage+"' border='"+border+"'></a>";

	}
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var mouseX = 0;
var mouseY = 0;
function getMouseXY(e) {
if(document.body != null) {
  if (IE) { // grab the x-y pos.s if browser is IE
  mouseX = event.clientX + document.body.scrollLeft;
  mouseY = event.clientY + document.body.scrollTop;
  }
  else {  // grab the x-y pos.s if browser is NS
  mouseX = e.pageX;
  mouseY = e.pageY;
  }
}
if (mouseX < 0) { mouseX = 0; }
if (mouseY < 0) { mouseY = 0; }  
return true;
}
//returns true if the mouse is over this element
function hitTest(elementID) {
    var obj = document.getElementById(elementID);
    if (obj == undefined) return false;
    var position = findPos(obj);
    var inRangeX = position[0] < mouseX && mouseX < position[0] + obj.clientWidth;
    var inRangeY = position[1] < mouseY && mouseY < position[1] + obj.clientHeight;
    if (inRangeX && inRangeY) {
        return true;
    }
    return false;
  }
var USDprices = new Array();
var CADprices = new Array();

function loadPrices(priceFile) {
  var xmlDoc = null;
  var isIE = false;
  if (typeof window.ActiveXObject != 'undefined' ) 
  {
    xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
    isIE = true;
  }
  else 
  {
    xmlDoc = new XMLHttpRequest();
  }
  xmlDoc.open("GET", priceFile, false);
  xmlDoc.send( null );

  var priceList = xmlDoc.responseXML.getElementsByTagName("price");
  for (var i = 0; i < priceList.length; i++) {
    var currencyList = priceList.item(i).getElementsByTagName("currency");
    for (var j = 0; j < currencyList.length; j++) {
      var type = currencyList.item(j).getAttribute("type");
      if (type == "CAD") {
        CADprices[priceList.item(i).getAttribute("name")] = isIE ? currencyList.item(j).text 
                                                                 : currencyList.item(j).textContent;
      }
      if (type == "USD") {
        USDprices[priceList.item(i).getAttribute("name")] = isIE ? currencyList.item(j).text
                                                                 : currencyList.item(j).textContent;
      }
    }
  }
}
