﻿function loadTwitterFeed() {
    var twitDiv = document.getElementById('twitterFeed');
    if (twitDiv) {
        twitDiv.innerHTML = 'Loading...';
    }
    LoadViaHTTP('cmd=LoadTwitterFeed');
}

function process_LoadTwitterFeed(res) {
    if (res) {
        var twitterDIV = document.getElementById('twitterFeed');
        var bits = res.split('|');
        if (twitterDIV && bits && bits.length > 1) {
            if (bits[1].indexOf('ERROR') > -1) {
                twitterDIV.innerHTML = 'ERROR Loading...';
            } else {
                var html = '';
                for (i = 1; i < bits.length; i++) {
                    var temp = bits[i].split('#');
                    if (temp && temp.length == 2) {
                        html += '<div class="twitterItem">';
                        //html += '<img src="assets/Twitter.gif" style="float:left"/>';
                        html += '<div style="margin-left:0px;">';
                        html += convertLinks(temp[0]);
                        html += '<div class="twitterDate">' + temp[1] + '</div>';
                        html += '</div>';
                        html += '</div>';
                    }
                }
                twitterDIV.innerHTML = html;
            }
        }
    }
}

function convertLinks(text) {

    if (!text) return text;
        text = text.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi, function(url) {
        nice = url;
        if (url.match('^https?:\/\/')) {
            nice = nice.replace(/^https?:\/\//i, '')
        }
        else
            url = 'http://' + url;

        return '<a target="_blank" rel="nofollow" href="' + url + '">' + nice.replace(/^www./i, '') + '</a>';

    });
    return text;
}   


/* AJAX Functions */


function LoadViaHTTP(qs) {

    try {

        var url = 'SimpleWebTools.aspx?' + qs;

        if (window.XMLHttpRequest) {
            http = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            http = new ActiveXObject("Microsoft.XMLHTTP");
        }

        if (http) {
            http.onreadystatechange = httpAppProcess;
            http.open('GET', url, true);
            http.send(null);
        }

    } catch (e) {
        alert('LoadViaHTTP ' + e.toString());
    }

}

function LoadViaHTTPWithXML(qs, xml) {

    try {

        var url = 'SimpleWebTools.aspx?' + qs;

        if (window.XMLHttpRequest) {
            http = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            http = new ActiveXObject("Microsoft.XMLHTTP");
        }

        if (http) {
            http.onreadystatechange = httpAppProcess;
            http.open('POST', url, true);
            http.setRequestHeader("Content-Type", "text/xml");
            http.send(xml);
        }

    } catch (e) {
        alert('LoadViaHTTPWithXML ' + e.toString());
    }

}

function httpAppProcess() {

    // clear post process
    var postProcess = '';
    var postProcessData = '';

    if (http.readyState == 4 && http.status == 200) {

        try {

            // get response
            var res = http.responseText;

            // clear http object
            http = null;

            // return the command followed by appropriate info
            var firstLine = res.indexOf('|');

            if (firstLine > -1) {

                // get command
                var command = res.substr(0, firstLine);

                if (command == 'LoadTwitterFeed') {
                    process_LoadTwitterFeed(res);
                }
                
            }

        } catch (e) {
            alert('httpAppProcess-->' + e.toString());
        }
    }
}