//<!--following form handling ajax based on http://www.w3schools.com/php/php_ajax_database.asp, stop gap while jQuery is pending-->

//TODO - don't really need all that faffing with querytype as it's not passed based on selection!
function loadIframe(iframeName, url) {
    if ( window.frames[iframeName] ) {
        document.getElementById (iframeName).width="700";
        document.getElementById (iframeName).height="550";
        window.frames[iframeName].location = url;
        
        return false;
    }
    $('#description').text('');
    $('#chart').text('');
    $('#chart1').text('');
    $('#chart2').text('');
    return true;
}

function hideAndClear(iframeName){
if ( window.frames[iframeName] ) {
        document.getElementById (iframeName).width="0";
        document.getElementById (iframeName).height="0";
        window.frames[iframeName].location = "123.php";

        return false;
    }
    else{
    return true;
}
}

function kmlDescriptionAjax(arr, q)
{
    //var arr=arr;
    //var q=q;
    //ge=
    init();
    //fetchKmlFromInput(arr, ge);
    function init() {
    google.earth.createInstance('chart', initCB, failureCB);
    //return ge;
    }

    function initCB(instance) {
         var ge = instance;
         ge.getWindow().setVisibility(true);
         if (currentKmlObject) {
            ge.getFeatures().removeChild(currentKmlObject);
            currentKmlObject = null;
            }
            var kmlString = ''
              + '<?xml version="1.0" encoding="UTF-8"?>'
              + '<kml xmlns="http://www.opengis.net/kml/2.2">'

              + '<Document>'
              + '  <Camera>'
              + '    <longitude>-122.444633</longitude>'
              + '    <latitude>37.801899</latitude>'
              + '    <altitude>139.629438</altitude>'
              + '    <heading>-70.0</heading>'
              + '    <tilt>75</tilt>'
              + '  </Camera>'

              + '  <Placemark>'
              + '    <name>Placemark from KML string</name>'
              + '    <Point>'
              + '      <coordinates>-122.448425,37.802907,0</coordinates>'
              + '    </Point>'
              + '  </Placemark>'

              + '</Document>'
              + '</kml>';
         google.earth.fetchKml(ge, 'http://www.shelter-associates.org/slumData/imagesandearth/Sangli/Indira_Nagar_Miraj/Indira_Nagar_Miraj.kml', finishFetchKml);

         //return ge
    }

    function failureCB(errorCode) {
    }


function finishFetchKml(kmlObject) {
  // check if the KML was fetched properly
  if (kmlObject) {
    // add the fetched KML to Earth
    var currentKmlObject = kmlObject;
    ge.getFeatures().appendChild(currentKmlObject);
    if (currentKmlObject.getAbstractView())
   ge.getView().setAbstractView(currentKmlObject.getAbstractView());
  } else {
    // wrap alerts in API callbacks and event handlers
    // in a setTimeout to prevent deadlock in some browsers
    setTimeout(function() {
      alert('Bad or null KML.');
    }, 0);
  }
  //imageDescriptionAjax2(q, 'description');
}

    
    //var place = arr;
    //var headID = document.getElementById('chart');//[0];
    //var newScript = document.createElement('script');
    //newScript.type = 'text/javascript';
    //newScript.src = "http://www.gmodules.com/ig/ifr?url=http://code.google.com/apis/kml/embed/embedkmlgadget.xml&;up_kml_url=http%3A%2F%2Fwww.shelter-associates.org%2FslumData%2Fimagesandearth%2F"+arr+"&;up_view_mode=earth&;up_earth_2d_fallback=0&;up_earth_fly_from_space=1&;up_earth_show_nav_controls=1&;up_earth_show_buildings=0&;up_earth_show_terrain=0&;up_earth_show_roads=1&;up_earth_show_borders=1&;up_earth_sphere=earth&;up_maps_zoom_out=0&;up_maps_default_type=map&;synd=open&;w=500&;h=400&;title=Indira+Gharkul%2C+Miraj&;border=%23ffffff%7C3px%2C1px+solid+%23999999&;output=js";
    //newScript.src = "http://www.gmodules.com/ig/ifr?url=http://code.google.com/apis/kml/embed/embedkmlgadget.xml&amp;up_kml_url=http%3A%2F%2Fwww.shelter-associates.org%2FslumData%2FslumWide%2Fmiraj%2FindiraGharkul%2FindiraGharkulWebsite.kml&amp;up_view_mode=earth&amp;up_earth_2d_fallback=0&amp;up_earth_fly_from_space=1&amp;up_earth_show_nav_controls=1&amp;up_earth_show_buildings=0&amp;up_earth_show_terrain=0&amp;up_earth_show_roads=1&amp;up_earth_show_borders=1&amp;up_earth_sphere=earth&amp;up_maps_zoom_out=0&amp;up_maps_default_type=map&amp;synd=open&amp;w=500&amp;h=400&amp;title=Indira+Gharkul%2C+Miraj&amp;border=%23ffffff%7C3px%2C1px+solid+%23999999&amp;output=js";
    //newScript.src = "http://www.gmodules.com/ig/ifr?url=http://code.google.com/apis/kml/embed/embedkmlgadget.xml&up_kml_url=http%3A%2F%2Fearth.google.com%2Fgallery%2Fkmz%2Fwomen_for_women.kmz&up_view_mode=earth&up_earth_2d_fallback=0&up_earth_fly_from_space=1&up_earth_show_nav_controls=1&up_earth_show_buildings=1&up_earth_show_terrain=1&up_earth_show_roads=1&up_earth_show_borders=1&up_earth_sphere=earth&up_maps_zoom_out=0&up_maps_default_type=map&synd=open&w=500&h=400&title=Embedded+KML+Viewer&border=%23ffffff%7C3px%2C1px+solid+%23999999&output=js"
    //newScript.src = "http://www.gmodules.com/ig/ifr?url=http://code.google.com/apis/kml/embed/embedkmlgadget.xml&up_kml_url=http%3A%2F%2Fwww.shelter-associates.org%2FslumData%2FslumWide%2Fmiraj%2FindiraGharkul%2FindiraGharkulWebsite.kml&up_view_mode=earth&up_earth_2d_fallback=0&up_earth_fly_from_space=1&up_earth_show_nav_controls=1&up_earth_show_buildings=0&up_earth_show_terrain=0&up_earth_show_roads=1&up_earth_show_borders=1&up_earth_sphere=earth&up_maps_zoom_out=0&up_maps_default_type=map&synd=open&w=500&h=400&title=Indira+Gharkul%2C+Miraj&border=%23ffffff%7C3px%2C1px+solid+%23999999&output=js"
    //headID.appendChild(newScript);
    imageDescriptionAjax2(q, 'description');
}


function imageDescriptionAjax(q, div, type)

{
hideAndClear("ifrm");
if (div==="")
  {
  document.getElementById("txtHint").innerHTML="";//TODO - fix this so sideMenu is reloaded
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    //var result=xmlhttp.responseText;
    //result=eval("("+result+")");
    document.getElementById(div).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET", "imagesDescripts.php?q="+q, true);
xmlhttp.send();
if (type===1||type===2){

        $('#chart').text('');
        $('#chart1').text('');
        $('#chart2').text('');
}

}

function imageDescriptionAjax2(q, div)

{
    hideAndClear("ifrm");

if (div==="")
  {
  document.getElementById("txtHint").innerHTML="";//TODO - fix this so sideMenu is reloaded
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp2=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp2.onreadystatechange=function()
  {
  if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
    {
    //var result=xmlhttp.responseText;
    //result=eval("("+result+")");
    document.getElementById(div).innerHTML=xmlhttp2.responseText;
    }
  }
xmlhttp2.open("GET", "imagesDescripts.php?q="+q, true);
xmlhttp2.send();
}


function imageDescriptionAjax3(q, div, type, note)

{
hideAndClear("ifrm");
if (div==="")
  {
  document.getElementById("txtHint").innerHTML="";//TODO - fix this so sideMenu is reloaded
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp4=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp4=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp4.onreadystatechange=function()
  {
  if (xmlhttp4.readyState==4 && xmlhttp4.status==200)
    {
    //var result=xmlhttp.responseText;
    //result=eval("("+result+")");
    document.getElementById(div).innerHTML=xmlhttp4.responseText;
    }
  }
xmlhttp4.open("GET", "imagesDescripts.php?q="+q, true);
xmlhttp4.send();
if (type===1||type===2){
    
        $('#chart').text('');
        $('#chart1').text('');
        $('#chart2').text('');
}
chartNotes(note);
}
