$(function() {
   $("#toggle_correspondence").click( 
	   function () {
	   		setToggle ($(this).attr ('checked'));
	   	} 
   	);
   	setToggle ($("#toggle_correspondence").attr ('checked'));


	
	$('#UserTerritoryId').change(function(){
		updateLanguages ();
	});
	
	updateLanguages ();
	
	sortBodies ();
	
	$('#submitForm').click (function () {
		$('#profileForm').submit ();
		return false;
	});
	
	if ($("#viewTermsLink").length > 0) {
		$("#viewTermsLink").click (function () {
			showOverlay ("/pages/display/terms", 800,500);
			return false;
		});
	}
});


function sortBodies () {
	$(".licensing_body_box").each( function() { 
			if (!this.checked) {
				var id= (this.name.substring (this.name.lastIndexOf ("[") + 1,this.name.length -1));
				disableField ('#LicensingBodyRegistrationNumber'+id);
				//$('#LicensingBodyRegistrationNumber'+id).addClass ('greyedout');
			}
		}
	);
	
	$(".licensing_body_box").click ( function() { 
		var id= (this.name.substring (this.name.lastIndexOf ("[") + 1,this.name.length -1));
			if (!this.checked) {
				
				disableField ('#LicensingBodyRegistrationNumber'+id);
				//$('#LicensingBodyRegistrationNumber'+id).addClass ('greyedout');
			}
			else {
				enableField ('#LicensingBodyRegistrationNumber'+id);
			}
		}
	);
	
}

function enableField (item) {
	$(item).get(0).disabled = false;
	$(item).removeClass ('greyedout');
	$(item).get(0).focus ();
}

function disableField (item) {
	$(item).get(0).disabled = true;
	$(item).addClass ('greyedout');
}
// set a loading image  
 function ajax_loading_image(div) {  
   $(div).html('<img src="/img/ajax-loader.gif"/>');  
 } 

 function ajax_remove_loading_image(div) {  
   $(div).html('');  
 } 


function updateLanguages () {
	 var self = $('#UserTerritoryId');
        var selected = $('#UserTerritoryId').val();
       
        
        //alert ($('#UserLanguageId').length);
		$('#UserLanguageId').find('option').remove().end();
        $('#UserLanguageId').get (0).disabled = true;
		//$('#UserLanguageId').hide ();
		$('#UserTerritoryId').get(0).disabled = true;
		//$('#submitbtn').get(0).disabled = true;
        $.ajax({
                type: "POST",
                url: '/territories/getlanguages/' + selected,
                data: "ajax=true",
                success: function(msg){
                		$('#UserLanguageId').html (msg);
                		$('#UserLanguageId').get (0).disabled = false;
                	//	$('#UserLanguageId').show ();
                		$('#UserTerritoryId').get(0).disabled = false;
                		//$('#submitbtn').get(0).disabled = false;
                       // $(self).parent('div').html(msg);
                    	  //alert (msg);
                        ajax_remove_loading_image('.language_loading_image');
                }
        });
	
}

function setToggle (checked) {
	
	$(".correspondenceField").each( function() { 
		this.disabled = checked;
		if (checked) {
			
			//alert ($(this));
			 $(this).addClass("greyedout");
		}
		else
		{
			$(this).removeClass("greyedout");
		}
		 } );
	
}
function toggleCorrespondence (dontDoIt) {
	$(".correspondenceField").each( function() { 
		if (!dontDoIt) {
			this.disabled = !this.disabled;
		}
		if (this.disabled) {
			//alert ($(this));
			 $(this).addClass("greyedout");
		}
		else
		{
			$(this).removeClass("greyedout");
		}
		 } );
}

$(document).ready(function(){
	$(document).ready(function () {
		$('#container').fadeIn(800);
        });
});



