
   //                                   Fraction Support
   //
   //    To use this support set, we must have the following defined:
   //    - the problem fraction1, fraction2,
   //    - the explanation fraction3, fraction4 with answer fraction5
   //    - the components ... whole1, whole2, whole3, whole4, whole5 
   //                         numerator1, numerator2, numerator3, numerator4, numerator5,
   //                         denominator1, denominator2, denominator3, denominator4, denominator5.
   //    - oper, oper2, equals.
   //    - document.math.frac_1_length, document.math.frac_2_length, document.math.frac_3_length
   //      document.math.frac_4_length, document.math.frac_5_length.
   //    - document.math.Answer,
   //    - a_num1A, a_num2A, a_num3A,
   //      a_num1B, a_num2B, a_num3B
   //      a_num3W, a_num3W, a_num3W
   //    - num1[], num2[], num3[]
   //    - current

    var prime = new Array(2,3,5,7,11,13,17,19,23,29,31,37,41) ;

    var timerA = new Timer(5, hidefractionlayer)      ;

    var fraction_1    = new fraction(1) ;
    var fraction_2    = new fraction(2) ;
    var fraction_3    = new fraction(3) ;
    var fraction_4    = new fraction(4) ;
    var fraction_5    = new fraction(5) ;
    var expression_1  = new expression(fraction_1, "oper", fraction_2) ;
    var explanation_1 = new explanation(fraction_3, "oper2", fraction_4, fraction_5) ;

    function hideCloud()  {
        hidefractionlayer()            ;
    }

    function showCloud() {
        showfractionlayer()            ;
        timerA.Start()         ;
    }
    
    function hidefractionlayer() {
        hideThought() ;
        if (IE) {

            document.getElementById('fraction3').style.display = "none";
            document.getElementById('fraction4').style.display = "none";
            document.getElementById('fraction5').style.display = "none";
            document.getElementById('oper2').style.display = "none";
            document.getElementById('equals').style.display = "none";

        } else if (NS4) {
            document.layers[fraction3].visibility = "hide";
            document.layers[fraction4].visibility = "hide";
            document.layers[fraction5].visibility = "hide";
            document.layers[oper2].visibility = "hide";
            document.layers[equals].visibility = "hide";
	    } else if (NS6) {
            document.getElementById([fraction3]).style.display = "none";
            document.getElementById([fraction4]).style.display = "none";
            document.getElementById([fraction5]).style.display = "none";
            document.getElementById([oper2]).style.display = "none";
            document.getElementById([equals]).style.display = "none";
        }
    }

    function showfractionlayer() {
        showThought() ;
        if (IE) {

            document.getElementById('fraction3').style.display = "";
            document.getElementById('fraction4').style.display = "";
            document.getElementById('fraction5').style.display = "";
            document.getElementById('oper2').style.display = "";
            document.getElementById('equals').style.display = "";

        } else if (NS4) {
            document.layers[fraction3].visibility = "show";
            document.layers[fraction4].visibility = "show";
            document.layers[fraction5].visibility = "show";
            document.layers[oper2].visibility = "show";
            document.layers[equals].visibility = "show";
        } else if (NS6) {
            document.getElementById([fraction3]).style.display = "block";
            document.getElementById([fraction4]).style.display = "block";
            document.getElementById([fraction5]).style.display = "block";
            document.getElementById([oper2]).style.display = "block";
            document.getElementById([equals]).style.display = "block";
        }
    }
 

    function fraction(id) {                        
        this.id     = id ;                                        
        this.length = 0  ;
    }

    function fraction.prototype.assign(whole, num, den) {

        wholes = new String(whole) ;
        num    = new String(num) ;
        den    = new String(den) ;
        wsize  = parseInt((whole == 0) ? 0 : wholes.length) ;
            
        this.length = (num.length < den.length) ? den.length : num.length ;
        this.length = parseInt(this.length) + wsize ;

        if (num.length < den.length) {
            for (i=num.length ; i < den.length+1 ; i++) {
                num += "&nbsp;" ;
            }
        }
        
        document.all["whole"+this.id].innerHTML       = (whole == 0) ? "" : wholes ;
        document.all["numerator"+this.id].innerHTML   = "" ;
        document.all["denominator"+this.id].innerHTML = "" ;

        for (i=0 ; i < wsize ; i++) {
            document.all["numerator"+this.id].innerHTML   += "&nbsp;&nbsp;" ;
            document.all["denominator"+this.id].innerHTML += "&nbsp;&nbsp;" ;
        }
        if (parseInt(num) > 0) {
            document.all["numerator"+this.id].innerHTML   += "<u>"+num+"</u>" ;
            document.all["denominator"+this.id].innerHTML += den ;
        } else {
            document.all["numerator"+this.id].innerHTML   += " " ;
            document.all["denominator"+this.id].innerHTML += " " ;
            if (whole == 0) {
                document.all["whole"+this.id].innerHTML = "&nbsp;0&nbsp;" ;
	    }
        }                     

    }

    function fraction.prototype.reduce() {
        var a = parseInt(this.num) ;
        var b = parseInt(this.den) ;
        var tempA             ;
        var tempB             ;
        var i                 ;
	       
        for (i = 0 ; prime[i] <= num ; i++) {
            tempA = a/prime[i] ;
            if ((tempA - Math.floor(tempA)) == 0) {
                tempB = b/prime[i] ;
                if ((tempB - Math.floor(tempB)) == 0) {
		    this.num = tempA ;
		    this.den = tempB ; 
                    this.reduce() ;    
                    return ;
                }
            }
        }
    }

    function fraction.prototype.width() {
        return (this.length) ;
    }

    //
    //   expression
    //
    //     - Fraction parameters must be the names of the defined variables, 
    //                               NOT the name of the layer that holds the fraction.
    //     - opr parameter must be the name of the layer holding the operating symbol.
    //
    //      example: fraction_1, fraction_2, fraction_3, fraction_4, fraction_5.
    //
    function expression(f1, opr ,f2) {                     
        this.f1  = f1 ;                           
        this.opr = opr ;
        this.f2  = f2 ;
    }

    //
    //  expression.prototype.position
    //
    //      - row and column location first character for the expression.  
    //
    function expression.prototype.position(x, y) {
        var obj      ;
        var oper_loc ;

        obj = document.getElementById("fraction"+this.f1.id) ;
        obj.style.left = x ;
        obj.style.top  = y ;
        obj = document.getElementById(this.opr) ;
        obj.style.left = x + this.f1.width()*20;
        obj.style.top  = y + 14 ;
        oper_loc = obj.style.left ;
        obj = document.getElementById("fraction"+this.f2.id) ;
        obj.style.left = parseInt(oper_loc) + 20 ;
        obj.style.top  = y ;

    }


    function explanation(f1, opr, f2 , f3) {
        this.f1  = f1  ;
	this.opr = opr ;
	this.f2  = f2  ;
        this.f3  = f3  ;
    }
    
    function explanation.prototype.position(x, y) {
        var obj        ;
        var oper_loc   ;
        var answer_loc ;
        var equal_loc  ;

        obj = document.getElementById("fraction"+this.f1.id) ;  
        obj.style.left = x ;
        obj.style.top  = y ;

        obj = document.getElementById(this.opr) ;
        obj.style.left = x + this.f1.width() * 20 ;

        obj.style.top  = parseInt(y) + 14 ;
        oper_loc = obj.style.left ;

        obj = document.getElementById("fraction"+this.f2.id) ;
        obj.style.left = parseInt(oper_loc) + 20 ;
        obj.style.top  = y ;

        equal_loc = parseInt(obj.style.left) + this.f2.width()*20 ;
        obj = document.getElementById("equals") ;
        obj.style.left = equal_loc ;
        obj.style.top  = parseInt(y) + 14 ;
        answer_loc = parseInt(equal_loc) + 20  ;

        obj = document.getElementById("fraction"+this.f3.id) ;
        obj.style.left = answer_loc ;
        obj.style.top  = y ;

    }
    
    function reduce(num,den) {
        var a = parseInt(num) ;
        var b = parseInt(den) ;
        var tempA             ;
        var tempB             ;
        var i                 ;
        
        a_num3A = num         ;
        a_num3B = den         ;
        
        for (i = 0 ; prime[i] <= num ; i++) {
            tempA = a/prime[i] ;
            if ((tempA - Math.floor(tempA)) == 0) {
                tempB = b/prime[i] ;
                if ((tempB - Math.floor(tempB)) == 0) {
                    reduce(tempA,tempB) ;    
                    return ;
                }
            }
        }
    }

    function frac_match(type, whole, num, den) {
        var status = 0 ;
        
        whole = parseInt(whole) ;
        num   = parseInt(num)   ;
        den   = parseInt(den)   ;
	WHOLE = String(a_num3W) ;

        if (type == 3) {
	      
            if (((whole - a_num3W) == 0) && ((num - a_num3A) == 0) && ((den - a_num3B) == 0)) {
                status = 1 ;
            }
	} else if (type == 2) {
            if (((num - a_num3A) == 0) && ((den - a_num3B) == 0)) {
                status = 1 ;
            }
	} else if (type == 1) {
            if ((whole - a_num3W) == 0) {
                status = 1 ;
            }
	    if ((WHOLE.length == 0) && ((whole - 0) == 0) && ((a_num3A - 0 ) == 0 )) {
                the_type = type ;
		status = 1 ;
	    }
        }
        if (the_type != type) {
            status = 0 ;
        }
	        
        return(status) ;
    
    }


    function a_question(num1W,num1A,num1B,num2W,num2A,num2B) {
        fraction_1.assign(num1W,num1A,num1B) ;
        fraction_2.assign(num2W,num2A,num2B)  ;
        expression_1.position(520, 290) ;
    }
    
  
    
    function an_explanation() {

        fraction_3.assign(a_num1W,a_num1A,a_num1B) ;
        fraction_4.assign(a_num2W,a_num2A,a_num2B) ;
        fraction_5.assign(a_num3W,a_num3A,a_num3B) ;
        explanation_1.position(120,260)       ;
        showfractionlayer()                 ;
    }

    function comment_assert(txt) {
        if (IE) {
            document.all["comment"].innerHTML = txt ;
        } else if (NS4) {
            document["comment"].document.write(txt);
            document["comment"].document.close();
        }         
    }    

    
    //
    //
    //    check
    //
    //

    function check() {
        var pattern     ;
        var entered     ;
        var explanation ;
        var expectation ;
        var type        ;
        var whole       ;
        var num         ;
        var den         ;
        var temp        ;
	
        pattern = /^([0-9]+)[ ]+([0-9]+)\/([0-9]+)|^([0-9]+)\/([0-9]+)|^([0-9]+)/ ;        
        entered     = document.math.Answer.value ;

        whole = "" ;
        num   = "" ;
        den   = "" ; 
        
        entered = new String(entered) ;
        temp = entered.match(pattern) ;

        type = 0              ;
        if (RegExp.$1 != "") {
            type = 3          ;
            whole = RegExp.$1 ;
            num   = RegExp.$2 ;
            den   = RegExp.$3 ; 
        } else if (RegExp.$4 != "") {
            type = 2          ;
            num   = RegExp.$4 ;
            den   = RegExp.$5 ;
        } else {
            type = 1          ;
            whole = RegExp.$6 ;
        }
        

	                              // If we have a student response submitted,
        if (clicked==0){
                                      // Determine if the answer equals the
                                      // expected value.
			      
            if ((entered != "") && (frac_match(type, whole, num, den))) {
                                      // If we match the expectation, 
                                      //    mark things correct,
                correct++; 
                document.math.correct.value = correct;
                showCheshire() ;      //    show the Cheshire cat.
                
            } else  {
                                      // Otherwise, mark things as wrong.
                if (entered == "") {
                    explanation = "Enter a number before pressing CHECK.  "
                    document.math.dcomment.value = "No value entered ..." ;
                    click = 1                                             ;
                    return ;
                } else  {
                    wrong++; 
                    document.math.dcomment.value = document.math.problem.value + " = " + expectation ;                  
		    showCloud()                        ;
		    explanation = an_explanation()     ;
                    showGarfield()                     ;
                    document.math.wrong.value   = wrong;
                }
		
                clicked                     = 1    ;
            }
                                     // Now determine total results checked
            total = correct + wrong ;
        
                                     // Determine the percent correct
            document.math.percent.value = Math.floor(correct/total*100) ;
                                     //    and go for the next problem.
            iterator();
        }

	     autoadvance() ;              // Check for a need to automatically advance to the next level of difficulty.
    }
    
        
    //
    //                              iterator
    //
    //    This provides a method for:
    //     1) Grabbing the next numbers to be displayed
    //     2) Determining the expected result.
    //     3) Blank the answer space
    //     4) Setting the focus to the answer
    //     5) Reset the random array if we have used up the array.
    //
    function iterator(){

        var tmp                     ;
        var numA1 = num1[current]   ;
        var numB1 = num2[current]   ;
        var numC1 = num3[current++] ;
        var numA2 = num1[current]   ;
        var numB2 = num2[current]   ;
        var numC2 = num3[current++] ;
                                             // If we have a decimal in the answer, use 2 digit fixed format, 
                                             //   otherwise us a whole number answer.
        
        

       if (numC1 < numC2) {
            tmp = numC2           ;
            numC2 = numC1         ;
            numC1 = tmp           ;
       } 
       if ((numC1 == numC2) || (numC1 == 0)) {
 
           if ( parseFloat(numA1/numB1) < parseFloat(numA2/numB2)) {
                                                                      //  swap(numA1, numA2) ;
               tmp = numA2        ;
               numA2 = numA1      ;
               numA1 = tmp        ;

                                                                      //  swap(numB1, numB2) ;
               tmp = numB2        ;
               numB2 = numB1      ;
               numB1 = tmp        ;                   
           }       
       }

        a_question(numC1,numA1,numB1,numC2,numA2,numB2) ;
        an_answer(numC1,numA1,numB1,numC2,numA2,numB2) ;
        clicked = 0 ;                        // Expect a key press.
   
        document.math.Answer.value = "";
        document.math.Answer.focus();        // Focus on the Answer box
        
        if (current > maxCount - 4){
            select_the_level(selected_level) ;
            current = 0;
        }
    }
    
    function init() {
    
        correct = "" ;
        wrong   = "" ;
        click   = 0  ;

        document.math.Answer.value      = "" ;
        document.math.correct.value     = "" ;
        document.math.wrong.value       = "" ;
        document.math.percent.value     = "" ;
        hidelayer("cheshire")            ;
        hidelayer("garfield")            ;
        hidelayer("thought")             ;
        comment_assert("&nbsp;&nbsp;Help Ernie solve his problem.") ;
        level_init(level1)               ;
        autoadvance_init()               ;
    }
    function run() {

        clicked = 1           ;
        iterator()            ;

    }

    //
    //                             strt
    //
    //    This provides a method for strting the problem
    //    quiz.   Starting involves:
    //    1) Initializing the random array
    //    2) Initializing to the first array element.
    //    3) clearing of the result, answer, and pctright
    //    5) Display the first problem.
    //
    function strt() {
        start_level = level6 ;   
        init()               ;
        run()                ;              // Run the body.
    }   

