// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//This method comes from 
// http://viralpatel.net/blogs/2008/11/ajax-style-file-uploading-using-hidden-iframe.html
//It allows to send a multi part form  data without reloading the page.
//Usage: 
// fileUpload(this.form,'index.php','upload');
//Will send the parent form of the current object to "index.php", 
// updating the div tag with id "upload" with its response.
var globalIframeParentId = null;
function fileUpload(form, action_url, div_id, iframe_parent_id)  
{  
	// Create the iframe...  
	var iframe = document.createElement("iframe");  
	iframe.setAttribute("id","upload_iframe");  
	iframe.setAttribute("name","upload_iframe");  
	iframe.setAttribute("width","0");  
	iframe.setAttribute("height","0");  
	iframe.setAttribute("border","0");  
	iframe.setAttribute("style","width: 0; height: 0; border: none;");  
	  
	// Add to document...  
	document.getElementById(iframe_parent_id).appendChild(iframe);  
	
	window.frames['upload_iframe'].name="upload_iframe";  
	  
	var iframeId = document.getElementById("upload_iframe");  

	// Add event...  
	var eventHandler = function()  {  
		if (iframeId.detachEvent)  
		iframeId.detachEvent("onload", eventHandler);  
		else  
		iframeId.removeEventListener("load", eventHandler, false);  
		  
		// Message from server...  
		if (iframeId.contentDocument) {  
		var content = iframeId.contentDocument.body.innerHTML;  
		} else if (iframeId.contentWindow) {
		var content = iframeId.contentWindow.document.body.innerHTML;  
		} else if (iframeId.document) {
		var content = iframeId.document.body.innerHTML;  
		}
		document.getElementById(div_id).innerHTML = content;  
		// Del the iframe...  
		globalIframeParentId = iframe_parent_id;
		setTimeout("document.getElementById(globalIframeParentId).removeChild(document.getElementById('upload_iframe'));", 250);
	}  
	  
	if (iframeId.addEventListener)  
	iframeId.addEventListener("load", eventHandler, true);  
	if (iframeId.attachEvent)  
	iframeId.attachEvent("onload", eventHandler);  
	
	// Set properties of form...  
	form.setAttribute("target","upload_iframe");  
	form.setAttribute("action", action_url);  
	form.setAttribute("method","post");  
	form.setAttribute("enctype","multipart/form-data");  
	form.setAttribute("encoding","multipart/form-data");  
  
	// Submit the form...  
	form.submit();  
	
	document.getElementById(div_id).innerHTML = "<span class='pink_bold_text'>Uploading...<span>";
}  