/*******************************************************************************

		Copyright 2009, ORGA Toolkit / Toomba
		All rights reserved

		$Id: libAJAX.js 46 2009-01-16 16:01:45Z jaquet $

*******************************************************************************/

// Cross browser function for adding events.
// http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
	else
	{
		// alert(type + "event handler '" + fn + "' could not be attached to element " + obj);
	}
}


// Cross browser function for adding events.
// http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
	else
	{
		alert(type + "event handler '" + fn + "' could not be removed from element " + obj);
	}
}


// Function to set the innerHTML of the second argument to the result
// of a request of the URL first argument. Synchronous. If there already
// is innerHTML it is erased.
function getContent(querystring, elementID, inc_div) //, toggle="TRUE", cache="FALSE")
{
	inc_div = typeof(inc_div) != 'undefined' ? inc_div : "FALSE";
	element = document.getElementById(elementID);
	if (element.innerHTML != '')
		element.innerHTML = '';
	else
	{
		var result; // Holds the response from the server
		var URL = '?event=' + querystring;
		result = syncHTTPGet(URL);
		if (inc_div == "FALSE")
		{		
			element.innerHTML = result;
		}
		else
		{
			element.innerHTML = '<div class="InfoContent">' + result + '</div>';
		}
	}
//	TODO: hide & cache option instead of zero-ing the innerHTML
}


// Returns HTTP response of URL as string
function syncHTTPGet(url)
{
	// Initialization code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
		xmlhttp.open("GET",url,false);
		xmlhttp.send(null);
	}
	// Initialization code for IE
	else if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("GET",url,false);
		xmlhttp.send();
	}

	// Perform the synchronous request
	if (xmlhttp)
	{
		// Error hadling
		if (xmlhttp.status != 200)
		{
			alert("There was a problem retrieving the data:\n" + req.statusText);
		}
		return xmlhttp.responseText;
	}
	alert("There was a problem initializing the XMLHttpRequest object");
	return "";
}

// onmouseover event for image swapping
// depends on the following naming convention:
//   - onmouseout image: <base>.jpg 
//   - onmouseover image: <base>_over.jpg
function swapImageIn()
{
	this.src = this.src.replace(".jpg","_over.jpg");
}

// onmouseout event for image swapping
// depends on the following naming convention:
//   - onmouseout image: <base>.jpg 
//   - onmouseover image: <base>_over.jpg
function swapImageOut()
{
	this.src = this.src.replace("_over.jpg",".jpg");
}

// Find all images of the element
// prelaod them and add event handlers to swap them
// on mouseover and mouseout
function preloadAndOnMouseOverDescendantImages(elem)
{
	if(document.getElementById(elem)) {
		var i;
		var j;
		var urlString;
		var images = document.getElementById(elem).getElementsByTagName("img");
	
		if (!document.preload)
			document.preload=new Array();
		j = document.preload.length;
	
		if (images)
		{
			for (i=0; i<images.length; i++)
			{
				urlSstring = images[i].src;
				urlSstring = urlSstring.replace(".jpg","_over.jpg");
				document.preload[j] = new Image;
				document.preload[j++].src = urlString;
				addEvent(images[i], 'mouseout', swapImageOut);
				addEvent(images[i], 'mouseover', swapImageIn);
			}
		}
	}
}

function addMenuSwaps()
{
	preloadAndOnMouseOverDescendantImages("menu_left");
	preloadAndOnMouseOverDescendantImages("menu_right");
}


addEvent(window, 'load', addMenuSwaps);


// Functions for generating passwords
// Adaption from http://javascript.internet.com/passwords/password-generator.html
function getRandomNum(lbound, ubound)
{
	return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}
function getRandomChar()
{
	var charSet = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
	return charSet.charAt(getRandomNum(0, charSet.length));
}
function getPassword(length)
{
	var rc = "";
	if (length > 0)
		rc = rc + getRandomChar();
	for (var idx = 1; idx < length; ++idx)
	{
		rc = rc + getRandomChar();
	}
	return rc;
}
function setPassword(elem)
{
	document.getElementById(elem).value = getPassword(8);
}
