// getDSGdates
// Find DST-treshold for a specifik year (EU -only)
function getDSTdates(thisYear)
{
	// getDSTdates(Y) - returns array of Timestamps for start and end of DST in EU
	var dstStart= (31-(Math.floor(thisYear * 5 / 4) + 4) % 7);	// March
	var dstEnd = (31-(Math.floor(thisYear * 5 / 4) + 1) % 7);	// October
	var dstStartMS = new Date(thisYear,2,dstStart,2,0,0).getTime();
	var dstEndMS = new Date(thisYear,9,dstEnd,3,0,0).getTime();
	return Array(dstStartMS, dstEndMS);
}


function getDateTimeNow()
{
	var myDate = new Date();
}

function getDateTimeUTC()
{
}

function getDateTimeConvert2UTC(outFormat, sourceDate, TZ)
{
	//	getDateTimeConvert2UTC Format(YmdHis) SourceDate(Y-m-d H:i:s) TimeZone(-12 - 12)
	var tmpDateArr = new Array();
	var arbDateArr = new Array();
	var arbTimeArr = new Array();
	tmpDateArr = sourceDate.split(" ");
	arbDateArr = tmpDateArr[0].split("-");
	arbTimeArr = tmpDateArr[1].split(":");
	var DSTarr = new Array();
	DSTarr = getDSTdates(arbDateArr[0]);
	
	var sourceDateMS = new Date((arbDateArr[0]),(arbDateArr[1]-1),arbDateArr[2],arbTimeArr[0],arbTimeArr[1],arbTimeArr[2]).getTime();
	if ((sourceDateMS > DSTarr[0]) && (sourceDateMS < DSTarr[1]))
	{
		// Is DST (+1h)
		sourceDateMS = sourceDateMS - ((60*1000*60)*(TZ+1));
	}
	else
	{
		// Not DST
		sourceDateMS = sourceDateMS - ((60*1000*60)*TZ);
	}
	var tmpOutFormat = outFormat;
	var tmpDate = new Date(sourceDateMS);

	var currY = tmpDate.getYear();
	if (currY < 1000) { currY+=1900; }

	var currM = tmpDate.getMonth()+1;
	if (currM<10) { currM = String("0"+currM); }

	var currD = tmpDate.getDate();
	if (currD<10) { currD = String("0"+currD); }

	var currH = tmpDate.getHours();
	if (currH<10) { currH = String("0"+currH); }

	var currI = tmpDate.getMinutes();
	if (currI<10) { currI = String("0"+currI); }

	var currS = tmpDate.getSeconds();
	if (currS<10) { currS = String("0"+currS); }

	tmpOutFormat = tmpOutFormat.replace(/Y/g,currY);
	tmpOutFormat = tmpOutFormat.replace(/m/g,currM);
	tmpOutFormat = tmpOutFormat.replace(/d/g,currD);
	tmpOutFormat = tmpOutFormat.replace(/H/g,currH);
	tmpOutFormat = tmpOutFormat.replace(/i/g,currI);
	tmpOutFormat = tmpOutFormat.replace(/s/g,currS);
	tmpOutFormat = tmpOutFormat.replace(/w/g,tmpDate.getDay());
	return tmpOutFormat;

}


function getDateTimeUTCConvert(outFormat, sourceDate, TZ)
{
	//	getDateTimeConvert2UTC Format(YmdHis) SourceDate(Y-m-d H:i:s) TimeZone(-12 - 12)
	var tmpDateArr = new Array();
	var arbDateArr = new Array();
	var arbTimeArr = new Array();
	tmpDateArr = sourceDate.split(" ");
	arbDateArr = tmpDateArr[0].split("-");
	arbTimeArr = tmpDateArr[1].split(":");
	var DSTarr = new Array();
	DSTarr = getDSTdates(arbDateArr[0]);
	
	var sourceDateMS = new Date((arbDateArr[0]),(arbDateArr[1]-1),arbDateArr[2],arbTimeArr[0],arbTimeArr[1],arbTimeArr[2]).getTime();
	if ((sourceDateMS > DSTarr[0]) && (sourceDateMS < DSTarr[1]))
	{
		// Is DST (+1h)
		sourceDateMS = sourceDateMS + ((60*1000*60)*(TZ+1));
	}
	else
	{
		// Not DST
		sourceDateMS = sourceDateMS + ((60*1000*60)*TZ);
	}
	var tmpOutFormat = outFormat;
	var tmpDate = new Date(sourceDateMS);

	var currY = tmpDate.getYear();
	if (currY < 1000) { currY+=1900; }

	var currM = tmpDate.getMonth()+1;
	if (currM<10) { currM = String("0"+currM); }

	var currD = tmpDate.getDate();
	if (currD<10) { currD = String("0"+currD); }

	var currH = tmpDate.getHours();
	if (currH<10) { currH = String("0"+currH); }

	var currI = tmpDate.getMinutes();
	if (currI<10) { currI = String("0"+currI); }

	var currS = tmpDate.getSeconds();
	if (currS<10) { currS = String("0"+currS); }

	tmpOutFormat = tmpOutFormat.replace(/Y/g,currY);
	tmpOutFormat = tmpOutFormat.replace(/m/g,currM);
	tmpOutFormat = tmpOutFormat.replace(/d/g,currD);
	tmpOutFormat = tmpOutFormat.replace(/H/g,currH);
	tmpOutFormat = tmpOutFormat.replace(/i/g,currI);
	tmpOutFormat = tmpOutFormat.replace(/s/g,currS);
	tmpOutFormat = tmpOutFormat.replace(/w/g,tmpDate.getDay());
	return tmpOutFormat;

}

//Returnerar timestamp (millisekunder sedan 1 jan 1970)
//Argument: Önskat datum/tid för beräkning. Format: ÅÅÅÅ-mm-dd HH:ii:ss
function getTimestamp(sourceDate)
{
	var tmpDateArr = new Array();
	var arbDateArr = new Array();
	var arbTimeArr = new Array();
	tmpDateArr = sourceDate.split(" ");
	arbDateArr = tmpDateArr[0].split("-");
	arbTimeArr = tmpDateArr[1].split(":");

	var sourceDateMS = new Date((arbDateArr[0]),(arbDateArr[1]-1),arbDateArr[2],arbTimeArr[0],arbTimeArr[1],arbTimeArr[2]).getTime();

	return sourceDateMS;

}



