// JavaScript Document


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&                                                                                  &&
  &&                                                                                  &&
  &&                        combinations and permutations                             &&
  &&                                                                                  &&
  &&                                                                                  &&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  


//function checks to see if number is positive integer
//function is called from continueTOproceed functions
//this function is used to calculate combinations and permutations
function analyze_numbers(a)
{
	if((numberArray[a].decimalValue % 1 == 0) && (numberArray[a].decimalValue >= 0))
	{
		return 'yes';
	}//end if
	else
	{
		return 'no';
	}//end else
}//end function


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
 

//function analyzes the numbers to determine if both are non negative integers and if the 
//value in the Y register is greater than or equal to the value in the X register
//this function is used to calculate combinations and permutations
function continueTOproceed_2numbers()
{
	var shouldIproceed = analyze_numbers(0);
	if(shouldIproceed == 'yes')
	{
		shouldIproceed = analyze_numbers(1);
	}
	
	if(shouldIproceed == 'yes')
	{
		if(numberArray[1].decimalValue >= numberArray[0].decimalValue)
		{
			shouldIproceed = 'yes';
		}//end if
		else
		{
			shouldIproceed = 'no';
		}
	}//end if
	
	return shouldIproceed;
}//end function


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/


//function is called from main and decides wether or not the numbers entered 
//are acceptable, if they are it then calls the function to calculate the 
//permutation and if not displays an error messasge
function permutation()
{
	var proceed = continueTOproceed_2numbers();
	
	if(proceed == 'yes')
	{
		permutation_decimal();
	}//end if
	else
	{
		alert('syntax error\n' + numberArray[0].decimalValue + ' and ' + numberArray[1].decimalValue + ' must both be non negative integers\nand ' + numberArray[1].decimalValue +' must be greater than or equal to ' + numberArray[0].decimalValue);
	}//end else
}//end function


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
 
 
//function calculates permutation and is called from the above function permutation()
function permutation_decimal()
{
	numberArray[0].decimalValue = factorial(numberArray[1].decimalValue) / factorial(numberArray[0].decimalValue);
	
	operations_2nd_level();
	drop();
}


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
 
 
//function is called from main and decides wether or not the numbers entered 
//are acceptable, if they are it then calls the function to calculate the 
//combination and if not displays an error messasge
function combination()
{
	var proceed = continueTOproceed_2numbers();
	
	if(proceed == 'yes')
	{
		combination_decimal()();
	}//end if
	else
	{
		alert('syntax error\n' + numberArray[0].decimalValue + ' and ' + numberArray[1].decimalValue + ' must both be non negative integers\nand ' + numberArray[1].decimalValue +' must be greater than or equal to ' + numberArray[0].decimalValue);
	}//end else
}


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
 

//function calculates combination and is called from the above function combination()
function combination_decimal()
{
	numberArray[0].decimalValue = factorial(numberArray[1].decimalValue) / (factorial(numberArray[1].decimalValue - numberArray[0].decimalValue) * factorial(numberArray[0].decimalValue));
	
	operations_2nd_level();
	drop();
}


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&                                                                                  &&
  &&                                                                                  &&
  &&                              modular division                                    &&
  &&                                                                                  &&
  &&                                                                                  &&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/


//function checks to see what type number is
//function is called from find_modulus function (next)
//function is used to calc mod
//function is also called from the add, sub, mult and divide functions in basic_math_functions.js
//function is also called from the It_power(), It_power_reverse(), It_shiftPower() and It_shiftPower_reverse()
//functions in 2nd_level_functions.js
function analyze_type()
{
	var concatanatedType = numberArray[0].type + numberArray[1].type;

	switch(concatanatedType)
	{
		case 'fractionfraction': return 'fraction';
		break;
		case 'fractionwhole': return 'fraction';
		break;
		case 'wholefraction': return 'fraction';
		break;
		default: return 'decimal';
	}//end switch
}//end function


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/

 
//function analyzes the type of numbers to decide type of modular division
//that needs to be done and then calls the appropriate function
//function is called from main.html
function find_modulus()
{
	var howTOproceed = analyze_type();

	switch(howTOproceed)
	{
		case 'fraction': find_modulus_Fraction();
		break;
		default: find_modulus_decimal();
	}//end switch
}//end function 


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
 
 
//if number types are mixed or decimal this function calculates the mod
function find_modulus_decimal()
{
	var my_modulus = numberArray[1].decimalValue % numberArray[0].decimalValue;
	
	numberArray[0].decimalValue = my_modulus;
	
	operations_2nd_level();
	drop();
}


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  
  
function find_modulus_Fraction()
{
alert('code not written yet\nfunction is find_modulus_Fraction() in combos.js');
}


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&                                                                                  &&
  &&                                                                                  &&
  &&                                  factorials                                      &&
  &&                                                                                  &&
  &&                                                                                  &&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/


//function determines if decimalValue is a non negative integer and if not returns
//an error message.  if decimalValue is non negative integer it calls function
//to calculate factorial
//function is called from main.html
//8 oct 2009 14:31
function find_factorial()
{
	if(analyze_numbers(0) == 'yes')
	{
		find_factorial_decimal();
	}//end if
	else
	{
		alert("syntax error\n" + numberArray[0].stringValue + " is not a non negative integer");
	}//end else
}//end function


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
 
 
//when decimalValue is a valid number this function will call the recursive
//function 'factorial()' to calculate the factorial
//and then displays answer
//8 oct 2009 14:35
function find_factorial_decimal()
{
	var my_factorial;
	
	my_factorial = factorial(numberArray[0].decimalValue);

	numberArray[0].decimalValue = my_factorial;
	
	operations_2nd_level();
}


/*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
 
 
//depressingly I just don't get recursive definitions
//it works
//this function is called from find_factorial_decimal() function (above)
//function is also called from combination_decimal() and permutation_decimal() functions
function factorial(n)
{
	if(n == 0)
	{
		return 1;
	}//end if
	else
	{
		if(n < 0)
		{
			alert('syntax error');
			return 'NaN';
		}//end else
		else
		{
			return (n * factorial(n - 1));
		}//end else
	}//end else
}//end function


