//Funçoes Diversas
// SerraPlan Informática   Jan/2006
window.onerror = null;

function AddItem(Codigo,Quant,Preco,Fator,Nome,QtDisp,QtMax,PrecoKg,Tipo) {
	parent.fInvisivel.location.href='AddItemCarrinho.cfm?Codigo='+Codigo+'&Quant='+Quant+'&Preco='+Preco+'&Fator='+Fator+'&Nome='+Nome+'&QtDisp='+QtDisp+'&QtMax='+QtMax+'&PrecoKg='+PrecoKg+'&Tipo='+Tipo;
	return true;
}

function esvaziaCarrinho() {
	if (confirm('Deseja esvaziar seu carrinho de compra?')) {
		parent.fInvisivel.location.href="LimpaCarrinho.cfm?"+(new Date()).getTime();
	}
}

function Abre(url, width, height) {
	window.open(url,"_blank","resizable=no,toolbar=no,status=no,menubar=no,scrollbars=yes,width=" + width + ",height=" + height)
}

function refazTxt(val) {
	var temp = val;
	if (temp.length == 0)
		fPesq.PalavraPsq.value = "Palavra-chave";
}

function newWindow(pImagem,pProduto,pGrupo) {
	if (window.imgWindow != null) {
		window.imgWindow.close();
	}
	var hwin = 300;
	var wwin = 350;
	if (pGrupo == 3) {
		hwin = 390;
		wwin = 510;	
	}
	imgWindow = window.open('','imgWin','width='+wwin+',height='+hwin+',top=60,left=30');
	imgWindow.document.write("<html><head><title>Foto do Produto</title></head><body><form name='imgForm'>")
	imgWindow.document.write("<font face='verdana' style='font-size:11' color='006262'><b><center>"+pProduto+"</center></b></font><br>")
	imgWindow.document.write("<center><img name='Logo' src="+pImagem+"></center>")
	imgWindow.document.write("<p><center><img name='btnClose' src='img/Fechar.jpg' onClick='window.close()'></center>")
	imgWindow.document.write("</form></body></html>")
	
	imgWindow.focus();
}

function InicializarIndices() {
	if (document.CargaInicial==null) {
		document.CargaInicial=false; // Seta para só fazer uma vez por documento
		var ctrlAnterior=null;
		var IndAnt=0;
		for ( var i=0; i<document.forms[0].elements.length;i++)	{
			var e=document.forms[0].elements[i];
			if ( e.type!="hidden" && e.type!="image" ) {
				if (ctrlAnterior != null) ctrlAnterior.IndicePosterior=i;
				ctrlAnterior=e;
				e.Indice=i;
				e.IndiceAnterior=IndAnt;
			}
		}
	}
}
	
// Colocar o foco em determinado campo
function SetarFoco(ind) {
	InicializarIndices();
	if ( isNaN(ind) && document.forms[0].elements[ind].type!="hidden" )
		document.forms[0].elements[ind].focus();
	else
		for (;ind<document.forms[0].elements.length;ind++)
			if (document.forms[0].elements[ind].type!="hidden")
				break;
		if (ind<=document.forms[0].elements.length)
			document.forms[0].elements[ind].focus();
}

// Limpar o conteúdo do(s) campo(s)
function LimparCampo(ind) {	// Para -1, limpa todos os elementos
	if (isNaN(ind)) // Limpa pelo nome
		document.forms[0].elements[ind].value="";
	else if (ind != -1) // Limpa o elemento "ind" ( só considera "text" e "password" )
		for (var i=ind; i < document.forms[0].elements.length;i++ )
			if(document.forms[0].elements[i].type=="text" || document.forms[0].elements[i].type=="password") { // Só limpa campo "text"
				document.forms[0].elements[i].value="";
				break;
			}
	else // Limpa todos os elementos "text" e "password"
		for ( var i=0; i < document.forms[0].elements.length; i++)
			if ( document.forms[0].elements[i].type=="text" || document.forms[0].elements[i].type=="password")
				document.forms[0].elements[i].value="";
}
	
// Verificar qual navegador
function QualNavegador() {
	var s = navigator.appName
	if(s == "Microsoft Internet Explorer") return "IE";
	else if ( s == "Netscape" ) return "NE";
	else return "";
}
	
// Verificar qual a versão do navegador
function QualVersao() {
	var s = navigator.appVersion;
	if ( QualNavegador() == "IE" ) {
		var i = s.search("MSIE");
		s=s.substring(i+5);
		i=s.search(".");
		return parseInt(s.substring(0,i+1));
	}
	else if ( QualNavegador() == "NE" )	return parseInt(s.substring(0,1));
	else return 0;
}
	
// Setar o evento
function SetarEvento(ctrl, Tam, Tipo, AutoSkip) { // Filtra navegadores conhecidos
	var s = QualNavegador();
	if (s.length==0) return;
	if (s=="IE" && QualVersao()>6) return;
	if (s=="NE" && QualVersao()>4) return;
	if (ctrl.onkeypress==null) {
		if (AutoSkip==null) AutoSkip=true;
		if (Tipo!=null)	Tipo.toUpperCase();
		ctrl.Tam=Tam;
		ctrl.Tipo=Tipo;
		ctrl.AutoSkip=true;
		ctrl.Saltar=false;
		InicializarIndices();
		ctrl.onkeypress=ValidarTecla;
		if (QualNavegador()=="IE" && QualVersao()>=5) ctrl.onkeyup=SaltarCampo;
	}
}
	
function SaltarCampo(ctrl) {
	if (ctrl==null)	ctrl=this;
	if ( ctrl.AutoSkip && ctrl.Saltar)
		if (ctrl.Saltar) {
			ctrl.Saltar=false;
			if ( ctrl.IndicePosterior != null ) SetarFoco(ctrl.IndicePosterior);
		}
}
	
// Fazer o salto de campo
function ValidarTecla(evnt) {
	var tk;
    var c;
	// Recebe a tela pressionada
	tk = ( (QualNavegador()=="IE") ? event.keyCode : evnt.which);
    c=String.fromCharCode(tk);
	c=c.toUpperCase();
	// Só aceita teclas alfanuméricas. Não aceita teclas de controle
	//if (tk == 13) enviaFormLogin(document.login.PROD_I[document.login.PROD_I.selectedIndex], document.login.USERID, document.login.AGENCIA, document.login.CONTA);
    if(tk<32) return true;
	if (tk>127)	return false;

	switch (this.Tipo) {
	case "D":
		if (c<"0" || c>"9") {
            //alert("Quantidade inválida.");
       		return false;
       	}
		break;
	case "N":
		//if ((c<"0" || c>"9") && (c!="." && c!=","))
		pont = ".";
		if ((c<"0" || c>"9") && (c!=pont))
			return false;
		//if ((c==",") && ((this.value.search(",")>-1) || (this.value.length==0)))
		if ((c==pont) && ((this.value.indexOf(pont)>-1) || (this.value.length==0)))
			return false;
		if ((c==pont) && (this.value.length==0))
			return false;			
		break;
	case "C":
		if ( c<"A" || c>"Z" ) return false;
		break;
	default:
		break;
	}
	this.Saltar=(this.value.length==this.Tam-1);
	if(((QualNavegador()=="IE") && QualVersao()<5) || (QualNavegador()!="IE")) SaltarCampo(this);
	return true;
}
	
function reloadPage(init) {
	c = navigator.appName
  	if (init==true) with (navigator) {
    	if (c=="Netscape") { document.pgW=innerWidth; document.pgH=innerHeight; onresize=reloadPage; }
  	} else if (innerWidth!=document.pgW || innerHeight!=document.pgH) location.reload();
}
reloadPage(true);
