/*jslint white: true, browser: true, undef: true, nomen: true, eqeqeq: true, plusplus: false, bitwise: true, regexp: true, strict: true, newcap: true, immed: true, maxerr: 14 */
/*global window: false, ActiveXObject: false*/

/*
The onreadystatechange property is a function that receives the feedback. It is important to note that the feedback
function must be assigned before each send, because upon request completion the onreadystatechange property is reset.
This is evident in the Mozilla and Firefox source.
*/

/* enable strict mode */
"use strict";

// global variables
var progress,			// progress element reference
	progress_text,		// progress element reference
	request,			// request object
	Percentage,			// request object
	intervalID = false,	// interval ID
	number_max = 3,		// limit of how many times to request the server (this limit is needed only for this demo)
	number,				// current number of requests
	// method definition
	initXMLHttpClient,	// create XMLHttp request object in a cross-browser manner
	send_request,		// send request to the server
	request_handler,	// request handler (started from send_request)
	polling_start,		// button start action
	polling_stop;		// button start action


// define reference to the progress bar and create XMLHttp request object
window.onload = function () {
	progress = document.getElementById('progress');
	progress_text = document.getElementById('progress_text');
	request = initXMLHttpClient();
};


// create XMLHttp request object in a cross-browser manner
initXMLHttpClient = function () {
	var XMLHTTP_IDS,
		xmlhttp,
		success = false,
		i;
	// Mozilla/Chrome/Safari/IE7+ (normal browsers)
	try {
		xmlhttp = new XMLHttpRequest(); 
	}
	// IE(?!)
	catch (e1) {
		XMLHTTP_IDS = [ 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0',
						'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' ];
		for (i = 0; i < XMLHTTP_IDS.length && !success; i++) {
			try {
				success = true;
				xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
			}
			catch (e2) {}
		}
		if (!success) {
			throw new Error('Unable to create XMLHttpRequest!');
		}
	}
	return xmlhttp;
};


// send request to the server
send_request = function () {
	if (number < number_max) {
		request.open('GET', 'sync.php?step='+number, true);	// open asynchronus request
		request.onreadystatechange = request_handler;		// set request handler	
		request.send(null);									// send request
		number++;											// increase counter
	}
	else {
		polling_stop();
	}
};


// request handler (started from send_request)
request_handler = function () {
	var level, txt, Text;
	var txt_new = new Array();
	if (request.readyState === 4) { // if state = 4 (operation is completed)
		//alert(request.status+"\n"+request.responseText);
		if (request.status === 200) { // and the HTTP status is OK
			//alert(request.status+"\n"+request.responseText);
			txt = request.responseText;
			txt_new = txt.split("<br>");
			
			//alert(txt);
			if(txt_new.length >= 2)
			{
				Percentage = txt_new[0];
				Text = txt_new[1];
				progress.style.width = progress.innerHTML = Percentage;
				$("#disp1").append("<br><br>"+Text).fadeIn(1000);
				
				if (number < number_max)
				{
					if(number == 1)
						intervalID = window.setInterval('send_request()', 2000);
					else
					{
						send_request();
						window.clearInterval(intervalID);
						intervalID = false;
					}
				}
				else {
					polling_stop();
				}
			}
			else
			{
				Text = txt;
				$("#disp1").append("<br><br>"+Text).fadeIn(1000);
				polling_stop();
			}
		}
		else { // if request status is not OK
			//alert(request.status+"\n"+request.responseText+"\n"+number);
			//progress.style.width = '100%';
			Text = 'Error:[' + request.status + ']' + request.statusText;
			$("#disp1").append("<br><br>"+Text).fadeIn(1000);
			polling_stop();
		}
	}
};


// button start
polling_start = function () {
	number = 0;
	send_request();
};


// button stop
polling_stop = function () {
	// abort current request if status is 1, 2, 3
	// 0: request not initialized 
	// 1: server connection established
	// 2: request received 
	// 3: processing request 
	// 4: request finished and response is ready
	//alert(request.status+"\n"+request.responseText);
	//alert(request.responseText+" "+request.readyState+" "+request.readyState);
	//alert(Percentage);
	if (0 < request.readyState && request.readyState < 4) {
		request.abort();
	}
	/*else if(request.status != 0)
	{
		window.clearInterval(intervalID);
		intervalID = false;
		// display 'Demo stopped'
		progress.style.width = '100%';
		progress.innerHTML = 'Completed';
		// $("#Close").show();
	}*/
	$("#Continue").hide();
	request.abort();
	//window.clearInterval(intervalID);
};
