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();
}

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;
		}

		if( fldName.indexOf( 'Card Type' ) != -1 ) {
			if( theFld.value.indexOf( 'Switch' ) != -1 || theFld.value.indexOf( 'Solo' ) != -1 ) {
				var cardIssue = theForm.elements[ 'card_issue_number' ];
				if( cardIssue.value == '' ) {
					alert( 'Issue Number must be completed when paying by Switch or Solo' );
					cardIssue.focus();
					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;
}