
//http://192.168.5.35/Misc

// This function executes a Web request.
// using the AJAX.NET client library for all the data access functions.
// Parameters:
// "methodName" - a string with the name of method to call.
// "parames" - array of objects for the parameters to send with the FlashMethod.
//		object structure - { name:<name of parameter> , value:<value of parameter> }
// "callbackData" - object with data to go with the call and come back on callback
//		2 must have properties - { onSuccessFunction:<the function to execute if call succeeded> , onFailureFunction:<the function to execute if call failed> }
//		other than that - add any data in additional properties that want to be sent with the request.
function executeWebRequest(methodName, parames, callbackData, serviceUrl, callbackFunc)
{
    // Default Service URL:
    if (serviceUrl == undefined) serviceUrl = "/Misc/ApiBroker.aspx";
    if (serviceUrl.indexOf("?") == -1){
      serviceUrl += "?";
    } else if (serviceUrl.charAt(serviceUrl.length - 1) != "&") {
      serviceUrl += "&";
    }
    // Create the WebRequest object.
    webRequest =  new Sys.Net.WebRequest();
	
    // Set the request Url.  
    var url = serviceUrl + "Method=" + methodName;
    if (parames.length > 0)
    {
		for(i = 0; i < parames.length; i++)
			url += "&" + parames[i].name + "=" + parames[i].value;
    }
   
    webRequest.set_url(url);
  
	// add to the request the data that will be passed to the callback function
	webRequest.set_userContext(callbackData);
    
    // Set the Completed event handler 
    // for processing return data
    if (callbackFunc == undefined) {
        // defult callback for items using jason
        webRequest.add_completed(callback); 
    } else {
        // special callback for itme using XML
        webRequest.add_completed(eval(callbackFunc));
    }
    
    webRequest.invoke();
}

// main callback function from all the requests
// if call succeeded - execute the "callbackData.onSuccessFunction" function.
// if call failed - execute the "callbackData.onFailureFunction" function.
function callback(executor, eventArgs)
{
    
	if(executor.get_responseAvailable()) 
    {
		var callbackData = executor.get_webRequest().get_userContext();
		if (executor.get_statusCode() == 200)		// loading completed successfully
		{
			try
			{
				eval('var recievedData = ' + executor.get_responseData());

			}
			catch(Error)	// error in servers response
			{
				callbackData.onFailureFunction(callbackData);
				return;
			}
		
			callbackData.onSuccessFunction(recievedData, callbackData);
		}
		else			// loading completed with errors - try again
		{
			callbackData.onFailureFunction(callbackData);
		}		
    }
    else
    {
        if (executor.get_timedOut())
        {
            // TODO: message for server call timeout
        }
        else if (executor.get_aborted())
        {
			// TODO: message for server call aborted
		}
    }
}

function callbackXml(executor, eventArgs)
{
	if(executor.get_responseAvailable()) 
    {
		var callbackData = executor.get_webRequest().get_userContext();
		if (executor.get_statusCode() == 200)		// loading completed successfully
		{
			try
			{

				var recievedData =  getXMLDoc(executor.get_responseData());

			}
			catch(Error)    // error in servers response
			{
				callbackData.onFailureFunction(callbackData);
				return;
			}
		
			callbackData.onSuccessFunction(recievedData, callbackData);
		}
		else            // loading completed with errors - try again
		{
			callbackData.onFailureFunction(callbackData);
		}		
    }
    else
    {
        if (executor.get_timedOut())
        {
            // TODO: message for server call timeout
        }
        else if (executor.get_aborted())
        {
			// TODO: message for server call aborted
		}
    }
}

