/* http://elmicox.blogspot.com/2007/03/upload-assncrono-iframe-como-ajax-1.html
 * @Modified-by: Pedro Amaral Couto
 */

/* IFrame
 * id : (string)
 * url : (string)
 * form : (string/object)
 * on_load : (function)
 * on_wait : (function)
 * on_error : (function)
 */
function wg_iframe(id, url, form, 
                   on_load, on_wait, on_error)
{

	/* create form */
	var form_obj = wg_getbyid(form);
	if(!form_obj)
	{
		alert('Form "' + form + '" does not exist.');
		return false;
	} // if
	var temp_action = form_obj.action;
	var temp_target = form_obj.target;
	form_obj.action = url;
	form_obj.target = id;
	
	/* create iframe */
	var iframe = document.createElement('iframe');
	iframe.setAttribute('id', id);
	iframe.setAttribute('name', id);
	window.frames[id].name=id; //ie sucks
	iframe.setAttribute('width', '0');
	iframe.setAttribute('height', '0');
	iframe.setAttribute('border', '0');
	iframe.setAttribute('style', 'width: 0; height: 0; border: none;');
	
	form_obj.parentNode.appendChild(iframe);
	var tmp_action = form_obj.action;
	
	/* loading function */
	var loading_func = function()
		{
			wg_remove_event( iframe, 'load', loading_func );
			if(on_load)
			{
				if( !on_load(id) )
				{
					if( on_error )
					{
						on_error();
					}
					return false;
				} // if onload
			} // if
			setTimeout(function(){ wg_remove(iframe); }, 250);
			return true;
		}; // function
	wg_add_event( iframe, 'load', loading_func);
	
	/* submit form and restore data */
	form_obj.submit();
	form_obj.action = temp_action;
	form_obj.target = temp_target;
	
	/* call wainting function */
	if(on_wait)
	{
		on_wait();
	} // if
} // function wg_iframe
