/**********************************************************
	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); }





// call colour change with given colours
function setRed (id) { setCol (id,'#F00','#FDD'); }
function setGreen (id) { setCol (id,'#0F0','#DFD'); }
function setBlue (id) { setCol (id,'#00F','#DDF'); }
function reset (id) { setCol (id); }

// change input colour: optionals - border, background
function setCol (id, border, background) {	
	border = border || '#999';
	background = background || '#FFF';
		
	getId (id).style.border = '1px solid '+ border;
	getId (id).style.background = background;
} // function: setCol





// 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 || 600; height = height || 500; 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: popup





// 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




/*
// Action to handle links that open a new page
// This is needed because "target" is depreciated in xhtml and will not validate on Strict formatting.
function externalLinks() {
	if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") &&
			anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
		}
}
window.onload = externalLinks;


*/

// move through links withing given sections and turn all into popups
function setPopups (id) {
	var template = 'template=popup';
	
	// loop through arguments
	for (i = 0; i < arguments.length; i++) {
		// save and loop through 'a' tags in current container
		var objs = getId (arguments [i]).getElementsByTagName ('a');
		for (n = 0; n < objs.length; n++) {
			var curLink = objs [n], href = curLink.href;

			// if not calling a javascript function already
			if (href.indexOf ('javascript') < 0) {
				// set javascript call wrapper
				var open = 'javascript:popup ("', close = '")';

				// remove target on link to prefent attempted opening of javascript as a link
				curLink.target = '';

				// for all external links, just call link in popup
				if (href.indexOf (document.domain) < 0)
					curLink.href = open + href +  '", "800", "600' + close;
				
				// all internal links need a template request added.
				else {
					var template_open = '?';
					if (href.indexOf (template_open) > -1) template_open = '&amp;';
					
					// if a hash request is present, put template request before the hash
					if (href.indexOf ('#') > -1) {
						var href_open = href.substr (0, href.indexOf ('#'));
						var href_close = href.substr (href.indexOf ('#'), href.length);
						curLink.href = open + href_open + template_open + template + href_close + close;
					} // if
					else curLink.href = open + href + template_open + template + close;

				} // else
			} // if
		} // for
	} // for
}
/******************************************************************
	----------------------------------- 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 (var 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

