/* ACB Calculator Utilities */

function getDaysInMonth(year, month)
{
	return 32 - new Date(year, month, 32).getDate();
}

function checkDateFormat()
{
    return new Date("13/12/2003").getFullYear() - 2003;
}

function getLocalizedShortDateString(date)
{
    var year = date.getFullYear();
    var month = (date.getMonth() + 1);
    var day = date.getDate();
    if (checkDateFormat() == 0)
    {
        return padWithZero(day) + '/' + padWithZero(month) + '/' + year;
    }
    return padWithZero(month) + '/' + padWithZero(day) + '/' + year;
}

function padWithZero(v)
{
    if ((v >= 0) && (v < 10))
    {
        return '0' + v;
    }
    return v;
}

var wholePositiveNumberRegEx = /^(0|[1-9][0-9]*)$/;
var wholeDecimalPositiveNumberRegEx = /(^(0|[1-9][0-9]*)$)|((^(0?|[1-9][0-9]*)\.(0*[1-9][0-9]*)$)|(^[1-9]+[0-9]*\.0+$)|(^0\.0+$))/;

function isInteger(v)
{
    return wholePositiveNumberRegEx.test(v);
}

function isDecimal(v)
{
    return wholeDecimalPositiveNumberRegEx.test(v);
}

/* ACB Calculator Transaction Object */

if (!AcbCalc) var AcbCalc = { };

AcbCalc.Transactions = Class.create(Enumerable,
{
    initialize: function(id)
    {
        this.id = id;
        this.transactions = $A();
    },   
    // implement _each to use Enumerable methods 
    _each: function(iterator)
    {     
        return this.transactions._each(iterator);   
    }, 
    id:function()
    {
        return this.id;
    },
    getCount:function()
    {
        return this.transactions.length;
    },
    addEntry: function(entry)
    {
        this.setEntry(this.transactions.length, entry);
    },
	setEntry:function(index, entry)
    {
        if (!(entry instanceof AcbCalc.Transaction))
        {
            throw new TypeError("Only transaction type permitted");
        }
        this.transactions[index] = entry;
    },
    getEntry:function(index)
    {
        if ((index >= 0) && (this.transactions.length > index))
        {
            return this.transactions[index];
        }
        else
        {
            throw new RangeError("Index " + index + " out of bounds");
        }
        return null;
    },
    delEntry:function(index)
    {
        if ((index >= 0) && (this.transactions.length > index))
        {
            this.transactions.splice(index, 1);
        }
        else
        {
            throw new RangeError("Index " + index + " out of bounds");
        }
    }
});

/* ACB Calculator Transaction Object */
AcbCalc.Transaction = Class.create(
{
    initialize: function(year, month, day, quantity, value)
    {
        this.year = year;
        this.month = month;
        this.day = day;
        this.quantity = quantity;
        this.value = value;
    },
	year:function()
    {
        return this.year;
    },
    month:function()
    {
        return this.month;
    },
    day:function()
    {
        return this.day;
    },
    quanity:function()
    {
        return this.quantity;
    },
    value:function()
    {
        return this.value;
    },
    speak:function()
    {
        alert(this.year + ", " + this.month + ", " + this.day + ", " + this.quantity + ", " + this.value);
    }
});

/* ACB Calculator Transaction Object */
AcbCalc.Serialization = Class.create(
{
    initialize: function(type)
    {
        var xmlDocument = SafeCreateXmlDom();
        xmlDocument.loadXML("<transactions />");                
        var rootNode = xmlDocument.getElementsByTagName("transactions")[0];
        rootNode.setAttribute('id', type);
        this.rootNode = rootNode;
    },
	serialize:function(transactions)
	{
        count = transactions.getCount();
        for (i = 0; i < count; i++)
        {
            this._serializeTransaction(transactions.getEntry(i), i);
        }
    },
    _serializeTransaction:function(value, index)
    {
        var document = this.rootNode.ownerDocument;
        
        var transNode = document.createElement("entry");
        transNode.setAttribute("id", index);
        
        var dateNode = document.createElement("date");
        dateNode.setAttribute("year", value.year);
        dateNode.setAttribute("month", (Number(value.month) + 1));
        dateNode.setAttribute("day", value.day);
        transNode.appendChild(dateNode);
        
        var quantityNode = document.createElement("quantity");
        quantityNode.appendChild(document.createTextNode(value.quantity));
        transNode.appendChild(quantityNode);
        
        var valueNode = document.createElement("value");
        valueNode.appendChild(document.createTextNode(value.value));
        transNode.appendChild(valueNode);
        
        this.rootNode.appendChild(transNode);
    },
    toString: function()
    {
        var document = this.rootNode.ownerDocument;
        return document.xml;
    }
});
