var trackertool = {

	save: function() {
		if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
			
			// IE6 needs window.setTimeout in order to submit form succesfully
			
			window.setTimeout( function() { $('#tracker_form')[0].submit()}, 100);
		
		} else {
			$('#tracker_form')[0].submit();
		}
	}, 
	
	previous: function() {
		var p = parseInt($('#f_page').val());
		if (p > 1) {
			p --;
		}
		$('#f_page').val(p);
		this.save();
	},
	
	next: function() {
		var p = parseInt($('#f_page').val());
		if (p < page_settings.no_of_pages) {
			p ++;
		}
		if (p == '10') {
			var f = document.getElementById('tracker_form');
			if (f.sendReportTo.value == '' || f.contactNumber.value == '' || f.email.value == '' || f.postalAddress.value == '' || f.approved.value == '' || f.approvedBy.value == '') {
				//errorContainer('You must fill in all required fields.');
				//location.href = '#errorContainer';
				alert('You must fill in all required fields.');
				return false;
			} else {
				$('#f_page').val(p);
				this.save();
			}
		} else {
			$('#f_page').val(p);
			this.save();
		}
	},
	
	login: function() {
		if (doOnSubmit()) {
			$('#login_form')[0].submit();
		}
	},
	
	final_submission: function() {
		if (parseInt($('#finish_confirm').val()) != 1) {
			alert("Please confirm you are ready to submit the Tracker Tool by clicking " + 
				"'Yes' above the 'Finish' button, then click 'Finish' again. You will not " +
				"be able to make changes once you have completed this final step.");
			return;
		}
		$('#f_completed').val(1);
		this.save();
	},
	
	upload: {
	
		toggle_panel: function() {
			// open the upload panel...
			if ($('#upload_panel').hasClass('opened')) {
				$('#upload_panel').removeClass('opened').hide();
			} else {
				$('#upload_panel').addClass('opened').show();
			}
		},
		
		on_complete: function() {
			// refresh the upload list, via ajax...
			this.refresh_file_list();
			// close the panel...
			this.toggle_panel();
		},
		
		refresh_file_list: function() {
			var url = '/logic/includes/members_trackertool_upload_list.lasso';
			var params = {
				'-session': 'tracker:' + page_settings.session_id,
				'directory_name': 'pledge-' + page_settings.pledge
			};
			var onload = function() {
			
			};
			$('#upload_files').load(url, params, onload);

		},
		
		remove: function(filepath, filename) {
			if (confirm("Are you sure you want to remove the file '" + filename + "'? This action " + 
				"cannot be undone. Click 'OK' to remove the file or 'Cancel' to leave it on " + 
				"the server")) {
				var url = '/logic/includes/members_trackertool_upload_remove.lasso';
				var params = {
					'-session': 'tracker:' + page_settings.session_id,
					'filepath': filepath
				};
				var onload = function() {
					trackertool.upload.refresh_file_list();
				};
				$('#upload_files').load(url, params, onload);
			}
		}
	
	}

}

	
$(document).ready(function() {
	
	if ($('#f_page') && $('#f_page').length > 0) {
		
		// add button actions.
		$('.trackertool_btn_save').attr('href', 'javascript:void(0)').click(function() {trackertool.save();});
		$('.trackertool_btn_previous').click(function() {trackertool.previous();});
		$('.trackertool_btn_next').click(function() {trackertool.next();});
		
		// hide inactive buttons
		trackertool.page_number = parseInt($('#f_page').val());
		if (trackertool.page_number == 1) {
			$('.trackertool_btn_save_outer').hide();
			$('.trackertool_btn_previous_outer').hide();
			$('.trackertool_btn_preceed_outer').show();
		} else if (trackertool.page_number == page_settings.no_of_pages) {
			$('.trackertool_btn_save_outer').hide();
			$('.trackertool_btn_next_outer').hide();
			$('.trackertool_btn_proceed_outer').show();
		}
		
	}
	
	if ($('#login_form_submit_btn') && $('#login_form_submit_btn').length > 0) {
		$('#login_form_submit_btn').click(function() {trackertool.login();});		
	}
	
	if ($('.finish_btn') && $('.finish_btn').length > 0) {
		$('.finish_btn').click(function() {trackertool.final_submission();});		
	}

	if ($('#upload_options') && $('#upload_options').length > 0) {
		$('#upload_documents_link').click(function() {trackertool.upload.toggle_panel();});
		trackertool.upload.refresh_file_list();
	}

});



var radio_image_off = '<img src="/site/images/radio_off_print.png" />';
var radio_image_on = '<img src="/site/images/radio_on_print.png" />';

function radioButtons(which) {
	document.write('<input type="hidden" name="' + which + '" id="' + which + '" value="" />');
	$('.' + which)
		.each(function () {
			var thisValue = $(this).attr('value');
			if ($(this).attr('checked')) {
				var checked = 'active ';
				var checked_img = radio_image_on;
				$('#' + which).attr('value',thisValue);
			} else {
				var checked = '';
				var checked_img = radio_image_off;
			}
			$(this).replaceWith('<div class="' + which + ' answer' + thisValue + ' ' + checked + 'radio" rel="' + thisValue + '">' + checked_img + '</div>');
			
		})
	;
	$('.' + which)
		.click(function () {
			$('.' + which).removeClass('active');
			$('.' + which).html(radio_image_off);
			$(this).addClass('active');
			$(this).html(radio_image_on);
			$('#' + which).attr('value', $(this).attr('rel'));
		})
	;
	if ($('#' + which).attr('value') != '') {
		$('.' + which).removeClass('active');
		$('.' + which + '.answer' + $('#' + which + '').attr('value')).addClass('active');
	}
}

function radioButtonsPrint() {
	$('.printVersion .radio').html(radio_image_off);
	$('.printVersion .radio.active').html(radio_image_on);
}

function fieldPrint() {
	$('#questionnaireForm textarea')
		.each(function () {
			$(this).after('<div class="fieldReplace">' + $(this).html() + '&nbsp;</div>');
		})
	;
	$('#questionnaireForm input')
		.each(function () {
			if ($(this).attr('type') == 'text') {
				$(this).after('<div class="fieldReplace">' + $(this).attr('value') + '&nbsp;</div>');
			}
		})
	;
}

errorOccured = 'no';
function errorContainer(which) {
	var errorContainer = document.getElementById('errorContainer');
	if (errorOccured == "no") {
		errorContainer.innerHTML = which;
		$("#errorContainer").slideDown('normal');
		errorOccured = 'yes';
	} else {
		errorContainer.innerHTML = which;
		$("#errorContainer").effect("pulsate", { times:3 }, 500);
	}
}

function successContainer() {
	if (document.getElementById('successContainer')) {
		setTimeout( function()
      {
         $('#successContainer').slideUp(800);
      }, 10000);

	}
}

$(document).ready(function() {
	successContainer();
	radioButtonsPrint();
	fieldPrint();
});
