// Browser checking functions

// CD Framework 1.2  Module

// Peter Hudson - 2001-11-20

// (c) 2004 TSO



// General features eg. browser object model

var DOM = false;

var IE = false;

var NS = false;



// Specific UA versions

var NS4 = false;

var NS6 = false;

var NS7 = false;

var MOZ = false;

var IE6 = false;

var IE5_5 = false;

var IE5 = false;

var IE4 = false;

var IE4early = false;

var OP4 = false;

var OP5 = false;

var OP6 = false;



function checkBrowser() {

var agent = navigator.userAgent.toLowerCase();

var app = navigator.appName.toLowerCase();

var codeName = navigator.appCodeName.toLowerCase();

var majorVersion = parseInt(navigator.appVersion);

var minorVersion = parseFloat(navigator.appVersion);

// alert("agent: " + agent + "\nappName: " + app + "\nappCodeName: " + codeName + "\nmajor ver: " + majorVersion + "\nminor ver: " + minorVersion);



if (agent.indexOf('opera') != -1) {

	OP6 = (agent.indexOf('opera 6') != -1 || agent.indexOf('opera/6') != -1) ? true:false;

	OP5 = (agent.indexOf('opera 5') != -1 || agent.indexOf('opera/5') != -1) ? true:false;

	OP4 = (agent.indexOf('opera 4') != -1 || agent.indexOf('opera/4') != -1) ? true:false;

	if (OP5 || OP6) {

		DOM = true;

		}

	}

else {

	if (agent.indexOf('netscape') != -1) {

		NS6 = (agent.indexOf('netscape6') != -1) ? true:false;

		NS7 = (agent.indexOf('netscape/7') != -1) ? true:false;

		}

	if (agent.indexOf('gecko') != -1 & !NS6 & !NS7) {

		MOZ = true;

		}

	if (app.indexOf('netscape') != -1) {

		NS4 = (majorVersion == 4) ? true:false;

		}

	if (NS6 || NS7 || MOZ) {

		DOM = true;

		}

	else if (NS4) {

		NS = true;

		}

	if (agent.indexOf('msie') != -1) {

		var brk = agent.indexOf(";") + 7;

		var lng = agent.length;

		var ieVer = agent.substring(brk,lng);

		brk = ieVer.indexOf(";");

		ieVer = ieVer.substring(0,brk);

		var ieMajVer = ieVer.split(".")[0];

		var ieMinVer = ieVer.split(".")[1];

		IE6 = (agent.indexOf('msie 6') != -1) ? true:false;

		IE5_5 = (agent.indexOf('msie 5.5') != -1) ? true:false;

		IE5 = ((document.all) && (window.print) && !(IE5_5 || IE6)) ? true:false;

		IE4 = ((document.all) && (!window.print)) ? true:false;

		if (IE4) {

			var brk = agent.indexOf(";") + 2;

			var lng = agent.length;

			var minVer = agent.substring(brk,lng);

			brk = minVer.indexOf(";");

			minVer = minVer.substring(0,brk);

			if (minVer == "msie 4.0") {

				IE4early = true;

				}

			}

		if (IE6 || ieMajVer >=6) {

			DOM = true;

			}

		else if (document.all) {

			IE = true;

			}

		}

	}

// alert("NS4: " + NS4 + "\nNS6: " + NS6 + "\nNS7: " + NS7 + "\nIE4: " + IE4 + "\nIE4 early: " + IE4early + "\nIE5: " + IE5 + "\nIE5.5: " + IE5_5 + "\nIE6: " + IE6 + "\nOP5: " + OP5 + "\nOP4: " + OP4 + "\nMOZ: " + MOZ);

// alert("DOM: " + DOM + "\nIE: " + IE + "\nNS: " + NS);

}





// Change form on change of radio button focus

function HAChangePage(strButton)

{

    if (strButton == "C")

    {

        window.open("traffic.aspx","_self");

    }

    else

    {

        window.open("forecast.aspx","_self");

}



}

// Set button to be checked when form first loaded

function HASetRadioButton(strPage)

{

	if( IE || DOM)

	{

		var oEbyId = document.getElementById(strPage);

		oEbyId.checked  = true ;

	}

	else

	{

		//alert(strPage);

		if (strPage == 'forecast')

		{

			document.ha_plan_form.rdoTraffic[0].checked = true;

			//alert('plan');

		}else

		{

			document.ha_plan_form.rdoTraffic[1].checked = true;

			//alert('info');

		}

	}

}

// Calculate yesterdays date

function HAgetYesterday(){

	today = new Date();  // get today's date

	yesterdayDate = today.getDate() -1 ;

	today.setDate( yesterdayDate );

	return today;

}

// Sets up the hidden form value for the areaname and submits the form

function gotraffic(strAreaname)

{

	//Work out cutoff dates 

	todaysDate = new Date();



	var lowerCutOffDate = todaysDate.getFullYear() + LZ(todaysDate.getMonth() + 1) + LZ(todaysDate.getDate());

	var upperCutOffDate = '20101231';

	var selectedDate = document.ha_plan_form.ha_date_year.options[document.ha_plan_form.ha_date_year.selectedIndex].value + LZ(document.ha_plan_form.ha_date_month.selectedIndex) + LZ(document.ha_plan_form.ha_date_day.selectedIndex);



	document.ha_plan_form.area.value=strAreaname;

	document.ha_plan_form.date.value=LZ(document.ha_plan_form.ha_date_day.selectedIndex) + "-" + LZ(document.ha_plan_form.ha_date_month.selectedIndex) + "-" + document.ha_plan_form.ha_date_year.options[document.ha_plan_form.ha_date_year.selectedIndex].value;



	if (selectedDate < lowerCutOffDate || selectedDate > upperCutOffDate)

	{

		alert('You cannot enter a date in the past.');

		//alert('The Traffic Forecaster will not accept historical dates.  Please enter a date between today and 31 Dec 2010');

	}

	else

	{

		document.ha_plan_form.submit();	

	}

}



// Set up todays's date for the initial values for the date and month drop downs

function HASetDateDropDowns()

{

	var thisminute = new Date();

	var firstYearOption = 0;

	firstYearOption = document.ha_plan_form.ha_date_year.options[1].value - 1;// because the first option is blank



	if( IE || DOM)

	{

		//document.ha_plan_form.date.value = LZ(thisminute.getDate()) + "-" + LZ(thisminute.getMonth() + 1) + "-" + thisminute.getYear();



		document.ha_plan_form.ha_date_month.selectedIndex= thisminute.getMonth() + 1;

		document.ha_plan_form.ha_date_day.selectedIndex=thisminute.getDate();

		//document.ha_plan_form.ha_date_year.selectedIndex= thisminute.getFullYear();

		document.ha_plan_form.ha_date_year.selectedIndex= thisminute.getFullYear()-firstYearOption;

	}

	else

	{



		//document.ha_plan_form.ha_date_month.value = LZ(thisminute.getDate()) + "-" + LZ(thisminute.getMonth() + 1) + "-" + thisminute.getYear();

		document.ha_plan_form.ha_date_month.options[(thisminute.getMonth() + 1)].selected = true;

		document.ha_plan_form.ha_date_day.options[thisminute.getDate()].selected = true;



		//grab the value of the first option in the list, convert two a number then use to index the year option

		document.ha_plan_form.ha_date_year.options[thisminute.getFullYear()-firstYearOption].selected = true;

	}



}



// Do the necessaries when the form loads

function HAOnLoad()

{



	var now = new Date();

	var yesterday = HAgetYesterday();



	checkBrowser();



	if (NS4 || NS6 || NS7)

	{

		show(false,'docimage');

		//show(false,'layimage1');

	}



	if( IE || DOM)

	{

	objCalendar.addDisabledDates(null,formatDate(yesterday,"yyyy-MM-dd"));

	objCalendar.addDisabledDates("Jan 1, 2011",null);

	objCalendar.setReturnFunction("setMultipleValues3");



	// Specify how many pixels to the right of the anchor the popup will appear

	objCalendar.offsetX = 0;



	// Specify how many pixels below the anchor the popup will appear

	objCalendar.offsetY = 0;

	}



	HASetRadioButton('forecast'); 

	HASetDateDropDowns();



}

// Set up the form elements to be populated when a date has been selected

function setMultipleValues3(y,m,d) {

     document.ha_plan_form.ha_date_month.selectedIndex=LZ(m);

     document.ha_plan_form.ha_date_day.selectedIndex=LZ(d);

     document.ha_plan_form.ha_date_year.value=y;

     document.ha_plan_form.date.value=document.ha_plan_form.ha_date_day.selectedIndex + "-" + LZ(document.ha_plan_form.ha_date_month.selectedIndex) + "-" + document.ha_plan_form.ha_date_year.value;

}



function show(sw,obj) {

	// show/hide the divisions

	//document.layers[obj].visibility = 'hidden'

	if (sw && (IE4 || IE5) ) document.all[obj].style.visibility = 'visible';

	if (!sw && (IE4 || IE5) ) document.all[obj].style.visibility = 'hidden';

	if (sw && (NS4)) document.layers[obj].visibility = 'visible';

	if (!sw && (NS4)) document.layers[obj].visibility = 'hidden';

	if (sw && (NS6)) document.images['popupbutton'].style.visibility = 'visible';

	if (!sw && (NS6)) document.images['popupbutton'].style.visibility = 'hidden';

	if (!sw && (NS7)) document.getElementById('docimage').style.position = 'static';

}



/* The following functions used for the new Traffic Information Page 22-10-08 - Juan Londono */

function displayOverlay(region)
{
	window.document.getElementById(region).style.border = "none";	
	window.document.getElementById(region).style.backgroundImage="url('../images/overlay_"+region+".jpg')";	
}

function hideOverlay(region)
{
	window.document.getElementById(region).style.border = "none";	
	window.document.getElementById(region).style.backgroundImage = "none";	
}

