var j$=jQuery.noConflict();

j$(document).ready(function(){

/*--- show search, since search only works with JS enabled, DotNetNuke's fault ---*/
j$("#search").show();

/* --------------------- Trip Planner ------------------ */
	j$("input.jsAddress").attr({ value:'Enter an address or major destination', style:'color:gray' }).click( function(){
		if(j$(this).attr('value') == "Enter an address or major destination"){
			j$(this).attr({ value:'', style:'color:black' });
		}
	}).blur(function(){
		if(j$(this).attr('value') == undefined){
			j$(this).attr({ value:'Enter an address or major destination', style:'color:gray' }).next('span').hide();
		}
	}).keyup(function(){
		j$(this).next('span').show().click(function(){
			j$(this).prev('input').attr('value','').focus();
			j$(this).hide();
		});
	});
	
	//variables
	var dnow = new Date;
	var nday = dnow.getDate();
	var nmonth = dnow.getMonth();
	var nyear = dnow.getFullYear();
	
	//fix month
	var nmonthi = parseInt(nmonth);
	nmonthi = nmonthi + 1;
	nmonth = nmonthi.toString();
	

//set the current time 
	var curr_hour = dnow.getHours();
	var curr_min = dnow.getMinutes();
	
	if (curr_hour / 12 > 1)
	{
		//time is in afternoon so subtract to get hour
		curr_hour = (curr_hour - 12);
		if (curr_hour == 0)
			curr_hour = 12;
		j$("#HourDropDown").val(curr_hour);
		j$("#Meridiem").val("p");
	}
	else
	{
		//time is in morning 
		j$("#HourDropDown").val(curr_hour);
		j$("#Meridiem").val("a");
	}
	
	var intPos = parseInt(curr_min.toString().substring(0,1));
	var intPos2 = 0;
	if (parseInt(curr_min) > 9)
	{
	   	intPos2 = parseInt(curr_min.toString().substring(1));
		if (intPos2 >= 5)
		{
			j$("#MinuteDropDown").val((intPos+1).toString()+"0");
		}
		else
		{
			j$("#MinuteDropDown").val((intPos).toString()+"5");
		}

	}
	else
	{
		if (intPos >= 5)
		{
			j$("#MinuteDropDown").val("10");
		}
		else
		{
			j$("#MinuteDropDown").val("05");
		}
	}




	//fix leading zeros
	if (nday.toString().length == 1)
    	{nday = "0" + nday;}
	if (nmonth.toString().length == 1)
    	{nmonth = "0" + nmonth;}

	j$("#Date").val(nmonth + "/" + nday + "/" + nyear);
	    

	j$("#SB").attr("href", "").click(function(){
	    j$("#Time").val(j$("#HourDropDown").val()+":"+j$("#MinuteDropDown").val());
	    j$("#Form").attr("method", "POST");
	    j$("#Form").attr("action","http://tp.cata.org/hiwire");
	    document.getElementById("Form").submit();
	   return false;
	});

/* --------------------- Route Look Up------------------ */
	j$("input.jsAddress-2").attr({ value:'Enter a Route Number (Example: 01 or 32)', style:'color:gray' }).click( function(){
		if(j$(this).attr('value') == "Enter a Route Number (Example: 01 or 32)"){
			j$(this).attr({ value:'', style:'color:black' });
		}
	}).blur(function(){
		if(j$(this).attr('value') == undefined){
			j$(this).attr({ value:'Enter a Route Number (Example: 01 or 32)', style:'color:gray' }).next('span').hide();
		}
	}).keyup(function(){
		j$(this).next('span').show().click(function(){
			j$(this).prev('input').attr('value','').focus();
			j$(this).hide();
		});
	});
	
	j$("#STB").attr("href", "").click(function(){
	j$("#Date").val('Today');
	    j$("#LineAbbr").val(j$("#dnn_ddRouteLookup").attr("value"));
	    j$("#Form").attr("method", "POST");
	    j$("#Form").attr("action","http://tp.cata.org/hiwire?.a=iScheduleLookup&");
	    document.getElementById("Form").submit();
	   return false;
	});
	
/* --------------------- Search ------------------ */
	j$("#dnn_xsOpenSearch_txtSearch").attr({ value: 'Enter Keyword' }).click(function(){
		if(j$(this).attr('value') == "Enter Keyword"){
			j$(this).attr({ value: '' });
		}
	}).blur(function(){
		if(j$(this).attr('value') == undefined){
			j$(this).attr({ value:'Enter Keyword' })
		}
	}).keypress(function(e){
		j$(this).next('span').show().click(function(){
			j$(this).prev('input').attr('value','').focus();
			j$(this).hide();
		});
		if(e.keyCode == 13){
			var url = '/SearchResults/tabid/37/portalid/0/Default.aspx?xsq=';
			location.href = url + j$(this).attr('value');
			return false;
		}
	});


/* ------------------- date picker -------------- */

	Date.firstDayOfWeek = 7;
	Date.format = 'mm/dd/yyyy';
	j$(function(){
		j$('.date-pick').datePicker();
	});
	
	
/*-------------------- Face Box ---------------*/

	j$('a[rel*=facebox]').facebox();
	
/*----- Search No Results ----

if(j$("#search-results").html().length   == 0){
 j$("#search-results").html("<h2>No Search Results Found</h2><p>We're sorry, please try searching again.</p>");
}

*/ 

/*-----Rider Alerts----*/
if (typeof(loadRiderAlertsScript) != 'undefined') { loadRiderAlertsScript(); }


});

/*--------------------- Popup -----------------*/
function popuptopicwh(title,topic,w,h,scrollbars)
{
window.open('/popup.aspx?title=' + title + '&topic=' + topic,'Popup37389','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + scrollbars + ',resizable=no,copyhistory=no,width=' + w + ',height=' + h + ',left=0,top=0');
return (true);
}

function formscript(){	
	document.forms[0].action = "http://rp.cata.org/rp/Service.ASP?rp3Action=Login";
	document.forms[0].method = "post";
}

function createFormAndSubmit(){
 
 var submitForm = document.createElement("form");
 document.body.appendChild(submitForm);
 submitForm.method = "POST";


 var usernameInput = document.createElement("input");
 var passwordInput = document.createElement("input");

 usernameInput.setAttribute("name", "Username");
 usernameInput.setAttribute("type", "text");
 usernameInput.setAttribute("value", document.getElementsByName("Username")[0].value);

 passwordInput.setAttribute("name", "Password");
 passwordInput.setAttribute("type", "text");
 passwordInput.setAttribute("value", document.getElementsByName("Password")[0].value);


 submitForm.appendChild(usernameInput);
 submitForm.appendChild(passwordInput);



 submitForm.action = "http://rp.cata.org/rp/Service.ASP?rp3Action=Login";
 submitForm.submit();
}

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
	createFormAndSubmit();
	return false;
   }
else
   return true;
}