//==================================
function afFinishAction(data,ok,bad,type){
	try{
		var dados = eval(data);
		if(dados){
			if( (dados[0].id != 0 && arguments[arguments.length-1] == 4) || (dados[0].id == 0 && arguments[arguments.length-1] != 4) ){
				parent.htcWinCloseandEnable('pnl');
				if(!arguments.length > 3){
					eval(arguments[4]).innerHTML = bad;
				}else{
					if(window.opener)
						(type == 'winstd')?window.opener.afDialog(bad,'Aviso!','btOkOnly'):alert(bad);
					else
						(type == 'winstd')?parent.afDialog(bad,'Aviso!','btOkOnly'):alert(bad);
				}
			}else{
				afGridReload();
				parent.htcWinCloseandEnable('pnl');
				/*if(window.opener)
					(type == 'winstd')?window.opener.afDialog(ok,'Aviso!','btOkOnly'):alert(ok);
				else
					(type == 'winstd')?parent.afDialog(ok,'Aviso!','btOkOnly'):alert(ok);*/
			}
		}else{
			status = 'ERRO \n- Formato de invalido';
		}
	}catch(e){
		status = 'ERRO - FA \n- ' + e.description;
	}
}

function afGridCallBack(dg,af,fkey,state,key,tipo,acao,w,h,dados){
	var params = '';
	params += 'dgID='+dg;
	params += '&'+ af +'_acao='+state;
	params += '&afNome='+af;
	params += '&afGrid=true';
	params += '&keyValue='+key;
	params += '&forKeyValue='+fkey;
	switch(tipo){
		case 'popup':
			if(acao.indexOf('?') != -1)
				htcPopUp('pop_'+af,acao+'&'+params, 0, 0, w, h);
			else
				htcPopUp('pop_'+af,acao+'?'+params, 0, 0, w, h);
		break;
		case 'htcwin':
			var b = new htcBrowserInfo();
			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.screenHeight / 2) - parseInt(h / 2) - 100 + 'px';
			}
			Obj('htcWin').style.height = (h+40) + 'px';
			Obj('htcWin').style.width = (w+20) + 'px';
			Obj('htcWin').style.display = 'block';
			(acao.indexOf('?') != -1)?acao = acao+'&'+params:acao = acao+'?'+params;
			Obj('htcAllDisabled').style.display = 'block';
			Obj('htcAllDisabled').style.height = b.fullHeight + 'px';
			Obj('htcWin_conteudo').innerHTML = '<iframe width="'+w+'" height="'+h+'" src="'+acao+'" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" allowtransparency="no"></iframe>'
			Obj('htcWin_titulo').innerHTML = '';
		break;
		case 'winstd':
			htcWinOpenandDisable();
			(acao.indexOf('?') != -1)?acao = acao+'&'+params:acao = acao+'?'+params;
			htcPanel('frame','&nbsp;','close',acao,w,h,null,'center',0,0,true);
			break;
			
	}
	
}

function afDialog(mensagem,titulo,acao,pos,neg,h,w){
	var valid = false;
	var botoes = '';
	(neg && neg != 'undefined')?neg+=',htcWinCloseandEnable(\'pnl\');':neg='htcWinCloseandEnable(\'pnl\');';
	(pos && pos != 'undefined')?pos+=',htcWinCloseandEnable(\'pnl\');':pos='htcWinCloseandEnable(\'pnl\');';
	switch(acao){
		case 'btOkOnly':
			valid = true;
			botoes = '<input type="button" value=" OK " onClick="'+ pos +'" id="htcDialogButton">';
			break;
		case 'btOkCancel':
			valid = true;
			botoes = '<input type="button" value=" OK " onClick="'+ pos +'" id="htcDialogButton">';
			botoes += ' &nbsp; &nbsp; &nbsp; <input type="button" value=" Cancel " onClick="'+ neg +'" id="htcDialogButton">';
			break;
		case 'btYesNo':
			valid = true;
			botoes = '<input type="button" value=" Sim " onClick="'+ pos +'" id="htcDialogButton">';
			botoes += ' &nbsp; &nbsp; &nbsp; <input type="button" value=" Não " onClick="'+ neg +'" id="htcDialogButton">';
			break;
	}
	if(valid){
		var corpo = '<table width="100%" border="0" cellspacing="1" cellpadding="2">' + 
					'<tr><td id="htcDialogText">'+ mensagem +'&nbsp;</td></tr>' + 
					'<tr><td id="htcDialogText">&nbsp;</td></tr>' + 
					'<tr><td id="htcDialogText">'+ botoes +'</td></tr>' + 
					'</table>'
		
		if(!w){w=250}
		if(!h){h=50}
		//TESTE
		htcPanel('conteudo',titulo,'',corpo,w,h,null,'center');
		htcWinOpenandDisable();		
	}
}


function getPageSize(){
		
	 var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

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 arrayPageSize = getPageSize();
	Obj('htcAllDisabled').style.height = arrayPageSize[1] + 'px';
	//Obj('htcAllDisabled').style.width = (arrayPageSize[0]-1) + 'px';
	Obj('htcAllDisabled').style.display = 'block';
}


function htcWinCloseandEnable(tipo){
	if(Obj('htcWin').style.display == 'block'){Obj('htcWin').style.display = 'none'}
	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 afGridReload(){
	if(window.opener){
		window.opener.renewAllGrids(Request.QueryString('dgID'));
		setTimeout('self.close()',500);
	}else if(parent.dgaID){
		parent.renewAllGrids(Request.QueryString('dgID'));
		parent.htcWinCloseandEnable();
	}else if(dgaID){
		renewAllGrids(Request.QueryString('dgID'));
		htcWinCloseandEnable();
	}else{
		history.go(-1);
	}

}



