function validateForm() {
	var errors = '';

	if (SB_Validator.isEmpty(document.getElementById('name').value) ) {
		errors += '- ' + SbGetLocaleByKey('EMPTY-NAME') + "\n";
	}

	var keys =  new Array('maxPages','maxRootPages','maxSites','maxUsers','maxHosts', 'trialLifeTime');
	for(var i = 0; i < keys.length; i++)
	{
		key       = keys[i];
		hiddenKey = 'hidden' + key;
		radioKey  = 'radio' + key;

		keyValue       = document.getElementById(key).value;
		hiddenKeyValue = document.getElementById(hiddenKey).value;

		if (key == 'trialLifeTime') {
			var ltChild  = document.forms['multiForm'].elements['trialLifeTypeId'].options[
				document.forms['multiForm'].elements['trialLifeTypeId'].selectedIndex
			].value;
			var ltParent = document.forms['multiForm'].elements['hiddentrialLifeTypeId'].value;
			if (ltChild != ltParent) {
				if (ltChild == 2) {
					hiddenKeyValue = hiddenKeyValue*24;
				}
				else {
					hiddenKeyValue = hiddenKeyValue/24;
				}
			}
		}
		
		if (document.forms['multiForm'].elements[radioKey][1].checked) {
			if (!SB_Validator.isInt(keyValue) ) {
				errors += '- ' + SbGetLocaleByKey('EMPTY-'+ key.toUpperCase()) + "\n";
			}
			else {
				if (parseInt(keyValue) < 0) {
					var msg = SbGetLocaleByKey('NEGATIVE-' + key.toUpperCase());
					errors += '- ' + msg + "\n";
				}
				else if (hiddenKeyValue >= 0) {
					if (parseInt(keyValue) > parseInt(hiddenKeyValue) || parseInt(keyValue) < 0) {
						var msg = SbGetLocaleByKey('INCORRECT-'+ key.toUpperCase());

						if (key == 'trialLifeTime') {
							msg = SbGetLocaleByKey('INCORRECT-'+ key.toUpperCase() + ltChild);
						}

						errors += '- ' + msg + ' {0,' + Math.round(hiddenKeyValue) + '}' + "\n";
					}
				}
			}
		}
	}

	// check that number of root pages is below of total number of pages	
	if (document.forms['multiForm'].elements['radio' + 'maxRootPages'][1].checked
		&& document.forms['multiForm'].elements['radio' + 'maxRootPages'][1].checked
	) {
		maxPages = document.getElementById('maxPages').value;		
		maxRootPages = document.getElementById('maxRootPages').value;
		
		if (parseInt(maxPages) < parseInt(maxRootPages)) {
			errors += '- ' + SbGetLocaleByKey('INCORRECT-MAXROOTPAGES'); + "\n";
		}
	}
	
	if (errors != '') {
		alert(errors);
		return false;
	}

	return true;
}

function submitForm() {
	if (!validateForm()) {
		return false;
	}
	
	document.forms.multiForm.action = baseUrl + '/Admin/Plan/Save';
	document.forms.multiForm.submit();
	return true;
}

function removeFromPlan(tab) {
	if (getCountCheckboxes(true, tab) == 0) {
		alert(SbGetLocaleByKey('PLEASE-SELECT-ITEMS'));
	} else {
		document.forms.multiForm.tab.value = tab;
		document.forms.multiForm.action = baseUrl + '/Admin/Plan/RemoveFromPlan';
		document.forms.multiForm.submit();
	}
}

function addToPlan(tab) {
	if (getCountCheckboxes(true, tab) == 0) {
		alert(SbGetLocaleByKey('PLEASE-SELECT-ITEMS'));
	} else {
		document.forms.multiForm.tab.value = tab;
		document.forms.multiForm.action = baseUrl + '/Admin/Plan/AddToPlan';
		document.forms.multiForm.submit();
	}
}

function setDefaultFamily(tab) {
	if (getCountCheckboxes(true, tab) == 0) {
		alert(SbGetLocaleByKey('PLEASE-SELECT-ITEMS'));
	} else {
		document.forms.multiForm.tab.value = tab;
		document.forms.multiForm.action = baseUrl + '/Admin/Plan/SetDefaultFamily';
		document.forms.multiForm.submit();
	}
}

function getCountCheckboxes(status, tab) {
	var result = 0;
	var form = document.forms.multiForm;
	for (var n = 0; n < form.elements.length; n++) {
		if (form.elements[n].type == 'checkbox'
			&& !form.elements[n].disabled
			&&  form.elements[n].name.substring(0, tab.length) == tab
			&&  form.elements[n].checked == status) {
			++result;
		}
	}
	return result;
}

function setCheckboxStatus(status, tab) {
	form = document.forms.multiForm;
	for (var n = 0; n < form.elements.length; n++) {
		if (form.elements[n].type == 'checkbox'
			&& !form.elements[n].disabled
			&& form.elements[n].name.substring(0, tab.length) == tab) {
			form.elements[n].checked = status;
		}
	}
}

function planToggle(object, tabLabel) {
	if (validateForm()) {
		if (!hasSupport()) return;
		if (eval(object)!='undefined') {
			eval(object+'.tooglePage(tabLabel.id);');
		}
	}
}