/*function list
function Clicked_btnBLS
function Round_1_Place(x) 
function Get_One_BLS_Report
function Nrand
function Get_Text
function Give_Me_An_X
function Get_New_Indicators() {
function Get_New_True_Job_Change_Value
function Clicked_btnGet_New_Indicators
function Clicked_btnGet_Actual_Indicators()
function Clicked_btnHint_for_Guessing_BLS_Report
function Clicked_btnReset
function Fix_How_Close
function Get_User_Guess_on_BLS_Change
function Calculate_Score_This_Time
function Clicked_btnGet_Balance
function Clicked_btnMore_LEI
function Clicked_btnMore_Cons_Sentiment
function Clicked_btnMore_ISM
function Clicked_btnMore_New_Claims
function Pattern_Alert_Message
function Clicked_btnQuickstart
function Fixup_Textboxes_for_Reset
*/

//This stuff should execute before the html is rendered.

//Actual most recent values 
var LEI_Change_Recent       =  -0.8;    //updated 01/08/2008 per Jeff
var Cons_Sentiment_Recent   =  60.1;   	//updated 01/08/2008 per Jeff
var ISM_Recent              =  32.4; 	//updated 01/08/2008 per Jeff
var New_Claims_Recent       =  556;		//updated 01/08/2008 per Jeff   

var LEI_Change_SD         =  Math.abs(LEI_Change_Recent/10);
var Cons_Sentiment_SD     =  Math.abs(Cons_Sentiment_Recent/10);
var ISM_SD                =  Math.abs(ISM_Recent/10);
var New_Claims_SD         =  Math.abs(New_Claims_Recent/10);

/*
document.writeln ("<br>LEI_Change_Recent     / SD: " +  LEI_Change_Recent + " / " + LEI_Change_SD );
document.writeln ("<br>Cons_Sentiment_Recent / SD: " +  Cons_Sentiment_Recent + " / " + Cons_Sentiment_SD  );
document.writeln ("<br>ISM_Recent            / SD: " +  ISM_Recent            + " / " + ISM_SD             );
document.writeln ("<br>New_Claims_Recent     / SD: " +  New_Claims_Recent     + " / " + New_Claims_SD      );
*/

var LEI_Change_B         =   0 ;
var Cons_Sentiment_B     =   3.189516 ;  //updated 11/29/2006
var ISM_B                =   8.776096 ;  //updated 11/29/2006
var New_Claims_B         =  -1.922859 ;  //updated 11/29/2006
var Reg_Constant         =   9.152959 ;  //updated 11/29/2006 

var LEI_Change_Value=0;
var Cons_Sentiment_Value=0;
var ISM_Value=0;
var New_Claims_Value=0;

var True_Jobs_Change_Value = 0;

var dummy;

var Num_Clicks = 0;

var How_Many = 0;

var Validated_User_Guess;

var BLS_Report_Result;

var User_Score_This_Time;

var User_Balance= 100000;

var Alive = 1;

function Clicked_btnBLS() {

  //what: process click on main Go button
  
  //lets make this into a calling program
  
  // first check to see if there is a legal guess
  
  var valid_guess;
  
  valid_guess = Get_User_Guess_on_BLS_Change();

  if (Alive > 0)
  {
  
  if ( valid_guess > 0 )  
      {
      //There is a valid guess so continue  
      //document.write ("\nIn Clicked_btnBLS looks like I got a 1. ");
      
      // get one bls report and put answer in global var;
            
      BLS_Report_Result = Get_One_BLS_Report(); 
      
      Calculate_Score_This_Time();

      if (User_Balance < 0) 
      
          {
          Alive = 0;
      
          var s;

          s = "GAME OVER!  BUSTED! You lasted for " + Num_Clicks + " plays. "
          s += "Click 'Reset Form' to play again."
      
          alert(s);

          }
      
      }
 
  else

    {
    //there is no valid guess so break out somehow
    //document.write ("\nIn Clicked_btnBLS looks like I got a NOT 1. ");
    dummy = 1;
    }
    }

    else

        {

        //not alive

        var s;

        s = "GAME OVER!  BUSTED! You lasted for " + Num_Clicks + " plays. "
        s += "Click 'Reset Form' to play again."
      
          alert(s);
 
        }
  
  }

function Round_1_Place(x) 

{

    //What: round input x to one decimal place
    
    var answer;
    
    answer = x * 10 ;
    
    answer  =   Math.round(answer) ;

    answer = answer / 10;

  return answer;

}

function Get_One_BLS_Report() 

{

  //What: get one BLS report and return the answer
  
  var answer;
  var xmean;
  var sd;

 
  xmean =True_Jobs_Change_Value ; 

  sd = 60;   

  var x;

  x =   (Nrand(xmean,sd)) ;

  x =   Round_1_Place(x) ;

   
  //alert("btnBLS -- xmean sd nrand: " + xmean + " " + sd + " " + x);
         
  document.frmJobs.txtActual_BLS_Change.value = x;

  Num_Clicks++;

  Fix_How_Close();

  answer = x;
  
  return answer;
  
}


function Nrand(xmean,sd) {

  //return a random normal deviate with mean and sd (mean,sd)

  // Doc the source of the open-source algo please  

  // "Math" needs a cap "M" 

  var x;
 
  x = -6;
  
  for (var i = 1; i < 13; i++)
  {

   x = x + Math.random();
   
   //document.write ("<br/>i and x so far: " +i +" " + x);

  }
  
  x = xmean + (sd * x);
  
  return x;  

}

function Get_Text(x,min,max) {

  //x=receive a textbox value
  //if not a number return 0
  //if number set to min, max, or return if in range
  
  if (isNaN(x)) {
  
    x=0; 
  
  }
  
  else if (x < min) {
  
    x = min;
  
  }
  
  else if (x > max) {
  
    x = max;
    
  }
  
  else {
  
   // x is fine, just return it
     
  }  

 return x;
  
}



function Give_Me_An_X() {

  //return a random integer between 0 and 9
  
  var x;

  // "Math" needs a cap "M" 
  
  x = (Math.random()*10) - .5 ;

  x=Math.round(x);


  return x;  

}




function Get_New_Indicators() {

  //get new indicators of change in jobs


    LEI_Change_Value=(Nrand( LEI_Change_Recent, LEI_Change_SD ));
    LEI_Change_Value =   Round_1_Place(LEI_Change_Value) ;

    Cons_Sentiment_Value=(Nrand( Cons_Sentiment_Recent, Cons_Sentiment_SD ));
    Cons_Sentiment_Value=Round_1_Place(Cons_Sentiment_Value) ;

    ISM_Value=(Nrand( ISM_Recent, ISM_SD ));
    ISM_Value=Round_1_Place(ISM_Value) ;

    New_Claims_Value=(Nrand( New_Claims_Recent, New_Claims_SD ));
    New_Claims_Value=Round_1_Place(New_Claims_Value) ;

}


function Get_New_True_Job_Change_Value() {

  //get new predicted job change value == predicted score


    True_Jobs_Change_Value =  Cons_Sentiment_Value * Cons_Sentiment_B
                            + ISM_Value            * ISM_B
                            + New_Claims_Value     * New_Claims_B
                            + Reg_Constant; 

}


function Clicked_btnGet_New_Indicators() {
  

    //What: Get new indicators and fill inside info textboxes: BLS predictors
    //      Also get calc new true job change value from the new indicators

    Get_New_Indicators();
    
    Get_New_True_Job_Change_Value();

    document.frmJobs.txtLEI.value = LEI_Change_Value;

    document.frmJobs.txtCons_Sentiment.value = Cons_Sentiment_Value;

    document.frmJobs.txtISM.value = ISM_Value;

    document.frmJobs.txtNew_Claims.value = New_Claims_Value;

       
}


function Clicked_btnGet_Actual_Indicators() {
  

    //What: Get actual recent indicators and fill inside info textboxes
    //      Also get calc new true job change value from the new indicators
     
    
    LEI_Change_Value      =  LEI_Change_Recent;
    Cons_Sentiment_Value  =  Cons_Sentiment_Recent;
    ISM_Value             =  ISM_Recent;
    New_Claims_Value      =  New_Claims_Recent;

    Get_New_True_Job_Change_Value();

    document.frmJobs.txtLEI.value = LEI_Change_Value;

    document.frmJobs.txtCons_Sentiment.value = Cons_Sentiment_Value;

    document.frmJobs.txtISM.value = ISM_Value;

    document.frmJobs.txtNew_Claims.value = New_Claims_Value;

       
}


function Clicked_btnHint_for_Guessing_BLS_Report() {

  
  //what: Give hint for guessed bls change

  var s;

s=   "Your research advisor has just handed you an unpublished research ";
s=s+ "report which suggests that you can make a reasonable prediction about what ";
s=s+ "the next BLS jobs report will show from these four indicators. ";
s=s+ "Job growth increases with increasing LEI, consumer sentiment, and ISM. ";
s=s+ "Job growth decreases with increasing unemployment claims. ";
s=s+ "\n\n In recent years, BLS has reported job growth between -330 and +506 ";
s=s+ "or between a loss of 330,000 jobs and a gain of 506,000 jobs per month. ";
s=s+ "The mean BLS job growth report is 135 or a gain of 135,000 jobs per month. ";
s=s+ "\n\n Good luck!";
            
  alert(s);
       
}

function Clicked_btnReset() {
 
//what: Reset the form

Num_Clicks=0;

How_Many = 0;

User_Balance= 100000;

Alive = 1;

//Get new indicators and a new true job change value

Clicked_btnGet_New_Indicators();


//Note: using type=button rather than type=reset to control the action
//      However, this means you have to explicitly set EVERY box



    Fixup_Textboxes_for_Reset();


}


function Fix_How_Close()
    {

    //What: fix text box for how close is the guess

    var how_close;
    var how_close_long;

    var guess;
    var actual;
    var diff;
    var msg = "";

    guess = parseFloat(document.frmJobs.txtGuessed_BLS_Change.value);

    actual= parseFloat(document.frmJobs.txtActual_BLS_Change.value);

    if (!(isNaN(guess) || isNaN(actual)))

        {

        //both are numbers

        how_close = Math.abs(actual - guess);
        how_close = Round_1_Place(how_close) ;

        how_close_long = how_close * 1000;
  

        if (guess == actual) 
       
        { msg = "Excellent! Your guess is exactly right."; }
  



        else if (guess > actual )
            {
            msg = "Your guess is HIGH by " + how_close + 
            ".  ( " + how_close_long + " jobs)";
            }
  
        else  
            {   
            msg = "Your guess is LOW by " + how_close + 
            ".  ( " + how_close_long + " jobs)";
            }


      
        document.frmJobs.txtHow_Close.size = msg.length;
        document.frmJobs.txtHow_Close.value = msg;

        }

    else 

        {
        //one or the other things is not a number

        //document.frmJobs.txtHow_Close.size = 1;

        //document.frmJobs.txtHow_Close.value = "";
        
        }


    }


function Get_User_Guess_on_BLS_Change()

{

//what:  get user guess about BLS change
//return: answer = 0 if no valid guess was made
//return: answer = 1 if a valid guess was made
//        and fill global variable Validated_User_Guess

  var answer = 0;
  
  var user_guess;

  var s;

  var minChange = -1000;
  
  var maxChange = 1000;
  
  user_guess = parseFloat(document.frmJobs.txtGuessed_BLS_Change.value);

  if (isNaN(user_guess) ) 

      {

        s = "The value on the form for your guess ";
        s += "cannot be interpreted as a number. ";
        s += "\n You have to make a guess to play. ";
        s += "\n For example, enter 0 if you think the next BLS job report will ";
        s += "show no seasonally-adjusted change in jobs. ";
        s += "\n Or enter 100 if you think BLS will report an increase of 100,000 ";
        s += "jobs.  \n Or enter - 100 if you think BLS will report a DECRASE of ";
        s += "jobs.";

        answer = 0;        
           
        alert(s);

        document.frmJobs.txtGuessed_BLS_Change.value = "";
        
      }

  else if (user_guess < minChange || user_guess > maxChange ) 

      {

        s =  "The value on the form was too high or too low to be meaningful." ;
        s += "\nYour guess must be between " + minChange + " ";
        s += "and " + maxChange + " ";
        s += "\nFor example, enter 0 if you think the next BLS job report will ";
        s += "show no seasonally-adjusted change in jobs, ";
        s += "\nor enter 100 if you think BLS will report an increase of 100,000 ";
        s += "jobs.  \n or enter - 100 if you think BLS will report a DECRASE of ";
        s += "100,000 jobs, ";
        s += "and so on.";


        alert(s);

        document.frmJobs.txtGuessed_BLS_Change.value = "";

        answer = 0;        
        
      }

  else

      {

        //normal result leave answer in text box and continue
        
        //s = "Wow! looks like a normal answer";

        //alert(s);

        answer = 1;        

        Validated_User_Guess = user_guess;

      }

return answer;
    
}

function Calculate_Score_This_Time()

{

    // calculate the score and update txtYour_Score, txtBalance and txtBalance_2
    //scoring algo: (35 - abs(dif))/10 * bet
    //start with a bet of 100
    //maybe change this later, possibly allow user to change bet size

    var bet = 1000;

    var scaling_factor = 3.5;

    var score;

    //var push_point = 35;

    var dif;

    var user_guess;

    var bls;

    user_guess = Validated_User_Guess;

    bls = BLS_Report_Result;

    dif = Math.abs(user_guess - bls) ;

    //august 2006 score
    //score = ((push_point - dif)/10) * bet;

    dif += 1;  //avoid div by zero

    score = (bet/dif) * scaling_factor;

    User_Score_This_Time = Math.round(score);

    User_Balance += score;

    User_Balance = Math.round(User_Balance);

    document.frmJobs.txtYour_Score.value=User_Score_This_Time;

    document.frmJobs.txtBalance.value=User_Balance;

    document.frmJobs.txtBalance_2.value=User_Balance;

    document.frmJobs.txtNum_Plays.value=Num_Clicks;

    document.frmJobs.txtBalance.size = 20 ;

    document.frmJobs.txtActual_BLS_Change.size = 20 ;

    document.frmJobs.txtYour_Score.size = 20 ;

    document.frmJobs.txtBalance_2.size = 20 ;

    document.frmJobs.txtNum_Plays.size = 20 ;

}

function  Clicked_btnGet_Balance()

{

//what:  update text box from global var;

document.frmJobs.txtBalance.value = User_Balance;
document.frmJobs.txtBalance.size = 20;
    
}



function Clicked_btnMore_LEI() {
 
  //what: Give more info about: LEI

  var s;

    s=   "Leading economic indicators: change from previous month. - This is a composite of ten indicators. It was ";      
    s=s+ "originally calculated by the federal government and was thought to be a concurrent ";
    s=s+ "indicator, despite the name. (They also did concurrent indicators, which were ";
    s=s+ "actually lagging, and lagging indicators, which REALLY lagged). In late 1995 ";
    s=s+ "The Conference Board took over the calculation of the LEI and they made some ";
    s=s+ "changes. They claim that it has a leading quality. ";
    s=s+ "\n The range in recent years (monthly change) is -1.2 to 1.8, with a mean of .3";


    alert(s);
       
}


function Clicked_btnMore_Cons_Sentiment() {
  
  //what: Give more info about: Consumer Sentiment

  var s;

    s=   "University of Michigan Consumer Confidence Index -- Some question whether this ";
    s=s+ "is leading or concurrent, but it has a 'future' component. The scale has not ";
    s=s+ "changed over time. ";
    s=s+ "\n The range in recent years is 74.2 to 112, with a mean of 96.";
           
    alert(s);
       
}

function Clicked_btnMore_ISM() {

  
  //what: Give more info about: ISM

  var s;

    s=   "ISM manufacturing survey. This is a diffusion index, generated through a survey. ";
    s=s+ "Purchasing managers are asked questions about orders, future orders, etc. ";
    s=s+ "Anything above 42.7 is supposed to be a growing economy. There are a few studies ";
    s=s+ "that predict GDP growth from this, where a 50 reading is real growth of about 2.5%. ";
    s=s+ "A reading of 51.5 is consistent with 3.1% growth. ";
    s=s+ "\n The range in recent years is 40.5 to 63.2, with a mean of 53.1";
           
    alert(s);
       
}


function Clicked_btnMore_New_Claims() {

  
  //what: Give more info about: New unemployment claims

  var s;

    s=   "Initial jobless claims --- This is the most up-to-the minute info. We have taken ";
    s=s+ "the average of the four or five preceding weeks to make this into monthly data. ";
    s=s+ "Some economists suggest that these have to hit 400K or so before we are in ";
    s=s+ "recession territory. It seems pretty obvious that this has something to do with ";
    s=s+ "payroll employment. ";
    s=s+ "\n The range in recent years is 269.8 to 471.8, with a mean of 345.3.";
           
    alert(s);
       
}


function Clicked_btnMore_Indicators() {

  
  //what: Give more info about: 2 buttons for more indicators
  

  var s;

    s=   "Click 'Get New Game Indicators' to play the game again with different values.";
    s=s+ "\n Click 'Get Actual Indicators' to play the game with actual recent values";
    s=s+ "\n Click 'Reset Form' completely reset the game including your initial balance.";
           
    alert(s);
       
}


function Pattern_Alert_Message() {
  
  //what: Pattern for alert message -- as an onclick function for example

  var s;


    s=   "put your text between quotes ";
    s=s+ " ";
    s=s+ " ";
    s=s+ " ";
    s=s+ " ";
    s=s+ " ";
    s=s+ " ";
    s=s+ " ";
    s=s+ " ";
    s=s+ " ";
    s=s+ " ";
    s=s+ " ";
    s=s+ " ";
    s=s+ " ";

           
  alert(s);
       
}

function Clicked_btnQuickstart() {

    //What: quick start rules
  var s;


    s=   "1. Enter guess at 'Guess HERE:====>'.";
    s=s+ "\n 2. Click 'BLS'.";
    s=s+ "\n 3. To replay with different values click 'Get New Game Indicators '";
    s=s+ "or 'Get Actual Indicators'";
    s=s+ "\n 4. To completely reset the game and start over, click 'Reset'.";
           
  alert(s);

}

function Fixup_Textboxes_for_Reset() 

{

    // blank the user guess
    
    document.frmJobs.txtGuessed_BLS_Change.value = "";


    //fixup boxes which are initially blank 
    // Make them appear to be non-blank and shrink them

    document.frmJobs.txtBalance.size = 1 ;

    document.frmJobs.txtHow_Close.size =1;

    document.frmJobs.txtActual_BLS_Change.size = 1 ;

    document.frmJobs.txtYour_Score.size = 1 ;

    document.frmJobs.txtBalance_2.size = 1 ;

    document.frmJobs.txtNum_Plays.size = 1 ;

    var stars = "****";
    
    document.frmJobs.txtBalance.value=stars ;

    document.frmJobs.txtHow_Close.value=stars ;

    document.frmJobs.txtActual_BLS_Change.value=stars ;

    document.frmJobs.txtYour_Score.value=stars ;

    document.frmJobs.txtBalance_2.value=stars ;

    document.frmJobs.txtNum_Plays.value=stars ;

     //indicators

     document.frmJobs.txtLEI.size = 10 ;

     document.frmJobs.txtCons_Sentiment.size = 10 ;

     document.frmJobs.txtISM.size = 10 ;

     document.frmJobs.txtNew_Claims.size = 10 ;

}