var popupHTML;
var ext;
var extList = Array();
extList[0] = '.co.uk';
extList[1] = '.com';
extList[2] = '.net';
extList[3] = '.org';
extList[4] = '.org.uk';
extList[5] = '.eu';
var curExt = 0;
var domain;
var domainName;
function domainSearch() {
	curExt = 0;
	domain = document.getElementById('domain_check_input').value;
	domain = domain.replace(/ /gi, '');
	document.getElementById('domain_check_input').value = domain;
	var split = domain.split('.');
	domainName = split[0];
	var randomnumber=Math.floor(Math.random()*115454)
	if (domain!=''){
		$.get('domainSearchLayout.html?rn='+randomnumber, function(data) {
			popupHTML = data;
			popupHTML = popupHTML.replace('{fulldomain}', domain);
			showFBox();
			doDomainSearch();
		});
	}
}

function doDomainSearch() {
	var elementID;
	if (curExt < extList.length) {
		ext = extList[curExt];
		elementID = ext.replace(/\./gi, '');
		document.getElementById(elementID).innerHTML = '<div class="domainName">' + domainName + ext + '</div><div class="domainResult" id="' + elementID + 'result"><img src="images/loading.gif" alt="Loading.." /></div><br /><div class="whoisInfo" id="' + elementID + 'whois" style="display: none;"></div>';
		$.get("searchdomain.php?domain="+domainName+ext, function(data){
			showDomainSearch(data,elementID);
		});
		curExt++;
	} else {
		//when all domains are finished searching
	}
}
function showDomainSearch(data,extName) {
	xmlDoc = parseXML(data);
	var available = getXMLValue('available');
	var error = getXMLValue('error');
	var errorCode = parseInt(getXMLValue('errorcode'));
	if (errorCode == 0) {
		if (available == '1') {
			document.getElementById(extName+'result').innerHTML = 'Available!';
		} else {
			var whoisInfo = getXMLValue('whoisinfo');
			if (whoisInfo.length < 10)	{
				whoisInfo = 'This is some test info<br />THis is some more Information<br />THis is somemore <br />ajkshdjashdjkasjkd<br />';
			}
			document.getElementById(extName+'result').innerHTML = 'Taken! <a href="" onClick="toggleDiv(\''+extName+'whois\'); return false;">Information</a>';
			document.getElementById(extName+'whois').innerHTML = whoisInfo;
		}
		doDomainSearch();
	} else {
		document.getElementById(extName).innerHTML = '';
		document.getElementById('errorMessage').innerHTML = error;
	}
}

function showFBox() {
	$.facebox(popupHTML);
}

function toggleDiv(div) {
	$('#'+div).toggle('slow');
}

function checkKeyDown(e,method) {
	if(window.event) {
		keynum = e.keyCode;
	} else if(e.which) 	{
		keynum = e.which;
	}
	if (keynum==13) {
		method();
	}
	return true;
}
