{
    var jsonprograms;
    var jsonprogramcategory;
    var pathname = window.location.pathname;

    $(document).ready(function() {
        $.ajax({
            type: "GET",
            url: "http://www.barry.edu/xml/programsjson.txt",
            data: {},
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            success: function(msg) {
                jsonprograms = msg;
                GetCategoryList();
                $("#programsearch").keyup(function() {
                    BuildProgramListing();
                });
            }
        });
    })
    function showlocations() {
        $('.ajax-holder').html("");
        $('.text-content').html("");
        $('.inner').html("");
        $('.listing').html("");
        var table = "";
        count = 1;
        count2 = 1;


        var row = "<table width='97%' border='0' cellpadding='6' cellspacing='0'><tbody>";
        if (count2 == 1) {
            row += "<tr><td height='5'></td></tr><tr>";

            row += "<td class='ListingAlternativeItemHolder'><b style='font-size:12pt'>&nbsp;&nbsp;Locations</b></td>";
            row += "</tr><tr><td height='5'></td></tr>";
            count2 = 2;
        }

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Alachua County', 'Alachua County Programs')\" >";
        row += "<td class='ListingItemHolder'> <span class='ListingLink'>Alachua County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Bahamas', 'Bahamas')\" >";
        row += "<td class='ListingAlternativeItemHolder'> <span class='ListingLink'>Bahamas</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Brevard County', 'Brevard County Programs')\" >";
        row += "<td class='ListingItemHolder'> <span class='ListingLink'>Brevard County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Broward County', 'Broward County Programs')\" >";
        row += "<td class='ListingAlternativeItemHolder'> <span class='ListingLink'>Broward County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Collier County', 'Collier County Programs')\" >";
        row += "<td class='ListingItemHolder'> <span class='ListingLink'>Collier County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Hillsborough County', 'Hillsborough County Programs')\" >";
        row += "<td class='ListingAlternativeItemHolder'> <span class='ListingLink'>Hillsborough County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Lake County', 'Lake County Programs')\" >";
        row += "<td class='ListingItemHolder'> <span class='ListingLink'>Lake County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Lee County', 'Lee County Programs')\" >";
        row += "<td class='ListingAlternativeItemHolder'> <span class='ListingLink'>Lee County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Leon County', 'Leon County Programs')\" >";
        row += "<td class='ListingItemHolder'> <span class='ListingLink'>Leon County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Marion County', 'Marion County Programs')\" >";
        row += "<td class='ListingAlternativeItemHolder'> <span class='ListingLink'>Marion County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Miami-Dade County (Main Campus)', 'Miami-Dade County (Main Campus) Programs')\" >";
        row += "<td class='ListingItemHolder'> <span class='ListingLink'>Miami-Dade County (Main Campus)</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Miami-Dade County (Other Locations)', 'Miami-Dade County (Other Locations) Programs')\" >";
        row += "<td class='ListingAlternativeItemHolder'> <span class='ListingLink'>Miami-Dade County (Other Locations)</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Orange County', 'Orange County Programs')\" >";
        row += "<td class='ListingItemHolder'> <span class='ListingLink'>Orange County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Palm Beach County', 'Palm Beach County Programs')\" >";
        row += "<td class='ListingAlternativeItemHolder'> <span class='ListingLink'>Palm Beach County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Pasco County', 'Pasco County Programs')\" >";
        row += "<td class='ListingItemHolder'> <span class='ListingLink'>Pasco County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Pinellas County', 'Pinellas County Programs')\" >";
        row += "<td class='ListingAlternativeItemHolder'> <span class='ListingLink'>Pinellas County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Polk County', 'Polk County Programs')\" >";
        row += "<td class='ListingItemHolder'> <span class='ListingLink'>Polk County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'Sarasota County', 'Sarasota County Programs')\" >";
        row += "<td class='ListingAlternativeItemHolder'> <span class='ListingLink'>Sarasota County</span></td>";
        row += "</tr>";

        row += "<tr onclick=\"BuildProgramCategoryListing('location', 'St. Lucie/Indian River/Martin Counties', 'St. Lucie/Indian River/Martin County Programs')\" >";
        row += "<td class='ListingItemHolder'> <span class='ListingLink'>St. Lucie/Indian River/Martin Counties</span></td>";
        row += "</tr>";

        row += "</tbody></table>";

        table += row;
        $('#Programdiv').html(table);

    }
    function GetCategoryList() {

       
        $.ajax({
            type: "GET",
            url: "http://www.barry.edu/xml/programcategoryjson.txt",
            data: {},
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            success: function(msg) {
            //alert(msg);
            jsonprogramcategory = msg;
            //alert(jsonprogramcategory);
                var str = "" + window.location + "";

                var partsArray = str.split('?');
                if (partsArray[1]) {
                    var variable = partsArray[1].split('=');
                    if (variable[0] == 'county')
                        BuildProgramCategoryListing('location', variable[1], variable[1] + ' Programs');

                    var variable2 = variable[1].split('&');
                    if (variable2[0].toLowerCase() == "ug")
                        BuildProgramCategoryListing('Cat', 2, 'Undergraduate Programs');
                    if (variable2[0].toLowerCase() == "gr")
                        BuildProgramCategoryListing('Cat', 15, 'Graduate Programs');

                    if (variable2[0].toLowerCase() == "all")
                        BuildAllProgramListing();

                }
            }
        });

    }
    function ProgramSearchInsert(keyword, link) {
        $.ajax({
            type: "POST",
            url: "http://www.barry.edu/webservice/ProgramsHost/programs.svc/ProgramSearchInsert",
            data: '{"Keyword": "' + keyword + '" , "IpAddress": "", "Link": "' + link + '"}',
            dataType: "xml",
            contentType: "application/json;charset=UTF-8",
            success: function() {
            }
        });
        parent.window.location = link;
    }
    function BuildProgramListing() {
        $('.ajax-holder').html("");
        $('.text-content').html("");
        $('.inner').html("");
        $('.listing').html("");


        var searchword2 = $("#programsearch").val();
        var searchword = $("#programsearch").val();
        searchword = searchword.replace("(", "");
        searchword = searchword.replace(")", "");
        var msg = jsonprograms;
        var table = "";
        count = 1;
        count2 = 1;

        var rowclass = "ListingItemHolder";
        for (var i = 0; i < msg.length; i++) {
            var programname = msg[i].Name;
            var keywords = msg[i].Keyword;
            programname = programname.replace("(", "");
            programname = programname.replace(")", "");
            var url = msg[i].URL;
            var result = programname.toLowerCase().match(searchword.toLowerCase());
            var result2 = keywords.toLowerCase().match(searchword.toLowerCase());
            if (result || result2) {

                var row = "<table width='97%' border='0' cellpadding='6' cellspacing='0'><tbody>";
                if (count2 == 1) {
                    row += "<tr><td height='5'></td></tr><tr>";

                    row += "<td class='ListingAlternativeItemHolder'><b  style='font-size:12pt'>&nbsp;&nbsp;Search results for: '" + searchword2 + "'</b></td>";
                    row += "</tr><tr><td height='5'></td></tr>";
                    count2 = 2;
                }
                if (url == '') {

                    row += "<tr><td class='" + rowclass + "'> <span class='ListingNoLink'>" + msg[i].Name + "</span></td></tr>";
                    row += "<tr><td class='" + rowclass + "'><div style='margin-left:15px;'><p>" + msg[i].Description + "</p></div></td></tr>";
                }
                else {
                    row += "<tr><td class='" + rowclass + "' onclick=\"ProgramSearchInsert('" + searchword2 + "', '" + url + "')\" > <span class='ListingLink'>" + msg[i].Name + "</span></td></tr>";
                    row += "<tr><td class='" + rowclass + "'><div style='margin-left:15px;'><p>" + msg[i].Description + "</p></div></td></tr>";
                }

                row += "</tbody></table>";

                table += row;
                if (count == 1) {
                    count = 2;
                    rowclass = "ListingAlternativeItemHolder";
                }
                else {
                    count = 1;
                    rowclass = "ListingItemHolder";
                }

            }

        }
        $('#Programdiv').html(table);
    }

    function BuildAllProgramListing() {
        if (jsonprograms) {
            clearInterval(intervalID);
            $('.ajax-holder').html("");
            $('.text-content').html("");
            $('.inner').html("");
            $('.listing').html("");

            var searchword = "";
            var msg = jsonprograms;
            var table = "";
            count = 1;
            count2 = 1;

            var rowclass = "ListingItemHolder";
            for (var i = 0; i < msg.length; i++) {
                var programname = msg[i].Name;
                var keywords = msg[i].Keyword;
                var url = msg[i].URL;
                var result = programname.toLowerCase().match(searchword.toLowerCase());
                var result2 = keywords.toLowerCase().match(searchword.toLowerCase());
                if (result || result2) {

                    var row = "<table width='97%' border='0' cellpadding='6' cellspacing='0'><tbody>";
                    if (count2 == 1) {
                        row += "<tr><td height='5'></td></tr><tr>";

                        row += "<td class='ListingAlternativeItemHolder'><b style='font-size:12pt'>&nbsp;&nbsp;All Programs</b></td>";
                        row += "</tr><tr><td height='5'></td></tr>";
                        count2 = 2;
                    }
                    if (url == '') {
                        row += "<tr><td class='" + rowclass + "'> <span class='ListingNoLink'>" + msg[i].Name + "</span></td></tr>";
                        row += "<tr><td class='" + rowclass + "'> <div style='margin-left:15px;'><p>" + msg[i].Description + "</p></div></td></tr>";
                    }
                    else {
                        row += "<tr><td class='" + rowclass + "' onclick=\"ProgramSearchInsert('All Programs', '" + url + "')\" > <span class='ListingLink'>" + msg[i].Name + "</span></td></tr>";
                        row += "<tr><td class='" + rowclass + "'> <div style='margin-left:15px;'><p>" + msg[i].Description + "</p></div></td></tr>";
                    }

                    row += "</tbody></table>";

                    table += row;
                    if (count == 1) {
                        count = 2;
                        rowclass = "ListingAlternativeItemHolder";
                    }
                    else {
                        count = 1;
                        rowclass = "ListingItemHolder";
                    }

                }

            }
            $('#Programdiv').html(table);
        }
        else {

            var intervalID = setInterval('BuildAllProgramListing()', 3000);
        }
    }
    function AboutBarry(listing, search, DisplayName) {
        var intervalID = setInterval('BuildProgramCategoryListing("' + listing + '", ' + search + ', "' +DisplayName + '")', 3000);
    }
    function BuildProgramCategoryListing(listing, search, DisplayName) {

        if (jsonprogramcategory) {
           // alert('1');
            var type;
            var programname2 = '';
            $('.ajax-holder').html("");
            $('.text-content').html("");
            $('.inner').html("");
            $('.listing').html("");

            var msg = jsonprogramcategory;
            var table = "";
            count = 1;
            count2 = 1;

            var rowclass = "ListingItemHolder";
            for (var i = 0; i < msg.length; i++) {
                var programname = msg[i].AltName;
                var url = msg[i].URL;
                var categoryid = msg[i].Category;
                var studenttype = msg[i].StudentType;
                var webname = msg[i].WEBNAME;
                if (listing == "Cat")
                    type = categoryid;
                if (listing == "Student")
                    type = studenttype;
                if (listing == "location")
                    type = webname;
                if (programname2 != programname) {
                    if (search == '15') {
                       
                        for (var j = 13; j < 16; j++) {
                            if (type == j) {
                                var row = "<table width='97%' border='0' cellpadding='6' cellspacing='0'><tbody>";
                                if (count2 == 1) {
                                    row += "</tr><tr><td height='5'></td></tr><tr>";

                                    row += "<td class='ListingAlternativeItemHolder'><b style='font-size:12pt'>&nbsp;&nbsp;" + DisplayName + "</b></td>";
                                    row += "</tr></tr><tr><td height='5'></td></tr>";
                                    count2 = 2;
                                }
                                if (url == '') {
                                    row += "<tr><td class='" + rowclass + "'> <span class='ListingNoLink'>" + programname + "</span></td></tr>";
                                    row += "<tr><td class='" + rowclass + "'><div style='margin-left:15px;'><p>" + msg[i].Description + "</p></div></td></tr>";
                                }
                                else {
                                    row += "<tr><td class='" + rowclass + "' onclick=\"ProgramSearchInsert('" + DisplayName + "', '" + url + "')\" > <span class='ListingLink'>" + programname + "</span></td></tr>";
                                    row += "<tr><td class='" + rowclass + "'><div style='margin-left:15px;'><p>" + msg[i].Description + "</p></div></td></tr>";
                                }
                                row += "</tbody></table>";

                                table += row;
                                if (count == 1) {
                                    count = 2;
                                    rowclass = "ListingAlternativeItemHolder";
                                }
                                else {
                                    count = 1;
                                    rowclass = "ListingItemHolder";
                                }
                                programname2 = programname;
                            }
                        }
                    }
                    else {
                        if (type == search) {

                            var row = "<table width='97%' border='0' cellpadding='6' cellspacing='0'><tbody>";
                            if (count2 == 1) {
                                row += "</tr><tr><td height='5'></td></tr><tr>";

                                row += "<td class='ListingAlternativeItemHolder'><b style='font-size:12pt'>&nbsp;&nbsp;" + DisplayName + "</b></td>";
                                row += "</tr></tr><tr><td height='5'></td></tr>";
                                count2 = 2;
                            }
                            if (url == '') {
                                row += "<tr><td class='" + rowclass + "'> <span class='ListingNoLink'>" + programname + "</span></td></tr>";
                                row += "<tr><td class='" + rowclass + "'><div style='margin-left:15px;'><p>" + msg[i].Description + "</p></div></td></tr>";
                            }
                            else {
                                row += "<tr><td class='" + rowclass + "' onclick=\"ProgramSearchInsert('" + DisplayName + "', '" + url + "')\" > <span class='ListingLink'>" + programname + "</span></td></tr>";
                                row += "<tr><td class='" + rowclass + "'><div style='margin-left:15px;'><p>" + msg[i].Description + "</p></div></td></tr>";
                            }
                            row += "</tbody></table>";

                            table += row;
                            if (count == 1) {
                                count = 2;
                                rowclass = "ListingAlternativeItemHolder";
                            }
                            else {
                                count = 1;
                                rowclass = "ListingItemHolder";
                            }
                            programname2 = programname;
                        }
                    }
                }
            }
            $('#Programdiv').html(table);
        }
    }
}

