/**
 * [PHPFOX_HEADER]
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author			Raymond Benc
 * @package 		Phpfox
 * @version 		$Id: ajax.js 751 2009-07-10 07:37:37Z Raymond_Benc $
 */

/**
 * Creates an AJAX call using jQuery.load()
 * Data is inserted into DOM
 *
 * @param string sCall Name of the Component
 * @param string sExtra Extra params we plan to pass
 */
$.ajaxBox = function(sCall, sExtra) {
	var sParams = getParam('sJsAjax') + '?phpfox[ajax]=true&phpfox[call]=' + sCall;
	if (sExtra)
	{
		sParams += '&' + sExtra;
	}	
	
	if (!sParams.match(/phpfox\[security_token\]/i))
	{
		sParams += '&phpfox[security_token]=' +  _proxy_jslib_handle(oCore, ('log.security_token'), 0, 0);
	}	
	
	return sParams;
}

var oCacheAjaxRequest = null;
var aCacheAjaxLastCall = {};

window.onbeforeunload = function() {
	if (oCacheAjaxRequest !== null)
	{
		oCacheAjaxRequest.abort();
	}	
}

/**
 * Create AJAX Call
 *
 * @param	string	sFunction	Name of the function we plan to use
 * @param	string	sId	Form ID
 */
$.fn.ajaxCall = function(sCall, sExtra, bNoForm) {	
	var sUrl = getParam('sJsAjax');
	var sParams = '&phpfox[ajax]=true&phpfox[call]=' + sCall + '' + (bNoForm ? '' : this.getForm());
	if (sExtra)
	{
		sParams += '&' + ltrim(sExtra, '&');
	}
	
	if (!sParams.match(/phpfox\[security_token\]/i))
	{
		sParams += '&phpfox[security_token]=' +  _proxy_jslib_handle(oCore, ('log.security_token'), 0, 0);
	}
	
	if (oCacheAjaxRequest !== null)
	{
		oCacheAjaxRequest.abort();
		if (isset( _proxy_jslib_handle(aCacheAjaxLastCall, ('last_call'), 0, 0)) &&  _proxy_jslib_handle(aCacheAjaxLastCall, ('last_call'), 0, 0) != sCall)
		{
			sParams += '&phpfox[last_call]=' +  _proxy_jslib_handle(aCacheAjaxLastCall, ('last_call'), 0, 0);			
		}		
		if (isset( _proxy_jslib_handle(aCacheAjaxLastCall, ('last_param'), 0, 0)))
		{
			sParams += '&phpfox[last_param]=' +  _proxy_jslib_handle(aCacheAjaxLastCall, ('last_param'), 0, 0);			
		}			
	}
	
	if (sCall == 'im.getMessages' || sCall == 'im.getRooms')
	{
		aCacheAjaxLastCall = {
			last_call: sCall 
 };
		
		if (sCall == 'im.getMessages')
		{		
			aMatches = sParams.match(/&im_id=(.*?)&/i);
			if (aMatches !== null && isset(aMatches[1]))
			{
				 _proxy_jslib_assign('', aCacheAjaxLastCall, ('last_param'), '=', ( aMatches[1]));		
			}	
		}
	}
	
	oCacheAjaxRequest = $.ajax({
			data: sParams 
 }
	);
}

$.ajaxCall = function(sCall, sExtra) {
    $(this).ajaxCall(sCall, sExtra, true);
}

/**
 * Get form details
 * @param	string	frm	Form ID or Element ID
 * @return	string	Return parsed URL string
 */
$.fn.getForm = function() {
	var objForm = this.get(0);	
	var prefix = "";
	var submitDisabledElements = false;
	
	if (arguments.length > 1 && arguments[1] == true)
	{
		submitDisabledElements = true;
	}
	
	if(arguments.length > 2)
	{
		prefix = arguments[2];
	}

	var sXml = '';
	if (objForm && objForm.tagName == 'FORM')
	{
		var formElements = objForm.elements;		
		for(var i=0; i < formElements.length; i++)
		{
			if (! _proxy_jslib_handle(formElements, (i), 0, 0).name)
			{
				continue;
			}
			
			if ( _proxy_jslib_handle(formElements, (i), 0, 0).name.substring(0, prefix.length) != prefix)
			{
				continue;
			}
			
			if ( _proxy_jslib_handle(formElements, (i), 0, 0).type && ( _proxy_jslib_handle(formElements, (i), 0, 0).type == 'radio' ||  _proxy_jslib_handle(formElements, (i), 0, 0).type == 'checkbox') &&  _proxy_jslib_handle(formElements, (i), 0, 0).checked == false)
			{
				continue;
			}
			
			if ( _proxy_jslib_handle(formElements, (i), 0, 0).disabled &&  _proxy_jslib_handle(formElements, (i), 0, 0).disabled == true && submitDisabledElements == false)
			{
				continue;
			}
			
			var name =  _proxy_jslib_handle(formElements, (i), 0, 0).name;
			if (name)
			{				
				sXml += '&';
				if( _proxy_jslib_handle(formElements, (i), 0, 0).type=='select-multiple')
				{
					for (var j = 0; j <  _proxy_jslib_handle(formElements, (i), 0, 0).length; j++)
					{
						if ( _proxy_jslib_handle( _proxy_jslib_handle(formElements, (i), 0, 0).options, (j), 0, 0).selected == true)
						{
							sXml += name+"="+encodeURIComponent( _proxy_jslib_handle( _proxy_jslib_handle( _proxy_jslib_handle(formElements, (i), 0, 0).options, (j), 0, 0), 'value', '', 0, 0))+"&";
						}
					}
				}
				else
				{
					sXml += name+"="+encodeURIComponent( _proxy_jslib_handle( _proxy_jslib_handle(formElements, (i), 0, 0), 'value', '', 0, 0));
				}
			}
		}
	}	

	if ( !sXml )
	{
		sXml += "&" + objForm.name + "="+ encodeURIComponent( _proxy_jslib_handle(objForm, 'value', '', 0, 0));
	}	
	
	return sXml;
} ;
_proxy_jslib_flush_write_buffers() ;
