//////////////////////SPELLING FRACTIONS////////////////////////////////
//////////////Javascript code, Java code, html, and objects:////////////////
////Code=(c) Maurici Carbó Jordi for Math Cats - www.mathcats.com///////////  
//////////////////////Page= (c)2003 Wendy Petti/////////////////////////////
///This code and application are protected under the Copyright laws.////////
///This code may not be reproduced on any other site or in any form.////////
/////////Questions? Contact Wendy Petti (wpetti@mathcats.com)///////////////
////////////////////////////////////////////////////////////////////////////
//////////////SPELLING OBJECTS/////////////////////////////////////////
function singplur(sing,plur)
{
this.m_sing=sing;
this.m_plur=plur;
}

units=new Object(10);
units[0]="zero";
units[1]="one";
units[2]="two";
units[3]="three";
units[4]="four";
units[5]="five";
units[6]="six";
units[7]="seven";
units[8]="eight";
units[9]="nine";


teens=new Object(10);
teens[0]="ten";
teens[1]="eleven";
teens[2]="twelve";
teens[3]="thirteen";
teens[4]="fourteen";
teens[5]="fifeteen";
teens[6]="sixteen";
teens[7]="seventeen";
teens[8]="eighteen";
teens[9]="nineteen";

tens=new Object(10);
tens[2]="twenty";
tens[3]="thirty";
tens[4]="forty";
tens[5]="fifty";
tens[6]="sixty";
tens[7]="seventy";
tens[8]="eighty";
tens[9]="ninety";


denunits=new Object(10);
denunits[2]=new singplur("half","halves");
denunits[3]=new singplur("third","thirds");
denunits[4]=new singplur("quarter","quarters");
denunits[5]=new singplur("fifth","fifths");
denunits[6]=new singplur("sixth","sixths");
denunits[7]=new singplur("seventh","sevenths");
denunits[8]=new singplur("eighth","eighths");
denunits[9]=new singplur("nineth","nineths");

denuns=new Object(10);
denuns[1]=new singplur("-oneth","-oneths");
denuns[2]=new singplur("-twoth","-twoths");
denuns[3]=new singplur("-threeth","-threeths");
denuns[4]=new singplur("-fourth","-fourths");
denuns[5]=new singplur("-fiveth","-fiveths");
denuns[6]=new singplur("-sixth","-sixths");
denuns[7]=new singplur("-seventh","-sevenths");
denuns[8]=new singplur("-eighth","-eighths");
denuns[9]=new singplur("-nineth","-nineths");

denteens=new Object(10);
denteens[0]=new singplur("tenth","tenths");
denteens[1]=new singplur("eleventh","elevenths");
denteens[2]=new singplur("twelfth","twelfths");
denteens[3]=new singplur("thirteenth","thirteenths");
denteens[4]=new singplur("fourteenth","fourteenths");
denteens[5]=new singplur("fifteenth","fifteenths");
denteens[6]=new singplur("sixteenth","sixteenths");
denteens[7]=new singplur("seventeenth","seventeenths");
denteens[8]=new singplur("eighteenth","eighteenths");
denteens[9]=new singplur("nineteenth","nineteenths");

dentens=new Object(10);
dentens[2]=new singplur("twentieth","twentieths");
dentens[3]=new singplur("thirtieth","thirtieths");
dentens[4]=new singplur("fortieth","fortieths");
dentens[5]=new singplur("fiftieth","fiftieths");
dentens[6]=new singplur("sixtieth","sixtieths");
dentens[7]=new singplur("seventieth","seventieths");
dentens[8]=new singplur("eightieth","eightieths");
dentens[9]=new singplur("ninetieth","ninetieths");



/////////////////END SPELLING OBJECTS///////////////////////////////////

///////////////////////////LOWFRACTION OBJECT//////////////////////////
////////////don't use this with fractions less than 1/28///////////////

function lowrational(num,den)
{
this.m_num=num;
this.m_den=den;
this.m_num_div=" ";
this.m_den_div=" ";
if(this.m_den==0){this.m_num=0;this.m_den=1;}
this.m_NombresPrimers_array=new Object (8);
this.m_NombresPrimers_array[0]=2;
this.m_NombresPrimers_array[1]=3;
this.m_NombresPrimers_array[2]=5;
this.m_NombresPrimers_array[3]=7;
this.m_NombresPrimers_array[4]=11;
this.m_NombresPrimers_array[5]=13;
this.m_NombresPrimers_array[6]=17;
this.m_NombresPrimers_array[7]=19;
this.m_NombresPrimers_array[8]=23;
this.m_NombresPrimers_array[9]=29;

this.m_NombresPrimers_array[10]=31;
this.m_NombresPrimers_array[11]=37;
this.m_NombresPrimers_array[12]=41;
this.m_NombresPrimers_array[13]=43;
this.m_NombresPrimers_array[14]=47;
this.m_NombresPrimers_array[15]=53;
this.m_NombresPrimers_array[16]=59;
this.m_NombresPrimers_array[17]=61;
this.m_NombresPrimers_array[18]=67;
this.m_NombresPrimers_array[19]=71;

this.m_NombresPrimers_array[20]=73;
this.m_NombresPrimers_array[21]=79;
this.m_NombresPrimers_array[22]=83;
this.m_NombresPrimers_array[23]=89;
this.m_NombresPrimers_array[24]=97;
this.m_NombresPrimers_array[25]=101;
this.m_NombresPrimers_array[26]=103;
this.m_NombresPrimers_array[27]=107;
this.m_NombresPrimers_array[28]=109;
this.m_NombresPrimers_array[29]=113;

this.m_NombresPrimers_array[30]=127;
this.m_NombresPrimers_array[31]=131;
this.m_NombresPrimers_array[32]=137;
this.m_NombresPrimers_array[33]=139;
this.m_NombresPrimers_array[34]=149;
this.m_NombresPrimers_array[35]=151;
this.m_NombresPrimers_array[36]=157;
this.m_NombresPrimers_array[37]=163;
this.m_NombresPrimers_array[38]=167;
this.m_NombresPrimers_array[39]=173;

this.m_NombresPrimers_array[40]=179;
this.m_NombresPrimers_array[41]=181;

for (x=0;x<=41;)
	{
	if((this.m_num%this.m_NombresPrimers_array[x]==0)&&(this.m_den%this.m_NombresPrimers_array[x]==0))
		{
		this.m_num=this.m_num/this.m_NombresPrimers_array[x];
		this.m_den=this.m_den/this.m_NombresPrimers_array[x];
		this.m_num_div+=this.m_NombresPrimers_array[x]+" x ";
		this.m_den_div+=this.m_NombresPrimers_array[x]+" x ";
		}
	else
		{
		x++;
		}	
	}
}
//////////////END LOWFRACTION OBJECT/////////////////////////////////////



//////////////MAIN OBJECT/////////////////////////////////////////////////////////////
function pietool(a,b)
	{
	this.m_num_original=a;
	this.m_den_original=b;
	this.m_frac_simp=new lowrational(a,b);	
	this.m_frac_pi=new lowrational(2*a,b);
	this.p_setfrac=sfr;
		function sfr(inputa,inputb)
			{
			this.m_num_original=inputa;			
			this.m_den_original=inputb;			
			delete this.m_frac_simp;
			delete this.m_frac_pi;
			this.m_frac_simp=new lowrational(inputa,inputb);
			this.m_frac_pi=new lowrational(inputa*2,inputb);
			}
	
	}
////////////END MAIN OBJECT////////////////////////////////////////////////////

///////////STARTING/////////////////////////////////////////////////////////////////
function init()
	{
	True_pieTool=new pietool(1,1);	
	showfrac(1);
	}
//////////////////////////////////////////////////////////////////STARTING///////

/////////////////INTERFACE////////////////////////////////////////////////////////
function n()
	{
	if(document.spellingfractionsform.denominator.selectedIndex+1>=document.spellingfractionsform.numerator.selectedIndex)
		{
		True_pieTool.p_setfrac(document.spellingfractionsform.numerator.selectedIndex,document.spellingfractionsform.denominator.selectedIndex+1)
		}
	else
		{
		document.spellingfractionsform.denominator.selectedIndex=document.spellingfractionsform.numerator.selectedIndex-1;
		}
	showfrac(1);
	}
function d()
	{
	if(document.spellingfractionsform.denominator.selectedIndex+1>=document.spellingfractionsform.numerator.selectedIndex)
		{
		True_pieTool.p_setfrac(document.spellingfractionsform.numerator.selectedIndex,document.spellingfractionsform.denominator.selectedIndex+1)
		}
	else
		{
		document.spellingfractionsform.denominator.selectedIndex=document.spellingfractionsform.numerator.selectedIndex-1;
		}
	showfrac(1);
	}

function showfrac(color)
	{
	if(color==1)
		{
		if(document.spellingfractionsform.denominator.selectedIndex+1>=document.spellingfractionsform.numerator.selectedIndex)
			{
			document.sf.InputDataResult(True_pieTool.m_frac_simp.m_num,True_pieTool.m_frac_simp.m_den);
			}
		}
	else
		{
		if(document.spellingfractionsform.denominator.selectedIndex+1>=document.spellingfractionsform.numerator.selectedIndex)
			{
			document.sf.InputDataResult2(True_pieTool.m_num_original,True_pieTool.m_den_original);
			}
		}
	document.spellingfractionsform.Snum.value=True_pieTool.m_frac_simp.m_num;
	document.spellingfractionsform.Sden.value=True_pieTool.m_frac_simp.m_den;
	document.spellingfractionsform.Inbutton.value=True_pieTool.m_num_original+"/"+True_pieTool.m_den_original;
	document.spellingfractionsform.Sbutton.value=True_pieTool.m_frac_simp.m_num+"/"+True_pieTool.m_frac_simp.m_den;
	document.spellingfractionsform.mainfraction.value=True_pieTool.m_frac_simp.m_num+"/"+True_pieTool.m_frac_simp.m_den;
	document.spellingfractionsform.rate.value=Math.floor(1000000*True_pieTool.m_frac_simp.m_num/True_pieTool.m_frac_simp.m_den)/1000000;
	document.spellingfractionsform.angulardeg.value=Math.floor(360*True_pieTool.m_frac_simp.m_num/True_pieTool.m_frac_simp.m_den);
	document.spellingfractionsform.angularrad.value=Math.floor(1000000*(2*Math.PI/360)*Math.floor(360*True_pieTool.m_frac_simp.m_num/True_pieTool.m_frac_simp.m_den))/1000000;
	document.spellingfractionsform.angularpi.value=True_pieTool.m_frac_pi.m_num+"/"+True_pieTool.m_frac_pi.m_den;
	spellfrac(color);
	why();
	}

function showinputfrac()
	{
	showfrac(2);
	}
function showsimpliffrac()
	{
	showfrac(1);
	}
function newrate()
	{
	if(parseFloat(document.spellingfractionsform.rate.value)>1)
		{
		document.spellingfractionsform.rate.value="1";		
		}
	actualfraction=parseFloat(document.spellingfractionsform.rate.value)*360;
	actuallowrational=new lowrational(Math.round(actualfraction),360);
	document.spellingfractionsform.numerator.selectedIndex=actuallowrational.m_num;
	document.spellingfractionsform.denominator.selectedIndex=actuallowrational.m_den-1;
	delete actuallowrational;
	True_pieTool.p_setfrac(document.spellingfractionsform.numerator.selectedIndex,document.spellingfractionsform.denominator.selectedIndex+1)
	showfrac(1);
	}
function newangulardeg()
	{
	if(parseFloat(document.spellingfractionsform.angulardeg.value)>360)
		{
		document.spellingfractionsform.angulardeg.value="360";		
		}
	actualfraction=parseFloat(document.spellingfractionsform.angulardeg.value);
	actuallowrational=new lowrational(Math.round(actualfraction),360);
	document.spellingfractionsform.numerator.selectedIndex=actuallowrational.m_num;
	document.spellingfractionsform.denominator.selectedIndex=actuallowrational.m_den-1;
	delete actuallowrational;
	True_pieTool.p_setfrac(document.spellingfractionsform.numerator.selectedIndex,document.spellingfractionsform.denominator.selectedIndex+1)
	showfrac(1);
	}
function newangularrad()
	{
	if(parseFloat(document.spellingfractionsform.angularrad.value)>(2*Math.PI))
		{
		document.spellingfractionsform.angularrad.value="6.283185";		
		}
	actualfraction=parseFloat(document.spellingfractionsform.angularrad.value);
	actuallowrational=new lowrational(Math.round(360*actualfraction/(2*Math.PI)),360);
	document.spellingfractionsform.numerator.selectedIndex=actuallowrational.m_num;
	document.spellingfractionsform.denominator.selectedIndex=actuallowrational.m_den-1;
	delete actuallowrational;
	True_pieTool.p_setfrac(document.spellingfractionsform.numerator.selectedIndex,document.spellingfractionsform.denominator.selectedIndex+1)
	showfrac(1);
	}
function spellfrac(cl)
	{
	aquestnum=0;aquestden=0;
	aquestnum2=0;aquestden2=0;
	if (cl==1) {document.spellingfractionsform.spelled.style.backgroundColor="yellow";}
	else{document.spellingfractionsform.spelled.style.backgroundColor="white";}
	if (cl==1){aquestnum=True_pieTool.m_frac_simp.m_num;aquestden=True_pieTool.m_frac_simp.m_den;}
	else{aquestnum=True_pieTool.m_num_original;aquestden=True_pieTool.m_den_original;}
	aquestnum2=aquestnum;aquestden2=aquestden;
   document.spellingfractionsform.spelled.value="";
	if((aquestnum<1000)&&(aquestnum>99))
		{
		document.spellingfractionsform.spelled.value+=units[Math.floor(Math.floor(aquestnum)/100)]+" hundred ";
		aquestnum=aquestnum-Math.floor(Math.floor(aquestnum)/100)*100;
		}
   if(aquestnum<10)
   		{
		document.spellingfractionsform.spelled.value+=units[aquestnum];
		}
	else
		{
		if(aquestnum<100)
			{
			if(aquestnum>19)
				{
				document.spellingfractionsform.spelled.value+=tens[Math.floor(aquestnum/10)];
			    if(aquestnum%10!=0)
			    	{
			    	document.spellingfractionsform.spelled.value+="-"+units[aquestnum%10];
					}			    
			    }
			else
				{
				document.spellingfractionsform.spelled.value+=teens[Math.floor(aquestnum-10)];
				}
			}
		}   	
	if(aquestnum2==1)
		{
		if((aquestden<1000)&&(aquestden>99))
			{
			if(aquestden-Math.floor(Math.floor(aquestden)/100)*100==0)
				{
				document.spellingfractionsform.spelled.value+=" "+units[Math.floor(Math.floor(aquestden)/100)]+" hundredth ";
				}
			else
				{
				document.spellingfractionsform.spelled.value+=" "+units[Math.floor(Math.floor(aquestden)/100)]+" hundred ";
				}			
			aquestden=aquestden-Math.floor(Math.floor(aquestden/100)*100);
			}
		if(aquestden!=0)
			{	
		   if(aquestden<10)
		   		{
				if(aquestden2<10)
					{
					if(aquestden2!=1)
						{
						document.spellingfractionsform.spelled.value+=" "+denunits[aquestden].m_sing;
						}
					}
				else
					{
					document.spellingfractionsform.spelled.value+=denuns[aquestden].m_sing;
					}
				}
			else
				{
				if(aquestden<100)
					{
					if(aquestden>19)
						{
					    if(aquestden%10!=0)
					    	{
							document.spellingfractionsform.spelled.value+=" "+tens[Math.floor(aquestden/10)];
					    	document.spellingfractionsform.spelled.value+=denuns[aquestden%10].m_sing;
							}			    
						else
							{
							document.spellingfractionsform.spelled.value+=" "+dentens[Math.floor(aquestden/10)].m_sing;
							}
					    }
					else
						{
						document.spellingfractionsform.spelled.value+=" "+denteens[Math.floor(aquestden-10)].m_sing;
						}
					}
				}   	
            }
		}
	else
		{
		if((aquestden<1000)&&(aquestden>99))
			{
			if(aquestden-Math.floor(Math.floor(aquestden)/100)*100==0)
				{
				document.spellingfractionsform.spelled.value+=" "+units[Math.floor(Math.floor(aquestden)/100)]+" hundredths ";
				}
			else
				{
				document.spellingfractionsform.spelled.value+=" "+units[Math.floor(Math.floor(aquestden)/100)]+" hundred ";
				}			
			aquestden=aquestden-Math.floor(Math.floor(aquestden/100)*100);
			}
		if(aquestden!=0)
			{	
		   if(aquestden<10)
		   		{
				if(aquestden2<10)
					{
					if(aquestden2!=1)
						{
						document.spellingfractionsform.spelled.value+=" "+denunits[aquestden].m_plur;
						}
					}
				else
					{
					document.spellingfractionsform.spelled.value+=" "+denuns[aquestden].m_plur;
					}
				}
			else
				{
				if(aquestden<100)
					{
					if(aquestden>19)
						{
					    if(aquestden%10!=0)
					    	{
							document.spellingfractionsform.spelled.value+=" "+tens[Math.floor(aquestden/10)];
					    	document.spellingfractionsform.spelled.value+=denuns[aquestden%10].m_plur;
							}			    
						else
							{
							document.spellingfractionsform.spelled.value+=" "+dentens[Math.floor(aquestden/10)].m_plur;
							}
					    }
					else
						{
						document.spellingfractionsform.spelled.value+=" "+denteens[Math.floor(aquestden-10)].m_plur;
						}
					}
				}   	
            }
		}
	}
function getangle()
	{
	angle=document.sf.getAngle();
	if (angle!=0)
		{
		document.spellingfractionsform.angulardeg.value=angle;
		newangulardeg();		
		}
	}

function onedeg(dir)
	{
	if (dir==1)
		{
		document.spellingfractionsform.angulardeg.value++;
		}
	else
		{
		document.spellingfractionsform.angulardeg.value--;
		}
	newangulardeg();
	}

function why()
	{
	document.spellingfractionsform.whytext.value="";
	if(True_pieTool.m_num_original!=True_pieTool.m_frac_simp.m_num)
		{
		document.spellingfractionsform.whytext.value+=" "+True_pieTool.m_num_original+"/"+True_pieTool.m_den_original;
		document.spellingfractionsform.whytext.value+=" = "+True_pieTool.m_frac_simp.m_num+"/"+True_pieTool.m_frac_simp.m_den;
		document.spellingfractionsform.whytext.value+="; Because: ";
		document.spellingfractionsform.whytext.value+=True_pieTool.m_num_original+" =" ;
		document.spellingfractionsform.whytext.value+= True_pieTool.m_frac_simp.m_num_div+""+True_pieTool.m_frac_simp.m_num+ "; ";
		document.spellingfractionsform.whytext.value+=True_pieTool.m_den_original+" =" ;
		document.spellingfractionsform.whytext.value+= True_pieTool.m_frac_simp.m_den_div+""+True_pieTool.m_frac_simp.m_den+ "; ";
		document.spellingfractionsform.whytext.value+="; Then: ";
		document.spellingfractionsform.whytext.value+="( "+True_pieTool.m_frac_simp.m_num_div+""+True_pieTool.m_frac_simp.m_num+" ) / ( " ;
		document.spellingfractionsform.whytext.value+=True_pieTool.m_frac_simp.m_den_div+""+True_pieTool.m_frac_simp.m_den+" )" ;
		document.spellingfractionsform.whytext.value+=" = "+True_pieTool.m_frac_simp.m_num+"/"+True_pieTool.m_frac_simp.m_den;
	
		}
	}
////////////////END INTERFACE////////////////////////////////////////////////


