﻿STV = {};

STV.Login = {
	field : function (id, value1, value2) {
		var element = document.getElementById(id);
		
		if (element && element.value == value1)
			element.value = value2;
	},
	autoLogin : function(isChecked) {
		if (isChecked)
			var el = document.getElementById('dmcautologintext');
			
			if (el)
				alert(el.innerHTML);
	}
}

STV.Home = {
	currentHighlight : 1,
	currentTopRecordOrder : 1,
	recordOrderMoveDistance : [2, 7, 16, 26, 35, 40, 42],
	recordOrderMoveStep : 0,
	chartListElement : null,
	onLoad : function (activeHighlight) {
		this.currentHighlight = activeHighlight;

		this.enableSwitchHighlight();
		this.enableSwitchTopRecordOrder();
	},
	enableSwitchHighlight : function (activeHighlight) {
		window.setTimeout('STV.Home.switchHighlight();', 10000);
	},
	enableSwitchTopRecordOrder : function() {
		window.setTimeout('STV.Home.switchTopRecordOrder();', 4000);
	},
	switchHighlight : function () {
		var imgOld = document.getElementById('highlight-image-' + this.currentHighlight);
		var textOld = document.getElementById('highlight-text-' + this.currentHighlight);
	
		this.currentHighlight++;
		
		var img = document.getElementById('highlight-image-' + this.currentHighlight);
		var text = document.getElementById('highlight-text-' + this.currentHighlight);
		
		if (!img || !text) {
			this.currentHighlight = 1;
			img = document.getElementById('highlight-image-' + this.currentHighlight);
			text = document.getElementById('highlight-text-' + this.currentHighlight);
		}
		
		if (imgOld && textOld && img && text) {
			imgOld.style.display = 'none';
			textOld.style.display = 'none';
			img.style.display = '';
			text.style.display = '';
			
			this.enableSwitchHighlight();
		}
	},
	switchTopRecordOrder : function () {
		this.recordOrderMoveStep = 0;
		this.chartListElement = document.getElementById('subconchartlist');
		this.getElementNode(4).style.display = '';
		this.moveTopRecordOrder();
	},
	moveTopRecordOrder : function () {
		var step = this.recordOrderMoveDistance[this.recordOrderMoveStep];
		this.chartListElement.style.top = '-' + step + 'px';
		
		this.recordOrderMoveStep++;
		
		if (this.recordOrderMoveStep >= this.recordOrderMoveDistance.length)
			window.setTimeout('STV.Home.finishMoveTopRecordOrder();', 60);
		else
			window.setTimeout('STV.Home.moveTopRecordOrder();', 60);
	},
	finishMoveTopRecordOrder : function () {
		var el = this.getElementNode(1);

		el.style.display = 'none'
		this.chartListElement.removeChild(el);
		this.chartListElement.appendChild(el);

		this.chartListElement.style.top = "0px";
		this.enableSwitchTopRecordOrder();
	},
	getElementNode : function (index)
	{
		var currentCount = 0;
		var nodes = this.chartListElement.childNodes;

		for	(var i = 0; i < nodes.length; i ++) {
			var node = nodes[i];
			
			if (node.nodeType == 1) {
				currentCount ++;
				
				if (currentCount == index)
					return node;
			}
		}
		
		return null;
	}
}

STV.RegisterNow = {
	email : function (value1, value2) {
		var element = document.getElementById('loginform_regstart');
		
		if (element && element.value == value1)
			element.value = value2;
	}
}

STV.Support = {
	openQuestion : function (index) {
		var elements = document.getElementsByName('support-faq-answer');
		
		for (var i = 0; i < elements.length; i++)
			elements[i].style.display = 'none';
			
		var el = document.getElementById('support-faq-answer-' + index);
		
		if (el)
			el.style.display = '';
	},
	changeCategory : function () {
		var form = document.getElementById('change-category-form');
		
		if (form)
			form.submit();
	},
	selectForm : function (isUser) {
		if (isUser) {
			document.getElementById('support-form-user').style.display = ""
			document.getElementById('support-form-nonuser').style.display = "none";			
		} else {
			document.getElementById('support-form-user').style.display = "none"
			document.getElementById('support-form-nonuser').style.display = "";			
		}	
		document.getElementById('bIsUser').value = isUser ? 1 : 0;
	},
	changeQuestion : function (id) {
		var el = document.getElementById(name);
		
		if (el)
			document.getElementById('questionID').value = el.value;
	},
	getData : function(isUser)
	{
		var state = DWRUtil.getValue(isUser ? "CategoryID2" : "CategoryID");
		
		DWREngine._execute('/STV/S/misc/support/miscgetSubject.cfm', null, 'getSubject', state, isUser ? this.handleDataResultUser : this.handleDataResultNotUser);
	},
	handleDataResultUser : function(result) {
		DWRUtil.removeAllOptions("questionIDSelect2");
		DWRUtil.addOptions("questionIDSelect2", result, "KEY", "VALUE");
	},
	handleDataResultNotUser : function(result) {
		DWRUtil.removeAllOptions("questionIDSelect");
		DWRUtil.addOptions("questionIDSelect", result, "KEY", "VALUE");
	}
}

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.Register = {};

STV.Register.CheckStatus = {
	timer : null,
	step : 10,
	stop : function () {
		if (this.timer)
			clearTimeout(this.timer);
			
		this.timer = null;
	},
	start : function (steps) {
		this.step = steps;
		this.stop();
		this.setTimer();
	},
	show : function() {
		this.timer = null;

		if (this.step > 0)
			this.step = this.step - 1;
		else {
			this.stop();
			return;
		}
			
		var el = document.getElementById('register-check-status-time');
		
		if (el)
			el.innerHTML = this.step.toString();
   
		this.setTimer();
	},
	setTimer : function() {
		this.timer = setTimeout('STV.Register.CheckStatus.show()', 1000);
	}
}

STV.AFEnquiry = {
	showSubType : function(type, show) {
		var elements = document.getElementsByName('TypeOfMarketingContent_' + type);
		
		for (var i = 0; i < elements.length; i++) {
			elements[i].style.display = show ? '' : 'none';
		}
	},
	getSubTypeId : function(type, subType) {
		return 'lURL_TypeOfMarketing_' + type + '_SubTypeOfMarketing_' + subType;
	},
	showLinks : function(type, subType, show) {
		var el = document.getElementById(this.getSubTypeId(type, subType) + '_list');
		
		if (el)
			el.style.display = show ? '' : 'none';
	},
	addUrl : function(type, subType) {
		var index = 0;
		var name = this.getSubTypeId(type, subType);
		
		while (document.getElementById(name + '_' + index))
			index++;
			
		var li = document.createElement('li');
		
		var label = document.createElement('label');
		label.className = 'x-small';
		label.setAttribute('for', name + '_' + index);
		label.appendChild(document.createTextNode('http://'));
		
		var input = document.createElement('input');
		input.className = 'x-large';
		input.setAttribute('type', 'text');
		input.setAttribute('type', 'text');
		input.setAttribute('name', name);
		input.setAttribute('id', name + '_' + index);
		
		li.appendChild(label);
		li.appendChild(input);

		var el = document.getElementById(name + '_list_last');
		el.parentNode.insertBefore(li, el);
	}
}