/*
	This file (c) Copyright by Lawyers Online Ltd.
	Rights Reserved.  http://www.lawyersonline.co.uk/
	
	Licensing Terms and Conditions Apply.  Contact Lawyers Online.
	
  --   ---------------------------------------------------------------
	
	$HeadURL: svn://HeBe/Customers/FSSLtd/trunk/includes/chain_upload.js $
	$Id: chain_upload.js 3191 2009-02-25 14:45:02Z qbnut $

  --   ---------------------------------------------------------------
*/

var callback_url = '/chain_upload.php?page=progress_json';
var finished_url = '/chain_upload.php?page=done&';

var re = /^(\.php)|(\.sh)/;	// disallow shell scripts and php
var uploads = new Object;

function check_types(id) {

	upload_form = document.getElementById('form_widget_' + id + '_id');
	for(i=0 ; i < upload_form.elements.length ; i++) {
		if(upload_form.elements[i].value.match(re))	{
			alert('Sorry ' + upload_form.elements[i].value + ' is not allowed');
			return false;
		}
	}

	return true;
}

function toggle(id) {
		
	object=document.getElementById(id);
	
	if (object.style.visibility=="visible")	{
		object.style.visibility="hidden";
		object.style.display="none";
	} else {
		object.style.visibility="visible";
		object.style.display="block";
	}

}

function go_progress(id) {
	
	object=document.getElementById('form_' + id + '_id');
	object.style.height="0px";
	object.style.visibility="hidden";
		
	toggle( 'progress_' + id + '_id' );
}
	
function go_form(id, height) {
		
	toggle( 'progress_' + id + '_id' );

	object=document.getElementById('form_' + id + '_id');
	object.style.height=height + "px";
	object.style.visibility="visible";
	object.style.display="block";

	object=document.getElementById('widget_iframe_' + id + '_id');
	object.height=height + "px";
		
}
	
function child_open( child, url ) {
		
	object=document.getElementById(child);
		
	object.src = url;
		
}
	
function me_open( url ) {

	location.href = url;
}

function go_finished(id, params) {

	widget = document.getElementById('widget_iframe_' + id + '_id');

	widget.src = finished_url + params + '&widget_id=' + id;

}

function update_progress(id) {

	var sid = document.getElementById('sid_' + id + '_id').value;
	var bar = document.getElementById('progress_' + id + '_id_bar');
	var bartext = document.getElementById('progress_' + id + '_id_text');

	var data = uploads[sid].data;

	if(typeof(data) == 'undefined') {
		return;
	}

	if (data == '') {
		return;
	}

	if (data.error_msg != '') {

		window.clearInterval(uploads[sid].check_intervalID);
		window.clearInterval(uploads[sid].update_intervalID);

		bartext.innerHTML = '<p class="error">Error Determining Progress: ' + data.error_msg + '</p>';
		bar.style.width = '0%';

	} else if (data.content_length == '' || parseInt(data.content_length) == 0) {

		bartext.innerHTML = 'Determining Upload Size...';
		bar.style.width = '0%';

	} else if (parseInt(data.current_content_length) >= parseInt(data.content_length)) {

		window.clearInterval(uploads[sid].check_intervalID);
		window.clearInterval(uploads[sid].update_intervalID);

		bartext.innerHTML = 'File Upload Completed';
		bar.style.width = '0%';

	} else {	
		
		var eta = '';

		if (parseInt(data.percent) > 0) {

			var time_unit = Math.round((parseInt(data.current_time) - parseInt(data.start_time)) / parseInt(data.percent));
			var s_time_remaining = (100 - parseInt(data.percent)) * time_unit;
			var time_remaining = s_time_remaining;

			var single_hour = 60*60;
			var single_minute = 60;

			var hours = Math.floor(time_remaining / single_hour);		/* 300 / (60*60) = 0.08 (0 hours) */
			time_remaining -= single_hour * hours;				/* remove the amount of hours from the total (60*0) = 0, leaving 0 */

			var minutes = Math.floor(time_remaining / single_minute);	/* 300 / (60) = 5 (5 mins) */
			time_remaining -= single_minute * minutes;			/* remove the amount of minutes from the total (60*5) = 300, leaving 0*/

			var seconds = time_remaining;					/* only seconds should remain at this point... */

			if (hours > 0) {
				eta += hours + 'h ';
			}
			if (minutes > 0) {
				eta += minutes + 'm ';
			}
			eta += seconds + 's';

			/*
				Using the estimated remaining time, move the percent on by how many units we would expect to be done in 250 ms.
				The "real" percentage will be obtained from the server every 5 seconds;
				if the download hasn't got as far as we thought it will "snap" the bar back to where it should be.
			*/

			/*
				if it takes X seconds (time_unit) to do Y (data.percent), how many extra % can we do in 1/4 second?
				in 1 second, you can do 1/X of Y
				in 1/4 second, you can do (1/X)/4 of Y
			*/

			var percent_move = 0.5;

			if (time_unit != 0) {
				percent_move = 1 / time_unit;
			}

			uploads[sid].data.percent = data.percent + percent_move;

	
		} else {
			eta = 'being calculated...';
		}
	
		bar.style.width = Math.round(parseInt(data.percent)) + '%';
		bartext.innerHTML = Math.round(parseInt(data.percent)) + '% (ETA ' + eta + ')';
	
	
	}

	uploads[sid].progress_url = callback_url + '&sid=' + sid + '&content_length=' + data.content_length + '&start_time=' + data.start_time;
	
	return;

}

function check_progress(id) {

	var sid = document.getElementById('sid_' + id + '_id').value;
	var bar = document.getElementById('progress_' + id + '_id_bar');
	var bartext = document.getElementById('progress_' + id + '_id_text');

	var callbackfunc_eval = function(call_result) {
		/* Is this safe enough? Should we further sanitize this with a JSON parser? */
		/* Though we trust where this data comes from we should probably still check anyway */
		uploads[sid].data = eval('(' + call_result + ')');
		return;
	}

	do_plain_httpcall(uploads[sid].progress_url, callbackfunc_eval, false, false, true, null);

}

function go_upload(id, sid) {

	var done = false;
	var upload_len = 0;
	var current_len = 0;

	uploads[sid] = new Object;

	uploads[sid].data = '';
	uploads[sid].progress_url = callback_url + '&sid=' + sid;

	/* Run this once right away to do the initial population... */
	check_progress(id);
	update_progress(id);

	uploads[sid].check_intervalID = window.setInterval(function() { check_progress(id); }, 5000);
	uploads[sid].update_intervalID = window.setInterval(function() { update_progress(id); }, 1000);

	go_progress(id);

}
	
function go_parent_upload(id, sid) {

	upload_form = document.getElementById('form_widget_' + id + '_id');

	if(check_types(id) == false) {
		return false;
	}
	
	parent.go_upload(id, sid);

	upload_form.submit();

}

function insert_upload_widget(id, upload_widget_inner_src) {

	var sid = document.getElementById('sid_' + id + '_id').value;

	document.write('<div id="form_' + id + '_id" class="upload_form">');
	document.write('<iframe id="widget_iframe_' + id + '_id" src="' + upload_widget_inner_src + '&widget_id=' + id + '&sid=' + sid + '" width="100%" height="24px" frameborder="0">');
	document.write('<p>Sorry, your browser does not support iframes, which are required for this facility to work</p>');
	document.write('</iframe>');
	document.write('</div>');

	document.write('<div id="progress_' + id + '_id" class="upload_progress">');
	document.write('<div id="progress_' + id + '_id_bar" class="upload_progress_bar"></div>');
	document.write('<div id="progress_' + id + '_id_text" class="upload_progress_text"></div>');
	document.write('</div>');	

}
