// -----------------------------------------------------
// Lekane TalkNow
// Copyright Lekane 2008
// Version 2
// -----------------------------------------------------


/**
 * Request to see if someone is available.
 * The server's response of true or false is passed to the availableCallback method.
 * @param searchPattern the pattern defining which users to target
 * @param availableCallback the function to call with the response.
 */

function isAvailable(searchPattern, availableCallback) {
    new Ajax.Request(proxyURL, {
        method: 'post',
        parameters: 'url222=' + lekaneServerURL + '/lekane/TalkNowStatus' + '&cmd=availability&deployment=' + encodeURIComponent(deploymentId) + '&searchPattern=' + encodeURIComponent(searchPattern),
        onSuccess: function(responseDetails) {
            var available = -1 != responseDetails.responseText.indexOf('true');
            availableCallback(available);
            // alert('toimii');
        },
        onException: function(req, e) {
            //alert( 'Could not find presence status');
        }
    });
}

/**
 * Send a call request
 * @param searchPattern the pattern defining which users to target
 * @param data[][] a 2xn dimensional array containing key - value data to send
 * @param requestCallback the function to call with the response
 */
function sendCallRequest(searchPattern, data, requestCallback) {
	// Iterate through the passed in data[][] and build a URL encoded param to send to server.
	var keyLength = data[0].length;
	var dataParams = '';
	for ( i=0; i<keyLength; i=i+1 ) {
    	var currentKey = data[0][i];
    	var currentValue = data[1][i]; 
    	
    	if ( currentValue.length == 0 ) {
    		currentValue = ' ';
    	}
    	if (i > 0) {
    		dataParams = dataParams + '|';
    	}
    	dataParams = dataParams + currentKey + '|' + currentValue;
	}
	
	// Build the URL
	var urlToRequest = lekaneServerURL + '/lekane/TalkNow2' + '&searchPattern=' + encodeURIComponent(searchPattern) + '&deployment=' + encodeURIComponent(deploymentId) + '&data=' + encodeURIComponent(dataParams);
	
	new Ajax.Request(proxyURL , {
		method: 'post',
		parameters: 'url222=' + urlToRequest,
	  	onSuccess: function( responseDetails ) {
	  		var response = null;
	  		
	  		if( null != responseDetails.responseText  && responseDetails.responseText.length > 0 ) {
				response = responseDetails.responseText.strip().split("|");
				requestCallback( response[0], response[1] );
			} else {
				requestCallback( null, null );
			}
		},
		onException: function(req, e) {
		requestCallback(null);
		
	  	} 
	});
}


/**
 * Request for who is available.
 * The server's responds with an array of available user unique search patterns and names which are passed to the whoIsAvailableCallback method
 * @param searchPattern the pattern defining which users to target
 * @param whoIsAvailableCallback the function to call with the response.
 */
function whoIsAvailable(searchPattern, whoIsAvailableCallback) {
    
	new Ajax.Request(proxyURL , {
		method: 'post',
		parameters: 'url222=' + lekaneServerURL + '/lekane/TalkNowStatus' + '&cmd=details&deployment=' + encodeURIComponent(deploymentId) + '&searchPattern=' + encodeURIComponent(searchPattern),
	  	onSuccess: function( responseDetails ) {
	  		var availableArray = null;
	  		if( null != responseDetails.responseText  && responseDetails.responseText.length > 0 ) {
				availableArray = responseDetails.responseText.strip(); 
			}
			whoIsAvailableCallback(availableArray);
			
		},
		onException: function(req, e) {
		//alert( 'Could not find presence status' );
	  	} 
	});
}



