﻿// v1.0 022108 ap   - create scripts to access the testimonial xml file, 
//                  - access appropriate testimonial, then display into the correct div    
//                  - have a way to determine which testimonial to take OR perhaps
//                  - have a random testimonial show up?

// call XMLHTTPRequest
// load XML init 
// necessary functions
window.onload = function(){
    var url="lwRequired/testimonials.xml";
    init.run(loadXMLFromURL,url,initialize_page);

//    x = new XMLContainer();
//    x.searchHTML();
    
    
}
function initialize_page(xml){
    t = new testimonials();
    t.init(xml);
    
//    s = new XMLContainer();
//    s.searchHTML();
}
// todo: 
// 1. grab the appropriate testimonial
// 2. display into the page
// 3. randomize testimonials
// START Testimonials Object
function testimonials(){
    //this.length=this.init;
    this.length="";
    this.xml="";
    this.text=""
    this.contributor="";
    this.random=""
    
    this.init=function(xml){
        this.xml = xml;
        this.length = this.xml.getElementsByTagName('testimonial').length
        this.getRandomNumber();
        this.getText();
        this.writeToHtml();
    }
    this.getRandomNumber=function(){
        if (this.random!='undefined'){
            this.random=Math.floor(Math.random()*eval(this.length))
        }
    }
    this.getText=function(){
        this.xml=this.xml.getElementsByTagName('testimonial');
        //alert("["+this.xml[this.random].getAttribute('contributor')+"]")
        if (this.xml[this.random].getAttribute('contributor')!=''){
            this.contributor="<br /> ~ "+this.xml[this.random].getAttribute('contributor');
        }
        this.text=this.xml[this.random].firstChild.nodeValue
    }
    this.writeToHtml=function(){
        if (document.getElementById('testimonial').innerHTML==''){
            document.getElementById('testimonial').innerHTML = '___<br /><br />'+
            '"'+this.text+'"'+this.contributor
        }
    }
    
    this.showText=function(){alert("Text is :"+this.text)}
    this.showNumberOfTestimonials=function(){alert("Number of testimonials in xml file is: "+this.length+".")}
    this.showRandomNumber=function(){alert("Random number generated is: "+this.random)}
}
// END Testimonials Object