function dependingTextBoxSet(obj, enable) {
	if (enable) {
		// enable
		obj.attr('disabled','');
		obj.removeClass('disabled');
	} else {
		// disable
		obj.attr('disabled','disabled');
		obj.addClass('disabled');
	}
}

function initPradoDependingTextBox(triggeredID, triggeringID) {
	// get jQuery objects
	var triggered = jQuery('#'+triggeredID);
	var triggering = jQuery('#'+triggeringID);

	// declare trigger function in scope to access jQuery objects
	var triggerFunction = function(){
		dependingTextBoxSet(triggered, triggering[0].checked);
	}

	// prepare a variable to register onto
	var eventSenders = triggering;

	// if triggering element is a radio button we need to register a handler to
	// all input elements of the same name (the whole button group)
	if (triggering.attr('type') == 'radio') {
		var name = triggering.attr('name');

		// escape name for use as a jQuery selector
		name = name.replace(/([#;&,.+*~':"!^$\[\]()=>|\/])/g, '\\$1');

		// get input elements of the same button group from jQuery
		eventSenders = jQuery('input[name='+name+']');
	}

	// register handler
	eventSenders.click(triggerFunction);

	// initialize correct state by firing the trigger one time
	triggerFunction();
}

function initPradoJPTPepTrack() {
	var options = new Array('trial', 'discovery', 'research', 'fast');

	var clickHandler = function(source){
		var selectedOption = '';

		if (typeof source == 'undefined') {
			// if source is not set this should be the initialization
			// we want to disable everything until user selects an option
			// in order to do so, we go into any option button without having any
			// selectedOption to match

			// NOTE: needs to be changed if multiple blocks should be used in future
			
			// try to get checked box because we have to restore the old view
			// after a refresh/post
			source = jQuery('fieldset.peptrackoptionblock div.option input[checked]');
			if (source.length == 1) {
				// option is selected
				// => source is already set, get label caption
				selectedOption = source.next('label').text().toLowerCase();
			} else {
				// nothing has been selected yet or ambigous selection has been
				// submitted (does that even work? we check nonetheless)
				// => pick any input item and set as source, leave label empty
				//    to match nothing below
				source = jQuery('fieldset.peptrackoptionblock div.option input:first');
			}
		} else {
			// if source is set we are being called as a user-triggered event
			selectedOption = jQuery(this).next('label').text().toLowerCase();
			source = jQuery(this);
		}

		// get the parent fieldset or stop
		var fieldset = jQuery(source).parents('fieldset');
		if (fieldset.length < 1) {
			return true;
		}
		fieldset = fieldset[0];

		// iterate over all options to enable/disable their depending input elements
		for (var i=0; i<options.length; i++) {
			var option = options[i];

			if (option == selectedOption) {
				// enable input elements for the user-selected PepTrack option
				jQuery('div.only'+option+' input', fieldset).attr('disabled', '');
				jQuery('div.only'+option+' input', fieldset).removeClass('disabled');
				jQuery('div.only'+option+' label', fieldset).removeClass('disabled');
				
				// additionally we should auto-select input elements if they
				// are the only choice for that PepTrack option
				var blocks = jQuery('div.only'+option, fieldset);
				for (var j=0; j<blocks.length; j++) {
					var elems = jQuery('input', blocks[j]);
					if (elems.length == 1) {
						elems.attr('checked', 'checked');
					}
				}
			} else {
				// disable input elements for all other PepTrack options
				jQuery('div.only'+option+' input', fieldset).attr('disabled', 'disabled');
				jQuery('div.only'+option+' input', fieldset).addClass('disabled');
				jQuery('div.only'+option+' label', fieldset).addClass('disabled');
			}
		}

		return true;
	};

	jQuery('fieldset.peptrackoptionblock div.option input').click(clickHandler);
	clickHandler();
}

function initSequenceCommentLinks() {
	// if we will be get a sequence file upload we don't need the sequence fields
	// so we simply add a text to those sequence fields
   jQuery('a.sequencecomment').click(function(){
	   var commentText = 'Please see attached file (follow the instructions to upload your file(s) after submitting this request).';
	   
	   // textarea sequence field:
	   // add comment to next available textarea
	   jQuery(this).parents('fieldset.sequences').find('textarea').append('\n'+commentText+'\n')
	   
	   // numbered sequences:
	   // add a comment to the first empty text field
	   var done = false;
	   jQuery(this).parents('fieldset.numberedsequences').find('.numberedsequence input').each(function(){
		   if (!done) {
			   var jThis = jQuery(this);
			   if (jThis.val() == '') {
				   jThis.val(commentText);
				   done = true;
			   }
		   }
	   });
	   
	   // select checkbox for file upload
	   var objs = jQuery('fieldset.fileupload .checkbox input');
	   if (objs.length > 0) {
		   var obj = objs[0];
		   obj.checked = true;
	   }
	   
	   return false;
   });
}

function initPradoFormsMain() {
	// upload comment links for sequence fields
	initSequenceCommentLinks();
	
	// special option switches for PepTrack page
	initPradoJPTPepTrack();
}

jQuery(document).ready(initPradoFormsMain);

