// this version contains hooks for programCategory and programCategoryGroups lock
var render = {
	createSelections : {
		byInputValue : function() {
			$('#varText').html('');
			var inputValue = $('select[name="programCategoryGroups"]').val();
			var catName = '';
			var catId = '';
			var groupName = '';
			var checkboxOptions='';	
			var check = false;
			var labelCount = 0;
			var divName = '.'+ inputValue + ':has(div)';
			$('.pcategories:has(div)').each(function() {
				$(this).find('div').each(function() {
					$(this).find('h2').each(function() { 
						var key = $(this).text();
							check = false;
							if(key == inputValue){
								check = true;  
							}	
						});
					    var count = 0;
					    if(check == true){
					    	$(this).find('span').each(function() {
					    		catId = $(this).text();
					    		if(count == 0){
					    			checkboxOptions+='<div class="checkboxOption"><input type="checkbox" id="categoryCheckBox" name="programCategories" value="'+catId;
					    		}
					    		count = count + 1;  
					    	});
					    	count = 0;
					    	$(this).find('p').each(function() {   
					    		catName = $(this).text();
					    		if(count == 0){
					    			checkboxOptions+='"><span>'+catName+'</span></div>';					}   
					    		count = count + 1;  
					    	});
					    	$(this).find('h1').each(function() {   
					    		catGroupName = $(this).text();	
					    		if(labelCount == 0){
					    			$('#categorySelectLabel').html('<ul><li>'+catGroupName+':&#160;&#160;</li><li><input type="checkbox" name="programCategories" id="programCategories"/><br/><div id="mixedCheck"></div></li><li><span>All</span></li></ul>');
					    		}  
					    		labelCount = labelCount + 1;
					    	});	
					    }	  
				});	                   
			}); 
			$('#categorySelectContainer').html(checkboxOptions);
		    checkControls();
		    adjustOptions();
		},
		byProgramCategory : function() {
			$('#varText').html('');
			var inputValue = pc;
			var catName = '';
			var catId = '';
			var groupName = '';
			var checkboxOptions='';	
			var check = false;
			var catGroupId = '';
			var labelCount = 0;
			var divName = '.'+ inputValue + ':has(div)';
			$('.pcategories:has(div)').each(function() {
			$(this).find('div').each(function() {
				check = false;
				var count = 0;
				$(this).find('span').each(function() {						      
					catId = $(this).text();									
					if(catId == pc){
						check= true;								
					}
					count = count + 1;  
				});							
				if(check == true){
					$(this).find('h2').each(function() { 
						catGroupId = $(this).text();							
					});
				}
			});
			$(this).find('div').each(function() {
				$(this).find('h2').each(function() { 
					var key = $(this).text();
					check = false;
					if(key == catGroupId){
						check = true;  
					}
				});
				var count = 0;
				if(check == true){
					$(this).find('h1').each(function() {   
						catGroupName = $(this).text();
						if(labelCount == 0){
							$('#categorySelectLabel').html(catGroupName+': ');
							$('#varText').html('Get free information about '+catGroupName+' degrees');
						}  
						labelCount = labelCount + 1;  
					});
					$(this).find('span').each(function() {
						catId = $(this).text();
						if(catId == pc){
							if(count == 0){
								if(fpc == 'true' && _globalLock['category'] == true){
									$('form:first').prepend('<input type="hidden" name="programCategories" value="'+pc+'"/>');
									$('#categorySelectLabel').html('<ul><li>'+catGroupName+':</ul>');
									checkboxOptions+='<div class="checkboxOption"><input type="checkbox" checked="checked" id="categoryCheckBox" value="'+catId;
								}else{
									$('#categorySelectLabel').html('<ul><li>'+catGroupName+':&#160;&#160;</li><li><input type="checkbox" name="programCategories" id="programCategories"/><div id="mixedCheck"></div></li><li><span>All</span></li></ul>');
									checkboxOptions+='<div class="checkboxOption"><input type="checkbox" checked="checked" id="categoryCheckBox"  name="programCategories" value="'+catId;
								}
							}
						}else{
							if(count == 0){
								if(fpc == 'true' && _globalLock['category'] == true){
									checkboxOptions+='<div class="checkboxOption"><input type="checkbox" id="categoryCheckBox" value="'+catId;
								}else{
									checkboxOptions+='<div class="checkboxOption"><input type="checkbox" id="categoryCheckBox" name="programCategories"  value="'+catId;
								}
							}
						}
						count = count + 1;  
					});
					count = 0;
					$(this).find('p').each(function() {   
						catName = $(this).text();
						if(catId == pc){
							if(count == 0){
								if(fpc == 'true' && _globalLock['category'] == true){
									checkboxOptions+='" disabled="disabled"><span>'+catName+'</span></div>';
								}else{
									checkboxOptions+='"><span>'+catName+'</span></div>';
								}
							}
						}else{
							if(count == 0){
								if(fpc == 'true' && _globalLock['category'] == true){
									checkboxOptions+='" disabled="disabled"><span>'+catName+'</span></div>';
								}else{
									checkboxOptions+='"><span>'+catName+'</span></div>';
								}
							}
						}   
						count = count + 1;  
					});
				}});	                         
			}); 
			$('#categorySelectContainer').html(checkboxOptions);		
			checkControls();
			adjustOptions();
		}
	}
};//end render

var checkControls = function(){
	if($('.checkboxOption').length!=$("input[@id=categoryCheckBox]:checked").length){
		$('#programCategories').attr('checked',false);
		$('#mixedCheck').show();	
	}
	if($('.checkboxOption').length===$("input[@id=categoryCheckBox]:checked").length){
		$('#programCategories').attr('checked',true);
		$('#mixedCheck').hide();	
	}
	if($("input[@id=categoryCheckBox]:checked").length===0){
		$('#programCategories').attr('checked',false);
		$('#mixedCheck').hide();
	}
	$('.checkboxOption').unbind('click').click(checkControls);
	$('#programCategories').unbind('click').click(function(){
		if($(this).attr('checked')==true){
			$("input[@id=categoryCheckBox]").attr('checked',true);
		}else{
			$("input[@id=categoryCheckBox]").attr('checked',false);
		}
		checkControls();
	});
	$('#mixedCheck').unbind('click').click(function(){
		$("#programCategories").attr('checked',true);
		$("input[@id=categoryCheckBox]").attr('checked',true);
		$(this).hide();
	});
	if($("input[@id=categoryCheckBox]:checked").length == 0){
		$('#programCategoriesAll').attr('disabled',false);
	}else{
		$('#programCategoriesAll').attr('disabled',true);
	}
};
var adjustOptions=function(){
	var split=parseInt($('.checkboxOption').length/2);
	if($('.checkboxOption').length % 2 > 0){split++};
	$('#categorySelectContainer').append('<table><tr><td id="catLeftColumn" valign="top"></td><td id="catRightColumn" valign="top"></td></tr></table>');
	$('.checkboxOption').appendTo($('#catRightColumn'));
	$('.checkboxOption:lt('+split+')').appendTo($('#catLeftColumn'));
}
$(function(){
	if(pc != '') {	   
		render.createSelections.byProgramCategory(); 
	}else{
		render.createSelections.byInputValue(); 	
	}
	if($('select[name="programCategoryGroups"]').val() != '' ){
		$('#categorySelectContainer').parent().parent().parent().show();
		$('.submitButtonRow').css('margin-top','0px');
	}else{
		$('#categorySelectContainer').parent().parent().parent().hide();
		$('.submitButtonRow').css('margin-top','60px');
	}
	$('select[name="programCategoryGroups"]').change(function(){
		if($('select[name="programCategoryGroups"]').val() != '' ){
			$('#categorySelectContainer').parent().parent().parent().show();	
			$('.submitButtonRow').css('margin-top','0px');
			render.createSelections.byInputValue();
		}else{
			$('#categorySelectContainer').parent().parent().parent().hide();
			$('.submitButtonRow').css('margin-top','60px');
		}
	});
	if($('#programCategoryGroups').attr('disabled') == true){
		$('form:first').prepend('<input type="hidden" name="programCategoryGroups" value="'+$('#programCategoryGroups').val()+'"/>');
	}
}); 
