uzCustomCalcPaitConsumption = {
    debug: 0,

    hData: {},
    blockPrefix: "",

    //resultPostfix: "",

    squareText: "",
    laysText: "",
    laysCoef: 0.7,

    init: function(){

        // Enable debug by ip
        if(typeof(DEBUG_BY_IP) != 'undefined'){
            if(DEBUG_BY_IP == 1){
                this.debug = 1;
            }
        }

        // Set options
        if(typeof(uzCustomCalcPaitConsumptionOptions) != 'undefined'){
            this.setOptions(uzCustomCalcPaitConsumptionOptions);
        }

        this.onFieldBlur(this.blockPrefix, "square");
        this.onFieldBlur(this.blockPrefix, "lays");

        // Auto recalc on load
        //if(typeof(this.hData[this.blockPrefix]) != 'undefined'){
        //    this.recalc();
        //}

    },

    setOptions: function(opt){
        for(var key in opt){
            this[key] = opt[key];
        }
    },

    onFormFieldChanged: function(blockPrefix, fieldName){
        this.blockPrefix = blockPrefix;

        if(typeof(this.hData[this.blockPrefix]) != 'undefined'){
            this.recalc();
        }
    },

    onFieldFocus: function(blockPrefix, fieldName){
        this.blockPrefix = blockPrefix;
        var val = "";
        if(document.getElementById(this.blockPrefix + fieldName)){
            val = document.getElementById(this.blockPrefix + fieldName).value;
        }
        var phText = "";
        switch(fieldName){
            case "square":
                phText = this.squareText;
                break;
            case "lays":
                phText = this.laysText;
                break;
            default:
                break;
        }

        if(val == phText){
            document.getElementById(this.blockPrefix + fieldName).value = "";
        }
    },

    onFieldBlur: function(blockPrefix, fieldName){
        this.blockPrefix = blockPrefix;
        var val = "";
        if(document.getElementById(this.blockPrefix + fieldName)){
            val = document.getElementById(this.blockPrefix + fieldName).value;
        }
        var phText = "";
        switch(fieldName){
            case "square":
                phText = this.squareText;
                break;
            case "lays":
                phText = this.laysText;
                break;
            default:
                break;
        }

        if(val == ""){
            document.getElementById(this.blockPrefix + fieldName).value = phText;
        }
    },

    recalc: function(){
        var res = "";
        var units = "";
        var product = this.getFieldVal("product");
        var square = this.getFieldVal("square");
        var lays = this.getFieldVal("lays");
        var hProdData = this.getProductData(product);
        if(hProdData){
            var cons = hProdData.cons / 1;
            var units = hProdData.units;
            if(hProdData != false && square > 0 && lays > 0){
                var laysCoef = (lays > 1) ? 1 + ((lays-1) * this.laysCoef) : 1;
                res = Math.ceil(square * cons * laysCoef);
                //res = square * cons * laysCoef;
            }
        }
        this.setResult(res, units);
    },


    getFieldVal: function(fieldName){
        var res = 0;
        if(document.getElementById(this.blockPrefix + fieldName)){
            res = document.getElementById(this.blockPrefix + fieldName).value;
            res = this.validateField(fieldName, res);
        }
        return res;
    },


    validateField: function(fieldName, val){
        var res = 0;
        switch(fieldName){
            case "product":
                res = this.validateProduct(fieldName, val);
                break;
            case "square":
                res = this.validateSquare(fieldName, val);
                break;
            case "lays":
                res = this.validateLays(fieldName, val);
                break;
            default:
                break;
        }
        return res;
    },

    validateProduct: function(fieldName, val){
        var res = 0;
        if(val != ""){
            res = val;
        }
        return res;
    },

    validateSquare: function(fieldName, val){
        var res = 0;
        if(val != "" && val != this.squareText){
            var re1 = /[^0-9.,]+/ig;
            val = val.replace(re1, "");
            val = val.replace(',', '.');
            //alert(val);
            var aVal = val.split('.');
            res = 0;
            if(aVal[0].length){
                res = aVal[0]/1;
            }
            if(aVal.length > 1){
                res = (res+"."+aVal[1])/1;
            }
            //var re2 = /^[0-9.,]+/ig;
            //val = val.match(re2);
            //res = val / 1;
        }
        // Set validated value
        if(res > 0){
            this.setFieldVal(fieldName, res);
        }

        return res;
    },

    validateLays: function(fieldName, val){
        var res = 1;
        if(val != "" && val != this.laysText){
            var re1 = /[^0-9.,]+/ig;
            val = val.replace(re1, "");
            val = val.replace(',', '.');
            //alert(val);
            var aVal = val.split('.');
            res = 0;
            if(aVal[0].length){
                res = aVal[0]/1;
            }
            if(aVal.length > 1){
                res = (res+"."+aVal[1])/1;
            }
            res = Math.ceil(res);

        }
        // Set validated value
        if(res > 0){
            this.setFieldVal(fieldName, res);
        }

        return res;
    },


    setFieldVal: function(fieldName, val){
        if(document.getElementById(this.blockPrefix + fieldName)){
            document.getElementById(this.blockPrefix + fieldName).value = val;
        }
    },


    
    
    getProductData: function(id){
        if(this.hData[this.blockPrefix].length){
            for(var i = 0; i < this.hData[this.blockPrefix].length; i++){
                if(this.hData[this.blockPrefix][i].id == id){
                    return this.hData[this.blockPrefix][i];
                }
            }
        }
        return false;
    },


    setResult: function(val, units){
        if(document.getElementById(this.blockPrefix + "result")){
            if(val != ""){
                val = val + units;
            }
            document.getElementById(this.blockPrefix + "result").innerHTML = val;
        }
    },


/*
    inArray: function(val, aVals){
        var res = 0;
        if(aVals.length){
            for(var i = 0; i < aVals.length; i++){
                if(aVals[i] == val){
                    res = 1;
                    break;
                }
            }
        }
        return res;
    },
//*/

    endvar: 0
}

//
// Init
//

uzCustomCalcPaitConsumption.init();

