var validatedFields = new Array();	// Array for errors showing
/**
 *  Shows section for error section.
 */
function drawErrorsList(isVisible, id) {
	var errors = getElement(id);
	if (errors != null) {
		var drawClass = 'warning';
		if (!isVisible) {
			drawClass = drawClass + ' hide';
		}
		errors.className = drawClass;
	}
}

/**
 *  Searches element of validatedFields array by its name.
 */
function getFieldEntry(fieldName) {
	var result = null;
	var id = getFieldEntryId(fieldName);
	if (id != null) {
		result = validatedFields[id];
	}
	return result;
}

/**
 *  Searches element's id of validatedFields array by its name.
 */
function getFieldEntryId(fieldName) {
	for (var f in validatedFields) {
		if (validatedFields[f][0] == fieldName) {
			return f*1;
		}
	}
	return null;	// element not found
}

/**
 *  Adds element to validatedFields array.
 */ PARAMETERS:
// 	fieldName - name of the form field;
// 	errorMessage - message will be shown for field.
//
function addFieldEntry(fieldName, errorMessage) {
	var fieldEntry = getFieldEntry(fieldName);	// search field in array
	if (fieldEntry == null) {
		// Field not found - add to array
		validatedFields[validatedFields.length] = new Array(fieldName, errorMessage);
	}
}

/**
 *  Removes a field entry if exists with the fieldName specified.
 */
function removeFieldEntry(fieldName) {
	var id = getFieldEntryId(fieldName);	// search field in array
	if (id != null) {
		// Shift an array elements
		for (var i = id; i < (validatedFields.length - 1); i++) {
			validatedFields[i] = validatedFields[i + 1];
		}
		validatedFields.length--;
	}
}

/**
 *  Shows/hides validation messages and errors for form fields.
 */
function showValidationErrorMessages() {
	var fieldName;
	for (var f in validatedFields) {
		// Validation failed for the field
		fieldName = validatedFields[f][0];	// Name of the validated field

		// Show error for field with non valid value
		showError(fieldName, validatedFields[f][1]);
		showArrow(fieldName);
	}
	validatedFields = new Array();		// Clear the fields array
}

/**
 *  Clears errors and arrows for form fields.
 */
function clearValidationErrorMessages(form) {
	var formElements = form.elements;
	for (var i = 0; i < formElements.length; i++) {
		if (isInputElement(formElements[i]) || isSelectElement(formElements[i])) {
			var name = formElements[i].name;
			if (name != null || name.length > 0) {
				hideError(name);
				hideArrow(name);
			}
		}
	}
}

/**
 *  Shows section for error section.
 */
function drawIndexedErrorsList(errorFieldTemplate, id, maxId) {
	for (var i = 0; i < maxId; i++) {
		var isVisible = errorFieldTemplate != null && getFieldEntry(getIndexedName(errorFieldTemplate, i)) != null;
		drawErrorsList(isVisible, getIndexedName(id, i));
	}
}

/**
 *  Replaces all occurences of '%id%' with id in array of templates specified.
 *  Also removes '[', ']' and '.' symbols.
 */
function getIndexedIds(templates, id) {
	
	
	
	var result = new Array();
	for (var i = 0; i < templates.length; i++) {
		result[i] = getIndexedName(cleanIndexedName(templates[i]), id);
	}
	
	return result;
}

/**
 *  Replaces all occurences of '%id%' with id in template specified.
 */
function getIndexedName(template, id) {

	return template.replace(/%id%/g, id);
}


/**
 *  Removes symbols [] and . from indexed field names.
 */
function cleanIndexedName(x) {
	var result = null;
	var name=x;	
	if (name != null) {
		result = name.replace(/[\[\]]/g, '').replace(/\./g, '-');	// remove [] and replace '.' with '-'
	}
	return result;
}

/**
 *  Returns element if found with Id specified.
 */
function getErrorElement(id) {
	if (id != null && id != '') {
		return getElement(cleanIndexedName(id) + 'Error');
	}
	return null;
}

/**
 *  Returns element if found with Id specified.
 */
function getArrowElement(id) {
	if (id != null && id != '') {
		return getElement(cleanIndexedName(id) + 'Arrow');
	}
	return null;
}

/**
 *  Shows error for field with name specified.
 */
function showError(fieldName, error) {
	// Showing <li> elements for payment details compartment
	var el = getErrorElement(fieldName);
	if (el != null) {
		if (el.className != null && el.className == 'hide-error') el.className = 'show-error';
		el.innerHTML = error;
	}
}

/**
 *  Hides error for field with name specified.
 */
function hideError(fieldName) {
	// Hiding <li> elements for payment details compartment
	var el = getErrorElement(fieldName);
	if (el != null) {
		if (el.className != null && el.className == 'show-error') el.className = 'hide-error';
		el.innerHTML = '';
	}
}

/**
 *  Shows arrow for field with name specified.
 */
function showArrow(fieldName) {
	setArrowDisplay(getArrowElement(fieldName), true);
}

/**
 *  Hides arrow for field with name specified.
 */
function hideArrow(fieldName) {
	setArrowDisplay(getArrowElement(fieldName), false);
}

function setArrowDisplay(element, isVisible) {
	if (element == null) return;
	if (isVisible) {
		element.className = arrowClassName;
	} else {
		element.className = '';
	}
}

//
// Shows/hides group of elements with ids specified based on isVisible flag.
function drawGroup(isVisible, ids) {
	for (var i in ids) {
		var elem = getElement(ids[i]);
		if (elem != null) {
			disableInnerInputElements(!isVisible, elem);
			if (isVisible) {
				showElement(elem);
			} else {
				hideElement(elem);
			}
		}
	}
}

function disableInnerInputElements(disable, elem) {
	if (elem == null) return;

	var children = elem.childNodes;
	for (var i = 0; i < children.length; i++) {
		if (isInputElement(children[i]) || isSelectElement(children[i])) {
			if (disable) {
				disableElement(children[i]);
				// Hide arrow and error message if present.
				hideArrow(children[i].name);
				hideError(children[i].name);
			} else {
				enableElement(children[i]);
			}
		} else {
			disableInnerInputElements(disable, children[i]);
		}
	}
}

function disableButton(id, disable) {
	var button = getElement(id);
	if (button) {
		var className = button.className;
		if (disable) {
			className = className + ' dis';
		} else {
			className = className.replace(/ dis/g, '');
		}
		button.className = className;
	}
}

function getFieldValue(field) {
	var value = null;
	if (field != null) {
		value = field.disabled? field.defaultValue: field.value;
	}
	return value;
}

function getFieldDefaultValue(field) {
	return getDefaultString(getFieldValue(field));
}
