function isDefined(property)
{
return (typeof property != 'undefined');
}

function correctPNG()
{

var arVersion = navigator.appVersion.split("MSIE")

var version = parseFloat(arVersion[1])

if ((version >= 5.5) && (isDefined(document.body.filters)))
{
for(var i=0; i<document.images.length; i++)
{
var img = document.images[i]
var imgName = img.src.toUpperCase()

if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
{
var imgID = (img.id) ? "id='" + img.id + "' " : ""
var imgClass = (img.className) ? "class='" + img.className + "' " : ""
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
var imgStyle = "display:inline-block;" + img.style.cssText 
if (img.align == "left") imgStyle = "float:left;" + imgStyle
if (img.align == "right") imgStyle = "float:right;" + imgStyle
if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
img.outerHTML = strNewHTML
i = i-1
}
}
}
}

// Targets
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"; 
}

}

}

function BlurLinks()
{
lnks			= document.getElementsByTagName("a");

for(i=0;i<lnks.length;i++)
{

lnks[i].onfocus	= new Function("this.blur()");
}

// Input Buttons
inpts			= document.getElementsByName("input");

for(i=0;i<inpts.length;i++)
{
inpts[i].onfocus= new Function("this.blur()");
}

}

function RunFunctions()
{
externalLinks();
BlurLinks();

if (document.all)
{
correctPNG();
}

}

if (isDefined(window.addEventListener))
{
window.addEventListener('load', RunFunctions, false);
}

else if (isDefined(window.attachEvent))
{
window.attachEvent('onload', RunFunctions);
}


/*
* Validates data types below using regular expressions
* 
*		-	email
*		-	date
*		-	mobile
*		-	postcode
*		-	password
*/
function validateType(type, val){
	switch(type){
		case 'email':
			var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
			break;
		case 'date':
			var pattern = /^(((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))$/;
			break;
		case 'mobile':
			var pattern = /^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/;
			break;
		case 'telephone':
			var pattern = /^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/;
			break;
		case 'postcode':
			var pattern = /^((([A-PR-UWYZ](\d([A-HJKSTUW]|\d)?|[A-HK-Y]\d([ABEHMNPRVWXY]|\d)?))\s*(\d[ABD-HJLNP-UW-Z]{2})?)|GIR\s*0AA)$/;
			break;
		case 'password':
			var pattern = /(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,15})$/;
			break;
		default:
			break;
	}
	return pattern.test(val); 
}

/*
*  Validates form content
*/
function validateForm(frm,reporting_div_id){
	/*------------------------------------------*/
	/*  CHECK ALL REQUIRED FIELDS HAVE A VALUE  */
	/*------------------------------------------*/
	var errors = [];
	var req;
	var error_string = '';
	//  Check for required hidden field so we know which fields are mandatory
	if(typeof frm.required==undefined){
		alert('Required hidden field missing');
		return false;
	}
	//  Check all required fields exist and have a value
	req = frm.required.value.split(",");

	for(var x=0; x<req.length; x++){
		if(typeof frm.elements[req[x]]=="undefined")
			errors[errors.length] = str_replace("_", " ", req[x]) + ' field doesn\'t exist';
		else{
			if(frm.elements[req[x]].value=='')
				errors[errors.length] = 'the ' + str_replace("_", " ", req[x]) + ' field must contain a value';
		}
	}
	//  Output errors if there are any
	if(errors.length>0){
		if(reporting_div_id!==false){						//  Either, print to screen within supplied <div>.....
			for(var x=0; x<errors.length; x++)
				error_string += '<li>' + errors[x] + '</li>';
			document.getElementById(reporting_div_id).innerHTML = '<ul class="error">' + error_string + '</ul>';
		}
		else{																			//	Or, show in alert box
			for(var x=0; x<errors.length; x++)
				error_string += '- ' + errors[x] + "\n";
			alert(error_string);
		}
		return false;
	}
	/*--------------------------------------------------------*/
	/*  PERFORM VALIDATION OF FIELD DATA BASED ON FIELD NAME  */
	/*--------------------------------------------------------*/
	var elems = frm.elements;
	for(var x=0; x<elems.length; x++){
		if(elems[x].name!='required' && in_array(elems[x].name, req)){
			switch(true){
				case elems[x].name.search('email')!=-1:
					if(!validateType('email', elems[x].value))
						errors[errors.length] = 'Invalid email address';
					break;
				case elems[x].name.search('date')>-1:
					if(!validateType('date', elems[x].value))
						errors[errors.length] = 'Invalid date (DD/MM/YY)';
					break;
				case elems[x].name.search('mobile')>-1:
					if(!validateType('mobile', elems[x].value))
						errors[errors.length] = 'Invalid mobile phone number';
					break;
				case elems[x].name.search('telephone')>-1:
					if(!validateType('telephone', elems[x].value))
						errors[errors.length] = 'Invalid phone number';
					break;
				case elems[x].name.search('postcode')>-1:
					if(!validateType('postcode', elems[x].value))
						errors[errors.length] = 'Invalid postcode';
					break;
				/*case elems[x].name.search('password')>-1:
					if(!validateType('password', elems[x].value))
						errors[errors.length] = 'Invalid password (must contain 1 number, 1 capital letter and be between 6 and 15 characters)';
					break;*/
				default:
					break;
			}
		}
	}
	//  Output errors if there are any
	if(errors.length>0){
		if(reporting_div_id!==false){						//  Either, print to screen within supplied <div>.....
			for(var x=0; x<errors.length; x++)
				error_string += '<li>' + errors[x] + '</li>';
			document.getElementById(reporting_div_id).innerHTML = '<ul class="error">' + error_string + '</ul>';
		}
		else{																			//	Or, show in alert box
			for(var x=0; x<errors.length; x++)
				error_string += '- ' + errors[x] + "\n";
			alert(error_string);
		}
		return false;
	}
	return true;
}


function in_array(needle, haystack, argStrict) {
	var key = '', strict = !!argStrict;
	if (strict) {
		for (key in haystack) {
			if (haystack[key] === needle) {
				return true;
			}
		}
	}
	else {
		for (key in haystack) {
			if (haystack[key] == needle) {
				return true;
			}
		}
	}
	return false;
}



function str_replace(search, replace, subject, count) {
	var 	i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
				f = [].concat(search),
				r = [].concat(replace),
				s = subject,
				ra = r instanceof Array, sa = s instanceof Array;
	s = [].concat(s);
	if (count) {
		this.window[count] = 0;
	}
	for (i=0, sl=s.length; i < sl; i++) {
		if (s[i] === '') {
			continue;
		}
		for (j=0, fl=f.length; j < fl; j++) {
			temp = s[i]+'';
			repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
			s[i] = (temp).split(f[j]).join(repl);
			if (count && s[i] !== temp) {
				this.window[count] += (temp.length-s[i].length)/f[j].length;
			}
		}
	}
	return sa ? s : s[0];
}