﻿var SponsoredLinks = new CSponsoredLinks();

var Catchword;
var Service;

var RSSRequestObject = false;
if (window.XMLHttpRequest)
	RSSRequestObject = new XMLHttpRequest();
if (window.ActiveXObject)
	RSSRequestObject = new ActiveXObject("Microsoft.XMLHTTP");

var XmlRequestObject = false;
if (window.XMLHttpRequest)
	XmlRequestObject = new XMLHttpRequest();
if (window.ActiveXObject)
	XmlRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
	
function ReqChange() {
	if (RSSRequestObject.readyState == 4) 
	{
		if (RSSRequestObject.responseText.indexOf('invalid') == -1) 
		{ 	
			var node = RSSRequestObject.responseXML.documentElement;
			var channel = node.getElementsByTagName('ads').item(0);
			var content = '';
			
			content = '<div class="advIdentifier">';
			content += '<div class="hr"><hr/></div>';
			content += '<h2>Sponsored Links</h2>';
			content += '</div>';
			content += '<div class="sponsoredLinks">';
		
        	var items = channel.getElementsByTagName('ad');

			for (var n=0; n < items.length; n++)
			{
	    		var attributeUrl = items[n].getAttributeNode('url').value;
	    		var attributeVisibleUrl = items[n].getAttributeNode('visible_url').value;
				var itemCaption = items[n].getElementsByTagName('caption').item(0).firstChild.data;
				var itemContent = items[n].getElementsByTagName('content').item(0).firstChild.data;

                content += '<div class="sponsoredLink">';
                content += '<h3><a href="' + attributeUrl + '" onmouseover=\"return setStatus(\'' + attributeVisibleUrl + '\')\" onmouseout=\"removeStatus()\"><strong>' + itemCaption + '</strong></a></h3>';
                content += '<p><a href="' + attributeUrl + '" onmouseover="return setStatus(\'' + attributeVisibleUrl + '\')" onmouseout="removeStatus()">' + itemContent + '</a></p>';
                content += '<p><a href="' + attributeUrl + '" onmouseover="return setStatus(\'' + attributeVisibleUrl + '\' )" onmouseout="removeStatus()"><span class="url">' + attributeVisibleUrl +'</span></a></p>';
                content += '</div>';
                content += '<div class="hr"><hr /></div>';
			}
			content += '</div>';
			
			if (items.length > 0)
			{
	    		document.getElementById("SponsoredLinks").innerHTML = content;
			}
			else
			{
    			document.getElementById("SponsoredLinks").innerHTML = '';
			}
		}
		else {
			document.getElementById("SponsoredLinks").innerHTML = "<p>Fehler beim Abruf der Daten.</p>";
		}
	}
}

function RSSRequest(Service, Catchword) {
	RSSRequestObject.open("GET", "../../Partners/1und1/ExternalWebService.aspx?Service=" + Service + "&Catchword=" + Catchword, true);
	RSSRequestObject.onreadystatechange = ReqChange;
	RSSRequestObject.send(null); 
}

function CSponsoredLinks()
{
	this._Settings = {};
}

CSponsoredLinks.prototype.Init = function(Settings)
{
	this._Settings = Settings;

	Catchword = (typeof(SponsoredLinks.GetParam("Catchword")) == "undefined") ? "" : SponsoredLinks.GetParam("Catchword");
	BranchBC = (typeof(SponsoredLinks.GetParam("BranchBC")) == "undefined") ? "" : SponsoredLinks.GetParam("BranchBC");
	Service = (typeof(SponsoredLinks.GetParam("Service")) == "undefined") ? "" : SponsoredLinks.GetParam("Service");

    var Today = new Date();
    var Hour = Today.getHours();

    if (Catchword != null && Catchword != "")
    {
        RSSRequest(Service, Catchword)
    }
    else
    {
        if (BranchBC != null && BranchBC != "")
        {
            RSSRequest(Service, BranchBC);
        }
        else
        {
            document.getElementById("SponsoredLinks").innerHTML = '';
        }
    }
};

CSponsoredLinks.prototype.GetParam = function(Name, DefaultVal) 
{
	var Value = (typeof(this._Settings[Name]) == "undefined") ? DefaultVal : this._Settings[Name];

	if (Value == "true" || Value == "false")
		return (Value == "true");

	return Value;
};

var HeaderFooter = new CGetHeaderFooter();

function CGetHeaderFooter()
{
	this._Settings = {};
}

CGetHeaderFooter.prototype.Init = function(Settings)
{
	this._Settings = Settings;

	Catchword = (typeof(HeaderFooter.GetParam("Catchword")) == "undefined") ? "" : HeaderFooter.GetParam("Catchword");
	Service = (typeof(HeaderFooter.GetParam("Service")) == "undefined") ? "" : HeaderFooter.GetParam("Service");

    var Today = new Date();
    var Hour = Today.getHours();
    if (Hour >= 2 && Hour < 5)
    {
    	HeaderRequest(Service, Catchword)
    }
};

CGetHeaderFooter.prototype.GetParam = function(Name, DefaultVal) 
{
	var Value = (typeof(this._Settings[Name]) == "undefined") ? DefaultVal : this._Settings[Name];

	if (Value == "true" || Value == "false")
		return (Value == "true");

	return Value;
};

function HeaderRequest(Service) {
    XmlRequestObject.open("GET", "../../Partners/1und1/ExternalWebService.aspx?Service=" + Service, true);
    XmlRequestObject.send(null);
}