﻿
function setTextimonial(destinationDivId, categoriesString) {
    var destinationDiv = document.getElementById(destinationDivId);
    var destinationPic = destinationDiv.getElementsByTagName("img").item(0);
    var categories = categoriesString.replace(", ", ",").split(",");
    var xmlDoc = xmlInit("/xml/textimonials.xml");
    var randomCategoryChoice = Math.floor(Math.random() * categories.length);
    var xmlCategories = xmlDoc.getElementsByTagName("Category");
    var xmlCategoryIndex;
    var xmlTextimonials;
    var randomTextimonialChoice;
    if (destinationPic == null) { //if the div doesn't already have an image to change, then add the image
        destinationPic = document.createElement("img");
        destinationDiv.appendChild(destinationPic);
    }
    for (xmlCategoryIndex = 0; xmlCategoryIndex < xmlCategories.length; xmlCategoryIndex++) {
        if (xmlCategories.item(xmlCategoryIndex).getAttribute("type") == categories[randomCategoryChoice]) {
            textimonials = xmlCategories.item(xmlCategoryIndex).getElementsByTagName("Textimonial");
            randomTextimonialChoice = Math.floor(Math.random() * textimonials.length);
            destinationPic.src = textimonials.item(randomTextimonialChoice).getElementsByTagName("Image").item(0).childNodes.item(0).nodeValue;
            destinationDiv.onclick = function() {
                setTextimonial(destinationDivId, categoriesString);
            };
            break;
        }
    }
}

