/**
 * Handler for the Domain Name Availability
 *
 */
 
function TidyInput(form)
{

	form.domain.value = domainHasSuffixForLanding(form.domain.value,0);

	if (getElemById('loaderContainer'))
	{
		getElemById('loaderContainer').style.visibility = 'visible';
	}
	
	return true;
	
}

// Prints out the country code

function PrintSuffixData(idx)
{
	
	var suffixes = ctries[idx];
	var output = '';
	
	for (i = 0; i < suffixes.length; i++)
	{
		output += "<acronym onClick='return addSuffix(\"" + suffixes[i] + "\",0);' title='Click to add " + suffixes[i] + " to your search list'>" + suffixes[i] + "</acronym><br />" + '\n';
	}

	document.write(output);
	
}

// Prints out the country data
function PrintRegionData(idx)
{
	var countries = region[idx];
	var output = '';
	
	for (i = 0; i < countries.length; i++)
	{
		output += "<acronym onClick='return addCountry(region[" + idx + "][" + i + "],0);' title='Click to add " + countrynames[idx][i] + " to your search list'>" + countrynames[idx][i] + "</acronym><br />" + '\n';
	}

	document.write(output);
}
 
// Checks that the domain entered is valid, and that there is a suffix selected
function DomainNameFormChecker(form) {
	
	var domain = document.forms[form].domain.value;
	var selDoms = document.forms[form].selDoms;
	var suffixes = "";

	// did they write a domain in there?
	domain = domainHasSuffixV(domain,form);

	if (!checkDomain(domain)) {
		document.forms[form].domain.focus();
		return false;
	}
	
	if (selDoms[0].value == "*") {
		alert("You must select at least one domain to search.");
		return false;
	}
	
	if (getElemById('loaderContainer'))
	{
		getElemById('loaderContainer').style.visibility = 'visible';
	}
	
	for (var i = 0; i < selDoms.length; i++) {
		if (suffixes == "") suffixes += selDoms[i].value; 
		else suffixes += "|" + selDoms[i].value;
	}
	
	document.forms[form].suffixes.value = suffixes;
		
	document.forms[form].domain.value = domain.toLowerCase();
	
	return true;
	
}
 
 
// Checks that the domain entered is valid, alters the selected domains accordingly
// in both the international form, and the common domains
function DomainsNameChecker(form) {
	
	var domain = document.forms[form].domain.value;
	if (document.InternationalDomains)
	{
		var selDoms = document.InternationalDomains.selDoms;
	}
	if (document.CommonDomains)
	{
		var selCoDoms = document.CommonDomains.selDoms;
	}
	var otherform = (form == 0) ? 1 : 0;

	// did they write a domain in there?
	domain = domainHasSuffixV(domain,form);
	
	
	if (!checkDomain(domain)) {
		document.forms[form].domain.focus();
		return false;
	}
	
	else {
		
		if (document.forms[otherform])
		{
			document.forms[otherform].domain.value = domain;
		}
		
		if (selCoDoms)
		{
			if (selCoDoms[0].value == "*" && getElemById('localdomains'))
			{
				selCoDoms[0] = new Option(domain + '.com.au','com.au');
				selCoDoms[1] = new Option(domain + ".net.au",'net.au');
				if (selDoms)
				{
					selDoms[0] = new Option(domain + '.com.au','com.au');
					selDoms[1] = new Option(domain + ".net.au",'net.au');				
				}
			}
			else if (selCoDoms[0].value == "*" && getElemById('international')) 
			{
				selCoDoms[0] = new Option(domain + '.com','com');
			}
			else {
				for (i=0; i < selCoDoms.length; i++) {
					selCoDoms[i] = new Option(domain + '.' + selCoDoms[i].value,selCoDoms[i].value);
				}			
			}
		}
		if (selDoms)
		{
			if (selDoms[0].value == "*") {
				selDoms[0] = new Option(domain + '.com','com');
				selDoms[1] = new Option(domain + ".net",'net');
				selDoms[2] = new Option(domain + ".org",'org');
			} 
			else {
				for (i=0; i < selDoms.length; i++) {
					selDoms[i] = new Option(domain + '.' + selDoms[i].value,selDoms[i].value);
				}
			}
		}
	}
	
  return true;		
	
}

// Removes any selected domains, or a domain of a specific type
function removeSelDomains(form,type) {

	var selDoms = document.forms[form].selDoms;
	
	// copy common domains over to international search
	if (form == 0 && document.forms[1]) {
		removeSelDomains('1',type);
	}	
	
	for(i = (selDoms.options.length-1); i >= 0; i--) {
		if (selDoms.options[i] != null) {		
			if (type == "all") {
				selDoms.options[i] = null;
			}
			else if (type && (selDoms.options[i].value.substr(0,type.length) == type)) {
				selDoms.options[i] = null;			
			}
			else if (selDoms.options[i].selected == true) {
				selDoms.options[i] = null;						
			}
		}
	}
	
	// Reset list
	if (selDoms.options[0] == null) {
			selDoms.options[0] = new Option('None selected','*');
	}
	
	return true;
	
}

// Test if a domain name contains a suffix they have already typed!
function domainHasSuffix(domain,form) {
	
	var ectry;
	var start;
		
	if (domain.indexOf(".") > -1) {
		for (var i=0; i < ctry.length; i++) {
			isSuffix = thisSuffixIsInList(domain,ctry[i]);
			if (isSuffix > -1) {
				domain = domain.replace(('.' + ctry[i][isSuffix]),"");
				if (document.InternationalDomains)
				{
					document.InternationalDomains.domain.value = domain;
				}
				if (document.CommonDomains)
				{
					document.CommonDomains.domain.value = domain;				
				}
				addSuffix(ctry[i][isSuffix],form);
				return domain;
			}
		}
	}

	return domain;
	
}

// Test if a domain name contains a suffix they have already typed! (v2)
function domainHasSuffixV(domain,form) {
	
	if (domain.indexOf(".") > -1)
	{
			isSuffix = thisSuffixIsInList(domain,allsuffs);
			if (isSuffix > -1)
			{
				suf = allsuffs[isSuffix];
				domain = domain.replace('.' + suf,"");
				document.CommonDomains.domain.value = domain;	
				addSuffix(suf,form);
				return domain;
			}
	}

	return domain;
	
}

// Test if a domain name contains a suffix they have already typed!
function domainHasSuffixForLanding(domain,form) {
	
	if (domain.indexOf(".") > -1) {
		isSuffix = thisSuffixIsInList(domain,allvalidsuffixes);
		if (isSuffix > -1) {
			domain = domain.replace(('.' + allvalidsuffixes[isSuffix]),"");
			document.forms[form].domain.value = domain;
			document.forms[form].suffixes.value = allvalidsuffixes[isSuffix];
			return domain;
		}		
	}

	return domain;
	
}



function thisSuffixIsInList(needle,haystack) {
	
	for (var w = 0; w < haystack.length; w++) {
		len = haystack[w].length + 1;
		if (('.' + haystack[w]) ==  needle.substr((len * -1),len)) {
				return w;
		}
		
	}
	
	return -1;
		
}



// Test if a domain suffix is already in the list
function notAlreadyInList(cctld,form) {
	
	var selDoms = document.forms[form].selDoms;	
	for (var n = 0; n < selDoms.options.length; n++) {
		if (cctld == selDoms.options[n].value) {
			return false;
		}
	}
	
	return true;
	
}

function addSuffix(suf,form) {

	var selDoms = document.forms[form].selDoms;	
	var domain = document.forms[form].domain.value;
	
	// do we have a domain?
	if (!checkDomain(domain)) {
		document.forms[form].domain.focus();
		return false;
	}		
	
	// remove the default
	if (selDoms[0].value == "*") {	
		selDoms.options[0] = null;
	}	
	if (notAlreadyInList(suf,form)) {
		selDoms[selDoms.length] = new Option(domain + "." + suf,suf);	
	}
	// copy common domains over to international search
	if (form == 0 && document.forms[1]) {
		addSuffix(suf,'1');
	}
}

// Add a single country to our selected domains list
function addCountry(country,form) {
	
	var selDoms = document.forms[form].selDoms;	
	var domain = document.forms[form].domain.value;

	// do we have a domain?
	if (!checkDomain(domain)) {
		document.forms[form].domain.focus();
		return false;
	}

	// add each domain that isn't already in the list
	for (x=0; x < country.length; x++) {
		addSuffix(country[x],form);
	}

	return true;
	
}

// Add an entire region to our list!
function addRegion(regid,form) {
	
	var domain = document.InternationalDomains.domain.value;	
	
	// check we have a domain
	if (!checkDomain(domain)) {
		document.InternationalDomains.domain.focus();
		return false;
	}	
	
	// scroll through each country in the region, add it
	for (var ctryid=0; ctryid < region[regid].length; ctryid++) {
		addCountry(region[regid][ctryid],form);
	}
	
	return true;
}

// Rollover script (wow, complicated eh..)
function highlight(area) {
	
	document.images['globe'].src = imgLocs[area].src;
	
	return true;
	
}

// Handler for various remove functions
function RemoveType(obj)
{
	type = obj.value;
	obj.selectedIndex = 0;
	if (type == 'selected')
	{
		return removeSelDomains(0);
	}
	else 
	{
		return removeSelDomains(0,type);		
	}
}

	
	

