//====================================================
// JDG - Enero/Febrero/Abril/Mayo 2008
//====================================================

var sProv;
var sPago;
var nroPagina;

//====================================================

function cambiarCateg(categ) {

  esNumerico = !(isNaN(parseInt(categ)));

  if (categ != 0)
    if (esNumerico)
//      if (categ > 0)
      location.href = "categoria.jsp?categoria=" + categ;
    else
      location.href = categ;
}

//====================================================

function checkProvincias(valor) {
  if (valor == 0) {
    chk = document.getElementById('provincia0');

    if (chk.checked == true) {      //Desmarco el resto
      for (i=0; i < document.formulario.elements.length; i++) {
        chk = document.formulario.elements[i];

        if (chk.type == "checkbox")
          if (chk.name == "provincia")
            if (chk.value != valor)
              chk.checked = false;
      }
    }
  } else {
    document.getElementById('provincia0').checked = false;   //Desmarco el primero
  }
  
  //Valido que haya al menos uno marcado
  var cant = 0;

  for (i=0; i < document.formulario.elements.length; i++) {
    chk = document.formulario.elements[i];

    if (chk.type == "checkbox")
      if (chk.name == "provincia")
        if (chk.checked == true)
          cant ++;
  }

  if (cant == 0)
    document.getElementById('provincia0').checked = true;   //Marco el primero
  //------------------

  //Almaceno las provincias marcadas
  sProv = "";

  for (i=0; i < document.formulario.elements.length; i++) {
    chk = document.formulario.elements[i];

    if (chk.type == "checkbox")
      if (chk.name == "provincia")
        if (chk.checked == true)
          sProv = sProv + chk.value + "|";
  }

  if (sProv == "")
    sProv = "0";

}

//====================================================

function checkMediosPagos(valor) {
  if (valor == 0) {
    chk = document.getElementById('mediodepago0');

    if (chk.checked == true) {      //Desmarco el resto
      for (i=0; i < document.formulario.elements.length; i++) {
        chk = document.formulario.elements[i];

        if (chk.type == "checkbox")
          if (chk.name == "mediodepago")
            if (chk.value != valor)
              chk.checked = false;
      }
    }
  } else {
    document.getElementById('mediodepago0').checked = false;   //Desmarco el primero
  }

  //Valido que haya al menos uno marcado
  var cant = 0;

  for (i=0; i < document.formulario.elements.length; i++) {
    chk = document.formulario.elements[i];

    if (chk.type == "checkbox")
      if (chk.name == "mediodepago")
        if (chk.checked == true)
          cant ++;
  }

  if (cant == 0)
    document.getElementById('mediodepago0').checked = true;   //Marco el primero
  //------------------

  //Almaceno los medios de pago marcados
  sPago = "";

  for (i=0; i < document.formulario.elements.length; i++) {
    chk = document.formulario.elements[i];

    if (chk.type == "checkbox")
      if (chk.name == "mediodepago")
        if (chk.checked == true)
          sPago = sPago + chk.value + "|";
  }

  if (sPago == "")
    sPago = "0";
    
}

//====================================================

function traerProvincias() {

  provincias = "";

  for (i=0; i < document.formulario.elements.length; i++) {
    chk = document.formulario.elements[i];

    if (chk.type == "checkbox")
      if (chk.name == "provincia")
        if (chk.checked)
          provincias += chk.value + "|";
  }

  if (provincias.length == 0)
    return "";
  else {
    return provincias.substring(0, provincias.length - 1);
  }
}

//====================================================

function traerMediosPagos() {

  mediosPagos = "";

  for (i=0; i < document.formulario.elements.length; i++) {
    chk = document.formulario.elements[i];

    if (chk.type == "checkbox")
      if (chk.name == "mediodepago")
        if (chk.checked)
          mediosPagos += chk.value + "|";
  }

  if (mediosPagos.length == 0)
    return "";
  else
    return mediosPagos.substring(0, mediosPagos.length - 1);
}

//====================================================

function traerOfertas() {

  document.getElementById('contenedor_muestras').style.display = 'block';
  document.getElementById('contenedor_muestras').innerHTML = "<img src='img/verificando.gif' align='absmiddle'> Buscando ofertas...";

  var categoria = document.getElementById('categoria').value;
  var esVentaTelef = document.getElementById('esVentaTelef').value;
  var breadcrumb = document.getElementById('breadcrumb').value;
  var provincias = traerProvincias();
  var mediosPagos = traerMediosPagos();

  query = "categoria=" + categoria + "&provincias=" + provincias + "&mediospagos=" + mediosPagos;
  query = query + "&esVentaTelef=" + esVentaTelef;

  var myConn = new XHConn();
  var req = "traerOfertas.jsp?" + query + "&";

  var peticion = function (oXML) {
    dev = oXML.responseText;
    document.getElementById('contenedor_muestras').innerHTML = dev;
  };
  myConn.connect(req, "GET", "", peticion);

  armarPaginas(1);
  armarPanel();
  armarVerMas(1);
}

//====================================================

function cambiarPagina(pagina) {

  document.getElementById('contenedor_muestras').style.display = 'block';
  document.getElementById('contenedor_muestras').innerHTML = "<img src='img/verificando.gif' align='absmiddle'> Buscando ofertas...";

  var categoria = document.getElementById('categoria').value;
  var esVentaTelef = document.getElementById('esVentaTelef').value;
  var provincias = traerProvincias();
  var mediosPagos = traerMediosPagos();

  query = "categoria=" + categoria + "&provincias=" + provincias + "&mediospagos=" + mediosPagos + "&pagina=" + pagina;
  query = query + "&esVentaTelef=" + esVentaTelef;

  var myConn = new XHConn();
  var req = "traerOfertas.jsp?" + query + "&";

  var peticion = function (oXML) {
    dev = oXML.responseText;
    document.getElementById('contenedor_muestras').innerHTML = dev;
  };
  myConn.connect(req, "GET", "", peticion);

  armarPaginas(pagina);
  armarVerMas(pagina);
  nroPagina = pagina;
}

//====================================================

function armarPaginas(pagina) {

  var categoria = document.getElementById('categoria').value;
  var esVentaTelef = document.getElementById('esVentaTelef').value;
  var provincias = traerProvincias();
  var mediosPagos = traerMediosPagos();

  query = "categoria=" + categoria + "&provincias=" + provincias + "&mediospagos=" + mediosPagos + "&pagina=" + pagina;
  query = query + "&esVentaTelef=" + esVentaTelef;

  var myConn = new XHConn();
  var req = "armarPaginas.jsp?" + query + "&";

  var peticion = function (oXML) {
    dev = oXML.responseText;
    document.getElementById('paginado').innerHTML = dev;
  };
  myConn.connect(req, "GET", "", peticion);
}

//====================================================

function armarPanel() {

  var categoria = document.getElementById('categoria').value;
  var esVentaTelef = document.getElementById('esVentaTelef').value;
  var provincias = traerProvincias();
  var mediosPagos = traerMediosPagos();

  query = "categoria=" + categoria + "&provincias=" + provincias + "&mediospagos=" + mediosPagos;
  query = query + "&esVentaTelef=" + esVentaTelef;

  var myConn = new XHConn();
  var req = "armarPanel.jsp?" + query + "&";

  var peticion = function (oXML) {
    dev = oXML.responseText;
    document.getElementById('panel').innerHTML = dev;
  };
  myConn.connect(req, "GET", "", peticion);
}

//====================================================

function armarVerMas(pagina) {

  var categoria = document.getElementById('categoria').value;
  var esVentaTelef = document.getElementById('esVentaTelef').value;
  var provincias = traerProvincias();
  var mediosPagos = traerMediosPagos();

  query = "categoria=" + categoria + "&provincias=" + provincias + "&mediospagos=" + mediosPagos + "&pagina=" + pagina;
  query = query + "&esVentaTelef=" + esVentaTelef;

  var myConn = new XHConn();
  var req = "armarVerMas.jsp?" + query + "&";

  var peticion = function (oXML) {
    dev = oXML.responseText;
    document.getElementById('vermas').innerHTML = dev;
  };
  myConn.connect(req, "GET", "", peticion);
}

//====================================================

function irURL(sURL) {
  var categoria = document.getElementById('categoria').value;
  var esVentaTelef = document.getElementById('esVentaTelef').value;
  var cTelef = -30;    // -30 = Categoría venta telefónica
  var breadcrumb = document.getElementById('breadcrumb').value;

  if (sURL.indexOf('?') == -1)
    sURL = sURL + "?";

  var paginaDestino = sURL + "&prov=" + sProv + "&pago=" + sPago + "&nroPagina=" + nroPagina + "&categ=" + categoria;
  var paginaDestino = paginaDestino + "&esVentaTelef=" + esVentaTelef + "&categVentaTelef=" + cTelef;
  var paginaDestino = paginaDestino + "&breadcrumb=" + breadcrumb;

  //window.alert(paginaDestino);
  location.href = paginaDestino;
}

//====================================================

function setearEstado(prov, pagos, pagina) {

  //----------------------

  prov = "|" + prov + "|";

  for (i=0; i < document.formulario.elements.length; i++) {
    chk = document.formulario.elements[i];

    if (chk.type == "checkbox")
      if (chk.name == "provincia")
        if (prov.indexOf("|" + chk.value + "|") > -1)
          chk.checked = true;
  }

  //----------------------

  pagos = "|" + pagos + "|";

  for (i=0; i < document.formulario.elements.length; i++) {
    chk = document.formulario.elements[i];

    if (chk.type == "checkbox"){
      if (chk.name == "mediodepago"){
        if (pagos.indexOf("|" + chk.value + "|") > -1){
          chk.checked = true;
		  }
		}
	}
  }

  //----------------------
    
  armarPaginas(pagina);
  traerOfertas();
}

//====================================================
//==================================================
//Utilizada en buscaOfertas
function cambiarComboCateg(){
  var provincias = document.getElementById('selProvincia').value;
  var query = "provincias=" + provincias; /*+ "&mediospagos=" + mediosPagos;*/
  var req = "traerCategoriasporProvincia.jsp?" + query + "&";

 var myConn = new XHConn();
  
  var peticion = function (oXML) {
    dev = oXML.responseText;
    dev = dev.replace(/^\s*|\s*$/g,"");     //Similar a trim
    document.getElementById("selCategoria").length = 0;
    var registros = dev.split(';');
   // alert(registros);
   for (i = 0; i < registros.length - 1; i++) {
      var campos = registros[i].split('|');
     
      if(campos[1]!= undefined){
      	document.getElementById("selCategoria").options[i] = new Option(campos[1], campos[0]);
		var id = document.getElementById("idCategoria").value;
      	
      	if(campos[0] == id ){
      		document.getElementById("selCategoria").options[i].selected = 1;
     	}
      	}else{
      	document.getElementById("selCategoria").options[i] = new Option("Categoria", 0);
      	}
    }
  };
  myConn.connect(req, "GET", "", peticion);
}

//====================================================
//Octubre 2008 para combos de provincias y categorias
function irACategoria() {
	  
	var categoria = document.getElementById('selCategoria').value;
	var esVentaTelef = false;
	var provincias = document.getElementById('selProvincia').value + "|99";
	//var mediosPagos = document.getElementById('selMediosPago').value;
	var mensaje = "";
	 
	var query = "categoria=" + categoria + "&prov=" + provincias;
	if(categoria == 0 | provincias == 0){
		mensaje = "Debe seleccionar el/los siguiente/s dato/s para la búsqueda:<BR>";
		if(categoria == 0){
			mensaje = mensaje + "* Categoria<BR>";
	  	}
	  	if(provincias == 0){
	  		mensaje = mensaje + "* Provincia<BR>";
	  	}
	}
  	if(mensaje == ""){
  		window.location.href = "categoria.jsp?"+ query;
	}else{
		document.getElementById('error').innerHTML = "<P>"+mensaje+"</p>";
	}
}
//====================================================
