var Calendario = null;
var primeiroErro = null;
function abreCalendario(id) {
  destroiCalendario();
  var obj = document.getElementById(id);
  var obj2 = obj;
  var x = 0;
  var y = 0;
  do {
    x += obj2.offsetLeft;
    y += obj2.offsetTop;
    obj2 = obj2.offsetParent;
  } while (obj2.tagName != "BODY");
  var nome = id.replace(new RegExp("_calendario$","i"),"");
  var mes;
  var ano;
  if (arguments.length == 3) { mes = arguments[1]; ano = arguments[2]; }
  else {
    var m = document.getElementById(nome + "_mes");
	var a = document.getElementById(nome + "_ano");
	if ((a.selectedIndex != 0) && (m.selectedIndex != 0)) {
	  mes = parseInt(m.value,10)-1;
	  ano = parseInt(a.value,10);
	} else {
	  mes = mesAtual();
	  ano = anoAtual();
	}
  }
  Calendario = document.createElement("div");
  Calendario.id = "Calendario";
  Calendario.style.position = "absolute";
  Calendario.zIndex = 100;
  if (ie6()) 
    if (nome == "chegada") Calendario.style.top = "342px"; else Calendario.style.top = "374px";
  else Calendario.style.top = (y + 20) + "px";
  Calendario.style.left = (x - 10) + "px";
  Calendario.style.width = 200;
  Calendario.style.backgroundColor = "#d2ffff";
  Calendario.style.border = "2px solid #daa520";
  document.body.appendChild(Calendario); 
  var tabl = document.createElement("table");
  tabl.border = 0;
  tabl.cellpadding = 0;
  tabl.cellspacing = 0;
  tabl.className = "calendario";
  Calendario.appendChild(tabl); 
  var tb = document.createElement("tbody");
  tabl.appendChild(tb);
  var r1 = document.createElement("tr");
  tb.appendChild(r1);
  var c11 = document.createElement("td");
  r1.appendChild(c11);
  c11.colSpan = 6;
  c11.className = "calendarioTitulo";
  c11.innerHTML = "Calend&aacute;rio";
  var c12 = document.createElement("td");
  r1.appendChild(c12);
  c12.innerHTML = '<input type="image" src="images/x20.gif" onClick="destroiCalendario();">';
  var r2 = document.createElement("tr");
  tb.appendChild(r2);
  r2.className = "calendarioMesAno";
  var c21 = document.createElement("td");
  r2.appendChild(c21);
  c21.colSpan = 7;
  c21.style.width = "100%";
  var stemp = '<table class="calendarioMesAno"><tr><td>'; 
  stemp += '<input type="image" id="calendarioEsq" src="images/setaazulesq.gif" class="calendarioMesAnoEsq">';
  stemp += '</td><td>' + mesAno(mes,ano) + '</td><td>';
  stemp += '<input type="image" id="calendarioDir" src="images/setaazuldir.gif" class="calendarioMesAnoDir">';
  stemp += '</td></tr></table>';
  c21.innerHTML = stemp;
  document.getElementById("calendarioEsq").onclick = function() { abreCalendario(obj.id, mesAnterior(mes), anoAnterior(mes,ano)); return true; }
  document.getElementById("calendarioDir").onclick = function() { abreCalendario(obj.id, mesSeguinte(mes), anoSeguinte(mes,ano)); return true; }
  var r3 = document.createElement("tr");
  tb.appendChild(r3);
  var c31 = document.createElement("td");
  r3.appendChild(c31);
  c31.className = "calendarioSubtitulo";
  c31.innerHTML = "Dom";
  var c32 = document.createElement("td");
  r3.appendChild(c32);
  c32.className = "calendarioSubtitulo";
  c32.innerHTML = "Lun";
  var c33 = document.createElement("td");
  r3.appendChild(c33);
  c33.className = "calendarioSubtitulo";
  c33.innerHTML = "Mar";
  var c34 = document.createElement("td");
  r3.appendChild(c34);
  c34.className = "calendarioSubtitulo";
  c34.innerHTML = "Mie";
  var c35 = document.createElement("td");
  r3.appendChild(c35);
  c35.className = "calendarioSubtitulo";
  c35.innerHTML = "Jue";
  var c36 = document.createElement("td");
  r3.appendChild(c36);
  c36.className = "calendarioSubtitulo";
  c36.innerHTML = "Vie";
  var c37 = document.createElement("td");
  r3.appendChild(c37);
  c37.className = "calendarioSubtitulo";
  c37.innerHTML = "Sab";
  var r = document.createElement("tr");
  tb.appendChild(r);
  var i;
  for (i=0; i<diaSemana(1,mes,ano); i++) {
    var c = document.createElement("td");
    r.appendChild(c);
  }
  var dia = 1;
  while (dia <= diasMes(mes,ano)) {
	if (diaSemana(dia,mes,ano) == 0) {
	  r = document.createElement("tr");
      tb.appendChild(r);
	}
	c = document.createElement("td");
    r.appendChild(c);
	if (dataMenor(dia,mes,ano)) {
      c.className = "calendarioCelulaPassada";
      c.innerHTML = dia;
	} else {
      c.className = "calendarioCelula";
      c.innerHTML = '<a class="calendarioCelula" href="javascript:dataParaCalendario(\'' + nome + '\',' + dia + ',' + mes + ',' + ano + ');">' + dia + '</a>';
	}
	dia++;
  }
  for (i=diaSemana(diasMes(mes,ano))+1; i<=6; i++) {
    var c = document.createElement("td");
    r.appendChild(c);
  }
}
function anoAnterior(mes, ano) {
  if (mes == 0) return ano-1; else return ano;
}
function anoAtual() {
  var dma = Hoje.split("/");
  return parseInt(dma[2],10);
}
function anoSeguinte(mes, ano) {
  if (mes == 11) return ano+1; else return ano;
}
function dataMenor(dia,mes,ano) {
  var dma = Hoje.split("/");
  if (ano < parseInt(dma[2],10)) return true;
  else if (ano > parseInt(dma[2],10)) return false;
  else {
    if (mes < parseInt(dma[1],10)-1) return true;
    else if (mes > parseInt(dma[1],10)-1) return false;
	else {
    if (dia < parseInt(dma[0],10)) return true;
    else return false;
	}
  }
}
function dataMenor2(dia,mes,ano,dia2,mes2,ano2) {
  if (ano < ano2) return true;
  else if (ano > ano2) return false;
  else {
    if (mes < mes2) return true;
    else if (mes > mes2) return false;
	else {
    if (dia < dia2) return true;
    else return false;
	}
  }
}
function dataParaCalendario(id,dia,mes,ano) {
  document.getElementById(id + "_dia").selectedIndex = dia;
  document.getElementById(id + "_mes").selectedIndex = mes+1;
  document.getElementById(id + "_ano").selectedIndex = ano-anoAtual()+1;
  destroiCalendario();
}
function destroiCalendario() {
  if (Calendario != null) { 
    removeElement("Calendario");
	Calendario = null;
  }
}
function diasMes(mes,ano) {
  var diasMeses = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  if ((ano % 4 == 0) && (mes == 1)) return 29;
  else return diasMeses[mes];
}
function diaSemana(dia,mes,ano) {
  var dt = new Date(ano,mes,dia);
  return dt.getDay();
}
function espacos0(obj) {
  var st = obj.value;
  obj.value = st.replace(new RegExp(" ","gi"),"");
}
function espacos1(obj) {
  var st = obj.value;
  st = st.replace(new RegExp(" {2,}","gi")," ");
  st = st.replace(new RegExp("^ ","i"),"");
  obj.value = st.replace(new RegExp(" $","i"),"");
}
function ie6() {
  var st = navigator.appVersion;
  var expr1 = new RegExp("MSIE","i");
  if (!expr1.test(st)) return false;
  else {
    st = st.replace(new RegExp("^.*MSIE","i"),"");
	return (parseInt(st,10) == 6);
  }
}
function insereMensagemErroApos(id, mensagem, flag_br) {
  var ref = document.getElementById(id);
  if (ref == null) return null;
  else {
    var refp = ref.parentNode;
    var erro = document.createElement("font");
    erro.id = "erro_" + id;
    erro.className = "erro";
    refp.appendChild(erro);
	if (flag_br) {
      var br = document.createElement("br");
      erro.appendChild(br);
	}
    var msg = document.createTextNode(mensagem);
    erro.appendChild(msg);
	return erro.id;
  }
}
function mesAno(mes,ano) {
  var meses = ["Enero","Febrero","Marzo","Abril","Maio","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];
  return meses[mes] + "&nbsp;/&nbsp;" + ano;
}
function mesAnterior(mes) {
  if (mes == 0) return 11; else return mes-1;
}
function mesAtual() {
  var dma = Hoje.split("/");
  return parseInt(dma[1],10)-1;
}
function mesSeguinte(mes) {
  if (mes == 11) return 0; else return mes+1;
}
function objetoData() {
  this.nome = null;
  this.ano_inicial = null;
  this.ano_final = null;
  this.refp = null; 
  this.titulo = null;
  this.erroDia = null;
  this.erroMes = null;
  this.erroAno = null;
  this.erroDataInvalida = null;
  if ((arguments.length >= 4) && (arguments.length <= 5)) {
    this.refp = arguments[0];
	this.refp.style.position = "relative";
	this.refp.style.left = "-7px";
    this.titulo = arguments[1];
    this.nome = arguments[2];
	this.ano_inicial = arguments[3];
	if (arguments.length == 5) {
	  this.ano_final = arguments[4];
	} else { // == 4
      this.ano_final = anoAtual();
	}
    var hid = document.createElement("input");
    hid.type = "hidden";
    hid.value = "";
	hid.id = this.nome;
	hid.name = this.nome;
    this.refp.appendChild(hid);
	var tab = document.createElement("table");
	tab.border="0";
	tab.cellspacing="0";
	tab.cellpadding="0";
    this.refp.appendChild(tab);
	var tb = document.createElement("tbody");
	tab.appendChild(tb);
	var row = document.createElement("tr");
	tb.appendChild(row);
	var c0 = document.createElement("td");
	c0.vAlign = "center";
	row.appendChild(c0);
	var t0 = document.createTextNode(this.titulo);
	c0.appendChild(t0);
	var c3 = document.createElement("td");
	c3.vAlign = "top";
	row.appendChild(c3);
    this.mes = document.createElement("select");
	this.mes.id = this.nome + "_mes";
    c3.appendChild(this.mes);
    atr = document.createElement("option");
	atr.value="0";
	atr.innerHTML = "month";
	this.mes.appendChild(atr);
	for (i=1; i<=12; i++) {
	  atr = document.createElement("option");
	  atr.value = (i<10)?"0"+i:i;
	  atr.innerHTML = (i<10)?"0"+i:i;
      this.mes.appendChild(atr);
	} 
	var c2 = document.createElement("td");
	c2.vAlign = "top";
	row.appendChild(c2);
	c2.innerHTML = "&nbsp;/&nbsp;";
	var c1 = document.createElement("td");
	c1.vAlign = "top";
	row.appendChild(c1);
    this.dia = document.createElement("select");
	this.dia.id = this.nome + "_dia";
    c1.appendChild(this.dia);
    var atr = document.createElement("option");
	atr.value="0";
	atr.innerHTML = "day";
	this.dia.appendChild(atr);
	for (var i=1; i<=31; i++) {
	  atr = document.createElement("option");
	  atr.value = (i<10)?"0"+i:i;
	  atr.innerHTML = (i<10)?"0"+i:i;
      this.dia.appendChild(atr);
	} 
	var c4 = document.createElement("td");
	c4.vAlign = "top";
	row.appendChild(c4);
	c4.innerHTML = "&nbsp;/&nbsp;";
	var c5 = document.createElement("td");
	c5.vAlign = "top";
	row.appendChild(c5);
    this.ano = document.createElement("select");
	this.ano.id = this.nome + "_ano";
    c5.appendChild(this.ano);
    atr = document.createElement("option");
	atr.value="0";
	atr.innerHTML = "year";
	this.ano.appendChild(atr);
	for (i=this.ano_inicial; i<=this.ano_final; i++) {
	  atr = document.createElement("option");
	  atr.value = i;
	  atr.innerHTML = i;
      this.ano.appendChild(atr);
	} 
  }
  this.critica = function() {
    document.getElementById(this.nome).value = "";
    removeMensagemErro(this.erroDia); this.erroDia = null;
    removeMensagemErro(this.erroMes); this.erroMes = null;
    removeMensagemErro(this.erroAno); this.erroAno = null;
    if (this.dia.selectedIndex == 0) this.erroDia = insereMensagemErroApos(this.dia.id, "Advertencia: seleccione el día.", true);
    if (this.mes.selectedIndex == 0) this.erroMes = insereMensagemErroApos(this.mes.id, "Advertencia: seleccione el mes", true);
    if (this.ano.selectedIndex == 0) this.erroAno = insereMensagemErroApos(this.ano.id, "Advertencia: seleccione el año.", true);
    if ((this.dia.selectedIndex == 0) || (this.mes.selectedIndex == 0) || (this.ano.selectedIndex == 0)) return false;
    else {
      removeMensagemErro(this.erroDataInvalida); this.erroDataInvalida = null;
	  if ((this.dia.value == "31") && ((this.mes.value == "02") || (this.mes.value == "04") || (this.mes.value == "06") || (this.mes.value == "09") || (this.mes.value == "11"))) {
        this.erroDataInvalida = insereMensagemErroApos(this.ano.id, " Error: la fecha no es válida.", false);
		return false;
      }	else if ((this.dia.value == "30") && (this.mes.value == "02")) {
        this.erroDataInvalida = insereMensagemErroApos(this.ano.id, " Error: la fecha es inválida.", false);
		return false;
      }	else if ((this.dia.value == "29") && (this.mes.value == "02") && (parseInt(this.ano.value,10)%4!=0)) {
        this.erroDataInvalida = insereMensagemErroApos(this.ano.id, " Error: esta fecha es inválida.", false);
		return false;
	  } else {
        document.getElementById(this.nome).value = this.dia.value + "/" + this.mes.value + "/" + this.ano.value;
        return true;
	  }
	}
  }
}
function objetoDataCalendario() {
  this.nome = null;
  this.ano_inicial = null;
  this.ano_final = null;
  this.refp = null; 
  this.titulo = null;
  this.erroDia = null;
  this.erroMes = null;
  this.erroAno = null;
  this.erroDataInvalida = null;
  if ((arguments.length >= 4) && (arguments.length <= 5)) {
    this.refp = arguments[0];
	this.refp.style.position = "relative";
	this.refp.style.left = "-7px";
    this.titulo = arguments[1];
    this.nome = arguments[2];
	this.ano_inicial = arguments[3];
	if (arguments.length == 5) {
	  this.ano_final = arguments[4];
	} else { // == 4
      this.ano_final = anoAtual();
	}
    var hid = document.createElement("input");
    hid.type = "hidden";
    hid.value = "";
	hid.id = this.nome;
	hid.name = this.nome;
    this.refp.appendChild(hid);
	var tab = document.createElement("table");
	tab.border="0";
	tab.cellspacing="0";
	tab.cellpadding="0";
    this.refp.appendChild(tab);
	var tb = document.createElement("tbody");
	tab.appendChild(tb);
	var row = document.createElement("tr");
	tb.appendChild(row);
	var c0 = document.createElement("td");
	c0.vAlign = "center";
	row.appendChild(c0);
	var t0 = document.createTextNode(this.titulo);
	c0.appendChild(t0);
	var c3 = document.createElement("td");
	c3.vAlign = "top";
	row.appendChild(c3);
    this.mes = document.createElement("select");
	this.mes.id = this.nome + "_mes";
    c3.appendChild(this.mes);
    atr = document.createElement("option");
	atr.value="0";
	atr.innerHTML = "month";
	this.mes.appendChild(atr);
	for (i=1; i<=12; i++) {
	  atr = document.createElement("option");
	  atr.value = (i<10)?"0"+i:i;
	  atr.innerHTML = (i<10)?"0"+i:i;
      this.mes.appendChild(atr);
	} 
	var c2 = document.createElement("td");
	c2.vAlign = "top";
	row.appendChild(c2);
	c2.innerHTML = "&nbsp;/&nbsp;";
	var c1 = document.createElement("td");
	c1.vAlign = "top";
	row.appendChild(c1);
    this.dia = document.createElement("select");
	this.dia.id = this.nome + "_dia";
    c1.appendChild(this.dia);
    var atr = document.createElement("option");
	atr.value="0";
	atr.innerHTML = "day";
	this.dia.appendChild(atr);
	for (var i=1; i<=31; i++) {
	  atr = document.createElement("option");
	  atr.value = (i<10)?"0"+i:i;
	  atr.innerHTML = (i<10)?"0"+i:i;
      this.dia.appendChild(atr);
	} 
	var c4 = document.createElement("td");
	c4.vAlign = "top";
	row.appendChild(c4);
	c4.innerHTML = "&nbsp;/&nbsp;";
	var c5 = document.createElement("td");
	c5.vAlign = "top";
	row.appendChild(c5);
    this.ano = document.createElement("select");
	this.ano.id = this.nome + "_ano";
    c5.appendChild(this.ano);
    atr = document.createElement("option");
	atr.value="0";
	atr.innerHTML = "year";
	this.ano.appendChild(atr);
	for (i=this.ano_inicial; i<=this.ano_final; i++) {
	  atr = document.createElement("option");
	  atr.value = i;
	  atr.innerHTML = i;
      this.ano.appendChild(atr);
	} 
	var c6 = document.createElement("td");
	c6.vAlign = "top";
	c6.innerHTML = "&nbsp;";
    row.appendChild(c6);
	var c7 = document.createElement("td");
	c7.vAlign = "top";
    row.appendChild(c7);
    this.calendario = document.createElement("input");
	this.calendario.type = "image";
	this.calendario.id = this.nome + "_calendario";
	this.calendario.src = "images/calendario.gif";
	this.calendario.border = 0;
	this.calendario.style.marginTop = "2px";
	this.calendario.onclick = function() { abreCalendario(this.id); return false; }
    c7.appendChild(this.calendario);
  }
  this.critica = function() {
    document.getElementById(this.nome).value = "";
    removeMensagemErro(this.erroDia); this.erroDia = null;
    removeMensagemErro(this.erroMes); this.erroMes = null;
    removeMensagemErro(this.erroAno); this.erroAno = null;
    if (this.dia.selectedIndex == 0) this.erroDia = insereMensagemErroApos(this.dia.id, "Advertencia: seleccione el día.", true);
    if (this.mes.selectedIndex == 0) this.erroMes = insereMensagemErroApos(this.mes.id, "Advertencia: seleccione el mes.", true);
    if (this.ano.selectedIndex == 0) this.erroAno = insereMensagemErroApos(this.ano.id, "Advertencia: seleccione el año.", true);
    if ((this.dia.selectedIndex == 0) || (this.mes.selectedIndex == 0) || (this.ano.selectedIndex == 0)) return false;
    else {
      removeMensagemErro(this.erroDataInvalida); this.erroDataInvalida = null;
	  if ((this.dia.value == "31") && ((this.mes.value == "02") || (this.mes.value == "04") || (this.mes.value == "06") || (this.mes.value == "09") || (this.mes.value == "11"))) {
        this.erroDataInvalida = insereMensagemErroApos(this.calendario.id, " Error: la fecha no es válida.", false);
		return false;
      }	else if ((this.dia.value == "30") && (this.mes.value == "02")) {
        this.erroDataInvalida = insereMensagemErroApos(this.calendario.id, " Error: la fecha es inválida.", false);
		return false;
      }	else if ((this.dia.value == "29") && (this.mes.value == "02") && (parseInt(this.ano.value,10)%4!=0)) {
        this.erroDataInvalida = insereMensagemErroApos(this.calendario.id, " Error: esta fecha no es válida.", false);
		return false;
      }	else if (dataMenor(parseInt(this.dia.value,10),parseInt(this.mes.value,10)-1,parseInt(this.ano.value,10))) {
        this.erroDataInvalida = insereMensagemErroApos(this.calendario.id, " Advertencia: esta fecha es anterior que hoy.", false);
		return false;
	  } else {
        document.getElementById(this.nome).value = this.dia.value + "/" + this.mes.value + "/" + this.ano.value;
        return true;
	  }
	}
  }
}
function obrigatorio(id) {
  if (document.getElementById(id).idErro) removeMensagemErro(document.getElementById(id).idErro);
  if (document.getElementById(id).value.length > 0) return true;
  else {
    document.getElementById(id).idErro = insereMensagemErroApos(id,"Advertencia: complete este campo.",true);
  }
}
function obrigatorioCriticaEmail(id) {
  var st = document.getElementById(id).value;
  var expr;
  if (document.getElementById(id).idErro) removeMensagemErro(document.getElementById(id).idErro);
  if (st.length > 0) {
    expr = new RegExp("^[a-z0-9]+((\\\.|-|_)[a-z0-9]+)*@[a-z][a-z0-9]*((\\\.|-)[a-z0-9]+)*$","i");
    if (expr.test(st)) return true;
    else {
      document.getElementById(id).idErro = insereMensagemErroApos(id,"Advertencia: " + id + " no es válido.",true);
	  return false;
    }
  } else {
    document.getElementById(id).idErro = insereMensagemErroApos(id,"Advertencia: complete este campo.",true);
    return false;
  }
}
function obrigatorioCriticaTelefone(id) {
  var st = document.getElementById(id).value;
  var expr;
  if (document.getElementById(id).idErro) removeMensagemErro(document.getElementById(id).idErro);
  if (st.length > 0) {
    //expr = new RegExp("^.*[0-9]{3,4}-?[0-9]{4}$","i");
    expr1 = new RegExp("^[0-9]{2,3}-[0-9]{3,4}-?[0-9]{4}$","i");
    expr2 = new RegExp("^[0-9]{1,3}-[0-9]{2,3}-[0-9]{3,4}-?[0-9]{4}$","i");
    if (expr1.test(st) || expr2.test(st)) return true;
    else {
      document.getElementById(id).idErro = insereMensagemErroApos(id,"Advertencia: " + id + " no es válido.\n(ddd-telefono o ddi-ddd-telefono)",true);
	  return false;
    }
  } else {
    document.getElementById(id).idErro = insereMensagemErroApos(id,"Advertencia: complete este campo.",true);
    return false;
  }
}
function pesquisaErro(obj) {
  var i;
  if (obj.tagName) {
	if (obj.id) {
	  if (obj.id.indexOf("erro_") == 0) {
        primeiroErro = obj; 
		return true;
	  }
	}
	if (primeiroErro == null) {
      for (i=0; i<obj.childNodes.length; i++) {
	    pesquisaErro(obj.childNodes[i]);
	  }
	}
	return false;
  }
}
function posicaoErro(obj) {
  var obj2 = obj;
  var n = 0;
  do {
    n += obj2.offsetTop;
	obj2 = obj2.offsetParent;
  } while (obj2.tagName != "BODY");
  n -= 50;
  if (n<0) return 0; else return n;
}
function removeMensagemErro(id) {
  var ref = document.getElementById(id);
  if (ref == null) return false;
  else {
    removeElement(id);
    return true;
  }
}
function prepDestroiCalendario() {
  var colec = document.getElementsByTagName("INPUT");
  var i;
  for (i=0; i<colec.length; i++) colec[i].onfocus = function() { destroiCalendario(); return true; }
  colec = document.getElementsByTagName("SELECT");
  for (i=0; i<colec.length; i++) colec[i].onfocus = function() { destroiCalendario(); return true; }
  colec = document.getElementsByTagName("TEXTAREA");
  for (i=0; i<colec.length; i++) colec[i].onfocus = function() { destroiCalendario(); return true; }
}
