// Contains methods to embed external *.htm pages in a html page


// Gets the request...
function GetRequest() {

    var req = false;

    // For Safari, Firefox, and other non-MS browsers
    if (window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } catch (e) {
            req = false;
        }

    }
    // For Internet Explorer on Windows
    else if (window.ActiveXObject) {

        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                req = false;
            }
        }
    }
    
    return req;
}


function GetElement(id){
    // Get the element where the page is to go
    var element = document.getElementById(id);
    if (!element) {
        alert("Bad id");
    }
    
    return element;
}



// This function reads in the html page (url) and inserts it into the
// element bit
function GetHTML(element, req, url) {
    if (req) {
        // Synchronous request, wait till we have it all
        req.open('GET', url, false);
        req.send(null);
        element.innerHTML += req.responseText;
    }
}

// This should process a CSV line... I hope
function ProcessCSVLine(line) {

    var isString = false;
    var csv = new Array();
    var start = 0;
    var i;
    
    for (i = 0; i < line.length + 1; i++) {

        if (i == line.length || line[i] == ',') {
            if (!isString) {
                csv.push(line.substring(start, i));
                start = i + 1;
            }
        }

        if (line[i] == '"') {
            if (isString) {
                csv.push(line.substring(start, i));
                i++;
                start = i + 1;
                isString = false;
            }
            else {
                isString = true;
                start = i + 1;
            }
        }
    }
    return csv;
}

function GetEventList() {

    var txtFile = new XMLHttpRequest();
    var i;

    txtFile.open("GET", "events.csv", false);
    txtFile.send(null);

    lines = txtFile.responseText.split("\r\n"); // Will separate each line into an array

    for (i = 0; i < lines.length; i++) {
        lines[i] = ProcessCSVLine(lines[i]);
    }
    
    return lines;
}


// Should be called from html page, reads in 
function embedHTML() {
    // Get the html argument
    var query = window.location.search;
    // Skip the leading ?, which should always be there,
    // but be careful anyway
    if (query.substring(0, 1) == '?') {
        query = query.substring(1);
    }
    // Don't bother splitting?
    var data = query;


    // Get data name
    if (data.length == 0) {
        data = 'news';
    }
    var i = parseInt(data);
    if (!isNaN(i)) {
        data = 'art' + data;
    }

    var req = GetRequest();
    var element = GetElement('pagecontent');

    if (element) {

        // Note any Javascript in sub pages will not run... so it must be done here

        if (data == 'news') {
        
            // Random Morris Picture
            var picz = new Array("main.jpg", "main1.jpg", "main2.jpg", "main3.jpg", "main4.jpg", "main5.jpg", "main6.jpg", "main7.jpg");
            var indexer = Math.round(Math.random() * (picz.length - 1));
            var nxEvt = "<div style=\"float:right; padding:10px 10px 10px 10px;\"><img src=\"" + picz[indexer] + "\" alt=\"Morris Minor\" /></div>";

            // Next Events
            lines = GetEventList();
            
            var dtNow = new Date()
            var NextEvent1 = null;
            var NextEvent2 = null;

            for (i = 0; i < lines.length; i++) {
                // "April 4th"  "May 13th/14th"
                var strx = lines[i][0].replace(new RegExp("st|nd|rd|th|/[0-9]+"), "") + ", " + dtNow.getFullYear();

                var dx = new Date(strx);

                if (dx > dtNow || i == lines.length-3) {
                    NextEvent1 = lines[i];
                    NextEvent2 = lines[i + 1];
                    break;
                }

            }

            nxEvt += "<h3><p><b>Next events</p><p>";

            nxEvt += NextEvent1[0] + " : " + NextEvent1[1] + "<br />";
            nxEvt += NextEvent2[0] + " : " + NextEvent2[1] + "</p></b></h3>";

            element.innerHTML = nxEvt;                    

        }

        GetHTML(element, req, data + ".htm");

        if (data == 'up') {
            
            var upPage = ""

            upPage += "<table class=\"snetable\"><tbody>"
            upPage += "<tr><td class=\"t_head\" style=\"width: 15%;\"><b>Date</b></td>"
            upPage += "<td class=\"t_head\" style=\"width: 60%;\"><b>Details</b></td>"
            upPage += "<td class=\"t_head\" style=\"width: 25%;\"><b>Apply to</b></td></tr>"

            lines = GetEventList();

            var i;
            for (i = 0; i < lines.length; i++) {
                if (lines[i].length == 3) {
                    // StringBuilder anyone?
                    upPage += "<tr><td class=\"t_ele\">"
                    upPage += lines[i][0];
                    upPage += "</td><td class=\"t_ele\">"
                    upPage += lines[i][1];
                    upPage += "</td><td class=\"t_ele\">"
                    upPage += lines[i][2];
                    upPage += "</td></tr>"
                }
            }
            
            upPage += "</tbody></table>";   
            
            element.innerHTML += upPage              
        }

        if (isNaN(i) || i == 0) {
            // Extra thingys
            element.innerHTML += "<hr noshade=\"noshade\"/><p align=\"center\"><a href=\"index.htm\">Back to Home</a></p>";
        }
        else {
            element.innerHTML += "<hr noshade=\"noshade\"/><p align=\"center\"><a href=\"index.htm?0\">Back to Articles</a></p><hr noshade=\"noshade\"/><p align=\"right\">Images hosted by <a href=\"http://photobucket.com/\">Photobucket</a>";
        }
    }
}
       