var photoWin = null;
function viewPhoto( imgSrc, imgTitle ) {
	// written by apatheticgenius.com - Ta Tim!
	if( photoWin ) {
		photoWin.close();
	}
	photoWin = window.open( '', 'photoWin', '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\';document.getElementById(\'closeWin\').style.visibility=\'visible\';window.resizeTo(document.images[ \'theImage\' ].width+10,document.images[ \'theImage\' ].height+28);">' +
	'<p style="font: 11px Verdana,sans-serif; position: absolute; top: 10px; left: 10px;">Loading...</p>' +
	'<div id="theDiv" style="visibility: hidden; position: absolute; top: 0; left: 0;"><img src="' + imgSrc + '" border="0" id="theImage" alt="' + imgTitle + '" /></div>' +
	'<div id="closeWin" style="visibility: hidden; position: absolute; top: 5px; left: 0; width: 100%; z-index: 100; text-align: right; display: block;"><form><input type="button" style="background: #fff; border: 1px solid #000000; color: #000000; font: bold 11px Verdana, sans-serif; margin-right: 5px; display: inline;" onclick="window.close();" value="Close Window" /></form></div>' +
	'</body>' +
	'</html>';

	photoWin.document.write( htm );
	photoWin.document.close();
}

var subWin = null;
function viewPage( URL, Width, Height ) {
	// written by apatheticgenius.com - Ta Tim!
	if( subWin ) {
		subWin.close();
	}
	subWin = window.open( URL, 'subWin', 'scrollbars=yes,width=' + Width + ',height=' + Height );

}

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 ) {

		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)(\w*)/;
		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].toLowerCase();
			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 ) {
			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 ) {
			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;
			}
		}

		if( fldName.indexOf( 'Card Number' ) != -1 && theFld.value.length < 16 ) {
			alert( fldName + ' must contain at least 16 characters' );
			theFld.focus();
			return false;
		}
	}

	for( i=0; i<theForm.elements.length; i++ ) {
		var theFld = theForm.elements[i];
		if( theFld.type == 'submit' ) {
			theFld.disabled = true;
			theFld.value = 'Sending...';
		}
	}
	return true;
}

function openWin( pageUrl, winName, params ) {

    if( typeof( poppedWin ) == 'object' ) {
        if( ! poppedWin.closed ) {
            poppedWin.close();
        }
    }
    poppedWin = window.open( pageUrl, winName, params );
}
