/*
	formbuilder-init.js - modified from NHRTP 'courses.js'
	
	Handles the 'More Information' accordion on course pages
	gets the datepicker reader
*/

$(document).ready(function(){
	$(".datepicker").datepicker();
	
	/* --- expandable fieldsets --- */
	$('.expandable div').hide();
	$('.expandable legend').addClass('handle');
	$('.expandable legend span').html('(click to expand)');
	
	$('.expandable legend').click(function() {										   
		$(this).parent().find('div:first').slideToggle().toggleClass('open');
		if( $(this).parent().find('div:first').hasClass('open') ) {
			$(this).find('span').html('(click to hide)');
		} else {
			$(this).find('span').html('(click to expand)');
		}
	});
	
	/* --- hide optional --- */
	$('#hide-fields').click(function() {										   
		$('.optional').toggle();
		if( $(this).html() == 'Display Optional Fields?' ){
		//if( $('.optional:first').is(':visible') ){
			$(this).html('Remove Optional Fields?');
		} else {
			$(this).html('Display Optional Fields?');
		}
	});
	
	/* --- for popup summary class --- */
	$('input,select,textarea').focus(function() {										   
		if( $(this).parent().find('div').hasClass('summary') ) {
			$(this).parent().addClass('summarized');
		}
	});
	$('input,select,textarea').blur(function() {										   
		if( $(this).parent().find('div').hasClass('summary') ) {
			$(this).parent().removeClass('summarized');
		}
	});
	
});

	/**
	 * To show or hide dependent elements
	 */
	function showhidediv(el,val,match) {
		e = document.getElementById(el);
		if( val == match ){
		//if (e.style.display == "none") {
			e.style.display = "block";
		} else {
			e.style.display = "none";
		}
	}

/* Auto Tabbing */
/* Original:  Cyanide_7 (leo7278@hotmail.com)
	Web Site:  http://members.xoom.com/cyanide_7

	This script and many more are available free online at
	The JavaScript Source!! http://javascript.internet.com */

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
		if(arr[index] == ele)
			found = true;
		else
			index++;
		return found;
	}
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)index = i;
			else i++;
		return index;
	}
	return true;
}
