var heads;
var cells;
var funcs;

$(
	function () {
		$('#tlds input').attr({checked: false});
		$('#tld_com').attr({checked: true});
		$('#tld_ca').attr({checked: true});		
		$('#tlds input').attr({disabled: false});
	}
);

function get_address_error(keyword) {
	var patt1 = /^-|-$/;
	var patt2 = /^[\-a-z\d]+$/i;
	if (keyword.length < 1 || keyword == 'Please enter keyword here') {
		return (keyword == '' || keyword == 'Please enter keyword here') ? 'Please enter keyword.'	: 'Domain name length too small.';
	} else if (patt1.test(keyword)) {
		return 'You should use the latin letters and numbers only.';
	} else if (!patt2.test(keyword)) {
		return 'Please use the latin letters and numbers only.';
	}
	return '';
}

function get_tlds_error(tlds) {
	var count = 0;
	tlds.each(
		function () {
			if (this.checked) count++;
		}
	);
	if (count == 0) return 'Please select TLDs.';
	return '';
}

function tloggetlds() {
	$('#gen').show();
	$('#more').hide();
	if ($('#tlds').css('display') == 'none') {
		$('#tlds').show();
	} else {
		$('#tlds').hide();
	}
}

function checkonltld(checkbox) {
	var count = 0;
	$('#tlds input[type=checkbox]').each(function(){
		if (this.checked) count++;
	});
	if (count == 5) {
		$('#tlds input[type=checkbox]').each(function(){
			if (!this.checked) this.disabled = true;
		});
	} else {
		$('#tlds input').attr({disabled: false});
	}
	$('#tldscount').html('<div style="font-size:17px;">You selected:<b> '+count+'</b> (5 max)</div><br>');
	return true;
}

function runwhois(domainname, tld, x, y) {
	$(cells[x][y]).html('<img src="suggestions/img/loading.gif" alt="loading..."/>');	
	funcs[x][y] = $.ajax(
		{
			url: base_url+'ajax.php',
			data : (
				{
					domain : domainname,
					'tlds[]' : tld,
					foo : new Date().getTime()
				}
			),
			success : function (data) {
				$(cells[x][y]).html(data);
			}
		}
	);
}

function generate(){
    $('#tlds').hide();			
	var keyword = $('#keyword').attr('value');
	var category = $('#category').attr('value');
	var error = get_address_error(keyword);
	if (error == '')	
	error = get_tlds_error($('#tlds input[type=checkbox]'));	
	if (error){
		alert(error);
	} 
	else {
		$('#gen').hide();				
		breakrequests();
		$.get(
			base_url+'ajax.php',
			{
				keyword : keyword,
				category : category,
				foo : new Date().getTime()
			},
			function (data) {
				var words = new Array();
				$('word',data).each(function(){
					words[words.length] = $(this).text();
				});
				var tlds = new Array();
				$('#tlds input[type=checkbox]').each(function(){
					if (this.checked) {
						var patt = /^tld_(.*)$/;
						tlds[tlds.length] = patt.exec($(this).attr('id'))[1];
					}
				});
				if (!heads) {
					heads = new Array();
				} else {
					for (var i in heads) $(heads[i]).remove();					
				}
				heads = new Array();
				for (var i in tlds) {
					heads[i] = $('<td style="font-weight:bold; text-align:center;"></td>');
					$(heads[i]).html('.'+tlds[i]);
					$('#result thead tr').append(heads[i]);
				}
				$('#result tbody').empty();
				cells = new Array();
				funcs = new Array();
				for (var i=0; i<words.length; i++) {
					cells[i] = new Array();
					funcs[i] = new Array();
					var tr = $('<tr><td></td></tr>');
					$('td',tr).html(words[i]);
					for (var j=0; j<tlds.length; j++) {
						var td = $('<td>&nbsp;</td>');
						tr.append(td);
						cells[i][j] = td;
						setTimeout('runwhois(\''+$('<div>'+words[i]+'</div>').text()+'\',\''+tlds[j]+'\','+i+','+j+')',1);
					}
					$('#result tbody').append(tr);
				}
				$('#more').show();
				$('#result').show();						
			}
		);
	}
}

function breakrequests() {
	if (funcs) {
		for (var i in funcs) {
			for (var j in funcs[i]) {
				if (window.XMLHttpRequest) {
					funcs[i][j].abort();
				}
			}
		}
	}
}

function clickclear(thisfield, defaulttext){
	clearMore();
	if (thisfield.value == defaulttext) thisfield.value = "";
}

function clearMore()
{
	$('#gen').show();
	$('#more').hide();
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") thisfield.value = defaulttext;
}
