function addToBasket(product_id)
{
	new Ajax.Request(base_path+'/cart/add', {
	  method: 'post',
	  parameters: {id: product_id, ajax:true},
	  onSuccess: function(transport, json)
	  {
	  	var data = transport.responseText.evalJSON();
		if (!data['available']) {
			$('tip' + data['id']).addClassName('out_of_stock');
			$('tip' + data['id']).setStyle({backgroundColor: '#CCCCCC'});
	  		$('tip' + data['id']).setStyle({color: '#999999'});
			$('tip' + data['id']).onclick = function(){return false;}
			eval('if(prototip_'+data['id']+'){prototip_'+data['id']+'.content = "Out of Stock";prototip_'+data['id']+'.tip.innerHTML = "Out of Stock<div style=\'clear:both;\'></div>"}');
		}
	  	$('quick-basket').insert({top:data['content']});
	  	$('quick-basket-container-main').show();
	  	$('basket-price').update(data['cost']);
	  	$('basket-count').update(data['count']);
		$('checkout-button').show();
		var showCount = 0;
		var hideCount = 0;
		new PeriodicalExecuter(function(pe) {
		  if (++hideCount > 4)
		  {
		    pe.stop();
		  }
		  else
		  {
		    $('qbasket-item-'+data['id']).setStyle({  backgroundColor: '#fff'});
		  }
		}, 0.8);
		new PeriodicalExecuter(function(pe2) {
		new PeriodicalExecuter(function(pe) {
		  if (++showCount > 4)
		  {
		    pe.stop();
		  }
		  else
		  {
		    $('qbasket-item-'+data['id']).setStyle({  backgroundColor: '#FEE5E8'});
		  }
		}, 0.8);
		pe2.stop();
	  } , 0.4);
	  $('addToBasketTipSize').update(data['size']);
	  $('addToBasketTipCode').update(data['code']);
	  $('addToBasketTip').setStyle({  display: 'block'});
	  new PeriodicalExecuter(function(pe) {
	  	//$('addToBasketTip').setStyle({  display: 'none'});
	  	pe.stop();
	  }, 3);
	  }
	  });
	  $$('div.size a').each(function(el){
		  if (!el.hasClassName('out_of_stock'))
		  {
		    el.setStyle({  color: '#ED1C24'})
		    el.setStyle({  backgroundColor: '#ffffff'})
		  }
	  });
	  if (!$('tip' + product_id).hasClassName('out_of_stock')) {
	  	$('tip' + product_id).setStyle({backgroundColor: '#ED1C24'});
	  	$('tip' + product_id).setStyle({color: '#ffffff'});
	  }
	 return false;
}

function AvailableProduct(id)
{
	var select = $('size');
	if(select)
	{
		for(var i = 0; i < select.options.length; i++)
		{
			if(select.options[i].value == id)
			{
				if(select.options[i].text.indexOf("Out of stock") == -1)
					return true;
				else
					return false;
			}
		}
	}
}

function empty(val)
{
	if ((0 === val) || (null === val) || ('' === val)) return true;
	return false;
}

function isNumeric(val)
{
  if ((null != val) && !val.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

var isBasketErr = false;

function addToBasketNew(product_id, quantity, redirect_url, cnt)
{
	if (isBasketErr)
	{
		$('trError').addClassName('x-hidden');
		$('divSize').setStyle({backgroundColor: '#ededed'});
		$('divSizeErr').addClassName('x-hidden');
		$('sizeErrEmpty').addClassName('x-hidden');
		$('sizeErrOutOfStock').addClassName('x-hidden');
		$('divQuantity').setStyle({backgroundColor: '#ededed'});
		$('divQuantityErr').addClassName('x-hidden');
	}

	if (empty(product_id) || !AvailableProduct(product_id) || (empty(quantity) || (0 >= quantity) || !isNumeric(quantity)))
	{
		$('trError').removeClassName('x-hidden');
		isBasketErr = true;
		if (empty(product_id))
		{
			$('divSizeErr').removeClassName('x-hidden');
			$('divSize').setStyle({backgroundColor: '#EAC9C8'});
			$('sizeErrEmpty').removeClassName('x-hidden');
		}
		else if (!AvailableProduct(product_id))
		{
			$('divSizeErr').removeClassName('x-hidden');
			$('divSize').setStyle({backgroundColor: '#EAC9C8'});
			$('sizeErrOutOfStock').removeClassName('x-hidden');
		}

		if (empty(quantity) || (0 >= quantity) || !isNumeric(quantity))
		{
			$('divQuantityErr').removeClassName('x-hidden');
			$('divQuantity').setStyle({backgroundColor: '#EAC9C8'});
		}
		return false;
	}

	var cntr = $(cnt);
	cntr.startWaiting('bigWaiting');

	new Ajax.Request(base_path + '/cart/add', {
		method: 'post',
		parameters: {id: product_id, quantity: quantity, ajax: true},
		onFailure: function(cntr) {
			cntr.stopWaiting();
		}.bind(this, cntr),
		onSuccess: function(cntr, redirect_url, transport, json)
		{
		  	var data = transport.responseText.evalJSON();
			if (!data['banned'])
			{
				if (!data['available'])
				{
				 	resetSizeSelect(data['id'], 'Out of stock');
				}
				if (redirect_url)
				{
					window.location.href = redirect_url;
				}
			}
			cntr.stopWaiting();
		}.bind(this, cntr, redirect_url)
	});

	return false;
}

function resetSizeSelect(id, status)
{
	var select = $('size');
	if(select)
	{
		for(var i=0; i < select.options.length;i++)
		{
			if(select.options[i].value == id)
			{
				var index = select.options[i].text.indexOf(' - ');
				var temp_Str = select.options[i].text.substr( 0, index+3) + status;
				select.options[i].text = temp_Str;
				return true;
			}
		}
	}

}

function deleteBasketItem(product_id)
{
	var cont = $('qbasket-item-' + product_id);
	cont.startWaiting('waiting');

	new Ajax.Request(base_path + '/cart/delete', {
		method: 'post',
		parameters: {id: product_id},
		onFailure: function(cont) {
			cont.stopWaiting();
		}.bind(this, cont),
		onSuccess: function(cont, transport, json) {
			var data = transport.responseText.evalJSON();
			if (data['available'])
			{
				resetSizeSelect(data['id'], 'In stock');
				/*
				$('tip' + data['id']).removeClassName('out_of_stock');
				$('tip' + data['id']).setStyle({backgroundColor: '#ED1C24'});
				$('tip' + data['id']).setStyle({color: '#FFFFFF'});
				$('tip' + data['id']).onclick = function(){return addToBasket(this);}.bind(data['id']);
				eval('if(prototip_'+data['id']+'){prototip_'+data['id']+'.content = "Add to basket";prototip_'+data['id']+'.tip.innerHTML = "Add to basket<div style=\'clear:both;\'></div>"}');
				*/
			}
			$('qbasket-item-'+data['id']).remove();

			if ($('quick-basket').empty())
			{
				$('quick-basket-container-main').hide();
//				$('checkout-button').hide();
			}
			$('basket-price').update(data['cost']);
			$('basket-count').update(data['count']);
			/*
			$('tip'+data['id']).setStyle({  backgroundColor: '#ffffff'});
			$('tip'+data['id']).setStyle({  color: '#ED1C24'});
			*/
			cont.stopWaiting();
		}.bind(this, cont)
	});
	return false;
}

function UserResolution()
{

	if (!getCookie("treds_user_resolution")) {
		var height = 0;
		var width = 0;

		if (self.screen) { // for NN4 and IE4
			width = screen.width
			height = screen.height
		}
		else
			if (self.java) { // for NN3 with enabled Java
				var jkit = java.awt.Toolkit.getDefaultToolkit();
				var scrsize = jkit.getScreenSize();
				width = scrsize.width;
				height = scrsize.height;
			}
		setCookie("treds_user_resolution", width+'x'+height, null, null, null, false);
	}

}
function setCookie(name, value, expires, path, domain, secure)
{
	var cookie=name+"="+escape(value);
    if (expires) {
        cookie+="; expires="+expires.toGMTString();
    }
    if (path) {
        cookie+="; path="+path;
    }
	else
	{
		cookie+="; path=/";
	}
    if (domain) {
        cookie+="; domain="+domain;
    }

    if (secure) {
            cookie+="; secure";
    }
    document.cookie=cookie;
}
function deleteCookie(name)
{
	setCookie(name,'null',new Date(0),null,null,false);
}
function getCookie( name )
{
    var prefix = name + "="
    var cookieStartIndex = document.cookie.indexOf(prefix)
    if (cookieStartIndex == -1)
            return null
    var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
    if (cookieEndIndex == -1)
            cookieEndIndex = document.cookie.length
    return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}
function changeHeight(cusHeight)
{
	document.getElementById('ifrBlog').style.height = cusHeight + 'px';
}

//Event.observe(document, 'dom:loaded', UserResolution.bindAsEventListener() );