// declare a global  XMLHTTP Request object
var XmlHttpObj;
var XmlHttoObjTwo;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
  // try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
  try
  {
    XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e)
  {
    try
    {
      XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    catch(oc)
    {
      XmlHttpObj = null;
    }
  }
  // if unable to create using IE specific code then try creating for Mozilla (FireFox) 
  if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
  {
    XmlHttpObj = new XMLHttpRequest();
  }
}

function CreateXmlHttpObjTwo()
{
  // try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
  try
  {
    XmlHttpObjTwo = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e)
  {
    try
    {
      XmlHttpObjTwo = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    catch(oc)
    {
      XmlHttpObjTwo = null;
    }
  }
  // if unable to create using IE specific code then try creating for Mozilla (FireFox) 
  if(!XmlHttpObjTwo && typeof XMLHttpRequest != "undefined") 
  {
    XmlHttpObjTwo = new XMLHttpRequest();
  }
}


function EstadosListOnChange () {
  
  //var destinationsList = document.getElementById("paisClasificado");
  
  //get select destination from destinationslist
  //var selectedDestination = destinationsList.options[destinationsList.selectedIndex].value;
   // url of page that will send xml data back to client browser
    var requestUrl;
    // use the following line if using asp
    requestUrl = "http://laguia.delvolante.com/admin/resources/XMLSearchProvider.php?filter=estado&criteria=1";
    
    CreateXmlHttpObj();
  
  // verify XmlHttpObj variable was successfully initialized
  if(XmlHttpObj)
  {
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
    XmlHttpObj.onreadystatechange = StateChangeHandler;
    
    // define the iteraction with the server -- true for as asynchronous.
    XmlHttpObj.open("GET", requestUrl,  true);
    
    // send request to server, null arg  when using "GET"
    XmlHttpObj.send(null);    
  } 
}

function CiudadesListOnChange () {
  
  var regionsList = document.getElementById("estadoClasificado");
  
  var selectedRegions = regionsList.options[regionsList.selectedIndex].value;
  
  var requestUrl;
    // use the following line if using asp
    requestUrl = "http://laguia.delvolante.com/admin/resources/XMLSearchProvider.php?filter=ciudad&criteria=" + encodeURIComponent(selectedRegions);
    
    CreateXmlHttpObjTwo();
  
  // verify XmlHttpObj variable was successfully initialized
  if(XmlHttpObjTwo)
  {
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
    XmlHttpObjTwo.onreadystatechange = StateChangeHandlerTwo;
    
    // define the iteraction with the server -- true for as asynchronous.
    XmlHttpObjTwo.open("GET", requestUrl,  true);
    
    // send request to server, null arg  when using "GET"
    XmlHttpObjTwo.send(null);   
  } 
}


//this function called when state of  XmlHttpObj changes
//we're interested in the state that indicates data has been
//received from the server
function StateChangeHandler()
{
  // state ==4 indicates receiving response data from server is completed
  if(XmlHttpObj.readyState == 4)
  {
    // To make sure valid response is received from the server, 200 means response received is OK
    if(XmlHttpObj.status == 200)
    {     
      PopulateEstadosList(XmlHttpObj.responseXML.documentElement);
    }
    else
    {
      alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status + " ReadyState : " + XmlHttpObj.readyState);
    }
  }
}

function StateChangeHandlerTwo()
{
  // state ==4 indicates receiving response data from server is completed
  if(XmlHttpObjTwo.readyState == 4)
  {
    // To make sure valid response is received from the server, 200 means response received is OK
    if(XmlHttpObjTwo.status == 200)
    {     
      PopulateCiudadesList(XmlHttpObjTwo.responseXML.documentElement);
    }
    else
    {
      alert("problem retrieving data from the server, status code: "  + XmlHttpObjTwo.status + " ReadyState : " + XmlHttpObjTwo.readyState);
    }
  }
}


//populate the contents of the regions dropdown list
function PopulateEstadosList(destinationNode)
{
    var regionsList = document.getElementById("estadoClasificado");
  // clear the country list 
  for (var count = regionsList.options.length-1; count >-1; count--)
  {
    regionsList.options[count] = null;
  }

  var regionsNodes = destinationNode.getElementsByTagName('estado');
  var idValue;
  var textValue; 
  var optionItem;
  // populate the dropdown list with data from the xml doc
  for (var count = 0; count < regionsNodes.length; count++)
  {
      textValue = GetInnerText(regionsNodes[count]);
      idValue = regionsNodes[count].getAttribute("id");
      optionItem = new Option( textValue, idValue,  false, false);
      regionsList.options[regionsList.length] = optionItem;
  }
}



function PopulateCiudadesList (regionNode) {
  
  var locationsList = document.getElementById("ciudadClasificado");
  // clear the country list 
  for (var count = locationsList.options.length-1; count >-1; count--)
  {
    locationsList.options[count] = null;
  }
  
  var locationsNodes = regionNode.getElementsByTagName('ciudad');
  var idValue;
  var textValue; 
  var optionItem;
  // populate the dropdown list with data from the xml doc
  for (var count = 0; count < locationsNodes.length; count++)
  {
      textValue = GetInnerText(locationsNodes[count]);
    idValue = locationsNodes[count].getAttribute("id");
    optionItem = new Option( textValue, idValue,  false, false);
    locationsList.options[locationsList.length] = optionItem;
  }
}

//returns the node text value 
function GetInnerText (node)
{
   return (node.textContent || node.innerText || node.text) ;
}


//show/hide premium features
function showHidePremiumOptions () {
  var typeValueObject = document.getElementById("tipoClasificado");
  var selectedType = typeValueObject.options[typeValueObject.selectedIndex].value;
  
  var ele1 = document.getElementById("premium01");
  var ele2 = document.getElementById("premium02");
  var ele3 = document.getElementById("premium03");
  var ele4 = document.getElementById("premium04");
  if (selectedType=="1") {
     ele1.style.display = "none";
     ele2.style.display = "none";
     ele3.style.display = "none";
     ele4.style.display = "none";
  } else {
     ele1.style.display = "inline";
     ele2.style.display = "inline";
     ele3.style.display = "inline";
     ele4.style.display = "inline";
  }
  
}



