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', 'status=0,width=150,height=150' );

	var htm = '<html>' +
	'<head>' +
	'<title>' + imgTitle + '</title>' +
	'<meta http-equiv="imagetoolbar" content="false" />' +
	'</head>' +
	'<body style="margin: auto; padding: auto; overflow: hidden;" onload="document.getElementById( \'theDiv\' ).style.visibility=\'visible\'; window.resizeTo( document.images[ \'theImage\' ].width + 30, document.images[ \'theImage\' ].height + 70 );">' +
	'<p style="font: 11px Verdana,sans-serif; position: absolute; top: 10px; left: 10px;">Image Loading<br /><blink>Please wait...</blink></p>' +
	'<div id="theDiv" style="visibility: hidden; position: absolute; top: 10px; left: 10px; text-align: center;"><img src="' + imgSrc + '" border="0" id="theImage" alt="' + imgTitle + '" /></div>' +
	'</body>' +
	'</html>';

	photoWin.document.write( htm );
	photoWin.document.close();
}

function preLoad( imgs ) {
	if( document.images && imgs ) {
		var preLoad = new Array();
		for( i = 0; i < imgs.length; i++ ) {
			preLoad[ i ] = new Image();
			preLoad[ i ].src = imgs[ i ];
		}
	}
}

function swapImg( target, imgSrc ) {

	// swaps the image
	var fld = new assignId( target );

	if( fld.obj ) {
		if( isNaN( parseInt( imgSrc ) ) ) {
			fld.obj.src = imgSrc;
		} else {
			fld.obj.src = prodImgs[ imgSrc ];
		}
	}
}

function assignId( target ) {

	// compatability check
	if( document.getElementById ) {
		this.obj = document.getElementById( target );
	} else if( document.all ) {
		this.obj = document.all[ target ];
	} else if( document.layers ) {
		this.obj = document.layers[ target ];
	}
}

var activeHotSpot = false;

function mapSwap( target ) {

	var div = new assignId( target );
	if( div.obj ) {
		if( activeHotSpot ) {
			activeHotSpot.style.display = 'none';
			activeHotSpot.style.visibility = 'hidden';
		}
		activeHotSpot = div.obj;
		div.obj.style.display = 'block';
		div.obj.style.visibility = 'visible';
	}
}

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( 'E-mail' ) != -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 externalLinks() {

	if( document.getElementsByTagName ) {
		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 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 out( url ) {
	window.open( '/?out=' + url, '', '' );
	return false;
}

window.onload = function() {
	externalLinks();
	preLoad( imgs );
	mapSwap( 'hs_0' );
}
