/**
 * Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
 *
 * Licensed under CLOUD LINUX LICENSE AGREEMENT
 * http://cloudlinux.com/docs/LICENSE.TXT
 */
(function(w){

    function validateInt(data, min, max) {
        var int_patt = /^[0-9]+$/,
            value;
        if (data.replace(/\s/g, "").localeCompare('DEFAULT') == 0){
            return true;
        }
        if (data.replace(/\s/g, "").localeCompare('') == 0){
            return true;
        }
        if (!int_patt.test(data)) {
            return false;
        }
        value = Number(data);
        if ((value < min ) || (value > max)) {
            return false;
        }
        return true;
    }

    function validateMem(data, min, max) {
    var match_array = data.match(/^(?:\d*\.)?\d+([kgmt])?$/i),
            value;
        if (data.replace(/\s/g, "").localeCompare('DEFAULT') == 0){
            return true;
        }
        if (data.replace(/\s/g, "").localeCompare('') == 0){
            return true;
        }
        if (match_array === null) {
            return false;
        }
        value = Number(match_array[1]);
        if ((value < min ) || (value > max)) {
            return false;
        }
        return true;
    }

    function printMessage(key) {
        switch(key) {
        case 'cpu':
            return translateDict['Must be in range 1 - 100 if used old format; use % or mhz|ghz to set cpu limit as speed.'] + "\n";
        case 'pmem':
        case 'mem':
            return translateDict['Memory must be positive. Postfix allowed only in (KGMT).'] + "\n";
        default:
            return translateDict['Must be positive.'] + "\n";
        }
    }

    function validateCpu(value) {
       var cpu_pattern = /\d{1,2}0?$/,   // 1-100
           speed_percent_pattern = /\d+(?:\.\d+)?%$/, //  *%
           speed_hz_pattern = /\d+(?:\.\d+)?(mhz|ghz)+$/i;  // *mhz\ghz
        if (value.replace(/\s/g, "").localeCompare('DEFAULT') == 0){
            return true;
        }
        if (value.replace(/\s/g, "").localeCompare('') == 0){
            return true;
        }

        match_array = value.match(cpu_pattern);
        if (match_array != null) {
           if (validateInt(value,1,100)) {
              return true;
           }
           else  {
              return false;
           }
        }
        match_array = value.match(speed_percent_pattern);
        if (match_array != null) {
           return true;
        }
        match_array = value.match(speed_hz_pattern);
        if (match_array != null) {
           return true;
        }
        return false;
    }

    function validateMemory(value) {
        return validateMem(value,0,Number.MAX_VALUE);
    }

    function validateOther(value) {
        return validateInt(value,0,Number.MAX_VALUE);
    }

   var d = w.document,
        main = d.getElementById("main"),
        dispatcher = {
            "cpu":validateCpu,
            "pmem":validateMemory,
            "mem":validateMemory,
            "ep":validateOther,
            "nproc":validateOther,
            "io":validateOther,
            "iops":validateOther
        };
    main.onchange = function(e){
        var tgt = e.target,
            prefix = 'lve_',
            input_id = tgt.getAttribute("id");
        if (tgt.tagName === 'INPUT' && tgt.getAttribute("class") === "lve") {
            if (input_id.indexOf(prefix) != -1) {
                var key = input_id.substring(prefix.length);
                if (!dispatcher[key](tgt.value)) {
                    alert(key+' => ' + tgt.value + '. ' + translateDict['Incorrect limits'] + '. ' + printMessage(key));
                    tgt.value = '';
                }
            }
        }
    };
}(window));
