var timeout = 2000;
var switchDiscountbox_id;
var switchNewbox_id;
var switchBidbox_id;

function init()
{
	if (typeof disboxitemcount!='undefined' && disboxitemcount>0)
		switchDiscountbox_id = setInterval('switchDiscountbox()', timeout);
	if (typeof newboxitemcount!='undefined' && newboxitemcount>0)
		switchNewbox_id = setInterval('switchNewbox()', timeout);
	if (typeof bidboxitemcount!='undefined' && bidboxitemcount>0)
		switchBidbox_id = setInterval('switchBidbox()', timeout);

	ellipse();
	var elements = getElementsByClassName('ajaxForm');
	for (var i=0; i<elements.length; i++)
		elements[i].addEvent('submit', function(e) {
					new Event(e).stop();
					var wait = $('waitspinner');
					if (window.ie)
					{
						wait.style.left = window.getWidth()/2+'px';
						wait.style.top  = window.getHeight()/2+window.getScrollTop()+'px';
					}
					wait.style.display = '';
					this.send({
						onComplete: function() {
							wait.style.display = 'none';
						}
					});
				});
	if (typeof Shadowbox == 'undefined')
		return;
/*	if (window.ie)
		var shadowspinner = new Element('div', {
				'id':	'shadowspinner',
				'styles': {
					'position': 'absolute',
					'top':		'49%',
					'left':		'49%',
					'height':	'0px'}}); 
	else
		var shadowspinner = new Element('div', {
				'id':	'shadowspinner',
				'styles': {
					'position': 'fixed',
					'top':		'49%',
					'left':		'49%',
					'height':	'0px'}}); 
	shadowspinner.innerHTML = '<TABLE class="szurkekeret" cellpadding="0" cellspacing="2" bgcolor="#FFFFFF"><TR><TD><IMG src="images/spinner.gif" style="display:inline;" alt=""/></TD></TR></TABLE>';
	var conf =
	{
		animate:		false,
		onOpen:			function() { $('shadowspinner').style.display = ''; },
		onLoad:			'$(\'shadowspinner\').style.display = \'none\';',
	};
	Shadowbox.init(conf);
	$('shadowbox_body').appendChild(shadowspinner);*/
}

function storeInit()
{
	ellipse();
	var elements = getElementsByClassName('ajaxForm');
	for (var i=0; i<elements.length; i++)
		elements[i].addEvent('submit', function(e) {
					new Event(e).stop();
					var wait = $('waitspinner');
					wait.style.display = '';
					this.send({
						onComplete: function() {
							wait.style.display = 'none';
						}
					});
				});
/*	if (typeof Shadowbox == 'undefined')
		return;
	if (window.ie)
		var shadowspinner = new Element('div', {
				'id':	'shadowspinner',
				'styles': {
					'position': 'absolute',
					'top':		'49%',
					'left':		'49%',
					'height':	'0px'}}); 
	else
		var shadowspinner = new Element('div', {
				'id':	'shadowspinner',
				'styles': {
					'position': 'fixed',
					'top':		'49%',
					'left':		'49%',
					'height':	'0px'}}); 
	shadowspinner.innerHTML = '<TABLE class="szurkekeret" cellpadding="0" cellspacing="2" bgcolor="#FFFFFF"><TR><TD><IMG src="images/spinner.gif" style="display:inline;" alt=""/></TD></TR></TABLE>';
	var conf =
	{
		animate:		false,
		onOpen:			function() { $('shadowspinner').style.display = ''; },
		onLoad:			'$(\'shadowspinner\').style.display = \'none\';',
	};
	Shadowbox.init(conf);
	$('shadowbox_body').appendChild(shadowspinner);*/
}

function toggleCategory(cat_id, length)
{
	var img = document.getElementById('catimg_'+cat_id);
	img.src = ((img.src.indexOf('images/menunyil-.GIF')==-1)?('images/menunyil-.GIF'):('images/menunyil+.GIF'));
	if (length == 0)
		return;
    var display = ((img.src.indexOf('images/menunyil-.GIF')==-1)?('none'):(''));
    for (var i=0; i<length; i++)
	{
		try
		{
			document.getElementById('alkat_' + cat_id + '_' + i).style.display = display;
			document.getElementById('vonal_' + cat_id + '_' + i).style.display = display;
		}
		catch (ex)
		{}
    }
}

function refreshSearchpage(storeid, searchfor, minprice, maxprice, prodtype, sortby, pageno, itemperpage, catid, subcatid)
{
	var temp = window.location.href.split('searchfor=');
	window.location.href = temp[0]+'searchfor='+searchfor+'&sortby='+sortby+
									'&minprice='+minprice+'&maxprice='+maxprice+'&prodtype='+prodtype+
									'&catid='+catid+'&subcatid='+subcatid+
									'&pageno='+pageno+'&itemperpage='+itemperpage;
}

function ellipse()
{
	var elements = _getElementsByClassName('ellipse','','');
	for (var i=0;i<elements.length;i++)
	{	
		var span   = elements[i].firstChild;
		var text   = span.getAttribute('text2ellipse');
		var rownum = span.getAttribute('row2ellipse');
		span.innerHTML = ellipseText(span, text, rownum);
	}
}

function ellipseTextByWord(spanElement, text, lineNo)
{
	var returnValue = '';
	var width = spanElement.parentNode.style.width;
	width = parseInt(width.substr(0,width.length-2))-4;
	spanElement.style.whiteSpace = 'nowrap';
	var temp = spanElement.innerHTML;
	spanElement.innerHTML = text;
	if ((spanElement.offsetWidth && spanElement.offsetWidth>width) || (spanElement.clientWidth && spanElement.clientWidth>width))
	{
		words = text.split(" ");
		var k = 0;
		for (var i=1; i<=lineNo; i++)
		{
			spanElement.innerHTML = '';
			while (k < words.length)
			{
				var line = spanElement.innerHTML;
				spanElement.innerHTML += words[k] + ((i<lineNo)?(' '):('...'));
				if ((spanElement.offsetWidth && spanElement.offsetWidth>width) || (spanElement.clientWidth && spanElement.clientWidth>width))
				{
					spanElement.innerHTML = line + ((i<lineNo)?('<BR/>'):('...'));
					break;
				}
				spanElement.innerHTML = (k>0)?(line+' '+words[k++]):(words[k++]);
			}
			returnValue += spanElement.innerHTML;
		}
	}
	else
		returnValue = text;
	spanElement.innerHTML = temp;
	return returnValue;
}

function ellipseText(spanElement, text, lineNo)
{
	var returnValue = '';
	var width = spanElement.parentNode.style.width;
	width = parseInt(width.substr(0,width.length-2))-4;
	spanElement.style.whiteSpace = 'nowrap';
	var temp = spanElement.innerHTML;
	spanElement.innerHTML = text;
	if ((spanElement.offsetWidth && spanElement.offsetWidth>width) || (spanElement.clientWidth && spanElement.clientWidth>width))
	{
		words = text.split(" ");
		var k = 0;
		var i = 1;
		for (; i<lineNo; i++)
		{
			spanElement.innerHTML = '';
			while (k < words.length)
			{
				var line = spanElement.innerHTML;
				spanElement.innerHTML += words[k] + ((i<lineNo)?(' '):('...'));
				if ((spanElement.offsetWidth && spanElement.offsetWidth>width) || (spanElement.clientWidth && spanElement.clientWidth>width))
				{
					if (line)
						spanElement.innerHTML = line + ((i<lineNo)?('<BR/>'):('...'));
					else
						spanElement.innerHTML = '';
					break;
				}
				spanElement.innerHTML = (k>0)?(line+' '+words[k++]):(words[k++]);
			}
			returnValue += spanElement.innerHTML;
		}

		lastLine = words.slice(k).join(" ");
		var k = 0;
		spanElement.innerHTML = '';
		while (k < lastLine.length)
		{
			var line = spanElement.innerHTML;
			spanElement.innerHTML += lastLine.charAt(k) + ((i<lineNo)?(''):('...'));
			if ((spanElement.offsetWidth && spanElement.offsetWidth>width) || (spanElement.clientWidth && spanElement.clientWidth>width))
			{
				spanElement.innerHTML = line + ((i<lineNo)?('<BR/>'):('...'));
				break;
			}
			spanElement.innerHTML = line+lastLine.charAt(k++);
		}
		returnValue += spanElement.innerHTML;
	}
	else
		returnValue = text;
	spanElement.innerHTML = temp;
	return returnValue;
}

function putIntoBasket(id, name)
{
	var quantity = document.getElementById('quantity_'+id).value;
	if (quantity=='')
		return;
	var item = document.getElementById('itemquantity_'+id);
	if (item)
	{
		if (parseInt(quantity)>0)
		{
			item.firstChild.value = parseInt(item.firstChild.value) + parseInt(quantity);
			item.childNodes[1].innerHTML = parseInt(item.firstChild.value) + ' db';
		}
		return;
	}
	var TD1 = document.getElementById('blank_itemquantity');
	var newTD1 = TD1.cloneNode(true);
	newTD1.id = 'itemquantity_'+id;
	if (parseInt(quantity)<=0)
		quantity = '1';
	newTD1.firstChild.value = quantity;
	newTD1.childNodes[1].innerHTML = quantity + ' db';
	var TD2 = document.getElementById('blank_item');
	var newTD2 = TD2.cloneNode(true);
	newTD2.id = 'item_'+id;
	newTD2.firstChild.value = id;
	var newTR = document.createElement("TR");
	newTD1.style.display = '';
	newTD2.style.display = '';
	newTR.appendChild(newTD1);
	newTR.appendChild(newTD2);
	var itemTable = document.getElementById('basketbody');
	itemTable.appendChild(newTR);
	newTD2.childNodes[1].firstChild.innerHTML = ellipseText(newTD2.childNodes[1].firstChild, name, 3);
	if (TD2.childNodes[1].firstChild.innerHTML == '')
	{
		TD1.style.display = 'none';
		TD2.style.display = 'none';
	}
}

function switchTab(tab_2show, tab_2hide)
{
	document.getElementById(tab_2show+'_tab+').style.display = 'block';
	document.getElementById(tab_2show+'_tab-').style.display = 'none';
	document.getElementById(tab_2hide+'_tab+').style.display = 'none';
	document.getElementById(tab_2hide+'_tab-').style.display = 'block';
	document.getElementById(tab_2show+'_text').style.display = 'block';
	document.getElementById(tab_2hide+'_text').style.display = 'none';
}



function verifyDigit(e, onTrueIE, onTrueFF)
{
	var keyCode;
	if (window.event && (keyCode=e.keyCode)==13) // IE
	{
		if (onTrueIE)
		{
			eval(onTrueIE);
			return false;
		}
	}
	else if (e.which && (keyCode=e.which)==13)   // Netscape/Firefox/Opera
	{
		if (onTrueFF)
		{
			eval(onTrueFF);
			return false;
		}
	}
	else
	{
		if ((typeof keyCode=='undefined') || keyCode==8) //backspace
			return;
		keyChar = String.fromCharCode(keyCode);
		numcheck = /\d/;
		return numcheck.test(keyChar);
	}
}

function gotospecificpage(url, delay_ms)
{
	if (delay_ms > 0)
	{
		setTimeout('gotospecificpage("'+url+'",0)', delay_ms);
		return;
	}
    window.location.href = url;
}

function getElementsByClassName(classname)
{
	return window.ie?$$('.'+classname):_getElementsByClassName(classname,'','');
}

function _getElementsByClassName(strClass, strTag, objContElm)
{
	strTag = strTag || "*";
	objContElm = objContElm || document;
	var objColl = objContElm.getElementsByTagName(strTag);
	if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
	var arr = new Array();
	var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
	var arrClass = strClass.split(delim);
	for (var i = 0, j = objColl.length; i < j; i++)
	{
		var arrObjClass = objColl[i].className.split(' ');
		if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
		var c = 0;
		comparisonLoop:
		for (var k = 0, l = arrObjClass.length; k < l; k++)
			for (var m = 0, n = arrClass.length; m < n; m++)
			{
				if (arrClass[m] == arrObjClass[k]) c++;
		        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length))
				{
					arr.push(objColl[i]);
					break comparisonLoop;
				}
			}
	}
	return arr;
}


var AIM = {
    frame : function(c)
	{

        var n = 'f' + Math.floor(Math.random() * 99999);
        var d = document.createElement('DIV');
        d.innerHTML = '';
        document.body.appendChild(d);

        var i = document.getElementById(n);
        if (c && typeof(c.onComplete) == 'function')
            i.onComplete = c.onComplete;
        return n;
    },

    form : function(f, name)
	{
        f.setAttribute('target', name);
    },

    submit : function(f, c)
	{
        AIM.form(f, AIM.frame(c));
        if (c && typeof(c.onStart) == 'function')
            return c.onStart();
        else
            return true;
    },

    loaded : function(id)
	{
        var i = document.getElementById(id);
        if (typeof(i.onComplete) == 'function')
            i.onComplete();
    }

}

function completeCallback()
{
	$('preview').src = 'tools/getpic.php?preview='+(Math.floor(Math.random() * 99999)+1);
	$('preview').style.display = '';
	if ($('previewtext'))
	{
		$('previewtext').style.display = 'none';
		if (window.ie)
			$('uploaddiv').style.top = '-135px';
	}
}

function startCallback()
{
	$('waitspinner').style.display = '';
	$('picdiv').style.display = 'none';
	return true;
}

function previewOnLoad()
{
	$('picdiv').style.display = '';
	$('waitspinner').style.display = 'none';
}

function delay(millis)
{
	if (millis>0)
		setTimeout('delay(0)', millis);
}

function refreshSubcategories(catSELECT, subcatSELECT, preselected)
{
	var index = catSELECT.selectedIndex;
	var c = subcatSELECT.options.length;
	for (var i=c; i>1; i--)
	   subcatSELECT.remove(i-1);
	var subcategories = alkategoriak[index];
	if (subcategories.length==0)
	   return;
	for (i=0; i<subcategories.length; i++)
	{
	   y = document.createElement('option');
	   y.value = subcategories[i][0];
	   y.text = subcategories[i][1];
	   if (y.value==preselected)
		   y.selected = true;
	   try { subcatSELECT.add(y, null); }
	   catch (ex) { subcatSELECT.add(y); }  // IE only
	}
}

function categorySelectSubmit()
{
	var catnode = $('catSELECT');
	var subcatnode = $('subcatSELECT');
	if (subcatnode.value)
	   $('catSELECT').value = '';
	var popupbrowse = $('popupBrowse');
	var popupform = $('popupForm');
	if (popupbrowse && popupbrowse.value) //csoportos termékfeltöltés
	   popupform.action += '?pageid=7&searchfor=&minprice=&maxprice=&prodtype=*';
	else if (popupbrowse)
	   popupform.action += '?pageid=14';
	else
	   popupform.action += '?pageid=14&bid=1';
//	popupform.style.display = 'none';
//	window.parent.document.getElementById('waitspinner').style.display = '';
}

function removeFromBoxes(id)
{
	for (i=0; typeof disboxitemcount!='undefined' && i<disboxitemcount; i++)
	{
		index = -1;
		if ($('discountbox_'+i).getAttribute('name')==id)
		{
			index = i;
			for (j=0; index>-1 && j<disboxitemcount; j++)
			{
				$('disboxanchor_'+j+'_'+index).style.display = 'none';
				for (var k=index; k<disboxitemcount; k++)
					$('disboxanchor_'+j+'_'+k).innerHTML = parseInt($('disboxanchor_'+j+'_'+k).innerHTML)-1;
			}
			$('discountbox_'+i).setAttribute('name','');
			_disboxitemcount--;
		}
		if (index>-1 && _disboxitemcount==0)		//az utolso kiemelt elemet is toroljuk
			$('discountbox_'+index).style.display = 'none';
		else if (index>-1 && disboxindex==index)	//eppen a torolt elem volt megjelenitve
			switchDiscountbox(index+1);
	}

	for (i=0; typeof newboxitemcount!='undefined' && i<newboxitemcount; i++)
	{
		index = -1;
		if ($('newbox_'+i).getAttribute('name')==id)
		{
			index = i;
			for (j=0; index>-1 && j<newboxitemcount; j++)
			{
				$('newboxanchor_'+j+'_'+index).style.display = 'none';
				for (var k=index; k<newboxitemcount; k++)
					$('newboxanchor_'+j+'_'+k).innerHTML = parseInt($('newboxanchor_'+j+'_'+k).innerHTML)-1;
			}
			$('newbox_'+i).setAttribute('name','');
			_newboxitemcount--;
		}
		if (index>-1 && _newboxitemcount==0)		//az utolso kiemelt elemet is toroljuk
			$('newbox_'+index).style.display = 'none';
		else if (index>-1 && newboxindex==index)	//eppen a torolt elem volt megjelenitve
			switchNewbox(index+1);
	}

	for (i=0; typeof bidboxitemcount!='undefined' && i<bidboxitemcount; i++)
	{
		index = -1;
		if ($('bidbox_'+i).getAttribute('name')==id)
		{
			index = i;
			for (j=0; index>-1 && j<bidboxitemcount; j++)
			{
				$('bidboxanchor_'+j+'_'+index).style.display = 'none';
				for (var k=index; k<bidboxitemcount; k++)
					$('bidboxanchor_'+j+'_'+k).innerHTML = parseInt($('bidboxanchor_'+j+'_'+k).innerHTML)-1;
			}
			$('bidbox_'+i).setAttribute('name','');
			_bidboxitemcount--;
		}
		if (index>-1 && _bidboxitemcount==0)		//az utolso kiemelt elemet is toroljuk
			$('bidbox_'+index).style.display = 'none';
		else if (index>-1 && bidboxindex==index)	//eppen a torolt elem volt megjelenitve
			switchBidbox(index+1);
	}
}

var disboxindex = 0;
function switchDiscountbox(index)
{
	if (typeof index == 'undefined')
	{
		var curIndex = disboxindex;
		var nextIndex = (disboxindex+1)%disboxitemcount;

	}
	else
	{
		clearInterval(switchDiscountbox_id);
		var curIndex = disboxindex;
		var nextIndex = index % disboxitemcount;
	}
	for (; $('discountbox_'+nextIndex).getAttribute('name')==''; nextIndex=(nextIndex+1)%disboxitemcount);
	disboxindex = nextIndex;
	
	$('discountbox_'+curIndex).style.display = 'none';
	$('discountbox_'+nextIndex).style.display = '';
	if (window.ie)
		$('disbox_'+nextIndex+'_prodname').innerHTML = ellipseText($('disbox_'+nextIndex+'_prodname'), $('disbox_'+nextIndex+'_prodname').getAttribute('text2ellipse'), 2);
}

var newboxindex = 0;
function switchNewbox(index)
{
	if (typeof index == 'undefined')
	{
		var curIndex = newboxindex;
		var nextIndex = (newboxindex+1)%newboxitemcount;
	}
	else
	{
		clearInterval(switchNewbox_id);
		var curIndex = newboxindex;
		var nextIndex = index % newboxitemcount;
	}
	for (; $('newbox_'+nextIndex).getAttribute('name')==''; nextIndex=(nextIndex+1)%newboxitemcount);
	newboxindex = nextIndex;

	$('newbox_'+curIndex).style.display = 'none';
	$('newbox_'+nextIndex).style.display = '';
	if (window.ie)
		$('newbox_'+nextIndex+'_prodname').innerHTML = ellipseText($('newbox_'+nextIndex+'_prodname'), $('newbox_'+nextIndex+'_prodname').getAttribute('text2ellipse'), 2);
}

var bidboxindex = 0;
function switchBidbox(index)
{
	if (typeof index == 'undefined')
	{
		var curIndex = bidboxindex;
		var nextIndex = (bidboxindex+1)%bidboxitemcount;
	}
	else
	{
		clearInterval(switchBidbox_id);
		var curIndex = bidboxindex;
		var nextIndex = index % bidboxitemcount;
	}
	for (; $('bidbox_'+nextIndex).getAttribute('name')==''; nextIndex=(nextIndex+1)%bidboxitemcount);
	bidboxindex = nextIndex;

	$('bidbox_'+curIndex).style.display = 'none';
	$('bidbox_'+nextIndex).style.display = '';
	if (window.ie)
		$('bidbox_'+nextIndex+'_prodname').innerHTML = ellipseText($('bidbox_'+nextIndex+'_prodname'), $('bidbox_'+nextIndex+'_prodname').getAttribute('text2ellipse'), 2);
}

function number_format(a, b, c, d) {
	// number_format(number, decimals, comma, formatSeparator)
	a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
	e = a + '';
	f = e.split('.');
	if(!f[0]) f[0] = '0';
	if(!f[1]) f[1] = '';
	if(f[1].length < b){
		g = f[1];
		for(i = f[1].length + 1; i <= b; i++) {
			g += '0';
		}
		f[1] = g;
	}
	if(d != '' && f[0].length > 3) {
		h = f[0];
		f[0] = '';
		for(j = 3; j < h.length; j += 3) {
			i = h.slice(h.length - j, h.length - j + 3);
			f[0] = d + i +  f[0] + '';
		}
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		f[0] = j + f[0];
	}
	c = (b <= 0) ? '': c;
	return f[0] + c + f[1];
}

