/**********************************************************
	Author:					Jonathan Allen
	Date Created:		21-01-2009
	Last updated:		28-01-2009
	Version:				1.0
	Contents:				commonly used javascript functions
								such as ajax and shorthand functions
**********************************************************/


// add onload event
function addOnload (fnc) {
	var old = window.onload;
	if (typeof old != 'function') window.onload = fnc;
	else window.onload = function () { old (); fnc (); }
} // function: addOnload





// getId shorthand function
function getId (id) { return document.getElementById(id) }





// class name manipulation (replaces colour changers by using custom classes)
function addClass (obj, clsName) { obj.className += " " + clsName; }
function remClass (obj, clsName) { obj.className = obj.className.replace (new RegExp (clsName + "\\b"), ''); }





// string trim
function trim (str) { return str.replace (/^\s\s*/, '').replace (/\s\s*$/, '') } // function: trim





// call popup window: optionals - width, height, full and name
function popup (url, width, height, full, name) {
	 // set default values (optional parameters)
	width = width || 1000; height = height || 600; full = full || false;
	name = name || 'window_' + (new Date () - 0);

	settings = 'statusbar=0, menubar=0, scrollbars=1, resizable=1' 	// add default settings
	settings += (!full ? ', width=' + width + ', height=' + height : ', fullscreen=1');

	window.open (url, name, settings);														// call popup window
} // function





// calls flash script - helps stop IE "click to activate": optionals - width, height, script, container (set null)
function call_flash (container, data, width, height, script) {
	width = width || 711;
	height = height || width;
	script = script || null;
	container = container || null;
	
	obj = "<object type='application/x-shockwave-flash' data='"+data+"' width='" + width + "' height='" + height + "'>" +
				"<param name='movie' value='"+data+"' /><param name='quality' value='high' />" +
				"<param name='wmode' value='transparent' /><param name='menu' value='false' />" ;
	if (script != null) obj += "<param name='flashvars' value='"+script+"'/><param name='allowScriptAccess' value='sameDomain' />";
	obj += "</object>";

	if (container != null) getId (container).innerHTML = obj;	
	else return obj;
} // function




function bookmarksite (title, url) {
	if (document.all) window.external.AddFavorite (url, title);
	else if (window.sidebar) window.sidebar.addPanel (title, url, "");
} // bookmarksite





/******************************************************************
	----------------------------------- Ajax functions -----------------------------------
	Example of use:
	url = get_url (getId ('myForm')); // returns submitted form for as "get" string
	getSend('worker.php?' + url, 'container'); // container being a tag ID
	
					----- if only sending data - remove the container option -----

	getSend('worker.php?getVar=value')
******************************************************************/
// send request
function getSend (url, fncName) {
	fncName = fncName || null;
	if (http.readyState == 0 || http.readyState == 4) {							// check availability
		http.open ("GET", url, true);														// send request - method (get) - URL of ducument - true for asynchronous
		http.onreadystatechange = fncName;											// set return function name
		http.send (null);																			// for post only - null as using get method
	} // if
	else setTimeout ("getSend ('" + url + "', " + fncName + ")", 10);		// wait and recall if not currently available
} // function: getSend



// setup http object
function getHTTPObject () {
	var httpObject = null;																	// set blank object variable
	if (window.XMLHttpRequest) httpObject = new XMLHttpRequest();		// check XMLHttpRequest object is available 
	else httpObject = new ActiveXObject("Microsoft.XMLHTTP");			// use ActiveX instead (how do you format again? :D)
	if (httpObject != null) { return httpObject; }									// returns object if aiablable
	else {																							// if nothing available (truely boned)
		alert ("I'm sorry, but your browser does not support Ajax!");			// let them know their browser sucks
		return false;																			// return false as nothing available
	} // else
} // function: getHTTPObject
var http = getHTTPObject();																// sets http object for ajax use



/* //  retrieve file contents
function getResponse () {
	if (http.readyState == 4)
		getId ('container').innerHTML = http.responseText;
} // function: getResponse */



// returns all form values as a 'get' type string
function get_url (frm) {
	var url = new Array ();
	for (i=0; i < frm.elements.length; i++) {
		obj = frm.elements [i];
		if (obj.name != '') {
			if ((obj.type == 'checkbox' || obj.type == 'radio') && obj.checked)
				url [url.length] = obj.name + "=" + obj.value
			else
				url [url.length] = obj.name + "=" + obj.value
		} // if
	} // for
	
	return url.join ('&');
} // form_submitter


