var svPoiHierarchy = new Array;

function mapResult()
{
	this.type		= "address";
	this.centerX		= 0;
	this.centerY		= 0;
	this.mapName		= "";
	this.coordValue		= "";
	this.desc		= "";
	this.placeName		= "";
	this.omc		= 0;
	this.placeID		= 0;
	this.streetName		= "";
	this.streetID		= 0;
	this.poiID		= 0;
	this.stopList		= new Array();
	this.stopNameList	= new Array();
}

function mapRequest()
{
	this.odvMap		= null;
	this.mapUsage	= "unknown";
	this.mapType	= "unknown";
	this.objectList = new Array();
}

var odvMapResult  = new mapResult();
var odvMapRequest = new mapRequest();

function setMapInput()
{
	if (odvMapResult && odvMapRequest.mapUsage)
	{
		if (odvMapResult.type == "address")
		{

			obj = document.forms[0].elements['typeInfo_'+odvMapRequest.mapUsage];
			if (obj) obj.value = "coord";
			
			obj = document.forms[0].elements['nameInfo_'+odvMapRequest.mapUsage];
			if (obj) obj.value = odvMapResult.coordValue;

			if (odvMapRequest.mapUsage!='dm'){
				obj = document.forms[0].elements['execInst'];
				if (obj) obj.value = "verifyOnly";
			}

			document.forms[0].submit();
		}
		else if (odvMapResult.type == "poi")
		{
			var obj = document.forms[0].elements['placeInfo_'+odvMapRequest.mapUsage]; 
			if (obj) obj.value = odvMapResult.omc + ":" + odvMapResult.placeID;

			obj = document.forms[0].elements['typeInfo_'+odvMapRequest.mapUsage];
			if (obj) obj.value = "poiID";
			
			obj = document.forms[0].elements['nameInfo_'+odvMapRequest.mapUsage];
			if (obj) obj.value = odvMapResult.poiID;

			if (odvMapRequest.mapUsage!='dm'){
				obj = document.forms[0].elements['execInst'];
				if (obj) obj.value = "verifyOnly";
			}

			document.forms[0].submit();
		}
		else if (odvMapResult.type == "stop")
		{
			var obj = document.forms[0].elements['placeInfo_'+odvMapRequest.mapUsage];
			if (obj) obj.value = "";

			obj = document.forms[0].elements['typeInfo_'+odvMapRequest.mapUsage];
			if (obj) obj.value = "stopID";
			
			var obj = document.forms[0].elements['nameInfo_'+odvMapRequest.mapUsage];
			if (obj) 
			{
				obj.value = "";
				for (var iStop = 0; iStop < odvMapResult.stopList.length; iStop++)
				{
					if (iStop > 0) obj.value += ":";
					obj.value += odvMapResult.stopList[iStop];
				}
			}

			if (odvMapRequest.mapUsage!='dm'){
				obj = document.forms[0].elements['execInst'];
				if (obj) obj.value = "verifyOnly";
			}

			obj = document.forms[0].elements['deleteAssignedStops_'+odvMapRequest.mapUsage];
			if (obj) obj.value = '1';

			document.forms[0].submit();
		}
	}
}


function getPOIHierarchy(placeID, usage){

	var xmlhttp;
	if (window.XMLHttpRequest){
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
	  // code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	  
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4){

			var pois = xmlhttp.responseXML.getElementsByTagName('itdPoiNode')
			var selBox = document.getElementById('nameHierarchy_' + usage)
			
			for(i=0; i < pois.length; i ++){

				newEntry = new Option(pois[i].childNodes[0].nodeValue, 'poiHierarchy:' + pois[i].getAttribute('hierarchy'), false, true);
				selBox.options[selBox.length] = newEntry;
				document.getElementById('savePoiH_' + usage).value += pois[i].childNodes[0].nodeValue + ':' + pois[i].getAttribute('hierarchy') + '|';
			}
			
			if(selBox.clientWidth < 213){
				selBox.style.width='213px'
			}
			else{
				selBox.style.width='auto';
			}
			
			selBox.options[0].selected = true;
			
			if(usage=='destination'){ 
				document.forms[0].type_destination.value='poi';
			}
			else{
				document.forms[0].type_origin.value='poi';
			}
		}
	}
	var host = "XML_TRIP_REQUEST2?type_origin=poi&name_origin=&placeInfo_origin=" + placeID
	
	xmlhttp.open("GET", host, true);
	xmlhttp.send(null);
	
}

function setPOIHierarchy(usage){

	if(document.getElementById('savePoiH_'+ usage)){
		var pois = document.getElementById('savePoiH_' + usage).value.split('|');
		var selBox = document.getElementById('nameHierarchy_' + usage);
		var selBox2 = document.getElementById('nameSubhierarchie_' + usage);
		var selBox3 = document.getElementById('namePOI_' + usage);
		var selOption = 0;

		for(i=0; i < pois.length -1; i ++){
			newEntry = new Option(pois[i].split(':')[0], 'poiHierarchy:' + pois[i].split(':')[1], false, true);
			selBox.options[selBox.length] = newEntry;
			
			if(document.getElementById('poiHierarchyValue_' + usage).value == pois[i].split(':')[1]){
				selOption = selBox.length -1;
			}
		}
		
		if(selBox.clientWidth < 213){
			selBox.style.width='213px'
		}
		else{
			selBox.style.width='auto';
		}
		
		if(selBox2.clientWidth < 213){
			selBox2.style.width='213px'
		}
		else{
			selBox2.style.width='auto';
		}
		
		if(selBox3.clientWidth < 213){
			selBox3.style.width='213px'
		}
		else{
			selBox3.style.width='auto';
		}
		
		selBox.options[selOption].selected = true;
	
		if(usage=='destination'){ 
			document.forms[0].type_destination.value='poi';
		}
		else{
			document.forms[0].type_origin.value='poi';
		}
	}
}

function checkPOIs() {

	if(!document.forms[0].placeState_origin){
		return false;
	}

	if(document.forms[0].placeState_origin.value =='identified' 
	&& document.forms[0].nameState_origin.value =='empty'
	&& document.getElementById('odvType_poi_origin').checked==true){
		getPOIHierarchy(document.forms[0].poiPlaceId_origin.value, 'origin')
	}
	else if(document.forms[0].nameState_origin.value !='identified' 
		&& document.forms[0].anyObjFilter_origin.value==32){
		setPOIHierarchy('origin');
	}
	
	if(document.forms[0].placeState_destination.value =='identified' 
	&& document.forms[0].nameState_destination.value =='empty'
	&& document.getElementById('odvType_poi_destination').checked==true){
		getPOIHierarchy(document.forms[0].poiPlaceId_destination.value, 'destination')
	}
	else if(document.forms[0].nameState_destination.value !='identified' &&
		document.forms[0].anyObjFilter_destination.value==32){
		setPOIHierarchy('destination');
	}
}

function submitEventListener(){

	if(document.getElementById('calcFare')){
	  if(this.addEventListener){
		document.forms[0].addEventListener("submit", checkDwellTime, false);
	  }
	  else if(this.attachEvent) {
		document.forms[0].attachEvent("onsubmit", checkDwellTime);
	  } 
	}
}

function checkDwellTime(){

	if(!document.forms[0].name_via.value=='' && document.getElementById('calcFare')){
		if(document.getElementById('dwellTime').value=='00:00'){
			document.getElementById('calcFare').value='1';
		}
		else{
			document.getElementById('calcFare').value='0';
		}
	}
}


function addOnload(onloadFunc) {

  if(this.addEventListener){
    this.addEventListener("load", onloadFunc, false);
  }
  else if(this.attachEvent) {
    this.attachEvent("onload", onloadFunc);
  } 
  else{
    var onloadOld = this.onload;
    this.onload = function() { onloadOld(); onloadFunc(); }
  }
}

addOnload(function() { checkPOIs() });
addOnload(function() { submitEventListener() });
