$(document).ready(function(){
	
	var maxSendTo = 10;
	
	showHidePanelSA();
	
	$('.saRow .sendToOption').each(function(){
		enableAddContentPoint(this);
	});
	
	$('.saRow .datacontentCenter input:checkbox').each(function(){
		enableOptionsSA(this);
	});
	
	$('#selectAcct').change(function(){
		showHidePanelSA();
	});
	
	$('#btnSuspend').click(function(){
		hideAllAlerts();
		$(this).hide();
		$('.updateAlerts').hide();
		$('.updateAlertsOff').show();
		$('#btnEnable').show();
		$('#disableText').show();
		$('#suspendSuccess').show();
		var temp = $('#alerts input, #alerts select').not('#selectAcct');
		setDisabledFlag(temp);
		//$('#alerts input, #alerts select').not('#selectAcct').attr('disabled', 'disabled');
		$('.additionalContact').hide();
		$('.removeLink').hide();
	});
	
	$('#btnEnable').click(function(){
		hideAllAlerts();
		$(this).hide();
		$('.updateAlertsOff').hide();
		$('.updateAlerts').show();
		$('#btnSuspend').show();
		$('#enableSuccess').show();
		var temp = $('#alerts input, #alerts select');
		resetDisabledFlag(temp);
		//$('#alerts input, #alerts select').attr('disabled', '');
		$('.additionalContact').show();
		$('.removeLink').show();
	});
	
	$('.updateAlerts').click(function(){
		hideAllAlerts();
		$('#success').show();
	});
	
	$('.saRow .datacontentCenter input:checkbox').click(function(){
		enableOptionsSA(this);			 
	});
	
	$('.balanceSent').change(function(){
		balanceSentShowHide(this);	   
	});
	
	$('.saRow .sendToOption').change(function(){
		var t = $(this).find('option:selected').val();
		if(t == 'addContact'){
			window.open('ContactPoints.aspx.htm','_self');
		}
	});
	
	$('a.additionalContact').click(function(){
		var additionalContactSelect = '<select class="sendToOption newSendToOption"><option selected="selected" value="default">Select a Contact</option><option>Primary E-Mail</option><option>Work E-mail</option><option>My Voice Number</option><option>My Cell Phone</option><option value="divider">------------------------------</option><option value="addContact">Add Contact Point</option></select>'
		var removeButton = '<span class="removeLink">&nbsp;&nbsp;<a href="javascript:void(0);" class="smalltxt"><img src="../Themes/TopTabMenu/Images/delete_icon.gif" alt="delete icon" /></a><br /></span>';
		var count = $(this).parent().parent().find('.sendToOption').size();
		
		$(this).hide()	
			   .before(additionalContactSelect)
			   .prev('.newSendToOption').bind('change',function(){	
					
					var t = $(this).find('option:selected').val();
					if(t == 'addContact'){
						window.open('ContactPoints.aspx.htm','_self');
					}else if(t != 'divider'){
						if(count < maxSendTo-1){
							$(this).parent().find('.additionalContact').show();
						}else{
							$(this).parent().find('.additionalContact').hide();
						}
							
						if(!$(this).next().hasClass("removeLink")){	
							$(this).parent().find('.additionalContact').before(removeButton);
						}
						
						$('.removeLink').bind('click',function(){
							$(this).parent().parent().find('.additionalContact').show();
							$(this).prev().remove();
							$(this).remove();
						});	
					}
		});	
	});
	
	function hideAllAlerts(){
		$('.successmsg').hide();
		$('#disableText').hide();
	}
	
	function showHidePanelSA(){
		var option = $('#selectAcct option:selected').val();
		option = '.type' + option;
		$('.saRow').hide();
		$(option).show();	
	}
	
	function enableOptionsSA(obj){
		var checkboxValue = $(obj).attr('checked');
		if(checkboxValue == true){
			$(obj).parent().parent().find('.sendToOption').attr('disabled', '');
			$(obj).parent().parent().find('.sendToOption').show();
			$(obj).parent().parent().find('.additionalContact').show();
			$(obj).parent().parent().find('.removeLink').show();
		}else{
			$(obj).parent().parent().find('.sendToOption').attr('disabled', 'disabled');
			$(obj).parent().parent().find('.sendToOption').not('.sendToOption:first').hide();
			$(obj).parent().parent().find('.additionalContact').hide();
			$(obj).parent().parent().find('.removeLink').hide();
		}
	}
	
	function balanceSentShowHide(obj){
		var option = $(obj).find('option:selected').val();
		option = option + 'Option';
		$(obj).parent().find('.sendingOptions').hide();
		$(obj).parent().find('.' + option).show();
	}

	function enableAddContentPoint(obj){
		var selectBoxIndex = $(obj).attr('selectedIndex');
		if(selectBoxIndex != null){
			$(obj).parent().parent().find('.additionalContact').show();
			$(obj).parent().parent().find('.removeLink').show();
		}else{
			$(obj).parent().parent().find('.additionalContact').hide();
			$(obj).parent().parent().find('.removeLink').hide();
		}
	}
	
	
	//Sets a flag to remember which inputs are already disabled  --  Then disables all fields
	function setDisabledFlag(obj){	
		$(obj).each(function(){
			if($(this).attr('disabled')){
				$(this).removeClass('rememberDisabled')
					   .addClass('rememberDisabled');
			}else{
				$(this).attr('disabled','disabled');
			}
		});
	}
	
	//Returns all inputs to their original state
	function resetDisabledFlag(obj){
		$(obj).each(function(){
			if($(this).hasClass('rememberDisabled')){
				$(this).removeClass('rememberDisabled');		
			}else{
				$(this).attr('disabled','');
			}
		});
	}
});