$(document).ready(function(){
	
	/* form button highlights */
    $('.pricetable input:radio').focus(updateSelectedStyle);
    $('.pricetable input:radio').blur(updateSelectedStyle);
    $('.pricetable input:radio').change(updateSelectedStyle);
    $('.pricetable input:radio:checked').each(updateSelectedStyle);// for reload/back etc  

    /* form radios clearing */
    $('.pricetable .clearradio').live('click', function(){
    	clearRow($(this));
    	return false;
    });

    $('.pricetable tr').append(function(){
    	if ( $(this).attr('class') == 'subjectlabelrow' || $(this).attr('class') == 'subjectheadrow' ) {
    		var clearrowcol = "<td></td>";
    	} else {
    		var clearrowcol = '<td><a href="" class="clearradio"><img src="images/clear.png" alt="clear" /></a></td>';
    	}
    	return clearrowcol;
    });
	
	/* form error colour, for a bit of ui goodness */
	$('.error', '#formtopsection').each(function(){
		$(this).siblings('input').css('border','1px solid red');
	});
	
	/* should be a bit of form validation */
	if($('form').length != 0) {
		$("#frmMain").validate({
		    rules: {
				'inpDynForm[1][2]': "required",    // simple rule, converted to {required: true}
				'inpDynForm[1][3]': {             // compound rule
					required: true,
					email: true
				}
			},
			submitHandler: function(form) {
			   form.submit();
			   return false;
			}
		});	
	}
	
});

function clearRow(jq) {
	
	//remove styles
	var radioclass = jq.parent('td').siblings('td').children('span').children('input').attr("class");
	$('.'+radioclass).parent('span').removeClass('focused');
	
	//remove radio checks 
	$('.'+radioclass).attr('checked', false);
	
	return false;
	
}

function updateSelectedStyle() {
	
	var radioclass = $(this).attr("class");
	$('.'+radioclass).parent('span').removeClass('focused');// clear the others
	
	$(this).parent('span').addClass('focused'); // ... and add the class
	
}
