// JavaScript Document

// Pre-Loading das Imagens e Swap
function SimpleSwap(el,which){
  el.src=el.getAttribute(which || "origsrc");
}

function SimpleSwapSetup(){
  var x = document.getElementsByTagName("img");
  for (var i=0;i<x.length;i++){
    var oversrc = x[i].getAttribute("oversrc");
    if (!oversrc) continue;
      
    // preload image
    // comment the next two lines to disable image pre-loading
    x[i].oversrc_img = new Image();
    x[i].oversrc_img.src=oversrc;
    // set event handlers
    x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
    x[i].onmouseout = new Function("SimpleSwap(this);");
    // save original src
    x[i].setAttribute("origsrc",x[i].src);
  }
}

var PreSimpleSwapOnload =(window.onload)? window.onload : function(){};
window.onload = function(){PreSimpleSwapOnload(); SimpleSwapSetup();}


// Busca dos Centros

function BuscaCidade(estado){

		try{
			xmlhttp = new XMLHttpRequest();
		}catch(ee){
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(E){
					xmlhttp = false;
				}
			}
		}

    //limpa o select
    var c=document.getElementById("cidade");
//    while(c.options.length>0)c.options[0]=null

	
    var u=document.getElementById("centro");
//    while(u.options.length>0)u.options[0]=null
	
   // c.options[0]=new Option(" -- Carregando ... -- "," -- Carregando ... -- ");

    //Monta a url com a uf
    xmlhttp.open("GET", "/scripts/unidades.asp?acao=1&estado=" + estado,true);
	//alert("ESTADO: "+estado);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //limpa o select
            var c=document.getElementById("cidade")
            while(c.options.length>0)c.options[0]=null
            //Transforma a lista de cidades JSON em Javascript
            var aCidades=eval((xmlhttp.responseText))
			
            //popula o select com a lista de cidades obtida
            for(var i=0;i<aCidades.length;i++){
                aCidades[i]=unescape(aCidades[i])
				aCidades[i] = aCidades[i].replace(/\+/g," ");
                c.options[c.options.length]=new Option(aCidades[i],aCidades[i])
            }
        }
    }

    xmlhttp.send(null)
	
	
}


function BuscaCentro(cidade){

		try{
			xmlhttp = new XMLHttpRequest();
		}catch(ee){
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(E){
					xmlhttp = false;
				}
			}
		}

    //limpa o select
    var c=document.getElementById("centro")
    while(c.options.length>0)c.options[0]=null
    c.options[0]=new Option(" -- Carregando ... -- "," -- Carregando ... -- ")

    //Monta a url com a uf
    xmlhttp.open("GET", "/scripts/unidades.asp?acao=2&cidade=" + cidade,true);
	//alert("CIDADE: "+cidade);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //limpa o select
            var c=document.getElementById("centro")
            while(c.options.length>0)c.options[0]=null
            //Transforma a lista de cidades JSON em Javascript
            var aCentros=eval((xmlhttp.responseText))
			
            //popula o select com a lista de cidades obtida
            for(var i=0;i<aCentros.length;i++){
                aCentros[i]=unescape(aCentros[i])
				aCentros[i] = aCentros[i].replace(/\+/g," ");
                c.options[c.options.length]=new Option(aCentros[i],aCentros[i])
            }
        }
    }

    xmlhttp.send(null)
	
	
}


function MostraCentro(centro){

			 try{
			 		xmlhttp = new XMLHttpRequest();
			 	}catch(ee){
			 		try{
			 			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			 		}catch(e){
			 			try{
			 				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			 			}catch(E){
			 				xmlhttp = false;
			 			}
			 		}
			 	}

			//	var indice = document.form_centros.unidade.selectedIndex 
			//  var unidade = document.form_centros.unidade.options[indice].value
	
				xmlhttp.open("GET", "/scripts/unidades.asp?acao=3&centro=" + centro,true);
				//alert("CENTRO: " + centro);

			 	xmlhttp.onreadystatechange=function() {
			 	if (xmlhttp.readyState==4){
					var aUnidades=xmlhttp.responseText;
					
						aUnidades=unescape(aUnidades)
						aUnidades = aUnidades.replace(/\+/g," ");
					
						document.all('end_centro').innerHTML = aUnidades;
			 		}
			 	}
		xmlhttp.send(null);
}


//Validação dos Cadastros

function ValidaCadastroEmpresa()
{
	if (document.all.txt_nome.value=="")
		{	
			alert("Preencha o nome da empresa");
			document.all.txt_nome.focus();
			return false;
		}
	if (document.all.txt_cnpj.value=="")
		{	
			alert("Preencha o CNPJ");
			document.all.txt_cnpj.focus();
			return false;
		}
	if (document.all.txt_ende.value=="")
		{	
			alert("Preencha o endereço");
			document.all.txt_ende.focus();
			return false;
		}
	if (document.all.txt_bairro.value=="")
		{	
			alert("Preencha o bairro");
			document.all.txt_bairro.focus();
			return false;
		}
	if (document.all.txt_cidade.value=="")
		{	
			alert("Preencha a cidade");
			document.all.txt_cidade.focus();
			return false;
		}		
	if (document.all.txt_cep.value=="")
		{	
			alert("Preencha o CEP");
			document.all.txt_cep.focus();
			return false;
		}
			
	return true;
}

//Produto

function ChkBloqueio(id,estado){

		try{
			xmlhttp = new XMLHttpRequest();
		}catch(ee){
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(E){
					xmlhttp = false;
				}
			}
		}
	
	var now = new Date();
		
    //Monta a url com a uf
    xmlhttp.open("GET", "/scripts/ajax.asp?acao=bloqueio&id=" + id + "&estado=" + estado,true);

	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
			var aResposta=xmlhttp.responseText;
			
			if (aResposta == 'Y') {
				document.all('bloqueio_'+id).src = '/imagens/chk_on.gif';
				document.all('str_bloqueio_'+id).value = aResposta;
			} else {
				document.all('bloqueio_'+id).src = '/imagens/chk_off.gif';
				document.all('str_bloqueio_'+id).value = aResposta;
			}
		}
	}
	
    xmlhttp.send(null)
	
	
}

function ChkEstoque(id,estado){

		try{
			xmlhttp = new XMLHttpRequest();
		}catch(ee){
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(E){
					xmlhttp = false;
				}
			}
		}
	
	var now = new Date();
		
    //Monta a url com a uf
    xmlhttp.open("GET", "/scripts/ajax.asp?acao=estoque&id=" + id + "&estado=" + estado,true);

	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4){
			var aResposta=xmlhttp.responseText;
			
			if (aResposta == 'Y') {
				document.all('estoque_'+id).src = '/imagens/chk_on.gif';
				document.all('str_estoque_'+id).value = aResposta;
			} else {
				document.all('estoque_'+id).src = '/imagens/chk_off.gif';
				document.all('str_estoque_'+id).value = aResposta;
			}
		}
	}
	
    xmlhttp.send(null)
	
	
}
