var KEYWORD_DEFAULT = "type keywords or phrases, one per line";
var DOMAIN_DEFAULT = "add other sites, e.g. digg.com, one per line";

function cloneObj(obj) {
	var item, newObj = Array();
	for (item in obj) {
		if (typeof obj[item] == "object") newObj[item] = cloneObj(obj[item]);
		else newObj[item] = obj[item];
	} 
	return newObj;
}


/* Query events */
function onQueryExecuted() {
	pagina.splitStatus.freeze();
	saveMultiQuery();
}

function onQueryData(results) {
	pagina.appendResults(results);
	pagina.splitStatus.update();
	pagina.allData.draw();
	pagina.tabel.update();
}

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("multi", ref_id)));
	temp.href = savedUrl("multi", ref_id);
	temp.id = 'permalink_link';
	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;
			domains = query.data.length;
			hits = 0;
			results = 0;
			sent = 0;
			for (i = 0; i < domains; i++) {
				result = query.data[i];
				if (result.readyState == 2) {
					hits += result.hits;
					results++;
				}
				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 " + query.keywords.length + " " + (query.keywords.length == 1 ? 'keyword' : 'keywords') + " and " + query.tlds.length + " " + (query.tlds.length == 1 ? 'domain' : 'domains') + extra);
			
			setValue("ss_keywords", (query.keywords.length) + " KEYWORD" + (query.keywords.length == 1 ? '' : 'S'));
			setValue("ss_domains", (query.tlds.length) + " DOMAIN" + (query.tlds.length == 1 ? '' : 'S'));
			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");
			
			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 ### domains";
		}
		
		this.freeze = function() {
			document.getElementById("processing").style.color = "#009ff4";
			document.getElementById("cancel").style.display = "none";
			document.getElementById("processing_loader").style.backgroundPosition = "100px 0px";
		}
	}
	
    /* All data tabel */
    this.allData = new function() {
    	this.data = Array();
    	this.sortColumn = false;
		
		this.reset = function() {
			this.clearHTML;
		}
		
    	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 < pagina.data.length; i++) {
    			data = pagina.data[i];
    			row = document.createElement('tr');
    			// Keyword
    			cell = document.createElement('td');
    			cell.appendChild(document.createTextNode(parseHtmlValue(data.keyword.value, false)));
    			row.appendChild(cell);
    			// Domain
    			cell = document.createElement('td');
    			cell.appendChild(document.createTextNode(data.tld.value));
    			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.value + (query.filterTime != "" ? "&as_qdr=" + query.filterTime : "");
				cell.firstChild.target = '_blank';
    			cell.firstChild.appendChild(document.createTextNode(toBigInt(data.hits)));
    			row.appendChild(cell);
    			
    			handle.appendChild(row);
    		}
    	}
		
		this.sort = function(column) {
			switch (column) {
				case "hits": this.data.sort(function(a, b) {return b.hits - a.hits;}); break;
				case "tld": this.data.sort(function(a, b){return (a.tld.value > b.tld.value) ? 1 : ((a.tld.value < b.tld.value) ? -1 : 0)}); break;
				case "keyword": this.data.sort(function(a, b){return (a.keyword.value > b.keyword.value) ? 1 : ((a.keyword.value < b.keyword.value) ? -1 : 0)}); break;
			}
			if (this.sortColumn == column) {
				this.data.reverse();
				this.sortColumn = false;
			}
			else {
				this.sortColumn = column;
			}
			this.draw();
		}
    }
	
	/* Multi tabel */
	this.tabel = new function() {
		this.sortCol = false;
		this.sortAsc = true;
		this.sortRow = false;
		this.sortRowAsc = true;
		this.switchTable = 'kd';
		
		this.setSwitch = function() {
			this.switchTable = this.switchTable == 'kd' ? 'dk' : 'kd';
			this.draw();
			this.update();
		}
		
		this.sort = function(column) {
			this.sortAsc = (this.sortCol == column && this.sortAsc) ? false : true;
			this.sortCol = column;
			if (this.sortRow) {
				this.sortRow = false;
				this.sortRowAsc = true;
				this.draw();
			}
			this.update();
		}
		
		this.sortR = function(row) {
			this.sortRowAsc = (this.sortRow == row && this.sortRowAsc) ? false : true;
			this.sortRow = row;
			this.update();
		}
		
		this.update = function() {
			var handle, data, i, c, total_tlds, total_keywords, max_tlds, max_keywords, max_total, max_tld, max_keyword, cell, paginaData, show, temp, link;
			
			// Get the table handle
			handle = document.getElementById("multiTabel").firstChild.firstChild;
			
			
			// Weergave modus
			//paginaData = eval(pagina.data.toSource());
			paginaData = cloneObj(pagina.data);
			
			show = document.getElementById('showModeSelect').value;
			switch (show) {
				case 'relative':
					for (i = 0; i < paginaData.length; i++) {
						if (paginaData[i].tld.hits) {
							paginaData[i].hits = (paginaData[i].hits * 1000) / paginaData[i].tld.hits;
						}
						else {
							paginaData[i].hits = 0;
						}
					}
					break;
				case 'perc_tlds':
					total_tlds = Array();
					for (i = 0; i < (query.tlds.length); i++) total_tlds[i] = 0;
					for (i = 0; i < paginaData.length; i++) {
						total_tlds[paginaData[i].y] += paginaData[i].hits;
					}
					for (i = 0; i < paginaData.length; i++) {
						paginaData[i].hits = total_tlds[paginaData[i].y] ? paginaData[i].hits / total_tlds[paginaData[i].y] : 0;
					}
					break;
				case 'perc_keywords':
					total_keywords = Array();
					for (i = 0; i < (query.keywords.length); i++) total_keywords[i] = 0;
					for (i = 0; i < paginaData.length; i++) {
						if (paginaData[i].tld.value != "") {
							total_keywords[paginaData[i].x] += paginaData[i].hits;
						}
					}
					for (i = 0; i < paginaData.length; i++) {
						paginaData[i].hits = total_keywords[paginaData[i].x] != 0 ? paginaData[i].hits / total_keywords[paginaData[i].x] : 0;
					}
					break;
			}
			
			
			// Update the permalink
			if (temp = document.getElementById('permalink_link')) {
				link = temp.href;
				if (link.indexOf('?disp') > 0) link = link.substr(0, link.indexOf('?disp'));
				link += '?disp=' + show;
				link += '&switch=' + this.switchTable;
				temp.href = link;
				temp.firstChild.nodeValue = link;
			}
			
			
			if (this.switchTable != 'dk') {
				// Sorteer opties
				if (this.sortCol) {
					// Temp sorteer tabel maken
					data = Array();
					for (i = 0; i < paginaData.length; i++) {
						if (paginaData[i].x == this.sortCol - 1 && paginaData[i].y < query.tlds.length) {
							data[data.length] = paginaData[i];
						}
					}
					data.sort(function(a, b) {return b.hits - a.hits;});
					if (!this.sortAsc) data.reverse();
					sortOrder = Array();
					sortOrder2 = Array();
					for (i = 0; i < data.length; i++) {
						sortOrder[data[i].y] = i;
						sortOrder2[i] = data[i].y;
					}
					
					// Tabel in juiste volgorde zetten
					for (i = 0; i < paginaData.length; i++) {
						if (sortOrder[paginaData[i].y] != null) {
							paginaData[i].y = sortOrder[paginaData[i].y];
						}
					}
					
					// Tlds in juiste volgorde zetten
					for (i = 0; i < query.tlds.length; i++) {
						handle.childNodes[i + 2].childNodes[0].firstChild.firstChild.nodeValue = query.tlds[sortOrder2[i]].value;
						handle.childNodes[i + 2].childNodes[1].firstChild.firstChild.nodeValue = toBigInt(query.tlds[sortOrder2[i]].hits);
						handle.childNodes[i + 2].childNodes[1].firstChild.href = "http://www.google.com/search?q=&as_sitesearch=" + query.tlds[sortOrder2[i]].value + (query.filterTime != "" ? "&as_qdr=" + query.filterTime : "");
					}
					
					// Stijl van de gesorteerde kolom aanpassen
					for (i = 0; i < query.keywords.length; i++) {
						handle.firstChild.childNodes[1 + i].className = i + 1 == this.sortCol ? 'h2_domains sortCol' : 'h2_domains';
					}
				}
				else {
					for (i = 0; i < query.tlds.length; i++) {
						handle.childNodes[i + 2].childNodes[1].firstChild.firstChild.nodeValue = toBigInt(query.tlds[i].hits);
						handle.childNodes[i + 2].childNodes[1].firstChild.href = "http://www.google.com/search?q=&as_sitesearch=" + query.tlds[i].value + (query.filterTime != "" ? "&as_qdr=" + query.filterTime : "");
					}
				}
				
				if (this.sortRow) {
					// Temp sorteer tabel maken
					data = Array();
					for (i = 0; i < paginaData.length; i++) {
						if (paginaData[i].y == this.sortRow - 1 && paginaData[i].x < query.keywords.length) {
							data[data.length] = paginaData[i];
						}
					}
					data.sort(function(a, b) {return b.hits - a.hits;});
					if (!this.sortRowAsc) data.reverse();
					sortOrder = Array();
					sortOrder2 = Array();
					for (i = 0; i < data.length; i++) {
						sortOrder[data[i].x] = i;
						sortOrder2[i] = data[i].x;
					}
					
					// Tabel in juiste volgorde zetten
					for (i = 0; i < paginaData.length; i++) {
						if (sortOrder[paginaData[i].x] != null) {
							paginaData[i].x = sortOrder[paginaData[i].x];
						}
					}
					
					// Keywords in juiste volgorde zetten
					for (i = 0; i < query.keywords.length; i++) {
						handle.childNodes[0].childNodes[i + 1].firstChild.firstChild.nodeValue = parseHtmlValue(query.keywords[sortOrder2[i]].value, false);
					}
					
					// Stijl van de gesorteerde kolom aanpassen
					for (i = 0; i < query.tlds.length; i++) {
						handle.childNodes[i + 2].childNodes[0].className = i + 1 == this.sortRow ? 'h2_keywords sortCol' : 'h2_keywords';
					}
				}
			}
			else {
				// Sorteer opties switched
				if (this.sortCol) {
					// Temp sorteer tabel maken
					data = Array();
					for (i = 0; i < paginaData.length; i++) {
						if (paginaData[i].x == this.sortCol - 1 && paginaData[i].y < query.tlds.length) {
							data[data.length] = paginaData[i];
						}
					}
					data.sort(function(a, b) {return b.hits - a.hits;});
					if (!this.sortAsc) data.reverse();
					sortOrder = Array();
					sortOrder2 = Array();
					for (i = 0; i < data.length; i++) {
						sortOrder[data[i].y] = i;
						sortOrder2[i] = data[i].y;
					}
					
					// Tabel in juiste volgorde zetten
					for (i = 0; i < paginaData.length; i++) {
						if (sortOrder[paginaData[i].y] != null) {
							paginaData[i].y = sortOrder[paginaData[i].y];
						}
					}
					
					// Tlds in juiste volgorde zetten
					for (i = 0; i < query.tlds.length; i++) {
						handle.childNodes[0].childNodes[i + 1].firstChild.firstChild.nodeValue = query.tlds[sortOrder2[i]].value;
						handle.childNodes[1].childNodes[i + 1].firstChild.firstChild.nodeValue = toBigInt(query.tlds[sortOrder2[i]].hits);
						handle.childNodes[1].childNodes[i + 1].firstChild.href = "http://www.google.com/search?q=&as_sitesearch=" + query.tlds[sortOrder2[i]].value + (query.filterTime != "" ? "&as_qdr=" + query.filterTime : "");
					}
					
					// Stijl van de gesorteerde kolom aanpassen
					for (i = 0; i < query.keywords.length; i++) {
						handle.childNodes[3 + i].childNodes[0].className = i + 1 == this.sortCol ? 'h2_keywords_sw sortCol' : 'h2_keywords_sw';
					}
				}
				else {
					for (i = 0; i < query.tlds.length; i++) {
						handle.childNodes[1].childNodes[i + 1].firstChild.firstChild.nodeValue = toBigInt(query.tlds[i].hits);
						handle.childNodes[1].childNodes[i + 1].firstChild.href = "http://www.google.com/search?q=&as_sitesearch=" + query.tlds[i].value + (query.filterTime != "" ? "&as_qdr=" + query.filterTime : "");
					}
				}
				
				if (this.sortRow) {
					// Temp sorteer tabel maken
					data = Array();
					for (i = 0; i < paginaData.length; i++) {
						if (paginaData[i].y == this.sortRow - 1 && paginaData[i].x < query.keywords.length) {
							data[data.length] = paginaData[i];
						}
					}
					data.sort(function(a, b) {return b.hits - a.hits;});
					if (!this.sortRowAsc) data.reverse();
					sortOrder = Array();
					sortOrder2 = Array();
					for (i = 0; i < data.length; i++) {
						sortOrder[data[i].x] = i;
						sortOrder2[i] = data[i].x;
					}
					
					// Tabel in juiste volgorde zetten
					for (i = 0; i < paginaData.length; i++) {
						if (sortOrder[paginaData[i].x] != null) {
							paginaData[i].x = sortOrder[paginaData[i].x];
						}
					}
					
					// Tlds in juiste volgorde zetten
					for (i = 0; i < query.keywords.length; i++) {
						handle.childNodes[i + 3].childNodes[0].firstChild.firstChild.nodeValue = parseHtmlValue(query.keywords[sortOrder2[i]].value, false);
					}
					
					// Stijl van de gesorteerde kolom aanpassen
					for (i = 0; i < query.tlds.length; i++) {
						handle.childNodes[0].childNodes[i + 1].className = i + 1 == this.sortRow ? 'h2_domains_sw sortCol' : 'h2_domains_sw';
					}
				}
			}
			
			
			
			total_tlds = Array();
			total_keywords = Array();
			max_tlds = Array();
			max_keywords = Array();
			max_total = false;
			max_tld = 0;
			max_keyword = 0;
			for (i = 0; i < (query.tlds.length + 1); i++) {
				total_tlds[i] = 0;
				max_tlds[i] = false;
			}
			for (i = 0; i < query.keywords.length; i++) {
				total_keywords[i] = 0;
				max_keywords[i] = false;
			}
			
			
			if (this.switchTable != 'dk') {
				// Maximale hits
				for (i = 0; i < paginaData.length; i++) {
					data = paginaData[i];
					total_tlds[data.y] += data.hits;
					if (data.hits > ((max_tlds[data.y]) ? max_tlds[data.y].hits : 0)) max_tlds[data.y] = data;
					if (data.tld.value != "") {
						if (data.hits > ((max_keywords[data.x]) ? max_keywords[data.x].hits : 0)) max_keywords[data.x] = data;
						total_keywords[data.x] += data.hits;
						if (data.hits > ((max_total) ? max_total.hits : 0)) max_total = data;
					}
				}
				
				// Cellen invullen
				for (i = 0; i < paginaData.length; i++) {
					data = paginaData[i];
					cell = handle.childNodes[data.y + 2].childNodes[data.x + 3];
					if (cell.firstChild.firstChild) {
						cell.firstChild.firstChild.nodeValue = toShowMode(data.hits, show, 'cell_multi');
						cell.firstChild.href = "http://www.google.com/search?q=" + parseHtmlValue(data.keyword.value, true) + "&as_sitesearch=" + data.tld.value + (query.filterTime != "" ? "&as_qdr=" + query.filterTime : "");
					}
					else {
						cell.firstChild.nodeValue = toShowMode(data.hits, show, 'roi_multi');
					}
					cell.id = "";
					if (data.tld.value != "") cell.className = "cell";
				}
				
				/* Totalen */
				for (i = 0; i < total_tlds.length; i++) {
					cell = handle.childNodes[i + 2].childNodes[2];
					cell.firstChild.nodeValue = toShowMode(total_tlds[i], show, 'totaal_tlds_multi');
					cell.id = "";
					if (i < (total_tlds.length - 1)) {
						if (total_tlds[i] > total_tlds[max_tld]) max_tld = i;
					}
				}
				for (i = 0; i < total_keywords.length; i++) {
					cell = handle.childNodes[1].childNodes[i + 3];
					cell.firstChild.nodeValue = toShowMode(total_keywords[i], show, 'totaal_keywords_multi');
					cell.id = "";
					if (total_keywords[i] > total_keywords[max_keyword]) max_keyword = i;
				}
				
				/* Maximalen */
				for (i = 0; i < (max_tlds.length - 1); i++) {
					data = max_tlds[i];
					if (data) handle.childNodes[data.y + 2].childNodes[data.x + 3].id = "max_hits_tld";
				}
				for (i = 0; i < max_keywords.length; i++) {
					data = max_keywords[i];
					if (data) {
						cell = handle.childNodes[data.y + 2].childNodes[data.x + 3];
						cell.id = (cell.id == "") ? "max_hits_keyword" : "max_hits_both";
					}
				}
				data = max_total;
				if (data) handle.childNodes[data.y + 2].childNodes[data.x + 3].className += " max_hits";
				handle.childNodes[max_tld + 2].childNodes[2].id = "max_tld";
				handle.childNodes[1].childNodes[max_keyword + 3].id = "max_keyword";
			}
			else {
				// Maximale hits switched
				for (i = 0; i < paginaData.length; i++) {
					data = paginaData[i];
					total_tlds[data.y] += data.hits;
					if (data.hits > ((max_tlds[data.y]) ? max_tlds[data.y].hits : 0)) max_tlds[data.y] = data;
					if (data.tld.value != "") {
						if (data.hits > ((max_keywords[data.x]) ? max_keywords[data.x].hits : 0)) max_keywords[data.x] = data;
						total_keywords[data.x] += data.hits;
						if (data.hits > ((max_total) ? max_total.hits : 0)) max_total = data;
					}
				}
				
				// Cellen invullen
				for (i = 0; i < paginaData.length; i++) {
					data = paginaData[i];
					cell = handle.childNodes[data.x + 3].childNodes[data.y + 2];
					if (cell.firstChild.firstChild) {
						cell.firstChild.firstChild.nodeValue = toShowMode(data.hits, show, 'cell_multi');
						cell.firstChild.href = "http://www.google.com/search?q=" + parseHtmlValue(data.keyword.value, true) + "&as_sitesearch=" + data.tld.value + (query.filterTime != "" ? "&as_qdr=" + query.filterTime : "");
					}
					else {
						cell.firstChild.nodeValue = toShowMode(data.hits, show, 'roi_multi');
					}
					cell.id = "";
					if (data.tld.value != "") cell.className = "cell";
				}
				
				/* Totalen */
				for (i = 0; i < total_keywords.length; i++) {
					cell = handle.childNodes[i + 3].childNodes[1];
					cell.firstChild.nodeValue = toShowMode(total_keywords[i], show, 'totaal_tlds_multi');
					cell.id = "";
					if (total_keywords[i] > total_keywords[max_keyword]) max_keyword = i;
				}
				for (i = 0; i < total_tlds.length; i++) {
					cell = handle.childNodes[2].childNodes[i + 2];
					cell.firstChild.nodeValue = toShowMode(total_tlds[i], show, 'totaal_keywords_multi');
					cell.id = "";
					if (i < (total_tlds.length - 1)) {
						if (total_tlds[i] > total_tlds[max_tld]) max_tld = i;
					}
				}
				
				/* Maximalen */
				for (i = 0; i < max_keywords.length; i++) {
					data = max_keywords[i];
					if (data) handle.childNodes[data.x + 3].childNodes[data.y + 2].id = "max_hits_tld";
				}
				for (i = 0; i < (max_tlds.length - 1); i++) {
					data = max_tlds[i];
					if (data) {
						cell = handle.childNodes[data.x + 3].childNodes[data.y + 2];
						cell.id = (cell.id == "") ? "max_hits_keyword" : "max_hits_both";
					}
				}
				data = max_total;
				if (data) handle.childNodes[data.x + 3].childNodes[data.y + 2].className += " max_hits";
				handle.childNodes[max_keyword + 3].childNodes[1].id = "max_tld";
				handle.childNodes[2].childNodes[max_tld + 2].id = "max_keyword";
			}
		}
		
		this.draw = function() {
			var i, c;
			var keywords, tlds;
			keywords = query.keywords.length;
			tlds = query.tlds.length;
			
			var handle;
    		handle = document.getElementById("multiTabel");
    		while (handle.childNodes.length > 0) handle.removeChild(handle.childNodes[0]);
			
			var table, tbody, row, cell;
			table = document.createElement('table');
			tbody = document.createElement('tbody');
			table.appendChild(tbody);
			table.cellPadding = "0";
			table.cellSpacing = "0";
			table.className = "tabel";
			
			if (this.switchTable != 'dk') {
				/* Header */
				row = document.createElement('tr');
				for (i = 0; i < (keywords + 2); i++) {
					cell = document.createElement('td');
					if (i == 0) {
						temp = document.createElement('a');
						temp.href = 'javascript: pagina.tabel.setSwitch()';
						temp.appendChild(document.createTextNode(''));
						temp.className = 'switch_button';
						cell.colSpan = "3";
						cell.appendChild(temp);
						cell.className = "h1_domains";
					}
					else if (i == (keywords + 1)) {
						cell.appendChild(document.createTextNode(" "));
						cell.className = "h1_roi";
					}
					else {
						temp = document.createElement('a');
						temp.href = "javascript: pagina.tabel.sort(" + i + ")";
						temp.appendChild(document.createTextNode(parseHtmlValue(query.keywords[i-1].value, false)));
						cell.appendChild(temp);
						cell.className = "h2_domains";
					}
					row.appendChild(cell);
				}
				tbody.appendChild(row);
				
				
				
				row = document.createElement('tr');
				for (i = 0; i < (keywords + 4); i++) {
					cell = document.createElement('td');
					if (i == 0) {
						cell.appendChild(document.createTextNode(""));
						cell.className = "h1_keywords";
						cell.width = "79px";
					}
					else if (i == 1) {
						cell.appendChild(document.createTextNode(""));
						cell.className = "h1_allpages";
					}
					else if (i == 2) {
						cell.appendChild(document.createTextNode(""));
						cell.className = "h1_totals";
					}
					else if (i == (keywords + 3)) {
						cell.appendChild(document.createTextNode(" "));
						cell.className = "h2_roi";
					}
					else {
						cell.appendChild(document.createTextNode("0"));
						cell.className = "h2_totals";
					}
					row.appendChild(cell);
				}
				tbody.appendChild(row);
				
				
				for (i = 0; i < (tlds + 1); i++) {
					row = document.createElement('tr');
					if (i == tlds) {
						for (c = 0; c < (keywords + 4); c++) {
							cell = document.createElement('td');
							cell.appendChild(document.createTextNode("-"));
							if (c == 0) {
								cell.firstChild.nodeValue = "THE REST OF THE INTERNET";
								cell.className = "h2_roi_key";
							}
							else if (c == 2) cell.className = "h2_totals_under";
							else if (c == (keywords + 2)) cell.className = "h2_roi_under";
							else if (c == (keywords + 3)) {
								cell.firstChild.nodeValue = " ";
								cell.className = "";
							}
							else cell.className = "h2_roi_under";
							row.appendChild(cell);
						}
					}
					else if (i == (tlds + 1)) {
						for (c = 0; c < (keywords + 2); c++) {
							cell = document.createElement('td');
							cell.appendChild(document.createTextNode("-"));
							if (c == 0) cell.className = "f_keywords";
							else if (c == 1) cell.className = "f_totals";
							else if (c == (keywords + 1)) cell.className = "f_roi";
							else cell.className = "f_cell";
							row.appendChild(cell);
						}
					}
					else {
						for (c = 0; c < (keywords + 4); c++) {
							cell = document.createElement('td');
							if (c == 0) {
								temp = document.createElement('a');
								temp.href = "javascript: pagina.tabel.sortR(" + (i + 1) + ")";
								temp.appendChild(document.createTextNode(query.tlds[i].value));
								cell.appendChild(temp);
								cell.className = "h2_keywords";
							}
							else if (c == 2) {
								cell.appendChild(document.createTextNode("0"));
								cell.className = "h3_totals";
							}
							else if (c == (keywords + 3)) {
								cell.appendChild(document.createTextNode(" "));
								cell.className = "h3_roi";
							}
							else {
								temp = document.createElement('a');
								temp.href = "lalala";
								temp.target = "_blank";
								temp.appendChild(document.createTextNode("-"));
								cell.appendChild(temp);
								cell.className = "cell";
							}
							row.appendChild(cell);
						}
					}
					tbody.appendChild(row);
				}	
			}
			else {
				/* Header switched */
				row = document.createElement('tr');
				for (i = 0; i < (tlds + 2); i++) {
					cell = document.createElement('td');
					if (i == 0) {
						temp = document.createElement('a');
						temp.href = 'javascript: pagina.tabel.setSwitch()';
						temp.appendChild(document.createTextNode(''));
						temp.className = 'switch_button';
						cell.colSpan = "2";
						cell.appendChild(temp);
						cell.className = "h1_domains_sw";
					}
					else if (i == (tlds + 2)) {
						cell.appendChild(document.createTextNode(" "));
						cell.className = "h1_roi";
					}
					else if (i == (tlds + 1)) {
						cell = document.createElement('td');
						cell.appendChild(document.createTextNode("-"));
						cell.firstChild.nodeValue = "THE REST OF THE INTERNET";
						cell.className = "h2_roi_key_sw";
					}
					else {
						temp = document.createElement('a');
						temp.href = 'javascript: pagina.tabel.sortR(' + i + ')';
						temp.appendChild(document.createTextNode(query.tlds[i-1].value));
						cell.appendChild(temp);
						cell.className = "h2_domains_sw";
					}
					row.appendChild(cell);
				}
				tbody.appendChild(row);
				
				
				
				// Rest of the internet domein rij
				row = document.createElement('tr');
				for (i = 0; i < (tlds + 2); i++) {
					cell = document.createElement('td');
					if (i == 0) {
						cell.colSpan = "2";
						cell.appendChild(document.createTextNode(""));
						cell.className = "h1_allpages_sw";
					}
					else if (i == (tlds + 1)) {
						cell = document.createElement('td');
						cell.appendChild(document.createTextNode("-"));
						cell.className = "f_roi_sw";
					}
					else {
						temp = document.createElement('a');
						temp.href = "lalala";
						temp.target = "_blank";
						temp.appendChild(document.createTextNode("-"));
						cell.appendChild(temp);
						cell.className = "cell";
					}
					row.appendChild(cell);
				}
				tbody.appendChild(row);
				
				
				row = document.createElement('tr');
				for (i = 0; i < (tlds + 3); i++) {
					cell = document.createElement('td');
					if (i == 0) {
						cell.appendChild(document.createTextNode(""));
						cell.className = "h1_keywords_sw";
						cell.width = "79px";
					}
					else if (i == 1) {
						cell.appendChild(document.createTextNode(""));
						cell.className = "h1_totals";
					}
					else if (i == (tlds + 2)) {
						cell.appendChild(document.createTextNode(" "));
						cell.className = "h2_roi";
					}
					else if (i == (tlds + 3)) {
						cell = document.createElement('td');
						cell.appendChild(document.createTextNode("-"));
						cell.className = "h2_roi_under";
					}
					else {
						cell.appendChild(document.createTextNode("0"));
						cell.className = "h2_totals";
					}
					row.appendChild(cell);
				}
				tbody.appendChild(row);
				
				
				for (i = 0; i < (keywords + 1); i++) {
					row = document.createElement('tr');
					if (i == keywords) {
						for (c = 0; c < (tlds + 2); c++) {
							cell = document.createElement('td');
							if (c == 0) {
								cell.className = "h2_roi_key";
							}
							else if (c == 1) cell.className = "h2_totals_under_sw";
							else if (c == (tlds + 1)) cell.className = "h2_roi_under";
							else if (c == (tlds + 2)) {
								cell.className = "";
							}
							else cell.className = "h2_roi_under";
							row.appendChild(cell);
						}
					}
					else if (i == (keywords + 1)) {
						for (c = 0; c < (tlds + 1); c++) {
							cell = document.createElement('td');
							cell.appendChild(document.createTextNode("-"));
							if (c == 0) cell.className = "f_keywords";
							else if (c == (tlds)) cell.className = "f_roi";
							else cell.className = "f_cell";
							row.appendChild(cell);
						}
					}
					else {
						for (c = 0; c < (tlds + 3); c++) {
							cell = document.createElement('td');
							if (c == 0) {
								temp = document.createElement('a');
								temp.href = "javascript: pagina.tabel.sort(" + (i + 1) + ")";
								temp.appendChild(document.createTextNode(parseHtmlValue(query.keywords[i].value, false)));
								cell.appendChild(temp);
								cell.className = "h2_keywords_sw";
							}
							else if (c == 1) {
								cell.appendChild(document.createTextNode("0"));
								cell.className = "h3_totals";
							}
							else if (c == (tlds + 2)) {
								cell.appendChild(document.createTextNode(" "));
								cell.className = "h3_roi";
							}
							else if (c == (tlds + 3)) {
								cell = document.createElement('td');
								cell.appendChild(document.createTextNode("-"));
								cell.className = "h2_roi_under";
							}
							else {
								temp = document.createElement('a');
								temp.href = "lalala";
								temp.target = "_blank";
								temp.appendChild(document.createTextNode("-"));
								cell.appendChild(temp);
								cell.className = "cell";
							}
							row.appendChild(cell);
						}
					}
					tbody.appendChild(row);
				}	
			}
			
			document.getElementById("multiTabel").appendChild(table);
		}
	}
	
	/* Algemene data handling */
	this.reset = function() {
		this.clearResults();
	}
	
	this.appendResults = function(results) {
   		var i;
   		for (i = 0; i < results.length; i++) {
   			this.data[this.data.length] = results[i];
   		}
		this.data.sort(this.sortHits);
		this.allData.data = this.data;
	}
	
	this.clearResults = function() {
		this.data = Array();
	}
	this.sortHits = function(a, b) {return b.hits - a.hits;}
}


/* Pagina interactie */
function startSplit() {
	var i, key, keywords, tlds, keyword_input, keyword, domain_input, domain, time;
	
	keywords = Array();
	keyword_input = parseHtmlValue(document.getElementById("input_a").value, false);
	if (keyword_input != KEYWORD_DEFAULT) keywords = splitWoorden(keyword_input);
	
	/*tlds = Array();
	if (!ignoreDomains.status) {
    	domain_input = document.getElementById("input_b").value;
    	if (domain_input != DOMAIN_DEFAULT) tlds = splitWoorden(domain_input);
    	key = tlds.length;
    	for (i = 0; i < CheckBox.data.length; i++) {
    		if (CheckBox.data[i][1]) tlds[key++] = "." + CheckBox.data[i][0].tld;
    	}
	}*/
	
	tlds = Array();
	domain_input = document.getElementById("input_b").value;
	if (domain_input != DOMAIN_DEFAULT) tlds = splitWoorden(domain_input);
	key = tlds.length;
	for (i = 0; i < CheckBox.data.length; i++) {
		if (CheckBox.data[i][1]) tlds[key++] = CheckBox.data[i][0][1];
    }
	
	// FilterTime
	time = document.getElementById("how_recent").value;
	
	if ((keywords.length * tlds.length) > 250) {
		alert("Total searches per split is limited to 250, for now. Please reduce your total number of searches.");
	}
	//else if (keywords.length > 0 && ((tlds.length > 0) || ignoreDomains.status)) {
	else if (keywords.length > 0) {
		query.createMultiQuery(keywords, tlds, time);
		query.execute();
		pagina.tabel.draw();
		document.getElementById("page_multi_input").style.display = "none";
		document.getElementById("page_results").style.display = "block";
		pagina.splitStatus.update();
	}
	else {
		alert("enter a keyword before splitting");
	}
}


// Start url request split
function startUrlSplit(keywords, tlds, time) {
	query.createMultiQuery(keywords, tlds, time);
	query.execute();
	pagina.tabel.draw();
	document.getElementById("page_multi_input").style.display = "none";
	document.getElementById("page_results").style.display = "block";
}

/* Split the same query again */
function splitAgain(keywords, tlds, time) {
	cancelSplit();
	pagina.allData.clearHTML();
	pagina.clearResults();
	query.noCache = true;
	query.createMultiQuery(keywords, tlds, time);
	query.execute();
	pagina.tabel.draw();
	document.getElementById("page_multi_input").style.display = "none";
	document.getElementById("page_results").style.display = "block";
}


function cancelSplit() {
	query.cancelSplit();
}

function newSplit() {
	var buffer, i, selectCat, c, tlds, x;
	cancelSplit();
	pagina.allData.clearHTML();
	
	// Gegevens multisplit invullen
	buffer = "";
	for (i = 0; i < query.keywords.length; i++) {
		buffer += query.keywords[i].value + "\n";
	}
	document.getElementById("input_a").value = buffer;
	
	// Catagorieenlijst terughalen
	selectCat = "";
	tlds = Array();
	for (i = 0; i < query.tlds.length; i++) {
		tlds[i] = Array(query.tlds[i].value, false);
	}
	if (query.tlds.length) {
		for (i = 0; i < catData.length; i++) {
			if (query.tlds[0].value == catData[i][1]) {
				selectCat = catData[i][2];
				i = 999;
			}
		}
	}
	if (selectCat != "") {
		Cat.currentCat = selectCat;
		CheckBox.drawTlds();
		c = 0;
		for (i = 0; i < catData.length; i++) {
			if (Cat.currentCat == catData[i][2]) {
				for (x = 0; x < tlds.length; x++) {
					if (tlds[x][0] == catData[i][1]) {
						tlds[x][1] = true;
						CheckBox.on(c);
					}
				}
				c++;
			}
		}
	}
	
	buffer = "";
	for (x = 0; x < tlds.length; x++) {
		if (!tlds[x][1]) {
			buffer += tlds[x][0] + "\n";
		}
	}
	if (buffer != "") {
		document.getElementById("input_b").value = buffer;
	}
	
	queryNum();
	
	document.getElementById("page_multi_input").style.display = "block";
	document.getElementById("page_results").style.display = "none";
	pagina.clearResults();
}


/* Overige functies */

var CheckBox = new function() {
	this.data = Array();
	this.drawn = false;
	
	this.swap = function(id) {
		if (this.data[id][1]) {
			this.off(id);
		}
		else {
			this.on(id);
		}
		queryNum();
	}
	
	this.allOff = function() {
		var i;
		for (i = 0; i < this.data.length; i++) {
			this.off(i);
		}
		queryNum()
	}
	
	this.allOn = function() {
		var i;
		for (i = 0; i < this.data.length; i++) {
			this.on(i);
		}
		queryNum()
	}
	
	this.on = function(id) {
		//if (ignoreDomains.status) ignoreDomains.swap();
		this.data[id][1] = true;
		document.getElementById("checkbox_" + id).style.backgroundImage = "url(style/checkbox_on.png)";
	}
	
	this.off = function(id) {
		this.data[id][1] = false;
		document.getElementById("checkbox_" + id).style.backgroundImage = "url(style/checkbox_off.png)";
	}
	
	/*this.drawTlds = function() {
		var i, c, region, tld, buffer, temp, tlds;
		tlds = ".com,.net,.org,.edu,.gov,.uk,.jp,.de,.cu,.ru,.it,.nl,.fr,.us,.ca,.pl,.tw,.au,";
		buffer = "";
		temp = this.data;
		c = 0;
		for (i = 1; i < data.tlds.length; i++) {
			if (tlds.indexOf("." + data.tlds[i].tld + ",") != -1) {
				temp[c++] = Array(data.tlds[i], false);
			}
		}
		for (i = 0; i < temp.length; i++) {
			buffer += "<a href=\"javascript: CheckBox.swap(" + i + ")\" id=\"checkbox_" + i + "\" class=\"checkbox\"> ." + temp[i][0].tld.toUpperCase() + "</a>";
		}
		document.write(buffer);
	}*/
	
	this.drawTlds = function() {
		var i, c, region, tld, buffer, temp, tlds;
		
		catData = catData.sort(function (a, b) { return a[0] < b[0] ? -1 : 1; });
		
		c = 0;
		buffer = "";
		this.data = Array();
		for (i = 0; i < catData.length; i++) {
			if (Cat.currentCat == catData[i][2]) {
				this.data[c] = Array(catData[i], false);
				buffer += "<a href=\"javascript: CheckBox.swap(" + c + ")\" id=\"checkbox_" + c + "\" class=\"checkbox\">" + catData[i][0] + "</a>";
				c++;
			}
		}
		if (Cat.currentCat == '') {
			buffer = "<p>Search across the internet (1 search per keyword).</p>";
		}
		if (this.drawn == false) {
			document.write(buffer);
			this.drawn = true;
		}
		else {
			document.getElementById("scrolldiv_tlds").innerHTML = buffer;
		}
	}
}

var ignoreDomains = new function() {
	this.status = true;
	this.swap = function() {
		this.status = (this.status) ? false : true;
		//document.getElementById("ignore_domains").style.backgroundImage = (this.status) ? "url(style/checkbox_on.png)" : "url(style/checkbox_off.png)";
		if (this.status) {
			CheckBox.allOff();
			document.getElementById("input_b").value = DOMAIN_DEFAULT;
		}
		queryNum();
	}
}

function resetField(field) {
	if (field == 'which words') {
		document.getElementById("input_a").value = "";
	}
	else if (field == 'popular domains') {
		if (!ignoreDomains.status) ignoreDomains.swap();
		CheckBox.allOff();
	}
	else if (field == 'specific domains') {
		document.getElementById("input_b").value = "";
	}
	queryNum();
}

function queryNum() {
	var total, keywords, domains, i;
	
	if (document.getElementById("input_b").value != "" && document.getElementById("input_b").value != "e.g. '.com', 'wikipedia.org'...") {
		if (ignoreDomains.status) ignoreDomains.swap();
	}
	
	/* Keywords */
	if (document.getElementById("input_a").value != "enter keywords") {
		keywords = splitWoorden(document.getElementById("input_a").value).length;
	}
	else {
		keywords = 0;
	}
	document.getElementById("qn_keywords").firstChild.nodeValue = keywords;
	total = keywords;
	
	/* Domains */
	domains = 0;
	/*if (!ignoreDomains.status) {
    	if (document.getElementById("input_b").value != "" && document.getElementById("input_b").value != "e.g. '.com', 'wikipedia.org'...") {
    		domains = splitWoorden(document.getElementById("input_b").value).length;
    	}
    	else {
    		domains = 0;
    	}
    	for (i = 0; i < CheckBox.data.length; i++) {
    		if (CheckBox.data[i][1]) domains++;
    	}
       	total = total * domains;
	}*/
	if (document.getElementById("input_b").value != "" && document.getElementById("input_b").value != DOMAIN_DEFAULT) {
		domains = splitWoorden(document.getElementById("input_b").value).length;
    }
    else {
    	domains = 0;
    }
	for (i = 0; i < CheckBox.data.length; i++) {
    	if (CheckBox.data[i][1]) domains++;
    }
	if (domains) total = total * domains;
	
	document.getElementById("qn_domains").firstChild.nodeValue = domains;
	if (total > 250) {
		document.getElementById("max_queries").style.color = "red";
		document.getElementById("max_queries").style.fontWeight = "bold";
		document.getElementById("max_queries").firstChild.nodeValue = "Too many searches ";
		document.getElementById("querynum_end").style.color = "red";
	}
	else {
		document.getElementById("max_queries").style.color = "#5e5959";
		document.getElementById("max_queries").style.fontWeight = "normal";
		document.getElementById("max_queries").firstChild.nodeValue = "Maximum of 250 searches ";
		document.getElementById("querynum_end").style.color = "#ff007a";
	}
	document.getElementById("qn_total").firstChild.nodeValue = total;
}

function setExactWording() {
	var input, output, exact, i, word;
	input = document.getElementById("input_a").value;
	exact = document.getElementById("exact_wording").checked == true ? true : false;
	if (input != KEYWORD_DEFAULT) {
		input = input.split("\n");
		output = "";
		for (i = 0; i < input.length; i++) {
			word = input[i].replace(/[\n\r]/g, "");
			if (word.match(/[^"]+"[^"]+/g)) {
				output += word + "\n";
			}
			else {
				if (exact) {
					if (word != "") output += "\"" + word.replace(/["]/g, "") + "\"\n";
				}
				else {
					output += word.replace(/["]/g, "") + "\n";
				}
			}
		}
		document.getElementById("input_a").value = output;
	}
}

var Cat = new function() {
	this.currentCat = '';
	
	this.change = function() {
		this.currentCat = document.getElementById("cat_select").value;
		CheckBox.drawTlds();
		CheckBox.allOn();
	}
	
	this.drawSelect = function() {
		var i, temp, buffer;
		buffer = "";
		temp = "";
		for (i = 0; i < catData.length; i++) {
			if (temp != catData[i][2]) {
				buffer += '<option value="' + catData[i][2] + '">' + catData[i][2] + '</option>';
				temp = catData[i][2];
			}
		}
		document.write(buffer);
	}
	
	this.init = function() {
		var i;
		for (i = 0; i < catData.length; i++) {
			catData[i][0] = convertEntities(catData[i][0]);
		}
	}
}

Cat.init();
