// =================
function areaOver(idx, bIsLink){
	// Mouseover only if this ares is unselected
	areaButtonSet(overArea, "Out", true);
	areaButtonSet(idx, "Over", true);
	overArea = idx;
}
// =================
function areaButtonSet(idx, state, bIgnoreClicked){
if (NS4) return ;
var strID = buttonID("A", idx);
var el = xbGetElement(window, strID);

	if (bIgnoreClicked && buttonState(el) == "CLICKED" )  return;
	if ( document.layers) swapBGImage(el, state);
	else{
	 	xbSetElClass(el, "area" + state) ;
	 	if ((state.toUpperCase()=="CLICKED") ) activeArea = idx;
	}
}
// =================
function  buttonState(el){
var strPath = String("");
var astrTemp = String("");
var strClass = String("");

	if (window.opera || document.layers) {	// we use the background path
		strPath = "" + xbGetElBGImage(el) ;

		if (strPath == "" || strPath == "undefined")  return("");
		else {
			astrTemp = strPath.split("_");
			astrTemp = astrTemp[1].split(".");	
			return	(astrTemp[0].toUpperCase());
		}
	}
	else{
    	strClass = xbGetElClass(el).toUpperCase(); 
    	if (strClass.indexOf("OUT") > 0) return("OUT");
    	else if (strClass.indexOf("OVER") > 0) return("OVER");
    	else if (strClass.indexOf("CLICKED") > 0) return("CLICKED");
	}
}
// =================
function buttonSearch(strID){
// Get the link
var elLink = xbGetElement(window, strID);
var strRes = String("");
var iPos = 0;
  		
		if (elLink != null){
			// Opera doesnt  support .search & all the others done include
			// search in .pathname!!
			if (window.opera){
				strRes 	= libUnixPath(elLink.pathname);
   			iPos = strRes.indexOf("?");
 				if (iPos > -1){
 					strRes = strRes.substr(iPos+1);
					strRes = strRes.replace(/%2F/gi, "/");
					return (strRes);
				}
				else return ("");
			}
			else{
				strRes 	= libUnixPath(elLink.search);
				// crop the leading ?
				if (strRes != "" && strRes != null) strRes = strRes.substring(1)
				strRes = strRes.replace(/%2F/gi, "/");
				return (strRes);
			}
		}
		else return (null)
}
// =================
function buttonUrl(strID){
// Get the link
var elLink = xbGetElement(window, strID)
var strPath = String("");
var iPos = 0;		

		if (elLink != null){
			
			strPath 	= libUnixPath(elLink.pathname); 
			if ( IE && ! window.opera ) strPath =  "/" + strPath;
			
			//if ( window.location.hostname.indexOf("127.") >= 0) {
			//	iPos = strPath.indexOf("/pages/")
			//	if (iPos > 0) strPath = strPath.substr(iPos+1);
			//}
 			// NB NS6 uses | instead of : ?????
			//if (strPath.indexOf(":") < 0  || (NS6 && strPath.indexOf("|") < 0)){
   		//	strPath = siteRoot() + strPath;
			//}
			
		}
		else strPath = null
	//alert( "buttonUrl: " + strPath)
	return (strPath);
}
// =================
function siteRoot(){
var strRoot = top.location.href;
var iPos = 0;

	iPos = strRoot.lastIndexOf("#");	
	if (iPos > 0) strRoot = strRoot.substring(0, iPos)

	// Strip off the filename
	iPos = strRoot.lastIndexOf("/");	
	if (iPos > 0) strRoot = strRoot.substring(0, iPos+1)

	//strRoot should now be http://www.sitename.com/ if this is a 'proper' site
	// if its a test site it may well be http://www.sitename.com/clients/clientname/

	iPos = strRoot.indexOf("/clients/");	
	if (iPos > 0) strRoot = strRoot.substring(0, iPos+1) // leave the trailing slash
	
	iPos = strRoot.indexOf("/examples/");	
	if (iPos > 0) strRoot = strRoot.substring(0, iPos+1) // leave the trailing slash
	
	return strRoot;
}

// =================
function areaOut(idx, bIsLink){areaButtonSet(idx, "Out", true)}
function areaClear(){
var strButtonURL;

	bMore=true;
	idx=0;
  	// Scan the page buttons
	while( bMore ){
		
		idx++; 
		// look for sucessive aArea-XXX elements - returns null if not found
		strButtonURL = buttonUrl("aArea-" + idx)
 
		if ( strButtonURL != null ){
			areaButtonSet(idx, "Out", false)
		}
		else{
			bMore = false	// No more areas
		}
	}

}
// =================
function areaClick(idx){
if (NS4 ) return ; 
var strButtonURL = buttonUrl("aArea-" + idx)

	if (strButtonURL != null && strButtonURL != ""){
		areaClear();
		areaButtonSet(idx, "Clicked", true)
		if (IE) window.event.cancelBubble = true;
		window.location.href = strButtonURL;
	}
 
}
// =================
function buttonID(strWhich, idx){
	if (strWhich.toUpperCase() == "A")  return("divArea-" + idx)
	else if (strWhich.toUpperCase() == "L")  return("divPage-" + idx)
}
// =================
function pageButtonSet(idx, state, bIgnoreClicked){
if (NS4) return ;
var strID = buttonID("L", idx)
var el = xbGetElement(window, strID)
 
	if (bIgnoreClicked && buttonState(el) == "CLICKED" )  return;
	if (window.opera || document.layers ) swapBGImage(el, state);
	else xbSetElClass(el, "page" + state) 

}
// =================
function pageOver(idx, bIsLink){	
	pageButtonSet(overPage, "Out", true)
	pageButtonSet(idx, "Over", true)
	overPage = idx;
}
// =================
function pagePrint(){
	//if ( IE ){
	 	window.event.cancelBubble = true;
		window.focus();
		window.print( );
	//}
	//else{
   //	alert( "Print function is not supported in this browser\n\rPlease use the 'File - Print' menu option");
	//}
}
// =================
function pageOut(idx, bIsLink){pageButtonSet(idx, "Out", true)}
// =================

// =================
//	 Return the filename part of a path, ie  /pages/about/intro.htm regturns intro.htm
function getFileName(strPage){
var iPos = 0;
var strName = String("")
var re = String("")
 	
 	// Translate all \ to / (this happens when we are testing locally)
 	strPage = strPage.replace(/\\/gi, "/");

	iPos = strPage.lastIndexOf("/");
	if (iPos > 0) strName = strPage.substring(iPos+1)
	return (strName)
}

function pageClear(){
var strButtonURL;

	bMore=true;
	idx=0;
  	// Scan the page buttons
	while( bMore ){
		idx++;
	 
		// look for sucessive aLeft-XXX elements - returns null if not found
		strButtonURL = buttonUrl("aPage-" + idx)
		if ( strButtonURL != null ){
			pageButtonSet(idx, "Out", false)
		}
		else{
			// we didnt find any more in the current sequence - do we need
			// to start a new one from 500 for sitemaps etc???
			// NB - these may not be allocated conexutively, ie we could have
			// 502 504 & 505 etc.
			if ( idx < 500 ) idx = 499;
			if ( idx > 510 ) bMore = false	// No more areas
		}
		 
	}

}
 
// =================
function pageClick(idx){
if (NS4   ) return ;
var strButtonURL = buttonUrl("aPage-" + idx)
var strButtonSrch = buttonSearch("aPage-" + idx);
 
	if (strButtonURL != null && strButtonURL != "") {
		pageClear();
		pageButtonSet(idx, "Clicked", true)
		if (IE) window.event.cancelBubble = true;
		if (strButtonSrch.length > 0) strButtonURL = strButtonURL + "?" + strButtonSrch;
	 
		window.location.href =  strButtonURL;
		activePage = idx;
	}
} 
function linkClicked(){
	if (IE) window.event.cancelBubble = true;
}
// =================
function pageLoaded(){		
	// Show print button if it exists unless this is a mac
 	if ( document.all && navigator.appVersion.indexOf('Mac') == -1) {
 		el = document.all["Left-600"];
 		if(el != null ) el.style.visibility = 'visible';
 	}
 	if(iLastAd>0) rotateAds(5);
 	setFormFocus()
}	 
// ================= 
function setFormFocus(){
// Find the first form
var frm = document.forms[0];		
var done = false;

	if (document.forms.length == 0) return;

 	// Scan the form elements
 	for (f = 0; f < frm.elements.length; f++){
		// If we havent set the focus yet
 		if (! done){
 				// Check the state of the form control
			 	if ( frm.elements[f].type != "hidden" && frm.elements[f].type != "submit" && ! frm.elements[f].disabled && ! frm.elements[f].readOnly){
					//alert( frm.elements[f].name);
					frm.elements[f].focus();
	   			// but only on the first
		 			done = true;
		  		 }
		 		
	 		//}
 		}
 	} 	
}
// =================
function swapBGImage(el, strSuffix){
var strPath =  xbGetElBGImage(el);

	if (! strPath  || strPath == "undefined")  return("")
 	
var astrName = strPath.split("_")

	// append the new suffix after the first part of the path (up to the _)
	strPath = astrName[0] + "_" + strSuffix.toLowerCase();
	// append the filetype 
	astrName = astrName[1].split(".");
	strPath = strPath + "." +  astrName[1] 
	xbSetElBGImage(el, strPath);
}

function writePageTitle(){
		// Only run this for IE
 		if ( ! window.opera && document.all) {
			// Find the image
			var elTitle = window.document.all["divPageTitle"]			
	 		if ( elTitle != null ){
		 		var elTitleShadow = window.document.all["divPageTitleShadow"]			
	 			elTitle.innerHTML = window.document.title // elTitle.innerHTML = document.title;
	 			if ( elTitleShadow != null )elTitleShadow.innerHTML  = elTitle.innerHTML ;
	 		}
	 		//top.xbSetInnerHTML( "header", "divPageTitle", "Testing 123");
		}
}
function preSubmit(){
	actionKey=formAction.toUpperCase();
	actionKey=actionKey.substring(0,4);
	res = true;
 	switch (actionKey){
 		case 'DELE':
 			res = window.confirm('Are you sure you want to delete this record?');
 			break;
 	}
 
 	return(res);
}
function checkMaxLength (textarea, evt, maxLength) {
  if (textarea.selected && evt.shiftKey) 
    // ignore shift click for select
    return true;
  var allowKey = false;
  if (textarea.selected && textarea.selectedLength > 0)
    allowKey = true;
  else {
    var keyCode = 
      document.layers ? evt.which : evt.keyCode;
    if (keyCode < 32 && keyCode != 13)
      allowKey = true;
    else           
      allowKey = textarea.value.length < maxLength;
  }
  if ( textarea.value.length > maxLength) textarea.value = textarea.value.substring(0,maxLength);
  textarea.selected = false;
  return allowKey;
}
function rotateAds(secs){
var el = window.document.images["imgAdvert"]

	if ( el != null  ){
		el.src = "../../images/adverts/advert_" + iAdIndex + ".gif"
		iAdIndex += 1;
		if ( iAdIndex > iLastAd ) iAdIndex = 1;
		// Callback in 10 seconds
		setTimeout('rotateAds(' + secs + ')', secs * 1000) 
	}
}
function openImgWindow(theURL,winName,w,h){
	// adjust w & h
	w += 50;
	h += 100;
	openWindow(theURL,winName,w,h);
}
function openWindow(theURL,winName,w,h)
{

	if (parseInt(navigator.appVersion)>3) {
 		if (navigator.appName=="Netscape") {
  			winW = window.innerWidth;
  			winH = window.innerHeight;
 		}
 		if (navigator.appName.indexOf("Microsoft")!=-1) {
  			winW = screen.availWidth - 20; //document.body.offsetWidth;
  			winH = screen.availHeight - 50 ; // document.body.offsetHeight;
 		}
	}

	if ( w > winW || h > winH ) strScroll = "scrollbars=yes";
	else strScroll = "scrollbars=no";
	
	if ( w > winW) w = winW;
	if ( h > winH) h = winH;
	
	 
	x=(winW - w ) / 2;
	y=(winH - h ) / 2 ;
	
	strOptions = strScroll + ",width=" + w + ",height=" + h;
	strOptions += ",left=" + x + ",top=" + y;
	strOptions += ",screenX=" + x + ",screenY=" + y;
	
	if (!window.imageWindow){ // window has not yet been defined
		imageWindow = window.open(theURL,winName,strOptions);
	} else { // has been defined
		if (!imageWindow.closed){ // window still open
			imageWindow.location=(theURL);
			imageWindow.focus();
		} else {
			imageWindow = window.open(theURL,winName,strOptions);
		}
	}
}
// =======================================================================
// Entry point code - run automatically after the file has loaded
// =======================================================================
var IE = (document.all);
var NS4 = false;
var NS6 = false;
var overArea = 0;
var activeArea = 0;
var activePage = 0;
var overPage = 0;
var formAction = String("");
var iAdIndex=1;
var iLastAd=2;

if(navigator.appName.toUpperCase().substr(0,3) == "NET"){
	if (navigator.appVersion.indexOf("4.") > -1)NS4 = true;
	if (navigator.appVersion.indexOf("5.") > -1) NS6 = true
}
