

function toggleDatatype_div( lang )
{
  var iconWindow = document.getElementById('datatype_div');
  var databutton = document.getElementById('datatype_button'); 
   
  if( iconWindow.style.display == 'block' )
  { 
    iconWindow.style.display = 'none';
    UpdateCookie( 0, 0 );
    if( lang == 0 )
    { 	
      databutton.src = "Images/roadwayconditions/graphics/showlegend.gif";
    }
    else
    {
      databutton.src = "Images/roadwayconditions/graphics/showlegend_sp.gif";
    }
  }
  else
  { 
    iconWindow.style.display = 'block'; 
    UpdateCookie( 0, 1 )
    if( lang == 0 )
    {
      databutton.src= "Images/roadwayconditions/graphics/hidelegend.gif";
    }
    else
    {
      databutton.src = "Images/roadwayconditions/graphics/hidelegend_sp.gif";
    }
  } 
}	  


function UpdateCookie( index, val )
{
  var pattern = GetCookie( "cbval" );
  var pat1 = pattern.substring( 0, index ); 
  var pat2 = pattern.substring( index + 1, pattern.length );
  var state = ( val == true ) ? 1 : 0;

  SetCookie( "cbval", pat1 + state + pat2, 31 );
}




function SetCookie( name, value, days )
{
  var expire = new Date ();

  expire.setTime( expire.getTime() + (24 * 60 * 60 * 1000) * days );

//  document.cookie = name + "=" + escape(value) + "; expires=" + expire.toGMTString();

  document.cookie = name + "=" + value + "; expires=" + expire.toGMTString();
}



// retrieve info on cookie.
function GetCookie( name )
{
  var startIndex = document.cookie.indexOf( name );

  if( startIndex != -1 )
  {
    var endIndex = document.cookie.indexOf( ";", startIndex );

    if( endIndex == -1 )
    {
      endIndex = document.cookie.length;
    }

    return unescape( document.cookie.substring( startIndex+name.length+1, endIndex ) );
  }
  else
  {
    return null;
  }
}

                   //   01234567890123456789012345
                   
var COOKIE_PATTERN = "11111000001111000000110000";
var PRI_OFFSET = 1;
var SHP_OFFSET = 10;
var SNG_OFFSET = 20;
var LEGEND_OFFSET = 0;

function setLegend( lang )
{
  var cbpattern = GetCookie( "cbval" );

  if( cbpattern == null )
  {
    SetCookie( "cbval", COOKIE_PATTERN, 365 );
    cbpattern = COOKIE_PATTERN;
  }


  var databutton = document.getElementById('datatype_button');
  
  var iconWindow = document.getElementById('datatype_div');
  var legendOpen = ( cbpattern.charAt(LEGEND_OFFSET) == "0" ) ? false : true;
  if( legendOpen )
  {
    iconWindow.style.display == 'block';
    if( lang == 0 )
    {
      databutton.src = "Images/roadwayconditions/graphics/hidelegend.gif";
    }
    else
    {
      databutton.src = "Images/roadwayconditions/graphics/hidelegend_sp.gif";
    }
  }
  else
  {
    iconWindow.style.display = 'none';
    if( lang == 0 )
    {
      databutton.src = "Images/roadwayconditions/graphics/showlegend.gif";
    }
    else
    {
      databutton.src = "Images/roadwayconditions/graphics/showlegend_sp.gif";
    }
  }
}


function  setchecks()
{
  var cbpattern = GetCookie( "cbval" );

  if( cbpattern == null )
  {
    SetCookie( "cbval", COOKIE_PATTERN, 365 );
    cbpattern = COOKIE_PATTERN;
  }

  var i = 0;
  var x = 0;
  
  //priorities
  for( i = 1, x = PRI_OFFSET; i < pris.length; i++, x++ )
  {
    pris[i].checked = ( cbpattern.charAt(x) == "0" ) ? false : true; 
    handlePri( pris[i].checked, i );
  }
   
  //shapes
  for( i = 1, x = SHP_OFFSET; i < shapes.length; i++, x++ )
  {
    shapes[i].checked = ( cbpattern.charAt(x) == "0" ) ? false : true; 
    handleShape( shapes[i].checked, i );
  }
  
  //singles
  for( i = 1, x = SNG_OFFSET; i < cbSingles.length; i++, x++ )
  {
    cbSingles[i].checked = ( cbpattern.charAt(x) == "0" ) ? false : true; 
    handleSingles( cbSingles[i].checked, i );
  }
  
  
  //for( i = 1; i < checkboxes.length; i++ )
  //{
  // checkboxes[i].checked = ( cbpattern.charAt(i) == "0" ) ? false : true; 
  // trap(checkboxes[i].checked, i);
  //}

}




var cbIcons = new Array( "",
"cam",
"rwis"
);


var cbSingles = new Array( "",
document.myform.cbCamera,
document.myform.cbRwis 
);


var priLevel = new Array( "",
"-red.gif",
"-yel.gif",
"-grn.gif",
"-gry.gif");


var priShape = new Array( "",
"tri-",
"dia-",
"capi-",
"cir-");


var shapes = new Array( "",
document.myform.cbTriangle,
document.myform.cbDiamond,
document.myform.cbCapi,
document.myform.cbCircle );


var pris = new Array( "",
document.myform.cbHighPri,
document.myform.cbMedPri,
document.myform.cbLowPri,
document.myform.cbNonePri );



function updateLegend( state )
{
  var i = 0;
  
  for( i = 1; i < shapes.length; i++ )
  {
    shapes[i].checked = state;
    shapes[i].value   = state;
  }
  
  for( i = 1; i < pris.length; i++ )
  {
    pris[i].checked = state;
    pris[i].value   = state;
    
    handlePri( state, i );
  }  
  
  for( i = 1; i < cbSingles.length; i++ )
  {
    cbSingles[i].checked = state;
    cbSingles[i].value   = state;
    
    handleSingles( state, i );
  }  
    
}


function getShapeIdx( name )
{
  var index = 0;
  
  for( var i = 0; i < priShape.length; i++ )
  {
    if( name.indexOf( priShape[i] ) > -1 )
    {
      index = i;
    }
  }
  
  return index;
   
}

function getPriIdx( name )
{
  var index = 0;
  
  for( var i = 0; i < priLevel.length; i++ )
  {
    if( name.indexOf( priLevel[i] ) > -1 )
    {
      index = i;
    }
  }

  return index;
   
}


function handlePri( state, index )
{
  UpdateCookie( index + PRI_OFFSET - 1, state );

  var allpics = document.images;
    
    //var allpics = document.getElementsByTagName('img');
  
  for( var i = 0; i < allpics.length; i++ )
  {       
    var image = allpics[i];
      
    if( (image != null ) && (image.src.indexOf( priLevel[index] ) != -1 ) && (image.alt != 'legend') )
    {
      if( state == true )
      {      
          //check if shape cb is on
        var shapeIdx = getShapeIdx( image.src ); 
             
        if( (shapeIdx != 0) && (shapes[shapeIdx].checked == true ) )
        {
          image.style.visibility = "visible";
        }
      }
      else
      {
        image.style.visibility = "hidden";
      }
    }
  }
}



function handleShape( state, index )
{
  UpdateCookie( index + SHP_OFFSET - 1 , state );

  var allpics = document.images;

  for( var i = 0; i < allpics.length; i++ )
  {
    var image = allpics[i];
    if( (image != null ) && (image.src.indexOf( priShape[index] ) != -1 ) && (image.alt != 'legend') )
    {    
      if( state == true ) //shape
      {
        var priIdx = getPriIdx( image.src );
          
        if( ( priIdx != 0) && ( pris[priIdx].checked == true ) )
        {  
          image.style.visibility = "visible";
        }
      }
      else
      {
        image.style.visibility = "hidden";
      }             
    }
  }
}



function handleSingles( state, index )
{
  UpdateCookie( index + SNG_OFFSET - 1, state );
  var i = 0;
  
  if( state == true )
  {
     var eleList = document.getElementsByName( cbIcons[index] );
     for ( i = 0; i < eleList.length; i++ )
     {
        eleList.item(i).style.visibility = "visible";
     }
  }
  else
  {
     var eleList = document.getElementsByName( cbIcons[index] );
     for ( i = 0; i < eleList.length; i++ )
     {
        eleList.item(i).style.visibility = "hidden";
     }
   }
}



function updateEventForm()
{
  var s = " ";

  var allpics = document.images;

  for( var i = 0; i < allpics.length; i++ )
  {
    var image = allpics[i];
    
    var idx = getPriIdx( image.src );
    if( (idx > 0 ) && (idx < (priLevel.length + 1) ) ) // valid icon
    {
      if( image.style.visibility == "visible" )
      {
        s += getIconName( image.src, s );
      }
    }
  }
  
  if( document.eventListForm != null )
   {
     document.eventListForm.events.value = s;
   }
}


function getIconName( icon, iconList )
{
  var addMe = ""; 
  
  icon = icon.substring( icon.lastIndexOf('/') + 1, icon.length - ".gif".length );
  
  if( iconList.indexOf( icon ) == -1 )
  {
      addMe = icon + ";";
  }
  
  return addMe;
}




var popup_window = null;
var netXOffset   = 0;
var netYOffset   = 0;

function popup( status, url, camera )
{
  var winname = "Popup" + camera;


  if( status != 0 )
  {
    if( popup != null )
    {
      popup.focus();
    }
    else
    {
      if( camera == 1 )
      {
        var popup = open( url, winname, "toolbar=yes, width=475,height=300" );
      }
      else
      if( camera == 0 )
      {
        var popup = open( url, winname, "width=675,height=550" );
      }
      else
      if( ( camera > 2 ) && ( camera < 7 ) ) 
      {
        var popup = open( url, winname, "scrollbars=yes, toolbar=yes, resizable=yes" );
      }
      else
	    if( camera == 11 )
      {
        var popup = open( url, winname, "width=840,height=540" );
      }
	    else
	    if( camera == 12 )
      {
        var popup = open( url, winname, "width=660,height=800" );
      }
      else
	    if( camera == 13 )
      {
        var popup = open( url, winname, "scrollbars=yes,resizable=yes,width=840,height=800" );
      }
      else
      {
        var popup = open( url, winname, "scrollbars=yes, toolbar=yes, width=475,height=300" );
      }

      popup_window = popup;
      popup.focus();
    }
  }
  else
  {
    if( popup_window != null )
    {
      popup_window.close();
    }
  }

}



function findPosX( obj )
{
  var curleft = 0; 

  if( document.getElementById || document.all )
  {
    while( obj.offsetParent )
    {
      curleft += obj.offsetLeft;
      obj      = obj.offsetParent;
    }
  }
  else
  if( document.layers )
  {
    curleft += obj.x;
  }

  return( curleft );
}


function findPosY( obj )
{
  var curtop = 0;


  if( document.getElementById || document.all )
  {
    while( obj.offsetParent )
    {
      curtop += obj.offsetTop;
      obj     = obj.offsetParent;
    }
  }

  else
  if( document.layers )
  {
    curtop += obj.y;
  }

  return( curtop );
}


function getX( o )
{
  var x = 0;

  var o_string = "window.document." + o;
  var o_div    = eval( o_string );
 
  x = findPosX( o_div );

  return( x + netXOffset );
}


function getY( o )
{
  var y = 0;

  var o_string = "window.document." + o;
  var o_div    = eval( o_string );

  y = findPosY( o_div );

  return( y + netYOffset );
}


function setpic( bg, the_div_name, offx, offy )
{
//alert( "bg = " + bg +" div_name = " + the_div_name + " offx = " + offx + " offy = " + offy );

  var the_div, div_string;
  var o;
  var gx, gy;

  gx = getX( bg );
  gy = getY( bg );

  //alert("x = " + gx + " y = " + gy );

  if( navigator.appName == "Netscape" )
  {
    gx = gx - netXOffset;
    gy = gy - netYOffset;
  }

  if( (document.all) || (document.getElementById) )
  {
    o = document.getElementById( the_div_name );
    o.style.left = gx + offx + "px";
    o.style.top  = gy + offy + "px";
  }
  else
  if( document.layers )
  {
    div_string = "window.document." + the_div_name;
    the_div    = eval( div_string );

    the_div.left = gx + offx;
    the_div.top  = gy + offy;
  }
  else
  {
    alert( "unsupported browser" );
  }
}


function getStyleObject( objectId )
{
    // checkW3C DOM, then MSIE 4, then NN 4.

  if( document.getElementById && document.getElementById( objectId ) )
  {
    return( document.getElementById( objectId ).style );
  }
  else
  if( document.all && document.all( objectId ) )
  {
    return( document.all( objectId ).style );
  }
  else
  if( document.layers && document.layers[ objectId ] )
  {
    return( document.layers[ objectId ] );
  }
  else
  {
    return( false );
  }
}


function setLangLinkTopStyle( the_div_name )
{
  var the_div, div_string;
  var o;

  if( (document.all) || (document.getElementById) )
  {
    o = document.getElementById( the_div_name );

    if( navigator.appName == "Microsoft Internet Explorer" )
    {
        o.style.top  = 75+ "px";
    }

    else
    if( navigator.appName == "Netscape" )
    {
        o.style.top  = 62+ "px";
    }

    else
    if( navigator.appName == "Opera" )
    {
        o.style.top  = 75+ "px";
    }

    else
    {
        o.style.top  = 75+ "px";
    }
  }

  else
  if( document.layers )
  {
    div_string = "window.document." + the_div_name;
    the_div    = eval( div_string );
   
    if( navigator.appName == "Microsoft Internet Explorer" )
    {
        the_div.top  = 75 + "px";
    }
    else
    if( navigator.appName == "Netscape" )
    {
        the_div.top  = 62 + "px";
    }
    else
    if( navigator.appName == "Opera" )
    {
        the_div.top  = 75+ "px";
    }
    else
    {
        the_div.top  = 75+ "px";
    }
  }
  else
  {
    alert( "unsupported browser" );
  }

}







