// JavaScript Document
/*include files
  newElement.js
  misc.js
*/
function WinPane(){
  this.blnShowClose = true;
  //this.blnShowOk = true;
  //this.blnShowCancel = false;
  this.blnShowStatusBar = true;
  this.blnResizable = true;
  this.strStatusText = 'Done';
  this.strCaption = '';
  this.strImagePath = '';
  this.strPaneSkin = 'modern';
  this.numInitWidth = 250;
  this.numInitHeight = 130;
  this.numMinWidth = 250;
  this.numMinHeight = 130;
  this.Content = '';
  this.funcOnClose = '';
  this.isMoveable = false;
  //this.funcOnOk = '';
  //this.funcOnCancel = '';
  this.top = 0;
  this.left = 0;
}

WinPane.prototype.init = function(){
  var self = this;
  var X=self.left, Y=self.top+60;
  self.divPane = '';
  
  if(self.blnShowStatusBar){
    var cookieVal = Cookie.Read('paneParams_WH_XY');
    if(cookieVal){
      var o0o = String(cookieVal).split(',');
      self.numInitWidth = (o0o[0]-((_isIE)?0 :2));
      self.numInitHeight = (o0o[1]-((_isIE)?47 :59));
      X = o0o[2]; //enable if you want dynamic left position
      Y = o0o[3]; //enable if you want dynamic top position
    };
  };
  //if(!X)X=((screen.width-self.numInitWidth)/2);
  if(!X)X=((document.body.clientWidth-self.numInitWidth)/2);
  
  //var btnClose = new Button();
  var btnOk = new Button();
  var btnCancel = new Button();
  var captionTable = new Table(1,2);
  var container = new Table(((self.blnShowStatusBar)?3:2),1);
  var statusBar = new Table(1,((self.blnResizable)?2:1));
  var divPane = new Div('divPane');
  self.Content = new Div();
  
  with(container){
    style.width = '100%';
    style.height = '100%';
    border = '0px';
    borderColor = '#CCCCCC';
    cellSpacing = '0';
    cellPadding = '0';
    with(rows[0].cells[0]){ //caption bar
      with(captionTable){
        unselectable = "on";
        with(style){
          width = '100%';
          cursor = 'default';
          background = 'transparent url('+ self.strImagePath +'/imgs/skin_modern_header.png)';
          border = 0;
          font = '11px Tahoma';
          fontWeight = 'bold';
          padding = '0px 5px 0px 5px';
          nowrap = true;
          MozUserSelect = "none";
        };
        border = '0';
        cellSpacing= '0';
        cellPadding= '0';
        //className = 'capBar';
        with(rows[0]){
          misc.setInnerText(cells[0], self.strCaption);
          with(cells[1]){
            height = 22;
            if(self.blnShowClose){
              dir = 'rtl';
              var btnClose = new Button();
              with(btnClose){
                with(style){
                  //height='20px';
                  //width='20px';
                  //fontWeight='bold';
                  //fontSize='10px';
                  //fontFamily='verdana';
                  //color='#000000';
                };
                innerHTML = 'X';
                title = 'Close';
              };
              btnClose.onmousedown = function(){
                self.Close();
              };
              appendChild(btnClose);
              
              self.hideClose = function(){
                btnClose.style.display = 'none';
              };
              
            }else innerHTML = '&nbsp;';
          };
        };
      };
      
	  if(self.isMoveable){
		  captionTable.onmousedown = function(e){
			var ev = e || window.event;
			captionTable.style.cursor = 'move';
        if (ev.button<=1){
          var el = divPane;
          var a = el.style.left;
          var b = el.style.top;
          var pleft=parseInt(a.substring(0, (a.length-2)));
          var ptop=parseInt(b.substring(0, (b.length-2)));
          var xcoor=ev.clientX;
          var ycoor=ev.clientY;
          
          document.onmousemove=function(e){
          var ev = e || window.event;
          var l = pleft+(ev.clientX-xcoor);
          var t = ptop+(ev.clientY-ycoor);
          el.style.left = (l + 'px');
          el.style.top = (t + 'px');
          };
        };
		  };
		  
		  captionTable.onmouseup = function(e){
			captionTable.style.cursor = 'default';
			document.onmousemove = '';
		  };
	  };
      
      appendChild(captionTable);
    };
    
    with(rows[1].cells[0]){ //body
      with(self.Content){
        //unselectable = "on";
        with(style){
          //font = '8px Verdana red';
          borderBottom = '1px solid black';
          borderTop = '1px solid black';
          padding = '5px';
          position = 'static';
          overflow = 'auto';
          height = self.numInitHeight +'px';
          width = (self.numInitWidth-(_isIE?0 :10)) +'px';
          //MozUserSelect = "none";
        };
      };
	  
	  style.height='auto';
      vAlign = 'top';
      appendChild(self.Content);
    };
    
    if(self.blnShowStatusBar){
      with(rows[2]){
        with(cells[0]){ //status bar
          with(statusBar){
            style.height = '17px';
            cellSpacing = '0px';
            cellPadding = '0px';
            border = '0px';
            with(rows[0].cells[0]){
              with(style){
                color = '#000000';
                fontFamily = 'Verdana, Arial, Helvetica, sans-serif';
                fontSize = '10px';
                width = '100%';
                padding = '0px 0px 0px 5px';
              };
              innerHTML = self.strStatusText;
            };
            if(self.blnResizable){
              with(rows[0].cells[1]){
                var divResize = new Div();
                with(divResize.style){
                  background = 'transparent url(\''+ self.strImagePath +'/imgs/resizeSE.gif' +'\') top right no-repeat';
                  float = 'right';
                  width = '11px'; /*width of resize image*/
                  height = '20px'; /*height of resize image*/
                  cursor = 'nw-resize';
                  fontSize = '0';
                };
                
                divResize.onmousedown = function(e){
                  var ev = e || window.event;
                  if (ev.button<=1){
                    var applySize = function(obj){
                      self.Content.style.height = obj.h-52;
                      self.Content.style.width = (obj.w-(_isIE?0 :10));
                      divPane.style.width = obj.w;
                      applySize = '';
                    };
                    misc.Resize(applySize, divPane, ev, self.numMinHeight, self.numMinWidth);
                  };
                };
                
                appendChild(divResize);
              };
            };
          };
          appendChild(statusBar);
        };
      };
    };
  };
  
  with(divPane){
    with(style){
      width = self.numInitWidth +'px';
      height = 'auto';
      border = '1px solid black';
      backgroundColor = 'white';
      position = 'absolute';
      //overflow = 'auto';
      cursor = 'default';
      top = Y +'px';
      left = X +'px';
    };
    className = 'pane' + ((self.strPaneSkin)?(' pane_'+self.strPaneSkin):'');
    appendChild(container);
  };
  self.divPane = divPane;
  document.body.appendChild(self.divPane);
};

WinPane.prototype.Close = function(){
  var self = this;
  if(self.blnShowStatusBar){
    var cookieVal = self.divPane.offsetWidth;
    cookieVal += (','+ self.divPane.offsetHeight);
    cookieVal += (','+ self.divPane.offsetLeft);
    cookieVal += (','+ self.divPane.offsetTop);
    Cookie.Append('paneParams_WH_XY', cookieVal, 0);
  };
  misc.removeElement(self.divPane);
  if((typeof(self.funcOnClose)).toLowerCase()=='function')self.funcOnClose();
  self.funcOnClose = '';
};

// type string[alert, information, question], string[Caption], [string, html]
function MsgBox(_type, caption, message, width, func_ok_Yes, func_cancel_No){
  if(!_type.Trim())return;
  
  Window.scrollBars.hide();
  
  var msgWin = new WinPane();
  var type = _type.toUpperCase();
  var tbl = new Table(2,1);
  with(tbl){
    with(rows[0].cells[0]){
      var tbl1 = new Table(1,2);
      with(tbl1.rows[0]){
        with(cells[0]){
          align = 'center';
          vAlign = 'top';
          innerHTML = '<img src="'+pathLib+'/imgs/icons/'+skin+'/'+type.toLowerCase()+'.gif" style="margin:15px">';
        };
        
        with(cells[1]){
          width = '100%';
          innerHTML = '<br />'+message+'<br />';
        };
      };
      
      appendChild(tbl1);
    };
    
    with(rows[1].cells[0]){
      align = 'center';
      height = 30;
      
      switch(type){
        case "ALERT"://ok
        case "INFORMATION"://ok
        case "QUESTION"://Yes
          var btnOk = new Button();
          btnOk.className = 'message';
          misc.setInnerText(btnOk, ((type=="QUESTION")?"Yes" :"Ok"));
          
          btnOk.onmousedown = function(){
            msgWin.Close();
            Window.scrollBars.show();
            if(func_ok_Yes)func_ok_Yes();
          };
          appendChild(btnOk);
          btnOk.focus();
        break;
      };
      
      switch(type){
        case "QUESTION"://No
          var btnCancel = new Button();
          btnCancel.className = 'message';
          misc.setInnerText(btnCancel, ((type=="QUESTION")?"No" :"Cancel"));
          
          btnCancel.onmousedown = function(){
            msgWin.Close();
            Window.scrollBars.show();
            if(func_cancel_No)func_cancel_No();
          };
          appendChild(btnCancel);
        break;
      };
    };
  };
  
  with(msgWin){
    blnShowClose = false;
    blnShowStatusBar = false;
    blnResizable = false;
    strCaption = caption;
    strImagePath = pathLib;
    strPaneSkin = skin;
    numInitWidth = width || 250;
    //numInitHeight = height || 100;
    funcOnClose = '';
    isMoveable = false;
    top = document.body.scrollTop;
    //left = 0;
    
    init();
    
    with(Content){
      misc.removeAttribute(style, 'overflow');
      misc.removeAttribute(style, 'height');
      appendChild(tbl);
    };
  };
};


function Alert(){
  this.Caption = 'Alert Message';
  this.Message = '';
  this.funcOnOk = function(){return false};
  this.strLibPath = '';
  this.strSkin = '';
  this.height = 100;
  this.width = 400;
};

Alert.prototype = {
  Show : function(){
    var self = this;
    var disabler = misc.Disable(document.body);
    var alertWin = new WinPane();
    var frame = new Table(3,1);
    //self.strLibPath += '/imgs';
    with(frame){
      width = '100%';
      border = 0;
      with(rows[0]){
        with(cells[0]){
          var t = new Table(1,2);
          t.border = 0;
          with(t.rows[0]){
            with(cells[0]){
              vAlign = 'top';
              align = 'center';
              var img = new Image();
              with(img){
                src = (self.strLibPath +'/imgs/icons/'+ self.strSkin+'/alert.gif');
                style.margin = '0px 5px 0px 5px';
              };
              appendChild(img);
            };
            
            with(cells[1]){
              innerHTML = self.Message;
            };
          };
          appendChild(t);
        };
        
        //cells[0].style.padding = '0px 8px 0px 8px';
        //cells[0].innerHTML += (''+self.Message);
      };
      
      rows[1].cells[0].innerHTML = '&nbsp;';
      with(rows[2].cells[0]){
        with(style){
          align = 'center';
        };
        var btnOk = new Button();
        btnOk.innerHTML = 'ok';
        btnOk.style.width = '70px';
        
        btnOk.onclick = function(){
          alertWin.Close();
          misc.removeElement(disabler);
          self.funcOnOk();
        };
        
        appendChild(btnOk);
      };
    };
    
    with(alertWin){
      strCaption = self.Caption;
      strImagePath = self.strLibPath;
      numInitWidth = self.width;
      numInitHeight = self.height;
      blnShowStatusBar = false;
      top = ((document.body.clientHeight-150)/2)+document.body.scrollTop;
      blnShowClose = false;
      init();
      Content.appendChild(frame);
    };
  }
};
