//Our XmlHttpRequest object to get the auto suggest
var statusReq = getXmlHttpRequestObject();
var reportAjax = null;

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Browser Error!\nYour browser does not support the necessary technologies for Lipperhey. Please upgrade your browser to one with AJAX support.\n");
	}
}

//Call the script to retrieve spider status for logged in user
function GetPrivateSpiderStatus() {
	if (statusReq.readyState == 4 || statusReq.readyState == 0) {
		statusReq.open("GET", baseurl + 'private/ajax.php?type=status', true);
		statusReq.onreadystatechange = handleSpiderStatus;
		statusReq.send(null);
	}
}

//Call the script to retrieve spider status for public user
function GetPublicSpiderStatus(scanrequestid) {
	if (statusReq.readyState == 4 || statusReq.readyState == 0) {
		statusReq.open("GET", baseurl + 'public/ajax.php?type=status&id=' + scanrequestid, true);
		statusReq.onreadystatechange = handleSpiderStatus;
		statusReq.send(null);
	}
}

//Called when the AJAX response is returned for status.
function handleSpiderStatus() {
	if (statusReq.readyState == 4) {
		var statusdiv = document.getElementById('live-spider-status');
		if (statusReq.responseText != "") {
			if (statusReq.responseText != "redirect"){statusdiv.innerHTML = statusReq.responseText;}
			if (statusReq.responseText == "redirect"){window.location.reload();}
		}
	}
}

//Change id and download so user can download report file
function downloadReport(event, websiteid, userid, download, filename, pdf) {
	document.getElementById('websiteid').value = websiteid;
	document.getElementById('userid').value = userid;
	document.getElementById('download').value = download;
	document.getElementById('filename').value = filename;
	document.getElementById('option').value = '';
	document.getElementById('form').action = '';

	//Generate RTF or PDF with overlay
	if(pdf == '1'){
		showOverlay('');
		if (reportAjax == null) {
			reportAjax = new Ajax();
		}
		reportAjax.loadXMLDoc(baseurl + "private/ajax.php?type=report&id=" + websiteid, downloadReportDone);
	} else {
		document.getElementById('form').submit();
	}

	//Make sure parent is not executed in tables
	if(window.event){event = window.event;}
	event.cancelBubble = true;
}

function downloadReportDone() {
	if (reportAjax.getReq().readyState == 4) {
		if (reportAjax.getReq().responseText != "error") {
			document.getElementById('overlay').style.display = "none";
			document.getElementById('form').submit();
		} else {
			alert('Error while gathering report information');
		}
	}
}


// Must more cool Ajax OO javascript thing :D
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);
					    }
}