// JavaScript Document
var Atterg = {

	RequireCountyForState: new Class({
		state: null,
		stateSelect: null,
		counties: null,
		required: false,
		countySelect: null,
		initialize: function (state) {
			this.state = state;
			this.counties = document.getElement('.counties[rel=' + state + ']');
			this.countySelect = this.counties.getElement('select');
			this.stateSelect = $('state');
			this.stateSelect.addEvent('change', this.onStateChange.bindWithEvent(this));
			this.checkIfRequired();		
		},
		
		onStateChange: function (event) {
			this.checkIfRequired();
		},
		
		checkIfRequired: function () {
			if (this.stateSelect.value == this.state) {
				this.require();
			} else {
				this.doNotRequire();
			}
		},
		
		doNotRequire: function () {
			this.required = false;
			this.disable();
		},
		
		require: function () {
			this.required = true; 
			this.enable();
			this.grabFocus();
		},
		
		enable: function () {
			this.counties.removeClass('hidden');
			this.countySelect.selectedIndex = 0;
		},
		
		disable: function () {
			if (this.counties.hasClass('hidden') == false) {
				this.counties.addClass('hidden');
			}
		},
		
		grabFocus: function () {
			this.countySelect.focus();
		},
		
		isEmpty: function (value) {
			if (
				value == null
				|| value.length == 0
				|| value == 0
			) {
				return true;
			}
			
			return false;
		},
		
		doesPass: function () {
			if (this.required == false) {
				return true;
			}
			
			if (this.isEmpty(this.countySelect.value)) {
				return false;
			} else {
				return true;
			}
		}		
	}),

	CheckoutForm1: new Class({
		counties: null,
		initialize: function () {
			form = document.getElement('form[name=Checkout1]');
			form.addEvent('submit', this.onSubmit.bindWithEvent(this));
			this.counties = new Array(
				new Atterg.RequireCountyForState('WI')
			);
			$('ShipSame').addEvent('change', this.onShipSameChange.bindWithEvent(this));
			
			this.toggleShipping();
		},
		
		onSubmit: function (event) {
			event.stop();
			
			for (var i = 0; i < this.counties.length; i++) {
				if (this.counties[i].doesPass() == false) {
					state = this.counties[i].state;
					message = "You indicated that you are a resident of " + state + ".\n\nPlease select a county.";
					new Atterg.FormErrorAlert(message, this.counties[i].countySelect);
					return false;
				}
			}
			
			ValForm();
		},
		
		onShipSameChange: function (event) {
			this.toggleShipping();
		},
		
		toggleShipping: function () {
			if ($('ShipSame').get('checked')) {
				this.disableShipping();
			} else {
				this.enableShipping();
			}
		},
		
		disableShipping: function () {
			document.getElements('.shipTo').each(function (item) {
				item.set('disabled', 'disabled');
			});
		},
		
		enableShipping: function () {
			document.getElements('.shipTo').each(function (item) {
				item.set('disabled', '');
			});
		}
	}),
	
	FormErrorAlert: new Class({
		initialize: function (message, focus) {
			alert(message);
			if (focus) {
				focus.focus();
			}
		}
	}),

    Pages: new Class({
        initialize: function () {
            this.global();
            $(document.body).getProperty('class').split().each( function (item) {
				if (this[item]) this[item]();
            }.bind(this));
        },

        global: function () {
        },

		home: function () {
			swfobject.embedSWF("../flash/atterglogo.swf", "logo", "238", "143", "8.0.0", null, null);
        },
        
        checkOut1: function () {
        	new Atterg.CheckoutForm1();
        }
	})
}

window.addEvent('domready', function(){
	new Atterg.Pages();
});