/*
    Copyright (C) 2008  Ivo Toby, Internet Voor Ondernemers, http://www.i-v-o.nl / http://syntacticsugar.nl 

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	Crudder Version : 0.50b
 */


	crudderField.file = Class.create(crudderField, {
		initialize:function($super, field, data, pob){
			$super(field, data,pob)
			this.instID = 'crudderField_file.' + parseInt(Math.random()*1000000);
			this.shouldUpload = false;
		},
		
		drawEditor : function(container,record){
			var value = '';
			if (record[this.field.name]){
				value = record[this.field.name];
			}else if (this.field.value && this.field.value !='' ){
				value = this.field.value;
			}
			value = stripslashes(value);
			value = value.toString().trim();
			this.uploadContainer = container;
			if (value){
				var div = new Element('a', {href:this.field.savePath + '/' + value, target:'_blank'}).update("View/download");
				container.insert(div );
				var replace = new Element('div', {style:'cursor:pointer;text-decoration:underline'}).update("replace file");
				container.insert(replace);
				replace.observe('click', (function(){
					container.update('');
					this.drawUploader();
				}).bind(this))
			}else{
				this.drawUploader();
			}
			this.value = value;
		},
		
		drawUploader : function(){
			// create hidden iframe;
			var iframe = new Element('iframe', {width:'0',height:'0', frameborder:'0', name:this.instID+'_iframe', id:this.instID+'_iframe'});
			this.uploadContainer.insert(iframe);
			var form = '<form action="' + this.pob.controller.rpcPath + '" method="post" enctype="multipart/form-data" name="' + this.instID+'_uploadForm" id="' + this.instID+'_uploadForm" target="'  +  this.instID+'_iframe"></form>'; // MSIE needs it inserted as plain HTML.. why? Dunno..
			this.uploadContainer.insert(form);
			this.form = $(this.instID+'_uploadForm'); 
			
			this.input = new Element('input', {type:'file', className:"crudderRecordInput", name:this.field.name });
			this.form.insert(this.input);
			this.input.observe('change', this.setFileName.bindAsEventListener(this));
			
			// insert hidden form elements
			var hidden = new Element('input', {type:'hidden', name:'rpc', value:'1'});
			this.form.insert(hidden);

			var hidden = new Element('input', {type:'hidden', name:'config', value:this.pob.controller.config});
			this.form.insert(hidden);
		},
		
		setFileName : function(e){
			this.value = e.element().getValue();
			if (Prototype.Browser.IE){
				// ARGH!!! MSIE doesn't parse the file as file, it's full path is parsed.. @*@!^)(@*^@)(%!@%$-BEEEEEP-MSIE
				var pathArr = this.value.split('\\');
				this.value = pathArr[pathArr.length-1];
			}
			this.shouldUpload = true;
		},
		
		doUpload : function(myID){
			if (!myID) myID = this.pKey;
			var hidden = new Element('input', {type:'hidden', name:'xmlReq', value:'<command><call><method>saveBin</method><arg>pKey=' + myID + '</arg><arg>storeField=' + this.field.name + '</arg><arg>' + this.field.name + '=<![CDATA[' + encodeURIComponent(this.value) + ']]></arg></call></command>'});
			this.form.insert(hidden);

			// post form;
			this.form.submit();
		},
		
		getRPCValue: function(){
				return '<![CDATA[' + encodeURIComponent(this.value) + "]]>";
		},		
		
		validate : function(){
			return  true;
		}
	})
