// app class 
// version 1.0

// class used with online applications for ajax saves

var app = 
{
	section: null,
	
	save: function(jqObject)
	{
		// method will look at the input element's name and value and make an ajax save to the database
		// note that the field name needs to be table-field name
		var field = jqObject.attr('name');
		var value = jqObject.val();
		
		// extra field to tell to php to encrypt the field
		var extraField = (jqObject.attr('id') == 'email_required') ? '&encrypt=1' : '';
		
		// init ajax call
		$.ajax(
		{
			type: 'post',
			dataType: 'json',
	   		url: 'webservice/application.php',
	   		data: 'action=save&field='+encodeURIComponent(field)+'&value='+encodeURIComponent(value)+extraField,
	   		success: function(jsonObject)
			{		
				if(jsonObject.save.status != 'success')
				{
					//blendDebugger.log(jsonObject);
				}
	   		},
			
			error: function(e1, e2, e3)
			{
				//blendDebugger.log(e1);
				//blendDebugger.log(e2);
				//blendDebugger.log(e3);
			}
	 	});
		// end ajax		
	},
	
	toggleGuardianValidation: function(total)
	{
		// array of fields NOT to toggle
		var omitFields = ['guardian2Addr2'];
				
		if(total == 1)
		{
			// make second set of guardian information as optional
			$('#guardian2 > li').each( function(i)
			{
				var label = $(this).children('div').children('label');
				var input = $(this).children('div').children('input');
				
				// if id is not in the omit fields
				if($.inArray(input.attr('id'), omitFields) == -1)
				{
					// remove _required (which triggers validation)
					input.attr('id', input.attr('id').replace(/_required/g,''));
					label.attr('for', label.attr('for').replace(/_required/g,''));
					
					// add option to label
					label.html(label.html()+' <em>optional</em>');					
				}				
			});					
		}
		else
		{
			// make second set of guardian information required
			$('#guardian2 > li').each( function(i)
			{
				var label = $(this).children('div').children('label');
				var input = $(this).children('div').children('input');
				
				// if id is not in the omit fields
				if($.inArray(input.attr('id'), omitFields) == -1)
				{
					// add_required (which triggers validation)
					if(!input.attr('id').match('_required'))
					{
						input.attr('id', input.attr('id')+'_required');
						label.attr('for', label.attr('for')+'_required');
					}
					
					// remove optional from label
					var html = label.html().replace(/<[eE][mM]>optional<\/[eE][mM]>/g,'');
					label.html(html);								
				}				
			});
		}
	},
	
	toggleHsWork: function(work)
	{				
		if(work == 'Y')
		{
			// change label for attribute for validation (needs _required)
			var empLabel = $('#hsEmployerRow > div > label');
			var hoursLabel = $('#hsHoursRow > div > label');
			empLabel.attr('for', empLabel.attr('for')+'_required');
			hoursLabel.attr('for', hoursLabel.attr('for')+'_required');
			
			// change input id attribute for validation (needs _required)
			var empInput = $('#hsEmployerRow > div > input');
			var hoursInput = $('#hsHoursRow > div > input');
			empInput.attr('id', empInput.attr('id')+'_required');
			hoursInput.attr('id', hoursInput.attr('id')+'_required');
			
			// show rows
			$('#hsEmployerRow').show();
			$('#hsHoursRow').show();			
		}
		else
		{
			// change label for attribute for validation (remove _required)
			var empLabel = $('#hsEmployerRow > div > label');
			var hoursLabel = $('#hsHoursRow > div > label');			
			empLabel.attr('for', empLabel.attr('for').replace(/_required/g,''));
			hoursLabel.attr('for', hoursLabel.attr('for').replace(/_required/g,''));
			
			// change input id attribute for validation (remove _required)
			var empInput = $('#hsEmployerRow > div > input');
			var hoursInput = $('#hsHoursRow > div > input');
			empInput.attr('id', empInput.attr('id').replace(/_required/g,''));
			hoursInput.attr('id', hoursInput.attr('id').replace(/_required/g,''));
			
			// show rows
			$('#hsEmployerRow').hide();
			$('#hsHoursRow').hide();
		}
	},
	
	toggleCollegeWork: function(work)
	{				
		if(work == 'Y')
		{
			// change label for attribute for validation (needs _required)
			var empLabel = $('#collegeEmployerRow > div > label');
			var hoursLabel = $('#collegeHoursRow > div > label');
			empLabel.attr('for', empLabel.attr('for')+'_required');
			hoursLabel.attr('for', hoursLabel.attr('for')+'_required');
			
			// change input id attribute for validation (needs _required)
			var empInput = $('#collegeEmployerRow > div > input');
			var hoursInput = $('#collegeHoursRow > div > input');
			empInput.attr('id', empInput.attr('id')+'_required');
			hoursInput.attr('id', hoursInput.attr('id')+'_required');
			
			// hide rows
			$('#collegeEmployerRow').show();
			$('#collegeHoursRow').show();			
		}
		else
		{
			// change label for attribute for validation (remove _required)
			var empLabel = $('#collegeEmployerRow > div > label');
			var hoursLabel = $('#collegeHoursRow > div > label');			
			empLabel.attr('for', empLabel.attr('for').replace(/_required/g,''));
			hoursLabel.attr('for', hoursLabel.attr('for').replace(/_required/g,''));
			
			// change input id attribute for validation (remove _required)
			var empInput = $('#collegeEmployerRow > div > input');
			var hoursInput = $('#collegeHoursRow > div > input');
			empInput.attr('id', empInput.attr('id').replace(/_required/g,''));
			hoursInput.attr('id', hoursInput.attr('id').replace(/_required/g,''));
			
			// hide rows
			$('#collegeEmployerRow').hide();
			$('#collegeHoursRow').hide();
		}
	},
	
	toggleStudentTranscript: function(choice)
	{				
		if(choice == 'upload')
		{
			// change label for attribute for validation (needs _required)
			var transcriptLabel = $('#studentTranscriptRow > div > label');
			transcriptLabel.attr('for', transcriptLabel.attr('for')+'_required');
			
			// change input id attribute for validation (needs _required)
			var transcriptInput = $('#studentTranscriptRow > div > input');
			transcriptInput.attr('id', transcriptInput.attr('id')+'_required');
			
			// show rows
			$('#studentTranscriptRow').show();			
		}
		else
		{
			// change label for attribute for validation (remove _required)
			var transcriptLabel = $('#studentTranscriptRow > div > label');
			transcriptLabel.attr('for', transcriptLabel.attr('for').replace(/_required/g,''));
			
			// change input id attribute for validation (remove _required)
			var transcriptInput = $('#studentTranscriptRow > div > input');
			transcriptInput.attr('id', transcriptInput.attr('id').replace(/_required/g,''));
			
			// hide rows
			$('#studentTranscriptRow').hide();
		}
	},
	
	toggleCounselorTranscript: function(choice)
	{				
		if(choice == 'upload')
		{
			// change label for attribute for validation (needs _required)
			var transcriptLabel = $('#counselorTranscriptRow > div > label');
			transcriptLabel.attr('for', transcriptLabel.attr('for')+'_required');
			
			// change input id attribute for validation (needs _required)
			var transcriptInput = $('#counselorTranscriptRow > div > input');
			transcriptInput.attr('id', transcriptInput.attr('id')+'_required');
			
			// show rows
			$('#counselorTranscriptRow').show();			
		}
		else
		{
			// change label for attribute for validation (remove _required)
			var transcriptLabel = $('#counselorTranscriptRow > div > label');
			transcriptLabel.attr('for', transcriptLabel.attr('for').replace(/_required/g,''));
			
			// change input id attribute for validation (remove _required)
			var transcriptInput = $('#counselorTranscriptRow > div > input');
			transcriptInput.attr('id', transcriptInput.attr('id').replace(/_required/g,''));
			
			// hide rows
			$('#counselorTranscriptRow').hide();
		}
	},
	
	init: function()
	{		
		// add events to all input text, textarea, and combobox
		$('input:text').bind('blur', function() { app.save($(this)); });
		$('textarea').bind('blur', function() { app.save($(this)); });
		$('select').bind('change', function() { app.save($(this)); });
		
		// get current table
		this.section = $('#section').val();
		
		/* step 1 */		
		// add special conditional events for step1 that will toggle guardian validation
		var guardianField = $('#totalGuardian_required');
		guardianField.bind('change', function() { app.toggleGuardianValidation($(this).val()); });
		
		// force same guardian validation toggle to run on page load in additional to change event
		if($('#totalGuardian_required').is('*'))
		{
			this.toggleGuardianValidation(guardianField.val());
		}
		
		/* step 2 */
		// add special conditional events for step2 that will show/hide highschool work fields
		var hsWork = $('#hsWork_required');
		hsWork.bind('change', function() { app.toggleHsWork($(this).val()); });
		
		// force same highschool toggle to run on page load in additional to change event
		if($('#hsEmployerRow').is('*'))
		{			
			this.toggleHsWork(hsWork.val());
		}
		
		// add special conditional events for step2 that will show/hide college work fields
		var collegeWork = $('#collegeWork_required');
		collegeWork.bind('change', function() { app.toggleCollegeWork($(this).val()); });
		
		// force same college toggle to run on page load in additional to change event
		if($('#collegeEmployerRow').is('*'))
		{			
			this.toggleCollegeWork(collegeWork.val());
		}
		
		// add special conditional events for step2 that will show/hide transcript fields
		var studentTranscript = $('#studentTranscript_required');
		studentTranscript.bind('change', function() { app.toggleStudentTranscript($(this).val()); });
		
		// force same transcript toggle to run on page load in additional to change event
		if($('#studentTranscript_required').is('*'))
		{			
			this.toggleStudentTranscript(studentTranscript.val());
		}
		
		/* counselor step */
		// add special conditional events for counselor step that will show/hide transcript fields
		var counselorTranscript = $('#counselorTranscript_required');
		counselorTranscript.bind('change', function() { app.toggleCounselorTranscript($(this).val()); });

		// force same transcript toggle to run on page load in additional to change event
		if($('#counselorTranscript_required').is('*'))
		{			
			this.toggleCounselorTranscript(counselorTranscript.val());
		}
	}
}

// add to document after dom is loaded
$(document).ready(function() { app.init(); });