
// Instantiate series autocomplete on body load
var whereToBuy = {
	init: function() {
		YAHOO.util.Event.addListener("region", "change", whereToBuy.regionChange);
		YAHOO.util.Event.addListener("whereToBuyForm", "submit", whereToBuy.submitForm);
	},
	
	submitForm: function(e) {
		var region = document.getElementById('region');
		var region = region.options[region.selectedIndex].value;
		var productOptions = document.getElementsByName('productOptions[]');
		var errors = new Errors();
		if(!region) {
			errors.add('You must choose your location');
		}
		else {
			if(region == 'United States') {
				var zip = document.getElementById('zip');
                var usState = document.getElementById('usState');
                if(usState.selectedIndex == 0 && zip.value.length == 0)
                {
                    errors.add('You must choose a state or enter your zip code');
                }
                else if(usState.selectedIndex > 0 && zip.value.length > 0)
                {
                    errors.add('You must choose a state OR enter your zip code, not both');
                }
				else if(zip.value.length != 0 && zip.value.length != 5) {
					errors.add('Your zip code must be 5 digits long');
				}
			}
			else if(region == 'Canada') {
				var canadaProvince = document.getElementById('canadaProvince').options;
				var provinceChosen = false;
				for(var i = 0; i < canadaProvince.length; ++i) {
					if(canadaProvince[i].selected) {
						provinceChosen = true;
					}
				}
				if(!provinceChosen) {
					errors.add('You must choose your province');	
				}
			}
			else {
				var latinAmericaCountry = document.getElementById('latinAmericaCountry').options;
				var regionChosen = false;
				for(var i = 0; i < latinAmericaCountry.length; ++i) {
					if(latinAmericaCountry[i].selected) {
						regionChosen = true;
					}
				}
				if(!regionChosen) {
					errors.add('You must choose your location');	
				}
			}
		}
		var productsChosen = false;
		for(var i = 0; i < productOptions.length; ++i) {
			if(productOptions[i].checked) {
				productsChosen = true;
			}
		}
		if(!productsChosen) {
			errors.add('You must choose at least one type of product');
		}
		if(errors.length > 0) {
			YAHOO.util.Event.stopEvent(e);
			errors.display();
			return false;
		}
		return true;
	},
	
	regionChange: function(e) {
		var region = document.getElementById('region');
		var zipAndStateContainer = document.getElementById('zipAndStateContainer');
		var latinAmericaCountryContainer = document.getElementById('latinAmericaCountryContainer');
		var canadaProvinceContainer = document.getElementById('canadaProvinceContainer');
		region = region.options[region.selectedIndex].value;
		if(region) {
			if(region == 'United States') {
				zipAndStateContainer.style.display = 'block';
				latinAmericaCountryContainer.style.display = 'none';
				canadaProvinceContainer.style.display = 'none';
			}
			else if(region == 'Canada') {
				zipAndStateContainer.style.display = 'none';
				latinAmericaCountryContainer.style.display = 'none';
				canadaProvinceContainer.style.display = 'block';
			}
			else {
				zipAndStateContainer.style.display = 'none';
				latinAmericaCountryContainer.style.display = 'block';
				canadaProvinceContainer.style.display = 'none';
			}
		}
		else {
			zipAndStateContainer.style.display = 'none';
			latinAmericaCountryContainer.style.display = 'none';
			canadaProvinceContainer.style.display = 'none';
		}
	},

    submitState: function(state)
    {
        var regionBox = document.getElementById('region');
        var stateBox = document.getElementById('usState');
        var zipBox = document.getElementById('zip');
        zipBox.value = '';
		for(var i = 0; i < regionBox.length; ++i) {
			if(regionBox[i].value == 'United States') {
				regionBox[i].selected = true;
			}
		}
		for(i = 0; i < stateBox.length; ++i) {
			if(stateBox[i].value == state) {
				stateBox[i].selected = true;
			}
		}
        document.getElementById('whereToBuyForm').submit();
    }

}
YAHOO.util.Event.addListener(window, "load", whereToBuy.init);
