//////////
// specific functions for domains check form and domains step 
//
// $Id: domains.js,v 1.5 2008/04/14 06:33:54 amark Exp $
//////////

////
// Enable / disable domain / subdomain selects and inputs (for hostname)  
function click_hostname(object, field, type){
	if(typeof(object) != 'object') {
		return;
	}
	if(typeof(field) != 'object') {
		return;
	}
	if(typeof(field[type]) == 'object') {
		field[type].checked=true;
	}
	if(type == -1) {
		disable_field(object.domain_hostname, 1);
		disable_field(object.subdomain, 1);
		disable_field(object.subdomain_hostname, 1);
	} else {
		disable_field(object.domain_hostname, type ? field.checked : !field.checked);
		disable_field(object.subdomain, type ? !field.checked : field.checked);
		disable_field(object.subdomain_hostname, type ? !field.checked : field.checked);
	}
}

////
// Switch between multi and single domains selection
function multi_domain() {
	document.getElementById('single_domain').style.display = 'none';
	document.getElementById('multi_domain').style.display = '';
	document.getElementById('domain_selection_type').value = 'multi';
	document.getElementById('domain_name').disabled = true;
	document.getElementById('tld').disabled = true;
}

function single_domain() {
	document.getElementById('single_domain').style.display = '';
	document.getElementById('multi_domain').style.display = 'none';
	document.getElementById('domain_selection_type').value = 'single';
	document.getElementById('domain_name').disabled = false;
	document.getElementById('tld').disabled = false;
}

////
// Skip domains selection
function skip_domains() {
		document.getElementById('action').value = 'skip_domains';
		document.domain_selection_form.submit();
}

////
// Check if domain name(s) entered, submit the form
function check_domains(form) {
	var error = "";

	if (typeof(form.dm_action) != 'object') {
		document.getElementById('action').value = 'check_domains';
		form.submit();
	}

	var dm_action;
	if(form.dm_action.length == undefined) {
		dm_action = document.getElementById('dm_action').value;
	} else {
		for(i = 0; i < form.dm_action.length; i++){
			//determine, which radiobutton checked now
			if(form.dm_action[i].checked == true){
				dm_action = form.dm_action[i].value;
				break;
			}
		}
	}

	if(
		dm_action == 'register_new' ||
		dm_action == 'reg_transfer' ||
		dm_action == 'domain_pointer'
	) {
		var www_re = new RegExp("^www\\.", "i");
		if ((document.getElementById('domain_selection_type').value == 'single') && (!document.getElementById('domain_name').value.length)) {
			error = error + string_STR_PLEASE_SPECIFY_DOMAIN_NAME + "\n";
		}
		if ((document.getElementById('domain_selection_type').value == 'single') && www_re.test(document.getElementById('domain_name').value)) {
			error = error + string_STR_WWW_EXISTS_IN_DOMAIN_NAME + "\n";
		}
		if ((document.getElementById('domain_selection_type').value == 'multi') && (!document.getElementById('domain_names').value.length)) {
			error = error + string_STR_PLEASE_SPECIFY_DOMAIN_NAMES + "\n";
		}
		if(document.getElementById('domain_selection_type').value == 'multi') {
			var pattern = /\s+/;
			var str = document.getElementById('domain_names').value;
			result = str.split(pattern);
			for(i = 0; i < result.length; i++) {
				if(www_re.test(result[i])) {
					error = error + string_STR_WWW_EXISTS_IN_DOMAIN_NAME + "\n";
					break;
				}
			}
		}
	} else if(
		dm_action == 'use_subdomain'
	) {
		var www_re = new RegExp("^www\\.", "i");
		var www_re2 = new RegExp("^www$", "i");
		if(typeof(document.getElementById('subdomain')) == 'object' && !document.getElementById('subdomain').value) {
			error = error + string_STR_PLEASE_SPECIFY_SUBDOMAIN_NAME + "\n";
		}
		if(typeof(document.getElementById('subdomain')) == 'object' && www_re.test(document.getElementById('subdomain').value)) {
			error = error + string_STR_WWW_EXISTS_IN_DOMAIN_NAME + "\n";
		}
		if(typeof(document.getElementById('subdomain')) == 'object' && www_re2.test(document.getElementById('subdomain').value)) {
			error = error + string_STR_WWW_EXISTS_IN_DOMAIN_NAME + "\n";
		}
		if(typeof(document.getElementById('subdomain')) == 'object' && !document.getElementById('subdomain_hostname').value) {
			error = error + string_STR_PLEASE_SPECIFY_SUBDOMAIN_HOSTNAME + "\n";
		}
	} else if(
		dm_action == 'use_domain'
	) {
		if(typeof(document.getElementById('domain_hostname')) == 'object' && !document.getElementById('domain_hostname').value) {
			error = error + string_STR_PLEASE_SPECIFY_DOMAIN_NAME + "\n";
		}
	}
	
	if (error) {
		alert(error);
		return false;
	} else {
		document.getElementById('action').value = 'check_domains';
		form.submit();
	}
}

////
// Order Selected domains
function order_domains() {
	var error = "";
	if (error) {
		alert(error);
		return false;
	} else {
		document.getElementById('action').value = 'order_domains';
		document.domain_selection_form.submit();
	}
}

////
// Switch between register / transfer / use existing actions
function change_dm_action(dm_action) {
	if (dm_action == 'register_new') {
		document.getElementById('tld').style.display = '';
		document.getElementById('dot').style.display = '';
		document.getElementById('multi_domain_link').style.display = '';
		document.getElementById('nsset').style.display = 'none';
	}
	if (dm_action == 'reg_transfer') {
		single_domain();
		document.getElementById('tld').style.display = 'none';
		document.getElementById('dot').style.display = 'none';
		document.getElementById('multi_domain_link').style.display = '';
		document.getElementById('nsset').style.display = 'none';
	}
	if (dm_action == 'domain_pointer') {
		single_domain();
		document.getElementById('tld').style.display = 'none';
		document.getElementById('dot').style.display = 'none';
		document.getElementById('multi_domain_link').style.display = 'none';
		document.getElementById('nsset').style.display = '';
	}
	disable_field(document.getElementById('domain_selection_form').subdomain, true);
	disable_field(document.getElementById('domain_selection_form').subdomain_hostname, true);
	disable_field(document.getElementById('domain_selection_form').domain_hostname, true);
}

////
// Add domains from suggestions list (see includes/domain_suggestions.ini for details)
function add_suggested_domains() {
	var error = "";
	if (error) {
		alert(error);
		return false;
	} else {
		document.getElementById('action').value = 'add_suggested_domains';
		document.domain_selection_form.submit();
	}
}
