function toGBDateFormat(gbdate){

	return cc.toDateString(gbdate)

	/*
  var d = gbdate.getDate();
  var m = gbdate.getMonth()+1;
  var y = gbdate.getFullYear();
  var dstr = '';
  if(d<10) dstr = dstr + '0';
  dstr = dstr + d;
  dstr = dstr + '-';
  if(m<10) dstr = dstr + '0';
  dstr = dstr + m;
  dstr = dstr + '-';
  dstr = dstr + y;
  return dstr;
  */
}
  
function toGBTimeFormat(gbdate){
  var h = gbdate.getHours();
  var n = gbdate.getMinutes();
  var tstr = '';
  if(h<10) tstr = tstr + '0';
  tstr = tstr + h;
  tstr = tstr + ':';
  if(n<10) tstr = tstr + '0';
  tstr = tstr + n;
  return tstr;
}

function fromGBDateFormat(gbstr){
  var d = gbstr.substr(0,2);
  var m = gbstr.substr(3,2);
  var y = gbstr.substr(6,4);
  var ddate = new Date;
  ddate.setDate(d);
  ddate.setMonth(m);
  ddate.setYear(y);
  return ddate;
}

function fromGBDateTimeFormat(gbstrD,gbstrT){
  var d = gbstrD.substr(0,2);
  var m = gbstrD.substr(3,2);
  var y = gbstrD.substr(6,4);
  var h = gbstrT.substr(0,2);
  var n = gbstrT.substr(3,2);
  var ddate = new Date;
  ddate.setDate(d);
  ddate.setMonth(m);
  ddate.setYear(y);
  ddate.setHours(h);
  ddate.setMinutes(n);
  return ddate;
}

// The DateSelect constructor
// oDate : Date Optional argument representing the date to select
function DateSelect( oDate ) {
	// check arguments
	if ( arguments.length == 0 ) {
		this._selectedDate = new Date;
	}
	else {
		this._selectedDate = oDate || new Date();
		if(this._selectedDate==null) {this._selectedDate = new Date()}
	}
  this._shownDate = this._selectedDate;
	
	this._matrix = [[],[],[],[],[],[],[]];
	this._showToday = true;
	this._firstWeekDay = 0;	// start week with monday according to standards
	this._redWeekDay = 6;	// sunday is the default red day.
	
	this._isDateSelected = false;
	this._div = null;
	this._textField = null;
	this._showButton = null;

}

// -------------------  ----------------------------------------------------------

// two static fields describing the name of the months abd days
DateSelect.translate = [];
DateSelect.months = [
	"januar", "februar", "marts", "april",
	"maj", "juni", "juli", "august",
	"september", "oktober", "november", "december"];
DateSelect.days = ["m", "t", "o", "t", "f", "l", "s"];


// -------------------  ----------------------------------------------------------
DateSelect.prototype.onchange = function () {
  if(this._isDateSelected){
  this._showButton.onclick();
    this._isDateSelected = false;
    var selDate = this.getDate();
    /*
    var d = selDate.getDate();
    var m = selDate.getMonth()+1;
    var y = selDate.getFullYear();
    var dstr = '';
    if(d<10) dstr = dstr + '0';
    dstr = dstr + d;
    //dstr = dstr + '-';
	dstr = dstr + page.getDateDelimiter();
    if(m<10) dstr = dstr + '0';
    dstr = dstr + m;
    //dstr = dstr + '-';
    dstr = dstr + page.getDateDelimiter();
    dstr = dstr + y;
    */
   // alert('seldate:'+selDate)
    dstr = cc.toDateString(selDate)
   // alert('dstr:'+dstr)
//    alert(',,,,'+dstr)
    this._textField.value=dstr;
    this.onafterchange();
  }
};

// -------------------  ----------------------------------------------------------
DateSelect.prototype.onafterchange = function () {};

// -------------------  ----------------------------------------------------------
// create the nodes inside the dateSelect
DateSelect.prototype.create = function ( doc ) {
	if ( doc == null ) doc = document;

	this._document = doc;
	// create elements
	this._el = doc.createElement( "div" );
	this._el.className = "dateSelect";
	this._el.style.display = 'none';
	this._el.style.position = 'absolute';
	this._el.style.zIndex = '200';
	
	// header
	var div = doc.createElement( "div" );
	div.className = "header";
	this._el.appendChild( div );
	
	var headerTable = doc.createElement( "table" );
	headerTable.className = "headerTable";
	headerTable.cellSpacing = 0;
	div.appendChild( headerTable );
	
	var tBody = doc.createElement( "tbody" );
	headerTable.appendChild( tBody );
	
	var tr = doc.createElement( "tr" );
	tBody.appendChild( tr );
	
	var td = doc.createElement( "td" );
	this._previousMonth = doc.createElement( "button" );
	this._previousMonth.className = "previousButton";
	td.appendChild( this._previousMonth );
	tr.appendChild( td );
	
	td = doc.createElement( "td" );
	td.className = "labelContainer";
	tr.appendChild( td );
	
	this._topLabel = doc.createElement( "a" );
	this._topLabel.className = "topLabel";
	this._topLabel.href = "#";
	this._topLabel.appendChild( doc.createTextNode( String.fromCharCode( 160 ) ) );
	td.appendChild( this._topLabel );
	
	this._labelPopup = doc.createElement( "div" );
	this._labelPopup.className = "labelPopup";
	// no insertion
	
	td = doc.createElement( "td" );
	this._nextMonth = doc.createElement( "button" );
	this._nextMonth.className = "nextButton";
	td.appendChild( this._nextMonth );
	tr.appendChild( td );
	
	// grid
	div = doc.createElement( "div" );
	div.className = "grid";
	this._el.appendChild( div );
	this._table = div;
	
	// footer
	div = doc.createElement( "div" );
	div.className = "footer";
	this._el.appendChild( div );
	
	var footerTable = doc.createElement( "table" );
	footerTable.className = "footerTable";
	footerTable.cellSpacing = 0;
	div.appendChild( footerTable );
	
	tBody = doc.createElement( "tbody" );
	footerTable.appendChild( tBody );
	
	tr = doc.createElement( "tr" );
	tBody.appendChild( tr );
	
	td = doc.createElement( "td" );
	td.style.textAlign = 'center';
	this._todayButton = doc.createElement( "button" );
	this._todayButton.className = "todayButton";
	this._todayButton.appendChild( doc.createTextNode( DateSelect.translate[0] ) );
	//this._todayButton.value = DateSelect.translate[0];
	td.appendChild( this._todayButton );
	tr.appendChild( td );
	
	this._createTable( doc );
	
	this._updateTable();
	this._setTopLabel();

	if ( !this._showToday )
		this._todayButton.style.visibility = "hidden";

	// IE55+ extension		
	this._previousMonth.hideFocus = true;
	this._nextMonth.hideFocus = true;
	this._todayButton.hideFocus = true;
	// end IE55+ extension
	
	// hook up events
	var dp = this;
	// buttons

// -------------------  ----------------------------------------------------------
	this._previousMonth.onclick = function () {
		dp.goToPreviousMonth();
	};

// -------------------  ----------------------------------------------------------
	this._nextMonth.onclick = function () {
		dp.goToNextMonth();
	};

// -------------------  ----------------------------------------------------------
	this._todayButton.onclick = function () {
		this._isDateSelected = true;
		dp.goToToday();
	};

// -------------------  ----------------------------------------------------------
	this._el.onselectstart = function () {
		return false;
	};
	
// -------------------  ----------------------------------------------------------
	this.reset = function () {
		dp._shownDate = dp._selectedDate;
		this._setTopLabel();
		this._updateTable();
	};
	
// ------------------- toggleShow ----------------------------------------------------------
  DateSelect.prototype.toggleShow = function(){
  }
  
// ------------------- _table.onclick ----------------------------------------------------------
	this._table.onclick = function ( e ) {
		// find event
		if ( e == null ) e = doc.parentWindow.event;
		
		// find td
		var el = e.target != null ? e.target : e.srcElement;
		while ( el.nodeType != 1 )
			el = el.parentNode;
		while ( el != null && el.tagName && el.tagName.toLowerCase() != "td" )
			el = el.parentNode;
		
		// if no td found, return
		if ( el == null || el.tagName == null || el.tagName.toLowerCase() != "td" )
			return;

		var d = new Date( dp._shownDate );
		var n = Number( el.firstChild.data );
		if ( isNaN( n ) || n <= 0 || n == null )
			return;
//			alert(n)
		d.setDate( n );
		dp._isDateSelected = true;
		dp.setDate( d );
	};
	
// ------------------- _topLabel.onclick ----------------------------------------------------------
	// show popup
	this._topLabel.onclick = function ( e ) {
		dp._showLabelPopup();
		return false;
	};
	
// ------------------- onkeydown ----------------------------------------------------------
	this._el.onkeydown = function ( e ) {
		if ( e == null ) e = doc.parentWindow.event;
		var kc = e.keyCode != null ? e.keyCode : e.charCode;
		
		if ( kc < 37 || kc > 40 ) return true;
		
		var d = new Date( dp._shownDate ).valueOf();
		if ( kc == 37 ) // left
			d -= 24 * 60 * 60 * 1000;
		else if ( kc == 39 ) // right
			d += 24 * 60 * 60 * 1000;
		else if ( kc == 38 ) // up
			d -= 7 * 24 * 60 * 60 * 1000;
		else if ( kc == 40 ) // down
			d += 7 * 24 * 60 * 60 * 1000;

		dp.setDate( new Date( d ) );
		return false;
	}
	
// ------------------- onmousewheel ----------------------------------------------------------
	// ie6 extension
	this._el.onmousewheel = function ( e ) {
		if ( e == null ) e = doc.parentWindow.event;
		var n = - e.wheelDelta / 120;
		var d = new Date( dp._shownDate );
		var m = d.getMonth() + n;
		d.setMonth( m );
		
		
		dp.setDate( d );
		
		return false;
	}

	this._div = this._el;
};

// ------------------- setDate ----------------------------------------------------------
DateSelect.prototype.setDate = function ( oDate ) {

	this._hideLabelPopup();

	// if string or number create a Date object
	if ( typeof oDate == "string" || typeof oDate == "number" ) {
		oDate = new Date( oDate );
	}
	
	this._shownDate = new Date( oDate );
	if(this._isDateSelected==true) {this._selectedDate = new Date( oDate );}

	this._setTopLabel();
	this._updateTable();
		
	if ( typeof this.onchange == "function" ){
		this.onchange();
	}
	
}


// ------------------- getDate ----------------------------------------------------------
DateSelect.prototype.getDate = function () {
	return new Date( this._selectedDate );	// create a new instance
}

// ------------------- _createTable ----------------------------------------------------------
// creates the table elements and inserts them into the dateSelect
DateSelect.prototype._createTable = function ( doc ) {
	var str, i;
	var rows = 6;
	var cols = 7;
	var currentWeek = 0;

	var table = doc.createElement( "table" );
	table.className = "gridTable";
	table.cellSpacing = 0;
	
	var tBody = doc.createElement( "tbody" );
	table.appendChild( tBody );
	
	// days row
	var tr = doc.createElement( "tr" );
	tr.className = "daysRow";

	var td, tn;
	var nbsp = String.fromCharCode( 160 );
	for ( i = 0; i < cols; i++ ) {
		td = doc.createElement( "td" );
		td.appendChild( doc.createTextNode( nbsp ) );
		tr.appendChild( td );
	}
	tBody.appendChild( tr );
	
	// upper line
	tr = doc.createElement( "tr" );
	td = doc.createElement( "td" );
	td.className = "upperLine";
	td.colSpan = 7;
	tr.appendChild( td );
	tBody.appendChild( tr );

	// rest
	for ( i = 0; i < rows; i++ ) {
		tr = doc.createElement( "tr" );
		for ( var j = 0; j < cols; j++ ) {
			td = doc.createElement( "td" );
			td.appendChild( doc.createTextNode( nbsp ) );
			tr.appendChild( td );
		}
		tBody.appendChild( tr );
	}
	str += "</table>";
	
	if ( this._table != null )
		this._table.appendChild( table )
};

// ------------------- _updateTable ----------------------------------------------------------
// this method updates all the text nodes inside the table as well
// as all the classNames on the tds
DateSelect.prototype._updateTable = function () {
	// if no element no need to continue
	if ( this._table == null ) return;
	
	var i;
	var str = "";
	var rows = 6;
	var cols = 7;
	var currentWeek = 0;
		
	var cells = new Array( rows );
	this._matrix = new Array( rows )
	for ( i = 0; i < rows; i++ ) {
		cells[i] = new Array( cols );
		this._matrix[i] = new Array( cols );
	}

	// Set the tmpDate to this month
	var tmpDate = new Date( this._shownDate.getFullYear(),
							this._shownDate.getMonth(), 1 );
	var today = new Date();
	// go thorugh all days this month and store the text
	// and the class name in the cells matrix
	for ( i = 1; i < 32; i++ ) {
		tmpDate.setDate( i );
		// convert to ISO, Monday is 0 and 6 is Sunday
		var weekDay = ( tmpDate.getDay() + 6 ) % 7;
		var colIndex = ( weekDay - this._firstWeekDay + 7 ) % 7;
		if ( tmpDate.getMonth() == this._shownDate.getMonth() ) {

			var isToday = tmpDate.getDate() == today.getDate() && 
						tmpDate.getMonth() == today.getMonth() &&
						tmpDate.getFullYear() == today.getFullYear();
		
			cells[currentWeek][colIndex] = { text: "", className: "" };
			
			if ( this._selectedDate.getDate() == tmpDate.getDate() && this._selectedDate.getMonth() == tmpDate.getMonth() && this._selectedDate.getFullYear() == tmpDate.getFullYear() )
				cells[currentWeek][colIndex].className += "selected ";
			if ( isToday )
				cells[currentWeek][colIndex].className += "today ";
			if ( ( tmpDate.getDay() + 6 ) % 7 == this._redWeekDay ) // ISO
				cells[currentWeek][colIndex].className += "red";
			
			cells[currentWeek][colIndex].text =			
				this._matrix[currentWeek][colIndex] = tmpDate.getDate();
			
			if ( colIndex == 6 )
				currentWeek++;			
		}
	}
	
	// fix day letter order if not standard
	var weekDays = DateSelect.days;												
	if (this._firstWeekDay != 0) {
		weekDays = new Array(7);
		for ( i = 0; i < 7; i++)
			weekDays[i] = DateSelect.days[ (i + this._firstWeekDay) % 7];
	}

	// update text in days row
	var tds = this._table.firstChild.tBodies[0].rows[0].cells;
	for ( i = 0; i < cols; i++ )
		tds[i].innerHTML = weekDays[i].replace('<font color=blue>','').replace('</font>','');
		//tds[i].firstChild.data = weekDays[i].replace('<font color=blue>','').replace('</font>','');
		
	// update the text nodes and class names
	var trs = this._table.firstChild.tBodies[0].rows;
	var tmpCell;
	var nbsp = String.fromCharCode( 160 );
	for ( var y = 0; y < rows; y++ ) {
		for (var x = 0; x < cols; x++) {
			tmpCell = trs[y + 2].cells[x];
			if ( typeof cells[y][x] != "undefined" ) {
				tmpCell.className = cells[y][x].className;
				//tmpCell.firstChild.data = cells[y][x].text;
				tmpCell.innerHTML = cells[y][x].text;
			}
			else {
				tmpCell.className = "";
				//tmpCell.firstChild.data = nbsp;
				tmpCell.innerHTML = "&nbsp;";
			}
		}
	}
}

// ------------------- _setTopLabel ----------------------------------------------------------
// sets the label showing the year and selected month
DateSelect.prototype._setTopLabel = function () {
	//var str = this._shownDate.getFullYear() + " " + DateSelect.months[ this._shownDate.getMonth() ];
	var str = DateSelect.months[ this._shownDate.getMonth() ] + " " + this._shownDate.getFullYear();
	if ( this._topLabel != null )
		this._topLabel.innerHTML = str;
		//this._topLabel.lastChild.data = str;
}

// -------------------  ----------------------------------------------------------
DateSelect.prototype.goToNextMonth = function () {
	var d = new Date( this._shownDate );
	d.setDate(1);
	d.setMonth( d.getMonth() + 1 );
	this.setDate( d );
}

// -------------------  ----------------------------------------------------------
DateSelect.prototype.goToPreviousMonth = function () {
	var d = new Date( this._shownDate );
	d.setDate(1);
	d.setMonth( d.getMonth() - 1 );
	this.setDate( d );
}

// -------------------  ----------------------------------------------------------
DateSelect.prototype.goToToday = function () {
	this._isDateSelected = true;
	this.setDate( new Date() );
}

// -------------------  ----------------------------------------------------------
DateSelect.prototype.setShowToday = function ( bShowToday ) {
	if ( typeof bShowToday == "string" )
		bShowToday = !/false|0|no/i.test( bShowToday );
		
	if ( this._todayButton != null )
		this._todayButton.style.visibility = bShowToday ? "visible" : "hidden";
	this._showToday = bShowToday;
}

// -------------------  ----------------------------------------------------------
DateSelect.prototype.getShowToday = function () {
	return this._showToday;
}

// -------------------  ----------------------------------------------------------
// 0 is monday and 6 is sunday as in the ISO standard
DateSelect.prototype.setFirstWeekDay = function ( nFirstWeekDay ) {
	if ( this._firstWeekDay != nFirstWeekDay ) {
		this._firstWeekDay = nFirstWeekDay;
		this._updateTable();
	}
}

// -------------------  ----------------------------------------------------------
DateSelect.prototype.getFirstWeekDay = function () {
	return this._firstWeekDay;
}

// -------------------  ----------------------------------------------------------
// 0 is monday and 6 is sunday as in the ISO standard
DateSelect.prototype.setRedWeekDay = function ( nRedWeekDay ) {
	if ( this._redWeekDay != nRedWeekDay ) {
		this._redWeekDay = nRedWeekDay;
		this._updateTable();
	}
}

// -------------------  ----------------------------------------------------------
DateSelect.prototype.getRedWeekDay = function () {
	return this._redWeekDay;
}


// -------------------  ----------------------------------------------------------
DateSelect.prototype.setTextField = function ( obj ) {
	this._textField = obj;
}

// -------------------  ----------------------------------------------------------
DateSelect.prototype.setShowButton = function (obj) {
	this._showButton = obj;
	this.create();
	getObj('dateselectDiv').appendChild(this._div);
}
	
// -------------------  ----------------------------------------------------------
DateSelect.prototype._showLabelPopup = function () {
	
	/*
	this._labelPopup document.createElement( "DIV" );
	div.className = "month-popup";
	div.noWrap = true;
	el.unselectable = div.unselectable = "on";
	el.onselectstart = div.onselectstart = function () { return false; };
	*/
	
	var dateContext = function ( dp, d ) {
		return function ( e ) {
			dp._hideLabelPopup();
			dp.setDate( d );
			return false;
		};
	};
	
	var dp = this;
	
	// clear all old elements in the popup
	while ( this._labelPopup.hasChildNodes() )
		this._labelPopup.removeChild( this._labelPopup.firstChild );
	
	var a, tmp;
	for ( var i = -3; i < 4; i++ ) {
		tmp = new Date( this._shownDate );
		tmp.setDate(1);	// set day to 1 to prevent overflow with shorter months
		tmp.setMonth( tmp.getMonth() + i );
		
		a = this._document.createElement( "a" );
		a.href = "javascript:void 0;";
		a.onclick = dateContext( dp, tmp );
		a.innerHTML = DateSelect.months[ tmp.getMonth() ] + " " + tmp.getFullYear();
		//a.appendChild( this._document.createTextNode( DateSelect.months[ tmp.getMonth() ] + " " +
		//							tmp.getFullYear() ) );
		if ( i == 0 )
			a.className = "selected";
		this._labelPopup.appendChild( a );
	}
	
	this._topLabel.parentNode.insertBefore( this._labelPopup, this._topLabel.parentNode.firstChild );
};

// -------------------  ----------------------------------------------------------
DateSelect.prototype._hideLabelPopup = function () {
	if ( this._labelPopup.parentNode )
		this._labelPopup.parentNode.removeChild( this._labelPopup );
};
