/*AJAX*/
var xmlhttp
var id_element

function carregar(file,identificador,method,qs) {

id_element = identificador;

if (!method) { //verifica se o method foi alterado para POST
  metodo = 'GET'
} else {
  metodo = method;
  var querystring;
  var z = '';
  for (i=0;i<qs.length;i++) { //monta a querystring
    z = z+qs[i]+"="+document.getElementById(qs[i]).value+"&";
  }
  querystring = unescape(z.substring(0,z.length-1));
}

//cria sempre um link difererente com date e time para evitar armazenamento com cache, verifica se há parâmetros na url
if (file.match('=')) {var arquivo = file+"&rand="+new Date().getTime();} else {var arquivo = file+"?rand="+new Date().getTime();}

//exibe a mensagem carregando
var loading=document.getElementById(identificador)
    loading.innerHTML='<div class="carregando">carregando.</div>'

// code for Mozilla, etc.
if (window.XMLHttpRequest) {
  xmlhttp=new XMLHttpRequest()
  xmlhttp.onreadystatechange=xmlhttpChange
  xmlhttp.open(metodo,arquivo,true)
  
  if (!querystring) {
    xmlhttp.send(null)
  } else {
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
    xmlhttp.send(querystring)
  }
  
  }
// code for IE
else if (window.ActiveXObject) {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp) {
    xmlhttp.onreadystatechange=xmlhttpChange
    xmlhttp.open(metodo,arquivo,true)

    if (!querystring) {
      xmlhttp.send(null)
    } else {
      xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
      xmlhttp.send(querystring)
    }

    }
  }
}

function xmlhttpChange() {
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4) {
  // if "OK"
  if (xmlhttp.status==200) {
  var resultado;
  resultado = xmlhttp.responseText;
  resultado = resultado.replace(/\+/g,' '); //substitui o + por um espaço
  if (metodo == 'POST') {resultado = unescape(resultado);} //desfaz a função urlencode (p/ POST)
  document.getElementById(id_element).innerHTML = resultado;
  }
  else {
    alert("Problem retrieving data:" + xmlhttp.statusText)
  }
  }
}

/*abrir nova janela com parâmetros*/
function pp(url,w,h) {
  window.open(url,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width="+w+", height="+h);
}

function checaCep(){
	if(document.getElementById('cepDestino').value == ''){
			alert('CEP inválido');
			return false;
	} else {
		document.getElementById('fCarrinho').submit();
		return true;
	}
}


/*validação dos campos do formulário*/
function ValidaForm(form) {
  var erros = Array();
  var foco = null;
  var nomeform = form;

 	if(nomeform.cepDestino){
		if (nomeform.cepDestino.value != nomeform.cepDestino.value){
			erros.push('Cep inválido');
			if (foco == null)
		  foco = nomeform.cepDestino;
		}
	}
	
	 if(nomeform.numcartao){
		if (nomeform.numcartao.value == ''){
			erros.push('Número de cartão inválido');
			if (foco == null)
		  foco = nomeform.numcartao;
		}
	}

	numErros = erros.length;	
	if (numErros > 0) {
	  msg = 'Campos obrigatórios:';
	  for (i = 0; i < numErros; i++) {
	    msg += '\n » ' + erros[i];
	  }
	  alert(msg);
	  foco.focus();
	  return false;
	} else {
	  nomeform.submit();
	  return true;
	}
}

function Oculta(div1, div2){
	 document.getElementById(div1).style.display = 'none';
	 document.getElementById(div2).style.display = 'none';
}
function Mostra(div1, div2){
	 document.getElementById(div1).style.display = 'block';
	 document.getElementById(div2).style.display = 'none';
}
function ChecaPay(){
		if(document.getElementById('pay').value == ''){
			alert('Escolha uma opção de pagamento');
			return false;
		} else {
			conf_dados.submit();
			return true;
		}
}
		