/*
 * -File        $Id: BaseLib.js,v 1.1 2002/05/23 01:34:00 turing Exp $
 * -License     LGPL (http://www.gnu.org/copyleft/lesser.html)
 * -Copyright   2001, The Turing Studio, Inc.
 * -Author      alex black, enigma@turingstudio.com
 */


/** 
 * this function opens a popup window. use:
 * open_window('http://www.yahoo.com','yahooWin','300','500');
 * 
 * note that you can safely omit 'windowScrollbars'
 * 
 * @author	Alex Black
 * @param	windowURL	the url to open in the new window
 * @param	windowName	the name of the new window
 * @param	windowWidth	the width in pixels of the new window
 * @param	windowHeight	the height in pixels of the new window
 * @param	windowScrollbars	can be set to yes|no|auto. controls the scrollbars
 */

function open_window(windowURL,windowName,windowWidth,windowHeight,windowScrollbars){
	windowName=window.open(windowURL, windowName,"width="+windowWidth+",height="+windowHeight+",toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollbars="+windowScrollbars+",resizable=1,dependant");
	windowName.focus();
}


/** 
 * this function sets the window's opener href to the url specified. if closeMe
 * is set to 1, the window closes itself after setting the location.
 * 
 * @author	Alex Black
 * @param	url	the url to set the parent's location to
 * @param	closeMe	value can be 0 or 1
 */
function set_parent_location(url,closeMe) {
	parent.opener.location.href = url;
	parent.opener.focus();
	
	if (closeMe == 1) {
		window.close();
	}
}


/**
 * this is a generalized function for setting form focus.
 * it takes the integer form index, and the element index to focus.
 * 
 * @author	Alex Black
 * @param	form_index	the document.forms index (integer)
 * @param	element_index	the form.elements index (integer)
 */
function setFocus(form_index, element_index) {
	if (document.forms[form_index].elements[element_index]) {
		document.forms[form_index].elements[element_index].focus();
	}
}


/**
 * this is part of a workaround to using images as submit buttons, but still
 * having netscape submit the form when the user hits 'enter'.
 * it scans all keys pressed - if the key is 'enter' is submits the form.
 * right now, it can only be used on a form with an index of 0.
 * 
 * the other bit of the code is:
 * 
 * if (navigator.appName == "Netscape"){
 * document.captureEvents(Event.KEYPRESS);
 * document.onkeypress = checkInputKey;
 * }
 * 
 * @author	Alex Black
 * @param	k	the kay passed in to check
 */
function checkInputKey(k) {
  	var key = escape(k.which);
  	if ( key == '3'){
	document.forms[0].submit();
  	}
}

/**
 * This function will check all the checkboxes in a particular form. It has been
 * abstracted to be used with any form, which means that you have to pass in
 * everything:
 * 	
 * -ckval is the value to set the checkbox to, can be "true" or "false"
 * -form_index is the integer index of the form. typically, this is 0, but if you
 * have some other forms on your page before the checkbox form, you'll need this.
 * 
 * example:
 * checkTheBoxes('true', '25', '0');
 * 
 * @author	Alex Black
 * @param	ckval	the value to set the element.checked attribute to
 * @param	form_index	the integer index of the form to check all the boxes in
 */
function checkTheBoxes(ckval, form_index) {
	var form_length = document.forms[form_index].elements.length;
	if (form_length != 0) {
		for (var i = 0; i <= form_length-1; ++i) { 
			if (document.forms[form_index].elements[i].type == "checkbox") {
				document.forms[form_index].elements[i].checked = ckval;
			}
		}
	}
}		

/**
 * img_switch('reply_via_email','images/','Moo.gif');
 * 
 * @author	Alex Black
 * @param	name        the name of the image (see above)
 * @param	basepath    the location in htdocs of the image
 * @param	src         the file to switch to.
 */
function img_switch(name, basepath, src) {
	path=basepath+src;
    if(document.images[name]){
        document.images[name].src = path;
    }
}

function img_preload(name, basepath) {
    if(document.images){
        if(!document.imgPreload) {
            document.imgPreload = new Array();
        }
        document.imgPreload[name] = new Image;
        document.imgPreload[name].src = basepath+name+"On.gif";
    }
}

/**
 * This is a workaround for the onFocus behaviour of mac browsers
 * which display a colored border around the a focused image enclosed
 * by a href tag. It's used like this:
 *
 * <a href="/url/" onFocus="Blur(this);">Url</a>
 *
 * @author	Andreas Aderhold, a.aderhold@thyrell.com
 * @param	obj	the object to blur
 */

function Blur(_obj) 
{
	if(_obj.blur) {
		_obj.blur();
	}
}
