///////////////////////////////////////////////////////////////////////////////////////
// set input focus


function setInputFocusDate()
{
	if (document.forms[0])
	{		
		// trip planner date
		//	if (document.forms[0].itdDateDay && document.forms[0].nameState_origin && document.forms[0].nameState_origin.value=="empty" && document.forms[0].placeState_origin && document.forms[0].placeState_origin.value=="empty" && document.forms[0].nameState_destination && document.forms[0].nameState_destination.value=="empty" && document.forms[0].placeState_destination && document.forms[0].placeState_destination.value=="empty")
		if (document.forms[0].itdDateDay)
		{ document.forms[0].itdDateDay.focus(); }
		test=true;
	}
	
}


function setInputFocus()
{
	if (document.forms[0])
	{		
		// trip planner date
		//	if (document.forms[0].itdDateDay && document.forms[0].nameState_origin && document.forms[0].nameState_origin.value=="empty" && document.forms[0].placeState_origin && document.forms[0].placeState_origin.value=="empty" && document.forms[0].nameState_destination && document.forms[0].nameState_destination.value=="empty" && document.forms[0].placeState_destination && document.forms[0].placeState_destination.value=="empty")
		//if (document.forms[0].itdDateDay)
		//{ document.forms[0].itdDateDay.focus(); }
		
		// personal timetable weekday
		//else 
		
		if (document.forms[0].psParamWeekday && document.forms[0].psParamWeekday.type!="hidden" && document.forms[0].nameState_origin && document.forms[0].nameState_origin.value=="empty" && document.forms[0].placeState_origin && document.forms[0].placeState_origin.value=="empty" && document.forms[0].nameState_destination && document.forms[0].nameState_destination.value=="empty" && document.forms[0].placeState_destination && document.forms[0].placeState_destination.value=="empty")
		{ document.getElementById("psParamWeekday").focus(); }
		
		// personal timetable for pda time
		else if (document.forms[0].itdTimeHour_arrFrom && document.forms[0].nameState_origin && document.forms[0].nameState_origin.value=="empty" && document.forms[0].placeState_origin && document.forms[0].placeState_origin.value=="empty" && document.forms[0].nameState_destination && document.forms[0].nameState_destination.value=="empty" && document.forms[0].placeState_destination && document.forms[0].placeState_destination.value=="empty")
		{ document.forms[0].itdTimeHour_arrFrom.focus(); }
		
		// origin name
		else if (document.forms[0].name_origin && document.forms[0].name_origin.type!="hidden")
		{ document.forms[0].name_origin.focus(); }

		// destination name
		else if (document.forms[0].name_destination && document.forms[0].name_destination.type!="hidden")
		{ document.forms[0].name_destination.focus(); }
		
		// itinerary
		else if (document.getElementById("tripPrev"))
		{ document.getElementById("tripPrev").focus(); }

		// fares
		else if (document.getElementById("fare"))
		{ document.getElementById("fare").focus(); }

		// download file
		else if (document.getElementById("download"))
		{ document.getElementById("download").focus(); }

		// download reset button
		else if (document.getElementById("downloadReset"))
		{ document.getElementById("downloadReset").focus(); }

		// map request
		else if (document.forms[0].stopSelector)
		{ document.forms[0].stopSelector.focus(); }
		
		// stop timetable type
		else if (document.forms[0].type_stt && document.forms[0].type_stt.value!="hp" && document.forms[0].nameState_stt.value=="empty")
		{ document.getElementById("stop").focus(); }
		
		// stop timetable odv
		else if (document.forms[0].name_stt && document.forms[0].name_stt.type!="hidden")
		{ document.forms[0].name_stt.focus(); }
		
		// assigned stop stt
		else if (document.forms[0].assignedStop_stt && document.forms[0].assignedStop_stt[0] && document.forms[0].assignedStop_stt.type!="hidden")
		{ document.forms[0].assignedStop_stt[0].focus(); }
		
		// assigned stop stt
		else if (document.forms[0].assignedStop_stt && document.forms[0].assignedStop_stt.type!="hidden")
		{ document.forms[0].assignedStop_stt.focus(); }
		
		// departure monitor date
		else if (document.forms[0].itdDateDay && document.forms[0].nameState_dm && document.forms[0].nameState_dm.value=="empty" && document.forms[0].placeState_dm && document.forms[0].placeState_dm.value=="empty")
		{ document.forms[0].itdDateDay.focus(); }
		
		// departure monitor odv
		else if (document.forms[0].name_dm && document.forms[0].name_dm.type!="hidden")
		{ document.forms[0].name_dm.focus(); }
		
		// line selection
		else if (document.forms[0].dmLineSelection && document.forms[0].dmLineSelection[0].type!="hidden")
		{ document.forms[0].dmLineSelection.focus(); }
		
		// assigned stop dm
		else if (document.forms[0].assignedStop_dm && document.forms[0].assignedStop_dm[0] && document.forms[0].assignedStop_dm.type!="hidden")
		{ document.forms[0].assignedStop_dm[0].focus(); }
		
		// assigned stop dm
		else if (document.forms[0].assignedStop_dm && document.forms[0].assignedStop_dm.type!="hidden")
		{ document.forms[0].assignedStop_dm.focus(); }
		
		// dm for pda
		else if (document.forms[0].name_ss && document.forms[0].name_ss.type!="hidden")
		{ document.forms[0].name_ss.focus(); }
		
		// dm for pda assigned stop
		else if (document.forms[0].assignedStop_ss && document.forms[0].assignedStop_ss.type!="hidden")
		{ document.forms[0].assignedStop_ss[0].focus(); }
		
		// pda E-Mail address
		else if (document.forms[0].emailAddress && document.forms[0].emailAddress.type!="hidden")
		{ document.forms[0].emailAddress.focus(); }
		
		// pda reset button
		else if (document.getElementById("pdaReset"))
		{ document.getElementById("pdaReset").focus(); }
		
		// timetable book
		else if (document.forms[0].lineName && document.forms[0].lineName.type!="hidden")
		{ document.forms[0].lineName.focus(); }
		
		// timetable book line selection
		else if (document.forms[0].lineSelIndex)
		{ document.forms[0].lineSelIndex.focus(); }
		
		// spa type
		else if (document.forms[0].type_rop && document.forms[0].nameState_rop && document.forms[0].nameState_rop.value=="empty")
		{ document.getElementById("stop").focus(); }
		
		// login
		else if (document.forms[0].netUsername && document.forms[0].netUsername.type!="hidden")
		{ document.forms[0].netUsername.focus(); }

		// first login and personal profile person
		else if (document.forms[0].ppPersonName && document.forms[0].ppPersonName.type!="hidden")
		{ document.forms[0].ppPersonName.focus(); }

		// personal profil odv
		else if (document.forms[0].ppOdvIdentifier && document.forms[0].ppOdvIdentifier.type!="hidden")
		{ document.forms[0].ppOdvIdentifier.focus(); }

		// personal profil options
		else if (document.forms[0].ppPTComfortMaxChanges && document.forms[0].ppPTComfortMaxChanges.type!="hidden")
		{ document.forms[0].ppPTComfortMaxChanges[0].focus(); }

		// personal profil configuration
		else if (document.forms[0].ppPTConfigurationSenderInfo && document.forms[0].ppPTConfigurationSenderInfo.type!="hidden")
		{ document.forms[0].ppPTConfigurationSenderInfo.focus(); }

		// print options
		else if (document.forms[0].psParamAddressName && document.forms[0].psParamAddressName.type!="hidden")
		{ document.forms[0].psParamAddressName.focus(); }
		
						
	}
	
}

/////////////////////////////////////////////////////////////////////////////////////
// Verify Date
function showdate1( year, month, date )
{
      if (date < 10) {
	date = "0" + date;
      }
      else {
	date = date;
      }

      if (month < 10) {
	month = "0" + month;
      }
      else {
	month = month;
      }

      year = year + "";
      if (year.length > 2) {
      	year = year.substring(3,4);
	if (year < 10){
		year = "0" + year;
	}
	else {
		year = year;
	}
       }
       else{
	  if (year < 10){
		year = "0" + year;
	  }
	  else {
		year = year;
	  }
	}

      	document.forms[0].itdDateYear.value = year;
      	document.forms[0].itdDateMonth.value = month;
      	document.forms[0].itdDateDay.value = date;
}

//////////////////////////////////////////////////////////////////////
//Set dmLineSelection All of true
function preSubmit(){


	if (document.forms[0].dmLineSelection) {
		document.forms[0].dmLineSelectionAll.value = 0;
	}
	if (document.forms[0].dmLineSelection && document.forms[0].dmLineSelection.value == 'all') {
		document.forms[0].dmLineSelectionAll.value = 1;
	}
	
	if (document.forms[0].lineSelIndex && document.forms[0].lineSelIndex.value == 'all') {
		document.forms[0].ppLineSelectionAll.value = 1;
	}
	
	
}

///////////////////////////////////////////////////////////////////////
//Function Up Down arrows
function dateUpDown(upOrDown) { 
	var tag;
	var monat;
	var jahr;

	tag = document.forms[0].itdDateDay.value;
	monat = document.forms[0].itdDateMonth.value;
	jahr = document.forms[0].itdDateYear.value;

	var jahrIntWert = parseFloat(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();
	jahr = jahr.substr(2,2);

	document.forms[0].itdDateDay.value = tag;
	document.forms[0].itdDateMonth.value = monat;
	document.forms[0].itdDateYear.value = jahr;
}

////////////////////////////////////////////////////////////////////////////////////////
// submit a form out of a combo box (IE Version only)
function checkReturnKey()
{
	if (window.event && window.event.keyCode==13)
	{		
		if (document.forms[0].execInst) {
			document.forms[0].execInst.value='normal';
		}
		
		preSubmit()
		
		prepareSubmittingHistoryOdvs()
		
		document.forms[0].submit();
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// close Java Applet and open XSLT Map
function openXSLTMapFormJavaApplet()
{
	if (window.opener)
	{
		window.opener.document.forms[0].itdLPxx_java.value='0';
		window.opener.document.forms[0].command.value="mapRequest_" + window.opener.odvMapRequest.mapUsage ;
		window.opener.document.forms[0].submit();
	}
	window.close()
}

/////////////////////////////////////////////////////////////////////////////////////////
// selectAssignedStops value selection
function selectAssignedStopsValueSelection()
{
	if (document.forms[0].reqType && document.forms[0].reqType.value == 'spa' && document.forms[0].selectAssignedStops)
	{
		document.forms[0].selectAssignedStops.value = 0;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// extract the type and name of the selected last odv by splitting the value
// at the ";" and fills the HTTP-parameters
function splitLastOdvValue(usage)
{
	// get the value of the selected lastOdv
	var name_usage = "name_" + usage;
	var valueNameUsage = document.getElementsByName(name_usage)[0].value;
	//alert("valueNameUsage: " + valueNameUsage);
	
	// if we have a valid value
	var valueLength = valueNameUsage.length;
	//alert("valueLength: " + valueLength);
	if (valueLength != 0)
	{
		// get the index of the ";"
		var ix = valueNameUsage.split(';');
		var odvPlace = ix[0];
		var odvType = ix[1];
		var odvName = ix[2];
		
		//alert("odvPlace: " + odvPlace);
		//alert("odvType: " + odvType);
		//alert("odvName: " + odvName);
		
		if (odvType == "place")
		{
			document.getElementsByName("place_" + usage)[0].value=odvPlace;
		}
		else
		{
			document.getElementsByName("placeInfo_" + usage)[0].value=odvPlace;
		}
		
		document.getElementsByName("typeInfo_" + usage)[0].value=odvType;		
		document.getElementsByName("nameInfo_" + usage)[0].value=odvName;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// Checks origin and destination for historical point type and prepares the
// http parameter where necessary.
function prepareSubmittingHistoryOdvs()
{

	if (document.forms[0].type_origin && document.forms[0].type_origin.value == "hp")
	{
		splitLastOdvValue('origin');
	}
	

	if (document.forms[0].type_destination && document.forms[0].type_destination.value == "hp")
	{
		splitLastOdvValue('destination');
	}
	
	if (document.forms[0].type_stt && document.forms[0].type_stt.value == "hp")
	{
		splitLastOdvValue('stt');
	}
	
	if (document.forms[0].type_dm && document.forms[0].type_dm.value == "hp")
	{
		splitLastOdvValue('dm');
	}
	
	if (document.forms[0].type_ttb && document.forms[0].type_ttb.value == "hp")
	{
		splitLastOdvValue('ttb');
	}
	
	if (document.forms[0].type_rop && document.forms[0].type_rop.value == "hp")
	{
		splitLastOdvValue('rop');
	}
	
	if (document.forms[0].type_ss && document.forms[0].type_ss.value == "hp")
	{
		splitLastOdvValue('ss');
	}
	
}

/////////////////////////////////////////////////////////////////////////////////////////
// Creates and error message becaus place is empty
function messagePlaceEmpty(errorMessage)
{
	alert(errorMessage);
	return false;
}
/////////////////////////////////////////////////////////////////////////////////////////
// get string length of place
function getStringLengthOfPlace(usage)
{
	var placeValue = document.getElementsByName("place_" + usage)[0].value;
	var placeValueLength = placeValue.length;
	return placeValueLength;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Calls the "checkPlaceOfOdv" function if necessary
function callCheckPlaceOfOdv(errorMessage)
{
	var originPlaceStringLength = getStringLengthOfPlace("origin");
	var destinationPlaceStringLength = getStringLengthOfPlace("destination");
	
	if (!(originPlaceStringLength > 0))
	{
		messagePlaceEmpty(errorMessage);
		
	}
	else if (!(destinationPlaceStringLength > 0))
	{
		messagePlaceEmpty(errorMessage);
	}
}
/////////////////////////////////////////////////////////////////////////////////////////
// Check if EMail Address input box have been filled with a valid EMail
var testresults
function checkEMail(errorText){
	var str=document.forms[0].ppPersonEmail.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (str && filter.test(str)) {
		testresults=true;
	}
	else {
		alert(errorText);
		testresults=false;
	}
	return (testresults)
}





function setSTTLine(infos) {

	var values = infos.split(':');


	var line = values[0];
	var dir = values[1];
	var stop = values[2];
	var proj = values[3];
	
	var multiple = 'XSLT_STT_REQUEST?language=de&name_stt=' + stop +'&type_stt=stop&mode=direct&net=vvs';

	
	// document.getElementById('submitLink').href = 'XSLT_STT_REQUEST?language=de&name_stt=' + stop +'&type_stt=stop&mode=direct&net=vvs&line=' + line + '&direction=' + dir + '&project=' + proj;


	for (i = 0; i < document.forms[0].elements['dmLineSelection'].length; i ++) {
	
		if(document.forms[0].elements['dmLineSelection'].options[i].selected){
		//alert(document.forms[0].elements['dmLineSelection'].options[i].value + i);
		
			var optionsValue = document.forms[0].elements['dmLineSelection'].options[i].value
		
			var values2 = optionsValue.split(':');
		
			var line2 = values2[0];
			var dir2 = values2[1];
			var stop2 = values2[2];
			var proj2 = values2[3];
			var sup = values2[4];
		
		
		  multiple = multiple + '&line=vvs:' + line2 + ':' + sup + ':' + dir2 + ':' + proj2;
		}
	}

	document.getElementById('submitLink').href = multiple;

}



function setTTBLine(infos) {

	var values = infos.split(':');

	var line = values[0];
	var dir = values[1];
	var sup = values[2];
	var proj = values[3];

	
	document.getElementById('submitLink').href = 'XSLT_TTB_REQUEST?language=de&command=direct&net=vvs&line=' + line + '&direction=' + dir + '&sup=' + sup + '&project=' + proj;
}



function setROPLine(infos) {

	var values = infos.split(':');

	var line = values[0];
	var dir = values[1];
	var sup = values[2];
	var proj = values[3];

	
	document.getElementById('submitLink').href = 'XSLT_ROP_REQUEST?language=de&command=direct&net=vvs&line=' + line + '&direction=' + dir + '&sup=' + sup + '&project=' + proj;
}
