

function ShowStep(stepNumber, skipValidation)
{
	if ( skipValidation || validateStep(stepNumber-1) ) {
		
		for ( var i = 1; i<= 3; i++ ) {
			if ( $('registration_step_' + i).visible() ) $('registration_step_' + i).hide();	
		}
		
		$('registration_step_' + stepNumber).show();	
		
		scroll(0,0);
	}
}



function showDates(element)
{
	dateContainer = document.getElementById(element.id + 'Dates');
	
	if ( element.checked ) {
		dateContainer.style.display = '';
	}
	else {
		dateContainer.style.display = 'none';
	}
}

function checkUsername(username)
{
	params = 'Username=' + username;
	
	var Req = new Ajax.Request('/volunteer/volunteer_exists.json.php',
	{
		method:'post',
		parameters: params,
		onSuccess: function(response) { 
						var json = response.responseText.evalJSON(); 
						if ( json.UserExists ) {
							$('UsernameError').innerHTML = 'A user with this username already exists.';
							$('IsDuplicateUser').value = 1;
						}
						else {
							$('UsernameError').innerHTML = '';
							$('IsDuplicateUser').value = 0;
						}
					},
		onFailure: function(response){ }
	});
}

function validateStep(stepNumber)
{
	if ( stepNumber == 1 ) {
		
		if ( $('IsDuplicateUser').value == "1" ) {
			alert("Please select a username that does not already exist.");
			return false;
		}
		
		if ( !document.getElementById('FirstName').value || !document.getElementById('LastName').value ) {
	 		alert("Please provide your full name");
			return false;
		}
		
		if ( !document.getElementById('Address1').value || !document.getElementById('City').value || !document.getElementById('PostalCode').value ) {
	 		alert("Please provide your full address including City and Zip Code");
			return false;
		}
		
		if ( !document.getElementById('Phone').value ) {
	 		alert("Please provide a valid phone number.");
			return false;
		}
		
		if ( !document.getElementById('Username').value ) {
	 		alert("Please provide a username.");
			return false;
		}
		
		if ( !document.getElementById('Password').value ) {
	 		alert("Please provide a password.");
			return false;
		}
		
		if ( !document.getElementById('PrimaryEmail').value || !document.getElementById('PrimaryEmail').value.match(/@/) ) {
	 		alert("Please provide a valid email address");
			return false;
		}
		
		if ( !document.getElementById('PrimaryEmail').value || !document.getElementById('PrimaryEmail').value.match(/@/) ) {
	 		alert("Please provide a valid email address");
			return false;
		}
		
		if ( document.getElementById('AlternativeEmail').value && !document.getElementById('AlternativeEmail').value.match(/@/) ) {
	 		alert("Alternative email address not valid - please re-enter");
			return false;
		}
		
		if ( !getCheckedValue(document.Register.Proficiency) ) {
	 		alert("Please specify your tax proficiency");
			return false;
		}
	
		if ( !getCheckedValue(document.Register.LacerteExperience) ) {
	 		alert("Please specify your lacerte experience");
			return false;
		}
		
		if ( getCheckedValue(document.Register.LacerteExperience) ) {
			var lacerteYears = parseInt(document.getElementById('LacerteYears').value);
			var experience = getCheckedValue(document.Register.LacerteExperience);
			
			if ( experience != 'Beginner' ) {
				if ( lacerteYears.toString() == 'NaN' || lacerteYears.toString() == 0 ) {
					alert('Please specify number of years using Lacerte');
					return false;
				}
			}
		}
		
		cpaLevel = getCheckedValue(document.Register.CPALevel)
		otherCpaLevel = document.getElementById('OtherCPALevel').value;
		if ( cpaLevel == "Other - Non tax related professional/student. Please list" && ! otherCpaLevel ) {
			alert('Please list your CPA level in the "Other Level" box.');
			return false;
		}
		
		if ( cpaLevel != "Other - Non tax related professional/student. Please list" && otherCpaLevel ) {
			alert('The CPA "Other Level" box does not need to be filled out if your level is "' + cpaLevel + '".');
			return false;
		}
		
		if ( !document.getElementById('AgreementAccepted').checked ) {
	 		alert("You must accept the terms of the agreement in order to become a volunteer.");
			return false;
		}
	}
	
	// Validate training session selection
	if ( stepNumber == 2 ) {
		// If the user either selects "beginning" under Lacerte Experience, or enters a value of 3 or less in Number of years using Lacerte they are required to select 2 or more training locations
		if ( getCheckedValue(document.Register.LacerteExperience) ) {
			var lacerteYears = parseInt(document.getElementById('LacerteYears').value);
			var lacerteExperience = getCheckedValue(document.Register.LacerteExperience);
			
			if ( lacerteExperience == 'Beginner' || lacerteYears <= 3 ) {
				if ( countTrainingDatesSelected() < 2 ) {
					alert('Please select 2 or more training locations');
					return false;
				}
			}
		}
		
		if ( document.getElementById('NumberOfYears').value != '*' ) {
		
			var years = parseInt(document.getElementById('NumberOfYears').value);
			
			if ( (years.toString() == 'NaN' || years != 1988) && !hasSelectedTrainginDate() ) {
		 		alert("Please specify a training location");
				return false;
			}
			
			if ( earlyTrainingSession() ) {
		 		alert("Please select an alternate training session date, and contact Minnie to see if the earlier date still has availability.");
				return false;
			}
		}
	}
	
	// Validate site location selection
	if ( stepNumber == 3 ) {
		
		if ( !validDatesSelected() ) {
			return false;	
		}
		
		if ( !hasVolunteered() ) {
			alert("Please select a volunteer location");
			return false;	
		}	
	}
	
	return true;
	
	
	// Define functions
	
	function earlyTrainingSession()
	{
		var elements = document.getElementsByName('TrainingLocation');
		
		// Iterate through dates for the training session date
		for ( i = 0; i < elements.length; i++ ) {
			
			if ( elements[i].checked ) {
			
				var dtTrainSessionDeadline = new Date(elements[i].getAttribute('alt'));
				dtTrainSessionDeadline.setDate(dtTrainSessionDeadline.getDate()-2)
				
				var dtToday = new Date();
				
				if ( dtTrainSessionDeadline <= dtToday ) 
				{
					return true;
				}				
				
				return false;
				
			}
		}		
		return false;
	}	
	
	function hasVolunteered()
	{
		var volunteered = false;
		
		$$('input[type="checkbox"][id^="VolunteerLocation"]').each(function(ele){
			if( $(ele).checked )
			{
			   volunteered = true;
			}
			});

		return volunteered;
	}
	
	function hasSelectedTrainginDate()
	{
		var selectedTrainingDate = false;
		
		$$('input[type="checkbox"][id^="TrainingLocation"]').each(function(ele){
			if( $(ele).checked )
			{
			   selectedTrainingDate = true;
			}
			});

		return selectedTrainingDate;
	}
	
	function countTrainingDatesSelected()
	{
		var trainingDateCount = 0;
		
		$$('input[type="checkbox"][id^="TrainingLocation"]').each(function(ele){
			if( $(ele).checked )
			{
			   trainingDateCount++
			}
			});

		return trainingDateCount;
	}
	
	
	function validDatesSelected()
	{
		var index = 1;
		var volunteerDates = new Array();
		
		for ( index = 1; index <= 99; index++ ) {
			
			if ( ! document.getElementById('VolunteerLocation' + index) ) {
				continue;
			}
			
			if ( document.getElementById('VolunteerLocation' + index).checked ) {
				
				var elements = getElementsByClass('date_selector', document.getElementById('VolunteerLocation' + index).parentNode);
				
				if ( elements ) {
					// Iterate through dates for the location
					for ( i = 0; i < elements.length; i++ ) {
						
						if ( elements[i].checked ) {
							
							// Check if date has been picked.
							for ( j = 0; j < volunteerDates.length; j++ ) {
								
								if ( elements[i].alt == volunteerDates[j] ) {
									alert("You have registered for multiple locations on " + volunteerDates[j]);
									return false;
								}
							}
							
							volunteerDates.push(elements[i].alt);
						}
					}
				}
			}
			
		}
		
		if ( volunteerDates.length < 2 ) {
			alert("Please select at least 2 dates you are able to volunteer.");
			return false;
		}
		
		return true;
	}
}

function validateForm()
{
	if ( validateStep(1) && validateStep(2) && validateStep(3) ) {
		return true;	
	}
	
	return false;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
