var KEYWORD_DEFAULT = "enter keywords";
var DOMAIN_DEFAULT = "type domains, e.g. digg.com or .com (one per line)";

/* 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);
	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 + " keywords and " + query.tlds.length + " domains" + extra);
			
			setValue("ss_keywords", (query.keywords.length) + " KEYWORDS");
			setValue("ss_domains", (query.tlds.length) + " DOMAINS");
			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(data.keyword.value));
    			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.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.update = function() {
			var handle, data, i, c, total_tlds, total_keywords, max_tlds, max_keywords, max_total, max_tld, max_keyword, cell;
			
			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;
			}
			
			for (i = 0; i < pagina.data.length; i++) {
				data = pagina.data[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;
				}
			}
			
			handle = document.getElementById("multiTabel").firstChild.firstChild;
			/* Cellen */
			for (i = 0; i < pagina.data.length; i++) {
				data = pagina.data[i];
				cell = handle.childNodes[data.y + 2].childNodes[data.x + 2];
				if (cell.firstChild.firstChild) {
					cell.firstChild.firstChild.nodeValue = toBigInt(data.hits);
					cell.firstChild.href = "http://www.google.com/search?q=" + data.keyword.value + "&as_sitesearch=" + data.tld.value + (query.filterTime != "" ? "&as_qdr=" + query.filterTime : "");
				}
				else {
					cell.firstChild.nodeValue = toBigInt(data.hits);
				}
				cell.id = "";
				if (data.tld.value != "") cell.className = "cell";
			}
			
			/*
			for (i = 0; i < pagina.data.length; i++) {
				data = pagina.data[i];
				cell = handle.childNodes[data.x + 2].childNodes[data.y + 2];
				if (cell.firstChild.firstChild) {
					cell.firstChild.firstChild.nodeValue = toBigInt(data.hits);
					cell.firstChild.href = "http://www.google.com/search?q=" + data.keyword.value + "&as_sitesearch=" + data.tld.value + (query.filterTime != "" ? "&as_qdr=" + query.filterTime : "");
				}
				else {
					cell.firstChild.nodeValue = toBigInt(data.hits);
				}
				cell.id = "";
				if (data.tld.value != "") cell.className = "cell";
			}
			*/
			
			/* Totalen */
			for (i = 0; i < total_tlds.length; i++) {
				cell = handle.childNodes[i + 2].childNodes[1];
				cell.firstChild.nodeValue = toBigInt(total_tlds[i]);
				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 + 2];
				cell.firstChild.nodeValue = toBigInt(total_keywords[i]);
				cell.id = "";
				if (total_keywords[i] > total_keywords[max_keyword]) max_keyword = i;
			}
			
			/*
			for (i = 0; i < total_tlds.length; i++) {
				cell = handle.childNodes[1].childNodes[i + 2];
				cell.firstChild.nodeValue = toBigInt(total_tlds[i]);
				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[i + 2].childNodes[1];
				cell.firstChild.nodeValue = toBigInt(total_keywords[i]);
				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 + 2].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 + 2];
					cell.id = (cell.id == "") ? "max_hits_keyword" : "max_hits_both";
				}
			}
			data = max_total;
			if (data) handle.childNodes[data.y + 2].childNodes[data.x + 2].className += " max_hits";
			handle.childNodes[max_tld + 2].childNodes[1].id = "max_tld";
			handle.childNodes[1].childNodes[max_keyword + 2].id = "max_keyword";
			
			/*
			for (i = 0; i < (max_tlds.length - 1); i++) {
				data = max_tlds[i];
				if (data) handle.childNodes[data.x + 2].childNodes[data.y + 2].id = "max_hits_tld";
			}
			for (i = 0; i < max_keywords.length; i++) {
				data = max_keywords[i];
				if (data) {
					cell = handle.childNodes[data.x + 2].childNodes[data.y + 2];
					cell.id = (cell.id == "") ? "max_hits_keyword" : "max_hits_both";
				}
			}
			data = max_total;
			if (data) handle.childNodes[data.x + 2].childNodes[data.y + 2].className += " max_hits";
			handle.childNodes[1].childNodes[max_tld + 2].id = "max_tld";
			handle.childNodes[max_keyword + 2].childNodes[1].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";
			
			/* Header */
			row = document.createElement('tr');
			for (i = 0; i < (keywords + 2); i++) {
				cell = document.createElement('td');
				if (i == 0) {
					cell.colSpan = "2";
					cell.appendChild(document.createTextNode(""));
					cell.className = "h1_domains";
				}
				else if (i == (keywords + 1)) {
					cell.appendChild(document.createTextNode(" "));
					cell.className = "h1_roi";
				}
				else {
					cell.appendChild(document.createTextNode(query.keywords[i-1].value));
					cell.className = "h2_domains";
				}
				row.appendChild(cell);
			}
			tbody.appendChild(row);
			
			
			
			row = document.createElement('tr');
			for (i = 0; i < (keywords + 3); 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_totals";
				}
				else if (i == (keywords + 2)) {
					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 + 3); 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 == 1) cell.className = "h2_totals_under";
						else if (c == (keywords + 1)) cell.className = "h2_roi_under";
						else if (c == (keywords + 2)) {
							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 + 3); c++) {
						cell = document.createElement('td');
						if (c == 0) {
							cell.appendChild(document.createTextNode(query.tlds[i].value));
							cell.className = "h2_keywords";
						}
						else if (c == 1) {
							cell.appendChild(document.createTextNode("0"));
							cell.className = "h3_totals";
						}
						else if (c == (keywords + 2)) {
							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);
			}	
				
			/* Header */
			/*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_domains";
				}
				else if (i == (tlds + 1)) {
					cell.appendChild(document.createTextNode("THE REST OF THE INTERNET"));
					cell.className = "h1_roi";
				}
				else {
					cell.appendChild(document.createTextNode(query.tlds[i-1].value));
					cell.className = "h2_domains";
				}
				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";
					cell.width = "79px";
				}
				else if (i == 1) {
					cell.appendChild(document.createTextNode(""));
					cell.className = "h1_totals";
				}
				else if (i == (tlds + 2)) {
					cell.appendChild(document.createTextNode("0"));
					cell.className = "h2_roi";
				}
				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 + 3); 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 == (tlds + 2)) 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) {
							cell.appendChild(document.createTextNode(query.keywords[i].value));
							cell.className = "h2_keywords";
						}
						else if (c == 1) {
							cell.appendChild(document.createTextNode("0"));
							cell.className = "h3_totals";
						}
						else if (c == (tlds + 2)) {
							cell.appendChild(document.createTextNode("0"));
							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);
			}
			*/
			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 = document.getElementById("input_a").value;
	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 = "";
	for (i = 1; i <= 5; i++) {
		if (document.getElementById("how_recent_" + i).checked) time = document.getElementById("how_recent_" + i).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";
	}
	else {
		alert("enter a keyword before splitting");
	}
}

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].toUpperCase() + "</a>";
				c++;
			}
		}
		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 != "enter keywords") {
		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 = catData[0][2];
	
	this.change = function() {
		this.currentCat = document.getElementById("cat_select").value;
		CheckBox.drawTlds();
	}
	
	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();