//===========================================================
//	Standard.js
// ----------------------------------------------------------
// This file contains generic JavaScript functions used 
// throughout the Bridger library.
// ----------------------------------------------------------
// Copyright (c)2004 Xionetic Technologies
//===========================================================

// ===================================================
// Global Constants and Macros
// ===================================================
var D=document;
var T=true;
var F=false;
var U="undefined";
var N=null;

//var tmp;
var BCAP;   //browser capable flag

//===========================================================
// isdf (is defined)
//-----------------------------------------------------------
// Returns true if the given value is defined or false if not
//-----------------------------------------------------------
// o - object to check for existence.
//===========================================================
function isdf(o)
{
	return (typeof(o)!=U);
}

//===========================================================
//	get
//-----------------------------------------------------------
// Similar to getElementById.  This function also handles
// clientid vs uniqueid name differences.
//-----------------------------------------------------------
// x = id of the element to get
//===========================================================
function get(x)
{
	var c=D.getElementById(x)
	if(c==N)
	{
		var re = new RegExp (':', 'gi') ;
		c = D.getElementById(x.replace(re,'_'));
	}
	return c;
}//get

//===========================================================
// fixE
//-----------------------------------------------------------
// Adds standard properties to an event so that we can have
// cross-browser processing
//-----------------------------------------------------------
// e = event object
//===========================================================
function fixE(e)
{
	if(!isdf(e)) e = window.event;
	if(!isdf(e.srcElement)) e.srcElement = e.target;
	if(!isdf(e.keyCode)) e.keyCode = e.which;
	return e;
}


// ========================
// Generic Functions
// ========================

//===========================================================
// show (ShowControl)
//-----------------------------------------------------------
// displays or hides a control
//-----------------------------------------------------------
// c = control to display
// v = visibility flag
//===========================================================
function show(c, v)
{
	// IE, NS7
	if(v)
		c.style.visibility='visible';
	else
		c.style.visibility='hidden';
}//show


//===========================================================
// enable (EnableControl)
//-----------------------------------------------------------
// enables or disables a control
//-----------------------------------------------------------
// n = name of control to enable
// e = enabled flag
//===========================================================
function enable(n,e)
{
	var c=get(n);
	c.disabled=!e;
}


//===========================================================
// clrLst (ClearControls)
//-----------------------------------------------------------
// clears input out of a list of controls
//-----------------------------------------------------------
// l = list of control id's
//===========================================================
function clrLst(l)
{
	var i;
	for(i=0;i<l.length;i++)
		clear(get(l[i]));
}//clrLst

//===========================================================
// clrChld (recursive)
//-----------------------------------------------------------
// Clears all form fields that are children of control c
//-----------------------------------------------------------
// c - object of the parent control
//===========================================================
function clrChld(c)
{
	var i;
	var o;
	if(isdf(c.childNodes))
	{
		o=c.childNodes;
		for(i=0;i<o.length;i++)
			clrChld(o[i]);
	}
	clr(c);
} // clrChld

//===========================================================
// clr
//-----------------------------------------------------------
// Clears the value of the current object.  If the object is 
// not a form field, it is ignored.
//-----------------------------------------------------------
// c - Object to clear
//===========================================================
function clr(c)
{
	switch(c.type)
	{
		case 'text':
			c.value='';
			break;
		case 'select-one':
			c.selectedIndex = 0;
			break;
		case 'select-multiple':
			c.selectedIndex = -1;
			break;
		case 'checkbox':
			if(c.checked)
				c.click();
			break;
		default:
	}
} // clr


//===========================================================
// df_set
//-----------------------------------------------------------
// Checks to see if a default button has been set for the 
// form.  
//-----------------------------------------------------------
// f = form variable
// Returns true if a default button is set or false if it is 
// not.
//===========================================================
function df_set(f)
{
	if(isdf(f.df_e))
		return f.df_e;
	return F;
}

//===========================================================
// foc (SetFocus)
//-----------------------------------------------------------
// Sets the focus on a control
//-----------------------------------------------------------
// i = the id of the control to focus
//===========================================================
function foc(i)
{
	var c;
	if(typeof(i)=='string')
		c = get(i);
	else
		c = i;
	try {
		if(isdf(c)&&(c!=N)&&isdf(c.focus))
			c.focus();
	}
   catch(err){return;}

}//foc


//===========================================================
// initPage (InitializePage)
//-----------------------------------------------------------
// Call all functions to initialize the page.
//===========================================================
function initPage()
{
   BCAP = isBCap();
	preload();
}//initPage


//===========================================================
// isBCap (_IsBrowserCapable)
//-----------------------------------------------------------
// Determine if the browser has either getElementById or document.all.
// Generate a getElementById function to be used by all other JavaScript
// functions for easy object retrieval.
//===========================================================
function isBCap() 
{
	if(typeof(__doPostBack) == U) return F;
	if(typeof(D.getElementById) == U) 
	{
		if(typeof(D.all) != U)
			D.getElementById = function(id){return D.all[id];};
		else 
			return F;
	}
	return T;
}//isBCap



//===========================================================
// noBack (DisableBack)
//-----------------------------------------------------------
// Disable the back button.
//===========================================================
function noBack()
{
	window.history.forward();
}//noBack


//===========================================================
// preload (_PreloadImages)
//-----------------------------------------------------------
// Pre-load registered images into memory for better display
//===========================================================
function preload()
{
	if( typeof(arImgUrls) == U )
		return;
	D.arImgs=new Array();
	for(var i=0;i<arImgUrls.length;i++)
	{
	   D.arImgs[i]=new Image;
	   D.arImgs[i].src=arImgUrls[i];
	}
}//preload


//===========================================================
// rmvOp (RemoveAllOptions)
//-----------------------------------------------------------
//	Remove all options for a given selection control.
//-----------------------------------------------------------
// c = selection control
//===========================================================
function rmvOp(c)
{
	for (var i=(c.options.length-1); i>=0; i--) 
		c.options[i]=N; 

	c.selectedIndex=-1; 
}//rmvOp


//===========================================================
// selOp (SelectOption)
//-----------------------------------------------------------
// Select an option in a selection control
//-----------------------------------------------------------
// c = selection control
// v = value to select
//===========================================================
function selOp(c, v)
{
	for(var i=0; i<c.options.length; i++)
		if(c.options[i].value == v)
		{	
			c.selectedIndex = i;
			break;
		}
}//selOp



//===========================================================
// getScroll
//-----------------------------------------------------------
//	Gets the scroll position for the given object.
//-----------------------------------------------------------
//	d - Name of the div object.
// v - Name of the hidden field in which to store vertical
//     position.
// h - Name of the hidden field in which to store horinzontal
//		 position.
//===========================================================
function getScroll(d,v,h)
{
	var o;
	var od = get(d);
	if(v != '')
	{
		o = get(v);
		o.value = od.scrollTop;
	}
	if(h != '')
	{
		o = get(h);
		o.value = od.scrollLeft;
	}
}//loadScroll

//===========================================================
// setScroll
//-----------------------------------------------------------
//	Sets the scroll position for the given object.
//-----------------------------------------------------------
//	d - Name of the div object.
// v - Vertical scroll value.
// h - Horizontal scroll value.
//===========================================================
function setScroll(d,v,h)
{
	var od = get(d);
	od.scrollLeft = h;
	od.scrollTop = v;
}//saveScroll


//===========================================================
// win (NewWindow)
//-----------------------------------------------------------
// Open a new browser window.
//-----------------------------------------------------------
// p = page to display
// n = window name
// w = window width
// h = window height
// r = resizeable flag - 'yes' if resizeable
// m = menubar flag - 'yes' if menubar should be present 
// s = scrollbars flag - 'yes' if scrollbars allowed
// l = locations flag - 'yes' if location bar should be present
// t = toolbar flag - 'yes' if toolbar should be present
//===========================================================
function win(p,n,w,h,r,m,s,l,t)
{
	// Set defaults
	if(r==N)
		r='yes';
	if(m==N)
		m='yes';
	if(s==N)
		s='yes';
	if(l==N)
		l='yes';
	if(t==N)
		t='yes';
	var opt='resizable='+r+',menubar='+m+',scrollbars='+s+',location='+l+',toolbar='+t;

	if(w!=N)
	{
		opt+=',width='+w;
		lft=(screen.width)?(screen.width-w)/2:0;
		opt+=',left='+lft;
	}
	if(h!=N)
	{
		opt+=',height='+h;
		tp=(screen.height)?(screen.height-h)/2:0;
		opt+=',top='+tp;
	}
	
	var r= window.open(p,n,opt);
	if(r != null)
		r.focus();
	return r;
}//win

