/* -File:       $Id: binarycloud.js,v 1.3 2004/07/02 17:44:56 purestorm Exp $
 * -Copyright:  Multiple authors (have a look at the comments)
 * -Author:     manuel holtgrewe <purestorm at teforge.org>
 */

/**
 * This file contains javascript code snippets collected from all
 * over the web with copyright notes etc. They are used throughout
 * the binarycloud base templates.
 */

/* This method returns the absolute X coordinate of the given
 * DOM node object.
 *
 * @author  Peter-Paul Koch
 * @see     http://www.quirksmode.org/
 */
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}


/* This method returns the absolute Y coordinate of the given
 * DOM node object.
 *
 * @author  Peter-Paul Koch
 * @see     http://www.quirksmode.org/
 */
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }

    return curtop;
}

/** 
 * 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();
	}
}

/*
 * These functions are used for swapping a named image with another
 * on an onmouseover event.
 * 
 * @author  jason hines, jason@greenhell.com
 */

function imageSwap(image_name, image) {
  if(document.images) {
    document.images[image_name].src = image;
    return true;
  }
}

function imagePreload(image_name, image) {
  if(document.images) {
    if(!document.imgPreload) {
      document.imgPreload = new Array();
    }  
    document.imgPreload[image_name] = new Image;
    document.imgPreload[image_name].src = image;
    return true;
  }
}

/*
 * These 3 functions are used for trimming whitespaces from strings
 *
 * @author jason hines, jason@greenhell.com
 * @param     the string to trim
 */

function ltrim(s) {
        return s.replace(/^\s*/, "")
}

function rtrim(s) {
        return s.replace(/\s*$/, "");
}

function trim(s) {
        return rtrim(ltrim(s));
}


/*
 * The followig code is a cute snippet written or stolen by Alex.
 * Added by Manuel
 */

function toggleVisibility(object) {
  styleObj = document.getElementById(object).style;
  if (styleObj.display=='none') {
    styleObj.display = '';
  } else {
    styleObj.display = 'none';
  }
}
