SB_Date.prototype.constructor = new SB_Date();
SB_Date.prototype.fromDMY = SB_Date_fromDMY;
SB_Date.prototype.fromSQL = SB_Date_fromSQL;
SB_Date.prototype.get_date_string = SB_Date_get_date_string;
SB_Date.prototype.get_sql_date = SB_Date_get_sql_date;
SB_Date.prototype.to_int = SB_Date_to_int;
SB_Date.prototype.is_valid_date = SB_Date_is_valid_date;
SB_Date.prototype.SetDateFromDropLists = SB_Date_SetDateFromDropLists;
/*NOTE - for horseshow, all dates will be in sql format*/
function SB_Date()
{
	this.day = 0;
	this.month = 0;
	this.year = 0;
}

function SB_Date_fromDMY(d,m,y)
{
	if (d == undefined)
		return;
	this.day = d;
	this.month = m;
	this.year = y;
}



function SB_Date_fromSQL(date_str)
{
	if (date_str == undefined)
		return;
		
	while(date_str.length < 8)
		date_str = date_str + '0';
		
	this.day = parseInt(date_str.substr(8, 2), 10);
	this.month = parseInt(date_str.substr(5, 2), 10);
	this.year = parseInt(date_str.substr(0, 4), 10);
}

function SB_Date_get_date_string()
{
	return this.day + "/" + this.month + "/" + this.year;
}

function SB_Date_get_sql_date()
{
	var y = this.year;
	var m = this.month;
	var d = this.day;

	if (parseInt(d, 10) < 10)
		d = '0' + d;
	if (parseInt(m, 10) < 10)
		m = '0' + m;
//	return new String(d) + new String(m) + new String(y);
	return new String(y) + "-" + new String(m) + "-" + new String(d);
}

function SB_Date_to_int()
{
/*start at year 2000*/
	var toint = 0;
	for (var y=2000; y < this.year; y++)
	{
		toint += 365;
		if ((y%4)==0)
			toint++;
	}
	
	for (var m=1; m<this.month; m++)	
		toint += days_per_month(m, this.year);
	toint += this.day;		
	return toint;
}

function SB_Date_is_valid_date()
{
	if (this.month < 1 || this.month > 12)
	{
		return false;
	}
	if (this.day < 1 || this.day > days_per_month(this.month, this.year))
	{
		return false;
	}
	if (this.year < 1900)
	{
		return false;
	}
	return true;
}

function SB_Date_SetDateFromDropLists(dl, ml, yl)
{
	this.day = gbi(dl).value;
	this.month = gbi(ml).value;
	this.year = gbi(yl).value;
}

function days_per_month(month, year)
{
	/*30 days september, april june nov = 4 6 9 11*/
	if (month == 4 || month == 6 || month == 9 || month == 11)
		return 30;
	else if (month == 2) /*feb*/
	{
		if ((year % 4) == 0 )
			return 29;
		else
			return 28;
	}
	return 31;
}


function fill_day_dropdown(elem)
{
	var i = 0;
	var dl = gbi(elem);
	for (i=0; i < 31; i++)
		dl.options[i] = new Option(i+1, i+1);
}

function fill_month_dropdown(elem)
{
	var i = 0;
	var dl = gbi(elem);
	for (i=0; i < 12; i++)
		dl.options[i] = new Option(i+1, i+1);
}

function fill_year_dropdown(elem, start, end)
{
	var i = 0;
	var dl = gbi(elem);
	for (i=start; i <= end; i++)
		dl.options[i-start] = new Option(i, i);
}




