$(function(){
    $('.JourneyPlanner input[name=sfil]').click(function() {
        $('input#saddr').flushCache();
        $('#saddr').val('Enter Departure ' + $(this).attr('title'));
    })
    
    $('.JourneyPlanner input[name=dfil]').click(function() {
        $('input#daddr').flushCache();
        $('#daddr').val('Enter Destination ' + $(this).attr('title'));
    })
    
    $("#cal_icon").click(function(){
        NewCal('demo8','ddmmyyyy',false,12,'dropdown',true);
    });
    $("#clock_icon").click(function(){
        NewClock('JPtime','ddmmyyyy',true,12,'dropdown',true);
    });
    $("#terms_link").click(function(){
        openTac();
    });
    
    $('.JourneyPlanner input#saddr').blur(function() {
        if(this.value == ''){
            this.value='Enter Departure Street Address';
        }
        this.className='JPInputEmpty';
    });
    
    $('.JourneyPlanner input#saddr').focus(function() {
        this.className='JPInput';
        if((this.value == 'Enter Departure Street Address')||(this.value == 'Enter Departure Suburb')||(this.value == 'Enter Departure Location Name')){
            this.value='';
        };
        this.className='JPInput';
    });
    
    $('.JourneyPlanner input#daddr').blur(function() {
        if(this.value == ''){
            this.value='Enter Destination Street Address';
        }
        this.className='JPInputEmpty';
    });
    
    $('.JourneyPlanner input#daddr').focus(function() {
        this.className='JPInput';
        if((this.value == 'Enter Destination Street Address')||(this.value == 'Enter Destination Suburb')||(this.value == 'Enter Destination Location Name')){
            this.value='';
        }
        this.className='JPInput';
    });
    
    $('.JourneyPlanner input[name=date]').blur(function() {
        if(this.value == ''){
            this.value='Enter Date';
        }
        this.className='JPInputEmpty';
    });
    
    $('.JourneyPlanner input[name=date]').focus(function() {
        this.className='JPInput';
        if(this.value == 'Enter Date'){
            this.value='';
        }
        this.className='JPInput';
    });
    
    $('.JourneyPlanner input#JPtime').blur(function() {
        if(this.value == ''){
            this.value='Enter Time';
        }
        this.className='JPInputEmpty';
    });
    
    $('.JourneyPlanner input#JPtime').focus(function() {
        this.className='JPInput';
        if(this.value == 'Enter Time'){
            this.value='';
        }
        this.className='JPInput';
    });
    
    $('.JourneyPlanner form').submit(function() {
        return onSubmitJP();
    });
    
    $('.JourneyPlanner a#terms_link').keypress(function() {
        openTac();
    });
    
    onLoadJP();
});

function openTac() {
    var uri = 'http://www.google.com/intl/en_ALL/help/terms_maps.html' ;
    var hPos = window.screen.availLeft ;
    var vPos = window.screen.availTop ;
    var hWidth = window.screen.availWidth ;
    var vHeight = window.screen.availHeight ;
    var wFeatures = 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=' + hWidth + ',height=' + vHeight + ',left=' + hPos + ',top=' + vPos ;
    if (confirm("You are now leaving the Adelaide Metro website. By choosing 'OK' you will be redirected to the Google Transit website. Adelaide Metro does not own or operate the Google Transit website and by using the Google Transit website you are agreeing to be bound by Google's terms of use.") == true) {
        window.open(uri, 'termswindow', wFeatures);
    }
}

function onLoadJP() {
    var now = new Date();
    var theHours = now.getHours() ;
    var theMinutes = parseInt(now.getMinutes()/5)*5 + 5 ;
    if (theMinutes == 60) {
        theMinutes = 0 ;
        theHours += 1 ;
    }
    var AmPm = " AM" ;
    if (theHours == 12) {
        AmPm = " PM" ;
    }
    if (theHours > 12) {
        AmPm = " PM" ;
        theHours = theHours - 12 ;
    }
    if($('form[name=google_transit_form]').length > 0){
        document.google_transit_form.date.value = now.getDate() + "/" + (now.getMonth() + 1) + "/" + now.getFullYear() ;
        document.google_transit_form.time.value = (theHours < 10 ? "0":"") + (theHours) + ":" + (theMinutes < 10 ? "0":"") + theMinutes + AmPm ;
    }
}

function onSubmitJP() {
    if (
        ((document.getElementById("saddr").value == 'Enter Departure Street Address') || (document.getElementById("saddr").value == 'Enter Departure Suburb') || (document.getElementById("saddr").value == 'Enter Departure Location Name'))
        &&
        ((document.getElementById("daddr").value == 'Enter Destination Street Address') || (document.getElementById("daddr").value == 'Enter Destination Suburb') || (document.getElementById("daddr").value == 'Enter Destination Location Name'))
        ) {
        alert("Please enter departure and destination details for your journey.");
        return false;
    } else if ((document.getElementById("saddr").value == 'Enter Departure Street Address') || (document.getElementById("saddr").value == 'Enter Departure Suburb') || (document.getElementById("saddr").value == 'Enter Departure Location Name')) {
        alert("Please enter departure details for your journey.");
        return false;
    } else if ((document.getElementById("daddr").value == 'Enter Destination Street Address') || (document.getElementById("daddr").value == 'Enter Destination Suburb') || (document.getElementById("daddr").value == 'Enter Destination Location Name')) {
        alert("Please enter destination details for your journey.");
        return false;
    } else {
        //these lines of code below are used to convert date and time in expected URL format
        var date = document.google_transit_form.date.value ;
        var temp = new Array();
        temp = document.google_transit_form.time.value.split(' ');
        var format = temp[1] ;
        var convert = '';
        if (parseInt(temp[0].substring(0,2)) >= 10) {
            var gettime = temp[0].substring(0,2);
        } else {
            var gettime = temp[0].substring(1,2);
        }
        convert = temp[0];
        if ((temp[1] =="PM") && (parseInt(gettime) != 12)) {
            var convhour = parseInt(gettime) + 12 ;
            convert = convhour + ":" + temp[0].substring(3,5);
        }
        if ((temp[1] =="AM") && (parseInt(gettime) == 12)) {
            convert = "00:" + temp[0].substring(3,5);
        }
        document.google_transit_form.saddr.value = document.google_transit_form.saddr.value ;
        document.google_transit_form.daddr.value = document.google_transit_form.daddr.value ;
        document.google_transit_form.date.value = date;
        document.google_transit_form.time.value = convert;
        return confirm("You are now leaving the Adelaide Metro website. By choosing 'OK' you will be redirected to the Google Transit website. Adelaide Metro does not own or operate the Google Transit website and by using the Google Transit website you are agreeing to be bound by Google's terms of use.");
    }
}

function contact(thing){
    //alert("This is a trial project, if you encounter faults with the information provided, please contact us.");
    document.getElementById("contactus").style.display = 'none';
    if (thing == 'on') {
        document.getElementById("contactus").style.display = 'block';
    }
}

//suburb suggest
$().ready(function() {
    if ($('h1.dbdown').length>0) {
        return;
    }
	// Preliminaries
	function findValueCallback(event, data, formatted) {
		$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	}
	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}
	
	// Start Address
	$("#saddr").autocomplete("/ajax/suggest.php", {
		width: 225,
		selectFirst: false,
		minChars: 2,
		max: 5000,
		cacheLength: 1,
		matchSubset: false,
		extraParams: {
			filtype: function() { return $("input[name='sfil']:checked").val(); }
		}
	});
	$("#saddr").result(function(event, data, formatted) {
		if (data)
			$(this).parent().next().find("input").val(data[1]);
	});
	
	// Destination Address
	$("#daddr").autocomplete("/ajax/suggest.php", {
		width: 225,
		selectFirst: false,
		minChars: 2,
		max: 5000,
		cacheLength: 1,
		matchSubset: false,
		extraParams: {
			filtype: function() { return $("input[name='dfil']:checked").val(); }
		}
	});
	$("#daddr").result(function(event, data, formatted) {
		if (data)
			$(this).parent().next().find("input").val(data[1]);
	});
});

