function initShop()
{
	initBuyForms();
	initAsyncBasket();
	initAlert();
	initQuestion();
	initFeaturedProducts();
	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);
		//hideSubCategories();
		//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 initFeaturedProducts(){
	if($('bannersFeaturedProductsBox').childNodes.length > 1){
		var V2 = new viewer($$('#bannersFeaturedProductsBox img)'),{
			mode: 'alpha',
			interval: 4000
		});
		V2.play(true);
	}
}

function validateAddProduct(req){
		validateAddProductShowMessageGoToCart(req);
}

function showPriceByOption(form, option){
	showPriceByOptionWithImage(form, option);
}

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;
	document.getElementById('userInformationLastName').style.display = userFieldsDisplay;
	/*document.getElementById('userInformationLastName1').style.display = userFieldsDisplay;
	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);
	document.getElementById('userInformationLastNameField').setAttribute('requiredField', userFieldsRequired);
	/*document.getElementById('userInformationLastName1Field').setAttribute('requiredField', userFieldsRequired);
	document.getElementById('userInformationLastName2Field').setAttribute('requiredField', userFieldsRequired);*/
	document.getElementById('userInformationNifField').setAttribute('requiredField', userFieldsRequired);
	document.getElementById('userInformationCompanyField').setAttribute('requiredField', companyFieldsRequired);
	document.getElementById('userInformationVatField').setAttribute('requiredField', companyFieldsRequired);
	
	initFormFields('formUserInformation');
}


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 = '';
	}	
	if($('formUserInformation'))initFormFields('formUserInformation');
}

function requireShipTo(value){	

	if(document.getElementById('userInformationFirstNameShippingField'))document.getElementById('userInformationFirstNameShippingField').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('userInformationNumberShippingField'))document.getElementById('userInformationNumberShippingField').setAttribute('requiredField', value);	


	if(value == 1){
		display = '';
	}
	else{
		display = 'none';
	}
	
	if(document.getElementById('userInformationShipTo'))document.getElementById('userInformationShipTo').style.display = display;
	
	//reloadCountries(value);
	
	if($('formUserInformation'))initFormFields('formUserInformation');
}

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(){
	ajaxLoad(shop.url+ '/ajaxTemplates/basket/asyncBasket.cfm', $('asyncBasketBox'), 'showMiniBasket();');
	ajaxLoad(shop.url+ '/ajaxTemplates/basket/miniBasket.cfm', $('miniBasket'));
}

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(){}


function restartUserFormInformation(){
	initFormFields('formUserInformation');
}


function loadUserRegisterInBasket(){
	ajaxLoad(shop.url+"/ajaxTemplates/users/userRegister.cfm?checkout=1", $('basketContent'), "initUserInformation();initFormsUser();restartShadowbox();initAsyncBasket();reloadMiniBasket();");
}

function getProduct(eventKey, obj){
	ev = new Event(eventKey)
	if(ev.key == 'up' || ev.key == 'down' || ev.key == 'left' || ev.key == 'right'){
	
	}else{
		showProductDescription(obj);
	}
}

function showProductDescription(obj){
	
	var responseText = ajaxLoadSync(shop.url+'/ajaxTemplates/products/getProductDescription.cfm?sku='+obj.value);
	var objResponse = eval('(' + responseText + ')');
	
	var index = obj.getAttribute('indexItem');
	document.getElementById('budgetSpecialContributionContentDescription'+index).innerHTML = objResponse.DESCRIPTION;
}

function writeFileName(){
	document.getElementById('fileName').value = document.getElementById('notFindPhotoField').value;
}

function contentToShadowbox(obj){
	var childs = obj.getElementsByTagName('img');
	var img = childs[0];
	var width = img.width;
	var height = img.height;
	Shadowbox.open({player:'html',	
										content: obj.innerHTML, width:width, height:height});
}

$(window).addEvent('domready', function(){initShop(); initProduct();});
//$(window).addEvent('click', function(){hideSubCategories()});

function validateAccount(req){
	var objResponse = eval('(' + req.xhRequest.responseText + ')');
	if( objResponse.ERROR ){
		switch (objResponse.ERROR){
			case 'TLG1000':
			case 'TLG1001':shop.cuteAlert.addMessage(languageSheet.lblError, languageSheet.lblUserNotFound,0,0,messageWidth,messageHeight);break;
			case 'TLG1002':shop.cuteAlert.addMessage(languageSheet.lblError, languageSheet.lblDifferentPassword,0,0,messageWidth,messageHeight);break;
			case 'TLG1003':shop.cuteAlert.addMessage(languageSheet.lblError, languageSheet.lblEmailInUse,0,0,messageWidth,messageHeight);break;
			case 'TLG1004':shop.cuteAlert.addMessage(languageSheet.lblError, languageSheet.lblDifferentEmail,0,0,messageWidth,messageHeight);break;
		}
	}else{
		if(objResponse.ACTION == 'customer') {
			if(window.location.href.search('users/user') != -1){window.location.href=shop.url+'/bienvenida';} //userControlPanel o user.cfm
			else{ loadBasketStep(3, true); reloadSmallLogin(); generateNewCalendar(); } //basket.cfm
		}
		if(objResponse.ACTION == 'login') {
			if(window.location.href.search('users/user') != -1){window.location.href=shop.url;} //userControlPanel o user.cfm
			else{ loadBasketStep(2); reloadSmallLogin(); } //userInformation.cfm
		}
	}
}

