function initShop()
{
	initBuyForms();
	initAsyncBasket();
	initAlert();
	initQuestion();
	initIndex();
	if($('smallSearchForm'))initFormFields("smallSearchForm");
	if($('smallLoginForm'))initFormFields("smallLoginForm"); 
	if($('smallNewsletterForm'))initFormFields('smallNewsletterForm');
}

var ajaxCategories = new Array();

function showSubCategories(parentId, level){
	var divElement = $('categoriesParent' + parentId);
	//if(divElement.className == 'categoriesParentClose'){
		var imageLoading = "<img src='" + shop.templatePath + "/images/smallLoading.gif'>";
		document.getElementById('categoriesTreeLoadingParent'+parentId).className = "categoriesTreeLoadingParentOpen";
		document.getElementById('categoriesTreeLoadingParent'+parentId).innerHTML = imageLoading;
		// alert(document.getElementById('categoriesTreeLoadingParent'+parentId).innerHTML)
		hideSubCategoriesByParentId(parentId);
		//cancelAjaxCategories();
		//clearTimeout(timeoutCategories);

		//timeoutCategories[parentId] = setTimeout(function(){ajaxLoad(shop.url + '/ajaxTemplates/common/categoriesContent.cfm?categoryId='+parentId+'&deepLevel=1&level='+level, divElement, 'actionShowSubCategories('+parentId+');hideLoadingParent('+parentId+')', null)}, 0);
		
		ajaxCategories[ajaxCategories.length+1] = ajaxLoad(shop.url + '/ajaxTemplates/common/categoriesContent.cfm?categoryId='+parentId+'&deepLevel=1&level='+level, divElement, 'actionShowSubCategories('+parentId+');hideLoadingParent('+parentId+')', null);
		
//	}
	//else{
//		hideSubCategoriesByParentId(parentId);
//	}
}

function cancelAjaxCategories(){
	for(var i=0; i<ajaxCategories.length;i++){
		if(ajaxCategories[i] != undefined) ajaxCancel(ajaxCategories[i]);
		delete ajaxCategories[i];
	}
	
}

function actionShowSubCategories(parentId){
	var divElement = $('categoriesParent' + parentId);
	divElement.className = 'categoriesParentOpen';
	topPosition = $('containerLeft').offsetTop - $('categoriesParent' + parentId).offsetTop;
	if($('subCategoriesTreeMain'+parentId)) $('subCategoriesTreeMain'+parentId).style.top = (topPosition + 10) + 'px';
	divElement.style.display = '';
}

function hideSubCategoriesByParentId(parentId){
	var divElement = document.getElementById('categoriesParent' + parentId);
	divElement.innerHTML = "";
	divElement.className = "categoriesParentClose";
}

function hideLoadingParent(parentId){
	document.getElementById('categoriesTreeLoadingParent'+parentId).innerHTML = "";
	document.getElementById('categoriesTreeLoadingParent'+parentId).className = "categoriesTreeLoadingParent";
}

function hideSubCategories(){
	//$$('.categoriesTreeLoadingParentOpen').each(function(el){el.className = 'categoriesTreeLoadingParent'; el.innerHTML = "";});
	$$('.categoriesParentOpen').each(function(el){el.className = 'categoriesParentClose'; el.innerHTML = "";});
}


function hideSubCategoriesAll(){
	$$('.categoriesParentOpen').each(function(el){el.className = 'categoriesParentClose'; el.innerHTML = "";});
}

function initIndex(){
	if($('bannersIndex')){
		var V2 = new viewer($$('#bannersIndexBox img)'),{
			mode: 'alpha',
			interval: 3000
		});
		V2.play(true);
		//$(window).addEvent('domready', V2.play.bind(V2,[false]));
	}
}
function validateAddProduct(req){
		validateAddProductShowMessage(req);
}

function showPriceByOption(form, option){
	showPriceByOptionWithImage(form, option);
	
	var index = 0;
	var step = 0;
	var element = document.getElementById('productOptionValueTextSelected');
	$$('.productOptionValueTextSelected').each(function(el){
		var option = el.getElementsByTagName('img');
		for(var i=0;i<option.length;i++){if(option[i].className == 'productOptionValueText'){ index = i; step++;}}
		resetDesign(option[index], step);
	});
}

function showArrowOption(objId){
	var obj = $(objId);
	$$('.productOptionValueTextSelected').each(function(el){el.removeClass("productOptionValueTextSelected");
		el.addClass("productOptionValueText");
	});
	
	obj.removeClass("productOptionValueText");
	obj.addClass("productOptionValueTextSelected");
}

function showArrowOptionGroup(objId){
	var obj = $(objId);

	if(obj.hasClass("productOptionValueText")){
		obj.removeClass("productOptionValueText");
		obj.addClass("productOptionValueTextSelected");
	}else{
		obj.removeClass("productOptionValueTextSelected");
		obj.addClass("productOptionValueText");
	}
}

function initProduct(){
	if(document.getElementById('productInfoBuyForm')){
		initFormFields('productInfoBuyForm');
	}
}

function checkUserType(type){
	if(type == 1){
		userFieldsRequired = 1;
		userFieldsDisplay = '';
		companyFieldsRequired = 0;
		companyFieldsDisplay = 'none';
	}
	else if(type == 2){
		userFieldsRequired = 0;
		userFieldsDisplay = 'none';
		companyFieldsRequired = 1;
		companyFieldsDisplay = '';
	}
	else if(type == 3){
		userFieldsRequired = 1;
		userFieldsDisplay = '';
		companyFieldsRequired = 1;
		companyFieldsDisplay = '';
	}
	
	document.getElementById('userInformationFirstName').style.display = userFieldsDisplay;
	if(document.getElementById('userInformationLastName'))document.getElementById('userInformationLastName').style.display = userFieldsDisplay;
	if(document.getElementById('userInformationLastName1'))document.getElementById('userInformationLastName1').style.display = userFieldsDisplay;
	if(document.getElementById('userInformationLastName2'))document.getElementById('userInformationLastName2').style.display = userFieldsDisplay;
	document.getElementById('userInformationNif').style.display = userFieldsDisplay;
	document.getElementById('userInformationCompany').style.display = companyFieldsDisplay;
	document.getElementById('userInformationVat').style.display = companyFieldsDisplay;
	
	document.getElementById('userInformationFirstNameField').setAttribute('requiredField', userFieldsRequired);
	if(document.getElementById('userInformationLastNameField'))document.getElementById('userInformationLastNameField').setAttribute('requiredField', userFieldsRequired);
	if(document.getElementById('userInformationLastName1Field'))document.getElementById('userInformationLastName1Field').setAttribute('requiredField', userFieldsRequired);
	if(document.getElementById('userInformationLastName2Field'))document.getElementById('userInformationLastName2Field').setAttribute('requiredField', userFieldsRequired);
	document.getElementById('userInformationNifField').setAttribute('requiredField', userFieldsRequired);
	document.getElementById('userInformationCompanyField').setAttribute('requiredField', companyFieldsRequired);
	document.getElementById('userInformationVatField').setAttribute('requiredField', companyFieldsRequired);
	
}


function requireSignIn(value){	
	document.getElementById('userInformationPasswordField').setAttribute('requiredField', value);	
	document.getElementById('userInformationConfirmPasswordField').setAttribute('requiredField', value);	
	//document.getElementById('userCheckoutType').value = value;		
	if(value == 0){
		document.getElementById('userInformationSubscribed').style.display = 'none';
		document.getElementById('userInformationPasswords').style.display = 'none';
	}	
	if(value == 1){		
		document.getElementById('userInformationSubscribed').style.display = '';		
		document.getElementById('userInformationPasswords').style.display = '';
	}	
}

function requireShipTo(value){	

	if(document.getElementById('userInformationFirstNameShippingField'))document.getElementById('userInformationFirstNameShippingField').setAttribute('requiredField', value);	
	if(document.getElementById('userInformationLastName1ShippingField'))document.getElementById('userInformationLastName1ShippingField').setAttribute('requiredField', value);	
	if(document.getElementById('userInformationLastName2ShippingField'))document.getElementById('userInformationLastName2ShippingField').setAttribute('requiredField', value);	
	if(document.getElementById('userInformationLastNameShippingField'))document.getElementById('userInformationLastNameShippingField').setAttribute('requiredField', value);	
	if(document.getElementById('userInformationNifShippingField'))document.getElementById('userInformationNifShippingField').setAttribute('requiredField', value);	
	if(document.getElementById('userInformationAddressShippingField'))document.getElementById('userInformationAddressShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationAddress2ShippingField'))document.getElementById('userInformationAddress2ShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationNumberShippingField'))document.getElementById('userInformationNumberShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationCityShippingField'))document.getElementById('userInformationCityShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationStateShippingField'))document.getElementById('userInformationStateShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationZipShippingField'))document.getElementById('userInformationZipShippingField').setAttribute('requiredField', value);
	if(document.getElementById('userInformationCountryShippingField'))document.getElementById('userInformationCountryShippingField').setAttribute('requiredField', value);


	if(value == 1){
		display = '';
	}
	else{
		display = 'none';
	}
	
	if(document.getElementById('userInformationShipTo'))document.getElementById('userInformationShipTo').style.display = display;
	else if(document.getElementById('userInformationControlPanelShipTo'))document.getElementById('userInformationControlPanelShipTo').style.display = display;
	
	//reloadCountries(value);
	
}

function initWishlistTable(){$('informationBox').style.visibility = 'visible';	initFormFields("formWishlistToEmail");}
function initWishlistToEmail(obj){	$('informationBox').style.visibility = 'visible';	initFormFields("formWishlistToEmail");}

function selectAllItemsWishlist(){
	var value = document.getElementById('optionSelect').value;
	if(value == '1') newValue = '2';
	else newValue = '1';
	
	document.getElementById('optionSelect').value =  newValue;
	$$('.userControlPanelWishlistField').each(
																						function(el){
																							if(newValue == '1') el.checked = true;
																							else el.checked = false;
																						})
}

function reloadWishlist(){
	loadAjaxFile(shop.url +'/ajaxTemplates/users/wishlist.cfm?getTemplates=0', 'userControlPanelInfo', 'initWishlistTable()', null);
}

function reloadBasket(){
	loadAjaxFile(shop.url+'/ajaxTemplates/basket/basket.cfm','basketResume', null,null);
}

function initFormsUser(){
	if(document.getElementById('formLogin'))initFormFields('formLogin');
	if(document.getElementById('formUserInformation')){
		initFormFields('formUserInformation');
		onlybuyCheck = $('userInformationCheckoutTypeField');
		checkLogin(onlybuyCheck);
	}
	restartShadowbox();
}

function hideZipResult(shipTo){
	if(shipTo == 1) divId = "userInformationZipShipping_Result";
	else divId = "userInformationZip_Result";

	document.getElementById(divId).style.display='none';
}

function valueZipResult(zip, shipTo){
	if(shipTo == 1) divId = "userInformationZipShipping_Result";
	else divId = "userInformationZip_Result";
	
	document.getElementById(divId).innerHTML = "";
	if(zip == "") document.getElementById(divId).style.display='none';
	else document.getElementById(divId).style.display='';
	
	ajaxLoad(shop.url+'/ajaxTemplates/users/postalCodes.cfm?zip='+zip+'&country='+countryId+'&shipTo='+shipTo,$(divId),null);

}

function valuesStateAndCity(postalCodeId, valueState, valueCity, shipTo){
	if(shipTo == 1) divId = "userInformationPostalCodeIdShippingField";
	else divId = "userInformationPostalCodeIdField";	
	document.getElementById(divId).value = postalCodeId;

	if(shipTo == 1) divId = "userInformationStateShippingField";
	else divId = "userInformationStateField";	
	document.getElementById(divId).value = valueState;
	
	if(shipTo == 1) divId = "userInformationCityShippingField";
	else divId = "userInformationCityField";	
	document.getElementById(divId).value = valueCity;	
}

function valueZip(valueZip, shipTo){
	if(shipTo == 1) divId = "userInformationZipShippingField";
	else divId = "userInformationZipField";	

	document.getElementById(divId).value = valueZip;
}

function reloadMiniBasket(){
	var loadNext = "ajaxLoad('"+shop.url+"/ajaxTemplates/basket/miniBasket.cfm', $('miniBasket'), 'initAsyncBasket();showMiniBasket();');";
	ajaxLoad(shop.url+ '/ajaxTemplates/basket/asyncBasket.cfm', $('asyncBasketBox'), loadNext);
}

function submitUserInformationStepBasket(){
	submitUserInformationForm('formUserInformation');
}

function reloadSmallLogin(){
	ajaxLoad(shop.url+ '/ajaxTemplates/common/smallLogin.cfm', $('smallLogin'));
}

function loadProductRelatedPageInfo(obj, objInfo){
	var clickClass = obj.getAttribute('clickClass');
	var obj = $(obj.id);
	alternateClass(obj, clickClass);
	
	var children = obj.getElementsByTagName('div');
	var objId = children[0].id;
	var obj = $(objId);
	var clickClass = obj.getAttribute('clickClass');
	alternateClass(obj, clickClass);

	var obj = $(objInfo);
	var clickClass = obj.getAttribute('clickClass');
	alternateClass(obj, clickClass);
}

function alternateClass(obj, nameClass){
	$$('.'+nameClass).each(function(el){el.removeClass(nameClass);});
	if(obj.hasClass(nameClass)) obj.removeClass(nameClass);
	else obj.addClass(nameClass);
}

function reloadStock(stock){}

function restartUserFormInformation(){
	initFormFields('formUserInformation');
}

function resetDesign(obj, step){
	var element;
	if(step == 1){
		element = document.getElementById('designBorder');
	}
	else if(step == 2){
		element = document.getElementById('designMaterial');
	}
	if(obj.getAttribute('largeSrc')){
		element.style.backgroundImage = "url("+obj.getAttribute('largeSrc')+")";
	}
}


$(window).addEvent('domready', function(){initShop(); initProduct();});
//$(window).addEvent('click', function(){hideSubCategories()});

