var branch = {
	reset : function() {
		jQuery('#branch-name').val('');
		jQuery('#branch-name-en').val('');
		jQuery('#branch-name').removeClass('form-error');
		jQuery('#branch-name-en').removeClass('form-error');

		jQuery('#branch-color').val('000000');
		jQuery('#color-selector div').css('backgroundColor', '#000000');
		jQuery('#color-selector').ColorPickerSetColor('000000');		
	},
	box : {
		get: function(element_id) {
			// check if element exists
			if (jQuery('#' + element_id).length > 0) { 
				// load msg before update
				jQuery('#' + element_id).html('<img src="' + doc_root + 'misc/icons/loader.gif" style="padding:45%;" />'); 
				var remote_url = doc_root + 'includes/ajax/branch.' + element_id + '.get.php';
				jQuery.post(remote_url, {
					
				}, function (data, status) {
					jQuery('#' + element_id).html(data);
					
//					jQuery('#branch-table').ingrid({
//						url: remote_url
//					});
				},'json');
			}
		} // end get
	}, // end box
	
	load : function(branch_id) {
		if (!isNaN(branch_id)) {
			jQuery('#branch-name').removeClass('form-error');
			jQuery('#branch-name-en').removeClass('form-error');			
			jQuery.post(doc_root + 'includes/ajax/branch.get.php', {
				branch_id: branch_id
			}, function (data, status) {
				if (data) {
					var branch_color = data.strColor.length > 1 ? data.strColor : '000000';
					jQuery('#branch-id').val(data.intId);
					jQuery('#branch-name').val(data.strName);
					jQuery('#branch-name-en').val(data.strNameEn);
					jQuery('#branch-color').val(branch_color);
					jQuery('#color-selector div').css('backgroundColor', '#' + branch_color);
					jQuery('#color-selector').ColorPickerSetColor(branch_color);
					
				}
				else {
					alert(gt.gettext("Keine Daten zurückgeliefert"));
				}
			},'json');
		}
		else {
			alert(gt.gettext("Branchen-id ist keine Zahl"));
		}
	},
	save : function() {
		var branch_id = jQuery('#branch-id').val();
		jQuery('#branch-id').val('');
		
		var branch_name = jQuery.trim(jQuery('#branch-name').val());
		var branch_name_en = jQuery.trim(jQuery('#branch-name-en').val());
		var branch_color = jQuery('#branch-color').val();
		
		branch_color = branch_color.length > 0 ? branch_color : 'ff0000';
		
		if (branch_name.length > 0 && branch_name_en.length > 0) {
			jQuery.post(doc_root + 'includes/ajax/branch.save.php', {
				branch_id: branch_id,
				branch_name: branch_name,
				branch_name_en: branch_name_en,
				branch_color: branch_color
			}, function (data, status) {
				if (data) {
					branch.box.get('branch-content');
					branch.reset();					
					
				}
				else {
					alert(gt.gettext("Speichern war nicht erfolgreich"));
				}
			},'json');
		}
		else {
			if( branch_name.length == 0 ){
				jQuery('#branch-name').addClass('form-error');
			}
			if( branch_name_en.length == 0 ){
				jQuery('#branch-name-en').addClass('form-error');
			}
		}
		
		
	},
	
	_delete : function(branch_id) {
		jQuery.post(doc_root + 'includes/ajax/branch.delete.php', {
			branch_id : branch_id
		}, function(data, status) {
			branch.box.get('branch-content');
			alert(gt.gettext("Branche gelöscht"));
		});
	}
};
