
// *****************************************************************************
//
//                                   exponentialFactor
//
//    This represents a single factor of a term and its associated exponent.
//
// *****************************************************************************
    function exponentialFactor(x,p) {
        this.assign(x,p) ;
    }

    exponentialFactor.prototype.assign = function(x,p) {
        if ((x == undefined) || (x == null)) {
            x = "" ;
            p = 0  ;
        }
        this.bbase  = x ;
        this.ppower = p ;
    }

    exponentialFactor.prototype.base = function() {
        return this.bbase ;
    }

    exponentialFactor.prototype.power = function() {
        return this.ppower ;
    }
    
    exponentialFactor.prototype.inc = function(x) {
        if ((x == undefined) || (x == null)) {
            this.ppower ++   ;
        } else {
            this.ppower += x ;
        }
    }



// *****************************************************************************
//
//                                   factorList
//
//    This represents the list of factors for a term.
//
// *****************************************************************************
    function factorList() {
        this.cnt       = 0                  ; // No factors to start
        this.arrayList = new Array()        ; // Space for factors.
    }

    factorList.prototype.add = function(obj) {
        this.arrayList[this.cnt] = new exponentialFactor()       ;
        this.arrayList[this.cnt].assign(obj.base(),obj.power())  ;
        this.cnt ++                         ; // New factor added to the list    
    
    }
    
    factorList.prototype.item = function(x) { // Return a specific factor
        return this.arrayList[x] ;               
    }
    
    factorList.prototype.count = function() {
        return (this.cnt)          ;
    }
    
    factorList.prototype.inc = function (x) {
        (this.arrayList[x]).inc() ;
    }   

    factorList.prototype.reset = function () {
        for (i=this.cnt ; i>0 ; i--) {
            delete this.arrayList[i-1] ;
	}
	this.arrayList = new Array() ;
	this.cnt = 0 ;
    }

 
// *****************************************************************************
//
//                                   Term
//
//    This associates the factored and expanded versions of a term that may
//
// *****************************************************************************
   function Term(x) {                             // Construct a list.
        this.str = ""                        ;
        this.list         = new factorList() ;    // this.list is a list of all factors.
        this.factorCount  = 0                ;
        if ((x == undefined) || (x == null)) {
            x = ""                           ;
        }
        this.assign(x)                       ;
    }
    
    Term.prototype.assign = function(x) {       // Assign the data to a list.
        this.str     = x                     ;
	this.factorCount = 0                 ;
    }

    Term.prototype.evaluate = function() {
        var i                                ;
        var str = new String()               ;
        var f = new factorList()             ;
        var sp = new exponentialFactor()     ;
        var c = new exponentialFactor()      ;
        if ((this.str.length == 0) && (this.factorCount > 0)) {
            alert ("BBBB") ;
        } else if ((this.str.length > 0) && (this.factorCount == 0)) {
            for (i = 0 ; i < this.str.length; i++) {
                tmp = this.str.charAt(i) ;
                if (i >= 0) {
                    if (tmp != " ") {            // If we find a character,
                                                 // check to see if we have already seen
                                                 // the character.  If we have seen the character
                                                 // before, bump the count of the number of times
                                                 // we have seen the character.
                                                 // If we have never seen the character,
                                                 // add it to the list of factors.
                        for ( j = 0 ; j <= this.factorCount-1 ; j++) {
                            sp = (this.list).item(j) ;                         // get a specific factor
                            str = sp.base() ; 
                            if (tmp == str) {
                                sp.inc() ;
                                break ;
                            } else {
                            }
                        }
                        if (j >= this.factorCount) {
                            c.assign(tmp,1) ;
                            this.list.add(c) ;
                            this.factorCount++   ;
                        }
                    } else {
                    }
                } else {
                                                  // If we have never seen any characters before
                                                  // and this is not a blank character, start
                                                  // our list of factors.
                    if (tmp != " ") {
                        c.assign(tmp,1)             ;
                        this.list.add(c)            ;
                        this.factorCount = 1        ;
                    }
                }
            }
        }
        for (i = 0 ; i < this.factorCount ; i++) {
            sp = this.list.item(i) ;
        }
    }

    Term.prototype.clear   = function() {
        this.str = ""           ;  // Clear the expanded form
        this.list.reset()       ;  // Empty the factor list.
	this.factorCount = 0    ;  // Reset to no factors.

    }

    Term.prototype.reduced = function() {
        var i                   ;
	var str = new String()  ;
	
	str = ""                ;
	
        if (this.factorCount < 1) {
            document.write("<b>No factors to print.</b>") ;
        } else {
            for (i = 0 ; i < this.factorCount; i++) {
                str += ((this.list).item(i)).base() + "<sup>" + ((this.list).item(i)).power() + "</sup>" ; 
            }
        }
        return str ;
    }

    Term.prototype.expanded = function() {
        return(this.str) ;
    }

    Term.prototype.reset = function () {        // Reset the sequential list retrieval.
        this.count = 0                     ;
    }

    Term.prototype.item    = function (i) {     // Select a particular item from a list.
        return (this.arrayList[i])         ;
    }

    Term.prototype.eof     = function()   {     // Determine if more characters exist in list.
        return (isNaN(this.list[this.factorCount])) ;
    }
    


