//utility functions

function dateadd(sAdditionType, oDatum, iValue){
	//deze functie verandert het datum object niet!!!
	var oNewDate;
	var oNewDatum;
	var MSPERDAY = 24 * 3600 * 1000;

	switch(sAdditionType){
		case "d":
			//dagen optellen
			oNewDate = new Date(oDatum.getTime() + (iValue * MSPERDAY));
			break;
		case "w":
			//weken optellen
			oNewDate = new Date(oDatum.getTime() + (iValue * 7 * MSPERDAY));
		case "y":
			//jaren optellen
			oNewDate = new Date(oDatum.getTime())
			oNewDate.setYear(oNewDate.getYear() + iValue);
		default:
			break;
	}
	oNewDatum = new Datum();
	oNewDatum.initByDate(oNewDate);
	return oNewDatum;
}

/******************************************************************************************************************************/
// Class Datum 
/******************************************************************************************************************************/
function Datum(iJaar, iMaand, iDag){
	this.oDate = new Date();
	if((iJaar>=0) && (iMaand>=0) && (iDag>=0)){
		this.oDate.setYear(iJaar);
		this.oDate.setMonth(iMaand-1);
		this.oDate.setDate(iDag);
		this.bIsValidDate = true;
	}
	else{
		this.bIsValidDate = false;
	}

	//methods
	this.add = add;
	this.getTime = getTime;
	this.getYear = getYear;
	this.getMonth = getMonth;
	this.getDay = getDay;
	this.getYYYYMMDD = getYYYYMMDD;
	this.initBySepDDMMYYYY = initBySepDDMMYYYY;
	this.initByYYYYMMDD = initByYYYYMMDD;
	this.initByDDMM = initByDDMM;
	this.initByDDMMYY = initByDDMMYY;
	this.initByDate = initByDate;
	this.isValid = isValid;
	this.getSepDDMMYYYY = getSepDDMMYYYY;
	this.MSPERDAY = 24 * 3600 * 1000;
}

//methods

function add(sAdditionType, iValue){
	var bResult = false;
	switch(sAdditionType){
		case "d":
			//dagen optellen
			this.oDate = new Date(this.oDate.getTime() + (iValue * this.MSPERDAY));
			bResult = true;
			break;
		case "w":
			//weken optellen
			this.oDate = new Date(this.oDate.getTime() + (iValue * 7 * this.MSPERDAY));
			bResult = true;
		case "y":
			//jaren optellen
			this.oDate.setYear(this.oDate.getYear() + iValue);
			bResult = true;
		default:
			break;
	}
	return bResult;
}

function getTime(){
	return this.oDate.getTime();
}

function getYear(){
	var iYear = this.oDate.getYear();

	if(iYear < 1000){
		iYear = iYear + 1900;
	}
	return iYear;
}

function getMonth()
{
	return this.oDate.getMonth()+1;
}

function getDay()
{
	var sDD = this.oDate.getDate().toString();
	for(var i=sDD.length+1; i<=2; i++){
		sDD = "0" + sDD;
	}
	return sDD;
}

function getYYYYMMDD(){
	var sYYYY = this.getYear().toString();
	var iMM = this.oDate.getMonth()+1;
	var sMM = iMM.toString();
	var sDD = this.oDate.getDate().toString();

	for(var i=sYYYY.length+1; i<=4; i++){
		sYYYY = "0" + sYYYY;
	}

	for(var i=sMM.length+1; i<=2; i++){
		sMM = "0" + sMM;
	}

	for(var i=sDD.length+1; i<=2; i++){
		sDD = "0" + sDD;
	}

	return sYYYY+sMM+sDD;
}
function getSepDDMMYYYY(sSeparator){
	var sYYYY = this.getYear().toString();
	var iMM = this.oDate.getMonth()+1;
	var sMM = iMM.toString();
	var sDD = this.oDate.getDate().toString();

	for(var i=sYYYY.length+1; i<=4; i++){
		sYYYY = "0" + sYYYY;
	}

	for(var i=sMM.length+1; i<=2; i++){
		sMM = "0" + sMM;
	}

	for(var i=sDD.length+1; i<=2; i++){
		sDD = "0" + sDD;
	}

	//return true;
	return sDD+sSeparator+sMM+sSeparator+sYYYY;
}

//initByDate

function initByDate(oDate){
	this.oDate = oDate;
	this.bIsValidDate = true;
	return true;
}

function initBySepDDMMYYYY(sDDMMYYYY, sSeparator){
	var bResult = false;

	var aParts = sDDMMYYYY.split(sSeparator);
	if(aParts.length==3){
		iJaar = parseInt(aParts[2], 10);
		iMaand = parseInt(aParts[1], 10);
		iDag = parseInt(aParts[0], 10);
		this.oDate = new Date(iJaar, iMaand-1, iDag);
		this.bIsValidDate = true;
		bResult = true;
	}
	return bResult;
}

function initByYYYYMMDD(sYYYYMMDD){
	var bResult = false;

	if(sYYYYMMDD.length==8){
		iJaar = parseInt(sYYYYMMDD.substring(0, 4), 10);
		iMaand = parseInt(sYYYYMMDD.substring(4, 6), 10);
		iDag = parseInt(sYYYYMMDD.substring(6), 10);
		this.oDate = new Date(iJaar, iMaand-1, iDag);
		this.bIsValidDate = true;
		bResult = true;
	}
	return bResult;
}

function initByDDMM(sDDMM){
	var bResult = false;
	var dtVandaag = new Date();

	if(sDDMM.length==4){
		iDag = parseInt(sDDMM.substring(0, 2), 10);
		iMaand = parseInt(sDDMM.substring(2), 10);
		iJaar = dtVandaag.getYear();
		this.oDate = new Date(iJaar, iMaand-1, iDag);
		if(this.oDate<dtVandaag)
		{
			this.oDate = new Date(iJaar+1, iMaand-1, iDag);
		}
		this.bIsValidDate = true;
		bResult = true;
	}
	return bResult;
}

function initByDDMMYY(sDDMMYY){
	var bResult = false;

	if(sDDMMYY.length==6){
		iDag = parseInt(sDDMMYY.substring(0, 2), 10);
		iMaand = parseInt(sDDMMYY.substring(2, 4), 10);
		iJaar = parseInt('20' + sDDMMYY.substring(4), 10);
		this.oDate = new Date(iJaar, iMaand-1, iDag);
		this.bIsValidDate = true;
		bResult = true;
	}
	return bResult;
}

function isValid(){
	return this.bIsValidDate;
}
