var  req = null;
try {
	req = new XMLHttpRequest();
} catch(trymicrosoft) {
	try { 
		req = new ActiveXObject("Msxml2.XMLHTTP"); 
	} catch(othermicrosoft) {
		try {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(failed) {
			req = null;
		}
	}
}

function criaXMLHttp() {
 if (typeof XMLHttpRequest != "undefined") {
    return new XMLHttpRequest();
 } else if (window.ActiveXObject){
    var versoes = ["MSXML2.XMLHttp.5.0",
    "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0",
    "MSXML2.XMLHttp", "Microsoft.XMLHttp"
    ];
 }
 for (var i = 0; i < versoes.length; i++){
    try{
        return new ActiveXObject(versoes[i]);
    }catch (e) {}
 }
 throw new Error("Seu browser nao suporta AJAX");
}

function muda_empresa(campo){
	vempresa=campo.value;
	xmlhttp = criaXMLHttp();
	xmlhttp.open("POST","scripts/ajax.php?funcao=muda_empresa&empresa="+vempresa,true);
		xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			campo.value = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null)
}

function tecla_enter(oEvent){
  var oEvent = (oEvent)? oEvent : event;
  var oTarget =(oEvent.target)? oEvent.target : oEvent.srcElement;
  if(oEvent.keyCode==13)
    oEvent.keyCode = 9;
  if(oTarget.type=="text" && oEvent.keyCode==13)
    //return false;
    oEvent.keyCode = 9;
  if (oTarget.type=="radio" && oEvent.keyCode==13)
    oEvent.keyCode = 9;
}

function busca_cliente(repres){
	f1=document.telapedidos;
	vcnpj=f1.cnpj_pedido.value;
	document.getElementById('nome_pedido').value= "Aguarde ...";
	xmlhttp = criaXMLHttp();
	xmlhttp.open("GET","scripts/ajax.php?funcao=busca_cliente&valor="+vcnpj+"&repres="+repres,true);
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			retorno=eval(xmlhttp.responseText);
			document.getElementById('nome_pedido').value = retorno[1];
			if (retorno[0]=='erro') {
				SetFocus('cnpj_pedido');
			}
		}
	}
	xmlhttp.send(null)
}

function somente_numero(campo,ponto){
	var digits="0123456789";
	if (ponto==true) {
		digits="0123456789"+".";
	}
	var campo_temp ;
	for (var i=0;i<campo.value.length;i++){
		campo_temp=campo.value.substring(i,i+1) ;
		if (digits.indexOf(campo_temp)==-1){
			campo.value = campo.value.substring(0,i);
			break;
		}
	}
}

function formata_data(campo,event){
	var key=event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (key!=8) {
		var vid=campo.value;
		if ((vid.length==2) || (vid.length==5))	{
			var vid=vid+'/';
		}
		campo.value=vid;
	}
}

function validar_data(campo){
	vdata=campo.value;
	xmlhttp = criaXMLHttp();
	xmlhttp.open("GET","scripts/ajax.php?funcao=validar_data&data="+vdata,true);
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			campo.value = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null)
}

function busca_produto(pedido){
	f1=document.itenspedido;
	vcodigo=f1.citem.value;
	if (vcodigo!=''){
		document.getElementById('descricao_item').value= "Aguarde ...";
		xmlhttp = criaXMLHttp();
		xmlhttp.open("GET","scripts/ajax.php?funcao=busca_produto&valor="+vcodigo+"&pedido="+pedido,true);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				retorno=eval(xmlhttp.responseText);
				document.getElementById('descricao_item').value = retorno[1];
				document.getElementById('prc_item').value = retorno[2];
				document.getElementById('ipi_item').value = retorno[3];
				if (retorno[0]=='erro') {
					SetFocus('citem');
				}
			}
		}
		xmlhttp.send(null)
	}
}

function calc_produto(pedido){
	f1=document.itenspedido;
	vqtd=f1.qtditem.value;
	vdes=f1.descitem.value;
	vcod=f1.citem.value;
	if (vcod!=''){
		xmlhttp = criaXMLHttp();
		xmlhttp.open("GET","scripts/ajax.php?funcao=calc_item&qtd="+vqtd+"&des="+vdes+"&cod="+vcod+"&pedido="+pedido,true);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				retorno=eval(xmlhttp.responseText);
				document.getElementById('tot_item').value = retorno[0];
				document.getElementById('valipi_item').value = retorno[1];
			}
//			if ((vqtd=='') && (vdes!='Aguarde...')){
//				SetFocus('qtditem');
//			}
		}
		xmlhttp.send(null)
	}
}

function checa_qtd(){
	f1=document.itenspedido;
	vqtd=f1.qtditem.value;
	if (vqtd=='') {
		SetFocus('qtditem');
	}
}

function retiraAcento(campo){
	vcampo=campo.value;
	xmlhttp = criaXMLHttp();
	xmlhttp.open("GET","scripts/ajax.php?funcao=retiraacento&campo="+vcampo,true);
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			campo.value = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null)
}

function copiaendereco(){
	f1=document.telaclientes;
	var vendereco=f1.ef.value;
	var vbairro=f1.bf.value;
	var vcidade=f1.cf.value;
	var vestado=f1.sf.value;
	var vcep=f1.pf.value;
	f1.ee.value=vendereco.toUpperCase();
	f1.ep.value=vendereco.toUpperCase();
	f1.be.value=vbairro.toUpperCase();
	f1.ce.value=vcidade.toUpperCase();
	f1.se.value=vestado.toUpperCase();
	f1.pe.value=vcep.toUpperCase();
	f1.bp.value=vbairro.toUpperCase();
	f1.cp.value=vcidade.toUpperCase();
	f1.sp.value=vestado.toUpperCase();
	f1.pp.value=vcep.toUpperCase();
}

function apaga_arquivo(arquivo){
/*	f=document.frmdisponibiliza;
	f.acao.value=acao;
	alert('passei');
	f.arquivo.value=arquivo;
	f.submit();*/

	varquivo=arquivo;
	xmlhttp = criaXMLHttp();
	xmlhttp.open("GET","scripts/ajax.php?funcao=apaga_arquivo&arquivo="+varquivo,true);
	alert('passei');
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			document.frmdisponibiliza.submit();
		}
	}
	xmlhttp.send(null)
}

function carrega_cor(vcor){
	f1=document.frm_cotacao;
	document.getElementById('dcor').innerHTML= "<select name=\"cor\" class=\"megatron-texto-representantes\"><option value=\"0\">Aguarde</option></select>";
	document.getElementById('dacond').innerHTML= "<select name=\"acondicionamento\" class=\"megatron-texto-representantes\"><option value=\"0\">Aguarde</option></select>";
	vchave = f1.especif.value;
	xmlhttp = criaXMLHttp();
	xmlhttp.open("GET","scripts/ajax.php?funcao=carrega_cor&chave="+vchave+"&cor="+vcor,true);
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			retorno=eval(xmlhttp.responseText);
			document.getElementById('dcor').innerHTML= retorno[0];
			document.getElementById('dacond').innerHTML= retorno[1];
		}
	}
	xmlhttp.send(null)
}

function carrega_metragem(event){
	f1=document.frm_cotacao;
	document.getElementById('dmetr').innerHTML= "<select name=\"metragem\" class=\"megatron-texto-representantes\"><option value=\"0\">Aguarde</option></select>";
	vchave = f1.especif.value;
	vacond = f1.acondicionamento.value;
	xmlhttp = criaXMLHttp();
	xmlhttp.open("GET","scripts/ajax.php?funcao=carrega_metragem&chave="+vchave+"&acond="+vacond,true);
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			retorno=eval(xmlhttp.responseText);
			document.getElementById('dmetr').innerHTML= retorno[0];
		}
	}
	xmlhttp.send(null)
}

function mudacursor(opt) {
	if (opt == 1) {
		document.body.style.cursor = 'hand';
	} else if (opt == 2) {
		document.body.style.cursor = 'help';
	} else if (opt == 3) {
		document.body.style.cursor = 'default';
	}
}

function removerItem(chave){
	xmlhttp = criaXMLHttp();
	xmlhttp.open("GET","scripts/ajax.php?funcao=removerItem&chave="+chave,true);
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			retorno=eval(xmlhttp.responseText);
			document.frm_listacotacao.submit();
		}
	}
	xmlhttp.send(null)
}

//<script language="javascript" >
//IncluiSWF("nome_da_imagem","altura","largura","identificação")
//</script>
function IncluiSWF(arquivo,altura,largura,id){
    document.writeln('    <object id="globalnav-object" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + largura + '" height="' + altura + '" id="' + id + '" name="' + id + '">');
    document.writeln('        <param name="movie" value="' + arquivo + '" />');
    document.writeln('        <param name="FlashVars" value="loc=en_US&htmlApp=false&gatewayURL=gwurl" />');
    document.writeln('        <param name="menu" value="false" />');
    document.writeln('        <param name="quality" value="high" />');
    document.writeln('        <param name="salign" value="tl" />');
    document.writeln('        <param name="scale" value="noscale" />');
    document.writeln('        <param name="wmode" value="transparent" />');
    document.writeln('        <embed id="globalnav-embed" src="' + arquivo + '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" flashvars="loc=en_US&htmlApp=false&gatewayURL=gwurl" menu="false" quality="high" salign="tl" scale="noscale" id="' + id + '" width="' + largura + '" height="' + altura + '"></embed>');
    document.writeln('    </object>');
}

