//<!--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 subAddOrDel(prev, newselect, column, table, querytype){
    var pattern=new RegExp("\\|"+column+":.*:"+table);//TODO WED - regex seems to be prob.
    var replace="|"+column+":"+newselect+":"+table+"";
    var v=prev;//TODOreturn types if no match, patten etc order in args
    var newstr=prev;
    newstr=prev.replace(pattern, replace);
    if(v===newstr&&v.match(pattern)===null){//fixed with == null? debug from here. something in menu
        //builder helping not working as no response from slumname select.
        newstr=v+"|"+column+":"+newselect+":"+table;
    }
    else{
        $('#description').text('');
        $('#chart').text('');
        $('#chart1').text('');
        $('#chart2').text('');
        hideAndClear("ifrm");
        var array=newstr.split("|");
        newstr="";
        var i=0;
        var end=false;
        pattern=new RegExp(column+":.*:"+table);
        while(end===false){
            if(array[i]!==""){
                newstr=newstr+"|"+array[i];
            }
            if(newstr.match(pattern)!==null){
                end=true;
            }
            i++;
        }
    }
    newstr=querytype+"-"+newstr;
    return newstr;
}

function passSelection(str, div, prev, column, table, querytype)
{
//var pattern=new RegExp("Data:.*:database_input_ui_fields");
//var x=false;
//if(prev.match(pattern)!==null){
//    x=true;
//}


if (str==="")
  {
  document.getElementById("txtHint").innerHTML="";//TODO - fix this so sideMenu is reloaded
  return;
  }
  else{
      str=subAddOrDel(prev, str, column, table, querytype);
  }
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)
    {
    document.getElementById(div).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","menuBuilder.php?q="+str,true);
xmlhttp.send();
//read these variables out of the innerhtml response - TODO - look this up
//drawChart(<?php echo($php_array.", ".$subject.", ".$column);?>);
//alert("The div has been loaded.");
//if(x===true){
    //var headID = document.getElementsByTagName("head")[0];
    //var newScript = document.createElement('script');
    //newScript.type = 'text/javascript';
    //newScript.src = 'http://localhost/phpproject2/js/drawChart.js';
    //headID.appendChild(newScript);
//}
}

function passSelection2(str, div)
{
//var pattern=new RegExp("Data:.*:database_input_ui_fields");
//var x=false;
//if(prev.match(pattern)!==null){
//    x=true;
//}


if (str==="")
  {
  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)
    {
    document.getElementById(div).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","menuBuilder.php?q="+str,true);
xmlhttp.send();
//read these variables out of the innerhtml response - TODO - look this up
//drawChart(<?php echo($php_array.", ".$subject.", ".$column);?>);
//alert("The div has been loaded.");
//if(x===true){
    //var headID = document.getElementsByTagName("head")[0];
    //var newScript = document.createElement('script');
    //newScript.type = 'text/javascript';
    //newScript.src = 'http://localhost/phpproject2/js/drawChart.js';
    //headID.appendChild(newScript);
//}
}

function chartNotes(str)
{
//var pattern=new RegExp("Data:.*:database_input_ui_fields");
//var x=false;
//if(prev.match(pattern)!==null){
//    x=true;
//}


if (str==="")
  {
  document.getElementById("chartnotes").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)
    {
    document.getElementById("chartnotes").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","chartNotes.php?q="+str,true);
xmlhttp.send();
//read these variables out of the innerhtml response - TODO - look this up
//drawChart(<?php echo($php_array.", ".$subject.", ".$column);?>);
//alert("The div has been loaded.");
//if(x===true){
    //var headID = document.getElementsByTagName("head")[0];
    //var newScript = document.createElement('script');
    //newScript.type = 'text/javascript';
    //newScript.src = 'http://localhost/phpproject2/js/drawChart.js';
    //headID.appendChild(newScript);
//}
}

