Event.observe(window, 'load', function() {
	//this is why i hate prototype and love jquery
	
	if( $('AngelLocationClaimRequestAddForm')!=null ){
		new Validation('AngelLocationClaimRequestAddForm');
	}

	if( $('UserLoginForm')!=null ){
		new Validation('UserLoginForm');
		//hide the states if this form defaults to non-US country
		if ($('UserProfileCountry').value!='US') {
			$('state_field').hide();
		}

		Event.observe('UserProfileCountry', 'change', validator.validateStatesForCountry);
	}
	
	if( $('LocationClaimForm')!=null ){
		Event.observe('LocationRegions', 'change', validator.validateSelect);
		Event.observe('LocationVenues', 'change', validator.validateSelect);
		Event.observe('LocationSearch', 'focus', validator.clearInput); 
		var searchField = $('LocationSearch');
		if(searchField.value == '') {
			searchField.value='Search Accounts';
		}
	}		
	if($('UserRegisterForm') != null){
		new Validation('UserRegisterForm');
	}
	
	if($('BarUploadForm') != null){
		new Validation('BarUploadForm');
	}
	
	if($('CapCodeClaimForm') != null){
		Event.observe('CapCodeCode1', 'keyup', function(){
			var fieldLength = $('CapCodeCode1').getValue().length; 
			if(fieldLength==4){
				$('CapCodeCode2').focus();
			};
		}, false);
		Event.observe('CapCodeCode2', 'keyup', function(){
			var fieldLength = $('CapCodeCode2').getValue().length; 
			if(fieldLength==4){
				$('CapCodeCode3').focus();
			};
		}, false);		
	}
	
	function init(){

		makeItCount('description', 300);

		makeItCount('comments',100);

		makeItCount('textarea', 55);	/*this textarea doesn't exist in the demo, but you see adding in the init does not return an error */

	}

	init();

	function charCounter(id, maxlimit){

		if (!$('counter-'+id)){

			$(id).insert({after: '<div id="counter-'+id+'"></div>'});

		}

		if($F(id).length >= maxlimit){

			$(id).value = $F(id).substring(0, maxlimit);

			$('counter-'+id).addClassName('charcount-limit');

			$('counter-'+id).removeClassName('charcount-safe');

		} else {	

			$('counter-'+id).removeClassName('charcount-limit');

			$('counter-'+id).addClassName('charcount-safe');

		}

		$('counter-'+id).update( $F(id).length + '/' + maxlimit );	

			

	}

	

	function makeItCount(id, maxsize){

		if ($(id)){

			Event.observe($(id), 'keyup', function(){charCounter(id, maxsize);}, false);

			Event.observe($(id), 'keydown', function(){charCounter(id, maxsize);}, false);

			charCounter(id,maxsize);

		}

	}	
	
});

function stateField(field) {
	if (field == null){
		field = "UserProfileCountry";
	}

	var country = document.getElementById(field);
	var state = document.getElementById("state_field");
	if(country.value != "US" && country.value != "United States") {
		state.style.display = 'none';
	} else {
		state.style.display = 'block';
	}
}

var validator = {
	
	validateSelect : function(event){
		var selectVal = Event.element(event).value;
		if(selectVal!='') $('LocationClaimForm').submit();
	},
	
	validateStatesForCountry : function(event){
		var selectVal = Event.element(event).value;
		if(selectVal != 'US') $('state_field').hide();
		else $('state_field').show();
	},
	
	clearInput : function(event){
		var input = Event.element(event)
		input.value='';
		Event.stopObserving('LocationSearch', 'focus', validator.clearInput);
	}
	
}