/**
 * Runtime Calculator.
 * Version: 1.0
 *
 * The use of this script is strictly forbidden without prior written
 * consent. You must obtain permission before copying, modifying, 
 * redistributing, deriving or selling any part of this program.
 *
 * Copyright (c) 2008 HYPERZOID
 * All Rights Reserved
 */


// The Calculator object.
var calculator = null;


/**
 * Initializes this program.
 */
function initCalculator() {
    calculator = new Calculator();
}


/**
 * The constructor.
 */
function Calculator() {
    // Information for all UPS models.
    this.models = new Array();
    for (var i = 0; i < 18; i++) {
        this.models[i] = new Array();
    }

    // Vesta525.
    this.models[0]['watts']      = 315;
    this.models[0]['va']         = 525;
    this.models[0]['pf']         = 0.6;
    this.models[0]['efficiency'] = 0.88;
    this.models[0]['ah']         = 7;
    this.models[0]['vdc']        = 12;
    this.models[0]['batteries']  = 1;

    // Vesta625.
    this.models[1]['watts']      = 375;
    this.models[1]['va']         = 625;
    this.models[1]['pf']         = 0.6;
    this.models[1]['efficiency'] = 0.88;
    this.models[1]['ah']         = 7;
    this.models[1]['vdc']        = 12;
    this.models[1]['batteries']  = 1;

    // Vesta1000.
    this.models[2]['watts']      = 600;
    this.models[2]['va']         = 1000;
    this.models[2]['pf']         = 0.6;
    this.models[2]['efficiency'] = 0.88;
    this.models[2]['ah']         = 7;
    this.models[2]['vdc']        = 12;
    this.models[2]['batteries']  = 2;

    // Vesta1500.
    this.models[3]['watts']      = 900;
    this.models[3]['va']         = 1500;
    this.models[3]['pf']         = 0.6;
    this.models[3]['efficiency'] = 0.88;
    this.models[3]['ah']         = 9;
    this.models[3]['vdc']        = 12;
    this.models[3]['batteries']  = 2;

    // Jupiter Pro 1000.
    this.models[4]['watts']      = 600;
    this.models[4]['va']         = 1000;
    this.models[4]['pf']         = 0.6;
    this.models[4]['efficiency'] = 0.88;
    this.models[4]['ah']         = 7;
    this.models[4]['vdc']        = 12;
    this.models[4]['batteries']  = 2;

    // Jupiter Pro 1500.
    this.models[5]['watts']      = 900;
    this.models[5]['va']         = 1500;
    this.models[5]['pf']         = 0.6;
    this.models[5]['efficiency'] = 0.88;
    this.models[5]['ah']         = 9;
    this.models[5]['vdc']        = 12;
    this.models[5]['batteries']  = 2;

    // Jupiter Pro 2000.
    this.models[6]['watts']      = 1200;
    this.models[6]['va']         = 2000;
    this.models[6]['pf']         = 0.6;
    this.models[6]['efficiency'] = 0.88;
    this.models[6]['ah']         = 7;
    this.models[6]['vdc']        = 12;
    this.models[6]['batteries']  = 4;

    // Jupiter Pro 3000.
    this.models[7]['watts']      = 1800;
    this.models[7]['va']         = 3000;
    this.models[7]['pf']         = 0.6;
    this.models[7]['efficiency'] = 0.88;
    this.models[7]['ah']         = 9;
    this.models[7]['vdc']        = 12;
    this.models[7]['batteries']  = 4;

    // Jupiter Pro XL 1000.
    this.models[8]['watts']      = 600;
    this.models[8]['va']         = 1000;
    this.models[8]['pf']         = 0.6;
    this.models[8]['efficiency'] = 0.88;
    this.models[8]['ah']         = 7;
    this.models[8]['vdc']        = 12;
    this.models[8]['batteries']  = 4;

    // Jupiter Pro XL 1500.
    this.models[9]['watts']      = 900;
    this.models[9]['va']         = 1500;
    this.models[9]['pf']         = 0.6;
    this.models[9]['efficiency'] = 0.88;
    this.models[9]['ah']         = 9;
    this.models[9]['vdc']        = 12;
    this.models[9]['batteries']  = 4;

    // Jupiter Pro XL 2000.
    this.models[10]['watts']      = 1200;
    this.models[10]['va']         = 2000;
    this.models[10]['pf']         = 0.6;
    this.models[10]['efficiency'] = 0.88;
    this.models[10]['ah']         = 7;
    this.models[10]['vdc']        = 12;
    this.models[10]['batteries']  = 8;

    // JupiterPro XL 3000.
    this.models[11]['watts']      = 1800;
    this.models[11]['va']         = 3000;
    this.models[11]['pf']         = 0.6;
    this.models[11]['efficiency'] = 0.88;
    this.models[11]['ah']         = 9;
    this.models[11]['vdc']        = 12;
    this.models[11]['batteries']  = 8;

    // Mars1000 RT PRO.
    this.models[12]['watts']      = 700;
    this.models[12]['va']         = 1000;
    this.models[12]['pf']         = 0.7;
    this.models[12]['efficiency'] = 0.88;
    this.models[12]['ah']         = 7;
    this.models[12]['vdc']        = 12;
    this.models[12]['batteries']  = 3;

    // Mars2000 RT PRO.
    this.models[13]['watts']      = 1400;
    this.models[13]['va']         = 2000;
    this.models[13]['pf']         = 0.7;
    this.models[13]['efficiency'] = 0.88;
    this.models[13]['ah']         = 7;
    this.models[13]['vdc']        = 12;
    this.models[13]['batteries']  = 6;

    // Mars3000 RT PRO.
    this.models[14]['watts']      = 2100;
    this.models[14]['va']         = 3000;
    this.models[14]['pf']         = 0.7;
    this.models[14]['efficiency'] = 0.88;
    this.models[14]['ah']         = 7;
    this.models[14]['vdc']        = 12;
    this.models[14]['batteries']  = 8;

    // Mars6000.
    this.models[15]['watts']      = 4200;
    this.models[15]['va']         = 6000;
    this.models[15]['pf']         = 0.7;
    this.models[15]['efficiency'] = 0.88;
    this.models[15]['ah']         = 7;
    this.models[15]['vdc']        = 12;
    this.models[15]['batteries']  = 20;

    // Mars8000.
    this.models[16]['watts']      = 5600;
    this.models[16]['va']         = 8000;
    this.models[16]['pf']         = 0.7;
    this.models[16]['efficiency'] = 0.88;
    this.models[16]['ah']         = 7;
    this.models[16]['vdc']        = 12;
    this.models[16]['batteries']  = 20;

    // Mars10000.
    this.models[17]['watts']      = 7000;
    this.models[17]['va']         = 10000;
    this.models[17]['pf']         = 0.7;
    this.models[17]['efficiency'] = 0.88;
    this.models[17]['ah']         = 9;
    this.models[17]['vdc']        = 12;
    this.models[17]['batteries']  = 20;
}


/**
 * Computes the runtime for a UPS.
 */
Calculator.prototype.getRuntime = function() {
    // Get user inputs.
    var form   = document.getElementById('calculatorForm');
    var model  = parseInt(document.getElementById('model').value);
    var rating = form.rating.value;
    var units  = parseInt(document.getElementById('units').value);

    if (model == -1) {
        return;
    }

    // Convert input to Watts.
    var watts = rating;
    if (units == 1) { // User input is in VA.
        var watts = this.models[model]['pf'] * rating;
    }

    // Battery power.
    var power = watts / this.models[model]['efficiency'];

    // Total battery Vdc.
    var totalVdc = this.getTotalVdc(model, this.models[model]['batteries']);
    
    var f1 = power / totalVdc;
    
    var f2 = (f1 / this.models[model]['ah']) / 24.28;
    
    var f3 = f2 * 1000;
    
    var f4 = ((power / this.models[model]['batteries']) / this.models[model]['ah']) / 6;
    
    var f5 = (-1452.6 * Math.log(f3)) + 9739.3;

    form.time.value = this.round((f5 / f4) / 60);
}


/**
 * Returns the total battery Vdc.
 */
Calculator.prototype.getTotalVdc = function(model, batteries) {
    var vdc = -1;
    switch(model) {
        case 8: // Jupiter Pro XL 1000.
            vdc = 24;
            break;
        case 9: // Jupiter Pro XL 1500.
            vdc = 24;
            break;
        case 10: // Jupiter Pro XL 2000.
            vdc = 48;
            break;
        case 11: // JupiterPro XL 3000.
            vdc = 48;
            break;
        default: // All other models.
            vdc = this.models[model]['vdc'] * batteries;
            break;
    }
    return vdc;
}


/**
 * Returns value rounded to the nearest 1 decimal place.
 */
Calculator.prototype.round = function(value) {
    value      = new String(value);
    var digits = value.split('.');
    
    if (digits[1].length > 1) {
        // Get the first and second digit after the decimal point.
        var i = parseInt(digits[1].substring(0,1));
        var j = parseInt(digits[1].substring(1,2));
        if (j >= 5) {
            i = i + 1;
        }
        digits[1] = i;
    }
    return digits[0] + '.' + digits[1];
}


// Initialize this program upon the onload event.
addOnLoad(initCalculator);
