﻿/** All the stuff with nowhere better to be
*/

function set_input_label_css_class( input_id, className )
{
  var lab = document.getElementsByTagName( "label" );
  if ( lab ) for ( var i = 0; i < lab.length; ++i )
  {
    if ( lab[i].htmlFor == input_id )
    {
      if ( lab[i].className ) lab[i].className += " " + className;
      else                    lab[i].className = className;
    }
  }
}


function notify_form_error( name )
{  
  libevent.add( "onload",
      function ()
      {
        for ( var i = 0; i < document.forms.length; ++i )
        {
          var f = document.forms[ i ];
          if ( f[ name ] && f[ name ].focus ) 
          {
            var c = f[ name ];
            c && c.style && c.style.display != "none" && c.focus();
            break;
          }
        }
        set_input_label_css_class( name, "err" );
      }        
    );
}


var message_popup_style = {
    
    /** all bools are anything but "no" */
    
    auto_title : "yes", 
    title_class : "popup-title",
    
    /** set to false to use style sheet supplied values by title_class
        Will also set the cursor to 'move' and the font weight to 'bolder'.
    */
    title_style : "no",
    title_background : "#00007F",    
    title_foreground : "#FFF",

    closebox_src : "images/close.gif",
    closebox_title : "Close this popup",
    closebox_alt : "X",
    closebox_width : "16px",
    closebox_height : "16px",
    closebox_offset : "2px",
    
    /** should a close box be added to a found popup title bar 
    */
    auto_closebox : "yes",
    
    /* objects found by className 
    */
    'popup-title-bar' : 'popup-title-bar',
    "popup-drag-handle" : "popup-drag-handle",
    
    /** set to "no" to use style sheet supplied values, note this is 
        here as the default background will be transparent.
    */
    popup_style : "yes",
    
    minimum_width : 200
  };


function onload_poups_by_tag( notlist )
{
  if ( ! document.getElementsByTagName ) return;
  
  for ( var tag in { div : 0, span : 0 } )
  {
    var dl = document.getElementsByTagName( tag );
    
    if ( ! dl ) continue;
    
    for ( var i = 0; i < dl.length; ++i )
    {
      var node = dl[ i ];
      if ( ( /^div-(alert|msg|err)$/ ).test( node.id ) ) continue;
      if ( ( /\b(alert|msg|err)\b/ ).test( node.className ) )
      {
        make_div_popup( node, message_popup_style );
      }
    }
  }
}

function onload_popups_by_id()
{
  if ( ! document.getElementById ) return [];
  var r = []
  
  for ( var id in { alert: 0, msg : 0, err : 0 } )
  {
    var node = document.getElementById( "div-" + id );
    
    if ( ! node ) continue;
    make_div_popup( node, message_popup_style );
    r.push( node );
  }
}



function add_onload( f )
{
  libevent.add( "onload", f );
}



function close_popup_div()
{
  var div = document.getElementById("popup-msg-div");
  if ( div ) div.style.display = "none";
}

function create_popup_div( title, w, h )
{
  var div = document.getElementById("popup-msg-div");
  
  w = w || 400;
  h = h || 200;
  title = title || "Help";
  
  if ( !div )
  {
    var body = document.getElementsByTagName("BODY")[0];
    div = document.createElement("DIV")
    div.id = "popup-msg-div";
    
    var title_bar = document.createElement("div");
    
    title_bar.className = "popup-title-bar";
    var span = document.createElement("span");
    span.id = "popup-msg-div-title";
    
    title_bar.appendChild( span );
    div.appendChild( title_bar );
    
    var content = document.createElement("DIV")
    content.id = "popup-msg-div-content";
    content.className = "popup-content";
    div.appendChild( content );

    //div.title = "XXX" + title;
    body.appendChild( div );
    
    make_div_popup( div, message_popup_style );
    
    var title_bar = find_tag( div, "div", "className", "popup-title" );
    if ( title_bar.length ) title_bar[0].id = "popup-msg-div-title";
  }

  var outer_x = 0, outer_y = 18, inner_x = 0, inner_y = 4;
  if ( ie_version() < 7.0 )
  {
    outer_x = 4, outer_y = 4, inner_x = 4, inner_y = 4;
  }
  
  var title_bar = document.getElementById( "popup-msg-div-title" );
  text_content( title_bar, title );
  
  div.style.width = (w + outer_x) + "px";
  div.style.height = (h + outer_y) + "px";
  
  var content = document.getElementById( "popup-msg-div-content" );
  content.style.width = (w + inner_x) + "px";
  
  return { div : div, content : content, w : w, h : h };
}




function popup_confirm( message, title, action, w, h )
{
  
  message = (
      "<div  align='center'>" +
        "<p>" + message + "</p>" +
        "<div>" +
          "<a href=\"" + action + "\">"
            + "<img alt='Yes' src='images/auto/Yes.gif?c=fake-gb' border='0'>"
          + "</a>" +
          "&nbsp" +
          "<img onclick='close_popup_div();' alt='No' src='images/auto/No.gif?c=fake-gb' border='0'>" +
        "</div>" +
      "</div>"
    ) ;
  with ( create_popup_div( title, w, h ) )
  {
    content.innerHTML = message;
    div.style.display = 'block';
    v_scroll_into_view( div );
  }
}


function v_scroll_into_view( div )
{
  var vp = ( document.documentElement || document.body );
  if ( vp.scrollTop > div.offsetTop ) 
  {
    div.style.top = vp.scrollTop + "px";
  }
  else if ( ( vp.scrollTop + vp.clientHeight  ) < ( div.offsetTop + div.offsetHeight ) ) 
  {
    div.style.top = ( vp.scrollTop + vp.clientHeight - div.offsetHeight ) + "px";
  }
}

function scroll_into_view( div )
{
  var vp = ( document.documentElement || document.body );
  if ( vp.scrollTop > div.offsetTop ) 
  {
    div.style.top = ( vp.scrollTop + 10 ) +"px";
  }
  else if ( ( vp.scrollTop + vp.clientHeight  ) < ( div.offsetTop + div.offsetHeight ) ) 
  {
    div.style.top = ( vp.scrollTop + vp.clientHeight - div.offsetHeight - 10 ) + "px";
  }

  //log( vp.scrollLeft, vp.clientWidth, div.scrollLeft, div.offsetWidth );

  if ( vp.scrollLeft > div.offsetLeft ) 
  {
    div.style.left = ( vp.scrollLeft + 10 ) + "px";
  }
  else if ( ( vp.scrollLeft + vp.clientWidth  ) < ( div.offsetLeft + div.offsetWidth ) ) 
  {
    div.style.left = ( vp.scrollLeft + vp.clientWidth - div.offsetWidth - 10 ) + "px";
  }
}


function ie_version()
{
  if ( window.navigator.appVersion.match( /.*MSIE\s(\d+\.\d+).*/ ) )
  {
    return +window.navigator.appVersion.replace( /.*MSIE\s(\d+\.\d+).*/, "$1" );
  }
}


function collapse_popup_to_frame( frame )
{
  var doc = frame.contentDocument || frame.contentWindow.document;
  doc = doc.documentElement || doc;
  
  var outer_x = 0, outer_y = 4, inner_x = 0, inner_y = 4;
  if ( ie_version() < 7.0 )
  {
    outer_x = 4, outer_y = 4, inner_x = 4, inner_y = 4;
  }
  
  var dy = doc.scrollHeight - frame.offsetHeight;
  var dx = doc.scrollWidth - frame.offsetWidth;
  
  var content = frame.parentNode;
  var div = content.parentNode;
  var vp = ( document.documentElement || document.body );
  
  if ( dy )
  {
    var newy = div.offsetHeight + dy;
    if ( ( newy + 50 ) > vp.clientHeight )
    {
      dy = vp.clientHeight - 50 - div.clientHeight;
    }
    
    if ( div.offsetHeight + dy > 100 )
    {
      div.style.height = ( div.clientHeight + dy + outer_y ) + "px";
      frame.style.height = ( frame.clientHeight + dy + inner_y ) + "px";
    }
  }

  if ( dx )
  {
    var newx = div.offsetWidth + dx;
    if ( vp.clientWidth && ( newx << 1 ) > vp.clientWidth )
    {
      dx = ( vp.clientWidth >> 1 ) - div.clientWidth;
    }
    
    if ( div.offsetWidth + dx > 100 )
    {
      div.style.width = ( div.clientWidth + dx + outer_x ) + "px";
      frame.style.width = ( frame.clientWidth + dx + inner_x ) + "px";
    }
  }
  
  scroll_into_view( div );
}


function popup_page( url, title, w, h )
{
  with ( create_popup_div( title, w, h ) )
  {
    content.innerHTML = (
        "<iframe" +
          " onload='parent.collapse_popup_to_frame( this );'" +
          " _name='popup-iframe'" +
          " id='popup-iframe'" +
          " class=\"popup-frame\"" +
          " width = '" +  w + "'" +
          " height='" + (h) + "'" +
          " src='" + url + "'" +
          "" +
        "></iframe>"
      );
    div.style.display = 'block';
  }
}

function popup_msg( msg, title, w, h )
{
  with ( create_popup_div( title, w, h ) )
  {
    content.innerHTML = msg;
    div.style.display = 'block';
    scroll_into_view( div );
  }
}


function page_alert( msg, html, title )
{
  html = html ? html : msg;
  html = (
      "<div style='height:150px; text-align:center; margin-top:10px;'>" + ( html ) + "</div>"
      + "<div style='text-align:center;'><img class=\"close-button\" src=\"images/close-button.gif\""
           + "style='margin-left: auto; margin-right: auto;'"
           + "alt=\"Close\" title=''"
           + "onclick='close_popup_div();'></div>"
    );
  popup_msg( html, title || "Page Alert" );
}




function limit_length( that, len )
{
  var span = document.getElementById( that.id + "-length" );
  if ( that.value.length > len ) 
  {
    that.value = that.value.substr(0, len);
  }  
  if ( span ) text_content( span, len - that.value.length );
}




function get_querystring( name )
{
  var qs = document.location.search.slice( 1 ).split( "&" );
  for ( var i = 0; i < qs.length; ++i )
  {
    var nvp = qs[i].split( '=' );
    var n = nvp[0];
    if ( n == name ) return nvp.slice(1).join( "=" );
  }
  return undefined;
}


function trim_field( field )
{
  return field.value = field.value.replace( /^\s+|\s+$/g, '' );
}


function onload_js_styling()
{
  if ( true ) 
  {
    var settings = {
      tl: { radius: 10 },
      tr: { radius: 10 },
      bl: { radius: 10 },
      br: { radius: 10 },
      antiAlias: true,
      autoPad: true,
      validTags: ["div"]
    }
    var myBoxObject = new curvyCorners( settings, "cornered" );
    myBoxObject.applyCornersToAll();
  }
  else //if( false )
  {
    var all = document.getElementsByTagName( "div" );
    var dat = 'top left top right bottom right bottom left'.split( ' ' );
    for ( var n = 0; n < all.length; ++n )
    {
      var div = all[ n ];
      if ( ! ( '' + div.className ).match( /(\s|^)cornered(\s|$)/ ) ) continue;

      //div.style.position = "relative";

      for ( var p = 0; p < dat.length ; p += 2 )
      {
        var top = dat[ p ], left = dat[ p + 1 ];
        var tl = new Image();
        tl.src =  "images/" + top + '-' + left + ".gif";

        var style = tl.style;
        style.position = "absolute";

        style[ top ] = "0px";
        style[ left ] = "0px";

        div.appendChild( tl );
      }
      
    }
  }  
}


/** assist in updating a form when a link is clicked ideas-edit.asp uses this
*/
function redirect_o_mat( a )
{
  var f = document.forms[ 0 ];
  var href = a.href || a;
  
  if ( f && a )
  {
    var h = document.createElement( "input" );
    
    h.setAttribute( "type", "hidden" );
    h.setAttribute( "name", "redirect-o-mat" );
    h.setAttribute( "value", a );
    h.setAttribute( "id", "redirect-o-mat" );

    f.appendChild( h );
    f.submit();
    return false;
  }
}


/** event handler that stops the enter key from submitting its form

  stops CR and LF from all non textarea's
*/
function cancel_enter_key_submit( e ) 
{
  if ( e.keyCode != 13 && e.keyCode != 10 ) return true;
  var on =  ( e.target || e.srcElement );    
  if ( on && on.tagName.toUpperCase() == "TEXTAREA" ) return true;
  libevent.stop_event( e );
  return false;
}
