function validateSpreadsheetDate(dateInput, minYear, maxYear) {
    var month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Oct', 'Nov', 'Dec'];
    var match = dateInput.match(/^(\d{1,2})\-(Jan|Feb|Mar|May|Apr|Jul|Jun|Aug|Oct|Sep|Nov|Dec)\-(\d{4})$/);

    if (match) {
        var dateArray = dateInput.split("-");
        var monthNum = -1;
        for (var i = 0; i < month.length; i++) {
            if (month[i] == dateArray[1]) {
                monthNum = i;
            }
        }
        if (monthNum != -1) {
            var date = new Date(dateArray[2], monthNum, dateArray[0]);

            var d = date.getDate();
            var m = month[date.getMonth()];
            var y = date.getFullYear();

            var dateStr = d + "-" + m + "-" + y;

            if (minYear <= y && y <= maxYear && (dateInput == dateStr || (d < 10 && dateInput == "0" + dateStr))) {
                return true;
            }
        }
    }

    if (dateInput == "") {
        return true;
    }

    return false;
}

function toogleSpreadsheet(spreadsheetId, rowId, isReadOnly) {
    var spreadsheet = $('#' + spreadsheetId + '_spreadsheetContent');
    if ($(spreadsheet).is(":visible")) {
        $(spreadsheet).hide(200);
    }
    else {
        $(spreadsheet).show(200, function () {
            var spreadsheetHeight = $(spreadsheet).children('table').first().innerHeight();
            var spreadsheetWidth = $(spreadsheet).children('table').first().innerWidth();

            var height = (spreadsheetHeight > 500)
                ? 500
                : (spreadsheetWidth > 530)
                    ? spreadsheetHeight + 17 //17 = scrollbar height
                    : spreadsheetHeight;

            $(spreadsheet).height(height);
        });

        if (isReadOnly != true) {
            var numberOfVisibleColumns = $('#iForm_' + spreadsheetId).find('tr:first').find('td:visible').length - 1;
            if (numberOfVisibleColumns == 0) {
                $('#iForm_' + spreadsheetId).find('tr:first').find('td:hidden').each(function() {
                    var curClassName = $(this).attr('class');
                    if (curClassName != null && curClassName.indexOf('_new') != -1) {
                        var x = curClassName.split('_');
                        var curColumnId = x[1];
                        editColumnTitle(spreadsheetId, rowId, curColumnId, 'new');
                        return false;
                    }
                    return true;
                });
            }
        }
    }
}

function confirmClearSpreadsheet(spreadsheetId, rowId, msg) {
    var response = confirm(msg);
    if (response == true) {
        clearSpreadsheet(spreadsheetId, rowId);
    }
}

function clearSpreadsheet(spreadsheetId, rowId) {
    $('#iForm_' + spreadsheetId).find('tr:first').find('td').each(function () {
        var curClassName = $(this).attr('class');
        if (curClassName != null) {
            var x = curClassName.split('_');
            var columnId = x[1];
            var type = x[2];
            var id = spreadsheetId + "_" + rowId + "_" + columnId + "_" + type;
            var className = spreadsheetId + "_" + columnId + "_" + type;
            var isDefaultColumn = ($('#' + id + '_colIsDefault').val() == "1");

            clearColumn(spreadsheetId, rowId, columnId, type);

            switch (type) {
                case "fitem":
                    setDefaultColumnValue(spreadsheetId, rowId, columnId, type);
                    $('#' + id + '_colStatus').val('fitem');
                    $('.' + className).show();
                    break;
                case "pitem":
                    if (isDefaultColumn) {
                        setDefaultColumnValue(spreadsheetId, rowId, columnId, type);
                        $('#' + id + '_colStatus').val('pitem');
                        $('.' + className).show();
                    } else {
                        $('#' + id + '_colStatus').val('deleted');
                        $('.' + className).hide();
                    }
                    break;
                case "new":
                    $('#' + id + '_colStatus').val('new');
                    $('.' + className).hide();
                    break;
            }
        }
    });
}

function clearColumn(spreadsheetId, rowId, columnId, type) {
    var id = spreadsheetId + "_" + rowId + "_" + columnId + "_" + type;
    var className = spreadsheetId + "_" + columnId + "_" + type;

    $('#' + id + '_colHeaderLink').html('');
    $('#' + id + '_colValue').val('');
    $('#iForm_' + id).val('');
    $('#iForm_' + id + 'Month').val('');
    $('#iForm_' + id + 'Day').val('');
    $('#iForm_' + id + 'Year').val('');
    $('.' + className).find(':input').each(function () {
        var curName = $(this).attr('name');
        if (curName.indexOf('_colStatus') == -1 &&
            curName.indexOf('_colIsDefault') == -1 &&
            curName.indexOf('_colDefault') == -1 &&
            curName.indexOf('_colOrder') == -1) {
            $(this).val('');
        }
    });
}

function setDefaultColumnValue(spreadsheetId, rowId, columnId, type) {
    var id = spreadsheetId + "_" + rowId + "_" + columnId + "_" + type;
    var value = $('#' + id + '_colDefault').val();

    $('#' + id + '_colHeaderLink').html(value);
    $('#' + id + '_colValue').val(value);
    $('#iForm_' + id).val(value);

    var x = value.split('/');
    if (x.length == 3) {
        $('#iForm_' + id + 'Month').val(x[0]);
        $('#iForm_' + id + 'Day').val(x[1]);
        $('#iForm_' + id + 'Year').val(x[2]);
    } else {
        $('#iForm_' + id + 'Month').val('');
        $('#iForm_' + id + 'Day').val('');
        $('#iForm_' + id + 'Year').val('');
    }
}

function editColumnTitle(spreadsheetId, rowId, columnId, type) {
    var id = spreadsheetId + "_" + rowId + "_" + columnId + "_" + type;
    $('#' + id + '_colHeader').jqmShow();
}

function okColumnTitle(spreadsheetId, rowId, columnId, type, errMsg) {
    var id = spreadsheetId + "_" + rowId + "_" + columnId + "_" + type;
    var className = spreadsheetId + "_" + columnId + "_" + type;
    var value = $('#iForm_' + id).val();
    var encodedValue = value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
    if (value != '') {
        $('#' + id + '_colHeaderLink').html(encodedValue);
        $('#' + id + '_colValue').val(value);
        $('#' + id + '_colHeader').jqmHide();
        $('.' + className).show();
    } else {
        alert(errMsg);
    }
}

function cancelColumnTitle(spreadsheetId, rowId, columnId, type) {
    var id = spreadsheetId + "_" + rowId + "_" + columnId + "_" + type;
    var value = $('#' + id + '_colValue').val();
    $('#iForm_' + id).val(value);

    var x = value.split('/');
    if (x.length == 3) {
        $('#iForm_' + id + 'Month').val(x[0]);
        $('#iForm_' + id + 'Day').val(x[1]);
        $('#iForm_' + id + 'Year').val(x[2]);
    } else {
        $('#iForm_' + id + 'Month').val('');
        $('#iForm_' + id + 'Day').val('');
        $('#iForm_' + id + 'Year').val('');
    }

    $('#' + id + '_colHeader').jqmHide();
}

function addColumn(spreadsheetId, rowId, limitChildAnswers, maxColumnMsg, maxPerOpMsg) {
    var tdAvailable = false;
    var numberOfVisibleColumns = $('#iForm_' + spreadsheetId).find('tr:first').find('td:visible').length - 1;

    if (limitChildAnswers == 0 || numberOfVisibleColumns < limitChildAnswers) {
        $('#iForm_' + spreadsheetId).find('tr:first').find('td:hidden').each(function () {
            var curClassName = $(this).attr('class');
            if (curClassName != null && curClassName.indexOf('_new') != -1) {
                var x = curClassName.split('_');
                var curColumnId = x[1];
                editColumnTitle(spreadsheetId, rowId, curColumnId, 'new');
                tdAvailable = true;
                return false;
            }
            return true;
        });
    }
    if (tdAvailable == false) {
        var numberOfColumns = $('#iForm_' + spreadsheetId).find('tr:first').find('td:visible').length - 1;

        if (limitChildAnswers == 0) {
            alert(maxPerOpMsg);
        } else if (numberOfColumns == limitChildAnswers) {
            alert(maxColumnMsg);
        } else {
            alert(maxPerOpMsg);
        }
    }
}

function confirmDeleteColumn(spreadsheetId, rowId, columnId, type, msg) {
    var response = confirm(msg);
    if (response == true) {
        deleteColumn(spreadsheetId, rowId, columnId, type);
    }
}

function deleteColumn(spreadsheetId, rowId, columnId, type) {
    var id = spreadsheetId + "_" + rowId + "_" + columnId + "_" + type;
    var className = spreadsheetId + "_" + columnId + "_" + type;

    var value = $('#' + id + '_colStatus').val();
    if (value == 'new') {
        clearColumn(spreadsheetId, rowId, columnId, type);

        $('.' + className).hide();
        $('.' + className).each(function () {
            var siblingTd = $(this).parent().find('td');
            if (!$(this).is(siblingTd.eq(siblingTd.length - 1))) {
                $(this).insertAfter(siblingTd.eq(siblingTd.length - 1));
            }
        });

        var colOrder = 0;
        $('#iForm_' + spreadsheetId).find('tr:first').find('td').each(function () {
            var curClassName = $(this).attr('class');
            if (curClassName != null && curClassName.indexOf('_new') != -1) {
                colOrder++;
                var x = curClassName.split('_');
                var curColumnId = x[1];
                $('#' + spreadsheetId + '_' + rowId + '_' + curColumnId + '_new_colOrder').val(colOrder);
            }
        });
    } else {
        $('#' + id + '_colStatus').val('deleted');
        $('.' + className).hide();
    }
}