var colors = new Array('#ff0000', '#00ff00', '#0000ff');
var curColor = 0;
var maxChecked = 50;

$(document).ready(function() {
	$('.selectAll,.deselectAll').click(handleSelectAllClick);
	initHandleTldClick();

	var domainNameField = document.getElementById('domainNameField');
	if (domainNameField) {
		wipeTextOnFocus(domainNameField, trStrings.lbl_your_domain);
	}

	$('a.toggleMultipleDomains').click(toggleMultipleDomains);
});

// Check or uncheck all selectboxes in a domainGroup
function handleSelectAllClick(e)
{
	e.preventDefault();

	// Get all checkboxes from the link parent 'domainGroup'.
	var inputElems	= $(this).parents('.domainGroup').find('ul.domainColumnList input:checkbox');

	var className	= this.className;
	var length		= inputElems.length;
	var checkedValue= (className.indexOf('deselectAll') > -1) ? false : true;

	for (var i = 0; i < length; i++) {

		var elem = inputElems[i];

		// Set or remove check
		$('input[name="' + $(elem).attr('name') + '"]').attr('checked', checkedValue);

	}

	// hide show select all / deselect all links
	$(this).parent().children('a').toggle();

	// After entire group is handled, update the numselected warning
	toggleNumTldWarning(getNumChecked() > maxChecked);
}

// Set up click events on tld checkboxes and their popular counterparts
// ie: .nl in Popular AND .nl in Europa
function initHandleTldClick()
{
	$('.domainGroup').each(
		function() {
			var domainGroup = $(this);
			if (domainGroup.find('input[type=checkbox]').filter(':checked').length
				== domainGroup.find('input[type=checkbox]').length
			) {
				domainGroup.find('.selectAll').hide();
			} else {
				domainGroup.find('.deselectAll').hide();
			}
		}
	);


	// Select all input elements that are NOT in the popular category
	$('.domainGroup input[type=checkbox]').click(
		function(e) {
			$('input[name="' + $(this).attr('name') + '"]').attr('checked', $(this).attr('checked'));
			toggleNumTldWarning(getNumChecked() > maxChecked);
		}
	);

	toggleNumTldWarning(getNumChecked() > maxChecked);
}

function getNumChecked()
{
	var names = {};
	var numChecked = 0;

	$('.domainGroup input[type=checkbox]').filter(':checked').each(
		function() {
			var name = $(this).attr('name');

			if (! (name in names)) {
				names[name] = 1;
				numChecked++;
			}

		}
	);

	return numChecked;
}

function wipeTextOnFocus(input, defaultValue) {

	input = $(input);
	var clone = input.clone();

	clone.attr('name', '');
	clone.attr('id', '');
	clone.attr('value', defaultValue);
	clone.css('display', 'none');
	clone.removeClass('roundedInput');
	clone.addClass('roundedInputDummy');

	var blur = function(e) {
		if (input.val() == '') {
			clone.css('display', '');
			input.css('display', 'none');
		}
	};
	blur();

	var focus = function(e) {
		clone.css('display', 'none');
		input.css('display', '');
		input.focus();
	};


   input.blur(blur);
   clone.focus(focus);

   clone.insertBefore(input);
}

function toggleMultipleDomains() {
	var domainEntryDiv = $(this).parents('#domainEntry');

	// Check to see what state we are in
	if ($(this).hasClass('single')) {

		$(this).text(trStrings.label_enter_one_domainname);
		$(this).toggleClass('multiple');
		$(this).toggleClass('single');

		domainEntryDiv.find('h2').text(trStrings.txt_add_domainnames);

		domainEntryDiv.find('.roundedInputMultiple').toggleClass('hidden');
		domainEntryDiv.find('.roundedInput').toggleClass('hidden');
		domainEntryDiv.find('.roundedInputDummy').toggleClass('hidden');

		$(this).closest('form').children('input:hidden[name=inputType]').val('multiple');
	} else {
		$(this).text(trStrings.label_insert_multiple_domains);
		domainEntryDiv.find('h2').text(trStrings.title_enter_domain_name);

		$(this).toggleClass('multiple');
		$(this).toggleClass('single');

		domainEntryDiv.find('.roundedInputMultiple').toggleClass('hidden');
		domainEntryDiv.find('.roundedInput').toggleClass('hidden');
		domainEntryDiv.find('.roundedInputDummy').toggleClass('hidden');

		$(this).closest('form').children('input:hidden[name=inputType]').val('single');
	}
}

/**
 * Function to enable / disable a warning for maximum allowed selected tld's.
 * The warning is shown in an error box where other errors may or may not be already displayed.
 *
 * When the error box is already present with an unrelated error, only the warning message itself
 * is affected, otherwise, the entire error box is toggled.
 */
function toggleNumTldWarning(show) {
	var errorUl = $('#domainEntry ul.errorMessages');

	if (show) {

		var warningText = trStrings['warning_max_selected_exceeded'];
		warningText = warningText.replace('&&1&&', maxChecked).replace('&&2&&', getNumChecked());

		if (! errorUl.length) {
			// errorUl doesn't exist and should be visible
			errorUl = document.createElement('ul');
			$(errorUl).attr('class', 'errorMessages');
			errorLi = document.createElement('li');
			$(errorLi).attr('id', 'numTldWarning');
			errorLi.appendChild(document.createTextNode(warningText));
			errorUl.appendChild(errorLi);

			$('span.domainPrefix').before(errorUl);
		} else {
			// errorUl exists and should be visible

			if ($('#numTldWarning').length == 0) {
				errorLi = document.createElement('li');
				$(errorLi).attr('id', 'numTldWarning');
				errorLi.appendChild(document.createTextNode(warningText));
				errorUl.append(errorLi);
			} else if (errorUl.children('li').length > 1) {
				// There are other errors, so only switch the relevant one
				$('#numTldWarning').text(warningText).css('display', 'block');
			} else {
				// No other errors, toggle the entire errorUl element
				$('#numTldWarning').text(warningText);
				errorUl.css('display', 'block');
			}
		}

	} else if (errorUl.length) {
		// errorUl exists and should be hidden
		if (errorUl.children('li').length > 1) {
			// There are other errors, so only switch the relevant one
			$('#numTldWarning').css('display', 'none');
		} else if ($('#numTldWarning').length == 1) {
			// No other errors, toggle the entire errorUl element
			errorUl.css('display', 'none');
		}
	} else {
		// errorUl doesn't exit and should be hidden. Do nothing!!
	}

	// Force parsing of DOM for IE6 (grumble, grumble, timewasters, grumble)
	$('div#domainEntryContainer').attr('class', '');

}
