﻿/*
    This file is part of HTTP upload module.
    Copyright Darren Johnstone 2007.

    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.

    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, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


function up_UpdateFormAction()
{
    var form = document.forms[0];
    var action = form.action;
    
    var re = new RegExp('&?DJUploadStatus=[^&]*');
    if (action.match(re)) action = action.replace(re, '');
    
    var delim;
   
    if (action.indexOf('?') == action.length-1) 
    {
         delim = '';
    }
    else 
    {
        delim = '?';
        if (action.indexOf('?') > -1) delim = '&';
    }
    action += delim + 'DJUploadStatus=' + document.getElementById('upProgressID').value;
    form.action = action
}

function up_SetProgress(progress)
{
    var el = document.getElementById('upProgressBar');
    el.style.width = progress+'%';
}

var up_xhttp;
var up_awaitingResult = false;

function up_ReportProgress(progress)
{   
    if (up_awaitingResult)
        return;
    
	up_awaitingResult = true;
	
	if (typeof(XMLHttpRequest) != 'undefined')
	{
		up_xhttp = new XMLHttpRequest();
	}
	else if (typeof(ActiveXObject) != 'undefined')
	{
		up_xhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	else return;

	up_xhttp.onreadystatechange = up_OnProgressResult;
	
	var ts = new Date().getTime(); // Time stamp to stop caching
	up_xhttp.open('GET', 'DJUploadProgress.ashx?' + 'DJUploadStatus=' + document.getElementById('upProgressID').value + '&ts=' + ts, true);
	up_xhttp.send('');
}

function up_OnProgressResult()
{
    if (up_xhttp.readyState == 4)
    {
        var label = document.getElementById('upLabel');
        
        if (up_xhttp.status == 200)
        {
            var res = up_xhttp.responseXML;
            if (res.documentElement.getAttribute('empty') == 'true')
            {
                up_SetProgress(0);
                label.innerHTML = 'Waiting for uploads';
            }
            else
            {
                var progress = parseInt(res.documentElement.getAttribute('progress'));
                
                up_SetProgress(progress);
                
                label.innerHTML = "Uploaded " + res.documentElement.getAttribute('progress') + "% of " + res.documentElement.getAttribute('size') + " bytes<br/>"
                    + "Now uploading " + res.documentElement.getAttribute('file');
            }
        }
        else
        {
            label.innerText = 'Error updating progress bar';
        }
        
	    up_awaitingResult = false;
    }
}

function startUpload()          
{          
    window.setInterval('up_ReportProgress()', 100);
}
