
//
//             level support
//
//    This file contains routines needed to support the concept of difficulty 
//    level selection menu.
//  
//    This file assumes that the user has defined level1, level2, level3, level4,
//    level5.  The number of levels used must be defined in the main program.  The number
//    of levels should be defined as 'levelCount'.
//    
//    
//    Also, this assumes that support data_setup1 through data_setup6 have been 
//    defined ... though they may be stubs.
//

    // 
    //  The following provide a method for supporting the setting and unsetting of colors on 
    //  labels.
    
    function setColor(item, color, bg) {
        item.style.color = color         ; // changes text color
        item.style.backgroundColor = bg  ; // changes background
    }

    function releaseColor(item) {
        if (selected_level == item) {
            item.style.backgroundColor = "blue"  ;
            item.style.color = "white" ; 
        } else {
            item.style.backgroundColor = "white" ;
            item.style.color = "blue" ;
        }
    }    
   
    //
    //   The following provide a method for requesting and setting up the database for problems at a
    //   specified difficulty level ... 0 (easiest) to 5(hardest)
    //
    function setup(x) {
    
        if (x == 0) {
            data_setup1()   ;
        }else if (x == 1) {
            data_setup2() ;
        } else if (x == 2) {
            data_setup3()  ;
        } else if (x == 3) {
            data_setup4() ;
        } else if (x == 4) {
            data_setup5() ;
        } else if (x == 5) {
            data_setup6() ;
	} else {
	    data_setup7() ;
	}
    }

    function select_the_level(the_level, color, bg) {
        i = 0 ;                   
        	
	while ((opts[i] != the_level) && (i < (levelCount+1))) i++ ;
        if (selected_level != the_level) {
            setColor(selected_level,"black","white") ;
        }

        selected_level = the_level ;
        setColor(selected_level,color, bg) ;
        setup(i) ;
    }
    
   //
   // The following set of methods support the ability to automatically advance from one
   // difficulty level to the next.
   //
   function level_init(x) {
        selected_level = level6 ;
        select_the_level(x,"white","blue") ;
    }
    var autoadvance_flipflop = 0 ;                // assume we will not auto increment difficulty
    
    function autoadvance_init() {
        autoadvance_flipflop = 0 ;
        setColor(auto,"blue","gray") ;
    }   

    function autoadvance_button() {
        if (autoadvance_flipflop == 0) {
            autoadvance_flipflop = 1 ;           // If not auto-incrementing, toggle to auto-increment difficulty.
            setColor(auto,"white","blue") ;
        } else {
            autoadvance_flipflop = 0 ;           // If auto-incrementing, toggle to stop auto-incrementing difficulty.
            setColor(auto,"blue","gray") ;
        }
    }

    function autoadvance() {
        //
        // This routine checks to see if more than 19 problems are correct and the percent
        // correct is at least 90%.    If we met the criteria, the system will advance to the
        // next level of difficulty.
        //

        if (autoadvance_flipflop == 1) {
            if (((document.math.correct.value%19) == 0) && (document.math.percent.value > 89)) {
                i = 0 ;
                while (((opts[i] != selected_level) && i < (levelCount+1))) i++ ;
                select_the_level(opts[++i], "white", "blue") ;
	    }
        }
    }



