/**********************************************************
	Author:					Jonathan Allen
	Date Created:	05-06-2007
	Last updated:		17-01-2008
	Version:				1.3.2
	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) }

// 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;

// ----------------------------------------------------------------------	common functions
// 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



// email check
function validEmail (email) {
	reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4}|museum|travel)$/
	return reg.test (email);
} // function: validEmail



// postcode check
function validPostCode (postcode) {
	reg = /^[A-Z,a-z]{1,2}[0-9]([A-Z,a-z]|[0-9])?\s?[0-9][A-Z,a-z]{2}$/
	return reg.test (postcode);
} // function: validPostCode



// UK Date check
function isUKDate (curDate) {
	if (typeof curDate == 'undefined') return false;

	if (curDate.indexOf ('/') < 0) return false;
	curDate = curDate.split ('/');
	d = new Date (curDate [2], curDate [1] -1, curDate [0]);
	
	if (d.getFullYear () != curDate [2] || curDate [2] < 1000) return false;
	if (d.getMonth () != curDate [1]-1) return false;
	if (d.getDate () != curDate [0]) return false;

	return true;
} // function: isUKDate



// 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: 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: call_flash



/******************************************************************
	----------------------------------- Ajax functions -----------------------------------
	Example of use:
	
	getSend('worker.php?getVar=value','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 */



