<?php
define('idDiGIR_testClient','$Id: testClient.php,v 1.2 2003/05/06 20:33:23 vieglais Exp $');
if (defined('idDIGIR_VERSION_REQUEST'))
    return;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php
include_once('DiGIR_utils.php');	

//set server for javascript generation
$UDDIjs = "DiGIR_uddiSearch.php";
$ClientJS = "DiGIR_clientJS.php";
$defaultDomain = "http://digir.sourceforge.net/fed/beta3/darwindomain.xml";
$domain = getvar('domaindoc',NULL);
$searchUDDI = getvar('searchUDDI',1);
if (is_null($domain))
{
?>
<html>
<head><title>DiGIR Test Client</title></head>
<body>
<p>Enter location of domain document to use:</p>
<form method='GET'>
<?php
	echo "<input id='domaindoc' name='domaindoc' ".
		"type='text' value='".htmlentities($defaultDomain)."' size='80' /><br />";
	echo "Load targets from UDDI? <input type='checkbox' name='searchUDDI' value='0' /><br />";
	echo "<input type='submit' value='OK' />";
	echo "</form>";
	echo "</body></html>";
	die();
}
?>
<html>
<head>
	<title>DiGIR Client</title>
<?php
echo '<script language="javascript" src="'.$UDDIjs.'?format=javascript&listres=1&searchUDDI='.$searchUDDI.'">';
echo '</script>';
echo "<script language='javascript' src='".$ClientJS."?metadata=1&domain=".$domain."'>";
echo "</script>";
?>
<script language="javascript">
/**
* $Id: testClient.php,v 1.2 2003/05/06 20:33:23 vieglais Exp $
*
* == License ==
* Copyright (c) 2002 The University of Kansas Natural History Museum and
* Biodiversity Research Center.  All rights reserved.
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* with the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* *  Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimers.
* 
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
* 
* * Neither the names of The University of Kansas Natural History Museum and
* Biodiversity Research Center, The University of Kansas at Lawrence, nor the
* names of its contributors may be used to endorse or promote products derived
* from this Software without specific prior written permission.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/


function makeCOPNode(qf,i,dns)
{
	var COPop = "";
	var Cpt = "";
	var term = "";
	var nullterm = false;
	var itm = qf.item("cop"+i);
	if ((itm == "undefined") || (itm==null))
		return null;
	COPop = itm.options[itm.selectedIndex].value;
	if (dns)
		COPop = dns + ":" + COPop;

	itm = qf.item("cpt"+i);
	if (itm.selectedIndex == 0)
	   return null;
	Cpt = itm.options[itm.selectedIndex].value;
	itm = qf.item("term"+i);
	term = itm.value;
	if (term == "NULL")
	{
		term = "";
		nullterm = true;		
	}
	var res = new DiGIRCOP(COPop,Cpt,term,nullterm);
	return res;
}

function makeLOPNode(qf,i,dns)
{
	var itm = qf.item("lop" + i);
	if ((itm=="undefined") || (itm == null))
		return null;
	var res = itm.options[itm.selectedIndex].value;
	if ((res == null) || (res == undefined) || (res.length <=0) || (res == "nop"))
		return null;
	if (dns)
		res = dns + ":" + res;
	var node = new DiGIRLOP(res);
	return node;
}

function makeDiGIRFilterStructForm(qf)
{
	var struct = new DiGIRFilter();
	struct.namespaces = g_conceptSet['schemaNamespaces'];
	struct.addNamespace('xsi',g_XmlSchemaInstanceNS);
	var idx=1;
	var cnode = null;
	var lnode = null;
	do
	{
		cnode = makeCOPNode(qf,idx,null);
		lnode = makeLOPNode(qf,idx,null);
		if (lnode != null)
			struct.addNode(lnode);
		if (cnode != null)
			struct.addNode(cnode);
		
		idx = idx + 1;
	}
	while ((lnode != null) && (cnode != null));
	return struct;
}


/*************************************************************/

/**
* called when endpoint selection is changed in header structure form to adjust 
* the endPoint field value.  Also cascades to update the resource value.
*/
function doChangeTarget(hf)
{
	hf.endPoint.value=hf.endPoints.options[hf.endPoints.selectedIndex].value;
	//remove resources from resource list
	hf.resList.options.length = 0;
	//add resources for target
	//first, find the target
	var i=0;
	for (i=0; i < g_endPoints.length; i++)
	{
		if (g_endPoints[i]['accessPoint'] == hf.endPoint.value)
		{
			var apd;
			for (apd in g_endPoints[i]['resources'])
			{
				var opt = document.createElement('OPTION');
				hf.resList.options.add(opt);
				var nm = g_endPoints[i]['resources'][apd]['name'];
				var cd = g_endPoints[i]['resources'][apd]['code'];
				opt.innerText = "[" + cd + "] " + nm;
				opt.value = cd;
			}
			doChangeResource(hf);
			return;
		}
	}
}

function doChangeResourceX(hf,resName)
{
	hf = document.headerForm;
	hf.resCode.value=resName;
}

function doChangeTargetX(hf,ep)
{
	//hf.endPoint.value=hf.endPoints.options[hf.endPoints.selectedIndex].value;
	hf.endPoint.value = ep;
	hf.resCode.value='';
}

/**
* Generates a dialog presenting a list of targets to select from.
*/
function targetList(hf,e)
{
	var sx = e.screenX;
	var sy = e.screenY;
	var sspos = ",left="+sx+",top="+sy+",screenX="+sx+",screenY="+sy;
	var w = window.open("","TargetList","width=200,height=250"+sspos);
	var d = w.document;
	d.open();
	d.write("<html><head><title>Select Target...</title><scr");
	d.write("ipt language='javascript'>\n");
	d.write("var g_tmpHForm = null;\n");
	d.write("function doSelectTarget()\n");
	d.write("{\n");
	d.write("var ep=document.selform.endPoints.options[document.selform.endPoints.selectedIndex].value;\n");
	d.write("opener.doChangeTargetX(g_tmpHForm,ep);\n");
	d.write("window.close();\n");
	d.write("\n");
	d.write("}\n");
	d.write("</scr");
	d.write("ipt></head>\n");
	d.write("<body>\n");
	d.write("<form name='selform'>\n");
	d.write("<select name='endPoints' size='10'/><br />\n");
	d.write("<input type='button' value='Select' onClick='doSelectTarget();' />");
	d.write("</form>\n");
	d.write("</body></html>");
	d.close();
	for (ep in g_endPoints)
	{
		var opt = d.createElement("OPTION");
		opt.value = g_endPoints[ep]['accessPoint'];
		opt.text = g_endPoints[ep]['name'];
		d.selform.endPoints.options.add(opt);
	}
	w.g_tmpHForm = hf;
}

function resourceList(hf,e)
{
	var sx = e.screenX;
	var sy = e.screenY;
	var sspos = ",left="+sx+",top="+sy+",screenX="+sx+",screenY="+sy;
	var w = window.open("","ResourceList","width=200,height=250"+sspos);
	var d = w.document;
	d.open();
	d.write("<html><head><title>Select resource...</title><scr");
	d.write("ipt language='javascript'>\n");
	d.write("var g_tmpHForm = null;\n");
	d.write("function doSelectTarget()\n");
	d.write("{\n");
	d.write("var ep=document.selform.resList.options[document.selform.resList.selectedIndex].value;\n");
	d.write("opener.doChangeResourceX(g_tmpHForm,ep);\n");
	d.write("window.close();\n");
	d.write("\n");
	d.write("}\n");
	d.write("</scr");
	d.write("ipt></head>\n");
	d.write("<body>\n");
	d.write("<form name='selform'>\n");
	d.write("<select name='resList' size='10'/><br />\n");
	d.write("<input type='button' value='Select' onClick='doSelectTarget();' />");
	d.write("</form>\n");
	d.write("</body></html>");
	d.close();
	var i=0;
	for (i=0; i < g_endPoints.length; i++)
	{
		if (g_endPoints[i]['accessPoint'] == hf.endPoint.value)
		{
			var apd;
			for (apd in g_endPoints[i]['resources'])
			{
				var opt = d.createElement('OPTION');
				d.selform.resList.options.add(opt);
				var nm = g_endPoints[i]['resources'][apd]['name'];
				var cd = g_endPoints[i]['resources'][apd]['code'];
				opt.innerText = "[" + cd + "] " + nm;
				opt.value = cd;
			}
			return;
		}
	}
	w.g_tmpHForm = hf;
}

function buildRecordFormat(hf,e)
{
	var sx = e.screenX;
	var sy = e.screenY;
	var sspos = ",left="+sx+",top="+sy+",screenX="+sx+",screenY="+sy;
	var w = window.open("","ResourceList","width=500,height=300"+sspos);
	var d = w.document;
	d.open();
	d.write("<html><head><title>Select resource...</title><scr");
	d.write("ipt language='javascript'>\n");
	d.write("var g_tmpHForm = null;\n");
	d.write("function doSelectPredefined()\n{\n");
	d.write("var v = document.selform.resPredef.options[document.selform.resPredef.selectedIndex].value; \n");
	d.write("opener.setPredefined(v);\n");
	d.write("window.close();\n");
	d.write("}\n");
	d.write("function doSelectCustom()\n");
	d.write("{\nvar v=new Array();\n");
	d.write("var sl = document.selform.resSelected.options.length;\n");
	d.write("for (var k=0; k<sl; k++)\n");
	d.write("{\n");
	d.write("  v[v.length] = document.selform.resSelected.options[k].value;\n");
	d.write("}\n");
	d.write("opener.setCustom(v.toString());\n");
	d.write("window.close();\n");
	d.write("\n");
	d.write("}\n");
	d.write("function addConcept()\n{\n");
	d.write("var v = document.selform.resList.options[document.selform.resList.selectedIndex].value;\n");
	d.write("for (var k in document.selform.resSelected.options)\n{\n");
	d.write("if ((document.selform.resSelected.options[k] != null) && (document.selform.resSelected.options[k].value == v))\n");
	d.write("return;\n}\n");
	d.write("\n");
	d.write("\n");
	d.write("var opt = document.createElement('OPTION');\n");
	d.write("document.selform.resSelected.options.add(opt);\n");
	d.write("opt.innerText=v;\n opt.value=v;\n");
	d.write("\n");
	d.write("}\n");
	d.write("function removeConcept()\n{\n");
	d.write("if (document.selform.resSelected.selectedIndex < 0) return;\n");
	d.write("document.selform.resSelected.options[document.selform.resSelected.selectedIndex]=null\n");
	d.write("}\n");
	d.write("</scr");
	d.write("ipt></head>\n");
	d.write("<body>\n");
	d.write("<form name='selform'>\n");
	d.write("<table boder='1'><tr>");
	d.write("<tr><td colspan='3'>");
	d.write("<select name='resPredef' size='1' />");
	d.write("</td></tr>\n");
	d.write("<tr><td><input type='button' value='Use Predefined' onclick='doSelectPredefined()' /></td><td></td><td></td></tr>");
	d.write("<td><select name='resList' size='10' ondblclick='addConcept();'/></td>\n");
	d.write("<td valign='center' halign='center'><input type='button' value='-->' onclick='addConcept();'/>");
	d.write("<br /><input type='button' value='<--' onclick='removeConcept();'/></td>\n");
	d.write("<td><select name='resSelected' size='10' ondblclick='removeConcept();'/></td>\n");
	d.write("</tr><tr>");
	d.write("<td><input type='button' value='Cancel' onClick='window.close();' /></td><td></td><td><input type='button' value='Use Custom' onClick='doSelectCustom();' /></td>");
	d.write("</tr></table>");
	d.write("</form>\n");
	d.write("</body></html>");
	d.close();
	for (var pdef in g_defaultRecordFormats)
	{
		var opt = d.createElement('OPTION');
		d.selform.resPredef.options.add(opt);
		opt.innerText = pdef + " [" + g_defaultRecordFormats[pdef] + "]";
		opt.value = g_defaultRecordFormats[pdef];
	}
	for (var cset in g_conceptSet['concepts'])
	{
		for (var k in g_conceptSet['concepts'][cset])
		{
			if (g_conceptSet['concepts'][cset][k]['returnable'] == 1)
			{
				//add this to the select box
				var opt = d.createElement('OPTION');
				d.selform.resList.options.add(opt);
				opt.innerText = k;
				opt.value = k;
			}
		}
	}
}

/**
* Called when the operation is changed in the the header form.  Turns on/off
* options appropriate for each operation type.
*
* @param hf form object	The header form.
* @param ff form object	The filter form.
* @param rf form object The record structure form.
*/
function doChangeOperation(hf,ff,rf)
{
	var itm = hf.item('opType');
	var op = itm.options[itm.selectedIndex].value;
	if (op == 'metadata')
	{
		//hf.item('resList').disabled=true;
		hf.item('resCode').disabled=true;
		ff.disabled = true;
		rf.disabled = true;
	}
	else
	{
		//hf.item('resList').disabled=false;
		hf.item('resCode').disabled=false;
		ff.disabled = false;
		rf.disabled = false;
	}
}


/*************************************************************/

/**
* called to initialize the header form with values loaded from javascript resources.
*/
function initializeHeaderForm(hf)
{
}

/**
* Initializes a form by adding the namespace values to the textArea called "uiNamespaces"
* and adds the concepts to the concept select boxes called "cptx" where x is an integer
* from 1 to n, where n is the number of concept + operator + term sections in the form.
* 
* @param form qf The for that is to be initialized.
*/
function initializeQueryForm(qf)
{
	stmp = "";
	for (ns in g_conceptSet['schemaNamespaces'])
	{
		stmp += "xmlns:" + ns + "='" + g_conceptSet['schemaNamespaces'][ns] + "'\n";
	}
	qf.uiNamespaces.value = stmp;
	qf.uiNamespaces.readOnly = true;
    var idx = 1;
    var itm = qf.item("cpt"+idx);
    //add options to each of the "cptxx" select boxes...
    while ((itm != null) && (itm!="undefined"))
    {
    	for (ns in g_conceptSet['concepts'])
    	{
    		var cset = g_conceptSet['concepts'][ns];
	        for (i in cset)
    	    {
    	    	if (cset[i]['searchable'])
    	    	{
	        		var opt = document.createElement("OPTION");
    	    		opt.value = i;
	    	    	opt.text = i;
    	    		itm.options.add(opt);
    	    	}
        	}
        }
        idx ++;
        itm = qf.item("cpt"+idx);
    }
}

//insert the first default record format
function initializeRecordForm(qf)
{
	var rf;
	for (var k in g_defaultRecordFormats)
	{
		if (qf.rFormat.value != '')
			return;
		qf.rFormat.value = g_defaultRecordFormats[k];
	}
}

function setPredefined(v)
{
	document.recordForm.customR.value = 0;
	document.recordForm.rFormat.value = v;
}

function setCustom(v)
{
	document.recordForm.customR.value = 1;
	document.recordForm.rFormat.value = v;
}

/**
* Initializes all the forms
*/ 
function initializeForms(hf,ff,rf)
{
	initializeHeaderForm(hf,ff,rf);
	initializeQueryForm(ff);
	initializeRecordForm(rf);
	doChangeOperation(hf,ff,rf);
}

/*************************************************************/


function makeDiGIRRecordStructForm(qf)
{
	var istart = qf.rStart.value;
	var imax = qf.rMax.value;
	var bcount = false;
	if (qf.rCount.checked)
		bcount = true;
	
	var rs = new DiGIRRecordStruct(istart,imax,bcount);
	rs.namespaces = g_conceptSet['schemaNamespaces'];
	//get selected format
	var selFormat = qf.customR.value;
	if (selFormat == 1)
	{
		//build a custom record format from the selected items
		var aData = qf.rFormat.value.split(",");
		for (var k in aData)
		{
			var nd = new DiGIRRecordElement(aData[k]);
			rs.addNode(nd);
		}
	}
	else
	{
		rs.setRef(qf.rFormat.value);
	}
	return rs;
}

function createEnvelope(hForm,qForm,rForm)
{
	var ep = hForm.item("endpoint").value;
	var itm = hForm.item("opType");
	var op = itm.options[itm.selectedIndex].value;
	var rescode = hForm.item("resCode").value;
	
	var envelope = "";
	if (op == "metadata")
	{
		envelope = DiGIRRequestMetadata(ep);
	}
	else if (op == "search")
	{
		var recStruct = makeDiGIRRecordStructForm(rForm);
		var filtStruct = makeDiGIRFilterStructForm(qForm);
		envelope = DiGIRRequestSearch(ep,rescode,g_conceptSet['namespaces'],recStruct,filtStruct);
	}
	else if (op == "scan")
	{
		var recStruct = makeDiGIRRecordStructForm(rForm);
		var filtStruct = makeDiGIRFilterStructForm(qForm);
		envelope = DiGIRRequestScan(ep,rescode,g_conceptSet['namespaces'],recStruct,filtStruct);
	}
	return envelope;
}

function showEnvelope(hForm,qForm,rForm)
{
	var ep = hForm.item("endpoint").value;
	var envelope = createEnvelope(hForm,qForm,rForm)
	var w = window.open("","_new");
	var d = w.document;
	var env_htm = htmlEncodeString(envelope);
	d.open();
	d.write("<ht"+"ml><head><title>Request Envelope Structure</title></head><body>");
	d.write("<h3>XML Structure</h3>");
	d.write("<form name='inpd'>");
	d.write("<textarea rows='20' cols='90' name='inreq'>");
	d.write(env_htm);
	d.write("</textarea>");
	d.write("</form>");
//	d.write("<h3>URL Encoded</h3>");
//	d.write("<p>");
//	d.write(escape(env_htm));
//	d.write("</p>");
	d.write("<p>");
	d.write("<form method='GET' action='" + ep + "'>");
	d.write("<input name='doc' id='doc' type='hidden' value=\"" + env_htm + "\" />");
	d.write("Debug? <input type='checkbox' name='debug' value='1' />");
	d.write("Cache? <input type='checkbox' name='cache' value='1' />");
	d.write("<input type='button' value='Submit via GET' onclick='doc.value=document.inpd.inreq.value; submit();'/>");
	d.write("</form>");
	d.write("<br />");
	d.write("<form method='POST' action='" + ep + "'>");
	d.write("<input name='doc' id='doc' type='hidden' value=\"" + env_htm + "\" />");
	d.write("Debug? <input type='checkbox' name='debug' value='1' />");
	d.write("Cache? <input type='checkbox' name='cache' value='1' />");
	d.write("<input type='button' value='Submit via POST' onclick='doc.value=document.inpd.inreq.value; submit();'/>");
	d.write("</form>");
	d.write("</p>");
	d.write("</body></ht"+"ml>");
	d.close();
}

</script>
</head>

<body>
<table border='0'>
<tr>
<td>
<!-- Form for request <header> structure -->
<table border='0'>
<form name="headerForm">
<tr>
<td>Operation</td>
<td><select 
		name="opType" 
		onChange='doChangeOperation(document.headerForm,document.qForm,document.recordForm);'>
<option value="metadata" selected="1">Metadata</option>
<option value="search">Search</option>
<option value="scan">Scan</option>
</select></td>
</tr>
<tr>
<td>Provider</td>
<td>
  <input type='text' value='' name='endPoint' size='60' />
    <input type='button' value='Select...' onclick='targetList(document.headerForm,event)' />
</td>
</tr>
<tr>
  <td>Resource</td>
  <td>
    <input type='text' value='' name='resCode' size='60' />
    <input type='button' value='Select...' onclick='resourceList(document.headerForm,event)' />
  </td>
</tr>
<tr></tr>
</form>
</table>
</td></tr>

<!-- Form for request <filter> structure -->
<tr>
<td><br />Define Filter...</td>
</tr>
<tr><td>
<table border='0'>
<form name="qForm" id="qForm">
<tr>
<td colspan="4">
<textarea name="uiNamespaces" cols="60">
</textarea>
</td>
</tr>
<tr>
<td>
<select name="cpt1" >
<option value="nop"></option>
</select>
</td>
<td>
<select name="cop1" />
<option value="lt">&lt;</option>
<option value="lteq">&lt;=</option>
<option value="eq">=</option>
<option value="neq">&lt;&gt;</option>
<option value="gt">&gt;</option>
<option value="gteq">&gt;=</option>
<option value="like" selected="1">LIKE</option>
<option value="in">IN</option>
</select>
</td>
<td>
<input type="text" name="term1" />
</td>
<td>
<select name="lop1">
<option value="nop" selected="1"></option>
<option value="and">and</option>
<option value="or">or</option>
<option value="andnot">and not</option>
<option value="ornot">or not</option>
</select>
</td>
<td />
</tr>
<tr>
<td>
<select name="cpt2" >
<option value="nop"></option>
</select>
</td>
<td>
<select name="cop2" />
<option value="lt">&lt;</option>
<option value="lteq">&lt;=</option>
<option value="eq">=</option>
<option value="neq">&lt;&gt;</option>
<option value="gt">&gt;</option>
<option value="gteq">&gt;=</option>
<option value="like" selected="1">LIKE</option>
<option value="in">IN</option>
</select>
</td>
<td>
<input type="text" name="term2" />
</td>
<td>
<select name="lop2">
<option value="nop" selected="1"></option>
<option value="and">and</option>
<option value="or">or</option>
<option value="andnot">and not</option>
<option value="ornot">or not</option>
</select>
</td>
</tr>
<tr>
<td>
<select name="cpt3" >
<option value="nop"></option>
</select>
</td>
<td>
<select name="cop3" />
<option value="lt">&lt;</option>
<option value="lteq">&lt;=</option>
<option value="eq">=</option>
<option value="neq">&lt;&gt;</option>
<option value="gt">&gt;</option>
<option value="gteq">&gt;=</option>
<option value="like" selected="1">LIKE</option>
<option value="in">IN</option>
</select>
</td>
<td>
<input type="text" name="term3" />
</td>
</tr></form>
</table>
</td></tr>
<!-- Form for request <filter> structure -->
<tr>
<td><br />Define Record Format...</td>
</tr>
<tr><td>
<form id='recordForm' name='recordForm'>
<input type='hidden' name='customR' value='0' />
<table border='0'>
<tr>
<td>Start</td><td>Max</td><td>Count?</td>
</tr>
<tr>
<td><input type='text' name='rStart' value='0' /></td>
<td><input type='text' name='rMax' value='10' /></td>
<td><input type='checkbox' name='rCount' /></td>
</tr>
<tr>
<td colspan='3'>
<textarea name='rFormat' cols='65' rows='2'>
</textarea><br />
<input type='button' value='Build record format...' onclick='buildRecordFormat(document.recordForm,event);' />
</td>
</tr>
</form>
</table>
</td></tr>
</table>
<form name='invokeForm'>
<input 
	type='button' 
	onclick='showEnvelope(document.headerForm,document.qForm,document.recordForm)' 
	value='Show Envelope' />
</form>
<script language='javascript'>
initializeForms(document.headerForm,document.qForm,document.recordForm)
</script>
</body>
</html>
