// ==================================================================
// Go to an anchor within a div without moving the whole page.
// ==================================================================

function divlink(anker) {
	window.location.href ='#' + anker;
}


// ==================================================================
// This function sets the focus on a stop/address/poi list.
// ==================================================================

function setFocus() {
	if (document.getElementById('name_origin') && document.getElementById('name_origin').value=='')
		document.getElementById('name_origin').focus();
	else if (document.getElementById('name_destination') && document.getElementById('name_destination').value=='')
		document.getElementById('name_destination').focus();
	else if (document.getElementById('nameList_origin'))
		document.getElementById('nameList_origin').focus();
	else if (document.getElementById('nameList_destination'))
		document.getElementById('nameList_destination').focus();
}


// ==================================================================
// This function causes submit if the focus is e.g. on a list and
// the enter key is pressed
// ==================================================================


// submit if enter pressed
function checkReturnKey(event) {
	if(!event) event = window.event;
		var keycode = event.which || event.keyCode;

		if (event && event.keyCode==13) {
			document.forms[0].submit();
		}
}

// ==================================================================
// This function removes the example of an input field.
// ==================================================================

function removeDefaultText(input, defaultText) {
	if (input.value == defaultText) {
		input.value = '';
		input.className = 'inputAddress';
	}
}

// ================================================================
// Sets the height and width of the map
// ================================================================

function setMapHeight () {
	var windowHeight = 1;
	var contentHeight = 1;
	var windowHeight;
	var windowWidth = 1000;
	var offsetTop = document.getElementById("resultColumn").offsetTop;

	if (self.innerHeight) { // all except Explorer
		windowHeight = self.innerHeight;
		// minimum width
		windowWidth = self.innerWidth;
		if (windowWidth < 1015) {
			windowWidth = 1015;
			document.getElementById("doc3").style.width = windowWidth + "px";
		}
		else {
			document.getElementById("doc3").style.width = '99%';
		}
		offsetTop -= 157; 
	}
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowHeight = document.documentElement.clientHeight;
		// minimum width
		windowWidth = document.documentElement.clientWidth;
		if (client == 'IE6' && windowWidth < 1000) {
			windowWidth = 1000;
			document.getElementById("doc3").style.width = windowWidth + "px";
		}
		else if (windowWidth < 995) {
			windowWidth = 995;
			document.getElementById("doc3").style.width = windowWidth + "px";
		}
		else {
			document.getElementById("doc3").style.width = '98%';
		}
	}
	else if (document.body) { // other Explorer versions
		windowHeight = document.body.clientHeight;
		// minimum width
		windowWidth = document.body.clientWidth;
		if (windowWidth < 1015) {
			windowWidth = 1015;
			document.getElementById("doc3").style.width = windowWidth + "px";
		}
		else {
			document.getElementById("doc3").style.width = '100%';
		}
	}
		
	// minimum height
	if (windowHeight < 500) 
		windowHeight = 500;

	document.getElementById("doc3").style.height = windowHeight-10+"px";
	document.getElementById("bd").style.height = windowHeight-10+"px";
	document.getElementById("resultColumn").style.height =  windowHeight - offsetTop - 167 +"px";

	contentHeight = windowHeight-168;
	
	// correct height for chrome
	if (client == 'NC6') {
		contentHeight -= 10;
	}

	document.getElementById("mapFrame").style.height = contentHeight+"px";
	document.getElementById("leftColumnArea").style.height = contentHeight+"px";
	
	// result area with footer area
	if (document.getElementById("ft") && document.getElementById("resultArea2")) {
		document.getElementById("resultArea1").style.height = contentHeight/2-10+"px";
		document.getElementById("resultArea2").style.height = contentHeight/2-100+"px";
		var offsetTop2 = offserTop2 = document.getElementById("resultArea2Content").offsetTop - document.getElementById("resultArea2Title").offsetTop + 10;
		document.getElementById("resultArea2Content").style.height = contentHeight/2-100-offsetTop2+"px";
	}
	else if (document.getElementById("resultArea2")) {
		document.getElementById("resultArea1").style.height = contentHeight/2-40+"px";
		document.getElementById("resultArea2").style.height = contentHeight/2-22+"px";
		var offsetTop2 = offserTop2 = document.getElementById("resultArea2Content").offsetTop - document.getElementById("resultArea2Title").offsetTop + 10;
		document.getElementById("resultArea2Content").style.height = contentHeight/2-15-offsetTop2+"px";
	}
	else if (document.getElementById("ft")) {
		document.getElementById("resultArea1").style.height = contentHeight-offsetTop-60+"px";
	}
	else {
		document.getElementById("resultArea1").style.height = contentHeight-offsetTop+"px";
	}
  
	if (mdvMap && mdvMap.mdvMap) {
	 	mdvMap.mdvMap.resize();
	 	if (mdvMap.mdvMiniMap) mdvMap.mdvMiniMap.resize();
  	}
	 //re-initialize rectZoom after resizing of viewport
	if (mdvMap.rectZoom) {
		mdvMap.rectZoom._setDimensions();
	}
}


// ================================================================
// Opens and closes the mini map.
// ================================================================

function toggleMiniMap () {
	if(document.getElementById('smallMap') && document.getElementById('miniMapOpener')) {
		if (document.getElementById('smallMap').style.display == 'none') {
			document.getElementById('smallMap').style.display = 'block';
			document.getElementById('miniMapOpener').style.display = 'none';
		}
		else {
			document.getElementById('smallMap').style.display = 'none';
			document.getElementById('miniMapOpener').style.display = 'block';
		}
	}
}


// ================================================================
// This function shows/hides the label selection for satellite map.
// ================================================================

var toggleLabelTimeout = null;

function toggleLabelSelection(state) {
    if (state=='true' && mdvMap.name && mdvMap.name=='ortho') {
        if (toggleLabelTimeout !== null) {
            clearTimeout(toggleLabelTimeout);
            toggleLabelTimeout = null;
        }
        document.getElementById('satelliteLabelSelection').style.display = 'block';
    } else {
        if (toggleLabelTimeout === null) {
            toggleLabelTimeout = setTimeout(function() {
        	    document.getElementById('satelliteLabelSelection').style.display = 'none';
                toggleLabelTimeout = null;
            }, 1000);
        }
    }
}

// ==================================================================
// This function sets the date in the correct format for the 
// calendar.
// ==================================================================

function setCalendarDate () {
	document.getElementById('calendarDate').innerHTML = document.getElementById('date1').value + '.' + document.getElementById('date2').value + '.' + document.getElementById('date3').value;
}


// ==================================================================
// This function enters the date in the date input fields after
// using the calendar.
// ==================================================================

function getCalendarDate () {
	var date = document.getElementById('calendarDate').innerHTML.split('.');
	document.getElementById('date1').value = date[0];
	document.getElementById('date2').value = date[1];
	document.getElementById('date3').value = date[2];
}


// ==================================================================
// This function shows the advanced options dialogue for a certain 
// mean of transport.  
// ==================================================================

function openAdvancedOption(id) {
	if (document.getElementById(id)) {
		document.getElementById(id).style.display = 'block';
		if (document.getElementById('blockInput')) 
			document.getElementById('blockInput').style.display = 'block';
	}
	// correct the height of the screen blocking div if the advanced options div exceeds the screen height
	setHeight();	
}


// ==================================================================
// This function closes the advanced options dialogue for a certain 
// mean of transport.  
// ==================================================================

function closeAdvancedOption(id) {
	if(document.getElementById(id)) 
		document.getElementById(id).style.display = 'none';
	if (document.getElementById('blockInput'))
			document.getElementById('blockInput').style.display = 'none';
}


// ==================================================================
// This function opens a selected tab and closes the other ones.
// ==================================================================

function openTab (tabNumber, numberOfTabs) {
	// hide all tabs
	for (var i=1; i<=numberOfTabs; i++) {
		if (document.getElementById('tab_' + i)) 
			document.getElementById('tab_' + i).className = 'inactiveTab';
		if (document.getElementById('tabContent_' + i))
			document.getElementById('tabContent_' + i).style.display = 'none';
	}
	// display selected tab
	document.getElementById('tab_' + tabNumber).style.display = 'block';
	document.getElementById('tab_' + tabNumber).className = 'activeTab';
	document.getElementById('tabContent_' + tabNumber).style.display = 'block';
}


// ==================================================================
// This function encodes a string in utf8.
// Source: http://aktuell.de.selfhtml.org/artikel/javascript/utf8b64/utf8.htm
// ==================================================================

function encode_utf8(rohtext) {
    // dient der Normalisierung des Zeilenumbruchs
    rohtext = rohtext.replace(/\r\n/g,"\n");
    var utftext = "";
    for(var n=0; n<rohtext.length; n++) {
        // ermitteln des Unicodes des  aktuellen Zeichens
        var c=rohtext.charCodeAt(n);
        // alle Zeichen von 0-127 => 1byte
        if (c<128)
            utftext += String.fromCharCode(c);
        // alle Zeichen von 127 bis 2047 => 2byte
		else if((c>127) && (c<2048)) {
            utftext += String.fromCharCode((c>>6)|192);
            utftext += String.fromCharCode((c&63)|128);
		}
        // alle Zeichen von 2048 bis 66536 => 3byte
		else {
            utftext += String.fromCharCode((c>>12)|224);
            utftext += String.fromCharCode(((c>>6)&63)|128);
            utftext += String.fromCharCode((c&63)|128);
		}
    }
    return utftext;
}
		 
// ==================================================================
// This function decodes a utf8 string.
// Source: http://aktuell.de.selfhtml.org/artikel/javascript/utf8b64/utf8.htm
// ==================================================================

function decode_utf8(utftext) {
    var plaintext = ""; var i=0; var c=c1=c2=0;
    // while-Schleife, weil einige Zeichen uebersprungen werden
    while(i<utftext.length) {
        c = utftext.charCodeAt(i);
        if (c<128) {
            plaintext += String.fromCharCode(c);
            i++;
		}
        else if((c>191) && (c<224)) {
            c2 = utftext.charCodeAt(i+1);
            plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
            i+=2;
		}
        else {
            c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
            plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
            i+=3;
		}
    }
    return plaintext;
}


// ==================================================================
// This function jumps to an ICS message on the map.
// ==================================================================

function goToICSIcon (x, y, zoomLevel, id, type) {
	if (type != '' && x!='' && y!='') {
		if (zoomLevel=='')
			zoomLevel = mdvMap.mdvMap.config.getZoomLevel(mdvMap.mdvMap.config.getZoomLevelIndex());
		mdvMap.mdvMap.setZoomLevel(zoomLevel);mdvMap.mdvMap.setCentre(new MDVCoordinates(nameMap, x, y));
		var layer;
		if (type == 'routeBlocking' || type == 'lineBlocking')
			layer = mdvMap.mdvMap.getLayer('blockingLine');	
		else if (type == 'routeInfo' || type == 'lineInfo')
			layer = mdvMap.mdvMap.getLayer('infoLine');	
		else if (type == 'stopBlocking')
			layer = mdvMap.mdvMap.getLayer('blockingStop');
		else if (type == 'stopInfo')
			layer = mdvMap.mdvMap.getLayer('infoStop');
		else if (type == 'blocking')
			layer = mdvMap.mdvMap.getLayer('blockings');	
		else if (type == 'roadWorks')
			layer = mdvMap.mdvMap.getLayer('roadWorks');	
		else
			layer = mdvMap.mdvMap.getLayer('information');
		if (layer) {
			var markers = layer.getMarkers();
			for (var m = 0; m < markers.length; m++) {
				var mapMarker = markers[m];
				if (mapMarker.getObjectId().id == id) { 
					mapMarker.toolTip.setPin(true);
					mapMarker.toolTip.display();
					break;	
				}
			}
		}
		mdvMap.mdvMap.update();
	}
}


// ==================================================================
// This function displays/hides ICS message of a certain type 
// (e.g. blockings)  in the list.
// ==================================================================
function toggleTrafficInfo(type, state) {
	// hide messages of a certain type in the list
	if (document.getElementById('icsMessages') && type && type!='') {
		for (var i=0; i<document.getElementById('icsMessages').childNodes.length; i++) {
			if (state && document.getElementById('icsMessages').childNodes[i].className && document.getElementById('icsMessages').childNodes[i].className.indexOf(type) != -1 && document.getElementById('icsMessages').childNodes[i].className.indexOf('invalidICSMessage') == -1) {
				document.getElementById('icsMessages').childNodes[i].style.display = 'block';
			}
			else if (!state && document.getElementById('icsMessages').childNodes[i].className && document.getElementById('icsMessages').childNodes[i].className.indexOf(type) != -1) {
				document.getElementById('icsMessages').childNodes[i].style.display = 'none';
			}
		}
	}
} 


// ==================================================================
// This function displays/hides all ICS message of a certain type 
// (e.g. blockings)  in the list.
// ==================================================================

function toggleAllTrafficInfos() {
	toggleTrafficInfo ('blocking', document.getElementById('itdLPxx_layer_blockings').checked);
	toggleTrafficInfo ('roadWorks', document.getElementById('itdLPxx_layer_roadWorks').checked);
	toggleTrafficInfo ('information', document.getElementById('itdLPxx_layer_information').checked);
}


// ==================================================================
// This function toggles the ICS message content.
// ==================================================================

function toggleICSMessageContent (id, state, routeIndex, partialRouteNo) {
	var routeIdx = '';
	var partialRouteIdx = '';
	if (routeIndex	&& partialRouteNo) {
		routeIdx = '_' + routeIndex;
		partialRouteIdx = '_' + partialRouteNo;
	}
	if (!state && document.getElementById('body_' + id + routeIdx + partialRouteIdx).style.display=='block') {
		document.getElementById('body_' + id + routeIdx + partialRouteIdx).style.display='none';
		document.getElementById('close_' + id + routeIdx + partialRouteIdx).style.display='none';
		document.getElementById('open_' + id + routeIdx + partialRouteIdx).style.display='block';
	}
	else {
		document.getElementById('body_' + id + routeIdx + partialRouteIdx).style.display='block';
		document.getElementById('close_' + id + routeIdx + partialRouteIdx).style.display='block';
		document.getElementById('open_' + id + routeIdx + partialRouteIdx).style.display='none';
	}
}


// ==================================================================
// This function toggles the Parking Error.
// ==================================================================

function toggleParkingError () {
	if (document.getElementById('parkingTypeZone') && document.getElementById('parkingError')) {
		if (document.getElementById('parkingTypeZone').checked) {
			document.getElementById('parkingError').style.display = 'none';
			document.getElementById('parkingErrorZone').style.display = 'block';
		}
		else {
			document.getElementById('parkingError').style.display = 'block';
			document.getElementById('parkingErrorZone').style.display = 'none';
		}
	}
}


// ==================================================================
// This function toggles the Park and Ride Near Origin/Destination-
// Options 
// To overwrite.
// ==================================================================

function toggleNearStartDest(type, state) {}


// ==================================================================
// This function sets the odv type.
// Parameter: 	usage - usage of the odv
//				value - odv type value
// ==================================================================

function setOdvType(usage, val) {
	var hf = document.getElementById('type_' + usage);
	if (hf) {
        hf.value = val;
    }
}


// ==================================================================
// This function requests the print screen.
// ==================================================================

function requestPrintScreen(mode) {
   document.forms['print'].itdLPxx_printScreen.value = mode;
   document.forms['print'].command.value = 'genMap';
   document.forms['print'].version.value = '3';
   document.forms['print'].mapName.value = document.getElementById('itdLPxx_mapName' + mode.substring(mode.indexOf(':') + 1)).value;
   document.forms['print'].submit();
}

