/**
* QLIB 1.0 Progress Control
* Copyright (C) 2002 2003, Quazzle.com Serge Dolgov
* 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 2
* of the License, or (at your option) any later version.
* http://qlib.quazzle.com
*/
function QProgress_update() {
with (this) {
if( !rendered ) {
alert("Progress component must be rendered before it can be updated");
}
var i = low;
for (var j=0; j<size; j++) {
images[j].src = i < value ? imgsrc1 : imgsrc0;
i += delta;
}
}
}
function QProgress_set(value) {
this.value = value - 0;
this.update();
}
function QProgress_setBounds(low, high) {
this.low = Math.min(low, high);
this.high = Math.max(low, high);
this.delta = (this.high - this.low) / this.size;
this.update();
}
function QProgress_render() {
with (this) {
var hor = this.style < 2;
var rev = this.style % 2;
document.write('<table class="qprogress" style="" border="0"  cellspacing="0" cellpadding="0" unselectable="on" ' +
(hor ? 'width="' + (size * res.width) + '" height="' + res.height + '"><tr>' : 'width="' + res.width +
'" height="' + (size * res.height) + '">'));
for (var j=0; j<size; j++) {
document.write((hor ? '' : '<tr>') + '<td width="' + res.width + '" height="' + res.height +
'" unselectable="on"><img name="' + id + (rev ? size - j - 1 : j) + '" src="' + res.list[0].src +
'" border="0" width="' + res.width + '" height="' + res.height + '"></td>' + (hor ? '' : '</tr>'));
}
document.write((hor ? '</tr>' : '') + '</table>');
for (var j=0; j<size; j++) {
images[j] = document.images[id + j] || new Image(1, 1);
}
rendered = true;
}
}
function QProgress(parent, name, res, size, style) {
this.init(parent, name);
if (!res) {
res = SEGS;
}
this.res = res;
this.value = 0;
this.low = 0;
this.high = 100;
this.size = size || 10;
this.delta = 100 / this.size;
this.style = style || 0;
this.images = new Array(this.size);
this.imgsrc0 = res.list[0] && res.list[0].src;
this.imgsrc1 = res.list[1] && res.list[1].src;
this.set = QProgress_set;
this.update = QProgress_update;
this.setBounds = QProgress_setBounds;
this.render = QProgress_render;
this.rendered = false;
}
QProgress.prototype = new QControl();
QProgress.NORMAL    = 0;
QProgress.REVERSE   = 1;
QProgress.FALL      = 2;
QProgress.RISE      = 3;
<!-- Defines class for handing a submit button that waits for local modifications to        -->
<!-- be distributed before commiting to server.  The locally_modified flag is polled for    -->
<!-- until it is equal to 0, otherwise if maxSconds is reached, displays a timeout message.  -->
<!-- see polling.js																			-->
SEGS = new QImageList(8, 17, "pro0.gif", "pro1.gif");
var maxSeconds = 10;
function UpdateProgress() {
this.base = QProgress;
this.base(null, "i802UpProg", SEGS);
this.high = maxSeconds;
this.delta = (this.high - this.low) / this.size;
this.handleSubmit = _handleSubmit;
this.handleSubmitUpdate = _handleSubmitUpdate;
this.handleTimer = _handleTimer;
}
UpdateProgress.prototype = new QProgress();
var currentUpdate;
var waitTime; // wait for poll response time.
var progressCount; // how long to wait for update to complete before timing out.
var form;
var submitParam;
var onSubmitHandler; // used to save original form submit handler.
var disabled;
var updateOnTimeout = false;
function _handleSubmit(name, value) {
this.handleSubmitUpdate(name, value, false);
}
function _handleSubmitUpdate(name, value, updateOnTimeoutIn) {
updateOnTimeout = updateOnTimeoutIn;
waitTime = 0;
progressCount = 0;
if(disabled) disabled.length = 0; else disabled = new Array();
submitParam = document.getElementById("submitParam");
submitParam.name = name;
submitParam.value = value;
if( ! submitParam ) {
alert("Unable to find form parameter submitParam");
return true;
}
form = submitParam.form;
if( form ) {
onSubmitHandler = form.onsubmit;
// call handler and return if handler returns false.
if( onSubmitHandler ) {
var submitResult = onSubmitHandler();
if( !submitResult ) {
return false;
}
}
form.onsubmit = function() { return false; };   // THIS AND THE REASSIGN NECESSARY ARE NOT NECESSARY.
}
else {
alert("Unable to find main form");
return true;
}
if( currentUpdate ) {
alert("!!!Cannot update more than one update progress bars at a time!!!");
}
currentUpdate = this;
currentUpdate.value = 0;
startPolling( 1000, 'locally_modified' ); // poll every second
window.setTimeout('_handleTimer()', 0);
return false;
}
function _handleTimer() {
if( !document.locally_modified ) { // if not defined, server has yet to respond
if( waitTime < 2 ) { // give it time to get a value						
window.setTimeout('_handleTimer()', 250);
waitTime++;
return;
}
}
_disableAll(form); // disable all form elements
// if value of locally_modified is already 0 then do not show a progress bar.
if( document.locally_modified == "0" ) {
stopPolling();
_submitForm();
return;
}
// if the server has yet to respond or locally_modified == 1, start progress bar.
window.setTimeout('_incrementProgress()', 0);
}
function _incrementProgress() {
var div = document.getElementById("updateProgressId");
//if( div && div.style.display ) {
//	div.style.display = ""; // show progress bar.
//	layoutContent();
//}
if( div ) {
div.style.visibility = "visible"; // show progress bar.
}
if( document.locally_modified == "0" ) {
_handleSyncComplete(true);
return;
}
if(progressCount >= maxSeconds) { // timeout after max seconds.
_handleSyncComplete(false);
return;
}
if(currentUpdate.value >= maxSeconds) { // in case we want to wrap
currentUpdate.set(0);
}
else {
currentUpdate.set(currentUpdate.value + 1);
}
progressCount++;
window.setTimeout('_incrementProgress()', 300);
}
function _handleSyncComplete( send ) {
var div = document.getElementById("updateProgressId");
//if( div && div.style.display != "none" ) {
//	div.style.display = "none"; // hide progress bar.
//	layoutContent();
//}
div.style.visibility = "hidden"; // hide progress bar.
stopPolling();
if( send || updateOnTimeout) {
_submitForm();
}
else {
// alert( "Update failed." );
// currentUpdate = null;
_enableAll( form ); // reenable all form elements
form.onsubmit = onSubmitHandler;
}
}
function _submitForm() {
_enableAll( form ); // reenable all form elements
form.submit();
currentUpdate = null;
form.onsubmit = onSubmitHandler;
}			  
function _disableAll( form )
{
if( form ) {
for(var i = 0; i < form.elements.length; i++)
{
disabled[i] = form.elements[i].disabled;
form.elements[i].disabled = true;
}
}
disableTabs();
}
function _enableAll( form )
{
if( form ) {
for(var i = 0; i < form.elements.length; i++)
{
if( ! disabled[i] ) { // if previously disabled do not enable.
form.elements[i].disabled = false;
}
}
}
enableTabs();
}
