//------------------------------------------------------------------------------
// Written by Ray Wilson
// Copyright Music From Outer Space LLC 2010.
// http://www.musicfromouterspace.com/mfosweb/home.action
// Simple AJAX code (written by Ray Wilson)
// Free for reuse with this header in place.
//------------------------------------------------------------------------------
// serviceURL       = URL of AJAX service to call.
// callbackFunction = function reference to call with string on success.
// argNames         = array of AJAX call argument names (parallels argValues).
// argValues        = array of AJAX call argument values (parallels argNames).
// serviceMethod    = ('POST' or 'GET')
//------------------------------------------------------------------------------
function callRemoteService(serviceURL, callbackFunction, argNames, argValues, serviceMethod)							
{
	var client;
    if(mozilla)
    {
        client = new XMLHttpRequest();        
    }
    else
    {
        client = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
	try	
	{
	    client.open(serviceMethod, serviceURL, true);
	}
	catch(e)
	{
		alert(e);
		return;    	
	}
	
	client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	client.onreadystatechange = function ()
	{
        if (client.readyState == 4) 
        {
            if(client.status == 200)
            {		
                var message = client.responseText;
                callbackFunction(message.trim());
            } 
            else
            {
            	// ------------------------------------------------------------
            	// Silently fail... uncomment alert for debugging only.
            	// Nothing a user can do anyway.
            	// The error is self healing.
            	// ------------------------------------------------------------
                //alert("Error occurred during AJAX send: " + " - " + client.status);
            }
        }
    };
    
    try
    {
    	var arglist = new StringBuffer();
    	if(argNames != null)
    	{
    		for(var i=0; i<argNames.length; ++i)
    		{
    			arglist.append((i>0) ? "&" : "");
    			arglist.append(argNames[i]);
    			arglist.append("=");
    			arglist.append(escape(argValues[i]));
    		}
    	}
   		client.send(arglist.toString());
    }
    catch(ex)
    {
		alert("AJAX Failed: " + ex);
    }
}

