$(document).ready(function(){

  // Hide blocks when DOM is ready
  $('#block2').hide();
  $('#block3').hide();
  
  // Set up multi-shift booking form
  $('#field65').parent().parent().before('<tr><td colspan="2"><p>Fill out the details below and Click <em>Add Shift</em> to automatically add shifts to the Shift Times box.</p><p class="shift-form"><input type="text" name="shift-date" id="shift-date" title="Date" /><input type="text" name="shift-start" id="shift-start" title="Start Time" /><input type="text" name="shift-finish" id="shift-finish" title="Finish Time" /><br /><input type="text" name="shift-desi" id="shift-desi" title="Staff Designation" /><input type="text" name="shift-ward" id="shift-ward" title="Ward/Area" /><input type="text" name="shift-req" id="shift-req" title="Staff Requested" /><input type="submit" value="Add Shift" name="butAddShift" id="butAddShift" /></p></td></tr>');
  
  // Set up nurse availability form
  $('#field70').parent().parent().before('<tr><td colspan="2"><p>Fill out the details below and Click <em>Add Availability</em> to automatically add your preferences to the Availability box.</p><p class="shift-form"><input type="text" name="shift-date" id="shift-date" title="Date" /> Morning:<input type="checkbox" name="shifts" value=" Morning" class="checkbox" /> Afternoon:<input type="checkbox" name="shifts" value=" Afternoon" class="checkbox" /> Night:<input type="checkbox" name="shifts" value=" Night" class="checkbox" /><input type="submit" value="Add Availability" name="butAddAvail" id="butAddAvail" /></p></td></tr>');

	$('#field65').attr('cols', '100');
	$('#field65').attr('wrap','off');

	// Date Picker
	$('#shift-date').datePicker({clickInput:true, createButton:false});

  // Time Picker
  $('#shift-start, #shift-finish').timePicker( { step:15, show24Hours:false });

  // Make certain links open in new window
  $('a.new-window').click(function() {
    window.open(this.href);
    return false;
  });

  // Show/hide 2nd block 
  $('a#b2toggle').click(function() {
    $('#block2').toggle(400);
    return false;
  });
  
  // Show/hide 3rd block 
  $('a#b3toggle').click(function() {
    $('#block3').toggle(400);
    return false;
  });
  
  // Handle Block 2 forwarding to appropriate URLs
  $('#register-submit').click(function() {
    if( ($('#register-region').val() != 0) && ($('#register-category').val() != 0) )
			window.open( $('#register-region').val() + $('#register-category').val() );
		else
			alert('Please select your category and region.');
    return false;
  });
  
   // Handle Block 3 forwarding to appropriate URLs
  $('#job-search-submit').click(function() {
    if($('#job-search-region').val() != 0)
			window.open($('#job-search-region').val());
		else
			alert('Please select your region.');
    return false;
  });
  
  // Put input labels in text box itself
	$('p.shift-form input').hint();
	
	var shiftDetails = '';
	
	// Add Shift Routine
	$('#butAddShift').click(function() {
		shiftDetails = $('#field65').val();
		shiftDetails += $('#shift-date').val() + ': ' + $('#shift-start').val() + ' - ' + $('#shift-finish').val() + ', ' + $('#shift-desi').val() + ', ' + $('#shift-ward').val() + ', ' + $('#shift-req').val() + "\n";
		$('#field65').val(shiftDetails);
		return false;
	});
	
	// Add Availability Routine
	$('#butAddAvail').click(function() {
		shiftDetails = $('#field70').val();
		shiftDetails += $('#shift-date').val();
		$('input:checkbox:checked[name=shifts]').each(function() {
			shiftDetails += $(this).val();
		});
		shiftDetails += "\n";
		$('#field70').val(shiftDetails);
		return false;
	});
   
 });