function showError(title,msg)
{
    var dlg = new Dlg(title,"cError",300,200);
    dlg.show();
    dlg.setMsg(msg);
}

window.addDialog=function(dlg)
{
  if(window.dialogs==undefined)window.dialogs=new Object();
  var result=1;
  while(window["dialog"+result.toString()]!=undefined){result++;}
  dlg.id="dialog"+result.toString();
  window[dlg.id]=dlg;
}
window.removeDialog=function(dlg){window[dlg.id]=null;}
window.hideDialog=function(id){window[id].hide();}
window.addTimeout=function(item)
{
  if(window.timeouts==undefined)window.timeouts=new Object();
  window["timeout"+item.id]=item;
}
window.incrementTimeout=function(id){window["timeout"+id].increment();}
window.removeTimeout=function(item){window["timeout"+item.id]=null;}

function Dlg(t,n,w,h){if(arguments.length>0)this.init(t,n,w,h);}
Dlg.prototype.attachDragEvents=function()
{
  var targetDlg=this;
  this.mouseMoveHandler=function(e)
  {
    e=(e)?e:event;
    if(e.clientX||e.clientY){targetDlg.shiftTo((e.clientX-targetDlg.offsetLeft),(e.clientY-targetDlg.offsetTop));}
    e.cancelBubble=true;
    return false;
  };
  this.mouseUpHandler=function(e)
  {
    deleteEvent(document,"mousemove",targetDlg.mouseMoveHandler);
    deleteEvent(document,"mouseup",targetDlg.mouseUpHandler);
  };
  this.mouseDownHandler=function(e)
  {
    e=(e)?e:event;
    addEvent(document,"mousemove",targetDlg.mouseMoveHandler);
    addEvent(document,"mouseup",targetDlg.mouseUpHandler);
    if(e.offsetX||e.offsetY)
    {
      targetDlg.offsetLeft=e.offsetX-((e.offsetX<-2)?0:hscrollPosition());
      targetDlg.offsetTop=e.offsetY-((e.offsetY<-2)?0:vscrollPosition());
    }
    else if (e.clientX)
    {
      targetDlg.offsetLeft=e.clientX-((targetDlg.el("dlgWindow").offsetLeft)?targetDlg.el("dlgWindow").offsetLeft:0);
      targetDlg.offsetTop=e.clientY-((targetDlg.el("dlgWindow").offsetTop)?targetDlg.el("dlgWindow").offsetTop:0);
    }
    return false;
  };
  addEvent(this.el("hdrc"),"mousedown",this.mouseDownHandler);
}
Dlg.prototype.clrMsg=function(){this.el("msg").innerHTML="";}
Dlg.prototype.clrSbar=function(){if(this.statusBar!=null){this.statusBar.hide();this.statusBar=null;}}
Dlg.prototype.clrState=function(){this.clrMsg();this.clrSbar();}
Dlg.prototype.contains=function(ele)
{
  return !(Math.max(this.left,elX(ele))>Math.min(this.left+this.el("dlgWindow").offsetWidth,elX(ele)+ele.offsetWidth)||Math.max(this.top,elY(ele))>Math.min(this.top+this.el("dlgWindow").offsetHeight,elY(ele)+ele.offsetHeight));
}
Dlg.prototype.createPng=function(name)
{
  if(isIe()){return '<span style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/'+name+'.png\',sizingMethod=\'scale\');"></span>';}
  else{return '<img src="/images/'+name+'.png" alt="" />';}
}
Dlg.prototype.el=function(id){return el(this.id+id);}
Dlg.prototype.hide=function()
{
  try
  {
    this.clrState();
    document.onmousemove=null;
    document.onmouseup=null;
    this.showAllSelects();
    window.removeDialog(this);
    el("dialogSocket").removeChild(this.el("dlgWindow"));
  }
  catch(e){}
}
Dlg.prototype.init=function(t,n,w,h,ha,va){this.title=t;this.name=n;this.width=w;this.height=h;this.msg=null;this.offsetLeft=null;this.offsetTop=null;this.selects=null;this.hasStatusBar=false;this.statusBar=null;window.addDialog(this);}
Dlg.prototype.setMsg=function(msg)
{
  this.clrMsg();
  this.msg=msg;
  this.el("msg").innerHTML=msg;
}
Dlg.prototype.setSelectDisplay=function()
{
  if(!isIe()||this.selects==undefined)return;
  for(var i=0;i<this.selects.length;i++)
  {
    if(this.contains(this.selects[i])) this.selects[i].style.visibility="hidden";
    else this.selects[i].style.visibility="visible";
  }
}
Dlg.prototype.shiftTo=function(x,y)
{
  this.left=x;this.top=y;
  this.style("dlgWindow").left=pxl(x);this.style("dlgWindow").top=pxl(y);
  this.setSelectDisplay();
}
Dlg.prototype.show=function()
{
  this.selects=document.getElementsByTagName("select");
  
  var wdw=new Element("div","dlgWindow",this,this.id);
  wdw.addStyle("display","block")
    .addStyle("width",pxl(this.width))
    .addElement("div","hdr")
      .addClass("clearfix")
      .addElement("div","hdrl")
        .addHtml(this.createPng("dul"))
      .parent.addElement("div","hdrc")
        .addStyle("width",pxl(this.width-12))
        .addElement("p","ttl")
          .addHtml(this.title)
          .addStyle("width",pxl(this.width-44))
        .parent.addElement("a")
          .addAttribute("onclick","window.hideDialog('"+this.id+"')")
          .addElement("img","clsbtn")
            .addAttribute("src","/images/dcb.gif")
            .addAttribute("alt","")
      .parent.parent.parent.addElement("div","hdrr")
        .addHtml(this.createPng("dur"))
    .parent.parent.addElement("div","bdy")
      .addStyle("width",pxl(this.width-2))
      .addElement("div","bdy1")
        .addStyle("width",pxl(this.width-4))
        .addElement("div","bdy2")
          .addStyle("width",pxl(this.width-6))
          .addElement("div","bdy3")
            .addStyle("width",pxl(this.width-6))
            .addElement("div","msg")
              .addHtml(this.msg)
          .parent.parent.addElement("div","cls")
            .addStyle("width",pxl(this.width-22))
            .addElement("a")
              .addAttribute("onclick","window.hideDialog('"+this.id+"')")
              .addHtml("Close");
  
  el("dialogSocket").innerHTML+=wdw.toString();
  
  this.shiftTo((windowWidth()/2)-(this.width/2)+hscrollPosition(), (windowHeight()/2)-(this.height/2)+vscrollPosition() );

  this.attachDragEvents();
  if(this.hasStatusBar)
  {
    this.statusBar=new StatusBar();
    this.statusBar.show(this.el("bdy3"));
  }
  
}
Dlg.prototype.showAllSelects=function()
{
  if(!isIe()||this.selects==undefined)return;
  for(var i=0;i<this.selects.length;i++){this.selects[i].style.visibility="visible";}
}
Dlg.prototype.style=function(id){return this.el(id).style;}
Dlg.prototype.toString=function(){return this.name;}

function Element(type,id,parent,idPrefix)
{
  this.parent=parent;
  this.type=type;
  this.idPrefix=idPrefix;
  this.children=new Array();
  this.id=id;
  this.attributes=new Object();
  this.innerHTML="";
  this.style="";
  if(this.id!=null){this.className=id;this.addAttribute("id",this.idPrefix+this.id);}else{this.className="";}
}
Element.prototype.addAttribute=function(name,value){this.attributes[name]=value;return this;}
Element.prototype.addElement=function(type,id)
{
  if(id==undefined)id=null;
  var ele=new Element(type,id,this,this.idPrefix);
  this.children.push(ele);

  return ele;
}
Element.prototype.addClass=function(className){this.className+= " " + className;return this;}
Element.prototype.addHtml=function(html){this.innerHTML+=html;return this;}
Element.prototype.addStyle=function(name,value){this.style+=name+":"+value+";";return this;}
Element.prototype.toString=function()
{
  var html="";
  for(var i=0;i<this.children.length;i++){html+=this.children[i].toString();}
  var result="<"+this.type;
  for($attribute in this.attributes){result+=' '+$attribute+'="'+this.attributes[$attribute]+'" ';}
  if(this.style.length>0){result+='style="'+this.style+'" ';}
  if(this.className.length>0){result+='class="'+this.className+'" ';}
  if(html.length>0){result+=">"+html+"</"+this.type+">";}
  else if(this.innerHTML.length>0){result+=">"+this.innerHTML+"</"+this.type+">";}
  else{result+="/>";}
  return result;
}
function StatusBar()
{
  this.parent=null;
  this.maxValue=0;
  this.value=0;
  this.id="";
  this.timeoutId=null;
}
StatusBar.prototype.hide=function()
{
  if(this.timeoutId!=null)clearTimeout(this.timeoutId);
  window.removeTimeout(this);
  el(this.parent.id).removeChild(el(this.id));
}
StatusBar.prototype.increment=function()
{
  this.value+=3;
  if(this.value>this.maxValue)this.value=0;
  el(this.id).style.width=pxl(this.value);
  this.timeoutId=setTimeout('window.incrementTimeout("'+this.id+'");',100);
}
StatusBar.prototype.show=function(parent)
{
  this.parent=parent;
  this.maxValue=parent.offsetWidth-40;
  this.id=parent.id+"StatusBar";
  
  var view=new Element("div","StatusBar",null,parent.id);
  view.addStyle("width",pxl(0))
    .addStyle("height",pxl(20))
    .addStyle("background-color","#00f")
    .addStyle("margin",pxl(20));
    
  var tmp=ce("div");
  tmp.innerHTML=view.toString();
  parent.appendChild(tmp.childNodes[0]);
  window.addTimeout(this);
  this.timeoutId=setTimeout('window.incrementTimeout("'+this.id+'");',100);
}
