﻿
//String Buffer to make building strings faster
//http://www.softwaresecretweapons.com/jspwiki/javascriptstringconcatenation
function StringBuffer() {
    this.buffer = [];
}

StringBuffer.prototype.append = function append(string) {
    this.buffer.push(string);
    return this;
};

StringBuffer.prototype.toString = function toString() {
    return this.buffer.join("");
}; 

//Check if value is numeric
function isNumeric(input) {
    return typeof (input) == 'number';
}

//Format a date from JSON
function formatJSONDate(jsonDate, formatString) {

    var k;
    
    //  "\/Date(1234418400000-0600)\/"
    var stdReg = /^\/Date\(([0-9]{13})-([0-9]{4})\)\/$/;

    if (k = jsonDate.match(stdReg)) {

        return dateFormat(new Date(parseInt(k[1])), formatString);
    }

}

//Convert a Military Time to Standard
function convertMilitaryToStandard(val) {

    var hours = val.toString().split(":")[0];
    var minutes = val.toString().split(":")[1];

    var dn = "AM";

    if (hours >= 12) {
        dn = "PM";
        hours = hours - 12;
    }

    if (hours == 0) {
        hours = 12;
    }

    if (minutes == undefined) {
        minutes = "00";
    } else if (minutes <= 9) {
        minutes = minutes + "0";
    }

    return hours + ":" + minutes + " " + dn;
}

//Convert a Standard Time to Military
function convertStandardToMilitary(val) {

    var hours = val.toString().split(":")[0];
    var minutes = val.toString().split(":")[1];

    if ((val.toLowerCase().indexOf("pm") > 0) && (hours != "12")) {
        hours = parseInt(hours) + 12;
    }

    minutes = parseInt(minutes);

    return hours + ":" + minutes;
}

