

$(document).ready( function(){
							
							
$(confBox).appendTo('body')
$(cfig).appendTo(configurator);

initState();

/* add options to #subject  */
$(firstSelect).change(     
	function() {

		removeValidErr();
		var thmVal = $(firstSelect).val()
		$(secondSelect).attr('disabled',true).addClass('disabled');  // disable the 2nd select
		$(secondSelect).children('option').each( function(){ $(this).remove() }); // first remove all existing options
		
		if (thmVal != '0' ) {
			var subjSel = "ul[@title="+this.value+"]" 
			$('<option value=\"0\" >'+second1stOpt+'<\/option>').appendTo(secondSelect); //add the first standard option
			$(configurator).children(subjSel).children('li').each(   // add all the others options
				function(){
					$('<option value=\"'+this.title+'\" >'+this.innerHTML+'<\/option>').appendTo(secondSelect); 
					elementStatus('disable');
					$(secondSelect).attr('disabled',false).removeClass('disabled'); //enable the second select
					showHide('collapse');		// collapse all optional divs	
			});
		$(secondSelect).attr('disabled',false).removeClass('disabled');  // enable the 2nd select	
	}
	else {  // if no option is selected sends back to initial state
		
		initState();
		
	};

	firstSelXtras();  // call to extra functions that may be necessary
});


$(secondSelect).change( function(){        

	
	removeValidErr();
	var sbjVal = $(secondSelect).val()
	

	if (sbjVal != '0' ) {
		showHide('collapse');
		var formAct = "/servletFormularios?id_formulario="+sbjVal
		$('form').attr("action",formAct);
		showHide('expand');  // expand all necessary optional divs
		elementStatus('enable');
		
		}
	else  {
		showHide('collapse');
		elementStatus('disable');
		$('form').attr("action",'');
	}	
	
	

	
	secondSelXtras();   // call to extra functions that may be necessary
	
});


}); /* END READY (onload) FUNCTION */

function initState() {  // reset to initial state					  
	$(firstSelect).children('option').remove() // first remove all previous options from 1st select
	$(secondSelect).children('option').remove() // first remove all previous options from 2nd select
				
	showHide('collapse');  //collapse all extra options
	$('<option value=\"0\" >'+def1stOpt+'<\/option>').appendTo(firstSelect); //sets default text for 1st select
	$('<option value=\"0\" >'+def2ndOpt+'<\/option>').appendTo(secondSelect);  // sets default text for 2nd select	
	$(secondSelect).attr('disabled',true).addClass('disabled');  // disable the 2nd select
	elementStatus('disable'); // disable all fields
	$(configurator).children('ul').each( function() { // add options to 1st select
			$('<option value=\"'+this.title+'\" >'+this.id+'<\/option>').appendTo(firstSelect);  // create the 1st select options
	});

};
function showHide(action){ //expand/collapse extra elements
	var sbjVal = $(secondSelect).val()

	if ( action == 'collapse') {  // collapse all the elements
		
			$(xtraOpts).children('li').each( function(){
															var divId = this.innerHTML
															var ito = "'"+divId+" input'"
															$(ito).not(exclusion).each( function(){ $(this).val('');  });
															$(divId).slideUp(180);
															
															
															
												  });	
		
	}
	if ( action == 'expand') {   //expand all the necessary elements
			var whichDiv = "li[@title="+sbjVal+"]"
			
			$(xtraOpts).children(whichDiv).each( function(){
															var divId = this.innerHTML
															var inpFields = divId+" input"
															var status =$(divId).css('display')
															$(inpFields).val('')
															$(divId).slideDown(240);
															
															
															
													});				
	}

};


function elementStatus(action) {  // enable/disable form elements except for the first two selects
	if ( action == 'disable') {
		var disab = true ;
		var add= 'disabled';
		var rem= '';
		}
	if ( action == 'enable') {
		var disab = false ;
		var add= '';
		var rem= 'disabled';
		}
	$('form input:text').attr('disabled',disab).addClass(add).removeClass(rem); // disable all input elements
	$('form input:not([@type=text])').attr('disabled',disab);
	$('form textarea').attr('disabled',disab).addClass(add).removeClass(rem); //disables the textareas
	$('form select').not(firstSelect).not(secondSelect).attr('disabled',disab).addClass(add).removeClass(rem); // disable selects except 1st and 2nd
};

function removeValidErr(){ 
/* Removes the validation error  */
	$('div.error').remove('');
	$('form input:text').removeClass('error');
	$('form input:checkbox').removeClass('error');
	$('form select').removeClass('error');	
};
