﻿// JScript File
var expenses = 0;
var selection = "";
var mileage = 0;
var mileageStartDate;
var weekEndingDateField;
var weDateField;
/**
function updateField(amt)
{
    
    updateVariables(amt)
    
    if( weekEndingDateField == null ){ weekEndingDateField = findField("weekEndingDate"); }
    if( weDateField == null ){ weDateField = findField("weDate"); }
    
    weekEndingDateField.innerText = currentWeek;
    weDateField.innerText = currentWeek;
}

function updateVariables(amt)
{
    var MilliSecondsToAdd = (86400000 * 7) ;
    var CurrDate = ( amt == 0 ) ? new Date(thisWeek) : new Date(currentWeek);
    var CurrWeek = ( amt == 0 ) ? eval(thisWeekNumber) : eval(currentWeekNumber);
    switch ( amt )
    {
        case -7:
            CurrDate = CurrDate.getTime() - MilliSecondsToAdd;
            CurrWeek = (CurrWeek - 1);
        break;
        
        case 7:
            CurrDate = CurrDate.getTime() + MilliSecondsToAdd;;
            CurrWeek = (CurrWeek + 1);
        break
        
        default:
            CurrDate = CurrDate.getTime();            
        break
    }
    var newDate = new Date(CurrDate);
    
    currentWeek =  (newDate.getMonth()+1) + "/" + newDate.getDate() + "/" + newDate.getFullYear();
    currentWeekNumber = CurrWeek;
}

function findField(fldName)
{
    
    var tagLen = document.all.length;
    for( var i = 0; i < tagLen; i++)
    {
        var tag = document.all[i];
        
        if( tag.tagName == "SPAN" )
        {
            var elem = tag;
            if( tag.id.substring(tag.id.length - fldName.length, tag.id.length )== fldName )
            {
                return tag;
            } 
        }
    }
}
**/
function isDecimal(evt, elem)
{
    try
    {
        var charCode = (evt.which) ? evt.which : event.keyCode;
        if(( charCode == 9) && (elem.value.length == 0))
        {
            return true;
        }
        else
        {
            if (((charCode >= 48) && (charCode <=57)) || (charCode == 46) || (charCode == 9) || (charCode == 13))
            {
                var txt = elem.value + "" + String.fromCharCode(charCode);
                //var re = new RegExp("^([\d]{0,2})*(\.?[\d]{0,2})$");
                if( isNaN(txt) )
                {
                    return false;
                }
                else
                {
                    if( txt.length < 8 )
                    {
                        var val = eval(txt);
                        if( val > -1 )
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else
            {
                return false;
            }
        }
    }
    catch(ex)
    {
        return false;
    }        
}

function getExpenseTotals(input, code)
{
    try
    {
        getRowExpenses(input, code)
        var expenseTotal = updateExpenseTotal(input,code);
        document.all.sheetTotal.value = expenseTotal;
    }
    catch(ex)
    {
        return false;
    }
}

function updateExpenseTotal(input, sub)
{
    try
    {
        var frm = input.form;
        var elems = frm.elements.length
        var total = 0 * 0;
        
        for( var i = 0; i < elems; i++)
        {
            var elem = frm.elements[i];
            
            if( ( elem.tagName == "INPUT" ) && 
                ( elem.name.substring(elem.name.length - sub.length, elem.name.length )== sub ) && 
                ( elem.value.length > 0  ) )
                {
                    total += parseFloat( elem.value );
                }
        }
        return total;
    }
    catch(ex)
    {
        return false;
    }
}

function deleteRow(elem)
{
    try
    {
        var row = elem.parentElement.parentElement;
        if( row.tagName == "TR" )
        {
            var cols = row.children.length;    
            for( var ctr = 0; ctr < cols; ctr++)
            {
                var col = row.children[ctr]
                if(col.children.length > 0 )
                {
                    var foundElem = col.children[0];
                    if( foundElem.tagName == "INPUT"  )
                    {
                        foundElem.value = 0;
                    }
                }    
            }
        }
        row.style.display = "none";
        //row.style.visibility = "hidden";
        var row = row.parentElement.rows[row.rowIndex + 1];
        if( row.tagName == "TR" )
        {
            var cols = row.children.length;    
            for( var ctr = 0; ctr < cols; ctr++)
            {
                var col = row.children[ctr]
                if(col.children.length > 0 )
                {
                    var foundElem = col.children[0];
                    if( foundElem.tagName == "INPUT"  )
                    {
                        foundElem.value = 0;
                    }
                }    
            }
        }
        
        getExpenseTotals(elem, "B")
        row.style.display = "none";
        //row.style.visibility = "hidden";
    }
    catch(ex)
    {
        return false;
    }    
        
}

function getPerDiemTotals(elem, code, calcRow)
{
    try
    {
        var rowExpenses = 0;
        var cols = calcRow.children.length
        for( var ctr = 0; ctr < cols; ctr++)
        {
            if( calcRow.children[ctr].children.length > 0 )
            {
                if( calcRow.children[ctr].children[0].tagName == "INPUT"  )
                {
                    var inputEl =  calcRow.children[ctr].children[0];
                    if(( inputEl.name.substring(inputEl.name.length-1, inputEl.name.length) == code )
                        && (inputEl.value.length > 0))
                    {
                        rowExpenses += parseFloat(inputEl.value);
                    }
                    else
                    {
                        if( inputEl.name.substring(inputEl.name.length-5, inputEl.name.length) == "Total" )
                        {
                            inputEl.value = rowExpenses;
                        }
                    }
                }
            }    
        }
    }
    catch(ex)
    {
        return false;
    }
}

function getRowExpenses(input, code)
{   
    try
    { 
        var expenses = 0;
        var row = input.parentElement.parentElement;
        if( row.tagName == "TR" )
        {
            var cols = row.children.length
            for( var ctr = 0; ctr < cols; ctr++)
            {
                if( row.children[ctr].children.length > 0 )
                {
                    if( row.children[ctr].children[0].tagName == "INPUT"  )
                    {
                        var inputEl =  row.children[ctr].children[0];
                        if(( inputEl.name.substring(inputEl.name.length-1, inputEl.name.length) == code )
                            && (inputEl.value.length > 0))
                        {
                            expenses += parseFloat(inputEl.value);
                        }
                        else
                        {
                            if( inputEl.name.substring(inputEl.name.length-5, inputEl.name.length) == "Total" )
                            {
                                inputEl.value = expenses;
                            }
                        }
                    }
                }    
            }
        }
    }
    catch(ex)
    {
        return false;
    }
}


function getPerDiemCalc(elem, elemTypeId)
{
    try
    {
        if(( elemTypeId != selection ) && (selection.length > 0 ))
        {
            switch( selection )
            {
                case "lodgingId":
                case "mealsId":
                     elemTypeId = selection;
                break;
                
                case "milesId":
                    getMileageCalc(elem, selection);
                    return;
                break;
                
                default:
                    getExpenseTotals(elem, "B");
                    return;
                break;
            }
        }
        var col = elem.parentElement;
        var row = col.parentElement;
        var perDiem = 0;
        for( var cellCtr = 0; cellCtr < row.cells.length; cellCtr++ )
        {
            var inputCnt = row.cells[cellCtr].children.length;
            for( var inputCtr = 0; inputCtr < inputCnt; inputCtr++)
            {
                var input = row.cells[cellCtr].children[inputCtr];
                if( input.id.substring(input.id.length - elemTypeId.length, input.id.length) == elemTypeId )
                {
                    var selectField = input;
                    if( selectField.selectedIndex > -1)
                    {
                        perDiem = ( selectField.options[selectField.selectedIndex].text * 1 )
                    }   
                    break;
                }
            }
            if( perDiem > 0 )
            {
                break;
            }
        }
        for( var cellCtr = 0; cellCtr < row.cells.length; cellCtr++)
        {
            var dayCell = row.cells[cellCtr];
            var calcRow = row.parentElement.rows[row.rowIndex-1];
             if( dayCell.children.length > 0 )
             {
                if((dayCell.children[0].tagName == "INPUT") && ( dayCell.children[0].id.substring ( dayCell.children[0].id.length - 4, dayCell.children[0].id.length ) == "Data" ))
                {
                    var input = dayCell.children[0];
                    for( var ctr = 0; ctr < calcRow.cells.length; ctr++ )
                    {
                        if( calcRow.cells[ctr].children.length > 0 )
                        {
                            var calcCell = calcRow.cells[ctr];
                            if( calcCell.children[0].id.substring(calcCell.children[0].id.length-4,calcCell.children[0].id.length) == input.id.substring(input.id.length-7,input.id.length-4) + "B" )
                            {
                                var amtElem = calcCell.children[0];
                                var days =  (input.value.length > 0) ? ( input.value * 1 ) : 0;
                                var amt = 0;
                                
                                if( days > 1 )
                                {
                                   amt = ((( days - 2 ) * perDiem ) + (2 * (.75 * perDiem )));
                                }
                                else
                                {
                                    amt = ( days * .75 * perDiem )
                                }
                                
                                amtElem.value = amt;
                                break;
                            }
                        }   
                    }
                }   
            }
        }
        if( calcRow != null )
        {
            getPerDiemTotals(elem, "B", calcRow)
        }
        getExpenseTotals(elem, "B")
    }
    catch(ex)
    {
        return false;
    }        
}


function getMileageCalc(elem, elemTypeId)
{
    try
    {
        if(( elemTypeId != selection ) && (selection.length > 0 ))
        {
            switch( selection )
            {
                case "lodgingId":
                case "mealsId":
                    getPerDiemCalc(elem, selection);
                    return;
                break;
                
                case "milesId":
                    elemTypeId = selection;
                break;
                
                default:
                    getExpenseTotals(elem, "B");
                    return;
                break;
            }
        }    
        var col = elem.parentElement;
        var row = col.parentElement;
        var mileageRate = mileage;
        for( var cellCtr = 0; cellCtr < row.cells.length; cellCtr++)
        {
            var dayCell = row.cells[cellCtr];
            var calcRow = row.parentElement.rows[row.rowIndex-1];
             if( dayCell.children.length > 0 )
             {
                if((dayCell.children[0].tagName == "INPUT") && ( dayCell.children[0].id.substring ( dayCell.children[0].id.length - 4, dayCell.children[0].id.length ) == "Data" ))
                {
                    var input = dayCell.children[0];
                    for( var ctr = 0; ctr < calcRow.cells.length; ctr++ )
                    {
                        if( calcRow.cells[ctr].children.length > 0 )
                        {
                            var calcCell = calcRow.cells[ctr];
                            if( calcCell.children[0].id.substring(calcCell.children[0].id.length-4,calcCell.children[0].id.length) == input.id.substring(input.id.length-7,input.id.length-4) + "B" )
                            {
                                var amtElem = calcCell.children[0];
                                var miles = (input.value.length > 0) ? ( input.value * 1 ) : 0;
                                amt = (miles * mileageRate);
                                amtElem.value = amt;
                                break;
                            }
                        }   
                    }
                }   
            }
        }
        getExpenseTotals(elem, "B")
    }
    catch(ex)
    {
        return false;
    }
    
}


function toggleRow(elem)
{
    try
    {
        var col = elem.parentElement;
        var row = col.parentElement;
        var toggleRow =  row.parentElement.rows[row.rowIndex+1];
        switch( elem.value )
        {
            case "6":
                selection = "mealsId";
                toggleRow.cells[0].style.display = "inline";
                toggleRow.cells[1].style.display = "inline";
                toggleRow.cells[2].style.display = "none";
                resetRowValues(row,true);
                resetRowValues(toggleRow,false);
                toggleRow.style.display = "inline";
                break;
                
            case "11":
                selection = "lodgingId"
                toggleRow.cells[0].style.display = "inline";
                toggleRow.cells[1].style.display = "inline";
                toggleRow.cells[2].style.display = "none";
                resetRowValues(row, true);
                resetRowValues(toggleRow, false);
                toggleRow.style.display = "inline";
            break;
            
            case "3":
                selection = "milesId";
                toggleRow.cells[0].style.display = "none";
                toggleRow.cells[1].style.display = "none";
                toggleRow.cells[2].style.display = "inline";
                toggleRow.cells[2].innerText = "Mileage";
                resetRowValues(row,true);
                resetRowValues(toggleRow,false);
                toggleRow.style.display = "inline";
            break;
            
             case "4":
                selection = "otherId";
                toggleRow.cells[0].style.display = "none";
                toggleRow.cells[1].style.display = "none";
                toggleRow.cells[2].style.display = "inline";
                toggleRow.cells[2].innerText = "Description";
                resetRowValues(row, false);
                resetRowValues(toggleRow, false);
                toggleRow.style.display = "inline";
            break;
            
            default:
                selection = "";
                toggleRow.cells[0].style.display = "none";
                toggleRow.cells[1].style.display = "none";
                toggleRow.cells[2].style.display = "none";
                resetRowValues(row, false);
                resetRowValues(toggleRow, false);
                toggleRow.style.display = "none"
            break;
        }
        selectionChange(elem);
        getExpenseTotals(elem, "B", row)
        var expenseTotal = updateExpenseTotal(elem, "Total");
        document.all.sheetTotal.value = expenseTotal;
    }
    catch(ex)
    {
        return false;
    }
}


function selectionChange(elem)
{
    try
    {
        switch (selection )
        {
            case "lodgingId":
            case "mealsId":
                getPerDiemCalc(elem, selection);
                break;
            
            case "milesId":
                getMileageCalc( elem, selection );
            break;
            
            default:
                selection = "";
            break;
        }
    }
    catch(ex)
    {
        return false;
    }
}

function resetRowValues(row, bReadOnly)
{
    try
    {
        for( var cellCtr = 0; cellCtr < row.cells.length; cellCtr++)
        {
            var cell = row.cells[cellCtr];
            
             if( cell.children.length > 0 )
             {
                if(cell.children[0].tagName == "INPUT")
                {
                    var input = cell.children[0];
                    input.value = "";
                    input.readOnly = bReadOnly
                }   
            }
        }
    }
    catch(ex)
    {
        return false;
    }
}

/**********************************/

function navigateWeek()
{
    
}