/**
* TiM's Regex Tester
* JavaScript code primarily handles hiding/showing 
* form elements based on the regex function selected.
*
* Powered by jQuery - http://www.jquery.com/
**/

var VisElements = [ '#match_all_flags', '#match_flags', '#replace_block', '#trim_long_cells' ];
var HideOrder = [ [0,3], [1,3], [2] ];

//Hide an element from view
function Hide( el )
{
	el.css( 'display', 'none' );
}

//Show an element.
function Show( el )
{
	el.css( 'display', '' );
}

// Loop through and blanket-hide all of the form elements
// that are specialized for certain regex functions.
function HideAllVisElements()
{
	for( i = 0; i < VisElements.length; i++ )
	{
		var el = $(VisElements[i]);
		if( !el ) { continue; }	
		
		Hide( el );
	}
}

// Depending on which regex function was selected, re-show certain
// form elements
function ShowFuncVisElements( i )
{
	var els = HideOrder[i];
	
	HideAllVisElements();
	
	for( j=0; j < els.length; j++ )
	{
		var el = $(VisElements[els[j]]);
		if( !el ) { continue; }	
		
		Show( el );
	}
}

// main init function
// -set callbacks on form elements
$(document).ready(function()  
{	
	//add click callbacks to the function checkboxes
	$('#radio_match_all').click(function() { ShowFuncVisElements(0); } );
	$('#radio_match').click(function() { ShowFuncVisElements(1); } );
	$('#radio_replace').click(function() { ShowFuncVisElements(2); } );
	
	//populate the example AJAX array
	PopulateExampleList();
	$('#regex_examples').change( function() {AddRegexExample(); } );
	
	//add AJAX request callback to form submission
	$('#regex_form').submit(function() { SubmitRequest(); return false; } );
});

