function getDisplayDate(date) {
    return date.toString('yyyy-MM-dd');
}

function displayAmount(num) {
    var temp = num * 100;
    temp = parseInt(temp);
    temp = temp / 100;
    temp = temp.toString();

    if (temp.indexOf(".") == - 1) temp += ".00";

    return temp;
}

function getInputsTableRow(trans) {
    var html = '';
    
    
    if (trans.getType() === "income") {
        html += '<tr id="i_' + trans.getId() + '" class="income">';
    } else {
        html += '<tr id="i_' + trans.getId() + '">';
    }
    

    html += '<td><input type="text" name="name" value="' + trans.getName() + '" class="nameInput"></td>';
    html += '<td><input type="text" name="amount" value="' + trans.getAmount() + '" class="amountInput"></td>';
    html += '<td><input type="text" name="date" value="' + trans.getDate() + '" class="dateInput"></td>';
    html += '<td><input type="text" name="description" value="' + trans.getDescription() + '" class="descInput"></td>';
    html += '<td>' + getCategoriesDropDown(trans.getCategory()) + '</td>';
    html += '<td><input type="button" name="add" class="addInput" value="Lägg till"><input type="button" class="deleteInput" name="delete" value="Ta bort"></td>';
    html += '</tr>';

    return html;
}


/**
 * Parses transactionsJSON
 * @param transactions in JSON
 * @returns An array with transactions
 * @type transactions
 */

function parseJSON(json, type) {
    var transactions = [];
    
    
    if (type === "expenses") {
        $.each(json, function () {
            var temp = new Transaction();
            temp.parseJSON(this);
            
            if(temp.getType() === "expense") {
                transactions.push(temp);
            }
        });
    }
    
    if (type === "incomes") {
        $.each(json, function () {
            var temp = new Transaction();
            temp.parseJSON(this);
            
            if(temp.getType() === "income") {
                transactions.push(temp);
            }
        });
    }

    return transactions;
}


function getMonthTranspackSelectors(from, select) {
    
    
    var start = from.moveToFirstDayOfMonth();
    var today = new Date();
    today.moveToFirstDayOfMonth();

    var selectors = Array();

    while (start < today) {

        var temp = new Transpackselector();

        temp.setStart(new Date(start));
        temp.setEnd(new Date(start).moveToLastDayOfMonth());
        
        if(select !== undefined) {
            if (select.bankAccount !== undefined) {
                temp.setBankAccount(select.bankAccount);
            }
        }
        
        
        
        
        
        selectors.push(temp);
        start.addMonths(1);
    }

    // remove last
    return selectors;
}

function message(messageToDisplay, type, time) {
    var timer = 1500;
    var typer = "notice";

    if (time !== undefined) {
        timer = time * 1000;
    }

    if (type !== undefined) {
        typer = type;
    }



    var settings = {
        life: timer,
        theme: typer,
        closer: false
    };

    $.jGrowl(messageToDisplay, settings);
}

function ajaxBeforeSend(xml) {
    $('#ajaxSpinner').show();
}

function ajaxComplete(xml, status) {
    $('#ajaxSpinner').hide();
}

function ajaxError(xml, status) {
    message("Något gick snett i kommunikationen med servern", "error");
}

/**
 * Return a dropdown with categories
 * @param String Category chosen
 * @returns a dropdown with categories
 * @type html
 */

function getCategoriesDropDown(category, id) {
    var html = '';
    if (id !== undefined) {
        html += '<select name="category" id="' + id + '">';
    } else {
        html += '<select name="category">';
    }


    $(storage.categories).each(function () {
        if (this.category === category) {
            html += '<option value="' + this.category + '" selected="selected">' + this.category + '</option>';
        } else {
            html += '<option value="' + this.category + '">' + this.category + '</option>';
        }
    });

    html += '</select>';
    return html;
}
