﻿// Configurations
var feedLocation = 'MarketWireSearchServiceProxy.asmx';
var threadIntervalInSeconds = 60; // In seconds
var newsDetailPage = 'displayNews.aspx'
var subCategoryPage = 'MarketWireSubCategories.aspx?category=';

// Variables declaration
var intervalId;
var http;
var newsDisplayElement = document.getElementById('categoriesDisplay');

// Gets the HTTP Object
function getHTTPObject() {

    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {
        }
    }
    return false;
}

function getInnerText(node) {
    if (typeof node.textContent != 'undefined') {
        return node.textContent;
    }
    else if (typeof node.innerText != 'undefined') {
        return node.innerText;
    }
    else if (typeof node.text != 'undefined') {
        return node.text;
    }
    else {
        switch (node.nodeType) {
            case 3:
            case 4:
                return node.nodeValue;
                break;
            case 1:
            case 11:
                var innerText = '';
                for (var i = 0; i < node.childNodes.length; i++) {
                    innerText += getInnerText(node.childNodes[i]);
                }
                return innerText;
                break;
            default:
                return '';
        }
    }
}
    
function getElement(node, innerElement, itemCounter) {
    return node.getElementsByTagName(innerElement).item(itemCounter)
}
    function getCategoryHtml(feedElement) {

    var category = getInnerText(feedElement);
    var html = '<td ><span id="category" ><a style="font-family:Arial;font-size:12px;color:black;" href="' +
                subCategoryPage + 
                category +
                '">' +
                category +               
                '</a></span>' +
                '</span></td>';

    return html;
}

// Displays the news
function displayCategories() {

    if (http.readyState == 4) {

        try {

            var xDoc = http.responseXML;

            var html = '<table id="categoryDisplayElement" style="width:650px;">';

            if (xDoc.childNodes.length > 0 &&
                xDoc.lastChild.tagName == 'ArrayOfFeedHeadCategory' &&
                xDoc.lastChild.childNodes.length > 0) {

                for (var count = 0; count < xDoc.lastChild.getElementsByTagName('Category').length; count++) {
                    html = html + "<tr>"
                    html = html + getCategoryHtml(getElement(xDoc.lastChild, 'Category', count));
                    count = count + 1;
                    if (count < xDoc.lastChild.getElementsByTagName('Category').length) {
                        html = html + getCategoryHtml(getElement(xDoc.lastChild, 'Category', count));
                    }
                    else {
                        html = html + "<td></td>";
                    }
                    html = html + "</tr>"
                }
                html = html + '</table>';
            }

            newsDisplayElement.innerHTML = html;
        }
        catch (exp) {
        }
    }
}

// Fetches categories
function FetchCategories() {

    http.open('POST', feedLocation, true);
    http.onreadystatechange = displayCategories;
    http.send(null);
}

// execute the news fetching process
function execute() {

    feedLocation = feedLocation + '/GetAllCategories';
    http = getHTTPObject();
    FetchCategories();
}

// Stops timer and news fetching process
function stopTimer() {
    clearInterval(intervalId);
}