var DocUtil={
  get:function(id,doc){
    if(doc)return id?(doc.all?doc.all[id]:doc.getElementById(id)):null;
    return id?(document.all?document.all[id]:document.getElementById(id)):null;
  },
  antiNIS:function(){
    try{
      if(SymRealWinOpen){
        window.open=SymRealWinOpen;
        SymRealWinOpen=null;
      }
    }catch(e){}
  }
}

var ActionUtil = {
  help:{
    path:'doc/help',
    files:[
      'Message-Center.pdf',
      'Buying-Tools.pdf',
      'Selling-Tool.pdf',
      'Trade-Match.pdf',
      'Account-Management.pdf',
      'Subscription.pdf',
      'My-Tradeeasy-Overview.pdf'],
    activeFile:0,
    go:function(){
      if(this.activeFile<0||this.activeFile>this.files.length-1)return;
      if(!this.files[this.activeFile])return;
      ActionUtil.popup(0,0,800,600,this.path+'/'+this.files[this.activeFile],'help',true,true);
    }
  },
  tabMenu:{
    lastTab:new Array(),
    activateTab:function(tab,target){
      this.deactivateTab(target);
      var t=DocUtil.get(target);
      var o=DocUtil.get('portlet-head-'+tab);
      if(o)o.className=o.className+' open';
      o=DocUtil.get(tab);
      if(o&&t)t.innerHTML=o.innerHTML;
      this.lastTab[target]=tab;
    },
    deactivateTab:function(target){
      if(!this.lastTab[target])return;
      var o=DocUtil.get('portlet-head-'+this.lastTab[target]);
      if(o)o.className=o.className.replace(' open','');
    },
    randomTab:function(tabs,target){
      if(!tabs.length||tabs.length==0)return;
      var n=Math.round(Math.random()*1000)%tabs.length;
      this.activateTab(tabs[n],target);
    }
  },
  showHideViewContent:function(obj){
    if(!obj) return;
    var target= DocUtil.get(obj);
    if(target)target.className=target.className=='hide-content'?'view-content':'hide-content';
  }, 
  popup:function(x,y,w,h,href,target,resize,scroll){
    var p='top='+y+',left='+x+',width='+w+',height='+h;
    var isdefault=true;
    if(resize||scroll) isdefault=false;
    Ajax.popup(target,href,y,x,w,h,isdefault);
  },
  bookmark:function(title,href) {
    if(!title)title=document.title;
    if(!href)href=location.href;
    if(window.sidebar){//firefox
      window.sidebar.addPanel(title,href,'');
    }else if(window.opera && window.print){//opera
    	var e=document.createElement('a');
    	e.setAttribute('href',url);
    	e.setAttribute('title',title);
    	e.setAttribute('rel','sidebar');
    	e.click();
    }else if(document.all){//ie
      window.external.AddFavorite(location.href, title?title:document.title);
    }else alert("Sorry, Your browser doesn't support this bookmark function!");
  },
  makeHomepage:function(href){
    if(!href)href=location.href;
    if(document.all){//ie
      var e=document.createElement('a');
      e.style.behavior='url(#default#homepage)';
      e.setHomePage(href);
    }else if(document.getElementById){//firefox
      var msg='To make Tradeeasy.com as your home page, simply drag <br><a href="'+
        href+'" target="parent">this link onto your Home button</a>.';
      this.dialog.ack('/p/portal/home/html/',msg);
    }
  },
  dialog:{
    id:'dialog-box',
    msg:null,
    err:function(path,msg){
      if(!path)path='';
      this.open(path+'dialog-err.htm',msg);
    },
    ack:function(path,msg){
      if(!path)path='';
      this.open(path+'dialog-ack.htm',msg);
    },
    open:function(href,msg,w,h){
      if(!w) w=500;
      if(!h) h=200;
      if(!msg)msg=this.msg;
      if(document.all){//for IE
        var p='dialogHeight:'+h+'px;dialogWidth:'+w+'px;status:0;scroll:0';
        var o=window.showModalDialog(href,msg,p);
      }else{//for FF
        var f=DocUtil.get(this.id);
        var wx=window.pageXOffset,wy=window.pageYOffset;
        var ww=window.innerWidth,wh=window.innerHeight;
        var x=wx+(ww-w)/2,y=wy+(wh-h)/2;
        if(!f){
          f=document.createElement("IFRAME");
          f.setAttribute('id',this.id);
          f.setAttribute('scrolling','no');
          document.body.appendChild(f);
          window.onscroll=window.onresize=function(){
            var f=DocUtil.get(ActionUtil.dialog.id);
            if(f){
              var wx=this.pageXOffset,wy=this.pageYOffset;
              var ww=this.innerWidth,wh=this.innerHeight;
              var x=wx+(ww-w)/2,y=wy+(wh-h)/2;
              f.style.top=y+'px';
              f.style.left=x+'px';
            }
          }
        }
        f.style.top=y+'px';
        f.style.left=x+'px';
        f.style.width=w+'px';
        f.style.height=h+'px';
        f.src=href+'?'+escape(msg);
        f.focus();
      }
    },
    fill:function(n,w){
      var d=DocUtil.get(n);
      if(!d)return;
      if(!w.dialogArguments){
        var p=w.location.href.indexOf('?');
        if(p>0)d.innerHTML=unescape(w.location.href.substring(p+1));
        d=DocUtil.get('dialog');
        if(d)d.className='show-border';
        if(parent){
          d=DocUtil.get(this.id,parent.document);
          if(d)d.style.height=(document.body.offsetHeight+4)+'px';
        }
      }else{
        d.innerHTML=w.dialogArguments;
        w.dialogHeight=(document.body.offsetHeight+29)+'px';
      }
    },
    close:function(){
      if(document.all)self.close();
      else if(parent){
        var d=DocUtil.get(this.id,parent.document);
        if(d)parent.document.body.removeChild(d);
      }
    }
  }
}

/* Ajax HTTP requestor */
var Ajax={
  popup:function(alias,href,top,left,width,height,isdefault,noresize){/* popup new window w/- specified URL */
    var p=isdefault?'':'hotkeys=0,toolbar=0,location=0,directories=0,status=0,menubar=0';
    p+=noresize?',scrollbars=0,resizable=0':',scrollbars=1,resizable=1';
    if(typeof(top)!='undefined'&top!=null)p+=',top='+top;
    if(typeof(left)!='undefined'&left!=null)p+=',left='+left;
    if(typeof(width)!='undefined'&width!=null)p+=',width='+width;
    if(typeof(height)!='undefined'&height!=null)p+=',height='+height;
    var ref=document.referrer;
    alias=alias?alias:'popup';
    DocUtil.antiNIS();
    if(document.all && ref){//ie
      var w=open(ref,alias,p);
      w.document.write("<title>" + alias + "</title>");
      w.document.write("<a id='go' href='"+href+"' style='display:none;' target='" + alias +"'>a</a>");
      w.document.getElementById("go").click();
      w.focus();
    }else{
      var c=open(this.convertUniqueUrl(href),alias,p);
      c.focus();
    }
  },
  convertUniqueUrl:function(href){/* convert incoming url to an unique url by appending a parameter unique request no. (urn) */
    var ohref=href+=(href.indexOf('?')==-1?'?':'&')+'urn='+new Date().getTime();
    return ohref;
  }
}

