/* Query events */
function onQueryExecuted() {
	pagina.splitStatus.freeze();
	document.getElementById("gb_ga_large").style.color = "#ff007a";
	document.getElementById("gb_wm_large").style.color = "#ff007a";
	document.getElementById("gb_ga_large").style.cursor = "pointer";
	document.getElementById("gb_wm_large").style.cursor = "pointer";
	saveBasicQuery();
}

function onQueryData(results) {
	pagina.splitStatus.update();
	pagina.catData.appendResults(results);
	pagina.catData.draw();
	pagina.allData.appendResults(results);
	pagina.allData.draw();
	pagina.appendResults(results);
	pagina.graph.draw();
}

function onQueryCanceled() {
	pagina.splitStatus.freeze();
}

function querySaved(ref_id) {
	var handle, temp;
	handle = document.getElementById("permalink");
	while (handle.childNodes.length > 0) handle.removeChild(handle.firstChild);
	handle.appendChild(document.createTextNode("Permanent link to these results: "));
	temp = document.createElement('a');
	temp.appendChild(document.createTextNode(savedUrl("basic", ref_id)));
	temp.href = savedUrl("basic", ref_id);
	handle.appendChild(temp);
}

/* Pagina Object */
var pagina = new function() {
	this.data = Array();

	/* Split status */
	this.splitStatus = new function() {
		this.update = function() {
			var hits, domains, result, results, sent, i, queryTime, biggestPressence, mostResults, noResults, extra, extra2, timeFilters;
			domains = query.data.length;
			hits = 0;
			results = 0;
			sent = 0;
			noResults = 0;
			biggestPressence = query.data[0];
			mostResults = query.data[0];
			for (i = 0; i < domains; i++) {
				result = query.data[i];
				if (result.readyState == 2) {
					hits += result.hits;
					results++;
					if (result.hits == 0) noResults++;
					if (result.hits > biggestPressence.hits) biggestPressence = result;
					if (result.pop > mostResults.pop) mostResults = result;
				}
				else if (result.readyState == 1) {
					sent++;
				}
			}
			sent += results;
			queryTime = query.processingTime() / 1000;
			
			setValue("processing_loader", Math.round(100 * results / domains) + "%");
			timeFilters = {'d':'24 hours', 'w':'week', 'm':'month', 'y':'year'};
			extra = query.filterTime != "" ? ", in the past " + timeFilters[query.filterTime] : "";
			extra2 = query.filterTime != "" ? "IN THE PAST " + timeFilters[query.filterTime].toUpperCase() : "";
			setValue("processing_text", "Splitting " + toBigInt(hits) + " hits for " + domains + " top level domains" + extra);
			
			setValue("ss_keyword", query.keywords[0].value);
			setValue("ss_query_time", extra2);
			setValue("ss_querys_sent", sent);
			setValue("ss_querys_received", results);
			setValue("ss_processing_time", toDecimal(queryTime, 1) + " sec");
			setValue("ss_avg_query_time", toDecimal(queryTime / results, 3) + " sec");
			setValue("ss_google_results", toBigInt(query.baseResults));
			setValue("ss_total_results", toBigInt(hits));
			setValue("ss_unaccounted_results", toBigInt(query.baseResults - hits));
			setValue("ss_biggest_pressence", biggestPressence.tld.title + " " + toBigInt(biggestPressence.hits) + " hits");
			setValue("ss_most_popular", mostResults.tld.title + " " + toDecimal(mostResults.pop, 2) + " / 10k pages");
			setValue("ss_domains_without_results", noResults);
			
			setValue("exp_ss_querys_sent", sent);
			setValue("exp_ss_querys_received", results);
			setValue("exp_ss_google_results", toBigInt(query.baseResults));
			setValue("exp_ss_total_results", toBigInt(hits));
			setValue("exp_ss_unaccounted_results", toBigInt(query.baseResults - hits));
			setValue("exp_ss_biggest_pressence", biggestPressence.tld.title + " " + toBigInt(biggestPressence.hits) + " hits");
			setValue("exp_ss_most_popular", mostResults.tld.title + " " + toDecimal(mostResults.pop, 2) + " / 10k pages");
			
			return true;
		}
		
		this.show = function() {
			document.getElementById("processing").style.color = "#ff007a";
			document.getElementById("cancel").style.display = "block";
			document.getElementById("processing_loader").style.backgroundPosition = "0px 0px";
			document.getElementById("processing").style.display = "block";
			document.getElementById("processing_loader").firstChild.nodeValue = "0%";
			document.getElementById("processing_text").firstChild.nodeValue = "Splitting 0 hits for ### top level domains";
		}
		
		this.freeze = function() {
			document.getElementById("processing").style.color = "#009ff4";
			document.getElementById("cancel").style.display = "none";
			document.getElementById("processing_loader").style.backgroundPosition = "100px 0px";
		}
	}
	
	/* Grafieken */
	this.graph = new function() {
		this.stepX = 27;
		this.stepY = 18;
		
		this.setTemplates = function() {
			var handle, handle2, temp, mapScaleX, mapScaleY;
			if (handle = document.getElementById("graph_a_flags")) {
				for (i = 0; i < 16; i++) {
					temp = document.createElement("div");
					temp.className = "vline";
					handle.appendChild(temp);
				}
				for (i = 0; i < 16; i++) {
					temp = document.createElement("div");
					temp.className = "hline";
					handle.appendChild(temp);
				}
				for (i = 1; i < data.tlds.length; i++) {
					data.tlds[i].id = i - 1;
					temp = document.createElement("div");
					//temp.style.backgroundImage = "url(flags/" + data.tlds[i].tld + ".png)";
					temp.style.backgroundPosition = "0px -" + (64 * data.tlds[i].tilePos) + "px";
					addEvent(temp, "mouseover", pagina.graph.flagHover);
					addEvent(temp, "mouseout", pagina.graph.flagOut);
					addEvent(temp, "click", pagina.graph.flagClick);
					handle.appendChild(temp);
				}
			}
			if (handle = document.getElementById("graph_a_xval")) {
				for (i = 0; i < 16; i++) {
					temp = document.createElement("div");
					temp.appendChild(document.createTextNode("#"));
					handle.appendChild(temp);
				}
			}
			if (handle = document.getElementById("graph_a_yval")) {
				for (i = 0; i < 16; i++) {
					temp = document.createElement("div");
					temp.appendChild(document.createTextNode("#"));
					handle.appendChild(temp);
				}
			}
			/* World map */
			handle2 = document.getElementById("wm_other");
			if (handle = document.getElementById("wm_countries")) {
				mapScaleX = 1 / 90;
				mapScaleY = 1 / 80;
				for (i = 1; i < data.tlds.length; i++) {
					if (data.tlds[i].posX != 0 || data.tlds[i].posY != 0) {
    					data.tlds[i].mapId = handle.childNodes.length;
						temp = document.createElement("div");
    					temp.appendChild(document.createTextNode("." + data.tlds[i].tld));
    					temp.style.color = data.tlds[i].region.color;
    					temp.style.left = Math.round((18000 + data.tlds[i].posX) * mapScaleX + 10) + "px";
    					temp.style.bottom = Math.round((9000 + data.tlds[i].posY) * mapScaleY + 30) + "px";
						handle.appendChild(temp);
					}
					else {
						data.tlds[i].mapId = handle2.childNodes.length;
						temp = document.createElement("div");
    					temp.appendChild(document.createTextNode("." + data.tlds[i].tld));
						handle2.appendChild(temp);
					}
				}
			}
		}
		
		this.draw = function() {
			var queryData = pagina.data;
			var flagField, axeFieldX, axeFieldY;
			var i, delta, node, label, temp;
			var width, height;
			var minX, maxX, minLogX, maxLogX, deltaX, stepX;
			var minY, maxY, minLogY, maxLogY, deltaY, stepY;
			
			/* Grafiek */
			width = 362;
			height = 120;
			
			/* Mini- en maximale waarden bepalen */
			minX = 0;
			maxX = 0;
			minY = 0;
			maxY = 0;
			for (i = 0; i < queryData.length; i++) {
				if (queryData[i].pop > 0) {
					if (maxX < queryData[i].pop) maxX = queryData[i].pop;
					if (minX == 0) minX = queryData[i].pop;
					else if (minX > queryData[i].pop) minX = queryData[i].pop;
				}
				if (queryData[i].hits > 0) {
					if (maxY < queryData[i].hits) maxY = queryData[i].hits;
					if (minY == 0) minY = queryData[i].hits;
					else if (minY > queryData[i].hits) minY = queryData[i].hits;
				}
			}
			
			if (maxX > 0 && maxY > 0) {
				flagField = document.getElementById("graph_a_flags");
				axeFieldX = document.getElementById("graph_a_xval");
				axeFieldY = document.getElementById("graph_a_yval");
				
				/* X-as */
				maxLogX = Math.ceil(log(maxX));
				minLogX = Math.floor(log(minX));
				deltaX = delta = maxLogX - minLogX;
				stepX = width / (delta + 1);
				
				for (i = 0; i < (delta + 1); i++) {
					node = flagField.childNodes[i];
					label = axeFieldX.childNodes[i];
					label.style.left = node.style.left = Math.round(i * stepX + stepX / 2) + "px";
					label.firstChild.nodeValue = Math.pow(10, minLogX + i);
				}
				for (; i < 16; i++) {
					node = flagField.childNodes[i];
					label = axeFieldX.childNodes[i];
					label.style.left = node.style.left = "-100px";
				}
				
				/* Y-as */
				maxLogY = Math.ceil(log(maxY));
				minLogY = Math.floor(log(minY));
				deltaY = delta = maxLogY - minLogY;
				stepY = height / (delta + 1);

				for (i = 0; i < (delta + 1); i++) {
					node = flagField.childNodes[i + 16];
					label = axeFieldY.childNodes[i];
					label.style.bottom = node.style.bottom = Math.round(i * stepY + stepY / 2) + "px";
					label.firstChild.nodeValue = Math.pow(10, minLogY + i);
				}
				for (; i < 16; i++) {
					node = flagField.childNodes[i + 16];
					label = axeFieldY.childNodes[i];
					label.style.bottom = node.style.bottom = "-100px";
				}
				
				/* Vlaggen */
				for (i = 0; i < queryData.length; i++) {
    				temp = queryData[i];
    				node = flagField.childNodes[temp.tld.id + 32];
    				node.id = i;
					node.style.left = ((temp.pop == 0) ? 0 : Math.round(stepX * (log(temp.pop) - minLogX) + stepX / 2 - 11)) + "px";
					node.style.bottom = ((temp.hits == 0) ? 0 : Math.round(stepY * (log(temp.hits) - minLogY) + stepY / 2 - 11)) + "px";
    			}
			}
			else {
				/* Geen pop waarde gevonden */
			}
			
			
			
			/* World map */
			var minFont, maxFont, dFont, dHits, logVal;
			
			handle2 = document.getElementById("wm_other");
			if (handle = document.getElementById("wm_countries")) {
				minFont = 6;
				maxFont = 24;
				dFont = maxFont - minFont;
				dHits = maxY - minY;
				
				logVal = (maxFont - minFont) / log(maxY);
				
				/*for (i = 0; i < queryData.length; i++) {
					if (queryData[i].tld.posX != 0 || queryData[i].tld.posY != 0) {
    					handle.childNodes[queryData[i].tld.mapId].style.fontSize = Math.round(minFont + dFont * (queryData[i].hits - minY) / dHits) + "px";
					}
					else {
						handle2.childNodes[queryData[i].tld.mapId].style.fontSize = Math.round(minFont + dFont * (queryData[i].hits - minY) / dHits) + "px";
					}
				}*/
				
				for (i = 0; i < queryData.length; i++) {
					if (queryData[i].tld.posX != 0 || queryData[i].tld.posY != 0) {
    					handle.childNodes[queryData[i].tld.mapId].style.fontSize = realInt(minFont + log(queryData[i].hits) * logVal, 10) + "px";
					}
					else {
						
						handle2.childNodes[queryData[i].tld.mapId].style.fontSize = realInt(minFont + log(queryData[i].hits) * logVal, 10) + "px";
					}
				}
			}
		}
		
		this.flagClicked = false;
		this.flagHover = function(e) {
			if (pagina.graph.flagClicked) return false;
			var handle, info;
			handle = pagina.data[this.id];
			info = document.getElementById("tld_info");
			info.style.display = "block";
			info.firstChild.firstChild.nodeValue = handle.tld.title;
			info.childNodes[1].childNodes[1].firstChild.nodeValue = "." + handle.tld.tld;
			info.childNodes[2].childNodes[0].firstChild.nodeValue = toBigInt(handle.tld.hits);
			info.childNodes[3].childNodes[0].firstChild.nodeValue = toBigInt(handle.hits);
			info.childNodes[4].childNodes[0].firstChild.nodeValue = toDecimal(handle.pop, 2);
			document.getElementById("info_popup_link").href = "http://www.google.com/search?q=" + handle.keyword.value + "&as_sitesearch=." + handle.tld.tld + query.filterTimeQuery;
			info.style.top = (((e) ? e.pageY : event.clientY + document.body.scrollTop) + 10) + "px";
			info.style.left = (((e) ? e.pageX : event.clientX + document.body.scrollLeft) + 10) + "px";
		}
		
		this.flagOut = function() {
			if (pagina.graph.flagClicked) return false;
			document.getElementById("tld_info").style.display = "none";
		}
		
		this.flagClick = function() {
			pagina.graph.flagClicked = true;
			setTimeout("addRemovableEvent(document, 'click', pagina.graph.flagBodyClick)", 10);
			document.getElementById("info_popup_link").style.display = "block";
		}
		this.flagBodyClick = function() {
			pagina.graph.flagClicked = false;
			document.getElementById("tld_info").style.display = "none";
			removeEvent(document, 'click', pagina.graph.flagBodyClick);
			document.getElementById("info_popup_link").style.display = "none";
		}
		
		this.clearHTML = function() {
			var handle, temp;
			if (handle = document.getElementById("graph_a_flags")) {
				for (i = 0; i < (data.tlds.length - 1); i++) {
					handle.childNodes[i+32].style.left = "-100px";
				}
			}
			if (handle = document.getElementById("wm_countries")) {
				for (i = 0; i < handle.childNodes.length; i++) {
					handle.childNodes[i].style.fontSize = "1px";
				}
			}
			if (handle = document.getElementById("wm_other")) {
				for (i = 0; i < handle.childNodes.length; i++) {
					handle.childNodes[i].style.fontSize = "1px";
				}
			}
		}
	}
	
	/* Catagorie lijsten */
	this.catData = new function() {
		this.data = Array();
		
		this.reset = function() {
			this.clearResults();
			this.clearHTML;
		}
		
    	this.clearHTML = function() {
    		var handle, handles;
			handles = Array("cd_highest_hits", "cd_highest_pop", "cd_unknown", "cd_unpopular", "cd_hpr", "cd_ppr");
    		for (i = 0; i < handles.length; i++) {
				if (handle = document.getElement(handles[i])) {
					for (c = 0; c < handle.childNodes.length; c++) {
						handle.childNodes[c].style.display = "none";
					}
				}
			}
    	}
		
		this.setTemplates = function() {
			var handle, handles, c, i, div, span, temp;
			handles = Array("cd_highest_hits", "cd_highest_pop", "cd_unknown", "cd_unpopular");
			for (c = 0; c < handles.length; c++) {
    			if (handle = document.getElementById(handles[c])) {
    				for (i = 0; i < 20; i++) {
    					span = document.createElement("span");
    					span.appendChild(document.createTextNode("#"));
    					div = document.createElement("a");
						div.href = "#";
						div.target = "_blank";
    					div.appendChild(span);
    					div.appendChild(document.createTextNode("#"));
    					handle.appendChild(div);
    				}
    			}
			}
			if (handle = document.getElementById("cd_hpr")) {
				for (c = 0; c < data.regions.length; c++) {
					temp = document.createElement("i");
					temp.appendChild(document.createTextNode("#"));
					span = document.createElement("span");
					span.appendChild(document.createTextNode("#"));
					span.appendChild(temp);
					div = document.createElement("div");
					div.appendChild(span);
					div.appendChild(document.createTextNode("#"));
					div.className = "cd_title";
					handle.appendChild(div);
					for (i = 0; i < 3; i++) {
    					span = document.createElement("span");
    					span.appendChild(document.createTextNode("#"));
    					div = document.createElement("a");
						div.href = "#";
						div.target = "_blank";
    					div.appendChild(span);
    					div.appendChild(document.createTextNode("#"));
    					handle.appendChild(div);
    				}
				}	
			}
			if (handle = document.getElementById("cd_ppr")) {
				for (c = 0; c < data.regions.length; c++) {
					div = document.createElement("div");
					div.appendChild(document.createTextNode("#"));
					div.className = "cd_title";
					handle.appendChild(div);
					for (i = 0; i < 3; i++) {
    					span = document.createElement("span");
    					span.appendChild(document.createTextNode("#"));
    					div = document.createElement("a");
						div.href = "#";
						div.target = "_blank";
    					div.appendChild(span);
    					div.appendChild(document.createTextNode("#"));
    					handle.appendChild(div);
    				}
				}	
			}
		}
		
		this.draw = function() {
			var handle, elem, i, num, temp, c, totalHits;
			totalHits = 0;
			
			for (i = 1; i < data.regions.length; i++) {
				data.regions[i].hits = 0;
				data.regions[i].pop = 0;
				data.regions[i].results = 0;
			}
			for (i = 1; i < this.data.length; i++) {
				temp = this.data[i];
				handle = temp.tld.region;
				handle.hits += temp.hits;
				handle.pop += temp.pop;
				handle.results++;
				totalHits += temp.hits;
			}
			for (i = 1; i < data.regions.length; i++) {
				data.regions.pop = data.regions.pop / data.regions.results;
			}
			data.regions.sort(this.sortHits);
			this.data.sort(this.sortHits);
			if (handle = document.getElementById("cd_hpr")) {
				for (i = 1; i < data.regions.length; i++) {
					elem = handle.childNodes[i*4];
					elem.childNodes[1].nodeValue = data.regions[i].title;
					elem.childNodes[0].firstChild.nodeValue = Math.round(100 * data.regions[i].hits / totalHits) + "% ";
					elem.childNodes[0].childNodes[1].firstChild.nodeValue = "(" + toBigInt(data.regions[i].hits) + ")";
					elem.style.display = "block";
					num = 0;
					for (c = 0; c < this.data.length && num < 3; c++) {
						if (this.data[c].tld.region == data.regions[i]) {
							temp = this.data[c];
							elem = handle.childNodes[i*4 + 1 + num];
							elem.firstChild.firstChild.nodeValue = toBigInt(temp.hits);
							elem.childNodes[1].nodeValue = temp.tld.title;
							//elem.style.backgroundImage = "url(flags/" + temp.tld.tld + ".png)";
							elem.style.backgroundPosition = "0px -" + (64 * temp.tld.tilePos) + "px";
							elem.style.display = "block";
							elem.href = "http://www.google.com/search?q=" + parseHtmlValue(temp.keyword.value, true) + "&as_sitesearch=." + temp.tld.tld + query.filterTimeQuery;
							num++;
						}
					}
					while (num < 3) {
						handle.childNodes[i*4 + 1 + num].style.display = "none";
						num++;
					}
				}
			}
			if (handle = document.getElementById("cd_highest_hits")) {
				for (i = 0; i < this.data.length && i < 20; i++) {
					temp = this.data[i];
					elem = handle.childNodes[i];
					elem.firstChild.firstChild.nodeValue = toBigInt(temp.hits);
					elem.childNodes[1].nodeValue = temp.tld.title;
					//elem.style.backgroundImage = "url(flags/" + temp.tld.tld + ".png)";
					elem.style.backgroundPosition = "0px -" + (64 * temp.tld.tilePos) + "px";
					elem.style.display = "block";
					elem.href = "http://www.google.com/search?q=" + parseHtmlValue(temp.keyword.value, true) + "&as_sitesearch=." + temp.tld.tld + query.filterTimeQuery;
				}
			}
			
			data.regions.sort(this.sortPop);
			this.data.sort(this.sortPop);
			if (handle = document.getElementById("cd_ppr")) {
				for (i = 1; i < data.regions.length; i++) {
					elem = handle.childNodes[i*4];
					elem.firstChild.nodeValue = data.regions[i].title;
					elem.style.display = "block";
					num = 0;
					for (c = 0; c < this.data.length && num < 3; c++) {
						if (this.data[c].tld.region == data.regions[i]) {
							temp = this.data[c];
							elem = handle.childNodes[i*4 + 1 + num];
							elem.firstChild.firstChild.nodeValue = toDecimal(temp.pop, 2);
							elem.childNodes[1].nodeValue = temp.tld.title;
							//elem.style.backgroundImage = "url(flags/" + temp.tld.tld + ".png)";
							elem.style.backgroundPosition = "0px -" + (64 * temp.tld.tilePos) + "px";
							elem.style.display = "block";
							elem.href = "http://www.google.com/search?q=" + parseHtmlValue(temp.keyword.value, true) + "&as_sitesearch=." + temp.tld.tld + query.filterTimeQuery;
							num++;
						}
					}
					while (num < 3) {
						handle.childNodes[i*4 + 1 + num].style.display = "none";
						num++;
					}
				}
			}
			if (handle = document.getElementById("cd_highest_pop")) {
				for (i = 0; i < this.data.length && i < 20; i++) {
					temp = this.data[i];
					elem = handle.childNodes[i];
					elem.firstChild.firstChild.nodeValue = toDecimal(temp.pop, 2);
					elem.childNodes[1].nodeValue = temp.tld.title;
					//elem.style.backgroundImage = "url(flags/" + temp.tld.tld + ".png)";
					elem.style.backgroundPosition = "0px -" + (64 * temp.tld.tilePos) + "px";
					elem.style.display = "block";
					elem.href = "http://www.google.com/search?q=" + parseHtmlValue(temp.keyword.value, true) + "&as_sitesearch=." + temp.tld.tld + query.filterTimeQuery;
				}
			}
			this.data.reverse();
			if (handle = document.getElementById("cd_unpopular")) {
				for (i = 0; i < this.data.length && i < 20; i++) {
					temp = this.data[i];
					elem = handle.childNodes[i];
					elem.firstChild.firstChild.nodeValue = toDecimal(temp.pop, 2);
					elem.childNodes[1].nodeValue = temp.tld.title;
					//elem.style.backgroundImage = "url(flags/" + temp.tld.tld + ".png)";
					elem.style.backgroundPosition = "0px -" + (64 * temp.tld.tilePos) + "px";
					elem.style.display = "block";
					elem.href = "http://www.google.com/search?q=" + parseHtmlValue(temp.keyword.value, true) + "&as_sitesearch=." + temp.tld.tld + query.filterTimeQuery;
				}
			}
			this.data.sort(this.sortSites);
			if (handle = document.getElementById("cd_unknown")) {
				num = 0;
				for (i = 0; i < this.data.length && num < 20; i++) {
					if (this.data[i].hits == 0) {
						temp = this.data[i];
						elem = handle.childNodes[num];
						elem.firstChild.firstChild.nodeValue = "0";
						elem.childNodes[1].nodeValue = temp.tld.title;
						//elem.style.backgroundImage = "url(flags/" + temp.tld.tld + ".png)";
						elem.style.backgroundPosition = "0px -" + (64 * temp.tld.tilePos) + "px";
						elem.style.display = "block";
						elem.href = "http://www.google.com/search?q=" + parseHtmlValue(temp.keyword.value, true) + "&as_sitesearch=." + temp.tld.tld + query.filterTimeQuery;
						num++;
					}
				}
			}
		}
		
		this.appendResults = function(results) {
    		var i;
    		for (i = 0; i < results.length; i++) {
    			this.data[this.data.length] = results[i];
    		}
		}
		
		this.clearResults = function() {
			this.data = Array();
		}
		
		this.sortHits = function(a, b) {return b.hits - a.hits;}
		this.sortPop = function(a, b) {return b.pop - a.pop;}
		this.sortSites = function(a, b) {return b.tld.hits - a.tld.hits;}
	}
	
    /* All data tabel */
    this.allData = new function() {
    	this.data = Array();
    	this.sortColumn = false;
		this.sortHandle = false;
		this.sortTitle = false;
		
		this.reset = function() {
			this.clearResults();
			this.clearHTML;
			this.resetSort();
		}
		
    	this.clearHTML = function() {
    		var handle;
    		handle = document.getElementById("all_data");
    		handle.removeChild(handle.childNodes[1]);
    		handle = handle.appendChild(document.createElement("tbody"));
    		return handle;
    	}
    	
    	this.draw = function() {
			var handle, data, i, row, cell;
    		handle = this.clearHTML();
    		for (i = 0; i < this.data.length; i++) {
    			data = this.data[i];
    			row = document.createElement('tr');
    			// Tld
    			cell = document.createElement('td');
    			cell.appendChild(document.createTextNode(data.tld.tld));
    			row.appendChild(cell);
    			// Domain title
    			cell = document.createElement('td');
    			cell.appendChild(document.createTextNode(data.tld.title));
    			row.appendChild(cell);
    			// Region
    			cell = document.createElement('td');
    			cell.appendChild(document.createTextNode(data.tld.region.title));
    			row.appendChild(cell);
    			// Pages
    			cell = document.createElement('td');
    			cell.appendChild(document.createTextNode(toBigInt(data.tld.hits)));
    			row.appendChild(cell);
    			// Hits
    			cell = document.createElement('td');
				cell.appendChild(document.createElement('a'));
				cell.firstChild.href = "http://www.google.com/search?q=" + parseHtmlValue(data.keyword.value, true) + "&as_sitesearch=." + data.tld.tld + query.filterTimeQuery;
				cell.firstChild.target = '_blank';
    			cell.firstChild.appendChild(document.createTextNode(toBigInt(data.hits)));
    			row.appendChild(cell);
    			// Hits Ranking
    			cell = document.createElement('td');
    			cell.appendChild(document.createTextNode(data.hitsRank));
    			row.appendChild(cell);
    			// Popularity
    			cell = document.createElement('td');
    			cell.appendChild(document.createTextNode(toDecimal(data.pop, 2)));
    			row.appendChild(cell);
    			// Popularity ranking
    			cell = document.createElement('td');
    			cell.appendChild(document.createTextNode(data.popRank));
    			row.appendChild(cell);
    			
    			handle.appendChild(row);
    		}
    	}
    	
    	this.appendResults = function(results) {
    		var i;
    		for (i = 0; i < results.length; i++) {
    			this.data[this.data.length] = results[i];
    		}
			this.data.sort(this.sortPop);
			for (i = 0; i < this.data.length; i++) {
				this.data[i].popRank = i + 1;
			}
			this.data.sort(this.sortHits);
			for (i = 0; i < this.data.length; i++) {
				this.data[i].hitsRank = i + 1;
			}
		}
		
		this.clearResults = function() {
			this.data = Array();
		}
		
		this.sort = function(column, handle, title) {
			switch (column) {
				case "hits": this.data.sort(function(a, b) {return b.hitsRank - a.hitsRank;}); break;
				case "popularity": this.data.sort(function(a, b) {return b.popRank - a.popRank;}); break;
				case "tld": this.data.sort(function(a, b){return (a.tld.tld > b.tld.tld) ? 1 : ((a.tld.tld < b.tld.tld) ? -1 : 0)}); break;
				case "title": this.data.sort(function(a, b){return (a.tld.title > b.tld.title) ? 1 : ((a.tld.title < b.tld.title) ? -1 : 0)}); break;
				case "region": this.data.sort(function(a, b){return (a.tld.region.title > b.tld.region.title) ? 1 : ((a.tld.region.title < b.tld.region.title) ? -1 : 0)}); break;
				case "pages": this.data.sort(function(a, b) {return b.tld.hits - a.tld.hits;}); break;
			}
			if (!this.sortHandle) {
				this.sortHandle = document.getElementById("sortDefault");
				this.sortTitle = "HITS";
			}
			this.resetSort();
			this.sortHandle = handle;
			this.sortTitle = title;
			if (this.sortColumn == column) {
				this.data.reverse();
				this.sortColumn = false;
				handle.firstChild.nodeValue = title + " ^";
			}
			else {
				this.sortColumn = column;
				handle.firstChild.nodeValue = title + " v";
			}
			handle.style.textDecoration = "none";
			this.draw();
		}
		
		this.resetSort = function() {
			if (this.sortHandle) this.sortHandle.style.textDecoration = "underline";
			if (this.sortTitle) this.sortHandle.firstChild.nodeValue = this.sortTitle;
		}
		
		this.sortHits = function(a, b) {return b.hits - a.hits;}
		this.sortPop = function(a, b) {return b.pop - a.pop;}
    }
	
	/* Algemene data handling */
	this.reset = function() {
		this.clearResults();
		this.graph.clearHTML();
	}
	
	this.appendResults = function(results) {
   		var i;
   		for (i = 0; i < results.length; i++) {
   			this.data[this.data.length] = results[i];
   		}
		this.data.sort(this.sortPop);
		for (i = 0; i < this.data.length; i++) {
			this.data[i].popRank = i + 1;
		}
		this.data.sort(this.sortHits);
		for (i = 0; i < this.data.length; i++) {
			this.data[i].hitsRank = i + 1;
		}
	}
	
	this.clearResults = function() {
		this.data = Array();
	}
	this.sortHits = function(a, b) {return b.hits - a.hits;}
	this.sortPop = function(a, b) {return b.pop - a.pop;}
}

/* Pagina interactie */
function startSplit() {
	var time = document.getElementById("input_timefilter").value;
	var keyword = parseHtmlValue(document.getElementById("input").value, false);
	if (keyword != "type keyword or phrase") {
		if (document.getElementById("look_where").value != 1 && document.getElementById("look_where").value != 2) {
			// Switch to multisplit
			var tlds = Array();
			var catValue = document.getElementById("look_where").value;
			for (i = 0; i < catData.length; i++) {
				if (catValue == catData[i][2]) {
					tlds[tlds.length] = catData[i][1];
				}
			}
			var url = 'multi.php?url_query=33&keywords=' + escape(keyword) + '&tlds=' + escape(tlds.join(',')) + '&time=' + escape(time);
			window.location = 'http://www.hitsplitter.com/' + url;
			return false;
		}
		document.getElementById('main_title').firstChild.nodeValue = "Splitting results";
		pagina.allData.reset();
		pagina.catData.reset();
		pagina.reset();
		pagina.splitStatus.show();
		query.createBasicQuery(keyword, document.getElementById("look_where").value == '2', time);
		query.execute();
		document.getElementById("page_default").style.display = "none";
		document.getElementById("page_results").style.display = "block";
		document.getElementById("gb_ga_large").style.color = "#6e6c6c";
		document.getElementById("gb_wm_large").style.color = "#6e6c6c";
		document.getElementById("gb_ga_large").style.cursor = "default";
		document.getElementById("gb_wm_large").style.cursor = "default";
	}
	else {
		alert("enter a keyword before splitting");
	}
}

/* Split the same query again */
function splitAgain(keyword, all, time) {
	query.noCache = true;
	document.getElementById('main_title').firstChild.nodeValue = "Splitting results";
	pagina.allData.reset();
	pagina.catData.reset();
	pagina.reset();
	pagina.splitStatus.show();
	query.createBasicQuery(keyword, all, time);
	query.execute();
	document.getElementById("page_default").style.display = "none";
	document.getElementById("page_results").style.display = "block";
	document.getElementById("gb_ga_large").style.color = "#6e6c6c";
	document.getElementById("gb_wm_large").style.color = "#6e6c6c";
	document.getElementById("gb_ga_large").style.cursor = "default";
	document.getElementById("gb_wm_large").style.cursor = "default";
}

function cancelSplit() {
	query.cancelSplit();
}



function setExactWording() {
	var input, output, exact, i, word;
	input = document.getElementById("input").value;
	exact = document.getElementById("exact_wording").checked == true ? true : false;
	if (input.match(/[^"]+"[^"]+/g)) {
		return false;
	}
	if (input != "type keyword or phrase") {
		if (exact) {
			if (input != "") output = "\"" + input.replace(/["]/g, "") + "\"";
		}
		else {
			output = input.replace(/["]/g, "");
		}
		document.getElementById("input").value = output;
	}
}



/* Grafiek popups */
function openWorldMap() {
	if (!query.processing) window.open("worldmap.html", "WorldMap", "toolbar=no, resizable=yes, scrollbars=no, location=no");
}

function openGraph() {
	if (!query.processing) window.open("graph.html", "Graph", "toolbar=no, resizable=yes, scrollbars=no, location=no");
}

/* Global events */
window.onload = function() {
	pagina.catData.setTemplates();
	pagina.graph.setTemplates();
	if (SetSavedQuery) setSavedQuery();
	//query.createBasicQuery("test", false);
	//query.execute();
}
