var ie4 = document.all;
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all; 

function Obj(txt){
	try{
		return document.getElementById(txt);
	}catch(e){
		status = 'Não foi encontrado o objeto ' + txt;
	}
}

function _gel(txt){
	return (document.getElementById)?document.getElementById(txt):null;
}


function htcWinDisplay(tipo,w,h,titulo,disableall,conteudo,t,l){
	try{
		if(t && l){
			eval('htcWin').style.left = l + 'px';
			eval('htcWin').style.top = t + 'px';
		}else{
			var b = new htcBrowserInfo();
			eval('htcWin').style.left = parseInt((b.screenWidth / 2) - (w / 2)) + 'px';
			eval('htcWin').style.top = (b.screenHeight / 2) - parseInt(h / 2) - 100 + 'px';
		}
		eval('htcWin').style.height = (h+40) + 'px';
		eval('htcWin').style.width = (w+20) + 'px';
		eval('htcWin_titulo').innerHTML = titulo;
		if(tipo == 'frame')
			eval('htcWin_conteudo').innerHTML = '<iframe width="'+ w +'" height="'+ h +'" src="'+ conteudo +'" scrolling="yes" frameborder="0" marginheight="0" marginwidth="0"></iframe>';
		else
			eval('htcWin_conteudo').innerHTML = conteudo;
		if(disableall)
			eval('htcAllDisabled').style.display = 'block';
		eval('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 htcPopUpWindow(URLStr, left, top, width, height, sc){
	if(sc)
		if(htcPopUpW)
			if(!htcPopUpW.closed) htcPopUpW.close();
	
	var b = new htcBrowserInfo();
	left = (b.screenWidth / 2) - (width / 2)
	top = (b.screenHeight / 2) - (height / 2)
	
	if(sc)
		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+'');
	else
		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+'');
}

function htcPopUp(name, URLStr, left, top, width, height){
	
	var b = new htcBrowserInfo();
	left = (b.screenWidth / 2) - (width / 2)
	top = (b.screenHeight / 2) - (height / 2)
	open(URLStr, name, '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+'');

}

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 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 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){
	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;}

//Funcao de formatacao de milhar e decimais

var htcMoneyMask = {
	add:function(campo){
		
		if(_gel(campo + '-div')){
		
			_gel(campo).onkeydown = function(e){
				var ie = (typeof window.ActiveXObject != 'undefined');
				var key = ie ? event.keyCode : e.which;
				
				//check key type
				if(!( ((key >= 48 && key <= 57) || (key >= 96 && key <= 105)) || ( key == 46 || key == 8 || key == 20 || key == 13 || (key >= 37 && key <= 40))	) ){ //não numerico
					if(ie){
						event.returnValue = false;
					}else{
						e.preventDefault();
					}	
				}
			}

			_gel(campo).onkeyup = function(){
				htcMoneyMask.ver(this);
				
			}
			
		}else{
			alert('DIV do campo '+ campo +' não encontrado.');
		}
	},
	ver:function(a){
		
		var dcp = 2, tstr = '', c=0, sc=0, ln=a.value.length, div = _gel(a.id+'-div');
		div.innerHTML = '';
	
		for(var i=ln;i>0;i--){
			/*if(c == dcp){
				div.innerHTML = ',' + div.innerHTML;
			}*/
			if((sc%3) == 0 && c > 1){
				div.innerHTML = '.' + div.innerHTML;
				sc = 0;
			}
			div.innerHTML = a.value.substring(i-1,i) + div.innerHTML;
			c++;
			sc++;
		}
		/*
		if(ln <= dcp){
			while(div.innerHTML.lenght < dcp){
				div.innerHTML = '0' + div.innerHTML;
			}
			div.innerHTML = '0,' + div.innerHTML;
		}
		*/
		
		div.innerHTML += ',00';
		
		if(div.innerHTML == ',00'){div.innerHTML = '0,00';}	
	}
};