(function() {
  window.ao = {};

//_ returns (arguments || id if string) as array
  function _() {
   var e;
   var arr = new Array();
   var siz = arguments.length;
    if( siz == 1 ) {
      e = arguments[0];
      return (typeof e == 'string') ? e != "" ? document.getElementById( e ) : null : e;
    } 
    for( var i=0; i<siz; i++ ) {
      e = arguments[i];
      arr.push( typeof e == 'string' ? e != "" ? document.getElementById( e ) : null : e );
    }
    return arr;
  }
  window.ao._ = _;
// addEvent
  window.ao.addEvent = function( e, evType, fn ) {
    e = _( e );
    if( e.attachEvent      ) return e.attachEvent( "on" + evType, function() { return fn( window.event ); } );
    if( e.addEventListener ) { e.addEventListener( evType, fn, false ); return true; }
    e['on' + evType] = fn;
    return true;
  };
// setEvent
  window.ao.setEvent = function( e, etype, fn ) {
    e = _( e );
    if( e.setAttribute ) e.setAttribute( 'on' + etype, fn );
//    else                 e['on' + etype] = fn;
    e['on' + etype] = fn;
  };
// addOnLoad event
  window.ao.addOnLoad = function( func ) {
   var o = window.onload;
    window.onload = o != 'function' ? func : function() { o(); func(); };
  };
//toggle
  window.ao.toggle = function( e ) { 
    var r, s;
    if( (e || window.event) == null ) return false;
    e = e.srcElement ? e.srcElement : e.target;
    while ( e && e.nodeType != 1 ) e = e.parentNode;
    if( e ) {
      r = RegExp( "showing" );
      e.className = e.className.match( r ) 
                  ? e.className.replace( r, "hiding" ) 
                  : e.className.replace( RegExp( "hiding" ), "showing" );
      e = e.id;
      if( e ) {
        s = RegExp( "shown" );
        e = e.replace( RegExp( "(Help|Toggle)$" ), "" );
        if( e ) e = _( e );
        if( e ) e.className = e.className.match( s ) 
                            ? e.className.replace( s, "hidden" )
                            : e.className.replace( RegExp( "hidden" ), "shown" );
      }
      return true;
    }
    return false;
  };
//toggle
  window.ao.toggleselect = function( e ) { 
    if( (e || window.event) == null ) return false;
    e = e.srcElement ? e.srcElement : e.target;
    while ( e && e.nodeType != 1 ) e = e.parentNode;
    return ao.updateselect( e );
  };
//update select options
  window.ao.updateselect =function( e ) {  
   var i, r, s, o;
    if( e ) {
      s = e;
      e = e.id;
      if( e ) {
        r = RegExp( "shown|hidden" );
        e = e.replace( RegExp( "Toggle" ), "" );
        for( i=0; i<s.options.length; i++ ) {
          o = _( e + i );
          if( o ) o.className = o.className.replace( r, s.options[i].selected ? "shown" : "hidden" );
        }
      }
      return true;
    }
    return false;
  };
// Cookies
  window.ao.getinput = function( e ) {
   var a, i, s;
    s = "";
    if( (e = _( e )) ) {
      a = e.getElementsByTagName( 'input' );
      for( i=0; i<a.length; i++ ) if( a[i].type != 'password' && a[i].type != 'hidden' ) {
        s += a[i].name + "#";
        if( a[i].type == 'checkbox' ) s += a[i].checked ? "x1#" : "x0#";
        else                          s += a[i].value + "#";
      }
    }
    return s;
  };
  window.ao.getselect = function( e ) {
   var a, i, k, s;
    s = "";
    if( (e = _( e )) ) {
      a = e.getElementsByTagName( 'select' );
      for( i=0; i<a.length; i++ ) 
        for( k=0; k<a[i].length; k++ ) 
          if( a[i].options[k].selected ) {
            s = s + a[i].name + "#" + k + "#";
          }
    }
    return s;
  };
  window.ao.setinput = function( e, s ) {
   var a, i, k;
    if( (e = _( e )) && s ) {
      if( (a = s.split( "#" )) ) for( i=1; i<a.length; i+=2 ) {
        s = document.getElementsByName( a[i - 1] );
        if( s ) for( k=0; k<s.length; k++ ) {
          if( !!(e = s[k]) ) {
            if(      a[i] == "x1" ) e.checked = true;
            else if( a[i] == "x0" ) e.checked = false;
            else                    try { e.value = a[i]; } catch( e ) { ; };
          }
        }
      }
    }
  };
  window.ao.setselect = function( e, s ) {
   var a, i, k;
    if( (e = _( e )) && s ) {
      a = s.split( "#" );
      for( i=1; i<a.length; i+=2 ) {
        s = document.getElementsByName( a[i - 1] );
        for( k=0; k<s.length; k++ ) if( !!(e = s[k]) && e.options[a[i]] ) e.options[a[i]].selected = true; 
      }
    }
  };
  window.ao.setCookie = function( name, value, days ) {
   var e = "";
    if( days ) {
     var d = new Date();
      d.setTime( d.getTime() + days*24*60*60*1000 );
      e = "; expires=" + d.toGMTString();
    }
    document.cookie = name + "=" + value + e + "; path=/";
  };
  window.ao.getCookie = function( name ) {
   var s = name + "=";
   var arr = document.cookie.split( ';' );
    for( var i=0; i<arr.length; i++ ) {
      for( var c=arr[i]; c.charAt(0)==' '; ) c = c.substring( 1, c.length );
      if( c.indexOf( s ) == 0 ) return c.substring( s.length, c.length );
    }
    return "";
  };
  window.ao.delCookie = function( name ) { ao.setCookie( name, "", -1 ); };
// Post form
  window.ao.post = function( url ) {
   var req = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject( "Microsoft.XMLHTTP" ) : false;
    if( req ) {
     var b = "--rdvBoundaryString";
     var n = b + '\n' + 'Content-Disposition: form-data; name="';
     var f = ao._( 'irdv' );
     var a, s;
      if( f ) {
        a = f.getElementsByTagName( 'input' );
        s = "";
        for( var i=0; i<a.length; i++ ) {
          if( a[i].type == 'checkbox' ) { if( a[i].checked ) s += n + a[i].name + '"\n\non\n'; }
          else if( a[i].type == 'text' ) { if( a[i].value != "" ) s += n + a[i].name + '"\n\n' + a[i].value + '\n'; }
          else if( a[i].type == 'file' ) { if( a[i].value != "" ) s += n + a[i].name + '"; filename="tmp.tmp"\n'
            + 'Content-Type: text/plain\n' + a[i].value;
          }
        }
        a = f.getElementsByTagName( 'select' );
        for( var i=0; i<a.length; i++ ) s += n + a[i].name + '"\n\n' + a[i].value;
        s += b + "--";
        alert( s );
      } else {
        ao._( 'irdv' ).submit();
      }
      return false;
      req.open( 'POST', url, true );
      req.setRequestHeader( 'Connection', 'close' );
      req.setRequestHeader( 'Content-length', bod.length );
      req.send( bod );
    };
    return true;
  };
//Dialogs
var mousex = 0;
var mousey = 0;
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var oriw = 0;
var orih = 0;
var odrag= 0;

var dragobj = null;
var onmupd;
var onname;
 
  function mouseupdate( e ) {
    e = e || window.event;
    mousex = e.pageX || (e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft);
    mousey = e.pageY || (e.clientY + document.body.scrollTop  + document.documentElement.scrollTop );
  } 
  function drag( e ) {
   var r = document.body.getBoundingClientRect();
   var z = (r.right - r.left)/document.body.offsetWidth;
    mouseupdate( e );
    if( dragobj ) {
      dragobj.style.position = "absolute";
      if( odrag& 1 ) dragobj.style.top    = Math.round( oriy + (mousey - graby)/z ).toString(10) + 'px';
      if( odrag& 2 ) dragobj.style.left   = Math.round( orix + (mousex - grabx)/z ).toString(10) + 'px';
      if( odrag& 4 ) dragobj.style.width  = Math.round( oriw + (grabx - mousex)/z ).toString(10) + 'px';
      if( odrag& 8 ) dragobj.style.height = Math.round( orih + (graby - mousey)/z ).toString(10) + 'px';
      if( odrag&16 ) dragobj.style.width  = Math.round( oriw + (mousex - grabx)/z ).toString(10) + 'px';
      if( odrag&32 ) dragobj.style.height = Math.round( orih + (mousey - graby)/z ).toString(10) + 'px';
      if( navigator.appName.indexOf("Microsoft") != -1 ) {
        var z = document.getElementById( "d_aatest" );
        var y = document.createElement("div");
        var x = z.parentNode;
        y.id  = z.id;
        y.className   = z.className;
        y.innerHTML   = z.innerHTML;
        y.onmousedown = z.onmousedown;
        x.removeChild( z );
        x.appendChild( y );
      }
    }
    return false;
  }
  function drop( e ) {
    dragobj = null;
    mouseupdate( e );
    ao.setEvent( document, "mousemove", null );
    ao.setEvent( document, "mouseup",   null );
    ao.setEvent( document, "mousedown", null );
  } 
  window.ao.dialogs_init = function( name ) { onmupd = document.onmousemove; document.onmousemove = mouseupdate; onname = name; }
  window.ao.dialogs_term = function() { document.onmousemove = onmupd; }
  window.ao.grab = function( e ) {
    mouseupdate( e );
    e = e || window.event;
    if( e.preventDefault ) e.preventDefault();
    else window.event.returnValue = false;
    if( e == null ) return false;
    for( e=e.srcElement?e.srcElement:e.target; e&&e.nodeType!=1; ) e = e.parentNode;
    if( e ) {
      ao.setEvent( document, "mousemove", drag );
      ao.setEvent( document, "mouseup",   drop );
      ao.setEvent( document, "mousedown", function(){ return false; } );
      odrag = parseInt( e.className.match( /d_(\d+)/ )[1] );
      for( ; e.parentNode; e=e.parentNode ) if( e.id.match( /dlg/ ) ) break;
      dragobj = e;
      grabx = mousex;
      graby = mousey;
      for( orix=oriy=0; e; e=e.offsetParent ) {
        orix += e.offsetLeft;
        oriy += e.offsetTop;
      }
      var z;
      z = document.body.getBoundingClientRect();
      z = (z.right - z.left)/document.body.offsetWidth;
      orix = Math.round( orix/z );
      oriy = Math.round( oriy/z );
      oriw = Math.round( dragobj.offsetWidth  );
      orih = Math.round( dragobj.offsetHeight );
      return true;
    }
    return false;
  };
//Print Obj
  window.ao.printObj = function( oObj ) {
   var s = "";
    for( var i in oObj ) {
     var ss = [];
      s += " " + i + " : [" + (typeof oObj[i]) + "] : " + oObj[i] + "\n";
     }
     return s;
  }
})();
