(function(){
  window.rdv = {};
// Validator  
  function rdvsubmit() {
   var f = ao._( 'irdv' );
    if( f ) {
      if( (s = ao.getinput ( f )) ) ao.setCookie( "irdvinput",  s, 366 );
      if( (s = ao.getselect( f )) ) ao.setCookie( "irdvselect", s, 366 );
    }
    return true;
  };
//Init
  window.rdv.initcookie = function() { 
   var f = ao._( 'irdv' );
    if( f ) {
      ao.setinput ( f, ao.getCookie( "irdvinput"  ) );
      ao.setselect( f, ao.getCookie( "irdvselect" ) );
    }
    ao.setEvent( 'irdv', 'submit', rdvsubmit ); 
    ao.addEvent( 'irdv', 'reset',  rdvreset ); 
  };
//Init
  window.rdv.inittoggle = function() { 
   var a, i;
   var r = RegExp( "(Help|Toggle)$" );
    a = document.getElementsByTagName( 'a' );
    if( a ) for( i=0; i<a.length; i++ ) {
      if( a[i].id.match( r ) ) ao.addEvent( a[i], 'click', function( e ) { 
          e = e || window.event;
          if (e.preventDefault) e.preventDefault();
          else window.event.returnValue = false;
          ao.toggle( e ); 
      } );
    }
  };
//Init Select
  window.rdv.initselect = function() { 
   var a, i, r;
    r = RegExp( "Toggle" );
    a = document.getElementsByTagName( 'select' );
    if( a ) for( i=0; i<a.length; i++ ) {
      if( a[i].id.match( r ) ) {
        ao.updateselect( a[i] );
        ao.addEvent( a[i], 'change', function( e ) { ao.toggleselect( e ); } );
      }
    }
  };
//update select
  function rdvreset() { 
   var a, i, r;
    a = document.getElementsByTagName( 'select' );
    if( a ) for( i=0; i<a.length; i++ ) {
      if( a[i].id.match( /Toggle/ ) ) {
        for( r=0; r<a[i].options.length; r++ ) a[i].options[r].selected = a[i].options[r].defaultSelected;
        ao.updateselect( a[i] );
      }
    }
    a = document.getElementsByTagName( 'input' );
    if( a ) for( i=0; i<a.length; i++ ) {
        if( a[i].type == 'checkbox' ) a[i].checked = a[i].defaultChecked;
        else if( a[i].defaultvalue  ) a[i].value   = a[i].defaultvalue;
    }
    if( (a = ao._( "d_aadlg"  )) ) aa_saved = ao.getinput( a );
    if( (a = ao._( "aasmry"   )) ) a.innerHTML = updatesummary( ao._( "d_aadlg" ), "amino acids" );
    if( (a = ao._( "d_ptmdlg" )) ) ptm_saved = ao.getinput( a );
    if( (a = ao._( "ptmsmry"  )) ) a.innerHTML = updatesummary( ao._( "d_ptmdlg" ), "PTMs" );
    return true;
  };
// addOnLoad event
  window.rdv.addOnLoad = function( func ) {
   var o = window.onload;
    window.onload = o != 'function' ? func : function() { o(); func(); };
  }; 
//Dialog
var h_init = 0;
var aa_saved = "";
var ptm_saved = "";
    function startdialog( name ) {
     var e, c;
      e = document.getElementById( "d_" + name + "dlg" );
      if( !e ) return true;
      if( h_init == 0 && name == "ptm" ) {
        h_init++;
        c = document.body.getBoundingClientRect();
        e.style.height = (c.bottom - c.top - 100).toString(10) + 'px';
      }
      e.style.display = "block";
      if( (c = document.getElementById( name + "tbl" )) ) c.style.height = (e.offsetHeight - 85).toString(10) + 'px';
      return false;
    };
    function filedelete( name, what ) {
     var e, k, req;
      if( !!(e = document.getElementById( name + "text" )) ) e.value="";
      if( !!(e = document.getElementById( name + "Key"  )) ) e.value="";
      if( !!(e = document.getElementById( name + "kf"   )) ) e.value="";
      if( !!(e = document.getElementById( name + "btn"  )) ) e.innerHTML=" Upload ";
      if( !!(e = document.getElementById( name + "smry" )) ){e.innerHTML="Nothing uploaded..."; e.title=""; }
      if( !!(e = document.getElementById( name + "cf"   )) ) e.innerHTML=what;
      if( !!(e = document.getElementById( name + "clr"  )) ) e.style.display = "none";
      k = ao.getCookie( name + "key" );
      ao.delCookie( name + "key" );
      ao.delCookie( name + "file" );
      if( !!k ) return false;
      if(      window.XMLHttpRequest ) req = new XMLHttpRequest();
      else if( window.ActiveXObject  ) req = new ActiveXObject("Microsoft.XMLHTTP");
      else                             return false;
      req.open( "GET", "../Efile.cgi?ex=2&Key=" + k );
	  req.send( null );
      return false;
    };
    function updatesummary( ed, what ) {
      var a, c, i, s;
       if( !ed || !what ) return "Error in UpdateSammary";
       a = ed.getElementsByTagName( "INPUT" );
       for( s="Selected",i=c=0; i<a.length; i++ ) if( a[i].checked ) s += (c++ == 0 ? ": " : ", ") + a[i].name;
       if( c < 1 || c > 10 ) {
         if(      c == 0 ) s = "No";
         else if( c == i ) s = "All";
         else if( c >  10 ) s = c;
         s = " " + s + " " + what + " are selected";
       }
       return s + ".";
    }
    function rdvclick( e ) {
     var a, s;
      e = e || window.event;
      if( e == null ) return false;
      if( e.preventDefault ) e.preventDefault();
      else window.event.returnValue = false;
      for( e=e.srcElement?e.srcElement:e.target; e&&e.nodeType!=1; ) e = e.parentNode;
      if( e.id == 'aax' || e.id == 'aaClose' ) { 
        if( !!(a = ao._( "d_aadlg"  )) ) { 
          a.style.display = "none";
          ao.setinput( a, aa_saved ); 
        }
      } else if( e.id == 'aaAccept'  ) { 
        if( !!(a = ao._( "d_aadlg"  )) ) aa_saved = ao.getinput( a );
        if( !!(a = ao._( "aasmry"   )) ) a.innerHTML = updatesummary( ao._( "d_aadlg" ), "amino acids" );
      } else if( e.id == 'ptmx' || e.id == 'ptmClose' ) { 
        if( !!(a = ao._( "d_ptmdlg" )) ) {
          a.style.display = "none";
          ao.setinput( a, ptm_saved );
        }
      } else if( e.id == 'ptmAccept' ) {
        if( !!(a = ao._( "ptmsmry"  )) ) a.innerHTML = updatesummary( ao._( "d_ptmdlg" ), "PTMs" );
        if( !!(a = ao._( "d_ptmdlg" )) ) aa_saved = ao.getinput( a );
      } else if( e.id == 'isfx' || e.id == 'isfClose' ) { 
        if( !!(a = ao._( "d_isfdlg" )) ) a.style.display = "none";
      } else if( e.id == 'isfUpload' ) {
        if( !!(e = document.getElementById( "isfform" )) ) e.submit();
        if( !!(a = ao._( "d_isfdlg" )) ) a.style.display = "none";
      } else if( e.id == 'isfClear' ) {
        filedelete( "isf", "no spectrum" );
      } else if( e.id == 'isqx' || e.id == 'isqClose' ) { 
        if( !!(a = ao._( "d_isqdlg" )) ) a.style.display = "none";
      } else if( e.id == 'isqUpload' ) {
        if( !!(e = document.getElementById( "isqform" )) ) e.submit();
        if( !!(a = ao._( "d_isqdlg" )) ) a.style.display = "none";
      } else if( e.id == 'isqClear' ) {
        filedelete( "isq", "no sequence" );
      }
      return true;
    };
    window.rdv.initdialog = function( name, what ) { 
     var et, eb, ed;
     var c, i, s;
     var a = new Array();
       if( !(ed = document.getElementById( "d_" + name + "dlg" )) ) return;
       if( !(et = document.getElementById( name + "title"      )) ) return;
       if( !(eb = document.getElementById( name + "body"       )) ) return;
       a = ed.getElementsByTagName( "*" );
       for( i=0; i<a.length; i++ ) {
         if( (c = a[i].className.match( /d_(\d+)/ )) ) ao.setEvent( a[i], "mousedown", ao.grab   );
         else if( a[i].className.match( /button/  )  ) ao.setEvent( a[i], "click",     rdvclick ); 
       }
       s = '<div class="b_b"> </div><div class="b_m"><span id="' + name + 'btn"> Change </span></div><div class="b_e"> </div>'
         + '<div class="b_s"><span id="'+ name + 'smry">' + updatesummary( eb, what ) + '</span></div>';
       if( (a = document.getElementById( "d_" + name + "test" )) ) a.innerHTML = et.innerHTML;
       if( (a = document.getElementById( "d_" + name + "body" )) ) { 
         c              = eb.innerHTML;
         eb.innerHTML   = s;
         a.innerHTML    = c;
       }
       if( (c = document.getElementById( name + "btn" )) ) ao.setEvent( c, 'click', function() { return startdialog( name ); } );
    };
//Upload
    function startupload( name ) {
     var e;
      if( !(e = document.getElementById( "d_" + name + "dlg" )) ) return true;
      e.style.display = "block";
      return false;
    };
    window.rdv.loaded = function( frame ) {
     var d, e, f, k, n;
      if( !(f = ao._( frame )) ) return;
      d = f.contentWindow ? f.contentWindow.document.body : f.contentDocument ? f.contentDocument.document.body : f;
	  if( !d || !d.innerHTML || !f.id.match( /(\S+)frame$/i ) ) return;
      n = RegExp.$1;
      k = d.innerHTML.match( /Key:\s*(\S+)/ ) ? RegExp.$1 : "";
      f = "";
      if( !!(e = document.getElementById( n + "file" )) ) f = e.value;
      if( f == "" ) f = "copy/pasted text";
      if( k == "" ) f = "no file";
      if( !!(e = document.getElementById( n + "smry" )) ) {
        e.innerHTML = d.innerHTML.match( /(Error|Status)(:[^\.]+\.)/i ) ? RegExp.$1 + RegExp.$2 : "ERROR: Upload requered.";
        e.title     = "Uploaded: " + f;
      }
      if( !!(e = document.getElementById( n + "Key"  )) ) e.value = k;
      if( !!(e = document.getElementById( n + "kf"   )) ) e.value = k;
      if( !!(e = document.getElementById( n + "btn"  )) ) e.innerHTML = k != "" ? " Reload " : " Upload ";
      if( !!(e = document.getElementById( n + "cf"   )) ) e.innerHTML = f;
      if( !!(e = document.getElementById( n + "clr"  )) ) e.style.display = k == "" ? "none" : "inline";
      if( k != "" ) { ao.setCookie( n + "key", k, 3 ); ao.setCookie( n + "file", f, 3 ); }
      else          { ao.delCookie( n + "key"       ); ao.delCookie( n + "file"       ); }
    };
    window.rdv.submit = function( form ) {
      alert( form.id + ".onsubmit()\n" );
    }
    window.rdv.initupload = function( name, what ) { 
     var a, c, e, ed, i, k, req;
      if( !(e = document.getElementById( name + 'body' )) ) return;
      req = null;
      if( window.XMLHttpRequest ) { req = new XMLHttpRequest(); } else
      if( window.ActiveXObject  ) { req = new ActiveXObject("Microsoft.XMLHTTP"); }
      if( req == null ) return;
      e.innerHTML = '<div class="b_b"> </div><div class="b_m"><span id="' + name + 'btn"> Upload </span></div><div class="b_e"> </div>'
         + '<div class="b_s"><span id="'+ name + 'smry">Nothing uploaded...</span></div>';
      if( !!(e = document.getElementById( name + 'btn'   )) ) ao.setEvent( e, 'click', function() { return startupload( name ); } );
      if( !!(e = document.getElementById( name + 'frame' )) ) ao.addEvent( e, 'load',  function() { return rdv.loaded ( name + 'frame' ); } );
      if( !!(e = document.getElementById( name + 'clr'   )) ) ao.setEvent( e, 'click', function() { return filedelete ( name, what ); } );
      if( !(ed = document.getElementById( "d_" + name + "dlg" )) ) return;
      a = ed.getElementsByTagName( "*" );
      for( i=0; i<a.length; i++ ) {
        if( (c = a[i].className.match( /d_(\d+)/ )) ) ao.setEvent( a[i], "mousedown", ao.grab   );
        else if( a[i].className.match( /button/  )  ) ao.setEvent( a[i], "click",     rdvclick ); 
      }
      if( !!(k = ao.getCookie( name + "key" )) ) {
        req.open( "GET", "../Efile.cgi?Key=" + k );
        req.onreadystatechange = function() {
         var a, e, k, m;
          k = m = "";
          if( req.readyState == 4 ) { 
            a = "ERROR: Upload requered: " + req.status;
            if( req.status == 200 || req.status == 0 ) { 
              a = req.responseText;
              if( (m = ao.getCookie( name + "file" )) == "" ) m = "copy/pasted text";
              if( a.match( /(Error|Status)(:[^\.]+\.)/i ) ) a = RegExp.$1 + RegExp.$2;
              if( req.responseText.match( /Key:\s*(\S+)/ ) ) {
                if( !!(RegExp.$1) ) {
                  ao.setCookie( name + "key",  k = RegExp.$1, 3 );
                  if( !!(e = document.getElementById( name + "Key" )) ) e.value = k;
                  if( !!(e = document.getElementById( name + "kf"  )) ) e.value = k;
                }
              }
		    } 
            if( !!(e = document.getElementById( name + "btn" )) ) e.innerHTML=  k != "" ? " Reload " : " Upload ";
            if( k == "" ) { 
              ao.delCookie( name + "key" );
              ao.delCookie( name + "file" );
              m = "no file";
            }
		    if( !!(e = document.getElementById( name + "smry" )) ) {
		      e.innerHTML = a;
		      e.title     = "Uploaded: " + m;
		    }
		    if( !!(e = document.getElementById( name + "cf"  )) ) e.innerHTML = m;
		    if( !!(e = document.getElementById( name + "clr" )) ) e.style.display = k == "" ? "none" : "inline";
            if( !!(m) ) ao.setCookie( name + "file", m, 3 );
		  }
		};
	    req.send( null );
	  }
    };
})();
rdv.addOnLoad( function() {
  rdv.initcookie();
  rdv.inittoggle();
  rdv.initselect();
  rdv.initdialog( "ptm", "PTMs" );
  rdv.initdialog( "aa", "amino acids" );
//  rdv.initupload( "isf", "no spectrum" );
//  rdv.initupload( "isq", "no sequence" );
} );
  
