function addPostfixForArray(obj, postfix) {
	var result = new Object();
	for (var o in obj) {
		result[o] = obj[o] + postfix;
	}
	return result;
}

function replaceInArray(obj, pattern, replacement) {
	var result = new Object();
	for (var o in obj) {
		result[o] = obj[o].replace(pattern, replacement);
	}
	return result;
}

/**
 * Check if the string ends with postfix specified.
 */
function endsWith(string, postfix) {
	var sl = string.length;
	var pl = postfix.length;
	var result = false;
	if (sl >= pl) {
		result = (string.substr(sl-pl, pl).toLowerCase() == postfix.toLowerCase());
	}
	return result;
}

/**
 * Disables the element specified and clears its value.
 */
function clearAndDisableElement(element) {
	clearValue(element);
	disableElement(element);
}

function disableElement(element) {
	if (isInputElement(element) || isSelectElement(element)) element.disabled = true;
}
/**
 * Enables the element specified.
 */
function enableElement(element) {
	if (isInputElement(element) || isSelectElement(element)) {
		element.disabled = false;
	}
}

/**
 * Shows the element specified.
 */
function showElement(element) {
	enableElement(element);
	if (element.style != null) {
		if (element.type == 'hidden') {
			element.style.display = '';
		} else {
			element.style.display = 'block';
		}
	}
}

/**
 * Clears a value for the element specified.
 *
 * @param element Element to clear.
 */
function clearValue(element) {
	if (!element) return;
	if (isInputElement(element) && ((element.type == 'radio' || element.type == 'checkbox'))) {
			// Checkbox or radio form element - reset value
			element.checked = false;
	} else if (isSelectElement(element)) {
		element.selectedIndex = 0;
	} else if (element.value != undefined) {
		element.value = '';
	}
	}

/**
 * Hides the element specified and clears its value.
 */
function hideElement(element) {
	if (element) {
		clearAndDisableElement(element);
//		clearValue(element);
//		if (isInputElement(element) || isSelectElement(element)) element.disabled = true;
	// Hide element
	if (element.style != null) element.style.display = 'none';
}
}

/**
 *  Checks if the element is INPUT element.
 */
function isInputElement(element) {
	return isTag(element, 'INPUT');
}

/**
 *  Checks if the element is SELECT element.
 */
function isSelectElement(element) {
	return isTag(element, 'SELECT');
}

/**
 *  Checks if the element is DIV element.
 */
function isDivElement(element) {
	return isTag(element, 'DIV');
}

/**
 *  Checks if the element has tag name specified in tagName parameter.
 */
function isTag(element, tagName) {
	return (element != null && element.tagName != null && element.tagName == tagName);
}

/**
 * Checks if the element is not visible on page.
 */
function isInvisible(element)  {
	return (element.style != null && element.style.display == 'none');
}

/**
 * Checks if the element is in disabled state.
 */
function isDisabled(element) {
	return (element.disabled != null && element.disabled);
}

/**
 * Returns selected value of radio button.
 */
function getRadioValue(radioGroup) {
	if (radioGroup != null) {
		for (var i = 0; i < radioGroup.length; i++) {
			// Check all radio buttons in group
			if (radioGroup[i].checked) {
				return radioGroup[i].value;
			}
		}
	}
	return null;
}

/**
 * Checks if radioValue specified is selected in radio button group.
 */
function isRadioChosen(radioGroup, radioValue) {
	return ((radioGroup != null) && (getRadioValue(radioGroup) == radioValue));
}

// Trim whitespace from left and right sides of s.
function trim(s) {
	return (s && s.length > 0)? s.replace( /^\s*/, "" ).replace( /\s*$/, "" ): null;
}

/**
 * Opens new popup window with url specified in link.
 */
function printPopup(linkElement, width, height){
    var WINDOW_FEATURES = 'location=0, statusbar=0, menubar=0, scrollbars=1, resizable=1, fullscreen=0';
	if (width) WINDOW_FEATURES = WINDOW_FEATURES + ', width=' + width;
	if (height) WINDOW_FEATURES = WINDOW_FEATURES + ', height=' + height;

	var theWindow = window.open(linkElement.href, '_blank', WINDOW_FEATURES );
	theWindow.focus();
    return false;
}

/**
 * Clears element's value if it's not empty and contains at least one mask_char.
 */
function emptyValueIfMasked(element, mask_char) {
	if (element != null && element.value != null && element.value.indexOf(mask_char) > -1) {
		element.value = "";
	}
}

/**
 * Returns max day allowed for month and year specified.
 */
function getMaxAllowedDay(month, year) {
	var maxAllowedDay = 31;
	month -= 1;
	for (var iMonth = 13; iMonth > month; maxAllowedDay--) {
         // Javascript date behaviour. Please, don't change it.
		var date = new Date(year,month,maxAllowedDay);
		iMonth = date.getMonth();
	}
	maxAllowedDay++;
	return maxAllowedDay;
}

/**
 * Converts object fields of type 'string' to array.
 */
function objectToArray(obj) {
	var result = new Array();
	for (var i in obj) {
		if (typeof obj[i] == 'string') {
			result[result.length] = obj[i];
		}
	}
	return result;
}

function getBaseUrl() {
	return document.location.href.replace(/^((http|https)\:\/\/[^\/]+\/shop\/).*/g, '$1');
}

/**
 * Show arrow and write a message near postcode field
 */
function showMessage(fieldName, message) {
	// Show error for field with not valid value
	showArrow(fieldName);

	// Show error for field with non valid value
	showError(fieldName, message);
}

/**
 * Checks if the string specified not null and not empty.
 */
function isNotBlank(str) {
	return str != null && str.length > 0;
}

/**
 * Calculating vertical coordinate of element specified.
 */
function calculateOffsetTop(element) {
	var offset = 0;
	if (element.offsetParent) {
		offset = element.offsetTop;
		while (element = element.offsetParent) {
			offset += element.offsetTop;
		}
	}
	return offset;
}

/**
 * Jumps to the anchor with id specified.
 */
function jumpToAnchor(anchorId) {
	if (anchorId != null && anchorId.length > 0) {
		var anchor = getElement(anchorId);
		if (anchor != null) {
			var offset = calculateOffsetTop(anchor);
			window.scrollTo(0, offset);
		}
	}
}

function setElementText(id, text) {
	if (undefined != getElement(id)) {
		getElement(id).innerHTML = text;
	}
}

/**
 * Shows or hides an element with Id specified.
 */
function drawElement(id, isVisible) {
	var header = getElement(id);
	if (header) {
		if (isVisible) {
			showElement(header);
	    } else {
			hideElement(header);
	    }
	}
}

/**
 * Parses year in a string to the int representation.
 */
function parseYear(yearStr) {
	var year = parseInt(yearStr.replace(/^0+/, ''));
	if (year < 1000) {
		year += (year < 50)? 2000: 1900;
	}
	return year;
}

/**
 * Returns Date of today.
 */
function getToday() {
    var today = new Date();
    today.setDate(1);
    today.setHours(0, 0, 0, 0);
    return today;
}

function getElement(id) {
	return document.getElementById(id);
}

function setFieldValue(field, newValue) {
	if (field != null && newValue != null && field != undefined && newValue != undefined) {
		field.value = newValue;
	}
}

function getDefaultString(str) {
	return str != null && str != undefined? str: ''; 
}

function disableButton(id, disable) {
	enableDivButton(getElement(id), !disable);
}

function enableDivButton(buttonElement, enable) {
	if (buttonElement) {
		var className = buttonElement.className;
		if (enable) {
			className = className.replace(/ dis/g, '');
		} else {
			className = className + ' dis';
		}
		buttonElement.className = className;
	}
}

/**
 *  Corrects days range to avoid incorrect dates.
 */
function updateDayRange(dayDropdown, monthDropdown, yearDropdown) {
	var month = monthDropdown.value * 1;	// Cast to number format
	var year = yearDropdown.value * 1;
	if (month > 0 && year > 0) {
		var maxDay = getMaxAllowedDay(month, year);
		updateSelectMaxRange(dayDropdown, maxDay)
	}
}

/**
 *  Corrects range of option's values for select box specified.
 */
function updateSelectMaxRange(selectBox, maxValue) {
	if (selectBox.selectedIndex > (maxValue + 1)) {
		selectBox.selectedIndex = 0;	// Non compatible selection - reset list to default value.
	}
	while (selectBox.length > (maxValue + 1)) {
		selectBox.remove(selectBox.length - 1);
	}
	while (selectBox.length < (maxValue + 1)) {
		var newOption = document.createElement('option');
		newOption.text = selectBox.length;
		newOption.value = selectBox.length;
		try
    	{
    		selectBox.add(newOption, null); // standards compliant
    	} catch(ex) {
    		selectBox.add(newOption); // IE only
    	}
	}
}