var ie4 = document.all;
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all; 

function Obj(txt){
	try{
		if(arguments.length == 2){
			if(arguments[1] == 'name')
				return document.getElementsByName(txt);
			else
				return document.getElementById(txt);
		}else{
			return document.getElementById(txt);
		}
	}catch(e){
		status = 'Não foi encontrado o objeto ' + txt;
	}
}

function htcWinDisplay(tipo,w,h,titulo,disableall,conteudo,t,l){
	try{
		var b = new htcBrowserInfo();
		if(t && l){
			Obj('htcWin').style.left = l + 'px';
			Obj('htcWin').style.top = t + 'px';
		}else{
			if(Request.QueryString('subWin') == 'true'){
				Obj('htcWin').style.left = '10px';
				Obj('htcWin').style.top = '10px';
			}else{	
				Obj('htcWin').style.left = parseInt((b.screenWidth / 2) - (w / 2)) + 'px';
				Obj('htcWin').style.top = ((b.posTop + (b.screenHeight / 2)) - parseInt(h / 2)) - 100 + 'px';
			}
			//Obj('htcWin').style.left = parseInt((b.screenWidth / 2) - (w / 2)) + 'px';
			//Obj('htcWin').style.top = (b.screenHeight / 2) - parseInt(h / 2) - 100 + 'px';
		}
		Obj('htcWin').style.height = (h+40) + 'px';
		Obj('htcWin').style.width = (w+20) + 'px';
		Obj('htcWin_titulo').innerHTML = titulo;
		if(tipo == 'frame'){
			(conteudo.indexOf('?') == -1)?conteudo+='?subWin=true':conteudo+='&subWin=true';
			Obj('htcWin_conteudo').innerHTML = '<iframe id="htcWinFrame" width="'+ w +'" height="'+ h +'" src="'+ conteudo +'" scrolling="yes" frameborder="0" marginheight="0" marginwidth="0"></iframe>';
		}else{
			Obj('htcWin_conteudo').innerHTML = conteudo;
		}
		if(disableall){
			Obj('htcAllDisabled').style.height = b.fullHeight + 'px';
			Obj('htcAllDisabled').style.display = 'block';
		}
		Obj('htcWin').style.display = 'block';
	}catch(e){
		status = 'ERRO HWD- ' + e.description;
	}
}

var htcPopUpW=0;
function closehtcPopUp(){
	if(htcPopUpW){
		if(!htcPopUpW.closed){
			htcPopUpW.close();
			htcPopUpW=0;
		}
	}
}

function htcPopUWin(URLStr, left, top, width, height, sc){
	if(htcPopUpW)
		if(!htcPopUpW.closed) htcPopUpW.close();
	
	var b = new htcBrowserInfo();
	left = (b.screenWidth / 2) - (width / 2)
	top = (b.screenHeight / 2) - (height / 2)
	
	htcPopUpW = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

//open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbar=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function htcPopUp(name, URLStr, left, top, width, height){
	
	var b = new htcBrowserInfo();
	left = (b.screenWidth / 2) - (width / 2)
	top = (b.screenHeight / 2) - (height / 2)
	htcPopUpW = open(URLStr, name, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbar=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

}

function htcBrowserInfo(){
	
	this.name 		= navigator.appName;
	this.codename 	= navigator.appCodeName;
	this.version 	= navigator.appVersion.substring(0,4);
	this.platform 	= navigator.platform;
	this.javaEnabled 	= navigator.javaEnabled();
	this.screenWidth 	= screen.width;
	this.screenHeight 	= screen.height;

	var Xz = 0, Py = 0, Yz = 0;
	if(navigator.appName.indexOf('Internet Explorer') != -1){
		try{
			Yz = document.body.scrollHeight;
			Xz = document.body.scrollWidth;
			Py = document.body.scrollTop;
		}catch(e){
			//alert('algo falhou');
		}
	}else{
		Yz = self.pageYOffset;
		Xz = self.pageXOffset;
	}
	this.fullHeight = (Yz == 0)?this.screenHeight:Yz;
	this.fullWidth = (Xz == 0)?this.screenWidth:Xz;
	this.posTop = Py;
}
var bi = new htcBrowserInfo();


function htcOpenPic(url,title){

	htcPopUpW=window.open(url,'','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
	htcPopUpW.document.write('<html><head><title>'+ title +'</title>')
	htcPopUpW.document.write('<body oncontextmenu="return false;" onLoad="self.resizeTo(document.images[0].width,document.images[0].height+30);" topmargin="0" leftmargin="0"><img onmoveend="return false;" src="'+url+'"></body></html>')
	htcPopUpW.location.reload();
	htcPopUpW.focus();

}

function moveXbySlicePos (x, img) { 
	if (!document.layers) {
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;
		var par = img;
		var lastOffset = 0;
		while(par){
			if( par.leftMargin && ! onWindows ) x += parseInt(par.leftMargin);
			if( (par.offsetLeft != lastOffset) && par.offsetLeft ) x += parseInt(par.offsetLeft);
			if( par.offsetLeft != 0 ) lastOffset = par.offsetLeft;
			par = macIE45 ? par.parentElement : par.offsetParent;
		}
	} else if (img.x) x += img.x;
	return x;
}

function moveYbySlicePos (y, img) {
	if(!document.layers) {
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;
		var par = img;
		var lastOffset = 0;
		while(par){
			if( par.topMargin && !onWindows ) y += parseInt(par.topMargin);
			if( (par.offsetTop != lastOffset) && par.offsetTop ) y += parseInt(par.offsetTop);
			if( par.offsetTop != 0 ) lastOffset = par.offsetTop;
			par = macIE45 ? par.parentElement : par.offsetParent;
		}		
	} else if (img.y >= 0) y += img.y;
	return y;
}

/*controle da janela*/
var _gDrag = true;
var htcWinCorX = 0;
var htcWinCorY = 0;
var htcWinCurrent = '';
function htcWindowAn(w,h,t){
	if(t == 1 || t == 3)
		(w <= 400)?htcWin.style.width = w:htcWin.style.width = '400px';
	if(t == 2 || t == 3)
		(h <= 445)?htcWin.style.height = h:htcWin.style.height='445px';
	if(w <= 400 || h <= 445)
		setTimeout('htcWindowAn('+(w+10)+','+(h+10)+','+t+')',1);
}
function htcStartWinDrag(lay){
	if(_gDrag){
		htcWinCorX = 0;
		htcWinCorY = 0;
		htcWinCurrent = lay;
		document.onmousemove = htcWinDrag;
		document.onmouseup = htcStopWinDrag;
		document.onselectstart = cancelSelect;
		document.body.style.cursor = 'move';
	}
}
function htcStopWinDrag(){
	document.onmousemove = null;
	document.onmouseup = null;
	//document.onselectstart = startSelect;
	htcWinCurrent = '';
	document.body.style.cursor = 'auto';
}

function htcWinDrag(e){
	var mm = document.getElementById(htcWinCurrent);
	if(ns4||ns6){
		x = e.pageX;
		y = e.pageY;
	}else{
		x = event.x + document.body.scrollLeft;
		y = event.y + document.body.scrollTop;
	}
	(isNaN(x))?x = 0:x=x;
	(isNaN(y))?y = 0:y=y;
	var left = parseInt(mm.style.left.replace('px',''));
	var top = parseInt(mm.style.top.replace('px',''));
	(isNaN(left))?left = 0:left=left;
	(isNaN(top))?top = 0:top=top;
	if(htcWinCorX == 0 || htcWinCorY == 0){
		(left > x)?htcWinCorX = parseInt(left - x):htcWinCorX = parseInt(x - left);
		(top > y)?htcWinCorY = parseInt(top - y):htcWinCorY = parseInt(y - top);
		status = 'X:'+ htcWinCorX + ' | Y:'+ htcWinCorY;
	}
	//status = (parseInt(htcWinCorX) + parseInt(x)) + ' | ' + (parseInt(htcWinCorY) + parseInt(y));
	mm.style.left = x - htcWinCorX;
	mm.style.top = y - htcWinCorY;
	return true;
}
function cancelSelect(e){return false;}
function startSelect(e){return true;}

function findObj(theObj, theDoc){
	var p, i, foundObj;
	if(!theDoc) theDoc = document;
	if( (p = theObj.indexOf("?")) > 0 && parent.frames.length){
		theDoc = parent.frames[theObj.substring(p+1)].document;
		theObj = theObj.substring(0,p);
	}
	if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
	for(i=0; !foundObj && i < theDoc.forms.length; i++) 
		foundObj = theDoc.forms[i][theObj];
	for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
		foundObj = findObj(theObj,theDoc.layers[i].document);
	if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
	return foundObj;
}

function showHideLayers(){ 
	var i, visStr, obj, args = showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3){
		if((obj = findObj(args[i])) != null){
			visStr = args[i+2];
			if(obj.style){
				obj = obj.style;
				if(visStr == 'show') visStr = 'visible';
				else if(visStr == 'hide') visStr = 'hidden';
			}
			obj.visibility = visStr;
		}
	}
}

function moveLayerToMouseLoc(theLayer, offsetH, offsetV){
	var obj;
	if ((findObj(theLayer)) != null){
		if (document.layers){//NS
			document.onMouseMove = getMouseLoc;
			obj = document.layers[theLayer];
			obj.left = mLoc.x +offsetH;
			obj.top  = mLoc.y +offsetV;
		}else if (document.all){//IE
			getMouseLoc();
			obj = document.all[theLayer].style;
			obj.pixelLeft = mLoc.x +offsetH;
			obj.pixelTop  = mLoc.y +offsetV;
		}
		showHideLayers(theLayer,'','show');
	}
}
// get mouse location
function Point(x,y) {  this.x = x; this.y = y; }
mLoc = new Point(-500,-500);

function getMouseLoc(e){
	if(!document.all){//NS
		mLoc.x = e.pageX;
		mLoc.y = e.pageY;
	}else{//IE
		mLoc.x = event.x + document.body.scrollLeft;
		mLoc.y = event.y + document.body.scrollTop;
	}
	return true;
}
//NS init:
if(document.layers){ document.captureEvents(Event.MOUSEMOVE); document.onMouseMove = getMouseLoc;}

function _gel(a){ //getElementById
	return document.getElementById?document.getElementById(a):null;
}

function _geln(a){ //getElementsByName
	return document.getElementsByName?document.getElementsByName(a):null;
}

function _geltn(a){
	if(a == "*" && document.all)
		return document.all;
	return document.getElementsByTagName?document.getElementsByTagName(a):[]
}
function _gopos(a) { 
	var p = {x:0,y:0};
	if (!document.layers) {
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;
		var par = a, l = {x:0,y:0};
		while(par){if( par.leftMargin && ! onWindows ) p.x += parseInt(par.leftMargin);if( ((par.offsetLeft != l.x) && par.offsetLeft) ) p.x += parseInt(par.offsetLeft); if( par.offsetLeft != 0 ) l.x = par.offsetLeft;par = macIE45 ? par.parentElement : par.offsetParent;}
		var par = a;
		while(par){if( par.topMargin && ! onWindows )	p.y += parseInt(par.topMargin);if( ((par.offsetTop != l.x) && par.offsetTop) )	p.y += parseInt(par.offsetTop);	if( par.offsetTop != 0 ) l.y = par.offsetTop; par = macIE45 ? par.parentElement : par.offsetParent;}
	}else if(a.x && a.y){
		p.x += a.x;
		p.y += a.y;
	}
	return p;
}

var drag_control={obj:null,lastMouseX:0,lastMouseY:0,
	start:function(a){
		obj = a;
		var a = drag_control.fixE(a), b = obj.parentNode;
		drag_control.lastMouseX = a.clientX;
		drag_control.lastMouseY = a.clientY;
		var p = _gopos(b);
		b.style.top = p.y + 'px';
		b.style.left = p.x + 'px';
		document.onmouseup	 = drag_control.end;
		document.onmousemove = drag_control.drag;
		document.onselectstart = function(){return false}
		return false
	},
	drag:function(a){
		var a = drag_control.fixE(a), b = obj.parentNode;
		if(a.which == 0){return drag_control.end()}
		var c = a.clientY, d = a.clientX;
		(!drag_control.lastMouseX)?drag_control.lastMouseX = d:null;
		(!drag_control.lastMouseY)?drag_control.lastMouseY = c:null;
		if(drag_control.lastMouseX == d && drag_control.lastMouseY == c){return false}
		var e = parseInt(b.style.top), f = parseInt(b.style.left),g,h;
		g = f + d - drag_control.lastMouseX;
		h = e + c - drag_control.lastMouseY;
		b.style.left 	= g + 'px';
		b.style.top		= h + 'px';
		drag_control.lastMouseX = d;
		drag_control.lastMouseY = c;
		return false
	},
	end:function(){var b = obj.parentNode;document.onmouseup = null;document.onmousemove = null;document.onselectstart = null;},
	fixE:function(a){if(typeof a == "undefined")a = window.event;if(typeof a.layerX == "undefined")a.layerX = a.offsetX;if(typeof a.layerY == "undefined")a.layerY = a.offsetY;if(typeof a.which == "undefined")a.which = a.button;return a}
};



var win_control = {
	cont:0,
	wins:new Array(),
	initWin:function(a,b,c,d,e,f,g,h,i,j,k,l){
		
		if(!a){return false}
		if(!b){b='&nbsp;'}
		if(!e){e=200}
		if(!f){f=200}
		if(!h){h='top'}

		var win=document.createElement("DIV"), header=document.createElement("DIV"), content=document.createElement("DIV");
		win.className 	  = "htcPanel";
		header.className  = "htcPanelHeader";
		content.className = "htcPanelContent";

		if(_gel(l)){
			if(a=='frame'){
				_gel(l).childNodes[1].childNodes[0].src=d;
				_gel(l).childNodes[1].childNodes[0].height=(f);
			}
			_gel(l).style.display = 'block';
			return;
		}else{
			(l)?win.id=l:win.id = 'HPNL'+win_control.cont++;
			win.style.width=(a!='frame')?(e)+'px':(e+15)+'px';
		}
		
		if(h == 'center'){
			var sw=screen.width, sh=screen.height,dif=150;
			if(document.all){
				var st = document.body.scrollTop;
				win.style.left = ((sw/2)-(e/2))+'px';
				win.style.top = ((st+((sh/2)-(e/2)-dif))<0) ? (st+((sh/2)-(e/2)))+'px' : (st+((sh/2)-(e/2)-dif))+'px';
			}else{
				win.style.left = ((sw/2)-(e/2))+'px';
				win.style.top = (((sh/2)-(f/2)-dif)<0)?((sh/2)-(f/2))+'px':((sh/2)-(f/2)-dif)+'px';
			}
		}else{
			win.style.top = (i)?i+'px':'5px';
			win.style.left = (j)?j+'px':'5px';
		}
		win.style.zIndex = 150 + win_control.cont;
		win.appendChild(header);
		win.appendChild(content);
		
		var title=document.createElement("DIV"), option=document.createElement("DIV");
		title.className = 'htcPanelTitle';
		title.innerHTML = b;

		option.className = 'htcPanelOptions';
		if(c){
			c = c.split(',');
			for(var i = 0;i < c.length;i++){
				switch(c[i]){
					case 'size':
						option.innerHTML += '<div class="htcPanelButton"><a href="javascript:void(0);" onclick="win_control.sizeWin(\''+ win.id +'\')">_</a></div>';
						break;
					case 'close':
						option.innerHTML += '<div class="htcPanelButton"><a href="javascript:void(0);" onclick="win_control.closeWin(\''+ win.id +'\')">X</a></div>';
						break;
					case 'option':
						if(g)
							option.innerHTML += '<div class="htcPanelButton"><a href="javascript:void(0);" onclick="win_control.optionWin(this,\''+ win.id +'\')">V</a></div>';
						break;
				}
			}
		}
		(a!='frame')?content.innerHTML=d:content.innerHTML='<iframe style="width:99.9%;" height="'+ (f) +'" src="'+ d +'" scrolling="auto" frameborder="0" marginheight="0" marginwidth="0" allowtransparency="no"></iframe>';
		
		content.style.height = (f) + 'px';

		header.appendChild(title);
		header.appendChild(option);
		if(k){
			header.onmousedown = function(){
				if(_gel(this.parentNode.id+'_MNU')!=null){_gel(this.parentNode.id+'_MNU').style.display='none'};
				drag_control.start(this)
			}
		}

		win_control.wins.push({win:win,opt:g})
		document.getElementsByTagName('body')[0].appendChild(win);
		
	},
	closeWin:function(obj){
		if(obj != 'all'){
			_gel(obj).style.display = 'none';
			var f = true;
			for(var i=0;i<win_control.wins.length;i++){if(_gel(win_control.wins[i].win.id).style.display != 'none'){f = false}}
			if(f){htcWinCloseandEnable()}
		}else{
			for(var i=0;i<win_control.wins.length;i++){_gel(win_control.wins[i].win.id).style.display = 'none'}
		}
	},
	sizeWin:function(obj){
		if(_gel(obj).childNodes[1].style.display == 'none')
			_gel(obj).childNodes[1].style.display = 'block';
		else
			_gel(obj).childNodes[1].style.display = 'none';
	},
	optionWin:function(btn,obj){
		if(win_control.wins.length == 0){return false}
		var opt = null,title,click,url;
		for(var i=0;i<win_control.wins.length;i++){if(win_control.wins[i].win.id==obj&&win_control.wins[i].opt){opt=win_control.wins[i].opt}}
		if(opt != null){
			if(_gel(obj+'_MNU') != null){
				var menu=_gel(obj+'_MNU'), p=_gopos(btn);
				menu.style.left = p.x + 'px';
				menu.style.top = p.y + 'px';
				menu.style.display = 'block';
			}else{
				var menu=document.createElement("DIV")
				menu.id=obj+'_MNU'
				menu.className='htcPanelMenu';
				var content='<table>'
				for(var i=0;i<opt.length;i++){
					(opt[i].url)?url=opt[i].url:url='javascript:void(0);';
					(opt[i].click)?click='onclick="'+ opt[i].click +'"':click='';
					(opt[i].title)?title='title="'+ opt[i].title +'"':title='';
					content+='<tr><td class="htcPanelMenuItem"><a href="'+ url +'" '+ click +' '+ title +'>'+ opt[i].label +'</a></td></tr>';
				}
				menu.innerHTML=content+'</table>';
				var p = _gopos(btn);
				menu.style.left = (p.x) + 'px';
				menu.style.top = (p.y) + 'px';
				menu.style.zIndex = _gel(obj).style.zIndex + 200;
				menu.onmouseout = function(){
					this.tmr = setTimeout('_gel(\''+ this.id +'\').style.display =\'none\'',1000);
				}
				menu.onmouseover = function(){
					clearTimeout(this.tmr);
				}
				document.body.appendChild(menu);
			}
		}
	}
};

function htcWinOpenandDisable(){
	if(document.all){
		for(var i= 0;i<document.frames.length;i++){
			var e = document.frames[i].document.getElementsByTagName('SELECT');
			for(var j=0;j<e.length;j++){e[j].style.visibility='hidden'};
		}
		var e = _geltn('SELECT');
		for(var i=0;i<e.length;i++){e[i].style.visibility='hidden'};
		var documentHeight = document.documentElement.clientHeight -1;
		var documentWidth = document.documentElement.clientWidth -1;
		
		if(navigator.appVersion.substring(0,3) == '4.0'){
			documentHeight = document.body.clientHeight -1;
			documentWidth = document.body.clientWidth -1;
		}
		Obj('htcAllDisabled').style.height = documentHeight + 'px';
		Obj('htcAllDisabled').style.width = documentWidth + 'px';
	}
	Obj('htcAllDisabled').style.display = 'block';
}

function htcWinCloseandEnable(tipo){
	if(document.all){
		for(var i= 0;i<document.frames.length;i++){
			var e = document.frames[i].document.getElementsByTagName('SELECT');
			for(var j=0;j<e.length;j++){e[j].style.visibility='visible'};
		}
		var e = _geltn('SELECT');
		for(var i=0;i<e.length;i++){e[i].style.visibility='visible'};
	}
	if(tipo){if(tipo=='pnl'){win_control.closeWin('all')}}
	Obj('htcAllDisabled').style.display = 'none';
}

function htcPanel(a,b,c,d,e,f,g,h,i,j,k,l){
	win_control.initWin(a,b,c,d,e,f,g,h,i,j,k,l);
}