﻿// loads a testimonial for display on the page
// method parameter is optional, if no specific testimonial specified then a random testimonial is selected
function LoadTestimonial( iTestimonialIndex )
{
    xmlDoc = LoadXmlDoc( "testimonials.xml" );
    
    if( xmlDoc == null )
    {
        return;
    }
    
    xmlTestimonials = xmlDoc.getElementsByTagName( "testimonial" );
    
    if( iTestimonialIndex == null )
    {
        iTestimonialIndex = Math.floor( Math.random() * xmlTestimonials.length );
    }
    
    contact = xmlTestimonials[ iTestimonialIndex ].getElementsByTagName( "contact" )[ 0 ];
    snippet = xmlTestimonials[ iTestimonialIndex ].getElementsByTagName( "snippet" )[ 0 ];
    
    document.getElementById( "TestimonialText" ).innerHTML = snippet.childNodes[0].nodeValue;
    document.getElementById( "TestimonialFullArticle" ).href = 
        snippet.attributes.getNamedItem( "fullArticleLink" ).nodeValue;
    
    document.getElementById( "TestimonialContact" ).innerHTML = 
        contact.attributes.getNamedItem( "name" ).nodeValue;
    document.getElementById( "TestimonialCompany" ).innerHTML = 
        contact.attributes.getNamedItem( "company" ).nodeValue;
    document.getElementById( "TestimonialCompany" ).href = 
        contact.attributes.getNamedItem( "companyLink" ).nodeValue;
    
    SetTestimonialNextLink( iTestimonialIndex, xmlTestimonials.length );
}

// sets the next testimonial links relative to the specified testimonial
function SetTestimonialNextLink( iTestimonialIndex, iNumberOfTestimonials )
{
    if( iTestimonialIndex + 1 < iNumberOfTestimonials )
    {
        iNextIndex = iTestimonialIndex + 1;
    }
    else
    {
        iNextIndex = 0;
    }
    
    document.getElementById( "TestimonialNext" ).href = "javascript:LoadTestimonial( " + iNextIndex + " );";
}

// generic function to load an XML document from a file
function LoadXmlDoc( strFileName )
{
    try //Internet Explorer
    {
        xmlDoc = new ActiveXObject( "Microsoft.XMLDOM" );
    }
    catch(e)
    {
        try //Firefox, Mozilla, Opera, etc.
        {
            xmlDoc = document.implementation.createDocument( "", "", null );
        }
        catch(e) 
        {
            return null;
        }
    }
    try 
    {
        xmlDoc.async = false;
        xmlDoc.load( strFileName );
    }
    catch(e) 
    {
        return null;
    }

    return xmlDoc;
}