var Benefit = {

	id: 'benefitTable',

	getCount: function() {
		return $('#' + Benefit.id).children('select').get(0).length;
	},
	
	change: function(obj) {
		if (!Benefit.isEmpty(obj) && Benefit.isLast(obj)) {
			Benefit.addNew();
		}
	},
	
	addNew: function() {
		var sel = Benefit.obj().children('tr').children('td').children('select').get().selectedIndex;
		Benefit.obj().children('tr').children('td').children('select').get().selectedIndex = 0;
		Benefit.obj().append('<tr>' + Benefit.obj().children('tr').html() + '</tr>');
		Benefit.obj().children('tr').children('td').children('select').get().selectedIndex = sel;
	},
	
	remove: function(obj) {
		if (!Benefit.isAlone()) {
			$(obj).parent().parent().remove();
		} else {
			alert('Poslední benefit nelze smazat.');
		}
	},
	
	isEmpty: function(obj) {
		return obj.selectedIndex == 0;
	},
	
	isLast: function(obj) {
		return ($(obj).parent().parent().nextAll().length == 0);
	},
	
	obj: function() {
		return $('#' + Benefit.id).children('tbody');
	},
	
	isAlone: function() {
		return Benefit.obj().children('tr').get().length <= 1
	}
};

function setCookie(c_name,value,expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function checkCookie(cookie) {
	var cookieName = getCookie(cookie)
	if (cookieName != null && cookieName != "") {
		return true;
	} else {
		return false;
	}
}

function checkBanner(element, cookie) {
	if (checkCookie(cookie)) {
		element.style.display = "none";
	} else {
		element.style.display = "block";
	}
}

