﻿function bc(obj)
{
	obj.form.submit();
}

function InitMain()
{
	if(!document.getElementById) return;
	
	var totbk = document.getElementById('toTBK');
	if (totbk != null)
	{
	    totbk.onsubmit = function ()
	    {
	        var cat = document.getElementById('backToCatalog');
	        if (cat != null)
	        {
	            if (cat.onsubmit()) cat.submit();
	            return false;	        
	        }
	        else if (window == top)
	        {
	            alert('Öppna tillbehörskatalogen i samma fönster som Bildelskatalogen innan du återgår.');
	            return false;
	        }
	        else
	        {
	            return true;
	        }
	    };
		var logo = document.getElementById('logo');
		if (logo != null)
		{
			logo.onclick = function () { 
					var totbk = document.getElementById('toTBK');
					if (totbk.onsubmit()) totbk.submit();
				}
			logo.style.cursor = 'pointer';
		}
	}
	var cat = document.getElementById('backToCatalog');
	if (cat != null)
	{
	    cat.onsubmit = function ()
	    {
	        if (window == top)
	        {
	            alert('Öppna tillbehörskatalogen i samma fönster som Bildelskatalogen innan du tar med din varukorg tillbaka Bildelskatalogen.');
	            return false;
	        }
	        else
	        {
	            return true;
	        }
	    };
	}
	
	var box = document.getElementById('search');
	box.onfocus = searchBox_Enter;
	box.onblur = searchBox_Leave;
	if(box.value == '' || box.value == searchtext)
	{
		box.value = searchtext;
		box.style.fontStyle = 'italic';
		box.style.fontWeight = 'bold';
		box.style.color = '#999999';
	}
	
	box = document.getElementById('section');
	box.isOver = false;
	box.onchange = dropBox_Selected;
	box.onfocus = dropBox_Enter;
	box.onblur = dropBox_Leave;
	box.setStyle = dropBox_setStyle;
	box.setStyle();
	
	box = document.getElementById('brand');
	box.isOver = false;
	box.onchange = dropBox_Selected;
	box.onfocus = dropBox_Enter;
	box.onblur = dropBox_Leave;
	box.setStyle = dropBox_setStyle;
	box.setStyle();
	
	var basket = document.getElementById('basket');
	if (basket != null)
	{
		var els = basket.getElementsByTagName('a');
		for(var i = 0; i < els.length; i++)
		{
			var a = els[i];
			var el = a.parentNode.parentNode;
			el.alternate = el.className == 'a';
			el.style.cursor = 'pointer';
			el.url = a.href;
			var elsb = el.getElementsByTagName('td');
			for(var b = 0; b < elsb.length; b++)
			{
				var elb = elsb[b];
				if(elb.className == 'c')
				{
					elb.firstChild.onfocus = function () { this.className = 'f'; this.select(); };
					elb.firstChild.onblur = function () { this.className = ''; };
				}
				else if(elb.className != 'd')
					elb.onclick = function () { window.location = this.parentNode.url; };
				elb.onmouseover = function () { this.parentNode.className = 'hover'; };
				elb.onmouseout = function () { this.parentNode.className = (this.parentNode.alternate) ? 'a' : ''; };
			}
		}
	
		var AJAX_SUPPORT = false;
		try	{ AJAX_SUPPORT = (YAHOO.util.Connect.createXhrObject(0) != null); } catch (e) { }
	
		if (AJAX_SUPPORT)
		{
			var total = YAHOO.util.Dom.getElementsByClassName('sum', 'div', 'basket')[0].getElementsByTagName('span')[0];
			els = YAHOO.util.Dom.getElementsByClassName('c', 'td', 'basket'); 
			for(var i = 0; i < els.length; i++)
			{
				var el = els[i];
				var inp = el.firstChild;
				inp.oldValue = inp.value;
				inp.onchange = changeCount;
				inp.price = el.previousSibling;
				inp.priceTotal = el.nextSibling;
				inp.sum = total;
			}
		}
	}
}

function changeCount()
{
	var val = this.value.replace(' ', '');
	if (val == '' || isNaN(val))
	{
		this.value = this.oldValue;
		return;
	}
	this.value = parseInt(val);
	var amountOnId = document.getElementById('amountOnId' + this.name.substr(3));
	if (amountOnId != null)
	    amountOnId.value = this.value;
	var callback = 
	{ 
	  success: changeSuccess, 
	  failure: changeFailure, 
	  timeout: 10000,
	  argument: this
	}
	YAHOO.util.Connect.asyncRequest('GET', './ajax/ChangeBasketCount.ashx?id=' + this.name.substr(3) + '&c=' + this.value, callback, null);
}
function changeSuccess(o)
{
	var res;
	try	{ res = eval('(' + o.responseText + ')'); } catch (e) { return changeFailure(o); }
	o.argument.oldValue = o.argument.value = res.count;
	o.argument.price.innerHTML = res.price;
	o.argument.priceTotal.innerHTML = res.priceTotal;
	o.argument.sum.innerHTML = res.priceSum;
	if (res.count == 0) { o.argument.form.submit(); return; }
}
function changeFailure(o)
{
	o.argument.value = o.argument.oldValue;
}
function dropBox_setStyle()
{
	if (this.selectedIndex > 0 || this.isOver)
	{
		this.style.fontStyle = 'normal';
		this.style.fontWeight = 'normal';
		this.style.color = '#000000';
	}
	else
	{
		this.style.fontStyle = 'italic';
		this.style.fontWeight = 'bold';
		this.style.color = '#999999';
	}
}

function dropBox_Selected(e)
{
	this.setStyle();
	this.form.submit();
}

function dropBox_Leave(e)
{
	this.isOver = false;
	this.setStyle();
}

function dropBox_Enter(e)
{
	this.isOver = true;
	this.setStyle();
}

function searchBox_Enter(e)
{
	var box = this;
	box.style.fontStyle = 'normal';
	box.style.fontWeight = 'normal';
	box.style.color = '#000000';
	if (box.value == searchtext) box.value = "";
}

function searchBox_Leave(e)
{
	var box = this;
	if (box.value == "")
	{
		box.value = searchtext;
		box.style.fontStyle = 'italic';
		box.style.fontWeight = 'bold';
		box.style.color = '#999999';
	}
}

YAHOO.util.Event.addListener(window, "load", InitMain);