
jQuery( document)
	.ready( function() {
		requiredBind( document.getElementById('arceForm'));
		
		jQuery( 'div.bindAddress')
			.each( function( index) {
				bindAddresses( this);
			});
		
		jQuery( 'input.bindPhone')
			.each( function( index) {
				bindPhone( this);
			});
		
		jQuery( 'input.money')
			.each( function( index) {
				limitMoney( this);
			});
		
		jQuery( 'div.help')
			.each( function( index) {
				bindHelpItem( this);
			});

		jQuery( '#frmMailcountry')
			.each( function( index) {
				bindCountry( this);
			});
		
		jQuery( '#frmNametag')
			.each( function( index) {
				bindNameTag( this, '');
			});
		
		jQuery( '#frmNametagb')
			.each( function( index) {
				bindNameTag( this, 'b');
			});
		
		bindCheckoutButtons();
	});


function bindNameTag( field, ab) {
	var src = [ '#frmPname' + ab, '#frmFname' + ab, '#frmMname' + ab, '#frmLname' + ab, '#frmSname' + ab ];
	jQuery( field)
		.data( 'autofill', src)
		.change( function( e) {
			jQuery( this).data( 'autofill', [] );
		});
	jQuery( src.join( ','))
		.data( 'nameTag', jQuery( field))
		.change( function( e) {
			var jNameTag = jQuery( this).data( 'nameTag');
			var src = jNameTag.data( 'autofill');
			if ( src.length > 0) {
				var v = [];
				var val;
				for ( var s = 0; s < src.length; s++) {
					val = jQuery( src[ s]).val();
					if ( val != '')
						v[ v.length] = val;
				}
				jNameTag.val( v.join( ' '));
			}
		});
		
}


function bindCheckoutButtons() {
	jQuery( '#paypalCheckoutButton, #googleCheckoutButton')
		.click( function(e) {
			var Dm = this.form.Dm;
			if ( Dm)
				jQuery.get( Dm.value);
			jQuery( '#mainContainer')
				.fadeTo( 'normal', 0.2, function() {
					Dm.form.submit();
				});
			e.stopPropagation();
			return false;
		});
}


function getAmountFromGroup( selector) {
	var amount = 0.00
	var jAmount = jQuery( selector + ' input:radio:checked');
	if ( jAmount.length > 0)
		var amount = parseFloat( ( jAmount.val() == '-OTHER' ? jQuery( selector + ' input[name$=Other]').val() : jAmount.val()).replace( /[^\d\.]/g, ''));
	return isNaN( amount) ? 0.00 : amount;
}


function bindPhone( textInput) {
	if ( textInput && textInput.tagName == 'INPUT') {
		textInput.onchange = function(){
			var phone = this.value.replace( /[^\d]/g, '');
			if ( phone.length >= 10)
				this.value = phone.replace( /(\d{3})(\d{3})(\d{4})(.*)/, '($1) $2-$3 $4');
			required( this);
		};
	}
}

function format_phone_usa( input) {
	input.value = input.value.replace( /(^\s*|\s*$)/g, '');
	var tendig = input.value.replace( /^(([^\d]*\d){10}).*$/, '$1');
	var matches = tendig.replace( /[^\d]/g, '').match( /^(\d{3})(\d{3})(\d{4})$/);
	if ( matches) 
		input.value = '(' + matches[1] + ') ' + matches[2] + '-' + matches[3] + ( tendig.length < input.value.length ? ' ' + input.value.substr( tendig.length).replace( /^\s+/, '') : '');
}

// If country selected is 'USA', require phone
function bindCountry( select) {
	jQuery( select)
		.change( function( e) {
			if ( this.options[this.selectedIndex].value == 'USA') {
				jQuery( '#frmPhoneLabel')
					.addClass( 'required')
					.removeClass( 'optional');
				jQuery( '#frmPhone')
					.change( function( e) {
						format_phone_usa( this);
						required( this);
					})
					.trigger( 'change');
				jQuery( '#frmFax')
					.change( function( e) {
						format_phone_usa( this);
					})
					.trigger( 'change');
			}
			else {
				jQuery( '#frmPhoneLabel')
					.removeClass( 'required')
					.removeClass( 'requiredGood')
					.addClass( 'optional');
				jQuery( '#frmPhone')
					.unbind( 'change');
				jQuery( '#frmFax')
					.unbind( 'change');
			}
		})
		.trigger( 'change');
}

function bindAddresses( obj) {
	if ( obj) {
		// Make entries in mail address fill out bill address if blank
		var inputs = obj.getElementsByTagName( 'input');
		for ( var i = 0; i < inputs.length; i++) {
			if ( document.getElementById( inputs[i].id.replace( /Mail/, 'Bill')))
				inputs[i].onchange = function(){
					required( this);
					var bill = document.getElementById( this.id.replace( /Mail/, 'Bill'));
					if ( bill && bill.value == '') {
						bill.value = this.value;
						required( bill);
					}
				};
		}
		// Handle state selector
		var obj = document.getElementById( 'frmMailstate');
		if ( obj && obj.tagName == 'SELECT')
			obj.onchange = function(){
				required( this);
				var bill = document.getElementById( this.id.replace( /Mail/, 'Bill'));
				if ( bill && bill.selectedIndex == 0) {
					bill.selectedIndex = this.selectedIndex;
					required( bill);
				}
			};
	}
}
	
function limitMoney( other) {
	if ( other)
		other.onkeydown = function(e){
			e = e || window.event;
			return ( e.keyCode == 8 || e.keyCode == 9 || e.keyCode == 37 || e.keyCode == 39 || (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105) || ((e.keyCode == 46 || e.keyCode == 110 || e.keyCode == 190) && !this.value.match(/\./)));
		};
}

function bindHelpItem( helpObj) {
	var jHelp = jQuery( helpObj);
	if ( jHelp.length > 0) {
		var jHelpLabel = jHelp.find( '.helpLabel');
		var helpLabelText = jHelpLabel.length > 0 ? jHelpLabel.html() : 'Help';
		jHelp.slideUp();
		jQuery( '<a />')
			.addClass( 'help')
			.attr( 'href', '#Help')
			.data( 'jHelp', jHelp)
			.toggle( 
				function(e ) {
					jQuery( this).data( 'jHelp').slideDown();
				},
				function(e ) {
					jQuery( this).data( 'jHelp').slideUp();
				}
			)
			.html( helpLabelText)
			.appendTo( jHelp.parent().find('label').first());
	}
}
	

jQuery( document)
	.ready( function() {
		
		membershipBind( document.getElementById('arceForm'));
		
	});

function membershipBind( form) {
	jQuery( form)
		.submit( function( e) {
			var good = false;
			if ( !requiredAll( this))
				alert( 'Please complete "' + requiredAllLastFeild + '"');
			
			else 
				good = true;
			
			if ( !good)
				e.stopPropagation();
			return good;
		})
		.find( '#frmMembership')
			.change( function( e) {
				var mQty = jQuery( '#frmMembershipQty');
				if ( this.options[ this.selectedIndex].text.match( /Student/))
					mQty
						.attr( 'checked', false)
						.closest( 'label')
							.hide();
				else
					mQty
						.closest( 'label')
							.show();
			})
			.trigger( 'change');
}

