// JavaScript Document


		
/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&                                                                                  &&
  &&                                                                                  &&
  &&                           functions to enter numbers                             &&
  &&                                                                                  &&
  &&                                                                                  &&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  

//function is called after a digit is entered as the next entry after 'enter' has been hit
//function is called from runCalc(a)
function  num_after_enter(a)
{
	reset_sciNot();						//resets all values in the X register to defaults incase scintific notation
										//had been used for the entry
							
	document.faceForm.plusMinus.disabled = false;	//a number is being entered thus this is enabled
	
	numberArray[0].enterFlag = 'no';	//change because the last key entered was a digit, not 'enter'
	numberArray[0].stringValue = a;		//assigns the digit typed to the array
	numberArray[0].stringValue += "";	//converts value to string
	
	document.stackForm.rowX.value = numberArray[0].stringValue;				//displays the digit to the X register
	numberArray[0].decimalValue = parseFloat(numberArray[0].stringValue);	//converts the string value to float
																			//and assigns the value to the decimal value
				
	numberArray[0].shiftUp = "no";	//any subsequent digits in the X register do not cause the stack to shift	
	
	document.faceForm.whole.disabled = false;	//number entered is now able to be used as a whole part of a fraction
	document.faceForm.numer.disabled = false;	//number entered is now able to be used as a numerator
	
}

						
/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  
  
//function is called if its the first digit entered when the registers are clear
//or if the first digit after an operation has been performed
//function is called from runCalc(a)
function  first_digit(a)
{
	lift();		//the stack is pushed up and 'a' is assigned to the X register
	
	document.faceForm.plusMinus.disabled = false;	//a number is being entered thus this is enabled
	
	reset_sciNot();	
	numberArray[0].stringValue = a;
	numberArray[0].stringValue += "";	//converts value to string
			
	document.stackForm.rowX.value = numberArray[0].stringValue;		//assigns the digit to the X register
	numberArray[0].decimalValue = parseFloat(numberArray[0].stringValue);	//converts the string value to float
																			//and assigns the value to the decimal 
				
	numberArray[0].shiftUp = "no";	//any subsequent digits in the X register do not cause the stack to shift
	numberArray[0].singleOp = "no";	//
	
	document.faceForm.whole.disabled = false;	//number entered is now able to be used as a whole part of a fraction
	document.faceForm.numer.disabled = false;	//number entered is now able to be used as a numerator
}

						
/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  
  
//function is called for 2nd, 3rd digits etc.
//function is called from runCalc(a)
function  subsequent_digit(a)
{
	if(numberArray[0].flag == 'EE')
	{
		subsequent_digit_EE(a);
	}//end if
	else
	{
		if(numberArray[0].type == 'fraction')
		{
			subsequent_digit_fraction(a);
		}//end if
		else
		{
			subsequent_digit_decimal(a);
		}//end else
	}//end else
}//end function
	

						
/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  

//function is called for 2nd, 3rd digits etc. when in scientific notation mode
//function is called from subsequent_digit(a) which is called from runCalc(a)
function  subsequent_digit_EE(a)
{
	numberArray[0].stringValue += '';			//converts string to string value
	
	//I don't know why an if is necessary as this function is only called if its a scintific notation number
	if(numberArray[0].flag == 'EE')
	{
		numberArray[0].exponent += "";			//converts exponent value to string
		numberArray[0].exponent += a;			//concatonates 'a' to exponent
		parseFloat(numberArray[0].exponent);	//converts exponent value back to float
		
		numberArray[0].stringValue += a;
		document.stackForm.rowX.value = numberArray[0].stringValue;	
		
		numberArray[0].decimalValue = numberArray[0].coefficient * Math.pow(10,numberArray[0].exponent);
	}//end if
	else
	{
		//17 oct 2009 @05:33 - I don't believe this else is ever called
		alert('you are in the else part of subsequent_digit_EE(a) which is on the enterDigits.js page.\nI can not believe that this was called - if so investigate');
		
		numberArray[0].stringValue += a;		//concatonates 'a' to string

		numberArray[0].decimalValue = parseFloat(numberArray[0].stringValue);	//converts decimal value back to float
	
		document.stackForm.rowX.value = numberArray[0].stringValue;		//displays new string value in X register
	}//end else
}//end function


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  

//function is called for 2nd, 3rd digits etc. when in fraction mode
//function is called from subsequent_digit(a) which is called from runCalc(a)
function  subsequent_digit_fraction(a)
{
	document.faceForm.numer.disabled = false;	//can now enter numerator
	
	if(numberArray[0].flag == 'wholePartDone')
	{
		if(a == 0 || a == 1 || a == 2 || a == 3 || a == 4 || a == 5 || a == 6 || a == 7 || a == 8 || a == 9)
		{
			numberArray[0].decimalValue = numberArray[0].wholePart;
			numberArray[0].numerator += "";	//converts numerator value to string
			numberArray[0].numerator += a;	//concatonates 'a' to string
			numberArray[0].numerator = parseFloat(numberArray[0].numerator);

			numberArray[0].stringValue += a;
			document.stackForm.rowX.value = numberArray[0].stringValue;		//displays new string value in X register
		}
		else
		{
			alert(a + ' is not a legal entry - try again');
		}//end else
	}//end if
	else
	{
		if(numberArray[0].flag == 'numeratorPartDone')
		{
			numberArray[0].denominator += "";	//converts numerator value to string
			numberArray[0].denominator += a;	//concatonates 'a' to string
			numberArray[0].denominator = parseFloat(numberArray[0].denominator);
			numberArray[0].decimalValue = (numberArray[0].numerator + numberArray[0].denominator * numberArray[0].wholePart) / numberArray[0].denominator;
			
			numberArray[0].stringValue += a;
			document.stackForm.rowX.value = numberArray[0].stringValue;		//displays new string value in X register
			
			document.faceForm.numer.disabled = true;	//a number must be now entered to enable this
														//this was previously enabled to allow for fractions w/o whole parts
			document.faceForm.whole.disabled = true;	//a number must be now entered to enable this
														//this was previously enabled to allow for fractions w/o whole parts	
												
		disable_Delete_Insert();		//disable delete and insert buttons - not compatible with fractions
		}//end if
		else
		{
			if(numberArray[0].flag == 'noWholeNumber')
			{alert('how did you get here?  you are in function subsequent_digit_fraction(a) on the enterDigits.js page');
				numberArray[0].denominator += "";	//converts numerator value to string
				numberArray[0].denominator += a;	//concatonates 'a' to string
				numberArray[0].denominator = parseFloat(numberArray[0].denominator);
				numberArray[0].decimalValue = numberArray[0].numerator / numberArray[0].denominator;
				
				numberArray[0].stringValue += a;
				document.stackForm.rowX.value = numberArray[0].stringValue;		//displays new string value in X register
			}//end if
		}//end else	
	}//end else
}//end function


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/  


//function is called for 2nd, 3rd digits etc. when in decimal mode
//function is called from subsequent_digit(a) which is called from runCalc(a)
function  subsequent_digit_decimal(a)
{
	numberArray[0].stringValue += '';	//converts string to string value
	
	numberArray[0].stringValue += a;	//concatonates 'a' to string

	numberArray[0].decimalValue = parseFloat(numberArray[0].stringValue);	//converts decimal value back to float
	
	document.stackForm.rowX.value = numberArray[0].stringValue;				//displays new string value in X register
}//end function


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/  
    
  
//function takes user input and runs program - program is called for every keystroke
//entry of a digit - 'a' is the digit that was entered
function runCalc(a)
{
	enable_Delete_Insert();
	
	//if statement is called to insert a digit somewhere 'inside' the string of digits
	if(numberArray[0].insertFlag == 'yes')
	{
		insertChar(a);
	}
	else
	{
		//if is called if the last key stroke was 'enter'
		if(numberArray[0].enterFlag == 'yes')
		{
			num_after_enter(a);
		}//end if
		//else is called if anything other than 'enter' was entered
		else
		{
			//if the digit is the first entered in a sting the stacked is pushed up or the last thing
			//done was taking the reciprocal, sq. root, etc
			if(numberArray[0].shiftUp == "yes" || numberArray[0].singleOp == "yes")
			{
				first_digit(a);
			}//end if
		
			//else is called for the second digit, third digit, etc
			else
			{
				subsequent_digit(a);
			}//end else
		}//end else
	}//end else	
	
	if(numberArray[0].decimalValue % 1 == 0)
	{
		numberArray[0].type = 'whole';
	}
}//end function  
	
	
/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  
  
//function 'enters' number into the Y register after 'enter' key is hit
//function is called from main.html (enter button)
function HitEnter()
{
	if(numberArray[0].flag == 'EE')
	{
		document.stackForm.rowX.value = numberArray[0].stringValue;
		//numberArray[0].flag = 'none';
	}//end if
	else
	{
		if(numberArray[0].enterFlag == 'yes' || numberArray[0].shiftUp == "yes" || numberArray[0].singleOp == "yes")
		{
			//numberArray[0].stringValue = numberArray[0].decimalValue + '';	
			//numberArray[0].stringValue = numberArray[0].decimalValue + '';	
			
			//I can't explain it but for 'subsequent' or 'repeat' hits of enter the string value needs to
			//be reasigned - it shouldn't be necessary but there is a glitch somewhere and this fixes it
			
			//apparently its fixed - hooya:)															
		}//end if
	}//end else

	numberArray[0].enterFlag = 'yes';	//enter is last key entered
	lift();		//stack is pushed up but X register remains what the current X register is	

	numberArray[0].singleOp = "no";
	disable_Delete_Insert();	//the insert and delete buttons are disabled until digits are entered
	disableFractoids();			//fraction buttons are disabled until number is entered
//	reset_sciNot(); 	//aparently not needed
}//end function
						
