var whitespace = " \t\n\r";

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s) {
	var i;

    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    return true;
}

function popup(URL,w,h,scr) {
	if (scr == null || scr == 0)
		scr = 0;
	else
		scr = 1;
	 day = new Date();
	id = day.getTime();
	eval("page" + id + " =window.open(URL,'"+id+"','toolbar=0,scrollbars="+scr+",location=0,statusbar=0,menubar=1,resizable=0,width='+w+',height='+h);");
}

function donothing() {}

function goto_selver(obj) {
	var idx = obj.selectedIndex;
	var href = obj.options[idx].value;
	if(href) {
		window.location.replace(href);
	}
}

function add_to_cart(page,prod) {
	var qty;
	qty = eval('document.forms["tooted"].qty'+prod+'.value');

	if (isEmpty(qty)) {
		qty = 1;
	}
	if (isWhitespace(qty)) {
		qty = 1;
	}
	
	avapopup(page + '&qty=' + qty, 'ost', 250, 100, 'no');
}

function calculate(obj,name,nr,price) {
	var qty;
	if(obj.value != Number(obj.value)) {
		qty = nr;	
	} else {
		qty = obj.value;
	} 
	obj.value = qty;
	price_obj = eval('obj.form.'+name);

	price_obj.value = CurrencyFormatted(Number(qty)*Number(price));

	var ids = obj.form.ids.value;
	
	ids = ids.substr(0,ids.length-1);
	var ids_ary = ids.split(';');
	var total = 0;

	for(var ii=0;ii<ids_ary.length;ii++) {
		ids_num = 0;
		ids_price = 0;

		//ids_num = eval('obj.form.qty'+ids_ary[ii]+'.value');
		ids_price = eval('obj.form.price'+ids_ary[ii]+'.value');
		//ids_price = ids_price.substr(0,ids_price.length-2);

		total = total + Number(ids_price);
	}
	obj.form.total.value = CurrencyFormatted(total);
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
// end of function CurrencyFormatted()
