var newBrowser = '';

function imprimirDiv(name, gAutoPrint, descricaoSair)
{
	var name;
	if (document.getElementById != null)
	{
		var html = '<HTML>\n<HEAD>\n';
		html += "<style type=\"text/css\" media=\"print\">";
  			html += "#botao { display: none; }\n";
  			html += ".land{filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1)} \n";
		html += "</style>";
		
		html += "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n<META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\">\n";

		if (document.getElementsByTagName != null)
		{
			var headTags = document.getElementsByTagName("head");
			if (headTags.length > 0)
				html += headTags[0].innerHTML;
		}

		html += '\n</HE' + 'AD>\n<BODY clas="land">\n';

		var printReadyElem = document.getElementById(name);

		if (printReadyElem != null)
		{
			html += printReadyElem.innerHTML;
		}
		else
		{
			return;
		}

		html += "<br/><br/>";
		html += "<table id='botao' width=\"100%\"><tr><td class=\"botoesFooter\">";
		html += "<input type=\"button\" name=\"btnSair\" value=\""+descricaoSair+"\" onclick=\"window.close()\" class=\"buttonStyle\">";
		html += "</table>";

		html += '\n</BO' + 'DY>\n';
	 	html += "<HEAD>\n<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n<META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\">\n</HEAD>";		 	
		html += '</HT' + 'ML>';

		var printWin = window.open("",name, "status=yes,resizable=yes,scrollbars=yes,toolbar=no,left=10,top=30");
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
		if (gAutoPrint)
		{
			printWin.print();
		}
	}
}

function habilitarCampos(habilitarSequencial, habilitarFixo)
{
	habilitarCampos(habilitarSequencial, habilitarFixo, false);
}

function habilitarCampos(habilitarSequencial, habilitarFixo, habilitarTudo)
{
    var x = 0;
    for(x; x<document.forms[0].elements.length; x++)
    {
    	var elem = document.forms[0].elements[x];
    	if (elem.isDescricaoAssistente==undefined)
    	{
    		elem.disabled = false;
     	if (!habilitarSequencial)
     	{
     		if (elem.isSequencial!=undefined)
     		{
     			elem.disabled = true;
     		}
     	}
     	if (!habilitarFixo)
     	{
     		if (elem.isFixo!=undefined)
     		{
     			elem.disabled = true;
     		}
     	}
    	}
    	
    	if (elem.disabled && habilitarTudo)
    	{
    		elem.disabled = false;
    	}
	}
}
    
function desabilitarBotoes()
{        
	var x = 0;
    for(x; x<document.forms[0].elements.length; x++)
    {
    	var elem = document.forms[0].elements[x];
    	if (elem.type=='button')
    		elem.disabled=true;
 	}
}

function processarCheckBoxes()
{        
	var x = 0;
    for(x; x<document.forms[0].elements.length; x++)
    {
    	var elem = document.forms[0].elements[x];
	    if (elem.type=='checkbox')
	    {
	    	if (elem.checked)
	      	{
 	      		elem.value="true";
 	      	}
 	      	else
 	      	{
 	      		elem.value="false";
 	      		var input = document.createElement("input");
				input.setAttribute("type", "hidden");
				input.setAttribute("name", elem.name);
				input.setAttribute("value", "false");
				document.forms[0].appendChild(input);
			}
 	    }
    }
}
    
var agent=navigator.userAgent.toLowerCase();
var IE5 = (agent.indexOf('msie 5') != -1) || (agent.indexOf('msie 6') != -1)|| (agent.indexOf('msie 7') != -1);
var Nav4 = document.layers;
var IE4 = document.all;

function getKeyUnicode(evento)
{
	var keyUnicode;
	if (Nav4) 	
	{
		keyUnicode = evento.which;
	}
	else if (IE4) 
	{
	    keyUnicode = window.event.keyCode;
	}
	return keyUnicode;
}

function formatar(src, mascara, evento) 
{
	var i = src.value.length;
	var saida = mascara.substring(0,1);
	var texto = mascara.substring(i)
	var keyUnicode = getKeyUnicode(evento);
	if (keyUnicode > 47 && keyUnicode < 58)
	{
  		if (texto.substring(0,1) != saida) 
		{
			src.value += texto.substring(0,1);
		}
		return true;
	}
	else
	{
		return false;
	}
}

function formataMonetario(fld, milSep, decSep, e)
{
	return formataMonetario(fld, milSep, decSep, e, false);
} 

function formataMonetario(fld, milSep, decSep, e, permiteNegativo) 
{
	var sep = 0;
	
	var key = '';
	
	var i = j = 0;
	
	var len = len2 = 0;
	
	var strCheck = '0123456789';
	if (permiteNegativo)
	{
		strCheck += '-';
	}
	
	var aux = aux2 = '';
	
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	if (whichCode == 13) return true;
	
	key = String.fromCharCode(whichCode);// Valor para o código da Chave
	
	if (strCheck.indexOf(key) == -1) return false; // Chave inválida
	
	len = fld.value.length;
	
	for(i = 0; i < len; i++)
	
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	
	aux = '';
	
	for(; i < len; i++)
	
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	
	aux += key;
	
	len = aux.length;
	
	if (len == 0) fld.value = '';
	
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	
	if (len == 2) fld.value = '0'+ decSep + aux;
	
	if (len > 2) {
	
	aux2 = '';
	
	for (j = 0, i = len - 3; i >= 0; i--) {
	
	if (j == 3) {
	
	aux2 += milSep;
	
	j = 0;
	
	}
	
	aux2 += aux.charAt(i);
	
	j++;
	
	}
	
	fld.value = '';
	
	len2 = aux2.length;
	
	for (i = len2 - 1; i >= 0; i--)
	
	fld.value += aux2.charAt(i);
	
	fld.value += decSep + aux.substr(len - 2, len);
	
	}
	
	return false;

}

function formataDecimal(fld, milSep, decSep, e, permiteNegativo, casasDecimais, maxCaracteres) 
{
	if (maxCaracteres<=fld.value.length)
		return false;
		
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	
	var strCheck = '0123456789';
	if (permiteNegativo)
	{
		strCheck += '-';
	}
	
	var aux = aux2 = '';
	
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	if (whichCode == 13) 
		return true;
		
	key = String.fromCharCode(whichCode);// Valor para o código da Chave
	
	if (strCheck.indexOf(key) == -1) 
		return false; // Chave inválida
		
	if (casasDecimais<=0)
	{
		return true;
	}
	
	len = fld.value.length;
	
	for(i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep))
			break;
	aux = '';
	
	for(; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) 
			aux += fld.value.charAt(i);
	
	aux += key;
	len = aux.length;
	
	var zerosDepoisDecimal = casasDecimais - len;
	
	fld.value = '';	
	if (len<=casasDecimais)
	{
		var zeros = '';
		for (var k=0; k<zerosDepoisDecimal; k++)
		{
			zeros += '0';
		}
		fld.value = '0' + decSep + zeros + aux;
	}
	else 
	{
		aux2 = '';
		
		for (j = 0, i = len - (casasDecimais+1); i >= 0; i--) 
		{
			if (j == 3) 
			{
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		
		fld.value = '';
		
		len2 = aux2.length;
		
		for (i = len2 - 1; i >= 0; i--)
			fld.value += aux2.charAt(i);
		
		fld.value += decSep + aux.substr(len - casasDecimais, len);
	}
	return false;
}

function alterarAlturaDiv(idDiv, offset)
{
	var alturaViewport;
	if (self.innerHeight) 
	{
		// all except Explorer
		alturaViewport = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		// Explorer 6 Strict Mode
		alturaViewport = document.documentElement.clientHeight;
	}
	else if (document.body) 
	{
		// other Explorers
		alturaViewport = document.body.clientHeight;
	}
	else
	{
		alert("Browser não suportado!!");
		return;
	}
	

	var alturaDiv = alturaViewport-offset;
	document.getElementById(idDiv).style.height=alturaDiv+"px";
}

/**
Função que determina a diferença entre duas datas:
	Parâmetros:
			per [período - ex: 'd' (em dias)]
			d1  [data1 	 - ex: '01/01/2008' (string)]
			d1  [data2 	 - ex: '05/01/2008' (string)]
			
Ex: datediff('d', '01/01/2008', '05/01/2008');  [data2 - data1 (retorno = 4 dias!)]
**/
function datediff(per ,d1 ,d2) 
{
        data1 = new Date( d1.substring(6,10) ,eval(d1.substring(3,5))-1 ,d1.substring(0,2) );
        data2 = new Date( d2.substring(6,10) ,eval(d2.substring(3,5))-1 ,d2.substring(0,2) );

        var d = (data2.getTime() - data1.getTime())/1000

        switch(per)
        {
            case "yyyy": d/=12			//período em anos
            case "m": d*=12*7/365.25	//período em meses
            case "ww": d/=7				//período em semanas
            case "d": d/=24				//período em dias
            case "h": d/=60				//período em horas
            case "n": d/=60
        }

        return Math.round(d);
}

function redimensionarImagemLarguraFixa(imgObject, larguraFixa)
{
	//alert('Redimensionar Imagem');	
	var x0 = imgObject.width;
	var y0 = imgObject.height;

	//alert('Largura = '+x0);
	//alert('Altura = '+y0);

	var xF = larguraFixa;
	var yF = (y0*xF)/x0;
	
	if (x0<xF)
	{
		return;
	}
	
	//alert('Nova Altura = '+yF);
	imgObject.width = xF;
	imgObject.height = yF;		
}
