$('#addrow').click(function () {
    var itr = Number($('#itr').val());
    var row = '<tr id="row' + itr + '">' +
            '      <td><input type="text" class="form-control positivefloat totalFrom" name="total_from' + itr + '" id="total_from' + itr + '" data-itr="' + itr + '"/></td>' +
            '      <td><input type="text" class="form-control positivefloat totalTo" name="total_to' + itr + '" id="total_to' + itr + '" data-itr="' + itr + '"/></td>' +
            '      <td><input type="text" class="form-control positivefloat percentage" name="discount' + itr + '" id="discount' + itr + '" data-itr="' + itr + '"/></td>' +
            '      <td><button type="button" class="close flt-none removeicon removeIt" data-itr="' + itr + '">×</button></td>' +
            '  </tr>';
    $('#discountsTable tbody').append(row);
    itr++;
    $('#itr').val(itr)
});
$('.removeIt').live('click', function () {
    $(this).parent().parent().remove();
});
$('.percentage').live('click', function () {
    if (Number($(this).val()) > 100) {
        $(this).val(100);
    }
});
$('.totalFrom, .totalTo').live('blur', function () {//check no range overlap
    checkRangeOverlap($(this));
});

$('#saveIt').live('click', function () {
    if (!$(this).hasClass('processing...')) {
        $(this).addClass('processing...');

        var rowsCount = Number($('#itr').val());
        //remove empty rows
        for (var i = 1; i <= rowsCount; i++) {
            if ($('#total_from' + i).length > 0 && Number($('#total_from' + i).val()) == 0 && Number($('#total_to' + i).val()) == 0 && Number($('#discount' + i).val()) == 0) {
                $('#row' + i).remove();
            }
        }

        //make empty required
        var flagReq = 1;
        for (var i = 1; i <= rowsCount; i++) {
            if ($('#total_from' + i).length > 0 && (Number($('#total_from' + i).val()) == 0 || Number($('#total_to' + i).val()) == 0 || Number($('#discount' + i).val()) == 0)) {
                flagReq = 0;
                if (Number($('#total_from' + i).val()) == 0)
                    $('#total_from' + i).css('background-color', 'yellow');
                if (Number($('#total_to' + i).val()) == 0)
                    $('#total_to' + i).css('background-color', 'yellow');
                if (Number($('#discount' + i).val()) == 0)
                    $('#discount' + i).css('background-color', 'yellow');
            } else {
                if (Number($('#total_from' + i).val()) == 0)
                    $('#total_from' + i).css('background-color', 'white');
                if (Number($('#total_to' + i).val()) == 0)
                    $('#total_to' + i).css('background-color', 'white');
                if (Number($('#discount' + i).val()) == 0)
                    $('#discount' + i).css('background-color', 'white');
            }
        }
        //check overlaping
        var flagOver = 1;
        $('.totalFrom').each(function () {
            var returnedflag = checkRangeOverlap($(this));
            if (returnedflag == 0) {
                flagOver = 0;
                return false;
            }
        });

        if (flagReq == 1 && flagOver == 1) {
            $('#discountcontrolForm').submit();
        } else {
            $('#saveIt').removeClass('processing...');
        }

    }
});
$(function () {
    if ($('.totalFrom').length < 1) {
        $('#addrow').click();
    }
});


function checkRangeOverlap(thisIs) {
    var rowFlag = 1;
    if ($('.totalFrom').length > 1 && thisIs.val() != "") {
        var itr = thisIs.data('itr');
        var thisfrom = Number($('#total_from' + itr).val());
        var thisto = Number($('#total_to' + itr).val());
        if (thisfrom != 0 && thisto != 0) {
            var rowsCount = Number($('#itr').val());
            for (var i = 1; i <= rowsCount; i++) {
                if (i != itr && $('#total_from' + i).length > 0 && Number($('#total_from' + i).val()) != 0 && Number($('#total_to' + i).val()) != 0) {
                    var loopfrom = Number($('#total_from' + i).val());
                    var loopto = Number($('#total_to' + i).val());
                    if (thisto >= thisfrom && ((loopfrom < thisfrom && loopto <= thisto) || (loopfrom >= thisto && loopto > thisto))) {
                        //this is ok
                    } else {
                        rowFlag = 0;
                        break;
                    }
                }
            }
        }
    }

    if (rowFlag == 1) {
        $('#row' + itr).css('background-color', 'white');
    } else {
        $('#row' + itr).css('background-color', 'red');
    }
    return rowFlag;
}


