Prototype.Browser.IE6=Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6;
Prototype.Browser.IE7=Prototype.Browser.IE && !Prototype.Browser.IE6;
function popWindow(url,w,h,scrollbars) 
{
	if(scrollbars) scrollbars = ",scrollbars=yes";
	else scrollbars = "";
	window.open(url, '',  'top=0,left=0,width='+w+',height='+h+scrollbars);
}
function isEmptyStr(strValue) 
{
   strValue = trimAll(strValue);
   if(strValue.length > 0){
     return false;
   }
   return true;
}
function check_all(id,form)
{
	var elements = $(form).getElementsByTagName('input');
	for (var i = 0; i < elements.length; i++)
	{
		var e = elements[i];
		if (e.id != id && e.type.toLowerCase() == 'checkbox' )
		{
			e.checked = $(id).checked;
		}
	}
}
function trimAll( strValue ) 
{
 var objRegExp = /^(\s*)$/;

    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}
var reloadWin = function (t)
{
	//alert(t.responseText);
	window.location.reload();
}
var OnAddToCart = function (t)
{
	//$('add_to_cart_txt').innerHTML = "Добави в кошницата";
	//$('cart_ico').src = '/images/cart_ico.gif';
	$('cart-buttons').style.display = 'block';
	ReloadCart();
}
var OnEditQuant = function (t)
{
	//$('add_to_cart_txt').innerHTML = "Добави в кошницата";
	//$('cart_ico').src = '/images/cart_ico.gif';
	$('cart-buttons').style.display = 'block';
	ReloadCart();
	var ret = t.responseText.split(',');
	$('cart-quant'+ret[1]).innerHTML = ret[0];
}
function edit_quantity(id,num)
{
	if((parseInt($('cart-quant'+id).innerHTML)+num) < 1) return false;
	new Ajax.Request('/products/add_to_cart', {method: 'post', onSuccess: OnEditQuant,  parameters: {id: id,quantity: num}});
}
function add_to_cart(id)
{
	hl_element = id;
	//$('add_to_cart_txt').innerHTML = "Моля изчакайте...";
	//$('cart_ico').src = '/images/move_cart.gif';
	new Ajax.Request('/products/add_to_cart', {method: 'post', onSuccess: OnAddToCart,  parameters: {id: id}});
}
var OnRemoveFromCart = function (t)
{
	if(t.responseText == 'empty') 
	{
		$('cart-content').innerHTML = "";
		$('cart-content').style.display = 'none';
		$('cart-buttons').style.display = 'none';
	}
}
function remove_from_cart(id)
{
	if(confirm("Сигурни ли сте, че искате да изтриете продукта?"))
	{
		if($('cart_item_'+id)) Element.Methods.remove($('cart_item_'+id));
		if($('cart_item_tr_'+id)) 
		{
			Element.Methods.remove($('cart_item_tr_'+id));
			recalculatePrice();
		}
		new Ajax.Request('/products/remove_from_cart', {method: 'post', onSuccess: OnRemoveFromCart,  parameters: {id: id}});
	}
}
var OnReloadCart = function (t)
{
	if(!isEmptyStr(t.responseText)) 
	{
		$('cart-content').innerHTML = t.responseText;
		if(hl_element != null)
		{
			new Effect.Highlight('cart_item_'+hl_element, {endcolor: '#F5F4F4'});
			hl_element = null;
			ShowHideCart();
		}
	}
	
}
var hl_element = null;
function ReloadCart()
{
	new Ajax.Request('/products/cart', {method: 'post', onSuccess: OnReloadCart});
}

function establishTopPosition() {
	var shoppingCartAisle = document.getElementById('shoppingCartAisle');
	var y = 0;
	while (shoppingCartAisle!=null) {
		y += shoppingCartAisle.offsetTop;
		shoppingCartAisle = shoppingCartAisle.offsetParent;
	}
	return y;
}
function pushOnIE6()
{
	shoppingCart.innerHTML = shoppingCart.innerHTML;
}
function addOnScroll() {

var htmlEl = document.getElementsByTagName('html')[0];
htmlEl.onscroll = function() { pushOnIE6(); }

}
function pushMyCart() {
	if(Prototype.Browser.IE6) return pushOnIE6();
	var shoppingCart = document.getElementById('shoppingCart');
	var topPos = establishTopPosition();
	if( window.XMLHttpRequest ) { // IE 6 doesn't implement position fixed nicely...
		if (document.documentElement.scrollTop > topPos  || self.pageYOffset > topPos) {
			shoppingCart.style.position = 'fixed';
		} else {
			shoppingCart.style.position = 'absolute';
		}
	}
}
function ShowCart()
{
	var e = $('cart-content');
	if(e.style.display == 'none') 
	{
		new Effect.BlindDown(e,{duration: 0.5});
		deleteCookie('hide-cart');
	}
	else 
	{
		new Effect.BlindUp(e,{duration: 0.5});
		setCookie('hide-cart',1);
	}
}
function ShowHideCart()
{
	if($('cart-content').style.display == 'none') 
	{
		new Effect.BlindDown($('cart-content'), { duration: 0.5,
							 afterFinish: function() 
							 {
								if(getCookie('hide-cart') == 1) setTimeout("new Effect.BlindUp($('cart-content'),{duration: 0.5})",2000);
							 }});
	}
}

function setCookie(name,value,days,path,domain,secure) {
	var expires, date;
	if(days>0) {
		date = new Date();
		date.setTime( date.getTime() + (days*86400) );
			expires = date.toGMTString();
	} else expires = 0;
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function getCookie(name) {
  var nameq = name + "=";
  var c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape(c.substring(nameq.length, c.length));
  }
  return null;
}

function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

var ProductSelector = Class.create();
ProductSelector.prototype = 
{
	initialize: function(name,url,item_id) 
	{
		this.name = name;
		this.url = url;
		this.item_id = item_id;
		var arr = $$('#'+this.name+'_in_list div');
		for(var i=0; i<arr.length; i++)
		{
			var ItemId = arr[i].id.replace(/[a-zA-Z]+/, '');
			this.addItem(ItemId);
			arr[i].ItemId = ItemId;
			arr[i].mainInstance = this;
			arr[i].onclick = this.onClickRemove;
		}
		this.search();
	},
	addItem: function(value)
	{
		var in_items = $('selected_'+this.name).value;
		if(in_items.length > 0) in_items += ","+value;
		else in_items = value;
		$('selected_'+this.name).value = in_items;
	},
	removeItem: function(value)
	{
		var in_items = $('selected_'+this.name).value.split(",");
		in_items = in_items.without(value);
		$('selected_'+this.name).value = in_items.join(",");
	},
	search: function()
	{
		new Ajax.Request(this.url, {method: 'post', onSuccess: this.doShowSearchedItems.bind(this),  parameters: {value: $(this.name).value, cat_id: $(this.name+'_cat_id').value, without: $('selected_'+this.name).value, item_id: this.item_id}});
	},
	doShowSearchedItems: function (t) 
	{
		$(this.name+'_auto_complete').innerHTML = t.responseText;
		
		var arr = $$('#'+this.name+'_auto_complete div');
		for(var i=0; i<arr.length; i++)
		{
			arr[i].mainInstance = this;
			arr[i].onclick = function()
			{
				var div = document.createElement('div');
				div.ItemId = this.id.replace(/[a-zA-Z]+/, '');
				this.mainInstance.addItem(div.ItemId);
				div.innerHTML = this.innerHTML;
				div.id = this.id;
				div.mainInstance = this.mainInstance;
				div.onclick = this.mainInstance.onClickRemove;
				$(this.mainInstance.name+'_in_list').appendChild(div);
				$(this.mainInstance.name+'_auto_complete').removeChild(this);
			}
		}
	},
	onClickRemove: function()
	{
		$(this.mainInstance.name+'_in_list').removeChild(this);
		this.mainInstance.removeItem(this.ItemId);
		this.mainInstance.search();
	}
}
function recalculatePrice()
{
	var elm = document.getElementsByClassName("cart-quant");
	var sum = 0;
	var sumPrice = 0;
	var q, shipping;
	var arrCuant = new Array();
	for(var i =0; i<elm.length; i++)
	{
		q = parseInt(elm[i].innerHTML);
		arrCuant[arrCuant.length] = q;
		sum += q;
	}
	var elm = document.getElementsByClassName("cart-price");
	for(var i =0; i<elm.length; i++)
	{
		q = parseFloat(elm[i].innerHTML);
		if(q>0) sumPrice += arrCuant[i]*q;
	}
	var min_no_shipping = parseFloat($('supply-w-tax').innerHTML);
	var shipping_tax = parseFloat($('supply-tax').innerHTML);
	if(sum>0) {
		$('count-all').innerHTML = sum;
		if(sumPrice>=min_no_shipping) shipping = 0;
		else shipping = shipping_tax;
		if(shipping == 0) 
		{
			$('tax-td').style.textDecoration = 'line-through';
			$('tax-td').style.color = '#FF4000';
		}
		else
		{
			$('tax-td').style.textDecoration = '';
			$('tax-td').style.color = '';
		}
		var price = sumPrice;
		price = Math.round(price*100)/100;
		$('price').innerHTML = price;
		$('price-total').innerHTML = Math.round((price+shipping)*100)/100;
	} else {
		//$('cartList').hide();
		//$('emptyMsg').style.display = 'block';
	}
}
function del_mybadget()
{
	if(confirm("Сигурни ли сте, че искате да изтриете данните?")) $('del_form').submit();
}