/* ############################################################################################################
###	VRN GmbH - EFA9 Interface - Markus Müller																###
###	Kunde: VRT GmbH   																						###
###	template: dynamic functions für das VRT-Layout															###
############################################################################################################ */

/* ### // Initialisierung // ######################################## */
var dFocus = false;
var hFocus = false;
var mFocus = false;

function set_date()
{
	var day, month, year;
	
	if (document.eingabe.action != 'XSLT_PS_REQUEST2')
	{
		day = document.eingabe.itdDateDay.value;
		month = document.eingabe.itdDateMonth.value;
		year = document.eingabe.itdDateYear.value;

		timeString = day + "." + month + "." + year;
		document.eingabe.date.value = timeString;
	}
}

/* ### // set the psparamdate in the ps request // ######################################## */
function set_date_ps() {
	var day, month, year, hours, minutes, seconds, ap;
	var intHours, intMinutes, intSeconds, intDay, intMonth, intYear;
	var today;

	today = new Date();
	intHours = today.getHours();
	intMinutes = today.getMinutes();
	intSeconds = today.getSeconds();
	intDay = today.getDate();
	intYear = today.getFullYear();
	intMonth = today.getMonth() + 1;

	if (intDay < 10) {
		day = "0"+intDay;
	} else {
		day = intDay;
	} 

	if (intMonth < 10) {
		month = "0"+intMonth;
	} else {
		month = intMonth;
	} 

	document.eingabe.psParamSampleDay.value = day;
	document.eingabe.psParamSampleMonth.value = month;
	document.eingabe.psParamSampleYear.value = intYear;
}


/* ### // set the psparamdate in the ps request as hidden parameter // ######################################## */
function setsampledate() {

	var date;
	var intDay, intMonth, day, month;
	
	intDay = document.eingabe.psParamSampleDay.value;
	intMonth = document.eingabe.psParamSampleMonth.value;

	var test=intDay.length;

	if (intDay.length == '1') {
		day = "0"+intDay;
	} else {
		day = intDay;
	} 

	if (intMonth.length == '1') {
		month = "0"+intMonth;
	} else {
		month = intMonth;
	} 

	date = document.eingabe.psParamSampleYear.value + month + day;
	document.eingabe.psParamSampleDate.value =date;
}


/* ### // change the class of an obj // ######################################## */
function change_class(id, obj_class) {
	document.getElementById(id).className = obj_class;
}


/* ### // clear the input field of the odv // ######################################## */
function clear_value(obj)
{
	if (obj.value == 'hier eintragen: Ort, Haltestelle oder Adresse oder interessanter Punkt (POI)' || obj.value == 'Enter: Place, Stop, Address or Point of Interest (POI)')
	{
		obj.value = '';
	} else if (obj.value == 'hier eintragen: Ort, Haltestelle' || obj.value == 'Enter: Place, Stop')
	{
		obj.value = '';
	}
}


/* ### // hide the map in the form // ######################################## */
function nomap()
{		
	divstate = document.getElementById('vrnMap');
	divstate.style.display = "none";
}


/* ### // hide an obj in the form // ######################################## */
function hide_id(obj)
{
	document.getElementById(obj).style.display ="none";
}


/* ### // set the focus to the input field // ######################################## */
function setFocus(obj)
{
	var panel = document.getElementById(obj);
	if (panel)
		panel.focus();
	else {
		return true;
	}
}


/* ### // show the zonePanel in the form // ######################################## */
function show_zonePanel(obj)
{
	var test= obj.id;
	var position = test.substring(9, 11);
	var img = document.getElementById(obj.id);
	var panel = document.getElementById('zonePanel' + position);
	var imgpanel = document.getElementById('zonePanelimg' + position);

	find_position(img);
	var objLeft = imgOffsetLeft -228;
	var objTop  = imgOffsetTop  + 20;
	
	if (panel)  {
		panel.style.left = objLeft + "px";
		panel.style.top  = objTop + "px";
		panel.style.display = "inline";
		panel.style.color = "black";
		panel.style.padding = "5px";
		panel.style.background = "#d2d2d4";
		panel.style.border = "solid 1px";
		Drag.init(panel);
	}

	var objLeft2 = imgOffsetLeft + 118;
	var objTop2  = imgOffsetTop  + 30;

	if (imgpanel)
	{
		imgpanel.style.left = objLeft2 + "px";
		imgpanel.style.top  = objTop2 + "px";
		imgpanel.style.display = "inline";
	}
}


/* ### // hide the zonePanel in the form // ######################################## */
function hide_zonePanel(pos)
{
	var pos= pos;
	var panel = document.getElementById('zonePanel' + pos);
	var imgpanel = document.getElementById('zonePanelimg' + pos);

	if (panel)
	{
		panel.style.display = "none";
	}
	if (imgpanel)
	{
		imgpanel.style.display = "none";
	}
}


/* ### // show the route details in the form // ######################################## */
function show_routePanel(pos)
{
	var pos= pos;
	var panel = document.getElementById('route_panel_' + pos);

	if (panel)
	{
		panel.style.display = "inline";
		document.getElementById('show_rp_' + pos).style.display ="none";
		document.getElementById('hide_rp_' + pos).style.display ="inline";
		document.getElementById('route_ani_' + pos).style.display ="inline";
	}
}


/* ### // hide the routePanel in the form // ######################################## */
function hide_routePanel(pos)
{
	var pos= pos;
	var panel = document.getElementById('route_panel_' + pos);

	if (panel)
	{
		panel.style.display = "none";
		document.getElementById('show_rp_' + pos).style.display ="inline";
		document.getElementById('hide_rp_' + pos).style.display ="none";
		document.getElementById('route_ani_' + pos).style.display ="none";
	}
}


/* ### // find the position of the obj // ######################################## */
function find_position(obj)
{
	imgOffsetTop=0;imgOffsetLeft=0;

	do 
	{
		imgOffsetTop+=obj.offsetTop;
		imgOffsetLeft+=obj.offsetLeft;
		obj=obj.offsetParent;
	} while (obj.tagName != "BODY" && obj);
}


/* ### // check the values of the input fields // ######################################## */
function check_value()
{
	var mapusage = document.eingabe.itdLPxx_mapusage.value;
	if(! (mapusage == 'stt' || mapusage == 'ttb' || mapusage == 'rop' || mapusage == 'dm' || mapusage == 'ss')){
		if (document.eingabe.nameState_origin.value == 'empty')
		{
			var nameorigin = document.eingabe.name_origin.value;
		}
		if (document.eingabe.nameState_destination.value  == 'empty')
		{
			var namedestination = document.eingabe.name_destination.value;
		}
		if (document.eingabe.nameState_via.value  == 'empty')
		{
			var namevia = document.eingabe.name_via.value;
		}
		if (nameorigin == 'hier eintragen: Ort, Haltestelle oder Adresse oder interessanter Punkt (POI)' || nameorigin == 'Enter: Place, Stop, Address or Point of Interest (POI)')
		{
			document.eingabe.name_origin.value = '';
		}
		if (namedestination == 'hier eintragen: Ort, Haltestelle oder Adresse oder interessanter Punkt (POI)' || namedestination == 'Enter: Place, Stop, Address or Point of Interest (POI)')
		{
			document.eingabe.name_destination.value = '';
		}
		if (namevia == 'hier eintragen: Ort, Haltestelle oder Adresse oder interessanter Punkt (POI)' || namevia == 'Enter: Place, Stop, Address or Point of Interest (POI)')
		{
			document.eingabe.name_via.value = '';
		}
	} else if (document.eingabe.itdLPxx_mapusage.value == 'stt'){
		if (document.eingabe.nameState_stt.value == 'empty')
		{
			var namestt = document.eingabe.name_stt.value;
		}
		if (namestt == 'hier eintragen: Ort, Haltestelle' || namestt == 'Enter: Place, Stop')
		{
			document.eingabe.name_stt.value = '';
		}
	} else if (document.eingabe.itdLPxx_mapusage.value == 'ttb'){
		if (document.eingabe.nameState_ttb.value == 'empty')
		{
			var namettb = document.eingabe.name_ttb.value;
		}
		if (namettb == 'hier eintragen: Ort, Haltestelle')
		{
			document.eingabe.name_ttb.value = '';
		}
	} else if (document.eingabe.itdLPxx_mapusage.value == 'rop'){
		if (document.eingabe.nameState_rop.value == 'empty')
		{
			var namerop = document.eingabe.name_rop.value;
		}
		if (namerop == 'hier eintragen: Ort, Haltestelle')
		{
			document.eingabe.name_rop.value = '';
		}
	} else if (document.eingabe.itdLPxx_mapusage.value == 'dm'){
		if (document.eingabe.nameState_dm.value == 'empty')
		{
			var namedm = document.eingabe.name_dm.value;
		}
		if (namedm == 'hier eintragen: Ort, Haltestelle oder Adresse oder interessanter Punkt (POI)')
		{
			document.eingabe.name_dm.value = '';
		}
	} else if (document.eingabe.itdLPxx_mapusage.value == 'ss'){
		if (document.eingabe.nameState_ss.value == 'empty')
		{
			var namess = document.eingabe.name_ss.value;
		}
		if (namess == 'hier eintragen: Ort, Haltestelle')
		{
			document.eingabe.name_ss.value = '';
		}
	}
}


/* ### // check the values of the input fields for the ps request // ######################################## */
function check_value_2()
{
	var mapusage = document.eingabe.itdLPxx_mapusage.value;

	if (document.eingabe.nameState_origin.value == 'empty')
	{
		var nameorigin = document.eingabe.name_origin.value;
	}
	if (document.eingabe.nameState_destination.value  == 'empty')
	{
		var namedestination = document.eingabe.name_destination.value;
	}
		
	if (nameorigin == 'hier eintragen: Ort, Haltestelle oder Adresse oder interessanter Punkt (POI)')
	{
		document.eingabe.name_origin.value = '';
	}
	
	if (namedestination == 'hier eintragen: Ort, Haltestelle oder Adresse oder interessanter Punkt (POI)')
	{
		document.eingabe.name_destination.value = '';
	}
}


/* ### // convert the date in the form for the efa interface // ######################################## */
function submit_date()
{
	var mydate = document.eingabe.date.value;

	splitString = mydate.split(".");

	document.eingabe.itdDateDay.value=splitString[0];
	document.eingabe.itdDateMonth.value=splitString[1];
	document.eingabe.itdDateYear.value=splitString[2];
}


/* ### // set the date one day in future or past // ######################################## */
function dateUpDown(upOrDown) { 
	var tag;
	var monat;
	var jahr;

	date = document.eingabe.date.value;
	splitString = date.split(".");
	tag = splitString[0];
	monat = splitString[1];
	jahr = splitString[2];

	var jahrIntWert = parseInt(jahr);

	if (jahrIntWert >= 0 && jahrIntWert <= 75)
	{
		jahrIntWert = 2000 + jahrIntWert; 
	} else {
		if (jahrIntWert > 75 && jahrIntWert <= 99)
		{ 
			jahrIntWert = 1900 + jahrIntWert; 
		}
	}
	monat = monat - 1;  
	heute = new Date(jahrIntWert, monat, tag);
	
	var millisecunden = heute.getTime();

	if (upOrDown == 'up')
	{ 
		millisecunden = millisecunden + (24*60*60*1000); 
	}
	if (upOrDown == 'down')
	{ 
		millisecunden = millisecunden - (24*60*60*1000); 
	}
	heute.setTime(millisecunden);
	tag = heute.getDate();
	if (tag > 0 && tag < 10)
	{ 
		tag = "0"+tag; 
	}
	monat = heute.getMonth() + 1;
	if (monat > 0 && monat < 10)
	{ 
		monat = "0"+monat; 
	}
	jahr = "" + heute.getFullYear();
	document.eingabe.date.value = tag + "." + monat + "." + jahr;
}


/* ### // set the hour_minute // ######################################## */
function hour_minute(upOrDown)
 { 
	var hour;
	var minute;
	var tag;
	var monat;
	var jahr;

	date = document.eingabe.date.value;
	hour = document.eingabe.itdTimeHour.value;
	minute = document.eingabe.itdTimeMinute.value;
	splitString = date.split(".");
	tag = splitString[0];
	monat = splitString[1];
	jahr = splitString[2];

	var jahrIntWert = parseInt(jahr);

	if (jahrIntWert >= 0 && jahrIntWert <= 75)
	{ 
		jahrIntWert = 2000 + jahrIntWert; 
	} else {
		if (jahrIntWert > 75 && jahrIntWert <= 99)	    
		{ 
			jahrIntWert = 1900 + jahrIntWert;
		}
	}
	monat = monat - 1;  
	heute = new Date(jahrIntWert, monat, tag, hour, minute);

	var millisecunden = heute.getTime();

	if (upOrDown == 'up_hour')
	{ 
		millisecunden = millisecunden + (60*60*1000); 
	}
	if (upOrDown == 'down_hour' )
	{ 
		millisecunden = millisecunden - (60*60*1000); 
	}
	if (upOrDown == 'up_minute')
	{ 
		millisecunden = millisecunden + (60*1000); 
	}
	if (upOrDown == 'down_minute' )
	{ 
		millisecunden = millisecunden - (60*1000); 
	}
	heute.setTime(millisecunden);
	hour = heute.getHours();
	if (hour >= 0 && hour < 10)
	{ 
		hour = "0"+hour; 
	}
	minute = heute.getMinutes();
	if (minute >= 0 && minute < 10)
	{ 
		minute = "0"+minute; 
	}
	document.eingabe.itdTimeHour.value = hour;
	document.eingabe.itdTimeMinute.value = minute;
}


/* ### // set the keydown event in the form // ######################################## */
document.onkeydown = findTaste;

function findTaste(Ereignis) 
{
	if (!Ereignis)
		Ereignis = window.event;
	if (Ereignis.which) {
		Tastencode = Ereignis.which;
	} else if (Ereignis.keyCode) {
		Tastencode = Ereignis.keyCode;
	}
	findkey(Tastencode);
}

function findkey(Tastencode)
{
	if ((Tastencode == 38) && (dFocus == true))
	{
		dateUpDown('up');
	}
	if ((Tastencode == 40) && (dFocus == true))
	{
		dateUpDown('down');
	}
	if ((Tastencode == 38) && (hFocus == true))
	{
		hour_minute('up_hour');
	}
	if ((Tastencode == 40) && (hFocus == true))
	{
		hour_minute('down_hour');
	}
	if ((Tastencode == 38) && (mFocus == true))
	{
		hour_minute('up_minute');
	}
	if ((Tastencode == 40) && (mFocus == true))
	{
		hour_minute('down_minute');
	}
	if ((Tastencode == 13) && ((document.eingabe.itdLPxx_mapusage.value == "Trip") || (document.eingabe.itdLPxx_mapusage.value == "dm")))
	{
		if (document.forms[0].itdLPxx_options.value == 0)
		{
			check_value_2();
		} else {
			check_value();
		}
		submit_date();
		setTimeout("document.eingabe.submit()",1000);
	}
	if ((Tastencode == 13) && (document.eingabe.itdLPxx_mapusage.value == "ps_Trip"))
	{
		check_value_2();
		setsampledate();
		document.eingabe.submit();
	}
	if ((Tastencode == 13) && (document.eingabe.itdLPxx_mapusage.value == "car_Trip"))
	{
		check_value_2();
		document.eingabe.submit();
	}
	if ((Tastencode == 113) && (document.eingabe.itdLPxx_mapusage.value == "Trip"))
	{
		location.href="XSLT_TRIP_REQUEST2?language=de";
	}
	if ((Tastencode == 13) && ((document.eingabe.itdLPxx_mapusage.value == "stt") || (document.eingabe.itdLPxx_mapusage.value == "ttb") || (document.eingabe.itdLPxx_mapusage.value == "rop") || (document.eingabe.itdLPxx_mapusage.value == "ss")))
	{
		check_value();
		document.eingabe.submit();
	}
	if ((Tastencode == 113) && (document.eingabe.itdLPxx_mapusage.value == "stt"))
	{
		location.href="XSLT_STT_REQUEST?language=de";
	}
	if ((Tastencode == 113) && (document.eingabe.itdLPxx_mapusage.value == "ttb"))
	{
		location.href="XSLT_TTB_REQUEST?language=de";
	}
	if ((Tastencode == 113) && (document.eingabe.itdLPxx_mapusage.value == "rop"))
	{
		location.href="XSLT_TTB_REQUEST?language=de";
	}
	if ((Tastencode == 113) && (document.eingabe.itdLPxx_mapusage.value == "dm"))
	{
		location.href="XSLT_DM_REQUEST?language=de";
	}
	if ((Tastencode == 113) && (document.eingabe.itdLPxx_mapusage.value == "ss"))
	{
		location.href="XSLT_SCREEN_SAVER_REQUEST?language=de";
	}
//	alert(event.keyCode+' : '+String.fromCharCode(event.keyCode));
}


/* ### // show the print-layout // ######################################## */
function submit_print_trip()
{
	document.eingabe.target = 'print_trip';
	document.eingabe.itdLPxx_printTrip.value='1';
	document.forms[0].submit();
}


/* ### // set the street layout in the rop request // ######################################## */
function set_mode(obj)
{
	document.eingabe.mode.value = obj;
}


/* ### // set the drag n drop functionin the form // ######################################## */
var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};
