﻿var facultyID;

//to get the contents of the xml file
//http://www.rodsdot.com/ee/readFileClientSide.asp
// 
//create the Cross-browser XMLHttpRequest object
function getFile(pURL, pFunc, ID) {
    facultyID = ID;

    if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc
        xmlhttp = new XMLHttpRequest();
        eval('xmlhttp.onreadystatechange=' + pFunc + ';');
        xmlhttp.open("GET", pURL, true); // leave true for Gecko
        xmlhttp.send(null);
    } else if (window.ActiveXObject) { //IE 
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        if (xmlhttp) {
            eval('xmlhttp.onreadystatechange=' + pFunc + ';');
            xmlhttp.open('GET', pURL, false);
            xmlhttp.send();
        }
    }
    else {
        alert("your browser can't handle this script");
    }

}

function getQuotes() {
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
            var doc = document;
            var quoteBackground = doc.getElementById("divQuoteBackground");
            var facQuotes = doc.getElementById("divFacultyQuotes");
            var divQuotes = doc.getElementById("divQuotes");

            var xslString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
                            "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\" exclude-result-prefixes=\"msxsl\">" +
                            "	<xsl:output method=\"html\" indent=\"yes\" />" +
                            "	<xsl:template match=\"quotes\">" +
                            "		<xsl:for-each select=\"faculty\">" +
                            "			<xsl:if test=\"id = " + facultyID + "\">" +
                            "				<xsl:copy-of select=\"quote\" />" +
                            "			</xsl:if>" +
                            "		</xsl:for-each>" +
                            "	</xsl:template>" +
                            "</xsl:stylesheet>";

            var sInnerHTML = "";

            //create the XML and xsl objects

            //Internet Explorer
            if (window.ActiveXObject) {
                objXmlDoc = new ActiveXObject("Msxml2.DOMDocument");
                objXmlDoc.async = "false";
                objXmlDoc.loadXML(xmlhttp.responseText);

                objXslDoc = new ActiveXObject("Msxml2.DOMDocument");
                objXslDoc.async = "false";
                objXslDoc.loadXML(xslString);

                var htmlParts = objXmlDoc.transformNode(objXslDoc).split("</quote>"); //.replace("<quote>", "<p>").replace("</quote>", "</p>");
                var htmlPartsLength = htmlParts.length - 1;

                sInnerHTML = ""

                for (var i = 0; i < htmlPartsLength; i++) {
                    if (i % 2 == 0) {
                        sInnerHTML += "<p class=\"altP\">" + htmlParts[i].replace("<quote>","") + "</p>";
                    }
                    else {
                        sInnerHTML += "<p class=\"regP\">" + htmlParts[i].replace("<quote>", "") + "</p>";
                    }
                }
               
            }
            //Firefox, opera, ect.
            else if (document.implementation && document.implementation.createDocument) {
                parser = new DOMParser();
                objXmlDoc = parser.parseFromString(xmlhttp.responseText, "text/xml");

                objXslDoc = parser.parseFromString(xslString, "text/xml");

                xsltProcessor = new XSLTProcessor();
                xsltProcessor.importStylesheet(objXslDoc);

                xmlContent = xsltProcessor.transformToFragment(objXmlDoc, document);
                var xmlContentLength = xmlContent.childNodes.length;

                sInnerHTML = "";
                for (var i = 0; i < xmlContentLength; i++) {
                    if(i % 2 == 0)
                    {
                        sInnerHTML += "<p class=\"altP\">" + xmlContent.childNodes[i].innerHTML + "</p>";
                    }
                    else
                    {
                        sInnerHTML += "<p class=\"regP\">" + xmlContent.childNodes[i].innerHTML + "</p>";
                    }
                }

            }

            divQuotes.innerHTML = sInnerHTML;

            //show quotes

            facQuotes.style.visibility = "visible";
            facQuotes.style.display = "inline";

            quoteBackground.style.visibility = "visible";
            quoteBackground.style.display = "inline";
        }
    }
}

function closeQuotes() {
    var doc = document;
    var quoteBackground = doc.getElementById("divQuoteBackground");
    var facQuotes = doc.getElementById("divFacultyQuotes");

    facQuotes.style.visibility = "hidden";
    facQuotes.style.display = "none";

    quoteBackground.style.visibility = "hidden";
    quoteBackground.style.display = "none";
}