$(function() {
	var validatePage = function(){
		if($('#ulProfileType>li>input:checked').length == 0){
			$('#buttonView').attr('disabled',true);
			return;
		}
		if(	$('#selectMunicipalityName').val() == 'default'){
			$('#buttonView').attr('disabled',true);
			return;
		}
		$('#buttonView').attr('disabled',false);
		return;
	}
//on load
	$.blockUI.defaults = { 
		// message displayed when blocking (use null for no message) 
		message:  '<h1>Please wait...</h1>', 
		 
		// styles for the message when blocking; if you wish to disable 
		// these and use an external stylesheet then do this in your code: 
		// $.blockUI.defaults.css = {}; 
		css: {  
			padding:        '15px', 
			margin:         0, 
			width:          '30%',  
			top:            '40%',  
			left:           '35%',  
			textAlign:      'center',  
			color:          '#fff',  
			border:         'none', 
			backgroundColor:'#000', 
			cursor:         'wait' 
		}, 
		 
		// styles for the overlay 
		overlayCSS:  {  
			backgroundColor:'#E0E0E0',  
			opacity:        '0.6'  
		}, 
		 
		// z-index for the blocking overlay 
		baseZ: 1000, 
		 
		// set these to true to have the message automatically centered 
		centerX: true, // <-- only effects element blocking (page block controlled via css above) 
		centerY: true, 
		 
		// allow body element to be stetched in ie6; this makes blocking look better 
		// on "short" pages.  disable if you wish to prevent changes to the body height 
		allowBodyStretch: true, 
		 
		// be default blockUI will supress tab navigation from leaving blocking content; 
		constrainTabKey: true, 
		 
		// fadeOut time in millis; set to 0 to disable fadeout on unblock 
		fadeOut:  400, 
		 
		// suppresses the use of overlay styles on FF/Linux (due to significant performance issues with opacity) 
		applyPlatformOpacityRules: true 
	}; 	
	$().ajaxStart($.blockUI).ajaxStop($.unblockUI);

	validatePage();
	
	$('#buttonReset').unbind();
	$('#buttonReset').bind('click',
		function(e){
			//reset all checkboxes
			$('#ulProfileType>li>input').attr('checked',false);	
			$('#selectMunicipalityType').val("default");		
			$('#selectMunicipalityName').val("default");
			validatePage();
		}
	);
	$('#ulProfileType>li>input').unbind();
	$('#ulProfileType>li>input').bind('click',function(e){validatePage()});
	$('#selectMunicipalityType').unbind();
	$('#selectMunicipalityType').bind('click',function(e){validatePage()});		
	$('#selectMunicipalityName').unbind();
	$('#selectMunicipalityName').bind('click',function(e){validatePage()});
	
	//populate municipalities based on on status type selection	
	$('#selectMunicipalityType').bind('change',
		
		function(e){
			if($("#selectMunicipalityType").val() != 'default'){
				$('#divLoad').show();
				$.ajax({
					type:'GET',
					url:glob.file_path+'proxy.cfm/getMunicipality',
					data: {	'munDesc': $('#selectMunicipalityType option:selected').text() ,
							'munType':$(this).val()},
					dataType: 'json',
					success: function(result){							
								$("#selectMunicipalityName").removeOption(/./);
								$("#selectMunicipalityName").addOption('default','- Municipality -',false);										
								$.map(result.contents, 
									function(g){
										$('#selectMunicipalityName').addOption(g.skhdid,g.name,false);
									}
								);//end map									
								$('#selectMunicipalityName').sortOptions();										
								$('#divLoad').hide();
							},
					error: function(req, errtype, err){	
								alert('getMunicipality is unavailable.');
								$('#divLoad').hide();
							}
				});//end ajax
			}
		}//end function(e)
	);	
	
});
