//=======================
// fractions
//=======================
function fraction()
{
	fract=true;
	swapImage("formatDec_img",formatFra_img.src);
	refresh();
}
//=======================
function tagleff()
{
	if(modeff == 0){
		modeff = 1;
		swapImage("format_ff_img",format_ffa_img.src);
	}else {
		modeff = 0;
		swapImage("format_ff_img",format_ff_img.src);
	}
	refresh();
}
//=======================
function frafunc(f)
{
	if(periodHit==0)
		fractEntry=true;
		
	if	(fractEntry==true){
		periodHit=2;
	
		//fractEntry=true;
		
		if (f=='+'){
			if(flevel<1){
				entryString += " ";
				fracEntryVal+= "+";
			}
			flevel=1;
		}else if (f=='/'){
			if(flevel<3){
				entryString += "/";
				fracEntryVal+= "/";
			}
			flevel=3;
		}else{
			if(flevel<3)
			;
			entryString += ""+f;
			fracEntryVal+= ""+f;
			if(flevel>=3){
			value = eval(fracEntryVal);	
			}
	
		}
		refresh();
	}	
}

//=======================
function dec()
{
	fract=false;
	swapImage("formatDec_img",formatDec_img.src);
	refresh();
}


//=======================
function toFraction(number)
{
var fractnum="";
var neg=false;

	if(number<0)
		neg=true;
	var rdeci= Math.abs(evalNum(number)).toString();
	var beforepat="";
	if ( rdeci.indexOf(".") == -1){
		valueStr=""+number;
		fractnum=""+evalNum(number);
		return fractnum;
	}	
	var signat=0;
	var expi="";

	if (exponent||((signat=(rdeci.lastIndexOf("e")))>=0)){
		expi=rdeci.substring(rdeci.indexOf("e"),rdeci.length);
		rdeci=rdeci.substring(0,rdeci.indexOf("e"));
	}

	var repeatpat=0;
	var start=0;
	var pos=0;
	var done=-1;
	var firstdigi;
	var theFixed;

	pos=rdeci.indexOf('.',0)
	theFixed=rdeci.substring(0,pos);
	rdeci=rdeci.substring(pos+1,rdeci.length);

	while(done<0){
		if(rdeci.length<6){
			repeatpat=-1;
			break;
		}
		firstdigi=rdeci.substring(start,start+1)
		if((pos = rdeci.indexOf(firstdigi,start+1))>=0){
			//we have a repeating num letsee if it is a pattern
			var firststring = rdeci.substring(start,pos);
			var medstring = rdeci.substring(pos,pos+pos-start);
			var laststring = rdeci.substring(pos+pos-start,pos+pos-start+pos-start);
			if (firststring == medstring){
				repeatpat=pos-start;
			}else	
				repeatpat=-1;			
			if (repeatpat==1){
				for(var index=start+1;index<rdeci.length-2;index++){
					if(rdeci.charAt(start) != rdeci.charAt(index)){
							repeatpat=-1;
							break;
					}
				}
				if (rdeci.charAt(start) == "0"){
						repeatpat=-1;
				}
			}
		}
		if(repeatpat>=1) {
			done=1;
			beforepat=rdeci.substring(0,start);
		}if(start >= rdeci.length)
			 done=1;
		start++;
	}	

	var top=0;
	var bottom=0;
	var i=0;

	if(repeatpat<=0){  												//  terminating
		while(rdeci.charAt(i)=='0') i++; //remove leading zeros
		top = parseInt(rdeci.substring(i,rdeci.length));
		bottom = (Math.floor((Math.exp(rdeci.length * Math.LN10))));
	}else if( beforepat==""){ 	
		top = parseInt(firststring);
		bottom = ((Math.floor((Math.exp(firststring.length * Math.LN10))))-1);
	}else{ 						
		var fisrtpart=beforepat+firststring;
		var bottom1=(Math.floor((Math.exp(fisrtpart.length * Math.LN10))));
		var bottom2=(Math.floor((Math.exp(beforepat.length * Math.LN10))));
		while(fisrtpart.charAt(i) == '0') i++; //remove leading zeros
		top=(parseInt(fisrtpart.substring(i,fisrtpart.length)) - parseInt(beforepat));
		bottom = bottom1 - bottom2;
	}
	
	var t=top;
	var b=bottom;
	var x=0;		
	var remainder=bottom-top*Math.floor(bottom/top);

	while(remainder>0){
		b=t;
		t=remainder;
		x=Math.floor(b/t);
		remainder=b-t*x;
	}
	if(theFixed=="0")
		fractnum=top/t+"/"+bottom/t;
	else
		if(modeff == 0)
			fractnum =theFixed+" "+top/t+"/"+bottom/t;
		else
			fractnum =((theFixed*(bottom/t))+(top/t))+"/"+bottom/t; //improper
	//number=eval(theFixed+"+"+top/t+"/"+bottom/t);
	if(neg){
		fractnum = "-"+fractnum;
	}
	if ((signat=(expi.lastIndexOf("e")))==0){
		fractnum+=""+expi;
	}
		
	return fractnum;
	
	
}
			
