

function openLink(s_linkAddress)
{
	top.location.href = s_linkAddress;
}


function focusField(s_fieldName)
{
	var o_fieldFocus = $(s_fieldName);
	o_fieldFocus.focus();
}


function submitForm(s_formName)
{
	var o_submitForm = $(s_formName);
	o_submitForm.submit();
}


function isWhiteSpace(c_textChar)
{
	return (c_textChar == ' ' || c_textChar == '\r' || c_textChar == '\n' || c_textChar == "\t");
}


function formatTextAreas()
{
	var o_textAreas = document.getElementsByTagName('textarea');
	
	if (o_textAreas != null && o_textAreas.length > 0)
	{
		for (var i_loopIndex = 0; i_loopIndex < o_textAreas.length; ++i_loopIndex)
		{
			var s_textString = o_textAreas[i_loopIndex].value;
			
			while (s_textString.length > 0 && isWhiteSpace(s_textString.charAt(0)))
			{
				s_textString = s_textString.substring(1);
			}
			while (s_textString.length > 0 && isWhiteSpace(s_textString.charAt(s_textString.length - 1)))
			{
				s_textString = s_textString.substring(0, s_textString.length - 2);
			}

			o_textAreas[i_loopIndex].value = s_textString;
		}
	}
}


function attachDomEvent(o_elementReference, s_eventName, o_functionPointer)
{
	var s_qualifiedEventName = 'on' + s_eventName;

	if (o_elementReference.addEventListener)
	{
		o_elementReference.addEventListener(s_eventName, o_functionPointer, false);
		o_elementReference.addEventListener(s_eventName, o_functionPointer, false);
	}
	else if (o_elementReference.attachDomEvent) 
	{
		o_elementReference.attachDomEvent(s_qualifiedEventName, o_functionPointer);
		o_elementReference.attachDomEvent(s_qualifiedEventName, o_functionPointer);
	}
	else
	{
		o_elementReference[s_qualifiedEventName] = o_functionPointer;
	}
}


function submitValue(s_formId, s_fieldName, s_fieldValue)
{
	/* obtain reference to form and array of elements */
	
	var o_formNode = document.getElementById(s_formId);
	var o_formElements = (o_formNode == null) ? null : o_formNode.getElementsByTagName('input'); 
	
	/* cycle elements, match name and set value  */

	if (o_formElements != null && s_fieldName != null && s_fieldValue != null)
	{
		for (var i_index = 0; i_index < o_formElements.length; ++i_index)
		{
			if (o_formElements[i_index].name == s_fieldName)
			{
				o_formElements[i_index].value = s_fieldValue;
			}
		}
	}
	
	/* submit form */
	
	if (o_formNode != null)
	{
		document.getElementById(s_formId).submit();
	}
}


function loadScroll()
{
	window.onscroll = scrollFrames;
	attachDomEvent(window, 'scroll', scrollFrames);
}


function scrollFrames()
{
	var o_frameTopRight = top.document.getElementById('frameTopRight').contentWindow;
	var o_frameBottomLeft = top.document.getElementById('frameBottomLeft').contentWindow;
	var o_frameBottomRight = top.document.getElementById('frameBottomRight').contentWindow;

	var o_outerNode = (o_frameBottomRight.document.documentElement != null) ? o_frameBottomRight.document.documentElement : o_frameBottomRight.document.body; 

	var i_offsetLeft = o_outerNode.scrollLeft;
	var i_offsetTop = o_outerNode.scrollTop;

	o_frameTopRight.scrollTo(i_offsetLeft, 0);
	o_frameBottomLeft.scrollTo(0, i_offsetTop);
}


function clickedQuestion(i_questionIndex)
{
	var o_listAnswers = document.getElementsByTagName('dd');
	var o_activeAnswer = $('itemAnswer' + i_questionIndex);
	
	if (o_activeAnswer.className == '')
	{
		o_activeAnswer.className = 'itemHidden';
	}
	else
	{
		for (var i_index = 0; i_index < o_listAnswers.length; ++i_index)
		{
			o_listAnswers[i_index].className = 'itemHidden';
		}

		o_activeAnswer.className = '';
	}
}


function clickedContentVisiblity(s_hiddenField, o_checkboxItem)
{
	$(s_hiddenField).value = o_checkboxItem.checked;
}


function clickedAdmin(b_isAdmin, s_languageCode) 
{
	var o_formAdmin = $('formAdmin');
	o_formAdmin.editlang.value = b_isAdmin;
	o_formAdmin.language.value = s_languageCode;
	o_formAdmin.submit();
}


function clickedChapter(i_chapterIndex)
{
	swapText(i_chapterIndex);
	swapVideo(i_chapterIndex);
	swapChapter(i_chapterIndex);
}


function swapChapter(i_chapterIndex)
{
	var o_divSchoolChapters = $('divSchoolChapters');
	var o_chapterLinks = o_divSchoolChapters.getElementsByTagName('a');
	
	for (var i_chapterIterator = 0; i_chapterIterator < o_chapterLinks.length; ++i_chapterIterator)
	{
		var s_className = 'linkBlock ';
		s_className += (o_chapterLinks[i_chapterIterator].id == 'linkChapter' + i_chapterIndex) ? '' : 'in';
		s_className += 'activeLink';
		o_chapterLinks[i_chapterIterator].className = s_className;
	}
}


function swapText(i_chapterIndex)
{
	var o_divSchoolRight = $('divSchoolRight');
	var o_textChapters = o_divSchoolRight.getElementsByTagName('div');
	
	var o_newChapter = $('divShoeFilmText' + i_chapterIndex);
	var o_oldChapter = null;

	if (o_newChapter != null)
	{
		for (var i_loopIndex = 0; i_loopIndex < o_textChapters.length; ++i_loopIndex)
		{
			o_oldChapter = o_textChapters[i_loopIndex].className == 'itemHidden' ? o_oldChapter : o_textChapters[i_loopIndex];
		}

		new Effect.Appear('divSchoolCurtain',  { afterFinish: function(){swapVisibility(o_oldChapter.id, o_newChapter.id);}, duration: 1.0, queue: { position: 'end', scope: 'global' }});
		new Effect.Fade('divSchoolCurtain', { duration: 1.0, queue: { position: 'end', scope: 'global' }});
	}
}


function swapVisibility(s_oldChapter, s_newChapter)
{
	$(s_oldChapter).className = 'itemHidden';
	$(s_newChapter).className = ''; 
}


function swapVideo(i_chapterIndex)
{
	var o_objectPlayer = $('objectPlayer');
	var o_objectHolder = o_objectPlayer.parentNode;

	var o_parmFlash = $('parmFlash');
	var o_inputFlash = $('inputFlash');
	
	o_parmFlash.value = o_inputFlash.value.replace('***', i_chapterIndex);
	o_objectPlayer.style.display = 'none';
	
	var o_cloneObject = o_objectPlayer.cloneNode(true);
	
	o_objectHolder.removeChild(o_objectPlayer);
	o_objectHolder.appendChild(o_cloneObject);

	o_cloneObject.style.display = 'block';

}


function initialiseSchool()
{
	$('divSchoolCurtain').style.display = 'none';
}


function swapDropDownSize(s_variantCode)
{
	var o_divMatrixSize = $('divMatrixSize');
	var o_selectDummy = $('selectDummy');
	var o_arraySizes = $('divMatrixSize') == null ? null : o_divMatrixSize.getElementsByTagName('select'); 

	if (o_arraySizes != null && o_arraySizes.length > 0)
	{
		for (var i_indexSize = 0; i_indexSize < o_arraySizes.length; ++i_indexSize)
		{
			o_arraySizes[i_indexSize].className = 'itemHidden';
		}
		
		var o_selectSize = $('selectSize' + s_variantCode);
		var o_optionSize = $('optionSize' + s_variantCode);
		
		if (o_selectDummy != null)
		{
			o_selectDummy.className = 'itemHidden';
		}
		
		if (o_optionSize != null)
		{
			o_selectSize.disabled = false;
			o_selectSize.className = 'inputSelect';
		}

		if (o_selectSize != null)
		{
			o_selectSize.disabled = false;
			o_selectSize.selectedIndex = 0;
		}
	}
}


function clickedSelectVariant()
{
	/* disable first option */
	
	var o_optionVariant = $('optionVariant');
	o_optionVariant.className = 'optionDisabled';
	o_optionVariant.disabled = true;
	
	/* determine active variant */
	
	var o_selectVariant = $('selectVariant');
	var s_variantCode = o_selectVariant.options[o_selectVariant.selectedIndex].value;
	
	/* swap image */

	swapArticleImage(s_variantCode);
	
	/* swap dropdown for size */

	swapDropDownSize(s_variantCode);

	/* swap packaging */
	
	swapPacking();

	/* handle price */
	
	swapPrice(s_variantCode, $('inputSize').value);
	
	/* update buy form */
	
	updateBuyForm();
}


function swapPacking()
{
	var o_spanPacking = $('spanPacking');
	var o_headerPacking = $('headerPacking');
	
	if (o_spanPacking != null && o_headerPacking != null)
	{
		var i_packQuantity = getPackQuantity();
		o_spanPacking.innerHTML = (i_packQuantity == null) ? '' : i_packQuantity;
		o_headerPacking.className = (i_packQuantity == null) ? 'itemHidden' : '';
	}
}


function clickedSelectSize()
{
	/* determine active variant */
	
	var o_selectVariant = $('selectVariant');
	var s_variantCode = (o_selectVariant == null) ? $('inputVariant').value : o_selectVariant.options[o_selectVariant.selectedIndex].value;

	/* determine active size */

	var o_selectSize = $('selectSize' + s_variantCode);
	var s_sizeCode = o_selectSize.options[o_selectSize.selectedIndex].value;

	/* disable first option */
	
	var o_optionSize = $('optionSize' + s_variantCode);
	o_optionSize.className = 'optionDisabled';
	o_optionSize.disabled = true;

	/* swap packaging */
	
	swapPacking();

	/* swap price */
	
	swapPrice(s_variantCode, s_sizeCode);
	
	/* update buy form */

	updateBuyForm();
}


function updateBuyForm()
{
	if ($('inputCountPacks') != null)
	{
		var b_hasQuantity = $('inputCountPacks').value != '';
		
		var s_inputModel = $('inputModel').value;
		var s_inputRow = $('inputRow').value;
		
		var o_selectVariant = $('selectVariant');
		var s_variantCode = (o_selectVariant == null) ? $('inputVariant').value : o_selectVariant.options[o_selectVariant.selectedIndex].value;
		
		var o_selectSize = $('selectSize' + s_variantCode);
		var s_sizeCode = (o_selectSize == null) ? $('inputSize').value : o_selectSize.options[o_selectSize.selectedIndex].value;
		
		var b_isBuyActive = (s_variantCode != '' && s_sizeCode != '' && b_hasQuantity);
		
		$('buttonBuy').disabled = !b_isBuyActive;
		$('buttonBuy').className = b_isBuyActive ? 'buttonBuy' : 'buttonBuyDisabled';
		
		if (b_isBuyActive)
		{	
			var o_inputBuy = $('inputBuy');
			o_inputBuy.name = s_inputModel + s_variantCode + s_sizeCode;
			o_inputBuy.name = 'cart0_' + o_inputBuy.name + s_inputRow;
			o_inputBuy.name = o_inputBuy.name.replace(' ', '');
		}
	}
}


function clickedBuy()
{
	$('inputBuy').value = $('inputCountPacks').value;
	$('formBuy').submit();
}


function clickedSelectFilter()
{
	/* determine values */
	
	var o_selectFilterBrand = $('selectFilterBrand');
	var o_selectFilterMaterial = $('selectFilterMaterial');
	
	var s_brandCode = o_selectFilterBrand.options[o_selectFilterBrand.selectedIndex].value;
	var s_materialCode = o_selectFilterMaterial.options[o_selectFilterMaterial.selectedIndex].value;
	
	/* submit values */
	
	var o_formAdmin = $('formAdmin');

	o_formAdmin.action.disabled = true;
	o_formAdmin.language.disabled = true;
	o_formAdmin.editlang.disabled = true;

	o_formAdmin.brand.disabled = false;
	o_formAdmin.material.disabled = false;

	o_formAdmin.brand.value = s_brandCode;
	o_formAdmin.material.value = s_materialCode;
	
	o_formAdmin.submit();
}


function clickedArticleTab(s_tabSet, s_tabName)
{
	/* obtain the tabset and its contents */
	
	var o_tabSet = $(s_tabSet);
	var o_tabSetDivs = o_tabSet.getElementsByTagName('div');
	
	/* determine which header and panel is to be active */
	
	var o_tabHeaders = null;
	var o_tabPanels = null;
	
	for (var i_loopDiv = 0; i_loopDiv < o_tabSetDivs.length; ++i_loopDiv)
	{
		o_tabHeaders = (o_tabSetDivs[i_loopDiv].className == 'tabHeaders') ? o_tabSetDivs[i_loopDiv] : o_tabHeaders;
		o_tabPanels = (o_tabSetDivs[i_loopDiv].className == 'tabPanels') ? o_tabSetDivs[i_loopDiv] : o_tabPanels;
	}
	
	/* cycle headers and ensure correct header is active */ 
	
	var o_tabHeaderDivs = o_tabHeaders.getElementsByTagName('div');
	
	for (var i_loopDiv = 0; i_loopDiv < o_tabHeaderDivs.length; ++i_loopDiv)
	{
		if (o_tabHeaderDivs[i_loopDiv].id.indexOf('divTabHeader') > -1)
		{
			o_tabHeaderDivs[i_loopDiv].className = (o_tabHeaderDivs[i_loopDiv].id == 'divTabHeader' + s_tabName) ? 'tabActive' : 'tabInactive';
		}
	}

	var o_tabPanelDivs = o_tabPanels.getElementsByTagName('div');

	/* cycle panels and ensure correct panel is active */ 

	for (var i_loopDiv = 0; i_loopDiv < o_tabPanelDivs.length; ++i_loopDiv)
	{
		if (o_tabPanelDivs[i_loopDiv].id.indexOf('divTabPanel') > -1)
		{
			o_tabPanelDivs[i_loopDiv].className = (o_tabPanelDivs[i_loopDiv].id == 'divTabPanel' + s_tabName) ? '' : 'itemHidden';
		}
	}
}


function clickedRadioType(o_typeRadio)
{
	o_typeRadio.checked = false;
}


function getHeight(o_domObject)
{
	var i_objectHeight = o_domObject.offsetHeight;
	i_objectHeight = (i_objectHeight != null) ? i_objectHeight : o_domObject.style.pixelHeight;
	return i_objectHeight;
}


function getWidth(o_domObject)
{
	var i_objectWidth = o_domObject.offsetWidth;
	i_objectWidth = (i_objectWidth != null) ? i_objectWidth : o_domObject.style.pixelWidth;
	return i_objectWidth;
}


function swapPrice(s_variantCode, s_sizeCode)
{
	/* obtain price spans */
	
	var o_arraySpan = $('divPrice').getElementsByTagName('span');
	
	/* hide all prices */
	
	for (var i_loopSpan = 0; i_loopSpan < o_arraySpan.length; ++i_loopSpan)
	{
		if (o_arraySpan[i_loopSpan].id.indexOf('spanPrice') > -1)
		{
			o_arraySpan[i_loopSpan].className = 'itemHidden';
		}
	}
	
	/* obtain model and variant price */
	
	var o_spanPriceModel = $('spanPriceModel'); 
	var o_spanPriceItem = (s_sizeCode == null ? null : $('spanPrice' + s_variantCode + s_sizeCode)); 
	
	/* make variant price visible if it exists otherwise model price */
	
	if (o_spanPriceItem == null)
	{
		o_spanPriceModel.className = '';
	}
	else
	{
		o_spanPriceItem.className = '';
	}
	
	/* apply change to stock iframe */
	
	var o_frameStock = $('frameStock');
	if (o_frameStock != null)
	{
		var s_inputModel = $('inputModel').value;
		var s_framePage = 'Servlet?page=107';
		s_framePage += (o_spanPriceItem == null) ? '' : '&article=' + s_inputModel + s_variantCode + s_sizeCode;
		
		o_frameStock.src = s_framePage;
		o_frameStock.className = (o_spanPriceItem == null) ? 'itemHidden' : '';
	}
}


function swapArticleImage(s_variantCode)
{
	/* obtain image holder and contents */
	
	var o_divImage = $('divImage');
	var o_arrayImages = o_divImage.getElementsByTagName('img');
	
	/* obtain model and variant image */
	
	var o_imgModel = $('imgModel');
	var o_imgVariant = $('imgVariant' + s_variantCode);

	/* define sequence number for image of active variant */
	
	var i_imageIndex = 0;
	
	/* loop through images */
	
	for (var i_loopIndex = 0; i_loopIndex < o_arrayImages.length; ++ i_loopIndex)
	{
		o_arrayImages[i_loopIndex].className = 'itemHidden';
		i_imageIndex = (o_arrayImages[i_loopIndex].id == 'imgVariant' + s_variantCode) ? i_loopIndex : i_imageIndex; 
	}
	
	/* make variant image visible if found, otherwise use model image */
	
	o_imgModel.className = 'itemHidden';
	o_imgVariant.className = '';

	/* align image */
	
	alignImage(o_divImage, i_imageIndex);
}


function alignArticleImage()
{
	var o_divImage = $('divImage');
	alignImage(o_divImage, 0);
}


function alignImages()
{
	/* obtain all anchors in main content */
	
	var o_productDivs = $('divWrapperContent').getElementsByTagName('div');
	
	/* cycle each anchor and align its image */
	
	for (var i_loopIndex = 0; i_loopIndex < o_productDivs.length; ++i_loopIndex)
	{
		var o_imageHolders = o_productDivs[i_loopIndex].getElementsByTagName('a');
		
		if (o_imageHolders != null && o_imageHolders.length > 0)
		{
			alignImage(o_imageHolders[0], 0);
		}
	}
	
	/* check for banner image */
	
	var o_divContentTop = $('divContentTop');
	var o_inputCaption = $('inputCaption');
	var s_imageCaption = (o_inputCaption == null || o_inputCaption.value.length == 0) ? null : o_inputCaption.value;
	
	if (s_imageCaption != null)
	{
		s_imageCaption = s_imageCaption.replace('/70/', '/garp/	');
		$('divContentTop').style.backgroundImage = 'url(\'' + s_imageCaption + '\')';
	}
	
	o_divContentTop.className = 'articleCategory';	
}


function alignImage(o_imageHolder, i_imageIndex)
{
	var o_imageItem = o_imageHolder.getElementsByTagName('img');
	
	if (o_imageItem != null && i_imageIndex < o_imageItem.length)
	{
		o_imageItem[i_imageIndex].className = '';

		var i_holderHeight = getHeight(o_imageHolder);
		var i_holderWidth = getWidth(o_imageHolder);
		
		var i_imgHeight = getHeight(o_imageItem[i_imageIndex]);
		var i_imgWidth = getWidth(o_imageItem[i_imageIndex]);
		
		o_imageItem[i_imageIndex].style.top = ((i_holderHeight - i_imgHeight) / 2) + 'px';
		o_imageItem[i_imageIndex].style.left = ((i_holderWidth - i_imgWidth) / 2) + 'px';
	}
}


function hoverArticleImage(o_imageHolder, b_isHover)
{
	o_imageHolder.parentNode.parentNode.className = b_isHover ? 'activeNode' : '';
	o_imageHolder.className = b_isHover ? 'itemTranslucent' : '';
}


function togglePopup(b_makeVisible, s_overlayClass)
{
	/* obtain reference to background and foreground */
	
	var o_divOverlayBackground = $('divOverlayBackground');
	var o_divOverlayForeground = $('divOverlayForeground');

	/* set visibility */
	
	o_divOverlayBackground.className = (b_makeVisible) ? '' : 'itemHidden';
	o_divOverlayForeground.className = (b_makeVisible) ? s_overlayClass : 'itemHidden';
	
	/* determine dimensions */

	var i_bodyHeight = Math.max(getHeight(document.body), getHeight($('divWrapperBody'))); 
	var i_bodyWidth = getWidth(document.body);
	var i_foregroundWidth = getWidth($('divOverlayForeground'));

	/* set position and dimension */

	var i_backgroundHeight = i_bodyHeight;
	var i_foregroundWidth = (i_bodyWidth - i_foregroundWidth) / 2;
		
	o_divOverlayBackground.style.height = i_backgroundHeight + 'px';
	o_divOverlayForeground.style.left = i_foregroundWidth + 'px';
	
	/* event handling */
	
	window.onscroll = b_makeVisible ? eventScrollResize : null;
	window.onresize = b_makeVisible ? eventScrollResize : null;
}


function eventScrollResize()
{
	togglePopup(true);
}


function getPackQuantity()
{
	var o_selectVariant = $('selectVariant');
	var s_variantCode = (o_selectVariant == null) ? $('inputVariant').value : o_selectVariant.options[o_selectVariant.selectedIndex].value;
	
	var o_selectSize = $('selectSize' + s_variantCode);
	var s_sizeCode = (o_selectSize == null) ? $('inputSize').value : o_selectSize.options[o_selectSize.selectedIndex].value;

	s_variantCode = s_variantCode != ' ' ? s_variantCode : '';
	s_sizeCode = s_sizeCode != ' ' ? s_sizeCode : '';
	
	var o_inputPackQuantity = $('inputPackQuantity' + s_variantCode + s_sizeCode);

	return (o_inputPackQuantity == null) ? null : parseInt(o_inputPackQuantity.value);
}


function changedQuantity(s_modelNumber)
{
	/* determine pack quantity */

	var i_packQuantity = getPackQuantity();

	/* set fields */
	
	var o_inputCountPacks = $('inputCountPacks');
	var o_inputCountItems = $('inputCountItems');

	var i_valPacks = parseInt(o_inputCountPacks.value);
	
	if (isNaN(i_valPacks))
	{
		o_inputCountPacks.value = '';
		o_inputCountItems.value = '';
	}
	else
	{
		o_inputCountItems.value = Math.max(1, i_packQuantity) * i_valPacks;
	}
	
	updateBuyForm();
}


function clickedOverlayLogin()
{
	var o_formLogin = $('formOverlayLogin');
	
	o_formLogin.comp.value = $('inputOverlayUser').value;
	o_formLogin.pass.value = $('inputOverlayPassword').value;
	o_formLogin.save.value = $('checkboxOverlaySave').checked;
	
	o_formLogin.submit();
}


function clickedRank(i_rankValue)
{
	var o_spanRank = $('spanRank');
	var o_arraySpan = o_spanRank.getElementsByTagName('span');
	
	for (var i_rankIndex = 0; i_rankIndex < 5; ++i_rankIndex)
	{
		o_arraySpan[i_rankIndex].className = (i_rankValue >= i_rankIndex) + 'Rank';
	}
	
	$('formContent').inputRank.value = i_rankValue + 1;
	checkFeedback();
}


function checkFeedback()
{
	var b_isValid = true;
	
	var o_formContent = $('formContent');
	
	b_isValid = b_isValid && o_formContent.inputTitle.value.length > 0;
	b_isValid = b_isValid && o_formContent.inputComment.value.length > 0;
	b_isValid = b_isValid && o_formContent.inputRank.value.length > 0;
	
	$('buttonFeedback').className = 'inputButton' + (b_isValid ? '' : 'Disabled');
	$('buttonFeedback').disabled = !b_isValid;	
}


function activateTab(i_tabIndex)
{
	var s_tabName = 'itemTab' + i_tabIndex;
	var o_tabObject = $(s_tabName);

	if (o_tabObject != null)
	{
		o_tabObject.className = 'tabActive';
		o_tabObject.innerHTML = o_tabObject.innerHTML + '<span>&nbsp;</span>';
	}
}


function submitSearch() 
{
	var o_inputSearch = $('inputSearch');
	return (o_inputSearch != null) && (o_inputSearch.value.length > 0);
}


function submitOrder()
{
	var b_isRadioExisting = $('radioAddressExisting').checked;
	
	if (b_isRadioExisting)
	{
		$('inputName').disabled = false;
		$('inputDeliveryAddress').disabled = false;
		$('inputDeliveryCity').disabled = false;
	}

	$('inputCancel').className = 'inputButton greenLongDisabled';
	$('inputSend').className = 'inputButton greenLongDisabled';

	$('inputCancel').disabled = true;
	$('inputSend').disabled = true;

	$('formSend').submit();
}


function clickedRadioAddress(o_radioClicked)
{
	/* check which option is active */
	
	var b_isRadioExisting = (o_radioClicked.id == 'radioAddressExisting');
	var b_isRadioNew = (o_radioClicked.id == 'radioAddressNew');
	
	/* manipulate address drop-down */
	
	var o_selectAddressExisting = $('selectAddressExisting');
	
	o_selectAddressExisting.className = b_isRadioExisting ? 'inputSelect' : 'inputSelectDisabled';
	o_selectAddressExisting.disabled = !b_isRadioExisting;
	
	/* obtain fields */

	var o_inputDeliveryName = $('inputName');
	var o_inputDeliveryAddress = $('inputDeliveryAddress');
	var o_inputDeliveryPostcode = $('inputDeliveryPostcode');
	var o_inputDeliveryCity = $('inputDeliveryCity');

	/* enable/disable fields */
	
	o_inputDeliveryName.disabled = !b_isRadioNew;
	o_inputDeliveryAddress.disabled = !b_isRadioNew;
	o_inputDeliveryPostcode.disabled = !b_isRadioNew;

	/* set input classes */
	
	o_inputDeliveryName.className = b_isRadioNew ? 'inputText' : 'inputTextDisabled';
	o_inputDeliveryAddress.className = b_isRadioNew ? 'inputText' : 'inputTextDisabled';
	o_inputDeliveryPostcode.className = b_isRadioNew ? 'inputText inputShort' : 'inputTextDisabled inputShort';
	o_inputDeliveryCity.className = b_isRadioExisting ? 'inputTextDisabled' : 'inputTextDisabled inputShort';
	
	/* show/hide fields */
	
	$('divDeliveryPostcode').className = b_isRadioExisting ? 'itemHidden' : 'inputBox inputShort clearBoth';
	$('divDeliveryCity').className = b_isRadioExisting ? 'inputBox clearBoth' : 'inputBox inputShort';
	$('labelDeliveryCity').innerHTML = b_isRadioExisting ? $('labelDeliveryPostcode').innerHTML : '&nbsp;';
	
	/* reset address if necessary */

	o_inputDeliveryName.value = (!b_isRadioExisting && !b_isRadioNew) ? $('inputHiddenName').value : '';
	o_inputDeliveryAddress.value = (!b_isRadioExisting && !b_isRadioNew) ? $('inputHiddenAddress').value : '';
	o_inputDeliveryPostcode.value = (!b_isRadioExisting && !b_isRadioNew) ? $('inputHiddenPostcode').value : '';
	o_inputDeliveryCity.value = (!b_isRadioExisting && !b_isRadioNew) ? $('inputHiddenCity').value : '';
	
	/* disable submit button if address is to be entered */
	
	$('inputSend').className = 'inputButton greenLong' + (b_isRadioNew ? 'Disabled' : '');
	$('inputSend').disabled = b_isRadioNew;
}


function changedOrderField()
{
	var b_isReady = true;
	
	b_isReady = b_isReady && isFieldEntered('inputName');
	b_isReady = b_isReady && isFieldEntered('inputDeliveryAddress');
	b_isReady = b_isReady && isFieldEntered('inputDeliveryPostcode');
	
	$('inputSend').className = 'inputButton greenLong' + (!b_isReady ? 'Disabled' : '');
	$('inputSend').disabled = !b_isReady;
}


function clickedSelectAddress()
{
	/* disable first option */
	
	var o_optionAddress = $('optionAddress');
	o_optionAddress.className = 'optionDisabled';
	o_optionAddress.disabled = true;
	
	/* parse address */
	
	var o_selectAddressExisting = $('selectAddressExisting');
	var s_optionAddress = $('selectAddressExisting').options[o_selectAddressExisting.selectedIndex].value;
	var s_addressFields = s_optionAddress.split('#');
	
	/* obtain address fields */

	var o_inputAddressName = $('inputName');
	var o_inputAddressStreet = $('inputDeliveryAddress');
	var o_inputAddressCity = $('inputDeliveryCity');

	/* set address fields */

	o_inputAddressName.value = s_addressFields[0];
	o_inputAddressStreet.value = s_addressFields[1];
	o_inputAddressCity.value = s_addressFields[2];
}


function clearCart(s_confirmText)
{
	if (confirm(s_confirmText))
	{
		openLink('Servlet?page=8&action=cart.delete');
	}
}


function submitCart(o_scriptEvent)
{
	var b_result = false;

	if ((window.event != null) && (window.event.keyCode == 13))
	{
		b_result = true;
	}
	else if ((o_scriptEvent != null) && (o_scriptEvent.which == 13))
	{
		b_result = true;
	}

	if (b_result)
	{
		submitForm('formCart');
	}
	
	return !b_result;
}


function pageLoaded(s_pageLink)
{
	/* set up social media */
	
	var o_frameShare = $('frameShare');
	var o_linkShareFacebook = $('linkShareFacebook');
	var o_linkShareTwitter = $('linkShareTwitter');

	if (o_frameShare != null)
	{
		o_frameShare.src = 'http://www.facebook.com/plugins/like.php?locale=sv_SE&href=' + s_pageLink;
	}
	
	if (o_linkShareFacebook != null)
	{
		o_linkShareFacebook.href='http://www.facebook.com/sharer.php?u=' + s_pageLink;
	}
	if (o_linkShareTwitter != null)
	{
		o_linkShareTwitter.href='http://twitter.com/home?status=' + s_pageLink;
	}
	
	/* bring up login popup if sales school was requested */
	
	var o_formOverlayLogin = $('formOverlayLogin');
	
	if ((o_formOverlayLogin != null) && (o_formOverlayLogin.sales != null))
	{
		prepareLogin();
		togglePopup(true, 'overlayLogin');
		focusField('inputOverlayUser');
	}	
}


function lookupPostcode(o_lookupField)
{
	var o_newRequest = getAjaxRequest(); 

	var o_inputDeliveryPostcode = $('inputDeliveryPostcode');
	
	o_newRequest.onreadystatechange = function() { handleAjaxResponse(o_newRequest); };
	o_newRequest.open('GET', 'Postcode?postcode=' + o_inputDeliveryPostcode.value, true);
	o_newRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	o_newRequest.send(null);
}


function handleAjaxResponse(o_newResponse)
{
	if (o_newResponse.readyState == 4 && o_newResponse.status == 200)
	{
		var s_responseText = o_newResponse.responseText;
		var s_responseValues = (s_responseText == null) ? null : s_responseText.split('=');

		if (s_responseValues != null && s_responseValues.length == 2)
		{
			$('inputDeliveryPostcode').value = s_responseValues[0];
			$('inputDeliveryCity').value = s_responseValues[1];
		}
		else
		{
			$('inputDeliveryCity').value = '';
		}
	}
}


function getAjaxRequest()
{
	var o_result = null;
	
	try
	{
		o_result = new XMLHttpRequest();
	}
	catch (o_throwable)
	{
		/* browser does not support this call */
	}

	try
	{
		o_result = new ActiveXObject('Microsoft.XMLHTTP');
	}
	catch (o_throwable)
	{
		/* browser does not support this call */
	}
	
	return o_result;
}


function clickedSelectQuestion(b_isLoggedIn, s_textQuestion, s_textOther)
{
	/* disable first option */
	
	var o_optionChoose = $('optionChoose');
	o_optionChoose.className = 'optionDisabled';
	o_optionChoose.disabled = true;

	/* check which option is active */
	
	var o_selectQuestion = $('selectQuestion');
	var o_activeOption = o_selectQuestion.options[o_selectQuestion.selectedIndex];

	var b_isCatalogue = o_activeOption == $('optionCatalogue');
	var b_isRegister = o_activeOption == $('optionRegister');
	var b_isQuestion = o_activeOption == $('optionQuestion');
	var b_isForgot = o_activeOption == $('optionForgot');
	
	/* set text for question/other */
	
	$('labelMessage').innerHTML = b_isQuestion ? s_textQuestion : s_textOther;
	
	/* change state of customer number field */
	
	$('inputCustomer').disabled = b_isLoggedIn || b_isRegister;
	$('inputCustomer').className = (b_isLoggedIn || b_isRegister || b_isForgot) ? 'inputTextDisabled' : 'inputText';

	/* change state of customer type */

	$('radioRetailerTrue').disabled = (b_isLoggedIn || b_isRegister || b_isForgot); 
	$('radioRetailerFalse').disabled = (b_isLoggedIn || b_isRegister || b_isForgot); 
	$('radioRetailerTrue').checked = (b_isLoggedIn || b_isRegister || b_isForgot) ? true : $('radioRetailerTrue').checked;
	$('radioRetailerFalse').checked = (b_isLoggedIn || b_isRegister || b_isForgot) ? false : $('radioRetailerFalse').checked;

	/* hide all text boxes except email if password request */

	switchVisibility('divInputName', !b_isForgot);
	switchVisibility('divInputCustomer', !b_isForgot);
	switchVisibility('divInputCompany', !b_isForgot);
	switchVisibility('divInputOrganisation', !b_isForgot);
	switchVisibility('divInputDeliveryAddress', !b_isForgot);
	switchVisibility('divInputDeliveryPostcode', !b_isForgot);
	switchVisibility('divInputDeliveryCity', !b_isForgot);
	switchVisibility('divInputDirect', !b_isForgot);
	switchVisibility('divInputMessage', !b_isForgot);
	switchVisibility('divContactButtons', !b_isForgot);
	
	$('divInputEmail').className = b_isForgot ? 'inputBox inputLong clearBoth' : 'inputBox';
	
	/* focus first enabled field */
	
	if (b_isForgot)
	{
		$('inputEmail').focus();
	}
	else if (!b_isLoggedIn)
	{
		$('inputName').focus();
	}
	else if (b_isQuestion)
	{
		$('inputMessage').focus();
	}

	/* check status of send button */

	changedContactField(b_isLoggedIn);
}


function switchVisibility(s_objectId, b_isVisible)
{
	var o_objectRef = $(s_objectId);
	o_objectRef.className = (b_isVisible) ? o_objectRef.className.replace(' itemHidden', '') : o_objectRef.className + ' itemHidden';
}


function isFieldEntered(s_fieldId)
{
	var b_result = true;
	
	var o_fieldRef = $(s_fieldId);
	
	if (o_fieldRef != null && !o_fieldRef.disabled)
	{
		var s_fieldText = o_fieldRef.value;

		while (s_fieldText.indexOf(' ') > -1)
		{
			s_fieldText = s_fieldText.replace(' ', '');
		}
		
		b_result = s_fieldText.length > 0;
	}
	
	return b_result;
}


function changedContactField(b_isLoggedIn)
{
	var b_isSendEnabled = true;	

	/* check that email has been entered */
	
	b_isSendEnabled = b_isSendEnabled && isFieldEntered('inputEmail');
	
	/* check that a customer type has been chosen */
	
	b_isSendEnabled = b_isSendEnabled && ($('radioRetailerTrue').checked || $('radioRetailerFalse').checked);

	/* check that a context has been selected */
	
	var o_selectQuestion = $('selectQuestion');
	var o_activeOption = o_selectQuestion.options[o_selectQuestion.selectedIndex];

	var b_isQuestion = o_activeOption == $('optionQuestion');
	var b_isForgot = o_activeOption == $('optionForgot');
	
	b_isSendEnabled = b_isSendEnabled && (o_selectQuestion.selectedIndex > 0);
	
	/* if the context is question, check that a question has been entered */
	
	if (b_isQuestion)
	{
		b_isSendEnabled = b_isSendEnabled && isFieldEntered('inputMessage');
	}
	
	/* if not logged in, check that contact details are provided */
	
	if (!b_isForgot)
	{
		if (!b_isLoggedIn)
		{
			b_isSendEnabled = b_isSendEnabled && isFieldEntered('inputName');
			b_isSendEnabled = b_isSendEnabled && isFieldEntered('inputCompany');
		}
		b_isSendEnabled = b_isSendEnabled && isFieldEntered('inputDeliveryAddress');
		b_isSendEnabled = b_isSendEnabled && isFieldEntered('inputDeliveryPostcode');
		b_isSendEnabled = b_isSendEnabled && isFieldEntered('inputDeliveryCity');
		b_isSendEnabled = b_isSendEnabled && isFieldEntered('inputDirect');
	}
	
	/* set button state */
	
	$('buttonSend').disabled = !b_isSendEnabled;
	$('buttonSend').className = b_isSendEnabled ? 'inputButton greenShort' : 'inputButtonDisabled greenShortDisabled';

	$('buttonForgot').disabled = !b_isSendEnabled;
	$('buttonForgot').className = b_isSendEnabled ? 'inputButton greenLong' : 'inputButtonDisabled greenLongDisabled';
}


function submitContactForm()
{
	$('buttonSend').disabled = true;
	$('buttonForgot').disabled = true;

	$('radioRetailerTrue').disabled = false;
	$('radioRetailerTrue').disabled = false;

	$('buttonSend').className = 'inputButtonDisabled greenShortDisabled';
	$('buttonForgot').className = 'inputButtonDisabled greenLongDisabled';

	return true;
}


function clickedImageLarge()
{
	/* obtain elements */
	
	var o_divImage = $('divImage');
	var o_imageArray = o_divImage.getElementsByTagName('img');
	var s_imageUrl = null;

	/* loop through images to determine active image */
	
	for (var i_indexArray = 0; i_indexArray < o_imageArray.length; ++i_indexArray)
	{
		s_imageUrl = (o_imageArray[i_indexArray].className != 'itemHidden') ? o_imageArray[i_indexArray].src : s_imageUrl
	}

	if (s_imageUrl != null)
	{
		/* set image */

		$('imageLarge').src = s_imageUrl.replace('250', '800');
		attachDomEvent($('imageLarge'), 'load', function() {togglePopup(true, 'overlayImage');});

		$('divOverlayImage').className = '';
		$('divOverlayLogin').className = 'itemHidden';
	}
}


function prepareLogin()
{
	$('divOverlayImage').className = 'itemHidden';
	$('divOverlayLogin').className = '';
}


function loadFrame(s_frameSuffix, s_pageNumber)
{
	var o_loadFrame = $('frame' + s_frameSuffix);
	
	if (o_loadFrame != null && o_loadFrame.src.indexOf('Servlet') == -1)
	{
		o_loadFrame.src = 'Servlet?page=' + s_pageNumber + '&frame=true';
	}
}


function loadPersonalMain()
{
	loadFrame('Template', '16');
}

