var btnCancel, btnNext,btnBack;
var dfltButton;

/* Define the Button object */
function Button(id, text, action)
{
	this.id = id;
	this.text = text;
	this.action = action;
}

function initButtonObjs()
{
	btnCancel	= new Button("cancel", btnCancelText,  "cancel" );
	btnNext = new Button("next", btnNextText, "next");
	btnBack = new Button("back", btnBackText, "back");
}

/* Preload buttons */
var bNames = new Array("enabled","disabled","over","down");
var iButtons = new Array();
var sendingEvent=false;

for( i=0; i<bNames.length; i++ )
{
        iButtons[bNames[i]] = new Image(94,24);
        iButtons[bNames[i]].src = "CTW/images/bttn_" + bNames[i] + ".gif";
}
var hoverDelay=null;
var tempButtonObj="";
function showevt()
{
	var evt,evtObj;
	evt = window.event;
	evtObj = evt.srcElement;
	if( evtObj.orig == null )return;
	var btnObj = eval("document.all."+evtObj.orig);
	switch( evt.type.toLowerCase() )
	{
		case "mouseover":
			if( sendingEvent ) return;
			if( (hoverDelay!=null) && (tempButtonObj==btnObj.id) )
			{
				clearTimeout(hoverDelay);
				hoverDelay=null;
			}
			btnObj.style.backgroundImage = "url("+iButtons["over"].src+")";
			window.event.cancelBubble;
			break;
		case "mouseout":
			hoverDelay=setTimeout(btnObj.id+".style.backgroundImage = \"url(\"+iButtons[\"enabled\"].src+\")\";",200);
			//btnObj.style.backgroundImage = "url("+iButtons["enabled"].src+")";
			tempButtonObj=btnObj.id;
			window.event.cancelBubble;
			break;
		case "mousedown":
			btnObj.style.backgroundImage = "url("+iButtons["down"].src+")";
			window.event.cancelBubble;
			break;
		case "mouseup":
			btnObj.style.backgroundImage = "url("+iButtons["over"].src+")";
			/* delay form submissions to allow button images to return */
			window.event.cancelBubble;
			break;
	}
}
document.onmouseover=showevt;
document.onmouseout=showevt;
document.onmousedown=showevt;
document.onmouseup=showevt;

function sendEvent( val )
{
	if( sendingEvent ) return;
	sendingEvent = true;
	parent.window.catchEvent( val );
	setTimeout("sendingEvent=false;",500 );
}

function catchEvent( val )
{
	/** Catch the event codes from buttons
  	 ** Then trigger the appropriate action function */
	if( val == "undefined" ) return;
	eval( val + "Action()" );
}

/** Incomplete **/
function focusButton()
{
	if( document.readyState!="complete" && document.readyState!="interactive")
	{
		setTimeout("focusButton();",5000);
		return;
	}
	if(document.links.length==0)return;
}
document.onFocus=focusButton;

function trapKeys()
{
	if( window.event.keyCode == 13 && document.readyState=="complete")
	{
		catchEvent( dfltButton.action );
	}
	return;
}

function renderButtons(buttonArray)
{
	var tabIndex=1;	
	var retStr = "<script src=\"js/buttons.js\"><\/scr" + "ipt>"
		+ "<table align=right border=0 cellpadding=0 cellspacing=3 height=30>";
	var btnRow = "<tr align=right>";
	for( var i=0,j=tabIndex+buttonArray.length-1; i<buttonArray.length; i++,j-- )
	{
		btnRow +="<td height=24 width=94 align=center><a href=\"javascript:sendEvent('"+buttonArray[i].action+"');\" tabindex=\""+j
			 + "\"  orig=\""+buttonArray[i].id+"\""
			 + " class=btnBack>"
			 + "<span style=\"height:24px;width:94px;cursor:default;background-image: url(CTW/images/bttn_enabled.gif);\" "
			 + "id=\""+buttonArray[i].id+"\" orig=\""+buttonArray[i].id+"\" onclick=\"sendEvent('"+buttonArray[i].action+"');\" >"
			 + buttonArray[i].text
			 + "</a></span></td>";
	}
		btnRow += "<td>&nbsp;</td></tr>";
	retStr += btnRow + "</table>";
	return retStr;
}

var buttons_loaded = true;

