/**********************************************************
	Author:					Jonathan Allen
	Date Created:		05-06-2007
	Last updated:		28-01-2009
	Version:				1.3.4
	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) }



// ----------------------------------------------------------------------	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



// 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


/******************************************************************
	common validation solutions
******************************************************************/
// returns true if at least one option completed - false if nothing found
// 1st argument is the class name to add to object parents upon fail
function valid_options (class_name) {
	var found = false;
	for (i = 1; i < arguments.length; i++) {
		obj = getId (arguments [i]);
		remClass (obj.parentNode, class_name);
		if (obj.type == 'radio' || obj.type == 'checkbox')
			{ if (obj.checked) found = true; } // if
		else if (trim (obj.value) != '') found = true;
	} // for
	
	if (!found)
		for (i = 1; i < arguments.length; i++) {
			obj = getId (arguments [i]);
			addClass (obj.parentNode, class_name);
		} // for
	
	return found;
} // function



// returns false if any fields left blank
// 1st argument is the class name to add to object parents upon fail
function valid_requirements (class_name) {
	var err = false;
	for (i = 1; i < arguments.length; i++) {
		var reqErr = false;
		obj = getId (arguments [i]);
		// alert (obj.id + " : " + obj.value);
		remClass (obj.parentNode, class_name);
		if (obj.type == 'checkbox' && !obj.checked) reqErr = true;
		else if (trim (obj.value) == '') reqErr = true;

		if (reqErr) {
			addClass (obj.parentNode, class_name);
			// alert (obj.id + " : " + obj.value);
			err = true;
		} // if
	} // for
	
	return !err;
} // function



// validates with given functions (funciton must return true/false)
function valid_type (id, func, class_name) {
	var obj = getId (id), err = false, val = trim (obj.value);

	remClass (obj.parentNode, class_name);
	if (trim (val) != '') {
		eval ("err = !" + func + "('"+ val +"')");
		if (err) addClass (obj.parentNode, class_name);
	} // if

	return !err;
} // function



// 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-Za-z]{1,2}[0-9]{1,2}[A-Za-z]{0,2}\s?[0-9][A-Za-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


/******************************************************************
	----------------------------------- 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 */


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')) {
				if (obj.checked)
					url [url.length] = obj.name + "=" + obj.value
			} // if
			else url [url.length] = obj.name + "=" + obj.value
		} // if
	} // for
	
	return url.join ('&');
} // form_submitter
