var httpRequest;
var i = 1;

function recargarPoblaciones( id_provincia )
{
  var url = 'ajax/getPoblaciones.php?id_provincia=' + id_provincia;
  if (window.ActiveXObject)
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  else if (window.XMLHttpRequest)
    httpRequest = new XMLHttpRequest();

  httpRequest.open("GET", url, true);
  httpRequest.onreadystatechange = function() {processRequest( 0, 'span_poblaciones' ); } ;
  httpRequest.send(null);
}

function processRequest( padre, tag )
{
  if( httpRequest.readyState == 4 )
  {
    if( httpRequest.status == 200 )
    {
      var resXML = httpRequest.responseText;
      updateHTML( resXML, padre, tag );
    }
    else
    {
      alert( "Error cargando página\n"+ httpRequest.status +":"+ httpRequest.statusText );
    }
  }
}

function updateHTML( resXML, padre, tag )
{
  document.getElementById( tag ).innerHTML = '';

  launchJavascript( resXML );

  if( padre )
    parent.document.getElementById( tag ).innerHTML += resXML;
  else
    document.getElementById( tag ).innerHTML += resXML;
}

function launchJavascript(responseText)
{
  var ScriptFragment = '(?:<script.*?>)((\n|.)*?)(?:</script>)';

  var match    = new RegExp(ScriptFragment, 'img');
  var scripts  = responseText.match(match);

    if(scripts) {
        var js = '';
        for(var s = 0; s < scripts.length; s++) {
            var match = new RegExp(ScriptFragment, 'im');
            js += scripts[s].match(match)[1];
        }
        eval(js);
    }
}
function pushCarrito( id_producto, id_modelo, cantidad )
{
  if( !cantidad ) cantidad = 1;
  var url = 'ajax/pushCarrito.php?id_producto=' + id_producto + '&id_modelo=' + id_modelo + '&cantidad=' + cantidad;

  if (window.ActiveXObject)
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  else if (window.XMLHttpRequest)
    httpRequest = new XMLHttpRequest();

  document.getElementById('mensajes').style["display"]="block";
  httpRequest.open("GET", url, true);
  httpRequest.onreadystatechange = function() {processRequest( 0, 'mensajes' ); } ;
  httpRequest.send(null);
  setTimeout("document.getElementById('mensajes').style['display']='none';",2000);
}

function popCarrito( id_producto, id_modelo )
{
  var url = 'ajax/popCarrito.php?id_producto=' + id_producto + '&id_modelo=' + id_modelo;

  if (window.ActiveXObject)
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  else if (window.XMLHttpRequest)
    httpRequest = new XMLHttpRequest();

  httpRequest.open("GET", url, true);
  httpRequest.onreadystatechange = function() {processRequest( 0, 'div_carri' ); } ;
  httpRequest.send(null);
}
