<HTML>
<!-- 
//========================================================================
// Collect a database command from user input
// Store the command is a "request" cookie
// Call DBaccess.php to process the command
// When done, DBaccess.php will redirect back to the caller (this page)
// Retrieve database output from the "response" cookie
// Display output 
// Important: request  data cannot include "+" nor "|" (used for parsing)
//            response data cannot include "." nor "|" (used for parsing) 
//========================================================================
-->
<HEAD>
<TITLE>Database Access</TITLE>

<style>
    a {color:brown; text-decoration:none}
</style>
<script src=cookieEscape.js></script>
</HEAD>

<body onload=receive()>

<h1>Database Access</h1>

<form  name=frm action=DBaccess.php>
<table>
<tr><td>Hostname<b>:</b>Port</td>
<td><input type=text name=host size=72>
<tr><td>Name / Pswd / Database</td>
<td><input type=text     name=user>
    <input type=password name=pswd>
    <input type=text     name=db>
<tr><td>SQL Command 
<td><textarea  name=cmd cols=70 rows=4></textarea>
<tr><td><input type=submit value=" Execute SQL " onclick=return(send())>
</table>
</form>
<hr>
<div id=table></div> 


<script>

//-----------------------------------------------------------------------------------------
//send: Store the form data into a request cookie before calling the server via action=
//-----------------------------------------------------------------------------------------
function send()
{
    host = document.frm.host.value;
    user = document.frm.user.value;
    pswd = document.frm.pswd.value;
    db   = document.frm.db.value;
    cmd  = document.frm.cmd.value;

    if (!host) host = 'localhost';

    if (!user || !pswd || !db || !cmd)
    {
        alert('Please enter user name, password, database and SQL command');
        return false;
    }

    request = host +'|'+ user +'|'+ pswd +'|'+ db +'|'+ cmd;	

    setCookie('request', request, 0);		//store in a cookie
}

//-------------------------------------------------------------------------------------------------
//receive: Obtain the data from response cookie, and display on screen
//         1.  If request cookie exists, repopulate the form with request data
//         2.  If response cookie exists, parse the cookie 
//         2a  Get the 1st row, and build column headers into "dataName" array
//         2b  Get all additional rows, and buid a 2 dim "data" array 
//-------------------------------------------------------------------------------------------------
function receive()
{
    req  = getCookie('request');		//get the request cookie
    resp = getCookie('response');		//get the response cookie

    if (req)					//if there is a request cookie
    {
        array = req.split('|');			//split the request on |    
        document.frm.host.value = array[0];	//repopulate the form fields
        document.frm.user.value = array[1];
        document.frm.pswd.value = array[2];
        document.frm.db.value   = array[3];
        document.frm.cmd.value  = array[4];
    }
    
    if (resp) 
    {
        resp = resp.replace(/\+/g,' ');		//change all + into ' '

        array = resp.split('|');		//split the response on | 

        var headers = array.shift();		//pop off the 1st line 
        dataName    = headers.split('.');	//split column headers on .

        data = new Array();			//2 dim array 
						//will contain the data 
        for (i=0; i<array.length-1; i++)	
            data[i] = array[i].split('.');	//split each line on .
    
        order = [ ];				//empty order array							

        sort(0);				//start sort on column 0 (Num)
    }
}
//-----------------------------------------------------------------------------------------
//sort: prepare the 2 dimentional "data" array for sorting
//      sort the "data" array based on the column chosen
//-----------------------------------------------------------------------------------------
function sort(col)
{						
    column  = col;					//save sort column
    heading = [ ];					//empty heading array							

    for (i=0; i<dataName.length; i++)			//create headings
        heading[i] = dataName[i];  			

    if (!order[col]) 					//if no order is specified
        order[col]  = 'asc';				//start ascending

    if (order[col] == 'asc')
    {
        data.sort(alphaAsc);				//sort the data ascending
	heading[col] += '<img src=down.gif border=0>'; 	//add the up image for that column
        order[col]    = 'desc';				//switch asc for desc
    } 		
    else
    {
        data.sort(alphaDesc);				//sort the data descending
	heading[col] += '<img src=up.gif border=0>';   	//add the down image for that column
        order[col]    = 'asc';				//switch desc for asc 
    } 		

    display()
}

//-------------------------------------------------------------------------------------------
//display: display the sorted 2 dimentional array in an html table
//-------------------------------------------------------------------------------------------
function display()
{
    content = '<table bgcolor=lightyellow border=1>' +
              '   <tr bgcolor=dddddd>';

    for (col=0; col < heading.length; col++)		//loop for as many columns
    {
        content += '<th><nobr><a href=javascript:sort(' + col + ')>' + 
                    heading[col] + '</a></nobr></th>' ;
    }
    
    for (row=0; row < data.length; row++)		//loop for as many rows
    {
	content += '<tr>';

        for (col=0; col < data[row].length; col++)
	    content += '<td><nobr>' + data[row][col] + ' </nobr></td>';  

	content += '</tr>';
    }
    content += '</table>';
    
    document.getElementById('table').innerHTML = content;	//place in div tag
}
 
//-----------------------------------------------------------------------------------------
//sort functions: sort a 2 dimentional array ascending or descending
//-----------------------------------------------------------------------------------------
function alphaAsc(a, b) 		//a & b are arrays
{
    a1 = a[column].toLowerCase();	//translate value to lowercase
    b1 = b[column].toLowerCase();	//so compare is case insensitive

    if (a1 < b1) return -1;		// return negative - Do not Flip elements
    if (a1 > b1) return  1;		// return positive - Flip elements
    return 0;				// Do nothing
}
function alphaDesc(a, b)
{
    a1 = a[column].toLowerCase();	//translate value to lowercase
    b1 = b[column].toLowerCase();	//so compare is case insensitive

    if (a1 < b1) return  1;		// return negative - Do not Flip elements
    if (a1 > b1) return -1;		// return positive - Flip elements
    return 0;				// Do nothing
}

//-----------------------------------------------------------------------------------------
</script>

</BODY>
</HTML>