﻿
var winTypes = {None:0,Ok:1,YesNo:2,AutoExpire:3,Iframe:4};
var resultTypes = {Ok:0,Yes:1,No:2,Cancel:3,Close:4};

function DWSWin(varName){

this.innerHTML='<br/>';
//this.id='DWSWin_'+ Math.round(Math.random()*1000);
this.myName=varName==null?'DWSWinObject':varName;
this.id=this.myName+'_DWSWin_id';
this.contentId=this.myName+'_winBody_id';
this.loadingId=this.myName+'_loading_id';
this.title=culture.term('DefaultWinTitle');
this.winType=winTypes.None;
this.width=300;
this.height=125;
this.pageSize;
this.pageScroll;
this.padding='10px 10px 20px 10px';
this.x;
this.y;
this.backGroundOpacity=50;
this.opacity=100;
this.grayOut=false ;
this.wrap=false;
this.closable=true;
this.movable=true;
this.titleEnable=true;
this.loadingImage=false;
this.expireTime=3000;
this.result='';
this.iframeUrl='';
this.iframeScroll='no';
this.darken=darken;
this.createWindow=createWindow;
this.close=close;
this.show=show;disableScreen
this.disableScreen=disableScreen;
this.getCH=getCH;
this.setInnerHTML=setInnerHTML;
this.getPageSize=getPageSize;
this.getPageScroll=getPageScroll;
this.reArrange=reArrange;
this.changeSelects=changeSelects;
this.visible=false;
this.contentVAlign='middle';
this.contentAlign='center';
this.onCloseParams='';
this.close=close; 
function getCH(){
    if(this.titleEnable)
        return (this.height-22);
    else
        return this.height;
}  
function setInnerHTML(html){

if(getElm(this.contentId))
   getElm(this.contentId).innerHTML=html;
 else
   this.innerHTML=html;
    
}
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	this.pageScroll = new Array('',yScroll) 
	return this.pageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	this.pageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return this.pageSize;
}

function reArrange(){
this.getPageSize();
this.getPageScroll();

var dark=document.getElementById('darkenScreenObject');
  if (dark && dark.style.display=='block')
      this.darken(true);
}

function changeSelects(state){
var selects = document.getElementsByTagName("select");
        for (var i = 0; i != selects.length; i++) {
                selects[i].style.visibility = state;
        }
}
        
function darken(vis) {

  var dark=document.getElementById('darkenScreenObject');
 
  if (!dark) {
   
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           
        tnode.style.position='absolute';                
        tnode.style.top='0px';                         
        tnode.style.left='0px';                     
        tnode.style.overflow='hidden';               
        tnode.style.display='none';                   
        tnode.id='darkenScreenObject';               
        tnode.innerHTML='<!--[if lte IE 6.5]><iframe id="hideSelectFrame" style="width:95%; height:95%;"></iframe><![endif]-->';       
        tbody.appendChild(tnode);                            
        dark=getElm('darkenScreenObject');

  }
  

  
 if(vis){
    setOpacity(dark,this.backGroundOpacity);
    dark.style.zIndex=50;        
    dark.style.backgroundColor='#000000';  
    dark.style.width= this.pageSize[0] + 'px';
    dark.style.height= this.pageSize[1] + 'px';
    dark.style.display='block';
    
   }else{
    dark.style.display='none';
    
   }

}

function createWindow(){
    var win=getElm(this.id);
    if(!win){
    win= document.createElement('div');
    var myBody = document.getElementsByTagName("body")[0];
    setClass(win,'dwswin');
    //setOpacity(win,this.opacity);
    win.id=this.id;
    myBody.appendChild(win);
    }

    if(isEmpty(this.x))
        this.x=((this.pageSize[0] - this.width) / 2);
    if(isEmpty(this.y))
        this.y=this.pageScroll[1] + ((this.pageSize[3] - 35 - this.height) / 2);
    win.style.width=this.width+'px';
    win.style.height= this.height+'px'; 
    win.style.left=this.x+'px';
    win.style.top=this.y+'px';
   
    
 
    var winBody='<table class="dwsTbl" width="100%"  cellpadding="0" cellspacing="0" border="0">';
    var hdrCss =this.movable?'winHdr movable':'winHdr';
    if(this.titleEnable){
        winBody+='<tr><td class="'+hdrCss+'">'
    winBody+= this.title;
    winBody+='</td><td class="'+hdrCss+'" align="right">'
    if(this.closable)
        winBody+='<a class="winClose" href="#" alt="'+culture.term('Close')+'" onclick="'+this.myName+'.close(resultTypes.Close); return false;"></a>';
        winBody+='</td><tr>';
        }
    
    winBody+='<tr><td colspan="2" align="'+this.contentAlign+'" valign="'+this.contentVAlign+'"><div id="'+this.contentId+'" class="winBody" ';
    winBody+='style="padding:'+this.padding+'; height:'+this.getCH()+'px; width:'+(this.width)+'px;" ';

    if(this.wrap)
        winBody+=' nowrap ';
        
    winBody+='>';
    
    if(this.loadingImage)
            winBody+='<div id="'+this.loadingId+'" class="loading" style="width:'+this.width+'px; height:'+this.getCH()+'px;"></div>';
         
    if(this.winType==winTypes.Iframe)
        winBody+='<iframe id="iframe" style="visibility:hidden; position:absolute" onload="hide(getElm('+this.myName+'.loadingId)); show(this);" width="'+this.width+'" height="'+this.getCH()+'" frameborder="0" marginheight="0" marginwidth="0" scrolling="'+this.iframeScroll+'" src="'+this.iframeUrl+'"> </iframe>';
    else
        winBody+=this.innerHTML;
    
           
    if(this.winType==winTypes.Ok || this.winType==winTypes.YesNo){
        winBody+='<center class="msgBtns">';
        if(this.winType==winTypes.Ok){
        winBody+='<input class="btn1" type="button" value=" '+culture.term('Ok')+' " onclick="'+this.myName+'.close(resultTypes.Ok);"/>'
        }else if(this.winType==winTypes.YesNo){
        winBody+='<input class="btn1" type="button" value="  '+culture.term('Yes')+'  " onclick="'+this.myName+'.close(resultTypes.Yes);"/>&nbsp;&nbsp;'
        winBody+='<input class="btn1" type="button" value="  '+culture.term('No')+'  " onclick="'+this.myName+'.close(resultTypes.No);"/>&nbsp;&nbsp;'
        winBody+='<input class="btn1" type="button" value="  '+culture.term('Cancel')+'  " onclick="'+this.myName+'.close(resultTypes.Cancel);"/>'
        }
    winBody+='</center>';
    }else if(this.winType==winTypes.AutoExpire)
                setTimeout("if(getElm('"+this.id+"')) "+this.myName+".close();",this.expireTime);

    winBody+='</div></td></tr></table>';
   if(!this.grayOut) 
    winBody+='<!--[if lte IE 6.5]><iframe id="hideSelectFrame"></iframe><![endif]-->';
   win.innerHTML=winBody ;
   }

function show(){
   this.visible=true;
   this.getPageSize();
   this.getPageScroll(); 
   //this.changeSelects('hidden');
   if(this.grayOut)
       this.darken(true);
    
   this.createWindow();
  // var vidPaneID = document.getElementById('msgID'); 
   
}
function disableScreen(){
  this.visible=true;
  this.getPageSize();
  this.getPageScroll(); 
  this.darken(true);
} 

function close(r){
    this.visible=false;
    this.result=r==null?this.result:r;
    if(this.onClose)
        this.onClose(this.result,this.onCloseParams);
      
    remove(this.id);
   // this.changeSelects('visible');
    if(this.grayOut)
        this.darken(false);
}

}

var DWSWinObject=new DWSWin();;

function msgBox(title,txt,type,grayOut,event,width,height,txtAlign){
    DWSWinObject=new DWSWin();
    type=type==null?winTypes.Ok:type;
    title=title==null?culture.term('msgBoxTitle'):title;

    if(title)DWSWinObject.title=title;
    if(txt)DWSWinObject.innerHTML=txt;
    if(type!=null)DWSWinObject.winType=type;
    if(grayOut!=null)DWSWinObject.grayOut=grayOut;
    if(event)DWSWinObject.onClose=event;
    if(!isEmpty(width))DWSWinObject.width=width;    
    if(!isEmpty(height))DWSWinObject.height=height;    
    if(!isEmpty(txtAlign))DWSWinObject.contentAlign=txtAlign; 

    DWSWinObject.show();
};

var savedTarget=null; 
var dragXoffset=0;  
var dragYoffset=0;  
   
function moveHandler(e){
  if (e == null) { e = window.event } 
  if (e.button<=1){
  
     savedTarget.style.left=e.clientX-dragXoffset+'px';
     savedTarget.style.top=e.clientY-dragYoffset+'px';
    document.onmousemove=null; 
     document.onmousemove=moveHandler;
     /*setTimeout('*/
     return false;
  }
}

function cleanup(e) {
  document.onmousemove=null;
  document.onmouseup=null;

}

function dragHandler(e){
  var htype='-moz-grabbing';
  if (e == null) { e = window.event; htype='move';} 
  var target = e.target != null ? e.target : e.srcElement;
  orgCursor=target.style.cursor;
  if (target.className.indexOf("movable")!=-1) {
     savedTarget=getActiveWindow(target);
     var dwsWinBody= savedTarget;     
     target.style.cursor=htype;
     dragOK=true;
     dragXoffset=e.clientX-parseInt(dwsWinBody.style.left);
     dragYoffset=e.clientY-parseInt(dwsWinBody.style.top);
     document.onmousemove=moveHandler;
     document.onmouseup=cleanup;
     return false;
  }
  }
function getActiveWindow(elm){
do{
    if(!isEmpty(elm.className) && elm.className.indexOf('dwswin')!=-1)
        return elm;
    else
        elm=elm.parentNode;
        
}while(!isEmpty(elm))
return null;
}

document.onmousedown=dragHandler;