function new_cap() {

	if( document.getElementById ) {
		var target = document.getElementById( 'cap' );
		if( target ) {
			target.src = target.src.replace( /[\d]+$/g, '' ) + Math.round( Math.random() * 100000 );
		}
	} else {
		alert( 'Sorry, cannot auto-reload image\nSubmit the form or refresh the page and a new image will be loaded' );
	}
	return false;
}
var photoWin = null;
function viewPhoto( imgSrc, imgTitle ) {

	if( photoWin ) {
		photoWin.close();
	}

	photoWin = window.open( '', 'photoWin', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,height=150,width=150' );

	var htm = '<html>' +
	'<head>' +
	'<title>' + imgTitle + '</title>' +
	'<meta http-equiv="imagetoolbar" content="false" />' +
	'</head>' +
	'<body style="margin: 0; padding: 10px; text-align: center;" onload="document.getElementById(\'theImage\').style.visibility=\'visible\';document.getElementById(\'closeWin\').style.visibility=\'visible\';window.resizeTo(( document.images[ \'theImage\' ].width > 750 ? 750 : document.images[ \'theImage\' ].width+50 ), ( document.images[ \'theImage\' ].height > 600 ? 600 : document.images[ \'theImage\' ].height )+100);">' +
	'<input type="button" id="closeWin" style="visibility: hidden; position: absolute; top: 15px; right: 10px; z-index: 100; background: #fff; border: 1px solid #000; color: #000; font: bold 11px Verdana, sans-serif; margin-right: 5px; display: inline;" onclick="window.close();" value="Close Window" /></div>' +
	'<img src="' + imgSrc + '" id="theImage" alt="' + imgTitle + '" style="visibility: hidden; z-index: 50;" />' +
	'</body>' +
	'</html>';

	photoWin.document.write( htm );
	photoWin.document.close();
	photoWin.focus();
}

var popUpWin = null;
function openWindow( winUrl, winWidth, winHeight ) {

    if( popUpWin ) {
		popUpWin.close();
	}
	popUpWin = window.open( winUrl, 'popUpWin', 'width=' + winWidth + ',height=' + winHeight + ',status=yes,scrollbars=yes,resizable=yes' );
}

function convert( str ) {

	if( str.name.toLowerCase().indexOf( 'postcode' ) != -1 ) {

		var words = str.value.split( ' ' );
		for( var word in words ) {
			words[ word ] = words[ word ].toUpperCase();
		}
		str.value = words.join( '' );

	} else if( str.name.toLowerCase().indexOf( 'telephone' ) != -1 || str.name.indexOf( 'other_number' ) != -1 || str.name.indexOf( 'mobile' ) != -1 ) {

		var replaceAlpha = str.value.replace( /([^.0-9-\+\(\)])/g, '' );
		str.value = replaceAlpha;

	} else if( str.name.toLowerCase().indexOf( 'email' ) != -1 ) {
		var eMailLowered = str.value.toLowerCase();
		str.value = eMailLowered;

	} else if( str.value != '' ) {
		var pattern = /(\w)([\/'a-zA-Z0-9_-]*)/;
		var a = str.value.split(/\s+/g);
		for (i = 0 ; i < a.length; i ++ ) {
			var parts = a[i].match( pattern );
			var firstLetter = parts[1].toUpperCase();
			var restOfWord = parts[2];
			a[i] = firstLetter + restOfWord;
		}
		str.value = a.join( ' ' );
	}
}

function verifyForm( theForm ) {

	for( i = 0; i < theForm.elements.length; i++ ) {
		var theFld = theForm.elements[i];
		var reqFld = ( theFld.id.indexOf( '_Req' ) != -1 ) ? true : false;
		var valSet = ( theFld.value == 0 || theFld.value == '' ) ? false : true;
		var fldChop = theFld.id.split( '_Req' );
		var fldName = fldChop[0].split( '_' ).join( ' ' );

		if( reqFld == true && ( valSet == false || ( theFld.type == 'checkbox' && theFld.checked == false ) ) ) {
			alert( fldName + ' cannot be left blank' );

			if( theFld.type == 'text' || theFld.type == 'textarea' ) {
				theFld.focus();
				theFld.select();
			} else {
				theFld.focus();
			}
			return false;
		}

		if( fldName.indexOf( 'Email' ) != -1 && valSet ) {
			if( !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test( theFld.value )) {
				alert( fldName + ' ' + theFld.value + ' is not in a recognised format' );
				theFld.focus();
				theFld.select();
				return false;
			}
		}

	}

    var btns = document.getElementsByTagName( 'input' );
	for( btn=0; btn<btns.length; btn++ ) {

		if( btns[ btn ].type == 'submit' ) {
			btns[ btn ].disabled = true;
			btns[ btn ].value = 'Sending...';
		}
	}
	return true;
}

function mailTo( e, m ) {

	if( ! e ) var e = 'info';
	if( ! m ) var m = 'Enquiry via web site...';
	var d = location.host.split( 'www.' ).join( '' );
	return '<a href="mailto:' + e + '@' + d + '?subject=' + m + '" onclick="return out( this.href );" title="E-mail: ' + e + '@' + d + '">' + e + '@' + d + '</a>';
}

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' ) ) {
			if( anchor.getAttribute( 'rel' ) == 'external' ) {
				anchor.onclick = function() {
					window.open( '/?out=' + this.href, '', '' );
					return false;
				}
			} else if( anchor.getAttribute( 'rel' ) == 'dev' ) {
				anchor.target = '_blank';
			}
		}
	}
}

function out( url ) {
	window.open( '/?out=' + url, '', '' );
	return false;
}

function setActiveStyleSheet( title ) {
	var i, a;
	for( i = 0; ( a = document.getElementsByTagName( 'link' )[ i ] ); i ++ ) {
		if( a.getAttribute( 'rel' ).indexOf( 'style' ) != -1 && a.getAttribute( 'title' ) ) {
			a.disabled = true;
			if( a.getAttribute( 'title' ) == title ) {
				a.disabled = false;
			}
		}
	}
}

function getActiveStyleSheet() {
	var i, a;
	for( i = 0; ( a = document.getElementsByTagName( 'link' )[ i ] ); i ++ ) {
		if( a.getAttribute( 'rel' ).indexOf( 'style' ) != -1 && a.getAttribute( 'title' ) && ! a.disabled ) {
			return a.getAttribute( 'title' );
		}
	}
	return null;
}

function getPreferredStyleSheet() {
	var i, a;
	for( i = 0; ( a = document.getElementsByTagName( 'link' )[ i ] ); i ++ ) {
		if( a.getAttribute( 'rel' ).indexOf( 'style' ) != -1 && a.getAttribute( 'rel' ).indexOf( 'alt' ) == -1 && a.getAttribute( 'title' ) ) {
			return a.getAttribute( 'title' );
		}
	}
	return null;
}

function createCookie( name, value, days ) {
	if( days ) {
		var date = new Date();
		date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
		var expires = '; expires=' + date.toGMTString();
	} else {
		expires = '';
	}
	document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie( name ) {
	var nameEQ = name + '=';
	var ca = document.cookie.split( ';' );
	for( var i = 0; i < ca.length; i ++ ) {
		var c = ca[ i ];
		while( c.charAt( 0 ) == ' ' ) {
			c = c.substring( 1, c.length );
		}
		if( c.indexOf( nameEQ ) == 0 ) {
			return c.substring( nameEQ.length, c.length );
		}
	}
	return null;
}

window.onload = function( e ) {
  var cookie = readCookie( 'style' );
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet( title );
  externalLinks();
}

window.onunload = function( e ) {
  var title = getActiveStyleSheet();
  createCookie( 'style', title, 365 );
}

var cookie = readCookie( 'style' );
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet( title );
