﻿if (typeof (STV) == 'undefined')
	STV = {};

STV.show = function (id, value) {
	if (typeof(value) != 'boolean')
		value = true;
		
	var el = document.getElementById(id);
	
	if (el)
		el.style.display = value ? '' : 'none';
}

STV.hide = function (id) {
	this.show(id, false);
}

STV.Thawte = {
	showSeal : function () {
		var source = document.getElementById('thawte-seal-source');
		var destination = document.getElementById('thawte-seal');
		
		if (source && destination) {
			destination.innerHTML = source.innerHTML;
			source.parentNode.removeChild(source);
		}
	}
}

STV.Channel = {
	unsavedState : false,
	activePanel : 'station',
	panels : ['station', 'time', 'category', 'genre', 'name'],
	
	onClickPanelLink : function(target) {
		var link = document.getElementById('channel-nav-link-' + target);
		
		if (link && link.getAttribute('href') == '#')
			this.selectPanel(target);
	},
	getPanelIndexByTarget : function (target) {
		if (!target)
			return;
			
		for (var i = 0; i < this.panels.length; i++)
			if (this.panels[i] == target)
				return i;
		
		return -1;
	},
	selectPrevPanel : function() {
		var index = this.getPanelIndexByTarget(this.activePanel);
		
		if (index > 0)
			this.selectPanel(this.panels[index - 1]);
	},
	selectNextPanel : function() {
		var index = this.getPanelIndexByTarget(this.activePanel);
		
		if (index < this.panels.length - 1)
			this.selectPanel(this.panels[index + 1]);
	},
	checkChannelName : function() {
		var name = document.getElementById('sName').value;
			
		if (name != '')
			return;
			
		var list = ['TVStationID', 'ChannelTimeFrameID', 'TVCategoryID', 'TVSubCategoryID'];
				
		for	 (var i = 0; i < list.length; i++) {
			var element = document.getElementById(list[i]);
			
			if (!element)
				return;
				
			var value = element.value;
			
			if (value == '0')
				continue;
				
			element = document.getElementById(list[i] + '_' + value + '_li')
			
			if (!element)
				continue;
				
			var title = element.getAttribute('title');
				
			if (name && title)
				name = name + ' ';
				
			name = name + title;
		}
		
		document.getElementById('sName').value = name;
	},
	selectPanel : function (target) {
		var index = this.getPanelIndexByTarget(target), link, li, content;

		if (index == -1)
			return;
			
		if (target == 'name')
			this.checkChannelName();
			
		this.activePanel = target;
			
		for (var i = 0; i < this.panels.length; i++) {
			link = document.getElementById('channel-nav-link-' + this.panels[i]);
			
			if (i <= index)
				link.setAttribute('href', '#');
			else
				link.removeAttribute('href');
				
			li = document.getElementById('channel-nav-' + this.panels[i]);
			li.className = i <= index ? 'active' : '';
			
			content =  document.getElementById('channel-' + this.panels[i]);
			content.style.display = i == index ? '' : 'none';
		}
		
		document.getElementById('channel-nav-prev').className = index == 0 ? '' : 'active';
		document.getElementById('channel-nav-next').className = index == this.panels.length - 1 ? '' : 'active';
		this.setButtonState();
	},
	setState : function(button, state) {
		var element = document.getElementById('channel-nav-' + button);
		
		if (element) {
			element.className = state ? 'active' : '';
			
			if (state)
				element.removeAttribute('disabled');
			else
				element.setAttribute('disabled', 'disabled');
		}
	},
	setButtonState : function () {
		//set button prev
		this.setState('prev', this.activePanel != this.panels[0]);
		
		//set button next
		var next = true;
		
		switch(this.activePanel) {
			case 'station':
				next = document.getElementById('TVStationID').value > 0;
				break;
			case 'time':
				next = document.getElementById('ChannelTimeFrameID').value > 0;
				break;
			case 'category':
				next = document.getElementById('TVCategoryID').value > 0;
				break;
			case 'genre':
				break;
			default:
				next = false;
				break;
		}
		
		this.setState('next', next);
		
		//button save
		var save = document.getElementById('TVStationID').value != '0'
					&& document.getElementById('ChannelTimeFrameID').value != '0'
					&& document.getElementById('TVCategoryID').value != '0'
					&& document.getElementById('sName').value != '';
			
		this.setState('save', save);
	},
	onChannelNameChange : function() {
		this.setUnsavedState();
		this.setButtonState();
	},
	submit : function () {
		if (!this.unsavedState)
			return;
		
		document.getElementById('channel-form').submit();
	},

	change : function (PK) {
		if (PK)
			document.location.href = '/STV/M/obj/myTVProgCtr/mytvctShow.cfm?PK=' + PK;
	},
	setUnsavedState : function() {
		if (this.unsavedState)
			return;
		
		var element = document.getElementById('channelIsSaved');
		
		if (element)
			element.style.display = 'none';
			
		element = document.getElementById('channelIsUnsaved');

		if (element)
			element.style.display = '';

		this.unsavedState = true;
	},
	toggleChannelValue : function (type, index, className, newValue) {
		var input = document.getElementById(type);
		
		if (!input)
			return;
		
		//set value of hidden field
		input.value = index;
		
		//set active/inactive-images of the buttons
		var element  = document.getElementById(type + '_ul');
		var target = type + '_' + index + '_li';
		
		for (var i = 0; i < element.childNodes.length; i++) {
			var child = element.childNodes[i];
			
			if (child.nodeType == 1 && child.nodeName == "LI")
				child.className = 'channel-' + className + (child.getAttribute('id') == target ? '-active' : '-inactive');
		}
		
		//hide/show text in footer
		element  = document.getElementById(type + '_No');
		element.style.display = index == 0 ? '' : 'none';

		element = document.getElementById(type + '_Yes');
		element.innerHTML = document.getElementById(target).getAttribute('title');
		element.style.display = index > 0 ? '' : 'none';
		
		//set buttons
		this.setUnsavedState();
		this.setButtonState();
	},
	toggleStation : function (index) {
		this.toggleChannelValue('TVStationID', index, 'small');
	},
	toggleTime : function (index) {
		this.toggleChannelValue('ChannelTimeFrameID', index, 'big');
	},
	toggleCategory : function (index) {
		this.toggleChannelValue('TVCategoryID', index, 'big');
		
		var subCategories = document.getElementById('TVCategoryID_' + index + '_sub').value.split(',');
		var target = 'TVSubCategoryID_' + document.getElementById('TVSubCategoryID').value + '_li';
		var isValid = false;
		
		var element  = document.getElementById('TVSubCategoryID_ul');
		
		for (var i = 0; i < element.childNodes.length; i++) {
			var child = element.childNodes[i];
		
			if (child.nodeType == 1 && child.nodeName == "LI")
				child.style.display = 'none';
		}

		for (var i = 0; i < subCategories.length; i++) {
			element = document.getElementById('TVSubCategoryID_' + subCategories[i] + '_li');
			element.style.display = '';
		
			if (element.getAttribute('id') == target)
				isValid = true;
		}
		
		if (!isValid)
			this.toggleSubCategory(0);
	},
	toggleSubCategory : function (index) {
		this.toggleChannelValue('TVSubCategoryID', index, 'big');
	}
}

STV.Telecast = {
	selectTab : function (id) {
		document.getElementById('telecast-tab-detail').className = id == 'detail' ? 'active' : 'inactive';
		document.getElementById('telecast-tab-participant').className = id == 'participant' ? 'active' : 'inactive';

		document.getElementById('telecast-detail').style.display = id == 'detail' ? '' : 'none';
		document.getElementById('telecast-participant').style.display = id == 'participant' ? '' : 'none';
	}
}
function del_channel()
{
Check = confirm("Sind Sie sicher, dass Sie den Channel löschen wollen?“ ");
if (Check == false)
  return false
 }
function submit_upgrade()
{
	result = confirm('Bitte bestätigen Sie das Upgrade');
	if(result == true)
		document.form_UPGRADE.submit();
}
