/*
Input javascript, to make the inputs more interactive 
*/

//Setting Input checkboxes.
var checkboxNotChecked = 'images/inputs/checkboxNotChecked.gif';
var checkboxChecked = 'images/inputs/checkboxChecked.gif';

$(document).ready(function(){
	$('.inputCheckbox').each(function(){
		if ($('#'+$(this).attr('title')).attr('value') == '1'){
			$(this).attr('src',checkboxChecked);
		} else {
			$(this).attr('src',checkboxNotChecked);
		}
		$(this).click(function(){
			if ($('#'+$(this).attr('title')).attr('value') == '1'){
				$(this).attr('src',checkboxNotChecked);
				$('#'+$(this).attr('title')).attr('value','0');
				if ($('#'+$(this).parents('.overviewForm').find('.inputCheckboxAll').attr('title')).attr('value')==1){
					$('#'+$(this).parents('.overviewForm').find('.inputCheckboxAll').attr('title')).attr('value','0');
					$(this).parents('.overviewForm').find('.inputCheckboxAll').attr('src',checkboxNotChecked);
				}
			} else {
				$(this).attr('src',checkboxChecked);
				$('#'+$(this).attr('title')).attr('value','1');
			}
			$(this).css('cursor','pointer');
			var set = 0;
			var notSet = 0;
			var numbers=0;
			$(this).parents('.overviewForm').find('.inputCheckbox').each(function(){
				if ($('#'+$(this).attr('title')).attr('value') == '1'){
					set++;
				} else {
					notSet++;
				}
				numbers++;
			});
			if (numbers == set){
				$(this).parents('.overviewForm').find('.inputCheckboxAll').attr('src',checkboxChecked);
				$('#'+$(this).parents('.overviewForm').find('.inputCheckboxAll').attr('title')).attr('value','1');
			} else {
				$(this).parents('.overviewForm').find('.inputCheckboxAll').attr('src',checkboxNotChecked);
				$('#'+$(this).parents('.overviewForm').find('.inputCheckboxAll').attr('title')).attr('value','0');
			}
		});
	});
	
	$('.inputRadio').each(function(){
		if($($(this).attr('title')).attr('value')==$(this).attr('alt')){
			$(this).attr('src',checkboxChecked);
		} else {
			$(this).attr('src',checkboxNotChecked);
		}
	});
	
	$('.inputRadio').click(function(){
		$(".inputRadio[title='"+$(this).attr('title')+"']").each(function(){
			$(this).attr('src',checkboxNotChecked);
		});
		$($(this).attr('title')).attr('value',$(this).attr('alt'));
		$(this).attr('src',checkboxChecked);
	});
	
	//SelectAll settings
	$('.overviewForm').each(function(){
		var set = 0;
		var notSet = 0;
		var numbers=0;
		$(this).find('.inputCheckbox').each(function(){
			if ($('#'+$(this).attr('title')).attr('value') == '1'){
				set++;
			} else {
				notSet++;
			}
			numbers++;
		});
		if (numbers == set){
			$(this).find('.inputCheckboxAll').attr('src',checkboxChecked);
			$('#'+$(this).find('.inputCheckboxAll').attr('title')).attr('value','1');
		} else {
			$(this).find('.inputCheckboxAll').attr('src',checkboxNotChecked);
			$('#'+$(this).find('.inputCheckboxAll').attr('title')).attr('value','0');
		}
	});
	$('.inputCheckboxAll').click(function(){
		if ($('#'+$(this).attr('title')).attr('value') == '1'){
			var setValue = 0;
			$(this).attr('src',checkboxNotChecked);
			$('#'+$(this).attr('title')).attr('value','0');
		} else {
			var setValue = 1;
			$(this).attr('src',checkboxChecked);
			$('#'+$(this).attr('title')).attr('value','1');
		}
		$(this).parents('.overviewForm').find('.inputCheckbox').each(function(){
			if (setValue == 1){
				$(this).attr('src',checkboxChecked);
				$('#'+$(this).attr('title')).attr('value','1');
			} else if (setValue == 0){
				$(this).attr('src',checkboxNotChecked);
				$('#'+$(this).attr('title')).attr('value','0');
			}
		});
	});
	
});


