// Copyright© 2005 Mike Waite. All rights reserved.
// Version=1.00

var popupVisible=false;

window.onresize=hidePopup;
window.onscroll=hidePopup;
document.onclick=hidePopup;
document.onkeypress=forwardEnterToClick;

// Show the popup window
function showPopup(popupType) {

    var elementClicked=event.srcElement;	
    //evt = (evt) ? evt : ((window.event) ? event : null);
    //if (evt) {
    //   var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    //   if (elem) {
    //       var elementClicked=elem;
    //   }
    //}

	
	
	
	var elementID=elementClicked.id.toLowerCase();
	var popupWidth, popupHeight, popupLeftOff, popupTopOff;
	var popupItems;
	
	hidePopup();	

	if (popupType=="list") {
		popupWidth=200;
		popupHeight=150;
		popupLeftOff=0;
		popupTopOff=15;
		popupItems=PopupListIFrame.document.all.tags("DIV");		
	}
	else {
		popupWidth=200;
		popupHeight=0;
		popupLeftOff=20;
		popupTopOff=20;
		popupItems=PopupBoxIFrame.document.all.tags("DIV");	
	}
	
	// populate content into popup box or list
	if (popupItems.length) {
		for (var i=0;i<popupItems.length;i++) {
			if (popupItems[i].id.toLowerCase()==elementID) {
				if (popupType=="list")  {
					popupItems[i].style.display="inline";
					}
				else {
					PopupBox.innerHTML=popupItems[i].innerHTML;
					}
				}
			else {
				popupItems[i].style.display="none";
				}
			}
		}
			
	//display the Div
	if (popupType=="list")  {
		PopupList.style.display="inline";
		}
	else {
		PopupBox.style.display="inline";
		}		
	
	//get the position of the element clicked
	var iPos=getElementPosition(elementClicked.id);
	
	//add offsets
	popupLeft=iPos.left+popupLeftOff;
	popupTop=iPos.top+popupTopOff;
	
	//if box type then set the width to get the height
	if (popupType!="list"){		
		PopupBox.style.pixelWidth=popupWidth;
		PopupBox.style.padding="5px";
		popupHeight=PopupBox.offsetHeight;
		}
	var pageLeft=document.body.scrollLeft;
	var pageTop=document.body.scrollTop;
	var pageWidth=document.body.clientWidth-1; //document.body.offsetWidth-21;
	var pageHeight=document.body.clientHeight-1;  //document.body.offsetHeight-21;
	var pageRight=document.body.scrollLeft+pageWidth;
	var pageBottom=document.body.scrollTop+pageHeight; 
	var popupRight=popupLeft+popupWidth;
	var popupBottom=popupTop+popupHeight;
	
	//adjust popup position for overflow
	if (popupRight>pageRight) popupLeft=pageRight-popupWidth;
	if (popupLeft<pageLeft) popupLeft=pageLeft;
	if (popupWidth>pageWidth) popupWidth=pageWidth;
	
	if (popupBottom>pageBottom) popupTop=pageBottom-popupHeight;
	if (popupTop<pageTop) popupTop=pageTop;
	if (popupHeight>pageHeight) popupHeight=pageHeight;
	
	if (popupType=="list")   {	
		PopupList.style.pixelLeft=popupLeft;
		PopupList.style.pixelTop=popupTop;
		PopupList.style.pixelWidth=popupWidth;
		PopupList.style.pixelHeight=popupHeight;		
		PopupList.style.backgroundColor="white";
		PopupList.style.padding="0px";
		//size the iframe too if its a list
		document.getElementById("PopupListIFrame").style.height=(popupHeight + "px");
		PopupListIFrame.scrolling="auto";
		PopupListIFrame.document.body.bgColor="white";
		}
	else{		
		PopupBox.style.pixelLeft=popupLeft;
		PopupBox.style.pixelTop=popupTop;
		PopupBox.style.pixelWidth=popupWidth;		
		PopupBox.style.backgroundColor="lightyellow";
		}
	
	popupVisible=true;
	window.event.cancelBubble=true;
	}

function ShowAll()
{
	var iDiv, iDivSource, oDiv, sCaption, sState, sImage;
	window.event.returnValue=0
	// toggle text and image
	sCaption=document.all("ShowAll").innerHTML;
	if (sCaption=="Show All")
		{
		sImage="../images/expand_tri_large.gif";
		sState="expanded";
		document.all("picHeader").src="../images/collapse_tri_small.gif";
		document.all("ShowAll").innerHTML="Hide All";
		}
	else
		{
		sImage="../images/collapse_tri_large.gif";		
		sState="collapsed";
		document.all("picHeader").src="../images/expand_tri_small.gif";
		document.all("ShowAll").innerHTML="Show All";
		}

	//Expand or collapse DIVs	
	for (iDiv=0; iDiv < document.all.tags("DIV").length; iDiv++)
		{	
			oDiv=document.all.tags("DIV").item(iDiv);
			iDivSource=oDiv.sourceIndex;
			if (oDiv.id=="ExpCol")
			{
				oDiv.className=sState;
				if (document.all(iDivSource-2).tagName=="IMG") {
				document.all(iDivSource-2).src=sImage;}
			}	
		}
	document.all("ShowAll").className="DropDown";
}

//Expand or collapse if a expandable DIV
function ExpandCollapse()
{
	//Get the index of the element which was clicked
	window.event.returnValue=0
	
	var clickedPos = event.srcElement.sourceIndex;
	// close any open popups
	hidePopup();

	//Determine where the Div to be expanded is.  
	for (var ioff=1; (clickedPos+ioff) < document.all.length; ioff++){	
		if(document.all(clickedPos+ioff).id=="ExpCol"){break;}
		}
		
	var divExpCol =document.all(clickedPos+ioff);
	//expand or collapse the Div
	if(divExpCol.className=="collapsed"){
		divExpCol.className="expanded";
		if (document.all(clickedPos).tagName=="IMG") {
			document.all(clickedPos).src="../images/expand_tri_large.gif";}
		else if (document.all(clickedPos-1).tagName=="IMG") {
			document.all(clickedPos-1).src="../images/expand_tri_large.gif";}
		//check to see if all exapndable DIVs are expanded - if so change state of Show all to Hide all
		if (document.all("ShowAll")) {
			var ihideall=1;
			var allDivs=document.all.tags("DIV");
			for (var ioff=1; ioff < allDivs.length; ioff++){
				if(allDivs[ioff].className=="collapsed"){
					ihideall=0;
					break;
					}
				}
			if (ihideall) ShowAll(); //change the state to hide all cause there arent anymore to show
			}
		}
	else {
		divExpCol.className="collapsed"
		if (document.all(clickedPos).tagName=="IMG") {
			document.all(clickedPos).src="../images/collapse_tri_large.gif";}
		else if (document.all(clickedPos-1).tagName=="IMG") {
			document.all(clickedPos-1).src="../images/collapse_tri_large.gif";}
	}
	window.event.cancelBubble = true;
}


function getElementPosition(elemID) {
    var offsetParent = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
	//check if elemID is an area - if so, must search for the image containing the map 
	//alert (offsetParent.tagName);
	if (offsetParent.tagName=="AREA") {
		offsetLeft += offsetParent.offsetLeft;
        offsetTop += offsetParent.offsetTop;
		oMap=offsetParent.offsetParent;
		mapname= oMap.name;
		var imageitems=offsetParent.document.all.tags("IMG");
		if (imageitems.length) {
			for (var i=0;i<imageitems.length;i++) {
				if (imageitems[i].useMap== ("#" + mapname)) {
					offsetParent=imageitems[i];
					}
				}
			}	
		}
	//now walk through the tree and accumulate offsets
    while (offsetParent) {
        offsetLeft += offsetParent.offsetLeft;
        offsetTop += offsetParent.offsetTop;
        offsetParent = offsetParent.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
	
    return {left:offsetLeft, top:offsetTop};
}

//Forward Enter key press to mouse click
function forwardEnterToClick() {
	window.event.cancelBubble=true;
	if (window.event.keyCode==13) event.srcElement.click();
	}
	
// Hide popup window
function hidePopup() {
	if (popupVisible) {
		if (document.getElementById("PopupBox")!=null) {PopupBox.style.display="none";}
		if (PopupList!=null) {PopupList.style.display="none";}
		popupVisible=false;
		}
	}

function nolink(){} //dummy script called from a object in html to prevent link action

function mailtous(){
emailE=('su' + 'pport' + '@' + 'visual' + 'bots.com');
document.write('<a href="mailto:' + emailE + '">' + emailE + '</a>');
}
