$(document).ready(function(){

	$('nav#main_nav ul li').hover(function(){
		$(this).find('ul.subnav').css('display', 'block');
	},function(){
		$(this).find('ul.subnav').css('display', 'none');
	});
	
	$('#apply_form input[type=submit]').hide();
	
	var i=0;
	$('#apply_form fieldset').each(function(){
		i = i + 1;
	});
	

	var n=1;
    for(n=1;n<=i;n++){
    	$('div.#steps').append('<span id="step' + n + '"><a>Step ' + n + '</a></span>');
    }
	
	$('#apply_form').append('<a class="next_button">Next</a>');
	$('#apply_form').append('<a class="prev_button">Previous</a>');
	
	$('#steps span').eq(0).addClass('active_step');
	if($('#steps').find('span.active_step') == $('#steps span').length - 1){
		$('.next_button').hide();
	}
	if($('#steps').find('span.active_step').index() == 0){
	    $('.prev_button').hide();
	}
	
	/* $('#steps span.active_step').after($('#apply_form fieldset.active_form legend').text()); */

	$('#steps span').click(function(){
		$('#steps span').removeClass('active_step');
		$(this).addClass('active_step');
		var selected = $(this).index();
		$('#apply_form fieldset').removeClass('active_form');
		$('#apply_form fieldset').eq(selected).addClass('active_form');
		if($('#steps').find('span.active_step').index() == $('#steps span').length - 1){
			$('.next_button').hide();
			$('input[type=submit]').show();
		}else{
			$('input[type=submit]').hide();
			$('.next_button').show();
		}
		
		if($('#steps').find('span.active_step').index() == 0){
			$('.prev_button').hide();
		}else{
			$('.prev_button').show();
		}

	});

	
	$('.next_button').click(function(){
		var current = $('#steps').find('span.active_step').index();
		$('#steps span').eq(current).removeClass('active_step');
		$('#apply_form fieldset').eq(current).removeClass('active_form');
		$('#steps span').eq(current+1).addClass('active_step');
		$('#apply_form fieldset').eq(current+1).addClass('active_form');
		if($('#steps').find('span.active_step').index() == $('#steps span').length - 1){
			$('.next_button').hide();
			$('input[type=submit]').show();
		}else{
			$('.next_button').show();
		}
		
		if($('#steps').find('span.active_step').index() == 0){
			$('.prev_button').hide();
		}else{
			$('.prev_button').show();
		}

	});
	
	$('.prev_button').click(function(){
	    var current = $('#steps').find('span.active_step').index();
		$('#steps span').eq(current).removeClass('active_step');
		$('#apply_form fieldset').eq(current).removeClass('active_form');
		$('#steps span').eq(current-1).addClass('active_step');
		$('#apply_form fieldset').eq(current-1).addClass('active_form');
		if($('#steps').find('span.active_step').index() == $('#steps span').length - 1){
			$('.next_button').hide();
		}else{
			$('input[type=submit]').hide();
			$('.next_button').show();
		}
		
		if($('#steps').find('span.active_step').index() == 0){
			$('.prev_button').hide();
		}else{
			$('.prev_button').show();
		}
	});
	
	var html = '<ul class="school">';
		html += '<li><label>Name of School:</label><input type="text" name="app_school" /></li>';
		html +=	'<li><label>Location:</label><input type="text" name="app_location" /></li>';
		html +=	'<li><label>Enrolled From:</label><input type="text" name="app_enr_from" /></li>';
		html +=	'<li><label>Enrolled To:</label><input type="text" name="app_enr_to" /></li>';
		html +=	'<li><label>Degrees / Credits:</label><input type="text" name="app_degrees" /></li>';
		html +=	'<li><label>Major:</label><input type="text" name="app_major" /></li>';
		html +=	'<li><label>GPA:</label><input type="text" name="app_gpa" /></li>';
		html += '</ul>';
	
	var n = 0;
	
	$('input[name=add_school]').click(function(){
		$('ul.school:last').after(html);
	});
	
	var add_work = '<ul class="work">';
		add_work += '<li><label>Name & Address of Employer:</label><input type="text" name="app_employer" /></li>';
		add_work += '<li><label>Job Title & Major Duties:</label><input type="text" name="app_title" /></li>';
		add_work += '<li><label>Employed From:</label><input type="text" name="app_emp_from" /></li>';
		add_work += '<li><label>Employed To:</label><input type="text" name="app_emp_to" /></li>';
		add_work += '<li><label>Health Field Related?:</label><input type="text" name="app_health_rel" /></li>';
		add_work += '</ul>';
		
	$('input[name=add_work]').click(function(){
		$('ul.work:last').after(add_work);
	});
	
	$('#apply_form').submit(function(event){

        $("#ret_url").val("http://www.sleepschool.com/apply-now/thank-you.php?name="+$("#app_first").val()+"+"+$("#app_last").val());
        
		event.preventDefault();
		var test='';

		$('#apply_form #personal li input').each(function(){
			test += $(this).val() + '\r\n';
		});
		
		
		$('#apply_form #education').find('.school').each(function(){
		$edu =$(this);
		
		test += $edu.find('input[name=app_school]:first').val() + '\r\n\r\n';
		test += $edu.find('input[name=app_location]:first').val() + '\r\n';
		test += $edu.find('input[name=app_enr_from]:first').val() + '\r\n';
		test += $edu.find('input[name=app_enr_to]:first').val() + '\r\n';
		test += $edu.find('input[name=app_degrees]:first').val() + '\r\n';
		test += $edu.find('input[name=app_major]:first').val() + '\r\n';
		test += $edu.find('input[name=app_gpa]:first').val() + '\r\n';
		
		
		});

		
		$('#apply_form #work').find('.work').each(function(){
		$work =$(this);
		
		test += $work.find('input[name=app_employer]:first').val() + '\r\n\r\n';
		test += $work.find('input[name=app_title]:first').val() + '\r\n';
		test += $work.find('input[name=app_emp_from]:first').val() + '\r\n';
		test += $work.find('input[name=app_emp_to]:first').val() + '\r\n';
		test += $work.find('input[name=app_health_rel]:first').val() + '\r\n';
		});
		
		$('#apply_form #statement').find('textarea').each(function(){
		$statement = $(this);
		
		test += $statement.val() + '\r\n';
		
		});
		
		var x = Math.floor(Math.random() * 5683) + 1000;
		var invoice = $('input[name=app_last]').val() +  x;
		
		$('input[type=hidden].invoice').val(invoice);
		
		test += invoice;
		
		
		$('.required').each(function(){
			if($(this).val() == ''){
				$(this).after('<p class="error">This is a required field</p>');
			}
			$(this).keyup(function(){
				if($(this).val() !== ''){
					$(this).siblings('p.error').remove();
				}
			});
		});

			if($('p.error').length <= 0){
				$.ajax({
					type:'POST',
					url:'send.php',
					async:false,
					data:{test: test},
					dataType:"text",
					contentType:'application/x-www-form-urlencoded',
					error: function(){
						alert('error');
						}
				});	
				
			$('#payPalForm').submit();
			
			}else{
				alert('Please Fill Out All Fields');
			}
});
	
});

