var path_to_scripts=''; // change this if you wish - include trailing slashes
var refresh_rate=1; // the data refresh period in seconds - change this if you want
var callback;
var file_upload_time=0;
var total_size=0;

function new_XMLHttpRequest() {
    if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
		return null;
    }
}

function loadData(url, values) {
	url=path_to_scripts+url;
    req = new_XMLHttpRequest()
	if(req){
    	req.onreadystatechange = processReqChange;
    	req.open("POST", url, true);
    	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	req.send(values);
	}else{
		// browsers that do not support the xmlhttprequest object fall here
	}
}

function processReqChange() {
    if (req.readyState == 4) {
		callback(req.responseText);
		if (req.status == 200) {
	    // ...processing statements go here...but we don't need any ;-)
        } else {
            //alert("There was a problem retrieving the data:\n" + req.statusText);
        }
    }
}

function get_upload_state(strData){
    data_parts=strData.split('&');
    for(i=0;i<data_parts.length;i++){
		eval(data_parts[i]);
    }
	/* You have the following javascript variable now available to you
	*  time_remaining, time_elapased, upload_speed
	*  total_size, percent_uploaded
	*/
	document.getElementById('progress_bar').style.width=percent_uploaded+'%';
    //document.getElementById('upload_status').innerHTML='Time remaining: '+time_remaining + ' <br />Percent uploaded: '+percent_uploaded;
    document.getElementById('upload_status').innerHTML='Percent uploaded: '+percent_uploaded;
    setTimeout('get_upload_progress()',refresh_rate*1000);
}

function get_upload_time(){
    callback=set_upload_time;
    loadData('get_time.php', '');
}

function set_upload_time(strData){
    file_upload_time=strData;
    callback=get_upload_state;
    // wait two seconds first: files uploading quicker than this are too small to bother with upload progress display
    setTimeout('get_upload_progress()',2000);
}

function get_upload_progress(){
    document.getElementById('info_container').style.display='block';
    values='sessionid='+session_id+'&total_size='+total_size+'&start_time='+file_upload_time;
    loadData('upload_progress.php', values);
}

