/* w2box: web 2.0 File Repository
 * (c) 2005-2006, Clément Beffa
 * http://labs.beffa.org/w2box/
 *
 */

function deletefile(row) {
	row.className='delete';
	new Ajax.Request("index.php", {
		parameters: 'delete=' + encodeURIComponent(row.cells[0].innerHTML),
		onComplete: function (req) {
			if (req.responseText == "successful") {
				row.parentNode.removeChild(row);
			} else {
				alert(req.responseText);
				row.className='off';
			}
		}
	});
}

function renameSync() {
	var fn = document.getElementById("file").value;
	if (fn == ""){
		document.getElementById("filename").value = '';
	} else {
		var filename = fn.match(/[\/|\\]([^\\\/]+)$/);
		if (filename==null)
		filename = fn; //opera...
		else
		filename = filename[1];

		document.getElementById("filename").value = filename;
	}

	filetypeCheck();
}

function filetypeCheck() {
	var fn = document.getElementById("filename").value;
	if (fn == ""){
		document.getElementById("allowed").className ='';
		document.getElementById("upload").disabled = true;
	} else {
		var ext = fn.split(".");
		if (ext.length==1)
		ext = '.noext.';
		else
		ext = '.' + ext[ext.length-1].toLowerCase() + '.';

		if (ALLOWED_TYPES.indexOf(ext) == -1) {
			document.getElementById("allowed").className ='red';
			document.getElementById("upload").disabled = true;
		} else {
			document.getElementById("allowed").className ='';
			document.getElementById("upload").disabled = false;
		}
	}

}




/*********
* Heavily modified version of 
*
* Javascript for file upload demo
* Copyright (C) Tomas Larsson 2006
* http://tomas.epineer.se/

* Licence:
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
* 
* Software distributed under this License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*/
var usid="";
var finished = false;
var uploadUpdater;

function beginUpload(sid) {	
	usid=sid;
	
	//$config['upload_cgiscript']."?sid=$sid".'" target="upload_iframe'; 
	var uform = document.getElementById('uploadform').getElementsByTagName('form')[0];
	var upb = document.getElementById('upload_pb');
	if (uform.file.value=="") return;
	
	upb.style.height = uform.offsetHeight+"px";
	document.getElementById('upload_filename').innerHTML = uform.filename.value;
	uform.style.display = 'none';
	upb.style.display = 'block';
	var pb = document.getElementById('upload_progress');
	//return;
	uform.action = UPLOAD_SCRIPT+'?sid='+sid+'&maxsize='+MAX_FILESIZE;
	//alert(uform.action);
	//return;
	uform.target = 'upload_iframe';
	uform.submit();
	uploadUpdater = new Ajax.PeriodicalUpdater({},'fileprogress.php',{'decay': 2,'frequency' : 0.5,'method': 'post','parameters': 'sid=' + sid,'onSuccess' : function(request){updateProgress(pb,request)},'onFailure':function(request){updateFailure(pb,request)}})
}

function updateProgress(pb,req) {
	var percent = parseInt(req.responseText);
	
	if (finished) return;
	if (isNaN(percent)) {
		uploadUpdater.stop();
		finished = true;
		
		//post redirect
		var form = document.createElement('form');
		form.method = 'post';
		var input = document.createElement('input');
		input.type = 'hidden';
		if (req.responseText == "FINISHED") {
			pb.style.width = "100%";
			
			input.name = 'sid';
			input.value = usid;
		} else {
			pb.style.width=0;
			
			input.name = 'errormsg';
			input.value = escape(req.responseText);
		}
		form.appendChild(input);
		document.body.appendChild(form);
		form.submit();
	} else {
		if(!percent) percent = 0;
		if(percent > 100) percent = 100;
		pb.style.width = ""+percent+"%";
	}
}

function updateFailure(pb,req) {
	if (finished) return;
	
	uploadUpdater.stop();
	finished = true;	
	
	pb.style.width=0;
	//post redirect
	var form = document.createElement('form');
	form.method = 'post';
	var input = document.createElement('input');
	input.name = 'errormsg';
	input.value = escape(req.responseText);
	form.appendChild(input);
	document.body.appendChild(form);
	form.submit();
}