/////////////////////////////////////////////////////////////////////////////
//
// Gladiator Components
//
// http://www.gladiatorweb.com
//
// (c) 2006 by Edward H. Trager .  All Rights Reserved
//
// NAME: gladiatorFileUpload.js
//
// Implements a form with an '<input type="file" />' element.
// Some additional hidden input elements are included as well.
//
// AUTHOR: Edward H. Trager
//
// LAST UPDATE: 2007.02.19
//
// NOTE: SEE "LICENSE" FILE FOR LICENSING TERMS
//
//////////////////////////////////////////////////////////////////////////////

//
// DEPENDENCY CHECKING:
//
if(typeof GLADIATOR_CORE_INCLUDED == 'undefined') alert('NOTA BENE: "gladiatorFileUpload.js" REQUIRES "gladiatorCore.js"!');
if(typeof GLADIATOR_FORM_COMPONENTS_INCLUDED == 'undefined') alert('NOTA BENE: "gladiatorFileUpload.js" REQUIRES "gladiatorFormComponents.js"!');

var GLADIATOR_FILE_UPLOAD_INCLUDED=true;

//
// gFileUpload
//
function gFileUpload(id,action,postOrGet,parentNode){
	
	var _id=id;
	
	var _action="";
	if(action) _action=action;
	
	var _method="POST";
	if(postOrGet) _method=postOrGet;
	
	//
	// Basic Form component:
	//
	var _gFormComponent = new gFormComponent("","fc_"+id,parentNode);
	var _canvasNode    = _gFormComponent.getNode();
	
	//
	// Choose file button:
	//
	var _chooseButton     = document.createElement("div");
	_chooseButton.setAttribute("class","fuChoose");
	_chooseButton.title="Click here to choose a file to upload";
	_canvasNode.appendChild(_chooseButton);
	
	//
	// Form:
	//
	var _thisForm = document.createElement("form");
	
	_thisForm.id      = _id+"_form";
	_thisForm.action  = _action;
	_thisForm.method  = _method;
	_thisForm.enctype = "multipart/form-data"
	
	var _fileUploadObject = document.createElement("input");
	_fileUploadObject.id=_id;
	_fileUploadObject.name = _id;
	_fileUploadObject.type="file";
	_fileUploadObject.setAttribute("class","fileUploadObject");
	_fileUploadObject.title="Click on the [+] button to choose a file";
	_thisForm.appendChild(_fileUploadObject);
	
	_canvasNode.appendChild(_thisForm);
	
	//
	// Where user types the text:
	//
	var _visibleInput     = document.createElement("input");
	_visibleInput.type    = "text";
	_visibleInput.setAttribute("class","visibleInput");
	_visibleInput.title="Click on the [+] button to choose a file";
	_canvasNode.appendChild(_visibleInput);
	
	//
	// Submission button:
	//
	var _submitButton     = document.createElement("div");
	_submitButton.setAttribute("class","fuSubmit");
	_submitButton.title="Click to upload file to server";
	_canvasNode.appendChild(_submitButton);
	
	//
	// _submitForm method:
	//
	function _submitForm(){
		_thisForm.submit();
	}
	
	//
	// copyFromVisibleToReal
	//
	//function _copyFromVisibleToReal(){
	//	_fileUploadObject.value = _visibleInput.value;
	//}
	
	//
	// copyFromRealToVisible
	//
	function _copyFromRealToVisible(){
		_visibleInput.value =_fileUploadObject.value;
	}
	
	
	//
	// Event Handlers:
	//
	_submitButton.addEventListener("click",_submitForm,false);
	//_visibleInput.addEventListener("change",_copyFromVisibleToReal,false);
	_fileUploadObject.addEventListener("change",_copyFromRealToVisible,false);
	
}