$(document).ready(
	function() {

		// check whether activeElement has already been defined
		// it will have been if the form is in "thank you" mode.
		// php outputs a script block with 'thanks' as a flag


		var hash = window.location.hash, nTabId;
		if( typeof window.activeElement != 'undefined') {
			if( window.activeElement == 'thanks' ) {
				$('#content_0').hide();
				$('#tab_0').removeClass('active');
				$('#thanks').show();
			}
		} else if( hash.length > 0 && !isNaN( nTabId = hash.substring(1)) ) {
			if( $('#tab_' + nTabId).size() == 0 ) {
				nTabId = 0;
			}
			window.activeElement = 'tab_' + nTabId;
			$('#content_0').hide();
			$('#tab_0').removeClass('active');
			$('#tab_' + nTabId).addClass('active');
			$('#content_' + nTabId).show();
		} else {
			window.activeElement = 'tab_0';
		}


		// if this page is the destination of a remote file and the remote
		// file wants to display an alternate tab as default, it will define
		// the tab as an integer in location.hash
		if( hash.length > 0 && !isNaN( nTabId = hash.substring(1)) ) {
			window.activeElement = 'tab_' + nTabId;
		}

		// start: tabs click, mouseover, mouseout
		$('#tabs').find('a').click(
			function() {
				// quick ref
				var _this = $(this);
				var qTab, qPanel;
				var sPanelId;
				
				$('#thanks').hide();
				
				if( window.activeElement != null) {
					// references to previously active tab & content
					qTab = $('#' + window.activeElement);
					
					qPanel = $('#' + window.activeElement.replace('tab_', 'content_') );
					
					// hide/disable previously active tab & content
					qTab.removeClass('active');
					qPanel.hide();
				}

				// store reference to new active element
				window.activeElement = _this.attr('id');
				
				qTab = $('#' + window.activeElement);
				sPanelId = window.activeElement.replace('tab_', 'content_');
				qPanel = $('#' + sPanelId);
				

				// show/enable newly active tab & content
				qTab.addClass('active');
				qPanel.show();
			
			}
		);
		
		$('table.form input[@type=text], table.form textarea').bind('focus', 
			function() {
				if( this.value == this.defaultValue) {
					this.value = '';
				}
			}
		).bind('blur', 
			function() {
				if( this.value == '') {
					this.value = this.defaultValue;
				}
			}
		);


        var jState = $('#state'),
            jAltState = $('#alt_state');

        $('#country').bind('change', function(){
            if( this[this.selectedIndex].value === 'USA' ) {
                jState.show();
                jAltState.hide();
            } else {
                jState.hide();
                jAltState.show();
            }

        });


	} // end: ready()
	
);


// so that banners, links, etc can show tabs
function showTab(tab) {
	var qTab = $('#' + window.activeElement);
	var qPanel = $('#' + window.activeElement.replace('tab_', 'content_') );
	qTab.removeClass('active');
	qPanel.hide();
				
	window.activeElement = 'tab_' + tab;

	var qTab = $('#tab_' + tab);
	var qPanel = $('#content_' + tab);
	// show/enable newly active tab & content
	qTab.addClass('active');
	qPanel.show();

}
