if(!w3d) var w3d = {};
if(!w3d.manager) w3d.manager = {};
if(!w3d.manager.clientgroup) w3d.manager.clientgroup = {};

var wmc = w3d.manager.clientgroup;

wmc.initManageClientGroup = function() {
	var form = $('.clientGroupForm');
	
	wmc.getClientGroupList(form);
	
	$('select.listBox', form).change(function() {
		wmc.selectClientGroup(form, $(this).val());
	});
	$('.clientGroupFunc.create', form).click(function() {
		wmc.createClientGroup(form);
	});
	$('.clientGroupFunc.modify', form).click(function() {
		wmc.modifyClientGroup(form);
	});
	$('.clientGroupFunc.delete', form).click(function() {
		wmc.deleteClientGroup(form);
	});
};

wmc.getClientGroupList = function(form, selectedId) {
	var selector = $('select.listBox', form);
	
	$.ajax({
		url: '/manager/clientgroup/getlist',
		type: 'POST',
		cache: false,
		dataType: 'xml',
		async: false,
		success: function(xml) {
			var clientGroupList = $('Records', $(xml));
			
			selector.empty();
			$('Record', clientGroupList).each(function(i) {
				var clientGroupId = $(this).attr('id');
				var clientGroupName = $(this).attr('name');
				$("<option value='" + clientGroupId + "'>" + clientGroupName + "</option>").appendTo(selector);
			});
			wmc.selectClientGroup(form, selectedId);
		},
		error: function() {
			alert('Loading clientGroup list has failed');
		}
	});
};

wmc.selectClientGroup = function(form, selectedId) {
	var selector = $('select.listBox', form);
	var nameInput = $('.clientGroupName', form);
	
	if ($('option', selector).length > 0) {
		var selectedOption;
		if (selectedId)
			selectedOption = $('option[value=' + selectedId + ']', selector);
		else
			selectedOption = $('option:first', selector);
			
		$('option', selector).removeAttr('selected');
		selectedOption.attr('selected', 'selected');
		nameInput.val(selectedOption.text());
	}
	else {
		nameInput.val('');
	}
};

wmc.createClientGroup = function(form) {
	var selector = $('select.listBox', form);
	var clientGroupName = $('.clientGroupName', form).val();
	
	if (clientGroupName.length === 0) {
		alert('client group name must be at least 1 character long.');
		return;
	}
	
	$.ajax({
		url: '/manager/clientgroup/create',
		type: 'POST',
		cache: false,
		data: {name: clientGroupName},
		dataType: 'json',
		async: false,
		success: function(response) {
			if (response['success']) {
				wmc.getClientGroupList(form, response['id']);
			}
			else {
				switch (response['reason']) {
					case 'NameAlreadyExists':
						alert('Creating clientGroup has failed : Already exists');
						break;
					default:
						alert('Creating clientGroup has failed : ' + response['reason']);
				}
			}
		},
		error: function() {
			alert('Creating clientGroup has failed');
		}
	});
};

wmc.modifyClientGroup = function(form) {
	var selector = $('select.listBox', form);
	var selectedValue = selector.val();
	var selectedOption = $('option[value=' + selectedValue + ']', selector);
	
	var clientGroupName = $('.clientGroupName', form).val();
	
	if (clientGroupName.length === 0) {
		alert('client group name must be at least 1 character long.');
		return;
	}
	
	$.ajax({
		url: '/manager/clientgroup/modify',
		type: 'POST',
		cache: false,
		data: {cgid: selectedValue, name: clientGroupName},
		dataType: 'json',
		async: false,
		success: function(response) {
			if (response['success']) {
				wmc.getClientGroupList(form, selectedValue);
			}
			else {
				var failReasons = response['reason'];
				var errorMsg = '';
				
				if (failReasons.search('DoesNotExist') > -1) {
					errorMsg += 'Does not exist.\n';//' : 수정할 사용자가 존재하지 않습니다.\n';
				}
				if (failReasons.search('NoChanges') > -1) {
					errorMsg += 'No changes.\n';//' : 변경 사항이 없습니다.\n';
				}
				alert('Modifying clientGroup name has failed\n' + errorMsg);
			}
		},
		error: function() {
			alert('Modifying clientGroup name has failed');
		}
	});
};

wmc.deleteClientGroup = function(form) {
	var selector = $('select.listBox', form);
	var selectedValue = selector.val();
	var selectedOption = $('option[value=' + selectedValue + ']', selector);
	
	if (selectedOption.length == 0 || !confirm("Delete [" + selectedOption.text() + "]?")) {
		return;
	}
	
	$.ajax({
		url: '/manager/clientgroup/delete',
		type: 'POST',
		cache: false,
		data: {cgid: selectedValue},
		dataType: 'json',
		async: false,
		success: function(response) {
			if (response['success']) {
				wmc.getClientGroupList(form);
			}
			else {
				switch (response['reason']) {
					case 'DoesNotExist':
						alert('Deleting clientGroup has failed\n : Does not exist');
						break;
					default:
						alert('Deleting clientGroup has failed : ' + response['reason']);
				}
			}
		},
		error: function() {
			alert('Deleting clientGroup has failed');
		}
	});
};