var urlSuffix = '?';
var formsArray = new Array();
var h1buttons = new Array();
var formActions = new Array();
var jsFunctionsRegistry = new Array();

$(document).ready(function() {
		
	// find all the forms on the page and initialise all the 
	// links and buttons within the form
	var forms = document.getElementsByTagName("form");
	
 	for (i=0; i< forms.length; i++) {
 		
 	 	var thisFormID = '#' + $(forms[i]).attr('id');
		initialiseForm(thisFormID);
 	}
 	
    $( "form#login_form h1" ).bind( "click", slideForm );
    $( "form#login_form .buttonSet" ).bind( "click", slideForm );

  });

  function slideForm() {
  
  	 if($( "#loginpanel" ).css( 'marginTop' ) == "-75px") {
  	 	$( "#loginpanel" ).animate( { marginTop:"-161px"}, 500, function() {
  	 		$( ".formAlerts" ).fadeIn(300);	 
  	 	} );
  	 	
  	 } else if($( "#loginpanel" ).css( 'marginTop' ) == "-161px") {
  	 	if($( ".formAlerts" ).length > 0) $( ".formAlerts" ).fadeOut(300, function() {
  	 		$( "#loginpanel" ).animate( { marginTop:"-75px"}, 500 );
  	 	} );
  	 	else $( "#loginpanel" ).animate( { marginTop:"-75px"}, 500 );
  	 	
  	 }

  }

function initialiseForm(thisFormID) {

		for (var i in jsFunctionsRegistry) {
			eval(jsFunctionsRegistry[i] + "('" + thisFormID.substring(1) + "')");
			// jsFunctionsRegistry[i](thisFormID);
		}
						
		// if there is a function defined on this page called initialiseFormAdditional, run it now
		if (typeof initialiseFormAdditional == 'function') {
			initialiseFormAdditional(thisFormID);
		}
		
		hideDescriptions(thisFormID);
		
		formsArray[thisFormID] = new Array();
		formsArray[thisFormID]['targetUrl'] = $(thisFormID).attr('action');
		
		var fckForm = false;
		if (($(thisFormID).find('.fckArea').length > 0) || ($(thisFormID).find('.fckSimple').length > 0)) fckForm = true;	
				
		var formActions;
		inFormActions = new Array();
					
		// initialise the date-pickers
		if ($('#' + thisFormID + ' .date-pick').length > 0) {
		
			if ($('' + thisFormID + '_startDate').length > 0) {
				startDateValue = $('' + thisFormID + '_startDate').attr('value');
			} else {
				startDateValue = '01/01/1996';
			}
	
			if ($('' + thisFormID + '_endDate').length > 0) {
				endDateValue = $('' + thisFormID + '_endDate').attr('value');
			} else {
				endDateValue = '01/01/3000';
			}
			
			$('#' + thisFormID + ' .date-pick').datePicker({
				clickInput:	true, 
				startDate:	startDateValue,
				endDate:	endDateValue
			});
			
		}
		
		inFormActions['updateNav'] = $('#' + thisFormID + ' .setNavigation');
		inFormActions['updateNav'].bind('change', function() { 
			$('' + thisFormID + '').submit();
		});
		
		inFormActions['textFilterClear']  = $('#' + thisFormID + ' .textFilterClear');
		$(inFormActions['textFilterClear']).bind('click',  function() {
			$(this).prev().val('');
			$(this).parents('form').submit();
		});
		
		inFormActions['cancelReturn'] = $('#' + thisFormID + ' .cancelReturn');
		inFormActions['cancelReturn'].bind('click', function() {
			
			history.go(-1);
		});
		
		// initialise the star pickers
		if ($('.starContainer').length > 0) {
			inFormActions['stars'] = $('input:radio.star').rating();
		}
		
		inFormActions['linkDelete'] = $('#' + thisFormID + ' .linkDelete');
		inFormActions['linkDelete'].bind('click', function() { 
			if (confirm("Are you sure you want to delete this item?\n\nThis cannot be undone.")) return true;
			else return false;
			
		});
		
		// initialise limited content text areas
		inFormActions['textareaLimited'] = $('#' + thisFormID + ' .textareaLimited');
		$(inFormActions['textareaLimited']).bind('keyup',  function() {
		
			var maxChars = $(this).next().attr('value');
			var currentChars = $(this).attr('value').length;
			var charRemaining = (((maxChars  - currentChars) == 1) ? '1 character remaining' : (maxChars  - currentChars) + ' characters remaining');
			
			if (currentChars > maxChars) {
				var newValue = $(this).attr('value').substr(0, maxChars);
				$(this).attr('value', newValue);
			} 
			
			$(this).siblings('.formNotice').attr('value', charRemaining);
			
		});
/*

		var shortPass = 'Too short'
		var badPass = 'Bad'
		var goodPass = 'Good'
		var strongPass = 'Strong'

*/
		inFormActions['passwordStrength'] = $(thisFormID).find('.itemRow .passwordStrength').prev();
		inFormActions['passwordStrength'].bind('keyup', function() {
			strength = passwordStrength(inFormActions['passwordStrength'].val());
			stengthField = $(this).next();
			stengthField.html(strength);
			switch(strength) {
				case('Strong'):
					stengthField.css('color', '#009900');
					break;
				case('Good'):
					stengthField.css('color', '#FF9900');
					break;
				case('Bad'):
					stengthField.css('color', '#FF0000');
					break;
				default:
					stengthField.css('color', '');
			}	
		});		
		
		// initialise extended select options
		inFormActions['selectExtended'] = $(thisFormID).find('.itemRow .selectExtended');
		$(inFormActions['selectExtended']).change( function(event) {
		
			selectBoxValue = document.getElementById($(this).attr('id')).value;
			textArea = $(this).next();
			textInput = $(this).next().children(0);
			
			if (selectBoxValue == '|-----| Add New |-----|') {
				textInput.attr('value', '');
				textArea.attr('style', 'display: inline');
				$(this).attr('style', 'display: none');
			} else {
				textInput.attr('value', selectBoxValue);
			}
			
		});
		
		inFormActions['selectExtendedCancel'] = $(thisFormID).find('.itemRow .selectExtendedCancel');
		$(inFormActions['selectExtendedCancel']).bind('click', function(event) {
		
			selectBox = ($(this).parent().prev().attr('id'));
			document.getElementById(selectBox).value = $('#' + selectBox).children('option:first-child').attr('value');
			
			$(this).prev().attr('value', '');
	    		$(this).parent().prev().attr('style', 'display: inline');
	    		$(this).parent().attr('style', 'display: none');
    
		});
		
		$(thisFormID + ' .checkboxGroup input').bind('click', function() {
		
			var boxesInGroup = $(this).parents('.checkboxGroup').find('input');
			var boxClicked = this;
			
			if (boxClicked.checked == true) {
				if (boxClicked.value == '') {
					
					boxesInGroup.each( function() {
						this.checked = false;
					}); 
					
					boxClicked.checked = true;
					
				} else {
					boxesInGroup.each( function() {
						if (this.value == '') this.checked = false;
					});
				}
			} else {
					var checkedCount = 0;
					boxesInGroup.each( function() {
						if (this.checked == true) checkedCount++;
					})
					if (checkedCount == 0) boxClicked.checked = true;
			}
					
		});
		
		
		
		// if there are any fck fields on this form complete them now
		if (fckForm == true) {
					
				jQuery(function(){
					jQuery.fck.config = {path: '/fckeditor/' };
					jQuery('.fckSimple').fck({					
						config: {
							Width: '100%', 
							Height: '200', 
							CustomConfigurationsPath: '/fckeditor_configs/simpleconfig.js'
							}
					});
					jQuery('.fckArea').fck({
						config: {
							Width: '100%', 
							Height: '300', 
							CustomConfigurationsPath: '/fckeditor_configs/config.js'
							}
					});
				});
		}
		
}

/*
jsFunctionsRegistry['hideDescriptions'] = 'hideDescriptions';
*/

function hideDescriptions(formID) {
	$('#' + formID + ' div.formDescription').addClass('revealed');
	$('#' + formID + ' div.formDescription').prev().before('<a class="revealDescription" href="javascript:;">i</a>');
	
	$('#' + formID + ' a.revealDescription').unbind('mouseover');
	$('#' + formID + ' a.revealDescription').unbind('mouseout');
	
	$('#' + formID + ' a.revealDescription').each(function() {
		$('#' + formID + ' a.revealDescription').click(function(i) {
			$(this).next().next('div').slideToggle('slow');
		});
	});
	
}

