var currentStep = 0;

Validation.addAllThese([
	['validate-zip5', 'Please enter a valid United States Zip Code', function(v) {
		v = v.toUpperCase();
	    return Validation.get('IsEmpty').test(v) || /^\d{5}$/.test(v);
			}],
	['validate-zip4', 'Please enter a valid United States Zip Code Extension', function(v) {
		return Validation.get('IsEmpty').test(v) ||  /^\d{4}$/.test(v);
			}],
	['validate-name', 'Please use letters only (a-z) in this field.', function(v) {
		return Validation.get('IsEmpty').test(v) ||  /^([A-Za-z-\/\']+ ?)+$/.test(v);
			}] 
	]);


function submitInitialize() {
	var steps = $('step1','step2','step3','step4');
	var submitButton = $('submit-button');



	if ( currentStep > 0 ) {
		result = Form.getElements('step' + currentStep ).collect(function(elm) { return Validation.validate(elm); }).all();
		if (!result) return false;
		if (result && currentStep == 4) {
/*			console.warn('done'); */
			$('survey-form').submit();
			return true;
			}
		}

	steps.each(function(step){ step.style.display = 'none'; });
	
	steps[currentStep].style.display = 'block';
	currentStep++;
/*	console.log(currentStep); */
	if( currentStep < 4 ) {
		submitButton.className = 'next-button';
		submitButton.value = 'Next Step &#x203A;';
		submitButton.innerHTML = '<strong>Next Step &#x203A;</strong>';
		}
	else {
		submitButton.className = 'submit-button';
		submitButton.value = 'SUBMIT';
		submitButton.innerHTML = '<strong>SUBMIT</strong>';
		}
	document.location.hash = 'survey';
	return false;
	}


function submitCheck(ev) {
	Event.stop(ev);
	return submitInitialize();
	}


function onOther(evt) {
	var el = Event.element(evt);
	if(el.nodeName.toUpperCase() != 'INPUT') return false;

	/* It's IE. 'this' worked in FF */
	if(el.hasClassName('show-other') && el.checked ) {
		$(el.ancestors()[3].id + '-other').show();
/*		Effect.BlindDown(el.ancestors()[3].id + '-other', {afterFinish : function(e) { $(el.ancestors()[3].id + '-other').style.margin = '0pt 30px 1em 28px'; }}); */
		}
	else {
		$(el.ancestors()[3].id + '-other').hide();
/*		Effect.BlindUp(el.ancestors()[3].id + '-other'); */
		}
	}



var initValidator = new domFunction(function() {
	submitInitialize();
	$$('dl.other').each(function(other) { other.style.display = 'none'; });
	$$('input.show-other').each(function(other) { Event.observe(other.ancestors()[3], 'click', onOther); });
	/* per-page validation hppens here */
	Event.observe('survey-form', 'submit', submitCheck, false);
}, { 'survey' : 'id'} );











