﻿$(document).ready(function(){

	// There's no easy way to find one of these columns; we'll look for the comment with the column name "Request"
	var searchText = RegExp("FieldName=\"Request\"", "gi");
	// Loop through all of the ms-formbody table cells
	$("td.ms-formbody").each(function() {
		// Check for the right comment
		if(searchText.test($(this).html())) {
			// Wrap with the sizer div
			$(this).wrap("<div id='sizer'></div>");
			// Find all of the checkboxes, and for each...
			$(this).find("span.ms-RadioText").each(function() {
				// ...grab the current label for the checkbox...
				var thisLabel = $(this).find("label").html();
				// ...remove the current label...
				$(this).find("label").remove();
				// ...and wrap the input element with a label with the right class applied to it
				$(this).find("input").wrap("<label class='label_check'>" + thisLabel + "</label>");
			});
		}
	});

	// There's no easy way to find one of these columns; we'll look for the comment with the column name "Options"
	var searchText = RegExp("FieldName=\"Options\"", "gi");
	// Loop through all of the ms-formbody table cells
	$("td.ms-formbody").each(function() {
		// Check for the right comment
		if(searchText.test($(this).html())) {
			// Wrap with the sizer div
			$(this).wrap("<div id='sizer'></div>");
			// Find all of the radio buttons, and for each...
			$(this).find("span.ms-RadioText").each(function() {
				// ...grab the current label for the radio button...
				var thisLabel = $(this).find("label").html();
				// ...remove the current label...
				$(this).find("label").remove();
				// ...and wrap the input element with a label with the right class applied to it
				$(this).find("input").wrap("<label class='label_radio'>" + thisLabel + "</label>");
			});
		}
	});

	$('body').addClass('has-js');
	$('.label_check, .label_radio').click(function(){
		setupLabel();
	});
	setupLabel(); 
});

function setupLabel() {
	if ($('.label_check input').length) {
		$('.label_check').each(function(){ 
			$(this).removeClass('c_on');
		});
		$('.label_check input:checked').each(function(){ 
			$(this).parent('label').addClass('c_on');
		});                
	};
	if ($('.label_radio input').length) {
		$('.label_radio').each(function(){ 
			$(this).removeClass('r_on');
		});
		$('.label_radio input:checked').each(function(){ 
			$(this).parent('label').addClass('r_on');
		});
	};
};