// Home Page Script
//function clFm(ff, dir) { if (dir == 1) { if (ff.value == 'Search Transport Sites') { ff.value = '' } } else { if (ff.value == '') { ff.value = 'Search Transport Sites' } } }
//function SubmitForm(formName) { document.getElementById(formName).submit(); }
//function SubmitMapForm(formName, trip) { var mapForm = document.getElementById(formName); mapForm.trip.value = trip; mapForm.submit(); }
//function initialize() { if (self.DoInitialize) { DoInitialize(); } }

var g_defaultAllText = "Enter location";
var g_defaultAddressText = "Enter street, number and suburb";
var g_defaultStopText = "Enter station/stop";
var g_defaultLandmarkText = "Enter landmark";

function ferryTTClick() {
	var O=document.getElementById('ferryroute');
	var R=document.getElementById('ferrystop');
	if(O[O.selectedIndex].value=='' && R[R.selectedIndex].value=='')
	{
		alert('Please select a Route or Wharf prior to submitting');
		return false;
	}
}

function initPage() {
    CheckFromLoc(0);
    CheckToLoc(0);

    var forms = document.getElementsByTagName('form');

    for (var i = 0; i < forms.length; i++) {
        var inputs = forms[i].getElementsByTagName('input');

        for (var j = 0; j < inputs.length; j++)
            addInputSubmitEvent(forms[i], inputs[j]);
    }
}

function addInputSubmitEvent(form, input) {
    input.onkeydown = function(e) {
        e = e || window.event;
        if (e.keyCode == 13) {
            form.submit();
            return false;
        }
    };
}

// Trip Planner validation script
function validateOnSubmit() {
    var validateSummary;
    var invalid;

    validateSummary = "";
    invalid = null;

    //FROM Check
    if (document.journey.itd_anyObjFilter_origin[0].checked == true) {
        if (document.journey.itd_name_origin.value == "" || document.journey.itd_name_origin.value == g_defaultAllText) {
            validateSummary += "Enter <strong>From</strong> location<br />";
            document.getElementById('validateFromStreet').innerHTML = '*';
            document.getElementById('validateFromSuburb').innerHTML = '';
            document.journey.itd_name_origin.value = g_defaultAllText;
            if (invalid == null) //sets focus to Street
            { invalid = document.journey.itd_name_origin; }

        }
        else {
            document.getElementById('validateFromStreet').innerHTML = '';
            document.getElementById('validateFromSuburb').innerHTML = '';
        }
    }

    if (document.journey.itd_anyObjFilter_origin[1].checked == true) {
        if (document.journey.itd_name_origin.value == "" || document.journey.itd_name_origin.value == g_defaultAddressText) {
            validateSummary += "Enter <strong>From</strong> street, number and suburb<br />";
            document.getElementById('validateFromStreet').innerHTML = '*';
            document.getElementById('validateFromSuburb').innerHTML = '';
            document.journey.itd_name_origin.value = g_defaultAddressText;
            if (invalid == null) //sets focus to Street
            { invalid = document.journey.itd_name_origin; }

        }
        else {
            document.getElementById('validateFromStreet').innerHTML = '';
            document.getElementById('validateFromSuburb').innerHTML = '';
        }
    }

    if (document.journey.itd_anyObjFilter_origin[2].checked == true) {
        if (document.journey.itd_name_origin.value == "" || document.journey.itd_name_origin.value == g_defaultStopText) {
            validateSummary += "Enter <strong>From</strong> station/stop<br />";
            document.getElementById('validateFromStreet').innerHTML = '*';
            document.getElementById('validateFromSuburb').innerHTML = '';
            document.journey.itd_name_origin.value = g_defaultStopText;
            if (invalid == null) //sets focus to Suburb
            {
                invalid = document.journey.itd_name_origin;
            }
        }
        else {
            document.getElementById('validateFromSuburb').innerHTML = '';
        }
    }

    if (document.journey.itd_anyObjFilter_origin[3].checked == true) {
        if (document.journey.itd_name_origin.value == "" || document.journey.itd_name_origin.value == g_defaultLandmarkText) {
            validateSummary += "Enter <strong>From</strong> landmark<br />";
            document.getElementById('validateFromStreet').innerHTML = '*';
            document.getElementById('validateFromSuburb').innerHTML = '';
            document.journey.itd_name_origin.value = g_defaultLandmarkText;
            if (invalid == null) //sets focus to Suburb
            {
                invalid = document.journey.itd_name_origin;
            }
        }
        else {
            document.getElementById('validateFromSuburb').innerHTML = '';
        }
    }
    //End FROM Check

    //TO Check
    if (document.journey.itd_anyObjFilter_destination[0].checked == true) {
        if (document.journey.itd_name_destination.value == "" || document.journey.itd_name_destination.value == g_defaultAllText) {
            validateSummary += "Enter <strong>To</strong> location<br />";

            document.getElementById('validateToStreet').innerHTML = '*';
            document.getElementById('validateToSuburb').innerHTML = '';
            document.journey.itd_name_destination.value = g_defaultAllText;
            if (invalid == null) //sets focus to Street
            {
                invalid = document.journey.itd_name_destination;
            }
        }
        else {
            document.getElementById('validateToStreet').innerHTML = '';
            document.getElementById('validateToSuburb').innerHTML = '';
        }
    }
    if (document.journey.itd_anyObjFilter_destination[1].checked == true) {
        if (document.journey.itd_name_destination.value == "" || document.journey.itd_name_destination.value == g_defaultAddressText) {
            validateSummary += "Enter <strong>To</strong> street, name and suburb<br />";

            document.getElementById('validateToStreet').innerHTML = '*';
            document.getElementById('validateToSuburb').innerHTML = '';
            document.journey.itd_name_destination.value = g_defaultAddressText;
            if (invalid == null) //sets focus to Street
            {
                invalid = document.journey.itd_name_destination;
            }
        }
        else {
            document.getElementById('validateToStreet').innerHTML = '';
            document.getElementById('validateToSuburb').innerHTML = '';
        }
    }

    if (document.journey.itd_anyObjFilter_destination[2].checked == true) {
        if (document.journey.itd_name_destination.value == "" || document.journey.itd_name_destination.value == g_defaultStopText) {
            validateSummary += "Enter <strong>To</strong> station/stop<br />";
            document.getElementById('validateToStreet').innerHTML = '';
            document.getElementById('validateToSuburb').innerHTML = '*';
            document.journey.itd_name_destination.value = g_defaultStopText;
            if (invalid == null) //sets focus to Suburb
            {
                invalid = document.journey.itd_name_destination;
            }
        }
        else {
            document.getElementById('validateToSuburb').innerHTML = '';
        }
    }

    if (document.journey.itd_anyObjFilter_destination[3].checked == true) {
        if (document.journey.itd_name_destination.value == "" || document.journey.itd_name_destination.value == g_defaultLandmarkText) {
            validateSummary += "Enter <strong>To</strong> landmark<br />";
            document.getElementById('validateToStreet').innerHTML = '';
            document.getElementById('validateToSuburb').innerHTML = '*';
            document.journey.itd_name_destination.value = g_defaultLandmarkText;
            if (invalid == null) //sets focus to Suburb
            {
                invalid = document.journey.itd_name_destination;
            }
        }
        else {
            document.getElementById('validateToSuburb').innerHTML = '';
        }
    }
    //End TO Check


    if (validateSummary == "") //all validation passes:
    {
        if (document.journey.itd_name_origin.value == g_defaultAddressText)
        { document.journey.itd_name_origin.value = "" }

        if (document.journey.itd_name_destination.value == g_defaultAddressText)
        { document.journey.itd_name_destination.value = "" }

        //assignValueToLocType();
        return true;
    }
    else {
        alert("Please make sure you have entered the 'From' and 'To' locations.");
        document.getElementById('validateSummary').style.display = 'block';
        document.getElementById('validateSummary').innerHTML = '<strong>Please review the following:</strong><br />' + validateSummary + '<br />';
        invalid.focus();
        return false;
    }
}



function clearValidateText() {
    //clear all previous validaiton messages:
    document.getElementById('validateFromSuburb').innerHTML = '';
    document.getElementById('validateFromStreet').innerHTML = '';
    document.getElementById('validateToSuburb').innerHTML = '';
    document.getElementById('validateToStreet').innerHTML = '';
    document.getElementById('validateSummary').innerHTML = '';
}


//function assignValueToLocType () {
//Assign a value to FromLocType and ToLocType (hidden fields) - (this has been included in the query string so 
//if a user selects 'amend trip' from the confirmation page the correct type is selected on the New Journey page.
//
//	var LastFromSeType;
//	var LastToSeType;
//	LastFromSeType = document.getElementById('LastFromSeType').value;
//	LastToSeType = document.getElementById('LastToSeType').value;
//
//	if (LastFromSeType == "address") 
//	{
//		document.getElementById('FromLocType').value = "a";
//	}
//	else if (LastFromSeType == "s") 
//	{
//		document.getElementById('FromLocType').value = "s";
//	}
//	else if (LastFromSeType == "l") 
//	{
//		document.getElementById('FromLocType').value = "l";
//	}
//	if (LastToSeType == "address") 
//	{
//		document.getElementById('ToLocType').value = "a";
//	}
//	else if (LastToSeType == "s") 
//	{
//		document.getElementById('ToLocType').value = "s";
//	}
//	else if (LastToSeType == "l") 
//	{
//		document.getElementById('ToLocType').value = "l";
//	}
//}

function GoAdvanced() {
    document.journey.submit();
    return false;
}

function CheckFromLoc(clear)
{
	clearValidateText(); 

	if (document.journey.itd_anyObjFilter_origin[0].checked == true) 
	{ 
	    if (clear != 0)
	    {
	    	document.journey.itd_name_origin.value = g_defaultAddressText;
	    }
	    else 
	    {
	        if (document.journey.itd_name_origin.value == g_defaultAddressText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultAllText;
	        }
	        if (document.journey.itd_name_origin.value == g_defaultStopText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultAllText;
	        }
	        if (document.journey.itd_name_origin.value == g_defaultLandmarkText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultAllText;
	        }	    
	    }
	}

	if (document.journey.itd_anyObjFilter_origin[1].checked == true) 
	{ 
	    if (clear != 0)
	    {
	    	document.journey.itd_name_origin.value = g_defaultAddressText;
	    }
	    else 
	    {
	        if (document.journey.itd_name_origin.value == g_defaultStopText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultAllText;
	        }
	        if (document.journey.itd_name_origin.value == g_defaultStopText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultAddressText;
	        }
	        if (document.journey.itd_name_origin.value == g_defaultLandmarkText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultAddressText;
	        }	    
	    }
	}


	if (document.journey.itd_anyObjFilter_origin[2].checked == true) 
	{
	    if (clear != 0) 
	    {
	    	document.journey.itd_name_origin.value = g_defaultStopText;
	    }
	    else 
	    {
	        if (document.journey.itd_name_origin.value == g_defaultAllText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultStopText;
	        }
	        if (document.journey.itd_name_origin.value == g_defaultAddressText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultStopText;
	        }
	        if (document.journey.itd_name_origin.value == g_defaultLandmarkText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultStopText;
	        }
	    }
	}

	if (document.journey.itd_anyObjFilter_origin[3].checked == true) 
	{
	    if (clear != 0)
	    {
	        document.journey.itd_name_origin.value = g_defaultLandmarkText;
	    }
	    else
	    {
	        if (document.journey.itd_name_origin.value == g_defaultAllText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultLandmarkText;
	        }
	        if (document.journey.itd_name_origin.value == g_defaultAddressText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultLandmarkText;
	        }
	        if (document.journey.itd_name_origin.value == g_defaultStopText)
	        {
	        	document.journey.itd_name_origin.value = g_defaultLandmarkText;
	        }
	    }
	}			
}


function CheckToLoc(clear)
{
	clearValidateText();

	if (document.journey.itd_anyObjFilter_destination[0].checked == true) 
	{ 
	    if (clear != 0)
	    {
	    	document.journey.itd_name_destination.value = g_defaultAddressText;
	    }
	    else 
	    {
	        if (document.journey.itd_name_destination.value == g_defaultAddressText)
	        {
	        	document.journey.itd_name_destination.value = g_defaultAllText;
	        }
	        if (document.journey.itd_name_destination.value == g_defaultStopText)
	        {
	        	document.journey.itd_name_destination.value = g_defaultAllText;
	        }
	        if (document.journey.itd_name_destination.value == g_defaultLandmarkText)
	        {
	        	document.journey.itd_name_destination.value = g_defaultAllText;
	        }	    
	    }
	}

	if (document.journey.itd_anyObjFilter_destination[1].checked == true) 
	{ 
	    if (clear != 0)
	    {
	    	document.journey.itd_name_destination.value = g_defaultAddressText;
	    }
	    else 
	    {
	        if (document.journey.itd_name_destination.value == g_defaultStopText)
	        {
	        	document.journey.itd_name_destination.value = g_defaultAllText;
	        }
	        if (document.journey.itd_name_destination.value == g_defaultStopText)
	        {
	        	document.journey.itd_name_destination.value = g_defaultAddressText;
	        }
	        if (document.journey.itd_name_destination.value == g_defaultLandmarkText)
	        {
	        	document.journey.itd_name_destination.value = g_defaultAddressText;
	        }	    
	    }
	}


	if (document.journey.itd_anyObjFilter_destination[2].checked == true) 
	{
	    if (clear != 0) 
	    {
	    	document.journey.itd_name_destination.value = g_defaultStopText;
	    }
	    else 
	    {
	        if (document.journey.itd_name_destination.value == g_defaultAllText)
	        {
	        	document.journey.itd_name_destination.value = g_defaultStopText;
	        }
	        if (document.journey.itd_name_destination.value == g_defaultAddressText)
	        {
	        	document.journey.itd_name_destination.value = g_defaultStopText;
	        }
	        if (document.journey.itd_name_destination.value == g_defaultLandmarkText)
	        {
	        	document.journey.itd_name_destination.value = g_defaultStopText;
	        }
	    }
	}

	if (document.journey.itd_anyObjFilter_destination[3].checked == true) 
	{
	    if (clear != 0) {
	        document.journey.itd_name_destination.value = g_defaultLandmarkText;
	    }
	    else {
	        if (document.journey.itd_name_destination.value == g_defaultAllText) {
	            document.journey.itd_name_destination.value = g_defaultLandmarkText;
	        }
	        if (document.journey.itd_name_destination.value == g_defaultAddressText) {
	            document.journey.itd_name_destination.value = g_defaultLandmarkText;
	        }
	        if (document.journey.itd_name_destination.value == g_defaultStopText) {
	            document.journey.itd_name_destination.value = g_defaultLandmarkText;
	        }
	    }
	}
}


function ClearJourney()
{
	document.journey.reset();

	document.journey.FromSeType[0].selected = true;
	document.journey.ToSeType[0].selected = true;

	document.journey.FromSingleEntry.value = "Enter street number and name";
	document.journey.ToSingleEntry.value = "Enter suburb or town";

	document.journey.AM_PM.value = "AM";

	var d = new Date();
	var datePart = d.getHours();
	if (datePart > 11) {
		document.journey.AM_PM.value = "PM";
	}
	if (datePart > 12) {
		datePart -= 12;
	}
	if (datePart < 10)
		datePart = "0" + datePart;
	document.journey.HH.value = datePart;

	datePart = parseInt(d.getMinutes() / 5) * 5;
	if (datePart < 10)
		datePart = "0" + datePart;
	document.journey.MM.value = datePart;

	document.journey.Date.options[0].selected = true;

	window.status = "";
	return false;
}

function SwapToFrom()
	{

		var t;

		if (document.journey.FromLocType[0].checked == true)
			t = 0;
		else if (document.journey.FromLocType[1].checked == true)
			t = 1;
		else if (document.journey.FromLocType[2].checked == true)
			t = 2;

		if (document.journey.ToLocType[0].checked == true)
			document.journey.FromLocType[0].checked = true;
		else if (document.journey.ToLocType[1].checked == true)
			document.journey.FromLocType[1].checked = true;
		else if (document.journey.ToLocType[2].checked == true)
			document.journey.FromLocType[2].checked = true;

		if (t == 0)
			document.journey.ToLocType[0].checked = true;
		else if (t == 1)
			document.journey.ToLocType[1].checked = true;
		else if (t == 2)
			document.journey.ToLocType[2].checked = true;

		t = document.journey.FromStreet.value;
		document.journey.FromStreet.value = document.journey.ToStreet.value;
		document.journey.ToStreet.value = t;

		t = document.journey.FromSuburb.value;
		document.journey.FromSuburb.value = document.journey.ToSuburb.value;
		document.journey.ToSuburb.value = t;

		CheckFromLoc(0, 0);
		CheckToLoc(0, 0);

		return false;
	}


function SelectAll(event, id) {
	var ischrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	
	if (ischrome && (event.type == 'focus' || event.type == 'click'))
		return true;
	else if (!ischrome && event.type != 'focus' && event.type != 'click')
		return true;
	else {
	    document.getElementById(id).focus();
	    document.getElementById(id).select();
	}
}

//window.onload = function() {
//    CheckFromLoc(0);
//    CheckToLoc(0);
//}

// Home Page TT Script
function ClearTT(formName) {
    f = eval(formName);
    f.reset();
    f = eval(formName + ".Street");
    if (f != null)
        f.value = "";
    f = eval(formName + ".Suburb");
    if (f != null)
        f.value = "";

    f = eval(formName + ".RouteWC");
    if (f != null)
        f.value = "";

    window.status = "";
    return false;
}

function CheckAction(formName) {
    f = eval(formName + ".TTKeyDir");
    if (f != null) {
        if (f.value != "") {
            f = eval(formName);
            f.action = "publicTimetable.asp";
        }
    }
}

function checkEmptyttKeyDir(theForm) {
    if (document.forms[theForm].TTKeyDir.value == '') {
        document.forms[theForm].action = "timetableBoth.asp";
        return true;
    }
    return true;
}

