//
//	UNH Weather graph page javascript functions
//		Dr. A. Prusevich
//		January 2004
//		modified- February 2009
//

var graphScript    = '/graph.pl?type=';
var forecastScript = '/forecast.pl?type=';

var month = [31,29,31,30,31,30,31,31,30,31,30,31];
var sdays = [0,31,59,90,120,151,181,212,243,273,304,334];
var day = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15',
      '16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31'];
var Date_start = Start_Year*365 + sdays[Start_Month_1] + Start_Month_Min_Day - 1;
var Date_end = End_Year*365 + sdays[End_Month_1] + End_Month_Max_Day - 1;

function  start_year_onchange() {
  if (document.date.end_year.selectedIndex < document.date.start_year.selectedIndex)
  {
    document.date.end_year.selectedIndex = document.date.start_year.selectedIndex;
  }
  end_month_onchange(); start_month_onchange();
}

function  end_year_onchange() {
  if (document.date.end_year.selectedIndex < document.date.start_year.selectedIndex)
  {
    document.date.start_year.selectedIndex = document.date.end_year.selectedIndex;
  }
  end_month_onchange(); start_month_onchange();
}

function  start_month_onchange() {
  var dmax = 0; var dmin = 0;
  switch (document.date.start_year.options[document.date.start_year.selectedIndex].value)
  {
    case String(Start_Year):
      if (document.date.start_month.selectedIndex <= Start_Month_1) {document.date.start_month.selectedIndex = Start_Month_1; dmin = Start_Month_Min_Day;};
      break;
    case String(End_Year):
      if (document.date.start_month.selectedIndex >= End_Month_1) {document.date.start_month.selectedIndex = End_Month_1; dmax = End_Month_Max_Day;};
      break;
  }
  if (document.date.end_year.selectedIndex*12 + document.date.end_month.selectedIndex < document.date.start_year.selectedIndex*12 + document.date.start_month.selectedIndex)
  {
    document.date.end_month.selectedIndex = document.date.start_month.selectedIndex;
  }
  var mnth = document.date.start_month.selectedIndex;
  var days = (mnth == 1) ? month[mnth]-Math.min(1,document.date.start_year.options[document.date.start_year.selectedIndex].value%4) : month[mnth];
  dmax = (dmax) ? dmax : days;
  var dday = Math.min(dmax,document.date.start_day.options[document.date.start_day.selectedIndex].text);
  dday = Math.max(dmin,dday);
  document.date.start_day.length = days;
  document.date.start_day.options[dday-1].selected  = true;
  for (i=0; i<days; i++){
    document.date.start_day.options[i].value = day[i];
    document.date.start_day.options[i].text  = i+1;
  }
  if (document.date.end_year.selectedIndex*365 + document.date.end_month.selectedIndex*30 + document.date.end_day.selectedIndex < document.date.start_year.selectedIndex*365 + document.date.start_month.selectedIndex*30 + document.date.start_day.selectedIndex)
  {
    document.date.end_day.selectedIndex = document.date.start_day.selectedIndex;
  }
  end_month_onchange();
}

function  end_month_onchange() {
  var dmax = 0; var dmin = 0;
  switch (document.date.end_year.options[document.date.end_year.selectedIndex].value)
  {
    case String(Start_Year):
      if (document.date.end_month.selectedIndex <= Start_Month_1) {document.date.end_month.selectedIndex = Start_Month_1; dmin = Start_Month_Min_Day;};
      break;
    case String(End_Year):
      if (document.date.end_month.selectedIndex >= End_Month_1) {document.date.end_month.selectedIndex = End_Month_1; dmax = End_Month_Max_Day;};
      break;
  }
  if (document.date.end_year.selectedIndex*12 + document.date.end_month.selectedIndex < document.date.start_year.selectedIndex*12 + document.date.start_month.selectedIndex)
  {
    document.date.start_month.selectedIndex = document.date.end_month.selectedIndex;
  }
  var mnth = document.date.end_month.selectedIndex;
  var days = (mnth == 1) ? month[mnth]-Math.min(1,document.date.end_year.options[document.date.end_year.selectedIndex].value%4) : month[mnth];
  dmax = (dmax) ? dmax : days;
  var dday = Math.min(dmax,document.date.end_day.options[document.date.end_day.selectedIndex].text);
  dday = Math.max(dmin,dday);
  document.date.end_day.length = days;
  document.date.end_day.options[dday-1].selected  = true;
  for (i=0; i<days; i++){
    document.date.end_day.options[i].value = day[i];
    document.date.end_day.options[i].text  = i+1;
  }
  if (document.date.end_year.selectedIndex*365 + document.date.end_month.selectedIndex*30 + document.date.end_day.selectedIndex < document.date.start_year.selectedIndex*365 + document.date.start_month.selectedIndex*30 + document.date.start_day.selectedIndex)
  {
    document.date.start_day.selectedIndex = document.date.end_day.selectedIndex;
  }
}

function navigate(Time,Dir) {
  var start_year_index = document.date.start_year.selectedIndex;
  var start_month_index = document.date.start_month.selectedIndex;
  var start_day_index = document.date.start_day.selectedIndex;
  var end_year_index = document.date.end_year.selectedIndex;
  var end_month_index = document.date.end_month.selectedIndex;
  var end_day_index = document.date.end_day.selectedIndex;
  switch (Time) {
    case "year":
        start_year_index += Dir;
	if (start_year_index < 0) start_year_index++;
	if (start_year_index == document.date.start_year.options.length) start_year_index--;
	start_month_index = 0; start_day_index = 0; end_month_index = 11; end_day_index = 30;
	break;
    case "month":
	start_month_index += Dir;
	if (start_month_index < 0) {
	  if (start_year_index) {start_year_index--; start_month_index = 11;}
	  else start_month_index++;}
	if (start_month_index > 11) {
	  if (start_year_index != document.date.start_year.options.length-1) {start_year_index++; start_month_index = 0;}
	  else start_month_index--;}
	end_month_index = start_month_index; start_day_index = 0; end_day_index = month[end_month_index]-1;
	if (end_month_index == 1 && document.date.start_year.options[start_year_index].value%4) end_day_index--;
	break;
    case "day":
	start_day_index += Dir;
	if (start_day_index < 0) {
	  if (!start_month_index){
	    if (!start_year_index) {start_day_index++; break;}
	    start_year_index--; start_month_index = 12;}
	  start_month_index--; start_day_index = month[start_month_index]-1;
	  if (start_month_index == 1 && document.date.start_year.options[start_year_index].value%4) start_day_index--;}
	else if (start_day_index == document.date.start_day.options.length) {
	  if (start_month_index == 11) {
	    if (start_year_index == document.date.start_year.options.length-1) {start_day_index--; break;}
	    start_year_index++; start_month_index = -1;}
	  start_month_index++; start_day_index = 0;}
	end_year_index = start_year_index; end_month_index = start_month_index; end_day_index = start_day_index;
	break;
    case "today":
	start_day_index = End_Month_Max_Day-1; end_day_index = start_day_index;
	start_month_index = End_Month_1; end_month_index = start_month_index;
	start_year_index = End_Year-Start_Year; end_year_index = start_year_index;
	break;
  }
  document.date.start_year.options[start_year_index].selected  = true;
  document.date.end_year.options[start_year_index].selected  = true;
  document.date.start_month.options[start_month_index].selected  = true; start_month_onchange();
  document.date.end_month.options[end_month_index].selected  = true; end_month_onchange();
  start_day_index = Math.min(start_day_index,month[document.date.start_month.selectedIndex]-1);
  var leap = (document.date.end_month.selectedIndex == 1 && document.date.start_year.options[start_year_index].value%4) ? 1 : 0;
  end_day_index = Math.min(end_day_index,month[document.date.end_month.selectedIndex]-leap-1);
  document.date.start_day.options[start_day_index].selected  = true; start_month_onchange();
  document.date.end_day.options[end_day_index].selected  = true; end_month_onchange();

  Get_Graph();
}

function Get_Graph() {
  document.images['graph'].src = "/weather/images/Loading.gif";

  var sidG = Math.random().toString().substr(2,9);
  var junk = (document.date.end_year.selectedIndex == document.date.end_year.options.length-1 &&
	document.date.end_month.selectedIndex == End_Month_1 &&
	document.date.end_day.selectedIndex == End_Month_Max_Day-1) ?
	'&junk='+sidG : '';

  document.images['graph'].src = graphScript +
	document.date.graph_type.options[document.date.graph_type.selectedIndex].value + "&start_date=" +
	document.date.start_year.options[document.date.start_year.selectedIndex].value + "-" +
	document.date.start_month.options[document.date.start_month.selectedIndex].value + "-" +
	document.date.start_day.options[document.date.start_day.selectedIndex].value + "&end_date=" +
	document.date.end_year.options[document.date.end_year.selectedIndex].value + "-" +
	document.date.end_month.options[document.date.end_month.selectedIndex].value + "-" +
	document.date.end_day.options[document.date.end_day.selectedIndex].value + junk;

  return false;
}

function updatePage() {
  calcTime(-5);
  Get_Graph();
  maps_expand(Map_status);
  forecast_expand(Forecast_status);
}

function calcTime(offset) {
  var Year	= End_Year;
  var Month	= End_Month_1;
  var Day	= End_Month_Max_Day;

  var d		= new Date();
  var utc	= d.getTime() + (d.getTimezoneOffset() * 60000);
  var nd	= new Date(utc + (3600000*offset));

  End_Year		= nd.getFullYear();
  End_Month_1		= nd.getMonth();
  End_Month_Max_Day	= nd.getDate();

  if (Year != End_Year) {
    for (i=0; i<(End_Year-Start_Year); i++){
	var Yr = Start_Year+i;
	document.date.start_year.options[i].value = Yr;
	document.date.start_year.options[i].text  = Yr;
	document.date.end_year.options[i].value   = Yr;
	document.date.end_year.options[i].text    = Yr;
    }
  }

  if (Day != End_Month_Max_Day) navigate('today',0);
}

//		Maps		//

var Map_status	= false;
var Map_ID	= new Array(2);
var Sel_ID	= new Array(2);
var Map_Sel	= [0,0];
var Bar		= new Array(5);
var Map		= 5;
var sid		= '0000000';

function maps_expand(swtch) {
  Map_status = swtch;
  var html   = swtch ? "maps_in.htm" : "maps_out.htm";

  DownloadUrl(html, false, function(htmlDoc,responseCode) {
    $("maps").innerHTML = htmlDoc;
    if (responseCode != 200) return false;

    if (swtch) {
      sid = Math.random().toString().substr(2,9);
      Map = 5;
      Map_ID = [document.images['map_1'], document.images['map_2']];
      Sel_ID = [document.forms['map_form'].map_1_sel, document.forms['map_form'].map_2_sel];
      Bar[0] = $("frame_0_1");
      Bar[1] = $("frame_0_2");
      Bar[2] = $("frame_0_3");
      Bar[3] = $("frame_0_4");
      Bar[4] = $("frame_0_5");

      Sel_ID[0].selectedIndex = Map_Sel[0];
      Sel_ID[1].selectedIndex = Map_Sel[1];

      Map_ID[0].src = Sel_ID[0].options[Map_Sel[0]].value + '?' + sid;
      Map_ID[1].src = Sel_ID[1].options[Map_Sel[1]].value + '?' + sid;
    }

  });
}

function change_map(sel) {
  Map_Sel[sel]    = Sel_ID[sel].selectedIndex;
  Map_ID[sel].src = Sel_ID[sel].options[Map_Sel[sel]].value + '?' + sid;

  if (sel==0) {
    Bar[Map-1].style.backgroundColor = "yellow";
    Bar[4].style.backgroundColor     = "red";
    Map = 5;
  }
}

function weather(dir) {
  var Map0 = Map;
  Map += dir;
  if (Map > 5) Map = 1;
  if (Map < 1) Map = 5;

  Bar[Map0-1].style.backgroundColor = "yellow";
  Bar[Map-1].style.backgroundColor  = "red";
  Map_ID[0].src = Map_ID[0].src.replace(Map0+"L.jpg",Map+"L.jpg");
}

//		Forecast		//

var Forecast_status = false;
var unitsT   = 0;		// 0 - Fahrenheit;	1 - Celsius
var tableT   = 0;		// 0 - Graph;		1 - Table

function forecast_expand(swtch) {
  Forecast_status = swtch;
  var html   = swtch ? "forecast_in.htm" : "forecast_out.htm";

  DownloadUrl(html, false, function(htmlDoc,responseCode) {
    $("forecast").innerHTML = htmlDoc;
    if (responseCode != 200) return false;

    if (swtch) {
      var sid = Math.random().toString().substr(2,9);
      setUnits(unitsT);
      setTable(tableT);

      var F_hbh = $("forecast_hbh");
      var F_td  = $("forecast_td");

      F_hbh.src = forecastScript+'hbh&units='+unitsT+'&table='+tableT+'&junk='+sid;
      F_td.src  = forecastScript+'td&units=' +unitsT+'&junk='+sid;
    }
  });
}

function setUnits() {
  var objA = unitsT ? $("degC") : $("degF");
  var objB = unitsT ? $("degF") : $("degC");

  objA.style.color  = "black";
  objA.style.backgroundColor = "AntiqueWhite";
  objA.style.cursor = "auto";
  objA.onclick      = function(){return false};

  objB.style.color  = "blue";
  objB.style.backgroundColor = "white";
  objB.style.cursor = "pointer";

  objB.onclick      = function(){
    unitsT = (unitsT + 1) % 2;

    var F_hbh = $("forecast_hbh");
    var F_td  = $("forecast_td");

    F_hbh.src = F_hbh.contentWindow.srcFrame.replace(/units=\d/,"units="+unitsT);
    F_td.src  = F_td.src.replace( /units=\d/,"units="+unitsT);

    setUnits();
  };
}

function setTable() {
  var objA = tableT ? $("hbhTable") : $("hbhGraph");
  var objB = tableT ? $("hbhGraph") : $("hbhTable");

  objA.style.color  = "black";
  objA.style.backgroundColor = "AntiqueWhite";
  objA.style.cursor = "auto";
  objA.onclick      = function(){return false};

  objB.style.color  = "blue";
  objB.style.backgroundColor = "white";
  objB.style.cursor = "pointer";

  objB.onclick      = function(){
    tableT = (tableT + 1) % 2;

    var F_hbh = $("forecast_hbh");

    F_hbh.src = F_hbh.contentWindow.srcFrame.replace(/table=\d/,"table="+tableT);

    setTable();
  };
}

function SetIFrameSize(frmName,hght) {
  $(frmName).style.height = hght;
}

//////////////////////////////////////////////////////////////////
/*
*
*	XML Block
*
*/
///////////////////////////////////////////////////////////////////////

function DownloadUrl(url,xml,func) {
  var req,responseData,responseCode;

  // branch for native XMLHttpRequest object
  if (window.XMLHttpRequest) {
	req = new XMLHttpRequest();
  // branch for IE/Windows ActiveX version
  } else if (window.ActiveXObject) {
	req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else {
    alert("This browser does not support AJAX -\nAsynchronous JavaScript and XML...");
    return false;
  }

  if (req) {
	req.open("GET", url, true);
	req.onreadystatechange = function() {
	  if (req.readyState == 4) {
	    responseCode = req.status;
		// only if "OK"
	    if (responseCode == 200) {
		responseData = xml ? req.responseXML : req.responseText;
	    }
	    else {
		responseData = "<html><head></head><body>Server connection failed or requested data is not available... <a href=\"javascript:updatePage()\">Retry</a>.</body></html>";
	    }

	    func(responseData,responseCode);
	  }
	};
	req.send(null);
  }
  else {
    alert("This browser does not support AJAX -\nAsynchronous JavaScript and XML...");
    return false;
  }
}

//////////////////////////////////////////////////////////////////
/*
*
*	Helper functions
*
*/
///////////////////////////////////////////////////////////////////////

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

/////////////////////////////////////////////////////////////////
