

///tabs///
/*function init () {
	var tabs = document.getElementsByClassName('tabs');
	for (var i = 0; i < tabs.length; i++) {
		$(tabs[i].id).onclick = function () {
			getTabData(this.id);
		}
	}
	getTabData('query_params');
	getTabData('aggregations');
	loadXMLDoc('aggs', 'aggs.xml', onAggsResponse);
	document.generatedQry.qryString.value='';
 
	setTimeout("init2();", 1000);
}

var imgCalendar_Click = function(e, input)
{
    showCalendar("calendar", input, "calendar-container", Event.element(e));
};

function init2() {

	Event.observe("imgDate", "click", imgCalendar_Click.bindAsEventListener(this, "date"));
	Event.observe("imgSDate", "click", imgCalendar_Click.bindAsEventListener(this, "startDate"));
	Event.observe("imgEDate", "click", imgCalendar_Click.bindAsEventListener(this, "endDate"));
}


function getTabData(id) {
	showLoad();
	var contentDivName = id + 'Content';
	if ($(contentDivName).innerHTML == "")
	{
		var url = 'qrygen/'+ id +'.shtml';//this can be changed to a .php, or .asp, etc for dynamic content and parameters
		var rand = Math.random(9999);
		var pars = 'id=' + id + '&rand=' + rand;
		var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: showResponse} );
	}
	
	var tabs = document.getElementsByClassName('tabs');
	for (var i = 0; i < tabs.length; i++) 
	{
		if (tabs[i].id == id)
		{
			tabs[i].style.background = 'white';
		}
		else
		{
			tabs[i].style.background = '#6699CC;';
		}
	}	
	
	var contentDivs = document.getElementsByClassName('content');
	for (var i = 0; i < contentDivs.length; i++) 
	{	
		if (contentDivs[i].id == contentDivName)
		{
			contentDivs[i].style.display = '';
		}
		else
		{
			contentDivs[i].style.display = 'none';
		}

	}
	stopLoad();
}
function showLoad () {
	$('load').style.display = 'block';
}
function stopLoad(){
	$('load').style.display = 'none';
	
}
function showResponse (originalRequest) {
	showLoad();
	var id = originalRequest.request.parameters.id + 'Content';
	var newData = originalRequest.responseText;
	$(id).innerHTML = newData;
	stopLoad();
}

*/
////mapping code lists from php xml////
var http_request = false;
function makeRequest(url) {
	alert('make request');
   http_request = false;
   alert (http_request);
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
         http_request.overrideMimeType('text/xml');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
   alert(http_request.readyState +''+ url);
   http_request.onreadystatechange = alertContents();
   http_request.open('GET', url, true);
   http_request.send(null);
}
function alertContents() {
	alert('alert Contents ' + http_request.status);
	
    if (http_request.readyState == 4) {
       if (http_request.status == 200) {

          var xmldoc = http_request.responseXML;
          var parameters = xmldoc.getElementsByTagName('parameters').item(0);

          for (var iNode = 0; iNode < parameters.childNodes.length; iNode++) {
             var node = parameters.childNodes.item(iNode);
             for (i = 0; i < node.childNodes.length; i++) {
                var sibl = node.childNodes.item(i);
                var len = parseInt(sibl.childNodes.length / 2);
                var arr = new Array(len);
                var cnt = 0;
                for (x = 0; x < sibl.childNodes.length; x++) {
                   var sibl2 = sibl.childNodes.item(x);
                   var sibl3;
                   if (sibl2.childNodes.length > 0) {
                      sibl3 = sibl2.childNodes.item(0);
                      arr[cnt] = sibl3.data;   
                      cnt++;
                   }
                }
                addrow("mytable", arr);
             }
          }
       } else {
          alert('There was a problem with the request.');
       }
    }
 }
 function do_xml() {
	 alert('do xml');
    makeRequest('/data/parameter_list.xml');
 }
 function addrow(tablename, arr) {
 var tbl = document.getElementById(tablename);
 var lastRow = tbl.rows.length;
 var row = tbl.insertRow(lastRow);
    for (r = 0; r < arr.length; r++) {   
       var cell = row.insertCell(r);
       cell.innerHTML = arr[r];
    }
 }
 
 ///form elements///
 function getSelectedValues(pformId) {
	 var retval= '';
	 var idsValue= 'id=';
	 var fieldsValue = 'fields=';
	 var vQryStr=''
	 /*Column Form Processed*/
	 var inputs = $(pformId).getElementsByClassName('NPRId');
	 for(var i = 0, il = inputs.length; i < il; i++){
		  /*is this checkbox checked?*/
			 if (inputs[i].checked) {
			 if (idsValue != 'id=') {
				 idsValue += '%2C';
			 }
			 idsValue += inputs[i].value;
			 }
	 }

	 var inputs = $(pformId).getElementsByClassName('fields');
	 for(var i = 0, il = inputs.length; i < il; i++){
		  /*is this checkbox checked?*/
			 if (inputs[i].checked) {
			 if (fieldsValue != 'fields=') {
				 fieldsValue += '%2C';
			 }
			 fieldsValue += inputs[i].value;
			 }
	 }	 
	 var showAmpersand = false;
	 if (idsValue != 'id=')
	 {
		 retval += idsValue;
		 showAmpersand = true; 
	 }
	 
	 if (fieldsValue != 'fields=')
	 {
		 if (showAmpersand)
		 {
			 retval += '&'; 
		 }
		 retval += fieldsValue;
		 showAmpersand = true;
	 }	 
	 
	 var inputs = genQry.getElementsByTagName('input');	 
	 for(var i = 0, il = inputs.length; i < il; i++){
		 if (inputs[i].type == 'text'  && inputs[i].id != 'qryString')
		 {
			 if (inputs[i].value != '')
			 {
				 if (showAmpersand)
				 {
					 retval += '&'; 
				 }
				 encVal=escape(inputs[i].value);
				 retval += inputs[i].id + '=' + encVal;
				 showAmpersand = true;
			 }
		 }
	 }
	 for(var i = 0, il = inputs.length; i < il; i++){
		 if (inputs[i].type == 'radio')
		 {
			 if (inputs[i].checked)
			 {
				 if (showAmpersand)
				 {
					 retval += '&'; 
				 }
				 
				 retval += inputs[i].name + '=' + escape(inputs[i].value);
				 showAmpersand = true;
			 }
		 }
		 
	 }
	 var selList=document.getElementById("numResults");
	 if (selList.options[selList.selectedIndex].value != ' ') 
			 {
		      if (retval != ''){
		        retval += '&numResults='+selList.options[selList.selectedIndex].value;
		      }else{
		    	  retval += 'numResults='+selList.options[selList.selectedIndex].value; 
		      }
			 }
	 
	 
	 	vQryStr='http://www.npr.org/templates/xanadu/view/api/view.php?'
	 		 
	 	   /*var inputString=retval;  
	 	   var encodedInputString=escape(inputString);
	 	   encodedInputString=encodedInputString.replace("+", "%2B");
	 	   encodedInputString=encodedInputString.replace("/", "%2F"); 
	 	   retval=encodedInputString;*/
	 	document.generatedQry.qryString.value= vQryStr + retval;
	 	retval = '';

		return(false);
 }
 
 function clearQryStr()
 {
	 retval = '';
	 document.generatedQry.qryString.value = '';
	 response.style.display = 'none';
	 getTabData('topics');
	 var inputs = genQry.getElementsByTagName('input');
	 for(var i = 0, il = inputs.length; i < il; i++){
		  /*is this checkbox checked?*/
			 if (inputs[i].checked) {
				 inputs[i].checked = false;
				 }
			else if (inputs[i].type == 'text') {
				 inputs[i].value = '';
			 }

	 }
 }

 function runQuery()
 {

 
  if ($('qryString').value==null||$('qryString').value==""){
 	 alert('Use the tabs below to select values to append the API Query String.\nThen, click "Click API Call" to create the Query String.');
  }else{

		 response.style.display = 'block';
		 getTabData('response');
		 $('queryResponse').src = '';
		 $('queryResponse').src = $('qryString').value;

  }

 }
 function removeSpaces(string) {
 	var tstring = "";
 	string = '' + string;
 	splitstring = string.split(" ");
 	for(i = 0; i < splitstring.length; i++)
 		if (tstring != ""){
 			tstring += '+' + splitstring[i];
 		}
 		else{
 			tstring += splitstring[i];
 		}
 	return tstring;
 }
///mapping index///
 var xmlhttp;

 function loadXMLDoc(url)
 {
 xmlhttp=null;
 if (window.XMLHttpRequest)
   {// code for IE7, Firefox, Mozilla, etc.
   xmlhttp=new XMLHttpRequest();
   }
 else if (window.ActiveXObject)
   {// code for IE5, IE6
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
 if (xmlhttp!=null)
   {
   xmlhttp.onreadystatechange=onResponse();
   xmlhttp.open("GET",url,true);
   xmlhttp.send(null);

   }
 else
   {
   alert("Your browser does not support XMLHTTP.");
   }
 }

 function onResponse()
 {
	//alert(vdivName);
 if(xmlhttp.readyState!=4) return;
 if(xmlhttp.status!=200)
   {
   alert("Problem retrieving XML data");
   return;
   }
 /**** to debug parse in IE   

 if (xmlhttp.responseXML.parseError.errorCode != 0){
 	alert("Error Code:" + xmlhttp.responseXML.parseError.reason+ "Error line: " + xmlhttp.responseXML.parseError.srcText);
 } else {
 alert("XML Root Tag Name: " + xmlhttp.responseXML.documentElement.tagName);
 }
* */

 txt="<table border='1'>";
 x=xmlhttp.responseXML.getElementsByTagName('item');
 txt= txt + "<tr><td>ID</td><td>TITLE</td><td>DESCRIPTION</td></tr>";
 for (i=0;i<x.length;i++)
   {

   txt=txt + "<tr>";
      {
     try
       {
       txt=txt + "<td>" + x[i].getAttribute('id'); + "</td>";
       }
     catch (er)
       {
       txt=txt + "<td> </td>";
       }
     }
    xx=x[i].getElementsByTagName("title");
     {
     try
       {
       txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
       }
     catch (er)
       {
       txt=txt + "<td> </td>";
       }
     }
   
   xx=x[i].getElementsByTagName("additionalInfo");
     {
     try
       {
       txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
       }
     catch (er)
       {
       txt=txt + "<td> </td>";
       }
     }
     txt=txt + "</tr>";
   } 
 txt=txt + "</table>";

 document.getElementById('copy').innerHTML=txt;
 }

 