var xmlhttp;

//=============================================================================
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

//===============================================================================
function SelectionValue(SelName)
{
res = -1;
ele=document.getElementById(SelName);
if (ele !== null){
  res = ele.value;
}
return res;

}
function SelectionInner(SelName)
{
res = -1;
ele=document.getElementById(SelName);
if (ele !== null){
  res = ele.innerHTML;
}
return res;

}

function HideSelection(SelName)
{
res = -1;
ele=document.getElementById(SelName);
if (ele !== null){
  ele.style.display = "none";
}
return res;

}

function ShowSelection(SelName)
{
res = -1;
ele=document.getElementById(SelName);
if (ele !== null){
  ele.style.display = "inline";
}
return res;

}
//=====================================================================================
function showAveGraph(str)
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
  

start = SelectionValue ("ys1");
end = SelectionValue ("ye1");
graph = SelectionValue ("IDGraph1");
ave = SelectionValue ("ave1");
chk = document.getElementById("df1");

 
get_plot(graph) ;
  
var url="getAveGraph.php";
url=url+"?graph1="+graph;
if (chk.checked != 1){
if (start != -1){
  url=url+"&ystart1="+start;
  YStart = start;
} else{
  url=url+"&ystart1="+YStart;
}
if (end != -1){
  url=url+"&yend1="+end;
  YEnd = end;
} else{
  url=url+"&yend1="+YEnd;
}
if (ave != -1){
  url=url+"&ave1="+ave;
} else{
  url=url+"&ave1=5";
}
}
url=url+"&sid="+Math.random();
//alert(url);
xmlhttp.onreadystatechange=NOAAyrChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function NOAAyrChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("AveGraph").innerHTML=xmlhttp.responseText;
}
}

//=====================================================================================
function showWeekAveGraph(str)
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
  

start = SelectionValue ("ys1");
end = SelectionValue ("ye1");
graph = SelectionValue ("IDGraph1");
ave = SelectionValue ("ave1");
chk = document.getElementById("df1");

 
get_plot(graph) ;
  
var url="getWeekAveGraph.php";
url=url+"?graph1="+graph;
if (chk.checked != 1){
if (start != -1){
  url=url+"&ystart1="+start;
  YStart = start;
} else{
  url=url+"&ystart1="+YStart;
}
if (end != -1){
  url=url+"&yend1="+end;
  YEnd = end;
} else{
  url=url+"&yend1="+YEnd;
}
if (ave != -1){
  url=url+"&ave1="+ave;
} else{
  url=url+"&ave1=5";
}
}
url=url+"&sid="+Math.random();
//alert(url);
xmlhttp.onreadystatechange=NOAAyrChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function NOAAyrChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("AveGraph").innerHTML=xmlhttp.responseText;
}
}


//=============================================================================

//var YStart, YEnd, Label;

function get_plot(graph){

  
switch (graph.toLowerCase()) {
case 'temphi':
    YStart = 0;
	YEnd = 40;
	name = 'Hi Temp.';
	Label = '°C';
	break;
case 'temp':
    YStart = 0;
	YEnd = 40;
	name = 'Temp.';
	Label = '°C';
	break;
case 'templo':
    YStart = 0;
	YEnd = 40;
	name = 'Lo Temp.';
	Label = '°C';
	break;
case 'windsp':
    YStart = 0;
	YEnd = 40;
	name = 'Wind speed';
	Label = 'Kph';
	break;
case 'winddir':
    YStart = 0;
	YEnd = 360;
	name = 'Wind Dir.';
	Label = '°';
	break;
case 'humid':
    YStart = 0;
	YEnd = 100;
	name = 'Humidity';
	Label = '%';
	break;
case 'barom':
    YStart = 960;
	YEnd = 1040;
	name = 'Pressure';
	Label = 'hpa';
	break;
case 'rain':
    YStart = 0;
	YEnd = 40;
	name = 'Rain';
	Label = 'mm';
	break;
default:
    YStart = 0;
	YEnd = 40;
	name = 'Hi Temp.';
	Label = '°C';
	break;
}

};

function dispinput(obj){
graph = SelectionValue ("IDGraph1");
get_plot(graph);
if (obj.checked != 1){
 // alert("Show");
  document.getElementById("ys1").value = YStart;
  document.getElementById("ye1").value = YEnd;
  document.getElementById("ave1").value = "5";
  ShowSelection("ys1");
  ShowSelection("ye1");
  ShowSelection("ave1");
}else{
  //alert("Hide");
  HideSelection("ys1");
  HideSelection("ye1");
  HideSelection("ave1");
}
  showAveGraph(1);  
};
//=============================================================================

function dispweekinput(obj){
graph = SelectionValue ("IDGraph1");
get_plot(graph);
if (obj.checked != 1){
 // alert("Show");
  document.getElementById("ys1").value = YStart;
  document.getElementById("ye1").value = YEnd;
  document.getElementById("ave1").value = "5";
  ShowSelection("ys1");
  ShowSelection("ye1");
  ShowSelection("ave1");
}else{
  //alert("Hide");
  HideSelection("ys1");
  HideSelection("ye1");
  HideSelection("ave1");
}
  showWeekAveGraph(1);  
};
//=============================================================================

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;

		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function TempDetails(obj){ 
pos = findPos(obj);
clickY = window.event.y - pos[1];
clickYs = (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop); 
clickY += clickYs;
alert(((YSize - clickY) / YRange) +" °C"); 

} 

function TempAlt(obj){ 
pos = findPos(obj);
clickY = window.event.y - pos[1];
clickYs = (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop); 
clickY += clickYs;

clickX = window.event.x - pos[0];
clickXs = (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft); 
clickX += clickXs;

Temp = Math.floor((YSize - clickY) / YRange);
if ((Temp >= MinDisp) && (Temp <= MaxDisp) && 
    (clickX >= Left) && (clickX <= Right)){
  obj.alt = " "+ Temp +" °C ";
  }
 else{
   obj.alt = DefAlt;
 }
}

function HoverAlt(obj){ 
pos = findPos(obj);
clickY = window.event.y - pos[1];
clickYs = (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop); 
clickY += clickYs;

clickX = window.event.x - pos[0];
clickXs = (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft); 
clickX += clickXs;

Temp = Math.round(((YSize - clickY)/ YHeight)*(YEnd-YStart))+ Math.round(YStart);
if ((Temp >= YStart) && (Temp <= YEnd) && 
    (clickX >= Left) && (clickX <= Right)){
  obj.alt = " "+ Temp +" "+Label+" ";
  }
 else{
   obj.alt = DefAlt;
 }
}
//	   <img  id="tempgraph" src="< ?php echo ROOT_PATH ?>wx_dual_month_graph.php" style="margin : 20" border="0" alt="Max/Min/Ave Temp Graph" onclick="TempDetails(this)">

