﻿function installSubscriptionLicense(cid)
{
	openPopup('InstallLicensePopup');
	writeToObj('InstallLicenseStatus', '<p>Haetaan käyttöoikeutta..</p>');
	
	var netobj = null;
	var xmlHttp = null;
	try
	{
		netobj = new GeckoActiveXObject("DRM.GetLicense.1");
	}
	catch (err)
	{
		try
		{
			netobj = new ActiveXObject("DRM.GetLicense.1");
		}
		catch (err)
		{
		}
	}
	
	if (netobj == null)
	{
		writeToObj('InstallLicenseStatus', '<p>Virhe! Käyttöoikeuden haku epäonnistui. Tarkista laitteistosi yhteensopivuus!</p><a href="#" onclick="return closePopup(\'InstallLicensePopup\');">Sulje</a>');
		return false;
	}
	
	try 
	{
		xmlHttp = new XMLHttpRequest();
		if (xmlHttp.overrideMimeType) 
		{
			xmlHttp.overrideMimeType('text/html');
		}
	}
	catch(e) 
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			try 
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
			}
		}
	}
	
	if (xmlHttp == null)
	{
		writeToObj('InstallLicenseStatus', '<p>Virhe! Käyttöoikeuden haku epäonnistui. Tarkista laitteistosi yhteensopivuus!</p><a href="#" onclick="return closePopup(\'InstallLicensePopup\');">Sulje</a>');
		return false;
	}
	
	
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				writeToObj('InstallLicenseStatus', '<p>Käyttöoikeus haettu. Asennetaan..</p>');
				netobj.StoreLicense(xmlHttp.responseText);
				writeToObj('InstallLicenseStatus', '<p>Käyttöoikeus asennettu</p><a href="#" onclick="return closePopup(\'InstallLicensePopup\');">Sulje</a>');
				setTimeout("closePopup('InstallLicensePopup')", 500);
			}
			else
			{
				writeToObj('InstallLicenseStatus', '<p>Virhe! Käyttöoikeuden haku epäonnistui. Ota yhteyttä asiakastukeen.</p><a href="#" onclick="return closePopup(\'InstallLicensePopup\');">Sulje</a>');
			}
		}
		else
		{
			writeToObj('InstallLicenseStatus', '<p>Haetaan käyttöoikeutta.. (' + xmlHttp.readyState + '/4)</p>');
		}
	}
	
	var param = "clientinfo=" + escape(netobj.GetSystemInfo());
	xmlHttp.open('POST', "/omasivu/License.ashx?ContentId=" + cid, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", param.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(param);
	
	return true;
}

var licenseInstallReady = false;
var subscriptionDownloadReady = false;
var downloadCid;
function downloadSubscriptionAlbum(cid)
{
	openPopup('InstallLicensePopup');
	
	downloadCid = cid;
	licenseInstallReady = false;
	subscriptionDownloadReady = false;
	musiikkikauppa.dnainternet.fi.omasivu.Subscription.GetDownload(cid, onSubscriptionDownloadStatus, onSubscriptionDownloadError, onSubscriptionDownloadTimeout);
	musiikkikauppa.dnainternet.fi.omasivu.Subscription.GetLicenses(cid, onSubscriptionLicenseStatus, onSubscriptionLicenseError, onSubscriptionLicenseTimeout);
	
	return false;
}

function showSubscriptionDownloadClose()
{
	if (licenseInstallReady && subscriptionDownloadReady)
	{
		writeToObj('SubscriptionAlbmDownloadClose', '<p><a href="#" onclick="return closePopup(\'InstallLicensePopup\');">Sulje</a></p>');
	}
}

function onSubscriptionDownloadError(err)
{
	writeToObj('SubscriptionAlbumDownload', '<p>Yhteysvirhe..</p>');
	subscriptionDownloadReady = true;
	showSubscriptionDownloadClose();
}

function onSubscriptionDownloadTimeout(err)
{
	writeToObj('SubscriptionAlbumDownload', '<p>Yhteys aikakatkaistu..</p>');
	subscriptionDownloadReady = true;
	showSubscriptionDownloadClose();
}

function updateSubscriptionDownloadStatus()
{
	musiikkikauppa.dnainternet.fi.omasivu.Subscription.GetDownload(downloadCid, onSubscriptionDownloadStatus, onSubscriptionDownloadError, onSubscriptionDownloadTimeout);
}

function onSubscriptionDownloadStatus(result)
{
	if (result == null)
	{
		// Pielessä
		subscriptionDownloadReady = true;
		writeToObj('SubscriptionAlbumDownload', '<p>Latauksen aloitus epäonnistui.. Ole yhteydessä <a href="/palaute/">asiakastukeen</a>.</p>');
		showSubscriptionDownloadClose();
		return ;
	}
	
	var status = result[0];
	if (status == 0)
	{
		// Kaikki ok
		subscriptionDownloadReady = true;
		showSubscriptionDownloadClose();
		var uAgent = navigator.userAgent.toLowerCase();
		if (uAgent.indexOf("msie") >= 0)
		{
			writeToObj('SubscriptionAlbumDownload', '<p><a href="' + result[1] + '">Lataa ostos&gt;&gt;</a></p>');
		}
		else
		{
			document.location = result[1];
		}
		return ;
	}
	else if (status >= 5)
	{
		// Virhe tilanne
		subscriptionDownloadReady = true;
		writeToObj('SubscriptionAlbumDownload', '<p>' + result[1] + '</p>');
		showSubscriptionDownloadClose();
		return ;
	}
	
	writeToObj('SubscriptionAlbumDownload', '<p>' + result[1] + '</p>');
	setTimeout('updateSubscriptionDownloadStatus()', 2000);
}

function onSubscriptionLicenseError(err)
{
	writeToObj('InstallLicenseStatus', '<p>Yhteysvirhe..</p>');
	licenseInstallReady = true;
	showSubscriptionDownloadClose();
}

function onSubscriptionLicenseTimeout(err)
{
	writeToObj('InstallLicenseStatus', '<p>Yhteys aikakatkaistu..</p>');
	licenseInstallReady = true;
	showSubscriptionDownloadClose();
}

var licenseUrls;
var licenseIndex = 0;
function onSubscriptionLicenseStatus(result)
{
	if (result == null)
	{
		licenseInstallReady = true;
		writeToObj('InstallLicenseStatus', '<p>Lisensejä ei löydy..</p>');
		showSubscriptionDownloadClose();
		return ;
	}
	
	licenseUrls = result;
	writeToObj('InstallLicenseStatus', '<p>Haetaan käyttöoikeuksia ' + licenseUrls.length + 'kpl</p>');
	
	var netobj = null;
	try
	{
		netobj = new GeckoActiveXObject("DRM.GetLicense.1");
	}
	catch (err)
	{
		try
		{
			netobj = new ActiveXObject("DRM.GetLicense.1");
		}
		catch (err)
		{
		}
	}
	
	if (netobj == null)
	{
		licenseInstallReady = true;
		writeToObj('InstallLicenseStatus', '<p>Virhe! Käyttöoikeuksien haku epäonnistui. Tarkista laitteistosi yhteensopivuus!</p>');
		showSubscriptionDownloadClose();
		return false;
	}
	
	licenseIndex = 0;
	makeSubscriptionLicenseRequest(netobj);
}

function makeSubscriptionLicenseRequest(netobj)
{
	var xmlHttp = null;
	try 
	{
		xmlHttp = new XMLHttpRequest();
		if (xmlHttp.overrideMimeType) 
		{
			xmlHttp.overrideMimeType('text/html');
		}
	}
	catch(e) 
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			try 
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
			}
		}
	}
	
	if (xmlHttp == null)
	{
		licenseInstallReady = true;
		writeToObj('InstallLicenseStatus', '<p>Virhe! Käyttöoikeuksien haku epäonnistui. Tarkista laitteistosi yhteensopivuus!</p>');
		showSubscriptionDownloadClose();
		return false;
	}
	
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				writeToObj('InstallLicenseStatus', '<p>Asennetaan käytötoikeuksia ' + licenseIndex + '/' + licenseUrls.length + '</p>');
				netobj.StoreLicense(xmlHttp.responseText);
				if (licenseUrls.length > licenseIndex)
				{
					makeSubscriptionLicenseRequest(netobj);
				}
				else
				{
					licenseInstallReady = true;
					writeToObj('InstallLicenseStatus', '<p>Käyttöoikeudet asennettu!</p>');
					showSubscriptionDownloadClose();
				}
			}
			else
			{
				licenseInstallReady = true;
				writeToObj('InstallLicenseStatus', '<p>Virhe! Käyttöoikeuksien haku epäonnistui. Ota yhteyttä asiakastukeen.</p>');
				showSubscriptionDownloadClose();
			}
		}
	}
	
	var param = "clientinfo=" + escape(netobj.GetSystemInfo());
	xmlHttp.open('POST', "/omasivu/License.ashx?ContentId=" + licenseUrls[licenseIndex++], true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", param.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(param);
}

function installLicense(pid, cid)
{
	openPopup('InstallLicensePopup');
	writeToObj('InstallLicenseStatus', '<p>Haetaan käyttöoikeutta..</p>');
	
	var netobj = null;
	var xmlHttp = null;
	try
	{
		netobj = new GeckoActiveXObject("DRM.GetLicense.1");
	}
	catch (err)
	{
		try
		{
			netobj = new ActiveXObject("DRM.GetLicense.1");
		}
		catch (err)
		{
		}
	}
	
	if (netobj == null)
	{
		writeToObj('InstallLicenseStatus', '<p>Virhe! Käyttöoikeuden haku epäonnistui. Tarkista laitteistosi yhteensopivuus!</p><a href="#" onclick="return closePopup(\'InstallLicensePopup\');">Sulje</a>');
		return false;
	}
	
	try 
	{
		xmlHttp = new XMLHttpRequest();
		if (xmlHttp.overrideMimeType) 
		{
			xmlHttp.overrideMimeType('text/html');
		}
	}
	catch(e) 
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) 
		{
			try 
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
			}
		}
	}
	
	if (xmlHttp == null)
	{
		writeToObj('InstallLicenseStatus', '<p>Virhe! Käyttöoikeuden haku epäonnistui. Tarkista laitteistosi yhteensopivuus!</p><a href="#" onclick="return closePopup(\'InstallLicensePopup\');">Sulje</a>');
		return false;
	}
	
	
	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				writeToObj('InstallLicenseStatus', '<p>Käyttöoikeus haettu. Asennetaan..</p>');
				try
				{
					netobj.StoreLicense(xmlHttp.responseText);
					writeToObj('InstallLicenseStatus', '<p>Käyttöoikeus asennettu</p><a href="#" onclick="return closePopup(\'InstallLicensePopup\');">Sulje</a>');
					setTimeout("closePopup('InstallLicensePopup')", 500);
					return ;
				}
				catch (err)
				{
				}
				
				writeToObj('InstallLicenseStatus', '<p>Virhe! Käyttöoikeuden asennus epäonnistui. Ota yhteyttä asiakastukeen.</p><a href="#" onclick="return closePopup(\'InstallLicensePopup\');">Sulje</a>');
			}
			else
			{
				writeToObj('InstallLicenseStatus', '<p>Virhe! Käyttöoikeuden haku epäonnistui. Ota yhteyttä asiakastukeen. #' + xmlHttp.status + '</p><a href="#" onclick="return closePopup(\'InstallLicensePopup\');">Sulje</a>');
			}
		}
		else
		{
			writeToObj('InstallLicenseStatus', '<p>Haetaan käyttöoikeutta.. (' + xmlHttp.readyState + '/4)</p>');
		}
	}
	
	try
	{
		var param = "clientinfo=" + escape(netobj.GetSystemInfo());
		xmlHttp.open('POST', '/omasivu/License.ashx?PurchaseId=' + pid + '&ContentId=' + cid, true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", param.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(param);
	}
	catch (err)
	{
		writeToObj('InstallLicenseStatus', '<p>Virhe! Käyttöoikeuden haku epäonnistui. Tarkista koneesi yhteensopivuus. #RQ01</p><a href="#" onclick="return closePopup(\'InstallLicensePopup\');">Sulje</a>');
	}
	
	return false;
}

function inputHelpTextFocus(box, text)
{
	if (box.value == text)
	{
		box.value = '';
	}
}

function inputHelpTextBlur(box, text)
{
	if (box.value == '')
	{
		box.value = text;
	}
}

function showHide(target, link, showtext, hiddentext)
{
	var targetobj = getObj(target);
	if (targetobj != null)
	{
		if (targetobj.style.display == 'none')
		{
			targetobj.style.display = "block";
			writeToObj(link, hiddentext);
		}
		else
		{
			targetobj.style.display = "none";
			writeToObj(link, showtext);
		}
	}
}

function getWindowHeight()
{
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number')
	{
		windowHeight = window.innerHeight;
	}
	else
	{
		if (document.documentElement && document.documentElement.clientHeight)
		{
			windowHeight = document.documentElement.clientHeight;
		}
		else
		{
			if (document.body && document.body.clientHeight)
			{
				windowHeight = document.body.clientHeight;
			}
		}
	}
	
	return windowHeight;
}

function getWindowWidth()
{
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number')
	{
		windowWidth = window.innerWidth;
	}
	else
	{
		if (document.documentElement && document.documentElement.clientWidth)
		{
			windowWidth = document.documentElement.clientWidth;
		}
		else
		{
			if (document.body && document.body.clientWidth)
			{
				windowWidth = document.body.clientWidth;
			}
		}
	}
	
	if (windowWidth > 1103)
	{
		windowWidth = 1103;
	}
	
	return windowWidth;
}

function centerPopup(id)
{
	try
	{
		var plus = 0;
		if (self.pageYOffset)
		{
			plus = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
		{
			plus = document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			plus = document.body.scrollTop;
		}
		var windowHeight = getWindowHeight();
		
		var t = getObj(id);
		if (t != null)
		{
			var windowWidth = getWindowWidth();
			var contentHeight = t.offsetHeight;
			var contentWidth = t.offsetWidth;
			
			if (windowHeight - contentHeight > 0)
			{
				t.style.top = ((windowHeight / 2) - (contentHeight / 2)) + plus + 'px';
			}
			
			if (windowWidth - contentWidth > 0)
			{
				t.style.left = ((windowWidth / 2) - (contentWidth / 2)) + 'px';
			}
			
			t.style.position = 'absolute';
		}
		
		t = getObj('PopupBackground');
		if (t != null)
		{
			t.style.position = 'absolute';
			t.style.top = plus + 'px';
			t.style.height = windowHeight + 'px';
		}
	}
	catch (err)
	{
	}
}

function closePopup(id)
{
	var b=getObj('PopupBackground');
	var t=getObj(id);
	try
	{
		if ((b != null) && (t != null))
		{
			b.style.display = "none";
			t.style.display = "none";
		}
	}
	catch (err)
	{
	}
	
	try
	{
		window.onresize = null;
		window.onscroll = null;
	}
	catch (err)
	{
	}
	
	return false;
}

function openPopup(id)
{
	var b=getObj('PopupBackground');
	var t=getObj(id);
	try
	{
		if ((b != null) && (t != null))
		{
			b.style.display = "block";
			t.style.display = "block";
			centerPopup(id);
		}
	}
	catch (err)
	{
	}
	
	try
	{
		window.onresize = function() { centerPopup(id); }
		window.onscroll = function() { centerPopup(id); }
	}
	catch (err)
	{
	}
	
	return false;
}

function ShowCart()
{
	if (hasCookie("cart"))
	{
		try
		{
			musiikkikauppa.dnainternet.fi.Cart.ShowCart(onShowCart, onMethodTimeout, onMethodError);
			return ;
		}
		catch (err)
		{
		}
	}
	
	writeToObj("modules", "<div class=\"module\"><h3 class=\"basket_empty\">OSTOSKORI</h3></div>");
}

function showCartItems()
{
	hideElement('ShowCartItems');
	showBlockElement('CartItems');
	showBlockElement('HideCartItems');
}

function hideCartItems()
{
	showBlockElement('ShowCartItems');
	hideElement('CartItems');
	hideElement('HideCartItems');
}

function onShowCart(result)
{
	try
	{
		if ((result != null) && (result[0] != null) && (result[1] != null) && (result[0].length > 0) && (result[1].length == 36))
		{
			deleteCookie("cart");
			
			var exDate = new Date();
			exDate.setDate(exDate.getDate() + 2);
			document.cookie = "cart=" + escape(result[1]) + ";path=/;expires=" + exDate.toGMTString();
			
			writeToObj("modules", result[0]);
			return ;
		}
	}
	catch (err)
	{
	}
	
	writeToObj("modules", "<div class=\"module\"><h3 class=\"basket_empty\">OSTOSKORI</h3></div>");
}

function RemoveItemFromCart(cid)
{
	if ((cid == null) || (cid.length != 36))
	{
		return ;
	}
	
	try
	{
		musiikkikauppa.dnainternet.fi.Cart.RemoveItemFromCart(cid, onCartUpdate, onMethodTimeout, onMethodError);
	}
	catch (err)
	{
	}
	
	return false;
}

function ClearCart()
{
	if (confirm('Oletko varma että haluat tyhjentää ostoskorin?'))
	{
		try
		{
			musiikkikauppa.dnainternet.fi.Cart.RemoveAllItemsFromCart(onClearCart, onEmpty, onEmpty)
		}
		catch (err)
		{
		}
	}
	
	return false;
}

function onClearCart(result)
{
	if (result)
	{
		writeToObj("modules", "<div class=\"module\"><h3 class=\"basket_empty\">OSTOSKORI</h3></div>");
	}
}

function onEmpty(error)
{
}

function AddItemToCart(cid)
{
	if ((cid == null) || (cid.length < 36))
	{
		return ;
	}
	/*
	var p=getObj('popupbackground');
	var h=getObj('smokebody');
	var t=getObj('AddCartPopup');
	try
	{
		if ((h != null) && (p != null) && (t != null))
		{
			p.style.display = "block";
			h.style.display = "block";
			t.style.display = "block";
			centerPopup('AddCartPopup');
		}
	}
	catch (err)
	{
	}
	*/
	try
	{
		musiikkikauppa.dnainternet.fi.Cart.AddItemToCart(cid, onCartUpdate, onMethodTimeout, onMethodError);
	}
	catch (err)
	{
		try
		{
			if ((h != null) && (p != null) && (t != null))
			{
				p.style.display = "none";
				h.style.display = "none";
				t.style.display = "none";
			}
		}
		catch (err2)
		{
		}
	}
	
	return false;
}

function onCartUpdate(result)
{
	onShowCart(result);
	/*
	var p=getObj('popupbackground');
	var h=getObj('smokebody');
	var t=getObj('AddCartPopup');
	try
	{
		if ((h != null) && (p != null) && (t != null))
		{
			p.style.display = "none";
			h.style.display = "none";
			t.style.display = "none";
		}
	}
	catch (err)
	{
		
	}*/
}

function onMethodTimeout(error)
{
	var p=getObj('popupbackground');
	var h=getObj('smokebody');
	var t=getObj('AddCartPopup');
	try
	{
		if ((h != null) && (p != null) && (t != null))
		{
			p.style.display = "none";
			h.style.display = "none";
			t.style.display = "none";
		}
	}
	catch (err)
	{
	}
}

function onMethodError(error)
{
	var p=getObj('popupbackground');
	var h=getObj('smokebody');
	var t=getObj('AddCartPopup');
	try
	{
		if ((h != null) && (p != null) && (t != null))
		{
			p.style.display = "none";
			h.style.display = "none";
			t.style.display = "none";
		}
	}
	catch (err)
	{
	}
}

function openHintPopup(id)
{
	try
	{
		var f=getObj('HintPopupForm');
		if (f != null)
		{
			f.style.display = "block";
		}
		
		var w=getObj('HintPopupWait');
		if (w != null)
		{
			w.style.display = "none";
		}
		
		var o=getObj('HintPopupOk');
		if (o != null)
		{
			o.style.display = "none";
		}
		
		var e=getObj('HintPopupError');
		if (e != null)
		{
			e.style.display = "none";
		}
	}
	catch (err)
	{
	}
	
	openPopup(id);
	
	return false;	
}

function SendHint(cid)
{
	if ((cid == null) || (cid.length < 36))
	{
		return false;
	}
	
	var sname;
	var semail;
	var remail;
	var msg;
	var i;
	for (i=0; i < document.forms[0].elements.length; i++)
	{
		if (document.forms[0].elements[i].name.indexOf('HintSenderName') != -1)
		{
			sname = document.forms[0].elements[i].value;
		}
		else if (document.forms[0].elements[i].name.indexOf('HintSenderEmail') != -1)
		{
			semail = document.forms[0].elements[i].value;
		}
		else if (document.forms[0].elements[i].name.indexOf('HintReceiverEmail') != -1)
		{
			remail = document.forms[0].elements[i].value;
		}
		else if (document.forms[0].elements[i].name.indexOf('HintMessage') != -1)
		{
			msg = document.forms[0].elements[i].value;
		}
	}
	
	var f=getObj('HintPopupForm');
	var w=getObj('HintPopupWait');

	try
	{
		if ((f != null) && (w != null))
		{
			f.style.display = "none";
			w.style.display = "block";
		}
		centerPopup('HintPopup');
	}
	catch (err)
	{
	}
	
	try
	{
		//(string Name, string FromEmail, string ToEmail, string Id, string Message)
		musiikkikauppa.dnainternet.fi.Hint.Send(sname, semail, remail, cid, msg, onHintOk, onSendHintMethodTimeout, onSendHintMethodError);
	}
	catch (err)
	{
		if ((f != null) && (w != null))
		{
			f.style.display = "block";
			w.style.display = "none";
		}
		centerPopup('HintPopup');
	}
	
	return false;
}

function onHintOk(result)
{
	var w=getObj('HintPopupWait');
	var o=getObj('HintPopupOk');
	var e=getObj('HintPopupError');
	
	if ((w != null) && (o != null) && (e != null))
	{
		w.style.display = "none";
		try
		{
			if (result == true)
			{
				o.style.display = "block";
				centerPopup('HintPopup');
				return;
			}
		}
		catch (err)
		{
		}
		e.style.display = "block";
		centerPopup('HintPopup');
	}
}

function onSendHintMethodError(error)
{
	var f=getObj('HintPopupError');
	var w=getObj('HintPopupWait');
	
	if ((f != null) && (w != null))
	{
		f.style.display = "block";
		w.style.display = "none";
	}
	
	centerPopup('HintPopup');
}

function onSendHintMethodTimeout(error)
{
	var f=getObj('HintPopupError');
	var w=getObj('HintPopupWait');
	
	if ((f != null) && (w != null))
	{
		f.style.display = "block";
		w.style.display = "none";
	}
	
	centerPopup('HintPopup');
}

function showBlockElement(id)
{
	var obj = getObj(id);
	if (obj != null)
	{
		try
		{
			obj.style.display = "block";
			return true;
		}
		catch (err)
		{
		}
	}
	
	return false;
}

function isHiddenElement(id)
{
	var obj = getObj(id);
	if (obj != null)
	{
		try
		{
			return (obj.style.display == "none");
		}
		catch (err)
		{
		}
	}
	
	return true;
}

function hideElement(id)
{
	var obj = getObj(id);
	if (obj != null)
	{
		try
		{
			obj.style.display = "none";
			return true;
		}
		catch (err)
		{
		}
	}
	
	return false;
}

function showDownloadTime(wrtobj, albumsize)
{
	if (albumsize > 0)
	{
		var showlink = true;
		try
		{
			var speed = getCookieValue("speed");
			if (speed != null)
			{
				var rc = "";
				var aika = Math.floor(albumsize / 1024 / speed / 10);
				if ((aika / 3600) > 1)
				{
					rc = Math.floor(aika / 3600) + "h ";
					aika -= Math.floor(aika / 3600) * 3600;
					rc += Math.floor(aika / 60) + "min";
				}
				else if (aika < 60)
				{
					rc = "&lt; 1 min";
				}
				else
				{
					rc = Math.floor(aika / 60) + "min " + Math.floor(aika % 60) + "s";
				}
				
				writeToObj(wrtobj, rc);
				showlink = false;
			}
		}
		catch (err)
		{
		}

		if (showlink)
		{
			try
			{
				var bugfix = "";
				if (document.location.toString().indexOf("?") == -1)
				{
					bugfix = "?tmp=1";
				}
				writeToObj(wrtobj, "<a href=\"http://lahna.securycast.com/bw.asp?url=" + escape(document.location + bugfix) + "\">Tarkista</a>");
			}
			catch(err)
			{
			}
		}
	}
}

function getObj(id)
{
	if (document.getElementById)
	{
		return document.getElementById(id);
	}
	else if (document.all)
	{
		return document.all[id];
	}
	else if (document.layers)
	{
		return document.layers[id];
	}
	
	return null;
}

function writeToObj(id, content)
{
	try
	{
		if (document.getElementById)
		{
			document.getElementById(id).innerHTML = content;
		}
		else if (document.all)
		{
			document.all[id].innerHTML = content;
		}
		else if (document.layers)
		{
			document.layers[id].document.write(content);
			document.layers[id].document.close();
		}
	}
	catch (err)
	{
	}
}

function hasCookie(name)
{
	try
	{
		if ((document.cookie.length > 0) && (name != null) && (name.length > 0))
		{
			if (document.cookie.indexOf(name + "=") != -1)
			{
				return true;
			}
		}
	}
	catch (err)
	{
	}
	
	return false;
}

function deleteCookie(name)
{
	if (hasCookie(name))
	{
		document.cookie = name + "=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

function getCookieValue(name)
{
	try
	{
		if ((document.cookie.length > 0) && (name != null) && (name.length > 0))
		{
			var start_i = document.cookie.indexOf(name + "=");
			if (start_i != -1)
			{
				start_i = start_i + name.length + 1;
				var end_i = document.cookie.indexOf(";", start_i);
				if (end_i == -1)
				{
					end_i = document.cookie.length;
				}
				
				return unescape(document.cookie.substring(start_i, end_i));
			}
		}
	}
	catch (err)
	{
	}
	
	return null;
}


//v1.7
// Flash Player Version Detection
// Detect Client Browser type
// Copyright 2005-2007 Adobe Systems Incorporated.  All rights reserved.
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function ControlVersion()
{
	var version;
	var axo;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			
			// default to the first public version
			version = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");

		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
}

// JavaScript helper required to detect Flash Player PlugIn version information
function GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");			
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			var versionRevision = descArray[3];
			if (versionRevision == "") {
				versionRevision = descArray[4];
			}
			if (versionRevision[0] == "d") {
				versionRevision = versionRevision.substring(1);
			} else if (versionRevision[0] == "r") {
				versionRevision = versionRevision.substring(1);
				if (versionRevision.indexOf("d") > 0) {
					versionRevision = versionRevision.substring(0, versionRevision.indexOf("d"));
				}
			}
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
	}	
	return flashVer;
}

// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
	versionStr = GetSwfVer();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];

        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}

function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
  var str = '';
  if (isIE && isWin && !isOpera)
  {
    str += '<object ';
    for (var i in objAttrs)
    {
      str += i + '="' + objAttrs[i] + '" ';
    }
    str += ' class="zero">';
    for (var i in params)
    {
      str += '<param name="' + i + '" value="' + params[i] + '" /> ';
    }
    str += '</object>';
  }
  else
  {
    str += '<embed ';
    for (var i in embedAttrs)
    {
      str += i + '="' + embedAttrs[i] + '" ';
    }
    str += ' class="zero"> </embed>';
  }

  document.write(str);
}

function AC_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_SW_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".dcr", "src", "clsid:166B1BCA-3F9C-11CF-8075-444553540000"
     , null
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblclick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
      case "id":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}
