
/*####################################
#	XmlDateTime object
#	object om een datum string gecombineerd met een format string om te zetten naar een xml datetime
#	of een javascript datum
#####################################*/

//constructor
XmlDateTime = function(Date,strFormat)
{
	
	var strDate = Date;
	var strFormat = strFormat;
	if(strFormat==null)strFormat='#yyyy-#M-#dT#H:#mm:#ss';
	if (typeof(Date)=="object")
	{ 
		strDate = Date.getDate()+'/'+(Date.getMonth()+1)+'/'+Date.getFullYear()+'/'+Date.getHours()+'/'+Date.getMinutes()+'/'+Date.getSeconds();
		strFormat = '#d/#M/#yyyy/#H/#mm/#ss';
	} 

	this.DateString = strDate;
	this.FormatString = strFormat;
	this.year="0000";
	this.month="00";
	this.day="00";
	this.hour="00";
	this.minutes="00";
	this.seconds="00";
	this.ExtractDate(this.DateString,this.FormatString);
};

//functie om xmldatum op te halen
XmlDateTime.prototype.GetXMLDateTime = function()
{
	return this.year+"-"+this.month+"-"+this.day+"T"+this.hour+":"+this.minutes+":"+this.seconds
};

//functie om datum op te halen
XmlDateTime.prototype.GetDateTime = function()
{
	return this.DateString;
};
XmlDateTime.prototype.GetDateTime = function(strFormat)
{
	var strResult = strFormat;
	strResult = strResult.replace('#yyyy',this.year);
	strResult = strResult.replace('#MM',this.month);
	strResult = strResult.replace('#M',parseInt(this.month,10));
	strResult = strResult.replace('#dd',this.day);
	strResult = strResult.replace('#d',parseInt(this.day,10));
	strResult = strResult.replace('#HH',this.hour);
	strResult = strResult.replace('#H',parseInt(this.hour,10));
	strResult = strResult.replace('#mm',this.minutes);
	strResult = strResult.replace('#ss',this.seconds);
	
	return strResult;
};

//functie om javascript datum op te halen
XmlDateTime.prototype.GetDateTimeJS = function()
{
	var dtmJS = new Date();
	
	dtmJS.setFullYear(this.year);
	dtmJS.setMonth(this.month-1);
	dtmJS.setDate(this.day);
	dtmJS.setHours(this.hour);
	dtmJS.setMinutes(this.minutes);
	dtmJS.setSeconds(this.seconds);
	return dtmJS;
};

XmlDateTime.prototype.ExtractDate = function (strDatePart,strFormatPart)
{
	var strFormat = strFormatPart
	var strDate = strDatePart
	var posStart = strFormat.indexOf("#")
	var posEnd = strFormat.substring(posStart+1,strFormat.length).indexOf("#")>-1?strFormat.substring(posStart+1,strFormat.length).indexOf("#"):strFormat.length;
	var strFirst = strFormat.substring(posStart,posEnd+1)
	var strRemainFormat = strFormat.substring(posEnd+1,strFormat.length)
	var strRemainDate = "";
	
	var strSeperator = strFirst.substring(strFirst.length-1,strFirst.length)

	if(strSeperator.search(/[#dmyhs]/i)>-1)
	{
		strSeperator = null;
		posEnd = strDate.length;
	}
	else
	{
		strFirst = strFirst.substring(0,strFirst.length-1)
		posEnd = strDate.indexOf(strSeperator)
	}

	var strValue = strDate.substring(posStart,posEnd)
	strValue = strValue.length<2?'0'+strValue:strValue
	
	switch(strFirst)
	{
		case '#yyyy':
		{
			this.year = strValue;
			break;
		}
		case '#MM':
		case '#M':
		{
			this.month = strValue;
			break;
		}
		case '#d':
		case '#dd':
		{
			this.day = strValue;
			break;
		}
		case '#H':
		case '#HH':
		{
			this.hour = strValue;
			break;
		}
		case '#mm':
		{
			this.minutes = strValue;
			break;
		}
		case '#ss':
		{
			this.seconds = strValue;
			break;
		}
	}
	if(strSeperator!=null)
	{
		if(strDate.indexOf(strSeperator)>-1)
		{
			strRemainDate = strDate.substring(strDate.indexOf(strSeperator)+1,strDate.length);
			this.ExtractDate(strRemainDate,strRemainFormat);
		}
	}
}

//#########################################

