﻿// JScript File
function MakeItFresh(i)
{
    switch (i)
    {
        case 0:
            ajaxEvents();    
            break;
        case 1:
            ajaxJobs();
            break;
        case 2:
            ajaxFunding();
            break;
        case 3:
            ajaxGrnts();
            break;
        case 4:
            ajaxNews();
            break;
        case 5:
            ajaxPols();
            break;
        case 6:
            ajaxGoogle();
            break;
        case 7:
            ajaxTube();
            break;
        default:
            break;
    }
}

function ajaxEvents()
{
    xmlEvent = ajaxCreate();

    if (xmlEvent!=null)
    {
        xmlEvent.open("GET","data/" + xmlFiles[0], true);
        xmlEvent.onreadystatechange=onEventResponse;
        xmlEvent.send(null);
    }
}

function onEventResponse()
{
    var count = 0;
    if (xmlEvent.readyState!=4)
    {
        return;
    }
    if (xmlEvent.status!=200)
    {
        document.getElementById(theParts[0]).innerHTML="Error!";
    }
    else
    {
        xIEvent=xmlEvent.responseXML.documentElement.getElementsByTagName(xmlElements[0]);
        count = xIEvent.length-0;
        document.getElementById(theParts[0]).innerHTML=String(count) + theSay[0] + "Posted";
    }
}

function ajaxJobs()
{
    xmlJob = ajaxCreate();

    if (xmlJob!=null)
    {
        xmlJob.open("GET","data/" + xmlFiles[1], true);
        xmlJob.onreadystatechange=onJobResponse;
        xmlJob.send(null);
    }
}

function onJobResponse()
{
    var count = 0, iCount = 0;
    var strDate = new String();
    var strBuild = new String(), cPrcStr = new String();
    
    if (xmlJob.readyState!=4)
    {
        return;
    }
    if (xmlJob.status!=200)
    {
        document.getElementById(theParts[1]).innerHTML="Error!";
    }
    else
    {
        xIJob=xmlJob.responseXML.documentElement.getElementsByTagName(xmlElements[1]);
        count = xIJob.length-1;
        document.getElementById(theParts[1]).innerHTML=String(count) + theSay[1] + "Posted";
    }
}

function ajaxFunding()
{
    xmlFund = ajaxCreate();

    if (xmlFund!=null)
    {
        xmlFund.open("GET","data/" + xmlFiles[2], true);
        xmlFund.onreadystatechange=onFundResponse;
        xmlFund.send(null);
    }
}

function onFundResponse()
{
    var count = 0, iCount = 0;
    var strDate = new String();
    var strBuild = new String(), cPrcStr = new String();
    
    if (xmlFund.readyState!=4)
    {
        return;
    }
    if (xmlFund.status!=200)
    {
        document.getElementById(theParts[2]).innerHTML="Error!";
    }
    else
    {
        xIFund=xmlFund.responseXML.documentElement.getElementsByTagName(xmlElements[2]);
        count = xIFund.length-1;
        document.getElementById(theParts[2]).innerHTML=String(count) + theSay[2] + "Posted";
    }
}

function ajaxGrnts()
{
    xmlGrnt = ajaxCreate();

    if (xmlGrnt!=null)
    {
        xmlGrnt.open("GET","data/" + xmlFiles[3], true);
        xmlGrnt.onreadystatechange=onGrntResponse;
        xmlGrnt.send(null);
    }
}

function onGrntResponse()
{
    var count = 0, iCount = 0;
    var strDate = new String();
    var strBuild = new String(), cPrcStr = new String();
    
    if (xmlGrnt.readyState != 4)
    {
        return;
    }
    if (xmlGrnt.status != 200)
    {
        document.getElementById(theParts[3]).innerHTML="Error!";
    }
    else
    {
        xIGrnt=xmlGrnt.responseXML.documentElement.getElementsByTagName(xmlElements[3]);
        count = xIGrnt.length-1;
        document.getElementById(theParts[3]).innerHTML=String(count) + theSay[3] + "Posted";      
    }
}

function ajaxNews()
{
    xmlNews = ajaxCreate();

    if (xmlNews!=null)
    {
        xmlNews.open("GET","data/" + xmlFiles[4], true);
        xmlNews.onreadystatechange=onNewsResponse;
        xmlNews.send(null);
    }
}

function onNewsResponse()
{
    var count = 0;
    
    if (xmlNews.readyState!=4)
    {
        return;
    }
    if (xmlNews.status!=200)
    {
        document.getElementById(theParts[4]).innerHTML="Error!";
    }
    else
    {
        xINews=xmlNews.responseXML.documentElement.getElementsByTagName(xmlElements[4]);
        count = xINews.length-1;
        document.getElementById(theParts[4]).innerHTML=String(count) + theSay[4] + "Posted";
    }
}

function ajaxPols()
{
    xmlPols = ajaxCreate();

    if (xmlPols!=null)
    {
        xmlPols.open("GET","data/" + xmlFiles[5], true);
        xmlPols.onreadystatechange=onPolResponse;
        xmlPols.send(null);
    }
}

function onPolResponse()
{
    var count = 0;
    
    if (xmlPols.readyState != 4)
    {
        return;
    }
    if (xmlPols.status != 200)
    {
        document.getElementById(theParts[5]).innerHTML="Error!";
    }
    else
    {
        xIPols=xmlPols.responseXML.documentElement.getElementsByTagName(xmlElements[5]);
        count = xIPols.length-1;
        document.getElementById(theParts[5]).innerHTML=String(count) + theSay[5] + "Posted";
    }
}

function ajaxGoogle()
{
    xmlGoogle = ajaxCreate();

    if (xmlGoogle!=null)
    {
        xmlGoogle.open("GET","data/" + xmlFiles[6], true);
        xmlGoogle.onreadystatechange=onGoResponse;
        xmlGoogle.send(null);
    }
}

function onGoResponse()
{
    if (xmlGoogle.readyState!=4)
    {
        return;
    }
    if (xmlGoogle.status!=200)
    {
        document.getElementById(theParts[6]).innerHTML="Error!";
    }
    else
    {
        xIGoogle=xmlGoogle.responseText;
        document.getElementById(theParts[6]).innerHTML=xIGoogle;
    }
}

function ajaxTube()
{
    xmlTube = ajaxCreate();

    if (xmlTube!=null)
    {
        xmlTube.open("GET","data/" + xmlFiles[7], true);
        xmlTube.onreadystatechange=onTubeResponse;
        xmlTube.send(null);
    }
}

function onTubeResponse()
{
    if (xmlTube.readyState!=4)
    {
        return;
    }
    if (xmlTube.status!=200)
    {
        document.getElementById(theParts[7]).innerHTML="Error!";
    }
    else
    {
        xITube=xmlTube.responseText;
        document.getElementById(theParts[7]).innerHTML=xITube;
    }
}

function DropTwo(iWhich)
{
    var cStr = new String("");
    var i = 0;
    
    for (i = 0; i < 6; i++)
    {
        document.getElementById(theExts[i]).innerHTML = "&nbsp;";
    }
    
    cStr = DropMore(iWhich);
    document.getElementById(theExts[iWhich]).innerHTML = cStr;
}

function DropMore(iExt)
{
    var cStrRetVal = new String(".");
    var strDate = new String(), cPrcStr = new String();
    var strBuild = new String();
    var i = 0, iCount = 0;
    
    strDate = ExpireDate();
    strBuild = "";
    
    switch (iExt)
    {
        case 0:
            for (i = 0; i < xIEvent.length; i++)
            {
                cPrcStr = String(xIEvent[i].getElementsByTagName("dend")[0].childNodes[0].nodeValue);
                if (cPrcStr  >= strDate)
                {
                    iCount += 1;
                    if (iCount == 1)
                    {
                        strBuild += "<ul>";
                    }
                    strBuild += "<li>";
                    strBuild += SwitchDate(xIEvent[i].getElementsByTagName("dstart")[0].childNodes[0].nodeValue);
                    strBuild += " - ";
                    strBuild += String(xIEvent[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
                    strBuild += "</li>";
                    if (iCount == 3)
                    {
                        break;
                    }
                }
            }
            
            if (strBuild != "")
            {
                strBuild += "</ul>";
                cStrRetVal = strBuild;
            }
            break;
        case 1:
            for (i = 0; i < xIJob.length; i++)
            {
                cPrcStr = String(xIJob[i].getElementsByTagName("expires")[0].childNodes[0].nodeValue);
                if (strDate < cPrcStr)
                {
                    iCount += 1;
                    if (iCount == 1)
                    {
                        strBuild += "<ul>";
                    }
                    strBuild += "<li>";
                    strBuild += String(xIJob[i].getElementsByTagName("position")[0].childNodes[0].nodeValue);
                    strBuild += "</li>";
                    if (iCount == 3)
                    {
                        break;
                    }
                }
            }
            
            if (strBuild != "")
            {
                strBuild += "</ul>";
                cStrRetVal = strBuild;
            }
            break;
        case 2:
        
            for (i = 0; i < xIFund.length; i++)
            {
                cPrcStr = String(xIFund[i].getElementsByTagName("expires")[0].childNodes[0].nodeValue);
                if (strDate < cPrcStr)
                {
                    iCount += 1;
                    if (iCount == 1)
                    {
                        strBuild += "<ul>";
                    }
                    strBuild += "<li>";
                    strBuild += String(xIFund[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
                    strBuild += "</li>";
                    if (iCount == 3)
                    {
                        break;
                    }
                }
            }
            
            if (strBuild != "")
            {
                strBuild += "</ul>";
                cStrRetVal = strBuild;
            }
            break;
        case 3:
        
            for (i = 0; i < xIGrnt.length; i++)
            {
                cPrcStr = String(xIGrnt[i].getElementsByTagName("expires")[0].childNodes[0].nodeValue);
                if (strDate < cPrcStr)
                {
                    iCount += 1;
                    if (iCount == 1)
                    {
                        strBuild += "<ul>";
                    }
                    strBuild += "<li>";
                    strBuild += String(xIGrnt[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
                    strBuild += "</li>";
                    if (iCount == 3)
                    {
                        break;
                    }
                }
            }
            
            if (strBuild != "")
            {
                strBuild += "</ul>";
                cStrRetVal = strBuild;
            }
            break;
        case 4:
           for (i = 0; i < xINews.length; i++)
            {
                cPrcStr = String(xINews[i].getElementsByTagName("expires")[0].childNodes[0].nodeValue);
                if (strDate < cPrcStr)
                {
                    iCount += 1;
                    if (iCount == 1)
                    {
                        strBuild += "<ul>";
                    }
                    strBuild += "<li>";
                    strBuild += String(xINews[i].getElementsByTagName("headline")[0].childNodes[0].nodeValue);
                    strBuild += "</li>";
                    if (iCount == 3)
                    {
                        break;
                    }
                }
            }
            
            if (strBuild != "")
            {
                strBuild += "</ul>";
                cStrRetVal = strBuild;
            }            
            break;
        case 5:
            for (i = 0; i < xIPols.length; i++)
            {
                cPrcStr = String(xIPols[i].getElementsByTagName("expires")[0].childNodes[0].nodeValue);
                if (strDate < cPrcStr)
                {
                    iCount += 1;
                    if (iCount == 1)
                    {
                        strBuild += "<ul>";
                    }
                    strBuild += "<li>";
                    strBuild += String(xIPols[i].getElementsByTagName("headline")[0].childNodes[0].nodeValue);
                    strBuild += "</li>";
                    if (iCount == 3)
                    {
                        break;
                    }
                }
            }
            
            if (strBuild != "")
            {
                strBuild += "</ul>";
                cStrRetVal = strBuild;
            }
            break;
        default:
            break;
    }

    return cStrRetVal;
}

var xmlFiles = new Array(8), xmlElements = new Array(8), theParts = new Array(8);
var theSay = new Array(6), theExts = new Array(6);
var xmlEvent, xmlJob, xmlFund, xmlGrnt, xmlNews, xmlPols, xmlGoogle, xmlTube;
var xIEvent, xIJob, xIFund, xIGrnt, xINews, xIPols, xIGoogle, xITube;

xmlFiles[0] = "cedata.xml";
xmlFiles[1] = "jbsdata.xml";
xmlFiles[2] = "scholdata.xml";
xmlFiles[3] = "grntdata.xml";
xmlFiles[4] = "newsdata.xml";
xmlFiles[5] = "poldata.xml";
xmlFiles[6] = "google.txt";
xmlFiles[7] = "tube.txt";

xmlElements[0] = "entry";
xmlElements[1] = "job";
xmlElements[2] = "source";
xmlElements[3] = "grant";
xmlElements[4] = "entry";
xmlElements[5] = "entry";

theParts[0] = "parta";
theParts[1] = "partb";
theParts[2] = "partc";
theParts[3] = "partd";
theParts[4] = "parte";
theParts[5] = "partf";
theParts[6] = "google";
theParts[7] = "tube";

theExts[0] = "extA";
theExts[1] = "extB";
theExts[2] = "extC";
theExts[3] = "extD";
theExts[4] = "extE";
theExts[5] = "extF";

theSay[0] = " Event(s) ";
theSay[1] = " Job(s) ";
theSay[2] = " Scholarship Resource(s) ";
theSay[3] = " Grant Resource(s) ";
theSay[4] = " News Item(s) ";
theSay[5] = " Political Item(s) ";
