/*
 * utils.js
 * by Emanuel Calso
 *
 * utils.js -- miscellaneous helper scripts for the calculator generator and the 
 * calculators that will be generated
 * 
 * AUTHOR: Emanuel Gardaya Calso <egcalso@gmail.com>
 * 
 * Modification Dates:
 *     2009-02-18
 * 
 * 
 * Copyright (C) 2008 Emanuel Calso <egcalso [at] gmail.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 **/



var MonthArray = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var MonthDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

var addDays = function (time, days) {
    var time_ms = time.getTime();
    var future_ms = time_ms + ( days * (24*60*60*1000) );
    var future = new Date(future_ms);
    return future;
}

var addMonths = function (time, mos) {
    var future;
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var future_y = y;
    var future_m = m + mos;
    while (future_m > 12) {
        future_y += 1;
        future_m -= 12;
    }
    var future_d = d;
    if (future_d > MonthDays[future_m-1]) {
        var tmp_future_d = MonthDays[future_m-1];
        var d_diff = future_d - tmp_future_d;
        var tmp_future = new Date(MonthArray[future_m-1] + ' ' + tmp_future_d  + ', ' + future_y);
        future = addDays(tmp_future, d_diff);
    } else {
        future = new Date(MonthArray[future_m-1] + ' ' + future_d  + ', ' + future_y);
    }
    return future;
}

var countDaysFrom = function (past) {
    /*
     * countDaysFrom ( Past Date Object )
     * Count how many days have gone from a time in the past
     *
     */
    var today = new Date();
    var past_ms = past.getTime();
    var today_ms = today.getTime();
    var difference = Math.round( (today_ms-past_ms) / (24*60*60*1000) );
    return difference
}

var countDaysTo = function (future) {
    /*
     * countDaysTo ( Future Date Object )
     * Count how many days to go to reach a future time
     *
     */
    var today = new Date();
    var future_ms = future.getTime();
    var today_ms = today.getTime();
    var difference = Math.round( (future_ms-today_ms) / (24*60*60*1000) );
    return difference
}

var getDateObj = function (form, field) {
    var y = parseInt(form[field + '_y'].value);
    var m = parseInt(form[field + '_m'].value);
    var d = parseInt(form[field + '_d'].value);
    var mon = MonthArray[m-1];
    var str = mon + ' ' + d + ', ' + y;
    var obj = new Date(str);
    return obj;
}

var parsePercent = function (s) {
    var v = parseFloat(s) / 100;
    return v
}

var parseCurrency = function (s) {
    var v = parseFloat(s);
    if (! v) {
        v = 0;
    }
    return v
}

var showInt = function (form, field, v) {
    form[field].value =  parseInt(Math.round(v));
}

var showFloat = function (form, field, v) {
    form[field].value =  parseFloat(v).toFixed(2);
}

var showCurrency = function (form, field, v) {
    form[field].value =  parseFloat(v).toFixed(2);
}

var showPercent = function (form, field, v) {
    form[field].value =  (v * 100).toFixed(2);
}

var showDate = function (form, field, obj) {
    form[field + '_y'].value = obj.getFullYear();
    form[field + '_m'].value = obj.getMonth() + 1;
    form[field + '_d'].value = obj.getDate();
}

var subtractDays = function (time, days) {
    var time_ms = time.getTime();
    var past_ms = time_ms - ( days * (24*60*60*1000) );
    var past = new Date(past_ms);
    return past;
}

var subtractMonths = function (time, mos) {
    var past;
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var past_y = y;
    var past_m = m - mos;
    while (past_m < 1) {
        past_y -= 1;
        past_m += 12;
    }
    var past_d = d;
    if (past_d > MonthDays[past_m-1]) {
        var tmp_past_d = MonthDays[past_m-1];
        var d_diff = past_d - tmp_past_d;
        var tmp_past = new Date(MonthArray[past_m-1] + ' ' + tmp_past_d  + ', ' + past_y);
        past = addDays(tmp_past, d_diff);
    } else {
        past = new Date(MonthArray[past_m-1] + ' ' + past_d  + ', ' + past_y);
    }
    return past;
}



