function Fix(nValue){
	return Math.round(Math.abs(nValue)-0.5);
}

function Integer(nValue){
	return Math.round(nValue-0.5);
}

Systools = {version: "1.0"}
Systools.nMinYear = 1600;
Systools.nMinDate = 0;					//Minimum valid date for a date variable - 01/01/1600
Systools.nMaxDate = 876581;		//Maximum valid date for a date variable - 12/31/3999
Systools.First2Months = 59;
Systools.nFirstDayOfWeek = 6;  // 01.01.1600 was a Saturday
Systools.Days400Yr = 146097;
Systools.BadDate= 0xFFFFFFFF;// 4294967295;

Systools.DayOfWeek = function(Julian){
  //-Return the day of the week for the date. Returns TStDayType(7) if Julian =
  //  BadDate.
  var B, nResult;
  if (Julian == Systools.BadDate){
    B = 7;
    nResult = B;
  }
  else{
    nResult = (Julian + Systools.nFirstDayOfWeek) % 7;
  }
	return nResult;
}

Systools.WeekOfYear = function(nJulian){
  //-Returns the week number of the year given the Julian Date
	var nDay, nMonth, nYear;
	var nFirstJulian;
	var nResult;

  if ((nJulian < Systools.nMinDate) || (nJulian > Systools.nMaxDate)){
    nResult = 0;
  }
  else{
		nYear	= parseInt(Systools.StDateToDateString("yyyy", nJulian));
		Systools.nFirstJulian = Systools.DMYToStDate(1,1,nYear);
		if (Systools.DayOfWeek(nFirstJulian) > 3){
			while (Systools.DayOfWeek(nFirstJulian) != 0)
				nFirstJulian++;
		}
		else{
			while (Systools.DayOfWeek(nFirstJulian) != 0)
				nFirstJulian--;
		}
		nResult = Math.round(1+Integer(Integer(nJulian - nFirstJulian) / 7));
	}
	return nResult;
}

Systools.DMYToStDate = function(nDate, nMonth, nYear){
	var nTemp;
	var nResult;
	if (nMonth > 2){
		nMonth = nMonth - 3;
	}
	else{
		nMonth = nMonth + 9;
		nYear = nYear - 1;
	}
	nResult = 0;
	nYear = nYear - Systools.nMinYear;
	nTemp = Math.round((nYear - (nYear % 100)) / 100)*Systools.Days400Yr;
	nTemp = (nTemp - (nTemp % 4)) / 4;
	nResult = nTemp;
	
	nTemp = Math.round(nYear % 100)*1461;
	nTemp = ((nTemp - (nTemp % 4)) / 4);
	nResult = nResult + nTemp;
	
	nTemp = (153 * nMonth) + 2;
	nTemp = ((nTemp - (nTemp % 5)) / 5)+nDate+Systools.First2Months;
	nResult = nResult + nTemp;
	return nResult;
}


Systools.CurrentDate = function(){
	var oNow;
	var nDay;
	var nMonth;
	var nYear;
	//debugger;
	oNow		= new Date();
	nDay		= oNow.getDate();
	nMonth	= oNow.getMonth() + 1;
	nYear		= oNow.getFullYear();
	//Session.Contents("DEBUG") = String("Date is: " + oNow + " - " + nDay + ":" + nMonth + ":" + nYear);
	return Systools.DMYToStDate(nDay, nMonth, nYear);
}

Systools.DateToStDate = function(oDate){
	var nDay;
	var nMonth;
	var nYear;
	if (typeof(oDate) == "string")
		oDate = new Date(oDate);
	nDay		= oDate.getDate();
	nMonth	= oDate.getMonth() + 1;
	nYear		= oDate.getFullYear();
	return Systools.DMYToStDate(nDay, nMonth, nYear);
}

Systools.CurrentTime = function(){
	var oNow;
	oNow		= new Date();
	return (oNow.getHours() * 60 * 60) + (oNow.getMinutes() * 60) +  oNow.getSeconds();
}

Systools.TimeStringToStTime = function(strValue){
	var nHour = 0, nMin = 0, nSec = 0;
	var nReturn = -1;
	var strTime = new String(strValue);
	var nIndex = 0;
	try{
		while ((nIndex < strTime.length) && (strTime.substr(nIndex, 1)!=":")){
			nHour = nHour * 10 + Math.round(strTime.substr(nIndex, 1));
			nIndex++;
		}
		nIndex++;
		while ((nIndex < strTime.length) && (strTime.substr(nIndex, 1)!=":")){
			nMin = nMin * 10 + Math.round(strTime.substr(nIndex, 1));
			nIndex++;
		}
		nIndex++;
		while ((nIndex < strTime.length) && (strTime.substr(nIndex, 1)!=":")){
			nSec = nSec * 10 + Math.round(strTime.substr(nIndex, 1));
			nIndex++;
		}
		nReturn = (((nHour*60) + nMin)*60) + nSec;
	}
	catch(e){
		nReturn = -1;
	}
	return nReturn;
}

Systools.StTimeToTimeString = function(strPicture, stTime){
	var nHour, nMinute, nSecond;
	var nMod;
	var nPos;

	nHour		= Fix(stTime / (60*60))
	nMinute = Fix((stTime - (nHour * 60 *60))/60);
	nSecond	= stTime - (nHour * 60 *60) - (nMinute * 60);

  nMod = 10;
  nPos = strPicture.lastIndexOf("h")
  while(nPos > -1){
		strPicture = strPicture.substr(0, nPos) + String(Integer((nHour % nMod) / (nMod/10))) + strPicture.substr(nPos + 1);
	  nPos = strPicture.lastIndexOf("h");
		nMod = nMod * 10;
  }

  nMod = 10
  nPos = strPicture.lastIndexOf("m")
  while(nPos > -1){
		strPicture = strPicture.substr(0, nPos) + String(Integer((nMinute % nMod) / (nMod/10))) + strPicture.substr(nPos + 1);
	  nPos = strPicture.lastIndexOf("m");
		nMod = nMod * 10
  }

  nMod = 10
  nPos = strPicture.lastIndexOf("s")
  while(nPos > -1){
		strPicture = strPicture.substr(0, nPos) + String(Integer((nSecond % nMod) / (nMod/10))) + strPicture.substr(nPos + 1);
	  nPos = strPicture.lastIndexOf("s");
		nMod = nMod * 10
  }
  return strPicture;
}

Systools.DateStringToStDate = function(strValue){
	var oCurrDate = new Date();
	var nYear = 0, nMonth = 0, nDay = 0;
	nYear		= Math.round(oCurrDate.getFullYear());
	nMonth	= Math.round(oCurrDate.getMonth());
	nDay		= Math.round(oCurrDate.getDay());
	var nReturn = -1;
	var strTime = new String(strValue);
	var nIndex = 0;
	//try{
		if (nIndex < strTime.length)
			nDay = 0;
		while ((nIndex < strTime.length) && (strTime.substr(nIndex, 1)!=".")){
			nDay = nDay * 10 + Math.round(strTime.substr(nIndex, 1));
			nIndex++;
		}
		nIndex++;
		if (nIndex < strTime.length)
			nMonth = 0;
		while ((nIndex < strTime.length) && (strTime.substr(nIndex, 1)!=".")){
			nMonth = nMonth * 10 + Math.round(strTime.substr(nIndex, 1));
			nIndex++;
		}
		nIndex++;
		if (nIndex < strTime.length)
			nYear = 0;
		while ((nIndex < strTime.length) && (strTime.substr(nIndex, 1)!=".")){
			nYear = nYear * 10 + Math.round(strTime.substr(nIndex, 1));
			nIndex++;
		}
		if (nYear < 50)
			nYear = nYear + 2000;
		else if (nYear < 100)
			nYear = nYear + 1900;
		nReturn =  Systools.DMYToStDate(nDay, nMonth, nYear);
	/*}
	catch(e){
		nReturn = -1;
	}*/
	return nReturn;
}

Systools.StDateToDateString = function(strPicture, stDate){
	var nYear, nMonth, nDay;
	var vbDate;
	var I, J;
	var nMod;
	var nPos;
	if (stDate <= Systools.First2Months){
		nYear = 1600;
		if (stDate <= 30){
			nMonth = 1;
			nDay = stDate + 1;
		}
		else{
			nMonth = 2;
			nDay = stDate-30;
		}
	}
	else{
		I = (4*Math.round(stDate-Systools.First2Months))-1;
		J = (4*Integer((I % Systools.Days400Yr) / 4))+3;
		nYear = (100*Integer(I / Systools.Days400Yr))+Integer(J / 1461);
		I = (5*Integer(((J % 1461)+4) / 4))-3;
		nDay = Integer(((I % 153)+5) / 5);
		nMonth = Integer(I / 153);
		if (nMonth < 10){
			nMonth = nMonth + 3;
	  }
		else{
			nMonth = nMonth - 9;
		  nYear = nYear + 1;
		}
		nYear = nYear + 1600;
	}
  nMod = 10;
  nPos = strPicture.lastIndexOf("y")
  while(nPos > -1){
		strPicture = strPicture.substr(0, nPos) + String(Integer((nYear % nMod) / (nMod/10))) + strPicture.substr(nPos + 1);
	  nPos = strPicture.lastIndexOf("y");
		nMod = nMod * 10;
  }

  nMod = 10;
  nPos = strPicture.lastIndexOf("m")
  while(nPos > -1){
		strPicture = strPicture.substr(0, nPos) + String(Integer((nMonth % nMod) / (nMod/10))) + strPicture.substr(nPos + 1);
	  nPos = strPicture.lastIndexOf("m");
		nMod = nMod * 10
  }

  nMod = 10;
  nPos = strPicture.lastIndexOf("d")
  while(nPos > -1){
		strPicture = strPicture.substr(0, nPos) + String(Integer((nDay % nMod) / (nMod/10))) + strPicture.substr(nPos + 1);
	  nPos = strPicture.lastIndexOf("d");
		nMod = nMod * 10;
  }
  return strPicture;
}

Systools.StDateToDate = function(stDate){
	//debugger;
	var nYear, nMonth, nDay;
	var vbDate;
	var I, J;
	var nMod;
	var nPos;
	if (stDate <= Systools.First2Months){
		nYear = 1600;
		if (stDate <= 30){
			nMonth = 1;
			nDay = stDate + 1;
		}
		else{
			nMonth = 2;
			nDay = stDate-30;
		}
	}
	else{
		I = (4*Math.round(stDate-Systools.First2Months))-1;
		J = (4*Integer((I % Systools.Days400Yr) / 4))+3;
		nYear = (100*Integer(I / Systools.Days400Yr))+Integer(J / 1461);
		I = (5*Integer(((J % 1461)+4) / 4))-3;
		nDay = Integer(((I % 153)+5) / 5);
		nMonth = Integer(I / 153);
		if (nMonth < 10){
			nMonth = nMonth + 3;
		}
		else{
			nMonth = nMonth - 9;
			nYear = nYear + 1;
		}
		nYear = nYear + 1600;
	}
	oDate = new Date();
	oDate.setDate(1);
	oDate.setMonth(0);
	oDate.setYear(nYear);
	oDate.setMonth(nMonth-1);
	oDate.setDate(nDay);
	return oDate;
}

Systools.MonthToMonthString = function(nMonth){
	switch(nMonth){
		case 0:  return String("Januar");
		case 1:  return String("Februar");
		case 2:  return String("Marts");
		case 3:  return String("April");
		case 4:  return String("Maj");
		case 5:  return String("Juni");
		case 6:  return String("Juli");
		case 7:  return String("August");
		case 8:  return String("September");
		case 9:  return String("Oktober");
		case 10:  return String("November");
		case 11:  return String("December");
		default:  return String("-");
	}
}
