BwClasses["BwDate"] = BwDate;

function BwDate ()
{
	this.isa = BwWidget;
	this.isa();
	
	this.initFromDOM = BwDate.initFromDOM;
	
	this.setValue = BwDate.setValue;
	this.getValue = BwDate.getValue;
}

/* date must be DD-MM-YYYY or YYYY-MM-DD */
BwDate.newInstance = function (date, lang, separ)
{
    var self = document.createElement ("DIV");

    this.dateRank = 0;
	
	self.BwClass = BwDate;
	self.BwClass();
	
	self.className = "BwLabel";
	
	if (date)
	{
        var isFrench = (lang=='FR')?true:false;
		self.setAttribute ("text", BwDate.format(date,isFrench,separ));
		self.setAttribute ("dateRank", date);
	}
	
	self.initFromDOM();
    return self;
};

BwDate.initFromDOM = function ()
{
	BwWidget.initFromDOM.call (this);
	
	this.style.display="inline";
	this.style.MozUserSelect="none";
	this.style.cursor="default";
	this.onselectstart=function(){return false;};

	var txt = this.getAttribute ("text");
	if (txt != null) {
		this.setValue(txt);
	}
	return false;
};

BwDate.getValue = function ()
{
    var dateRank = this.getAttribute ("dateRank");
    if (dateRank != null)
    	return dateRank;
    return 0;
};

BwDate.setValue = function (txt)
{
	this.innerHTML = txt;
};

/* date must be DD-MM-YYYY or YYYY-MM-DD */
BwDate.format = function(date, isFrench, separ)
{
    if (date && date.length==10 && date.indexOf(separ,0)!=-1)
    {
        if (separ==null)
            separ='/';
        var isDateFR = date.indexOf(separ,1)<4;
        if (isDateFR)
        {
            var DD = date.substr(0,2);
            var MM = date.substr(3,2);
            var YY = date.substr(6,4);
        }
        else
        {
            var YY = date.substr(0,4);
            var MM = date.substr(5,2);
            var DD = date.substr(8,4);
        }
        if (isFrench==true)
            return (DD+separ+MM+separ+YY);
        else
            return (YY+separ+MM+separ+DD);
    }
    else
        return date;
};

