var ajaxResults = new Ajax();
var timerResults = null;

//Change visibility for id
function visibility(whichelement){
	if (document.getElementById) {
		var elementstyle = document.getElementById(whichelement).style;
		if (elementstyle.display == "block") {
			elementstyle.display = "none";
		} else {
			elementstyle.display = "block";
		}
		return false;
	} else {
		return true;
	}
}

//Set the interval
function setResultsTimer(scanrequestid) {
	timerResults = setInterval("reloadResults(" + scanrequestid + ");", 5000);
}

//Retrieve the new results with a ajax request
function reloadResults(scanrequestid) {
	var urlArr = window.location + "";
	urlArr = urlArr.split('/');
	var url = "";
	for (i = 0; i < urlArr.length - 3; i++) {
		url += urlArr[i] + "/";
	}
	ajaxResults.loadXMLDoc(url + 'php/ajax.php?id=' + scanrequestid, procResults);
}

//Get the ajax results and deal with them
function procResults() {
	if (ajaxResults.getReq().readyState == 4) {

		//Retrieve and split result we got from ajax.php
		ResultArr = ajaxResults.getReq().responseText.split(';')

		//Waiting for a spider
		if(ResultArr[0]==0){
			document.getElementById('waiting').style.display = '';
			document.getElementById('scanningwebsite').style.display = 'none';
			document.getElementById('url').innerHTML = '<a href="' + ResultArr[1] + '" title="' + ResultArr[2] + '" target="_blank">' + ResultArr[1] + '</a>';
		}

		//Analyzing
		if(ResultArr[0]==1){
			document.getElementById('waiting').style.display = 'none';
			document.getElementById('scanningwebsite').style.display = '';
			document.getElementById('progressbar').style.width = parseInt(ResultArr[1]) + '%';
			document.getElementById('progresspercentage').innerHTML = parseInt(ResultArr[1]) + '%';
			document.getElementById('url').innerHTML = '<a href="' + ResultArr[2] + '" title="' + ResultArr[2] + '" target="_blank">' + ResultArr[2] + '</a>';
		}

		//Analysis completed
		if(ResultArr[0]==2){
			clearInterval(timerResults);
			window.location = window.location;
		}

		//Error
		if(ResultArr[0]==3){
			clearInterval(timerResults);
			alert(ResultArr[1]);
			window.location = ResultArr[2];
		}
	}
}

//Ajax
function Ajax() {
	this.req = null;
	this.arg = null;
	this.getReq = function () {
		if (this.req === null) {
			if (window.XMLHttpRequest) {
				this.req = new XMLHttpRequest();
			} else if (window.ActiveXObject) {
				this.req = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return this.req;
	}
	this.loadXMLDoc = function (url, handler) {
		this.getReq().open("GET", url, true);
		this.getReq().onreadystatechange = handler;
		this.getReq().send(null);
	}
}

//Set focus to an element in the form
function setFocus(elementid){
	var element=document.getElementById(elementid);
	if (element!=null ){
		element.focus();
	}
}

//Change visibility of element
function changeVisibility(elementid) {
	var status = document.getElementById(elementid).style.display;
	var hash = location.hash;

	//Only change visibility if id is other then hash (url#hash)
	if(hash=='#'+elementid){
		document.getElementById(elementid).style.display = 'block';
	} else {
		if(status=='none'){
			document.getElementById(elementid).style.display = 'block';
		} else {
			document.getElementById(elementid).style.display = 'none';
		}
	}
}

//Replace blockquotes with images
function hideQuickwins(quickwincodes) {

	//Create array of quickwincodes	
	var QuickwinArr = quickwincodes.split(',');

	//Hide quickwins that are not a valid result
	var x=0;
	for (var x = 100; x < 135; x++) {
		var y=0;		
		var showQuickwin = false;
		for (var y = 0; y < QuickwinArr.length; y++) {
			if(x == QuickwinArr[y]){showQuickwin = true;}
		}
		
		//Hide quickwin if it is not found
		if(showQuickwin == false){changeVisibility('quickwin-' + x);}
		
		//Hide all descriptions
		changeVisibility('description-' + x)
		
		//Create onclick function for topics
		document.getElementById('topic-' + x).onclick = new Function("changeVisibility('description-" + x +  "');");
	}
}
