var Shop 						= {};
Shop.Version 				= 1;
Shop.LockedSubmit		= false;


Shop.LockSubmit = function() {
	this.LockedSubmit		= true;
}

Shop.UnlockSubmit = function() {
	this.LockedSubmit		= false;
}

Shop.AllowSubmit = function() {
	return !this.LockedSubmit;
}

Shop.Recalculate = function () {
	$('.OrderButton').attr('disabled','disabled');
	var quantities = [];
	var products = []; 
	var form		= document.forms.ShopForm;
	form.ShopAction.value	= "Recalculate";
	jQuery.each($('.OrderQuantity'), function(){
		quantities.push(this.value);
		products.push($(this).attr('name'));
	});
	form.IdsProducts.value = products.join();
	form.Quantities.value = quantities.join();
	form.submit();
}

Shop.GoToCheckoutPhase = function(Phase) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "GoToCheckoutPhase";
		form.Phase.value									= Phase;
		form.submit();		
	}
}

Shop.ConfirmOrderAndGoToPayment = function(AllowPartialDelivery, UserRemark) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "ConfirmOrderAndGoToPayment";
		form.AllowPartialDelivery.value		= AllowPartialDelivery;
		form.UserRemark.value							= UserRemark;
		form.submit();
	}
}

Shop.ConfirmOrderAndGoToPaymentOnAccount = function(AllowPartialDelivery, UserRemark) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "ConfirmOrderAndGoToPayment";
		form.PaymentOnAccount.value				= 1;
		form.AllowPartialDelivery.value		= AllowPartialDelivery;
		form.UserRemark.value							= UserRemark;
		form.submit();
	}
}

Shop.ConfirmFreeOrder = function(AllowPartialDelivery, UserRemark) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "ConfirmFreeOrder";
		form.AllowPartialDelivery.value		= AllowPartialDelivery;
		form.UserRemark.value							= UserRemark;
		form.submit();
	}
}

Shop.ConfirmQuotationOrderAndGoToPayment = function(IdQuotation, AllowPartialDelivery, UserRemark) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "ConfirmQuotationOrderAndGoToPayment";
		form.IdQuotation.value						= IdQuotation;
		form.AllowPartialDelivery.value		= AllowPartialDelivery;
		form.UserRemark.value							= UserRemark;
		form.submit();
	}
}

Shop.ConfirmQuotationOrderAndGoToPaymentOnAccount = function(IdQuotation, AllowPartialDelivery, UserRemark) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "ConfirmQuotationOrderAndGoToPayment";
		form.IdQuotation.value						= IdQuotation;
		form.PaymentOnAccount.value				= 1;
		form.AllowPartialDelivery.value		= AllowPartialDelivery;
		form.UserRemark.value							= UserRemark;
		form.submit();
	}
}

Shop.RejectQuotation = function(IdQuotation, UserComment) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "RejectQuotation";
		form.IdQuotation.value						= IdQuotation;
		form.UserComment.value						= UserComment;
		form.submit();
	}
}

Shop.ConfirmOrderAndRequestQuotation = function(UserComment) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "ConfirmOrderAndRequestQuotation";
		form.UserComment.value						= UserComment;
		form.submit();
	}
}

Shop.ConfirmPaymentOnAccount = function() {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "ConfirmPaymentOnAccount";
		form.submit();
	}
}

Shop.CancelPayment = function() {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "CancelPayment";
		form.submit();
	}
}

Shop.AddToBasket = function(IdProduct, IdFolder, ProductUrl) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "AddToBasket";
		form.IdProduct.value							= IdProduct;
		form.IdFolder.value								= IdFolder;
		form.ProductUrl.value							= ProductUrl;
		form.ContinueShoppingUrl.value		= window.location.href;
		form.submit();
	}
}


Shop.AddArticleNrToBasket = function(SourceFormName, InvalidNrMessage) {
	var SourceForm		= document.forms[SourceFormName];
	var form					= document.forms.ShopForm;
	var ArticleNr;
	var Quantity;
	
	if(SourceForm && form && this.AllowSubmit()) {
		this.LockSubmit();
		
		ArticleNr 			= SourceForm.ArticleNr.value	= SourceForm.ArticleNr.value.replace(/ /, "");
		Quantity 				= SourceForm.Quantity.value		= SourceForm.Quantity.value.replace(/ /, "");
		
		if(ArticleNr.length) {
			if(/^[0-9]+$/.test(Quantity) && parseInt(Quantity, 10) > 0) {

				form.ShopAction.value							= "AddArticleNrToBasket";
				form.ArticleNr.value							= ArticleNr;
				form.Quantity.value								= Quantity;
				form.ContinueShoppingUrl.value		= window.location.href;
				form.submit();

			} else {
				alert(InvalidNrMessage + " " + Quantity);
				this.UnlockSubmit();
				return;
			}
		}
	}
}

Shop.AddMultipleToBasket = function(SourceFormName, InvalidNrMessage, ComponentSolution) {
	var SourceForm		= document.forms[SourceFormName];
	var form					= document.forms.ShopForm;
	var IdsProducts		= [];
	var Quantities		= [];
	var element;
	var quantity;
	
	if(SourceForm && form && this.AllowSubmit()) {
		this.LockSubmit();
		
		for(var i=0; i<SourceForm.elements.length; i++) {
			element				= SourceForm.elements[i];
			element.value = element.value.replace(/ /, "");
			if(/Quantity_[0-9]+/.test(element.name) && element.value.length) {
				if(/^[0-9]+$/.test(element.value) && parseInt(element.value, 10) > 0) {
					IdsProducts.push(element.name.replace(/Quantity_/, ""));
					Quantities.push(element.value);
				} else {
					alert(InvalidNrMessage + " " + element.value);
					this.UnlockSubmit();
					return;
				}
			}
		}
		
		if(IdsProducts.length) {
		
			form.ShopAction.value							= "AddMultipleToBasket";
			form.IdsProducts.value						= IdsProducts.join(",");
			form.Quantities.value							= Quantities.join(",");
			form.ContinueShoppingUrl.value		= window.location.href;
			form.submit();	
			
		} else {
			this.UnlockSubmit();
		}
	}
}

Shop.RemoveFromBasket = function(IdProduct) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value		= "RemoveFromBasket";
		form.IdProduct.value		= IdProduct;
		form.submit();
	}
}

Shop.SetItemQuantity = function(IdProduct, PreferredQuantity) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value		= "SetItemQuantity";
		form.IdProduct.value		= IdProduct;
		form.Quantity.value			= PreferredQuantity;
		form.submit();
	}
}

Shop.AddItemGiftwrap = function(IdProduct) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value		= "AddItemGiftwrap";
		form.IdProduct.value		= IdProduct;
		form.submit();
	}
}

Shop.RemoveItemGiftwrap = function(IdProduct) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value		= "RemoveItemGiftwrap";
		form.IdProduct.value		= IdProduct;
		form.submit();
	}
}


Shop.KeywordFormat = function(Keywords) {
	var Result		= Keywords.replace(/ +$/, "").replace(/^ +/, "").replace(/ +/, " ");
	var array			= Result.split(" ");
	for(var i in array) {
		array[i]		= encodeURIComponent(array[i]);
	}
	return array.join("+");
}


Shop.ToggleFilterSection = function(id) {
	toggleElement('FilterOptionsMin_' + id);
	$('#FilterOptionsMax_' + id).slideToggle('slow');
}

Shop.DoSearch = function(FormName, BaseUrl) {
	pageTracker._trackEvent('Shop', 'Search for keywords', document.forms[FormName].q.value, 0);
	window.location	= BaseUrl + '/' + Shop.KeywordFormat(document.forms[FormName].q.value);
	return false;
}
Shop.ClearSearch = function(FormName) {
	document.forms[FormName].q.value = "";
	Shop.UpdateSearch(FormName);
}				
Shop.UpdateSearch = function(FormName) {
	document.forms[FormName].q.value.replace(/^\s*/, "").replace(/\s*$/, "");
	document.getElementById("SearchClearButton_" + FormName).style.display =
		(document.forms[FormName].q.value == "" ? "none" : "block");
}	


Shop.ClickOut = function(BaseUrl, Url, Params, Type, Label, Detail, Value) {
	pageTracker._trackEvent((Type || 'Shop')
												, (Label || 'Clickout')
												, (Detail || 'Default')
												, (Value || 0));
	window.open(BaseUrl + '/Clickout/?' + (Params != '' ? Params + '&' : '') + 'Target=' + escape(Url));
}


Shop.ActivatePromoCode = function(Code) {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "ActivatePromoCode";
		form.PromoCode.value							= Code;
		form.submit();
	}
}


Shop.RemovePromoCode = function() {
	var form		= document.forms.ShopForm;
	if(form && this.AllowSubmit()) {
		this.LockSubmit();
		form.ShopAction.value							= "RemovePromoCode";
		form.submit();
	}
}


Shop.OnEditAddress = function(formname, prefix) {
	var form						= document.forms[formname];
	var Street					= form[prefix + "Street"];
	var StreetNr				= form[prefix + "StreetNr"];
	var StreetNrExt			= form[prefix + "StreetNrExt"];
	var PostBoxNr				= form[prefix + "PostBoxNr"];
	if(PostBoxNr && (Street || StreetNr || StreetNrExt)) {
		var AllowStreet			= PostBoxNr.value == "";
		var AllowPostBoxNr	= (!Street 			|| Street.value == "") &&
													(!StreetNr		|| StreetNr.value == "") &&
													(!StreetNrExt	|| StreetNrExt.value == "");
		
		if(!AllowStreet && !AllowPostBoxNr) {
			AllowStreet 			= true;
		}

		if(AllowStreet) {
			if(Street) {
				Street.disabled 		=  false;
				if(document.getElementById(formname + '_' + prefix + 'Street_Required')) document.getElementById(formname + '_' + prefix + 'Street_Required').style.display = 'inline';
			}
			if(StreetNr) {
				StreetNr.disabled 	=  false;
				if(document.getElementById(formname + '_' + prefix + 'StreetNr_Required')) document.getElementById(formname + '_' + prefix + 'StreetNr_Required').style.display = 'inline';
			}
			if(StreetNrExt) {
				StreetNrExt.disabled 	=  false;
				if(document.getElementById(formname + '_' + prefix + 'StreetNrExt_Required')) document.getElementById(formname + '_' + prefix + 'StreetNrExt_Required').style.display = 'inline';
			}
		} else {
			if(Street) {
				Street.disabled 		=  true;
				Street.value 				= "";
				if(document.getElementById(formname + '_' + prefix + 'Street_Required')) document.getElementById(formname + '_' + prefix + 'Street_Required').style.display = 'none';
			}
			if(StreetNr) {
				StreetNr.disabled 	=  true;
				StreetNr.value 			= "";
				if(document.getElementById(formname + '_' + prefix + 'StreetNr_Required')) document.getElementById(formname + '_' + prefix + 'StreetNr_Required').style.display = 'none';
			}
			if(StreetNrExt) {
				StreetNrExt.disabled 	=  true;
				StreetNrExt.value 	= "";
				if(document.getElementById(formname + '_' + prefix + 'StreetNrExt_Required')) document.getElementById(formname + '_' + prefix + 'StreetNrExt_Required').style.display = 'none';
			}
		}

		if(AllowPostBoxNr) {
			if(PostBoxNr) {
				PostBoxNr.disabled 		=  false;
				if(document.getElementById(formname + '_' + prefix + 'PostBoxNr_Required')) document.getElementById(formname + '_' + prefix + 'PostBoxNr_Required').style.display = 'inline';
			}
		} else {
			if(PostBoxNr) {
				PostBoxNr.disabled 		=  true;
				PostBoxNr.value 		= "";
				if(document.getElementById(formname + '_' + prefix + 'PostBoxNr_Required')) document.getElementById(formname + '_' + prefix + 'PostBoxNr_Required').style.display = 'none';
			}
		}
	}
}


Shop.ProductCompare = function(uri, text) {
  var b 						= this;
  this.uri 					= uri + "/c/";
	this.text					= text;
  this.Products 		= { length: 0 };
	this.MaxProducts	= 3;
  this.CheckBoxes 	= $("input.checkbox.Compare");
  $.each(this.CheckBoxes, function() {
    $(this).removeAttr("disabled");
    $(this).removeAttr("checked");
    $(this).click(function() {
      b.AddRemoveProduct($(this));
    })
  });
  $.each($("a.CompareButton.Link"), function() {
    $(this).click(function() {
      b.Compare($(this));
    })
  })
};

Shop.ProductCompare.prototype = {
	Compare:function() {
	  var a, b = [];
	  if(this.Products.length > 1) {
	    for(a in this.Products)
				this.Products.hasOwnProperty(a) && a != "length" && b.push(a);
	    document.location.href = this.uri + b.join() + '/' + this.text.UrlSeo;
	  } else {
			alert(this.text.AtLeastTwo);
		}
	}, AddRemoveProduct:function(a) {
  	if($(a).attr("checked")) {
    	if(this.Products.length < this.MaxProducts) {
	      this.Products[$(a).val()] = true;
  	    this.Products.length++;
    	  this.Products.length == this.MaxProducts && this.DisableAll()
	    }
		}else if(!$(a).attr("checked")) {
			delete this.Products[$(a).val()];
			this.Products.length--;
			this.Products.length == this.MaxProducts - 1 && this.EnableAll()
		}
		this.SwitchClones(a);
	}, DisableAll:function() {
		$.each($("input.checkbox.Compare:enabled"), function() {
			!$(this).attr("checked") && $(this).attr("disabled", true)
		});
	}, EnableAll:function() {
		$.each($("input.checkbox.Compare:disabled"), function() {
			$(this).removeAttr("disabled")
		});
	}, SwitchClones:function(a) {
		$.each(this.CheckBoxes, function() {
			if($(this).attr("id") != $(a).attr("id") && $(this).val() == $(a).val())
				$(this).attr("disabled") ? $(this).removeAttr("disabled") : $(this).attr("disabled", true);
		});
	}
};
