var cur_str_chr;
function json_parse(text) {
	var at = 0;
	var ch = ' ';

	function error(m) {
		throw {
			name: 'JSONError',
			message: m,
			at: at - 1,
			text: text
		};
	}
	
	function next() {
		ch = text.charAt(at);
		at += 1;
		return ch;
	}
	
	function white() {
		while (ch !== '' && ch <= ' ') {
			next();
		}
	}
	
	function str() {
		var i, s = '', t, u;
	
		if (ch == '\'' || ch == '"') { //change " to ' for python
			cur_str_chr = ch;
			outer:          
				while (next()) {
					if (ch == cur_str_chr) {
						next();
						return s;
					} else if (ch == '\\') {
						switch (next()) {
							case 'b':
								s += '\b';
								break;
							case 'f':
								s += '\f';
								break;
							case 'n':
								s += '\n';
								break;
							case 'r':
								s += '\r';
								break;
							case 't':
								s += '\t';
								break;
							case 'u':
								u = 0;
								for (i = 0; i < 4; i += 1) {
									t = parseInt(next(), 16);
									if (!isFinite(t)) {
										break outer;
									}
									u = u * 16 + t;
								}
								s += String.fromCharCode(u);
								break;
							default:
								s += ch;
						}
					} else {
						s += ch;
					}
				}
			}
		error("Bad string");
	}
	
	function arr() {
		var a = [];
	
		if (ch == '[') {
			next();
			white();
			if (ch == ']') {
				next();
				return a;
			}
			while (ch) {
				a.push(val());
				white();
				if (ch == ']') {
					next();
					return a;
				} else if (ch != ',') {
					break;
				}
				next();
				white();
			}
		}
		error("Bad array");
	}
	
	function obj() {
		var k, o = {};
	
		if (ch == '{') {
			next();
			white();
			if (ch == '}') {
				next();
				return o;
			}
			while (ch) {
				k = str();
				white();
				if (ch != ':') {
					break;
				}
				next();
				o[k] = val();
				white();
				if (ch == '}') {
					next();
					return o;
				} else if (ch != ',') {
					break;
				}
				next();
				white();
			}
		}
		error("Bad object");
	}
	
	function num() {
		var n = '', v;
		if (ch == '-') {
			n = '-';
			next();
		}
		while (ch >= '0' && ch <= '9') {
			n += ch;
			next();
		}
		if (ch == '.') {
			n += '.';
			while (next() && ch >= '0' && ch <= '9') {
				n += ch;
			}
		}
		if (ch == 'e' || ch == 'E') {
			n += 'e';
			next();
			if (ch == '-' || ch == '+') {
				n += ch;
				next();
			}
			while (ch >= '0' && ch <= '9') {
				n += ch;
				next();
			}
		}
		if (ch == 'L')next();//for python long
			v = +n;
			if (!isFinite(v)) {
			error("Bad number");
		} else {
			return v;
		}
	}
	
	function word() {
		switch (ch) {
			case 't':
				if (next() == 'r' && next() == 'u' && next() == 'e') {
					next();
					return true;
				}
				break;
			case 'f':
				if (next() == 'a' && next() == 'l' && next() == 's' && next() == 'e') {
					next();
					return false;
				}
				break;
			case 'n':
				if (next() == 'u' && next() == 'l' && next() == 'l') {
					next();
					return null;
				}
				break;
		}
		error("Syntax error");
	}
	
	function val() {
		white();
		switch (ch) {
			case '{':
				return obj();
			case '[':
				return arr();
			case '\'':
			case '"':
				return str();
			case '-':
				return num();
			default:
				return ch >= '0' && ch <= '9' ? num() : word();
		}
	}
	return val();
}
	
var xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");

function loadDATA(url) { 
   	xmlhttp.open('GET', url, true);
    xmlhttp.onreadystatechange = getDATA;
    xmlhttp.send(null);
}

function getDATA() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var json_data = json_parse(xmlhttp.responseText);
		var val = Math.round(json_data[1]);
		if (val > 0) {
			document.getElementById("percent").innerHTML = val + "% Completed";
			document.getElementById("bytes").innerHTML = "<b>" + json_data[2] + "</b> of <b>" + json_data[3] + "</b> at <b>" + json_data[4] + "</b> kB/s";
		} else {
			document.getElementById("percent").innerHTML = "Initiating Upload";
		}
		if (json_data[5] && json_data[6]) {
			if (parseInt(json_data[6].substring(0, 2)) < 23) {
				document.getElementById("timetogo").innerHTML = json_data[5] + " - Time Remaining";
				document.getElementById("timetillnow").innerHTML = json_data[6] + " - Time Elapsed";
			} else {
				document.getElementById("timetogo").innerHTML = "Calculating Time Remaining";
				document.getElementById("timetillnow").innerHTML = "Calculating Time Elapsed";
			}
		}
		percentile = val + "%";
		//dpocument.getElementById("background").setAttribute("width", "200px");
		//var x = Math.round(json_data[2] * 100 / json_data[3]);
		//x += "px";
		//alert(x);
		//document.getElementById("bgnd").setAttribute("style", "width: 50px");
		//alert(document.getElementById("bgnd").getAttribute("width"));
		if (document.layers) {
			bgnd = document.layers["bgnd"];
		} else if (document.all) {
			//bgnd = document.all["bgnd"];
			//document.all["bgnd"].style.width = val + '%';
			document.all["debug"].style.visibility = "visible";
			document.all["bgnd"].innertHTML = "HERE";
			document.all["debug"].innerHTML = '<table class="background" border="0" width=' + val + '%" cellspacing=0 cellpadding=0><tr><td><img src="/images/spacer.gif" height="19" width="0"></td></tr></table>';
		} else if (document.getElementById) {
			//bgnd = document.getElementById("content");
			document.getElementById("status").style.visibility = "visible";
			document.getElementById("bgnd").style.width = val + '%';
		}
		/*
		if (document.all) {
			bgnd = document.all["bgnd"];
		} else {
			bgnd = document.getElementById("bgnd");
		}
		*/
		//alert(bgnd.getAttribute('width'));
		//document.getElementById("bgnd").setAttribute = ("style", "width: " + x + ";");
		//document.getElementById("background").style.width = (x + 'px');
		//if (document.getElementById("background")) {
		//	document.getElementById("background").setAttribute("width", "100");
		//}
		myurl = json_data[0];
		//alert(percentile);
	}
}

function loadFirstURL(url) { 
   	xmlhttp.open('GET', url, true);
    xmlhttp.onreadystatechange = getNextURL;
    xmlhttp.send(null);
}

var table = 0
var myTable;
function getNextURL() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		var json_data = json_parse(xmlhttp.responseText);
		myurl = json_data[0];
	}
}

var dtstart = 0;
var first = 0;
var myscript = "/progress.pl";
var myparameters = "";
var myurl = "";
var first = 1;


function launchIt() {
	var now = new Date();
	//first += 1;
	if (dtstart == 0) {
		dtstart = Math.round(now.getTime() / 1000);
	}
	var dtnow = Math.round(now.getTime() / 1000);

	var sessionId = document.getElementById("sessionid");

	if (first == 1) { 
		first = 0;
		myparameters = "?dtnow=" + dtnow + "&dtstart=" + dtstart + "&sessionid=" + sessionId.value + "&iStatus=1";
		loadFirstURL(myscript + "?" + myparameters);
		setTimeout("launchIt()", 4000);
	} else {
		//alert("starting load");
		loadDATA(myurl);
		setTimeout("launchIt()", 2000);
	}
}

