
//flags to mark previous function
var opFlag=false        //indicates previous entry was an operator
var eqFlag=false        //indicates previous function was a calculation

function checkDec() {
//prevents multiple decimal points in entry
        if (document.calc.screen.value.indexOf('.') == -1){
                enterVal('.')
                return
        }

                if ((document.calc.screen.value.indexOf('.') >= 0) && ((eqFlag) || (opFlag))){
                enterVal('.')
                return
        }
        
}

function enterVal(num){         
//determine how to handle entry based on previous function

                
        if (eqFlag) {
        //last entry gave a total, no operators entered, so replace both stores
                document.calc.input.value=num
                document.calc.screen.value=num
                eqFlag=false
                return
        }
        
        if ((!opFlag) && (!eqFlag)){
        //no operators or totals entered, still entering numbers
                document.calc.input.value += num
                document.calc.screen.value += num
                return                          
        }

        if (opFlag) {
        //operator entered, change screen value, build on input
                document.calc.screen.value =num
                document.calc.input.value += num
                opFlag=false
                return
        }       

}

function compute() {
//if last entry is an operator, input is not complete so do nothing

        len=document.calc.input.value.length
        var oneChar=document.calc.input.value.charAt(len-1)
        if ((oneChar == '+') || (oneChar == '-') || (oneChar == '*') ||
                (oneChar == '/')) {     
                return
        }

        document.calc.screen.value = fixIt(eval(document.calc.input.value))
        document.calc.input.value = fixIt(eval(document.calc.input.value))
        
        eqFlag=true
        
        
}

//a fix for JavaScript's crappy math handling
function fixIt(num) {
	return(Math.round(num*10000000)/10000000);
}


function operator(op) {
        //can't enter operator before entering a value
        if (document.calc.screen.value == ''){
                return
        }

        //if last entry is not an operator, use the one just entered
        eqFlag=false
        len=document.calc.input.value.length
        var oneChar=document.calc.input.value.charAt(len-1)
        if ((oneChar != '+') && (oneChar != '-') && (oneChar != '*') &&
                (oneChar != '/') && (oneChar != '.')){
                        document.calc.screen.value = eval(document.calc.input.value)
                        document.calc.input.value = eval(document.calc.input.value)
                        document.calc.input.value += op
                        
        }
        else {          
        //if last entry is an operator, replace it with this one
        //this returns input store less operator
                document.calc.input.value=parseFloat(document.calc.input.value)
                document.calc.input.value+=op
        }
        opFlag=true
}

function clearAll() {
        document.calc.input.value = ''
        document.calc.screen.value=''
        
}

function clearEntry() {
        //don't do anything if the last entry is not a number
        len=document.calc.input.value.length
        var oneChar=document.calc.input.value.charAt(len-1)
        if ((oneChar == '+') || (oneChar == '-') || (oneChar == '*') ||
                (oneChar == '/')) {     
                return
        }
        
        if (document.calc.screen.value == ''){
        //nothing here to clear
                return
        }
        
        if (document.calc.screen.value==document.calc.input.value) {  
        //only one entry, so clear it
                document.calc.screen.value=''
                document.calc.input.value=''
                return
        }
                //get last number entered and clear it, return previous value to screen
                str1=document.calc.screen.value
                str2=document.calc.input.value
                ind=str2.lastIndexOf(str1)
                document.calc.input.value=document.calc.input.value.substring(0,ind)
                document.calc.screen.value=parseFloat(document.calc.input.value)
                
}
function sqrRT() {

        if (document.calc.input.value == ''){
        //nothing entered, do nothing
                return
        }

        document.calc.screen.value=Math.sqrt(document.calc.screen.value)
        document.calc.input.value=document.calc.screen.value
        eqFlag=true
}

function sqr() {

        if (document.calc.input.value == ''){
                return
        }
        
        document.calc.screen.value=(document.calc.screen.value)*(document.calc.screen.value)
        document.calc.input.value=document.calc.screen.value
        eqFlag=true
}

function neg() {//get last entry and negate it
        str1=document.calc.screen.value
        str2=document.calc.input.value
        ind=str2.lastIndexOf(str1)

        document.calc.input.value=document.calc.input.value.substring(0,ind)
        document.calc.screen.value=(document.calc.screen.value)*-1
        document.calc.input.value += "(" + document.calc.screen.value + ")"
        //placed in () to solve double operator problem
}
        

function pcnt() {

        if (document.calc.input.value == ''){ //nothing here
                return
        }
        //if last entry is an operator, do nothing
        len=document.calc.input.value.length  
        var oneChar=document.calc.input.value.charAt(len-1)
        if ((oneChar == '+') || (oneChar == '-') || (oneChar == '*') ||
                (oneChar == '/')) {     
                return
        }
        //check that there are two numbers entered
        if ((document.calc.input.value.indexOf('+') == -1)&& 
        (document.calc.input.value.indexOf('-') == -1)&&
        (document.calc.input.value.indexOf('*') == -1)&&
        (document.calc.input.value.indexOf('/') == -1)){
                return
        }
        //calculate percentage

        if ((document.calc.input.value.indexOf('+') != -1) || 
        (document.calc.input.value.indexOf('-') != -1)) {

                percent=((document.calc.screen.value)/100)*parseFloat(document.calc.input.value)
        
        }

        else {
                percent=((document.calc.screen.value)/100)
                
        }
        
        //replace last entry with percentage and evaluate expression
        str1=document.calc.screen.value
        str2=document.calc.input.value
        ind=str2.lastIndexOf(str1)

        document.calc.input.value=document.calc.input.value.substring(0,ind)
        document.calc.input.value+=percent
        document.calc.screen.value = eval(document.calc.input.value)
        document.calc.input.value = eval(document.calc.input.value)
        

        eqFlag=true
}

function memory_p() {
        if (document.calc.screen.value ==''){
                return
        }
        //add screen value to memory store
        document.calc.memory.value+= "+" + document.calc.screen.value
        document.calc.memory.value=eval(document.calc.memory.value)
        eqFlag=true
        document.calc.mem_win.value='M' //indicator of something in memory
        
}

function memory_s() {

        if (document.calc.screen.value ==''){
                return
        }

        document.calc.memory.value += "-(" + (document.calc.screen.value) +")"
        document.calc.memory.value=eval(document.calc.memory.value)

        eqFlag=1
        document.calc.mem_win.value='M' 

}

function memory_r() {
        //recall memory store to screen
        document.calc.screen.value=''
        enterVal(document.calc.memory.value)
        eqFlag=true
        
        
        
}

function memory_c() {
        //clear memory indicator
        document.calc.memory.value=0
        document.calc.mem_win.value=''  

}

function constant(what) {
        if (what == 'pi') {
                document.calc.screen.value=''
                enterVal(Math.PI)
                eqFlag=true
                
        }
}
                        



