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;
}

$( function() {
	$( 'input#Preferred_Start_Date_Req' ).calendar({ closeAtTop: false, minDate: new Date() });
});

var photoWin = null;
function viewPhoto( imgSrc, imgTitle ) {

	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+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>' +
	'<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 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;
			}
		}

		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;
}

function clearSaved( path ) {

	var c = confirm( 'Are you sure you want to permanently remove this Saved Order?' );
	if( c ) {
		return window.location = path;
	} else {
		return false;
	}
}

function mailTo( e, m ) {

	if( ! e ) var e = 'info';
	if( ! m ) var m = 'Enquiry via web site...';
	var d = 'tuckonline.com';
	return '<a href="mailto:' + e + '@' + d + '?subject=Enquiry via web site..." title="' + m + '">' + 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" ) && anchor.getAttribute( "rel" ) == "external" ) {
			anchor.target = "_blank";
		}
	}
}

function out( url ) {
	window.open( '/?out=' + url, '', '' );
	return false;
}

window.onload = function() {
	externalLinks();
}