var xmlDoc;
xmlDoc = loadXMLDoc("services.xml");


var servicesData = new Array();
var numServices = 0;
var NAME = 0;
var TIME = 1;
var WEB = 2;
var GRADE = 3;
var FLAG = 4;
var AUTHOR = 5;
var TITLE = 6;
var SUBJECT = 7;
var AUTHOR_EFF = 8;
var TITLE_EFF = 9;
var SUBJECT_EFF = 10;
var MONTH_EFF = 11;
var DOM_EFF = 12;
var YEAR_EFF = 13;
var NUM_PARAMS = 14;

var wedNightBibleStudy = new Array();

var SUNDAY = 0;
var WEDNESDAY = 1;




initialize_services();


function GetAttr(check_elem, attr_name)
{
    the_attr = check_elem.attributes;
    num_attr = the_attr.length;
    for (acount=0; acount<num_attr; acount++)
    {
        curr_attr = the_attr[acount];
        curr_name = String(curr_attr.nodeName);
        search_name = attr_name;
        
        if ( (curr_name.indexOf(search_name) >= 0) &&
             (curr_name.length == search_name.length) )
        {
             return curr_attr.nodeValue;
        }  
    }
    return "";
}



function GetParentName(check_elem)
{
    parent_node = check_elem.parentNode;
    return parent_node.nodeName;
}

function GetName(check_elem)
{
    return GetAttr(check_elem, "my_name");
}

function GetTime(check_elem)
{
    return GetAttr(check_elem, "time");
}

function GetLink(check_elem)
{
    return GetAttr(check_elem, "link");
}

function GetGrade(check_elem)
{
    return GetAttr(check_elem, "grade");
}

function GetAuthor(check_elem)
{
    return GetAttr(check_elem, "author");
}

function GetAuthorEff(check_elem)
{
    return GetAttr(check_elem, "author_eff");
}

function GetTitle(check_elem)
{
    return GetAttr(check_elem, "title");
}

function GetTitleEff(check_elem)
{
    return GetAttr(check_elem, "title_eff");
}

function GetMonthEff(check_elem)
{
    return GetAttr(check_elem, "month_eff");
}

function GetDOMEff(check_elem)
{
    return GetAttr(check_elem, "dom_eff");
}

function GetYearEff(check_elem)
{
    return GetAttr(check_elem, "year_eff");
}


function GetSubject(check_elem)
{
    return GetAttr(check_elem, "subject");
}

function GetSubjectEff(check_elem)
{
    return GetAttr(check_elem, "subject_eff");
}


function initialize_services()
{
    allServices = xmlDoc.getElementsByTagName("Event");
    num_services = allServices.length;
    for (count=0; count<num_services; count++)
    {
        curr_service = allServices[count];
        parent_name = GetParentName(curr_service);
        if ( (parent_name.indexOf("Sunday") >= 0) &&
              (parent_name.length == 6) )
        {
            flag = SUNDAY;
        }
        else
        {
            flag = WEDNESDAY;
        }
        
        service_name = GetName(curr_service);
        service_time = GetTime(curr_service);
        service_link = GetLink(curr_service);
        service_grade = GetGrade(curr_service);    

        book_author = GetAuthor(curr_service);
        book_title = GetTitle(curr_service);
        book_subject = GetSubject(curr_service); 



        author_eff = GetAuthorEff(curr_service);
        title_eff = GetTitleEff(curr_service);
        subject_eff = GetSubjectEff(curr_service);
        month_eff = GetMonthEff(curr_service);
        dom_eff = GetDOMEff(curr_service);
        year_eff = GetYearEff(curr_service);
        add_service_data(service_name, service_time, service_link, service_grade, flag, book_author, book_title, book_subject, author_eff, title_eff, subject_eff, month_eff, dom_eff, year_eff);
    }
        
}

function add_service_data(name, time, web_site, grade, flag, author, title, subject, author_eff, title_eff, subject_eff, month_eff, dom_eff, year_eff)
{
    servicesData[numServices] = new Array();
    servicesData[numServices][NAME] = name;
    servicesData[numServices][TIME] = time;
    servicesData[numServices][WEB] = web_site;
    servicesData[numServices][GRADE] = grade;
    servicesData[numServices][FLAG] = flag;
    servicesData[numServices][AUTHOR] = author;
    servicesData[numServices][TITLE] = title;
    servicesData[numServices][SUBJECT] = subject;
    servicesData[numServices][AUTHOR_EFF] = author_eff;
    servicesData[numServices][TITLE_EFF] = title_eff;
    servicesData[numServices][SUBJECT_EFF] = subject_eff;
    servicesData[numServices][MONTH_EFF] = month_eff;
    servicesData[numServices][DOM_EFF] = dom_eff;
    servicesData[numServices][YEAR_EFF] = year_eff;
    numServices++;
}

function write_all_services()
{
    document.write("<TABLE cellpadding='2' border='1' width='850'>");

    write_day_heading("SUNDAY");
    for (count=0; count<numServices; count++)
    {
        write_service(count, SUNDAY);
    }

    write_day_heading("WEDNESDAY");
    for (count=0; count<numServices; count++)
    {
        write_service(count, WEDNESDAY);
    }

    document.write("</TABLE>");
}

function write_day_heading(day_str)
{
    document.write("<TR>");
    document.write("<TD width='50%' class='writing'>");
    document.write("<B>" + day_str + "</B>");
    document.write("</TD>");
    document.write("<TD width='50%'>&nbsp</TD>");
    document.write("</TR>");
}

function write_service(service_idx, flag)
{
    if (servicesData[service_idx][FLAG] == flag)
    {
        document.write("<TR>");
        document.write("<TD width='50%' class='writing'>");
        if (servicesData[service_idx][WEB].length > 0)
        {
            document.write("<A HREF=" + servicesData[service_idx][WEB] + ">");
        }
        document.write(servicesData[service_idx][NAME]);
        if (servicesData[service_idx][GRADE].length > 0)
        {
            document.write(" (grades " + servicesData[service_idx][GRADE] + ")");
        }
        if (servicesData[service_idx][WEB].length > 0)
        {
            document.write("</A>");
        }
        document.write("</TD>");
        document.write("<TD width='50%'>"); 
        document.write("<TABLE width='400'>");
        document.write("<TR>");
        document.write("<TD width='80%'/>");
        document.write("<TD width='20%'>");
        document.write(servicesData[service_idx][TIME]);
        document.write("</TD>");
        document.write("</TR>");
        document.write("</TABLE>");
        document.write("</TD>");
        document.write("</TR>");   
    } 
}