// ----------------------------------------------------------------------------
// Javascript functions used by the Commonwealth Bureau of Meteorology 
// Flood Warning Centre Web Site
// Copyright BOM 2002 
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// Get the default parameters (This function is used in the clickable maps)

function get_default()
{
   var cookieValue, pieces, dataset;
   
   cookieValue=ReadCookie("BOM_HYDRO");  
   pieces=breakdown(cookieValue);
   dataset = pieces[1];
   defaultClickable = pieces[2];

   if (dataset == "river")  { defaultGif = riverGif;    defaultData = riverData;}
   if (dataset == "24hr")   { defaultGif = dayGif;      defaultData = dayData;}
   if (dataset == "since")  { defaultGif = sinceGif;    defaultData = sinceData;}
   if (dataset == "last")   { defaultGif = lastGif;     defaultData = lastData;}
   if (dataset == "")       { defaultGif = dayGif;      defaultData = dayData;}

   //alert("Dataset = " + dataset + "\nClickable = " + defaultClickable);

}

// ----------------------------------------------------------------------------
// Get the default parameters (Used in the top index.shtml and  non-clickable maps)

function get_default_index()
{
   var cookieValue, pieces, dataset;

   cookieValue=ReadCookie("BOM_HYDRO");  
   pieces=breakdown(cookieValue);
   dataset = pieces[1];
   defaultClickable = pieces[2];
   
   if (dataset == "river")  { defaultGif = riverGif; defaultDataset = "river";}
   if (dataset == "24hr")   { defaultGif = dayGif;   defaultDataset = "24hr";}
   if (dataset == "since")  { defaultGif = sinceGif; defaultDataset = "since";}
   if (dataset == "last")   { defaultGif = lastGif;  defaultDataset = "last";}
   if (dataset == "") { 
   	defaultGif = dayGif;   defaultDataset = "24hr"; defaultClickable = "N";
        //alert("If you have cookies enabled, this site will use \na cookie to remember the data type selection.")
   }

   //alert("Dataset = " + defaultDataset + "\nClickable = " + defaultClickable);
      
}

// ----------------------------------------------------------------------------
// Switches the image and the data set used for the popup

function switch_image_map(dataset)
{
   var im_name,map_name;
       
   if (dataset == "river")  { im_name = riverGif;    map_name = riverData;}
   if (dataset == "24hr")   { im_name = dayGif;      map_name = dayData;}
   if (dataset == "since")  { im_name = sinceGif;    map_name = sinceData;}
   if (dataset == "last")   { im_name = lastGif;     map_name = lastData;}
   if (dataset == "default") { im_name = defaultGif; map_name = defaultData;}

   var now = new Date();
   Image_update = new Image();
   Image_update.src = im_name + '?' + now.getTime();
   document.mapwindow.src = Image_update.src;
   document.mapwindow.useMap = map_name;

   // if changing the default then write cookie
   // timeout is in milliseconds eg 3hrs x 60min x 60sec x 1000ms = 10800000
   
   if (dataset != "default") {
      value=combine(dataset,defaultClickable);
      expiry=new Date((new Date()).getTime()+10800000);
      WriteCookie("BOM_HYDRO",value, expiry,".bom.gov.au","/hydro/flood/");
      //WriteCookie("BOM_HYDRO",value, expiry);
   }
}

// ----------------------------------------------------------------------------
// Update the cookie used in regional pages (clickabe and non clickable)

function update_cookie(dc)
{
   var cookieValue, pieces, dataset;

   cookieValue=ReadCookie("BOM_HYDRO");  
   pieces=breakdown(cookieValue);
   dataset = pieces[1];

   value=combine(dataset,dc);
   expiry=new Date((new Date()).getTime()+10800000);
   WriteCookie("BOM_HYDRO",value, expiry,".bom.gov.au","/hydro/flood/");
   //WriteCookie("BOM_HYDRO",value, expiry);

}

// ----------------------------------------------------------------------------
// Update the cookie used in index page

function update_cookie_index(ds,dc)
{

   value=combine(ds,dc);
   expiry=new Date((new Date()).getTime()+10800000);
   WriteCookie("BOM_HYDRO",value, expiry,".bom.gov.au","/hydro/flood/");
   //WriteCookie("BOM_HYDRO",value, expiry);

}

// ----------------------------------------------------------------------------
// Display popup of river data

function RvMsg(name,value,fclass,tendency,obs_time)
{
	var msgtext = "Name : " + name + "\nHeight : " + value + " m\nClass : " + fclass + "\nTendency : " + tendency + "\nObs Time : " + obs_time
	confirm(msgtext)
}
   
// ----------------------------------------------------------------------------
// Display popup of rainfall data

function RnMsg(name,value,duration)
{
   	var msgtext = "Name : " + name + "\nRainfall : " + value + " mm\nDuration : " + duration + " hrs"
	confirm(msgtext)
}


// ----------------------------------------------------------------------------
// Switch the map image on the top index.shtml document

function switch_image(dataset)
{
      var im_name,map_name;
      
      if (dataset == "river")  { im_name = riverGif;}
      if (dataset == "24hr")   { im_name = dayGif;}
      if (dataset == "since")  { im_name = sinceGif;}
      if (dataset == "last")   { im_name = lastGif;}
      if (dataset == "tend")   { im_name = tendGif; dataset = "river";}
      if (dataset == "default"){ im_name = defaultGif; dataset = defaultDataset;}

      var now = new Date();
      Image_update = new Image();
      Image_update.src = im_name + '?' + now.getTime();
      document.mapwindow.src = Image_update.src;
      
      // Always write cookie 
      
      value=combine(dataset,defaultClickable);
      expiry=new Date((new Date()).getTime()+10800000);
      //WriteCookie("BOM_HYDRO",value, expiry);
      WriteCookie("BOM_HYDRO",value, expiry,".bom.gov.au","/hydro/flood/");
}
   
// ----------------------------------------------------------------------------
// Zoom into regional map 

function zoom_in(indexfile) 
{

   var newURL;
      
   if (defaultClickable == 'Y') {
	  newURL = indexfile + "_clickable.shtml";
   } else {
	  newURL = indexfile + ".shtml";
   }	
      
        //--If the selected file name is not blank, send it to reader.
   if (newURL != "") {
            //parent.display.location.href=newURL
            location.href=newURL
   }
}   

// ----------------------------------------------------------------------------
// Combine fields into one cookie string

function combine(){

	var delimiter, numArgs, combination, i;
	combination="";
	delimiter="|";
	numArgs=combine.arguments.length;
	if (numArgs>1) {
    		for (i=0; i<numArgs; i++) {
			combination=combination+combine.arguments[i]+delimiter;
    		}
	}
	return combination.substring(0, combination.length-1);
}

// ----------------------------------------------------------------------------
// Break down cookie string into fields

function breakdown(combination){
	var delimiter, start, end, count;
	broken=new Object();
	delimiter="|";
	start=end=0;
	count=1;
	end=combination.indexOf(delimiter,end)
	while ((end) && (end>-1)){
    		broken[count]=combination.substring(start,end);
    		count++;
    		start=end+1;  
    		end=combination.indexOf(delimiter,start) 
	}
	broken[count]=combination.substring(start,combination.length);
	broken[0]=count+1;
	return broken;
}

// ----------------------------------------------------------------------------
// Read cookie - get from browser

function ReadCookie(name){
	var allCookie, CookieVal, length,start,end;
	cookieVal="";
	name=name+"=";  //append equals to avoid false matches.
	allCookie=document.cookie;
	length=allCookie.length;
	if (length>0) {//no cookies - user is probably incinerating cookies.
    		start=allCookie.indexOf(name,0)
    		if (start!=-1) {//if string appeared - otherwise cookie wasn't set.
			start+=name.length;
			end=allCookie.indexOf(";",start);
			if (end==-1) {end=length;}
			cookieVal=unescape(allCookie.substring(start,end));
    		}
	}
	return(cookieVal);
}

// ----------------------------------------------------------------------------
// Write cookie - send to browser

function WriteCookie(name,value,expires,domain,path,secure){
	var CookieVal,CookError;
	CookieVal=CookError="";
	if (name) {
		CookieVal=CookieVal+escape(name)+"=";
    		if (value) {
    			CookieVal=CookieVal+escape(value);
			if (expires) {
				CookieVal=CookieVal+"; expires="+expires.toGMTString();
			}
			if (domain){
				CookieVal=CookieVal+"; domain="+domain;
			}
			if (path) {
				CookieVal=CookieVal+"; path="+path;
			}
			if (secure) {
				CookieVal=CookieVal+"; secure";
			}
    		}
    		else {CookError=CookError+"Value failure";}//need valid value
		}
	else {CookError=CookError+"Name failure";}//need valid name
	if (!CookError) {
    		document.cookie=CookieVal;//sets cookie
    		if (value != ReadCookie(name)) //checks to make sure it worked
			{CookError="Write failure";}
    
	}
	return CookError;
}
// ----------------------------------------------------------------------------
// Reload the current page - used to refresh the #included map data

function reload_page() {
	document.location.reload()
}

// ----------------------------------------------------------------------------
// Offset from mouse position

function get_mouse(e) {
var x=(ns4||ns6)?e.pageX:event.x+document.body.scrollLeft+10;
var y=(ns4||ns6)?e.pageY:event.y+document.body.scrollTop-10;
tip.left=x+XOFFSET;
tip.top=y+YOFFSET;

//var pg = new Pg();

//window.status = ("x = " + x + " y = " + y + " pg hgt = " + document.height + " pg hgt = " + document.documentElement.clientHeight+ 
//" pg hgt = " + document.body.clientHeight+ " pg hgt = " + window.pageYOffset + " p y = " + event.y );
// return true;

//alert("x = " + x + "\ny = " + y + "\n page height = " + document.height);

      
}

// ----------------------------------------------------------------------------
// Hide Display of popup

function HidePopup() {
if(ns4){tip.visibility="hidden";}
else {tip.display="none";}
}

// ----------------------------------------------------------------------------
// Display popup of rainfall data (html)

function PopupRain(stn,st,lat,lon,rn,period,pstr) {
var prd;
prd='' + period + ' ' + pstr;
//if (period != 1) prd=prd + 's';
//var labels = ['Station No','Latitude (Dec)','Longitude (Dec)','Rainfall (mm)','Duration'];
var labels = ['Station No','Rainfall (mm)','Duration'];
//var values = [st,lat,lon,rn,prd];
var values = [st,rn,prd];
var rinfo = '';
for (var i = 0; i < labels.length; i++)
  rinfo=rinfo+"<tr><td class=popup align=left width=100>"+labels[i]+"</td><td class=popup align=left width=140>"+values[i]+"</td></tr>";
var content="<table bordercolorlight=black bordercolor=white border=1 cellpadding=2 cellspacing=0 bgcolor=#DDDDDD><tr><th class=popup colspan=2 bgcolor=#C0C0C0>"+stn+"</th></tr>"+rinfo+"</table>";
document.onmousemove=get_mouse;
if(ns4){tip.document.write(content); tip.document.close(); tip.visibility="visible";}
if(ns6){document.getElementById("tbx").innerHTML=content; tip.display='';}
if(ie){document.all("tbx").innerHTML=content;tip.display='';}
}

// ----------------------------------------------------------------------------
// Display popup of river data (html)

function PopupRiver(stn,st,lat,lon,hgt,cls,tend,otime,min,mod,maj,clickmsg) {
var prc,prd,alt,opr;
//var labels = ['Station No','Latitude (Dec)','Longitude (Dec)','Height (m)','Flood Class','Tendency','Obs Time Local','Minor (m)','Moderate (m)','Major (m)'];
//var values = [st,lat,lon,hgt,cls,tend,otime,min,mod,maj];

if (min == '') {
	var labels = ['Station No','River Height (m)','Time of Obs','Flood Class','Tendency'];
	var values = [st,hgt,otime,cls,tend];
} else {
	var labels = ['Station No','River Height (m)','Time of Obs','Flood Class','Tendency','Minor (m)','Moderate (m)','Major (m)'];
	var values = [st,hgt,otime,cls,tend,min,mod,maj];
}
var rinfo = '';
for (var i = 0; i < labels.length; i++)
  rinfo=rinfo+"<tr><td class=popup align=left width=100>"+labels[i]+"</td><td class=popup align=left width=140>"+values[i]+"</td></tr>";
var content="<table bordercolorlight=black bordercolor=white border=1 cellpadding=2 cellspacing=0 bgcolor=#DDDDDD><tr><th class=popup colspan=2 bgcolor=#C0C0C0>"+stn+"</th></tr>"+rinfo+"<tr><td class=popup colspan=2><font class=orangebold><center>"+clickmsg+"</center></font></td></tr></table>";
document.onmousemove=get_mouse;
if(ns4){tip.document.write(content); tip.document.close(); tip.visibility="visible";}
if(ns6){document.getElementById("tbx").innerHTML=content; tip.display='';}
if(ie){document.all("tbx").innerHTML=content;tip.display='';}
}


