<!--

function addslot(slot_nr)
{
	atgal = parseInt(slot_nr-1);
	pirmyn = parseInt(slot_nr+1);
	document.getElementById('sl_'+slot_nr).innerHTML = "Kaina #"+slot_nr+" <input type='text' id='kaina"+slot_nr+"' name='kainos["+slot_nr+"][0]' style='width:70px;' maxlength='7' onchange='set_kaina_eur("+slot_nr+")'> LTL <input type='text' name='kainos["+slot_nr+"][1]' style='width:150px;'><span name='kaina_eur"+slot_nr+"' id='kaina_eur"+slot_nr+"'></span><span id='sp_"+slot_nr+"'>&nbsp;&nbsp;&nbsp;<a href='javascript:;' onclick='addslot("+pirmyn+")'>pridėti</a></span><br /><div id='sl_"+pirmyn+"' name='sl_"+pirmyn+"'></div>";
	document.getElementById('sp_'+atgal).innerHTML = "";
}

function init_selector(block,in_val,fval,kslot)
{
	document.getElementById(block).style.background='#E4F1CF';
	document.getElementById(in_val).innerHTML = "<div style='padding-left:18px;color:#70932F;'><input type='text' name='txt"+in_val+"' id='txt"+in_val+"' class='pirk_input' maxlength='5' style='width:40px;' onfocus='onform()'" + "onblur=f_blur('"+block+"','"+in_val+"')> Kiekis</div>";
	setCookie('kaina_selected', in_val);
	setCookie('kslot', kslot)
}

function onform()
{
	clearTimeout(blurid)
}

function f_blur(block,in_val)
{
	if(document.getElementById('txt'+in_val).value=='')
	{
		hideall(block,in_val)
	}else{
		blurid = setTimeout("hideall('"+block+"','"+in_val+"')",3000);
	}
}

function init_blur(block,in_val)
{
	blurid = setTimeout("hideall('"+block+"','"+in_val+"')",1000);
}

function hideall(block,in_val)
{
	document.getElementById(block).style.background='#ffffff';document.getElementById(in_val).innerHTML='';
}

function add_to_cart(id,pavadinimas,numeris)
{
	kiekis = 1;
	var prekebd = pavadinimas+';'+id+';'+kiekis+';'+numeris+':|:';
	addToCookie('add_preke',prekebd);
	update_cart_info();
	if(document.getElementById('prekiu_krepselis').innerHTML!='')
	{
		show_cart();
		show_cart();
	}
}

function skaiciuok_kaina(kiekis_id,kaina,kaina_id)
{
	var kiekis_g = parseInt(document.getElementById(kiekis_id).value);
	var kaina = kaina;
	if(kiekis_g){
		kaina_val = kiekis_g*kaina;
		kaina_val_eur = kiekis_g*(kaina/3.54);
		document.getElementById('fvallt_'+kaina_id).value = Math.round(100*kaina_val)/100;
		document.getElementById('fvaleur_'+kaina_id).value = Math.round(100*kaina_val_eur)/100;
		document.getElementById(kiekis_id).value = kiekis_g;
	}else{
		return false;
	}
}

function set_kaina_eur(element_id)
{
	var kaina1_lt = parseFloat(document.getElementById('kaina'+element_id).value);
	if(kaina1_lt)
	{
		var replaced_num = document.getElementById('kaina'+element_id).value.replace(',','.');
		var kaina_eur = replaced_num/3.54
		document.getElementById('kaina'+element_id).value = replaced_num;
		document.getElementById('kaina_eur'+element_id).innerHTML = ' / ' + Math.round(100*kaina_eur)/100 + ' EUR';
	}else{
		document.getElementById('kaina_eur'+element_id).innerHTML = '0';
		document.getElementById('kaina'+element_id).value = '0';
	}
	
}

function show_cart()
{
	var cart_s = document.getElementById('prekiu_krepselis');
	var cookie = getCookie('add_preke');
	if(cart_s.innerHTML!='')
	{
		cart_s.innerHTML = '';	
	}else{
		if(cookie)
		{
			tmpbd = '<p /><table width="400" cellpadding="0" cellspacing="1" bgcolor="#ffffff">';
			prekes = cookie.split(':|:');
			prekes.pop();						
			tmpbd += '<tr><td width="300" class="cart_list_top">Title</td><td width="100" class="cart_list_top" >Item number</td><td width="70" class="cart_list_top" >Quantity</td><td class="cart_list_top" > &nbsp; </td></tr>';			
			for(var ct=0; ct < prekes.length; ct++)
			{
				preke = prekes[ct].split(';');							
				tmpbd += '<tr>';
				tmpbd += '<td style="background-color:#f5f5f5;color:black;"><input type="text" value=\''+preke[0]+'\' style="border:0;background-color:#f5f5f5;width:100%" readonly></td>';
				tmpbd += '<td style="background-color:#f5f5f5;color:black;"><input type="text" value="'+preke[3]+'" style="border:0;width:100px;background-color:#f5f5f5;" readonly></td>';
				tmpbd += '<td style="background-color:#f5f5f5;color:black;"><input type="text" id="krepsys_'+ct+'" value="'+preke[2]+'" style="border:0;width:70px;background-color:#FFFFFF;" onKeyUp=updateKiekis("'+ct+'");></td>';
				tmpbd += '<td style="background-color:#f5f5f5;color:black;padding-left:3px;padding-right:3px;"><a href="javascript:;" onclick=delete_cart_item("'+ct+'")>Delete</a></td>';
				tmpbd += '</tr>';																																						
			}			
			tmpbd += '<tr><td align="right" colspan="4" style="padding:5px;background-color:#f5f5f5;color:black;"><input type="button" value="Send" class="button" onclick=open_wnd2("500","400","uzsakymas.php")></td></tr>';
			tmpbd += '</table>';
			cart_s.innerHTML = tmpbd;
			//document.getElementById('catalogs_list0').innerHTML='';
		}	
		
	}
		
}

function setFocus(fid)
{
	document.getElementById(fid).focus();
}

function updateKiekis(nr)
{
	cookis = cookie = getCookie('add_preke');
	prekes = cookis.split(':|:');
	prekes.pop();
	kiekis = document.getElementById('krepsys_'+nr).value;
	if(kiekis=='')
	{
		kiekis = 1;
	}
	if(kiekis==0)
	{
		kiekis = 1;
	}
	prekebd = "";
	for(var ct=0; ct < prekes.length; ct++)
	{
		preke = prekes[ct].split(';');
		if(ct==nr)
		{
			preke[0] = preke[0];
			preke[2] = kiekis;
			preke[3] = preke[3];
			preke[4] = kiekis*preke[6];
			prekebd += preke[0]+''+';'+preke[1]+''+';'+preke[2]+';'+preke[3]+':|:';
		}else{						
			prekebd += preke[0]+''+';'+preke[1]+''+';'+preke[2]+';'+preke[3]+':|:';		
		}		
	}
	setCookie('add_preke',prekebd);

	setFocus('krepsys_'+nr);
}

function parseelement(thisone)
{
	var wd
	if (thisone.charAt(0)=="$")
		return
	wd="w"
	var tempnum=thisone
	for (i=0;i<tempnum.length;i++)
	{
		if (tempnum.charAt(i)==".")
		{
			wd="d"
			break
		}
	}
	if (wd=="w")
		return tempnum+".00";
	else{
		if (tempnum.charAt(tempnum.length-2)==".")
		{
			return tempnum+"0";
		}
		else{
			tempnum=Math.round(tempnum*100)/100
			return tempnum;
		}
	}
}

function show_cart_final()
{
	var cart_s = document.getElementById('prekiu_krepselis');
	var cookie = getCookie('add_preke');
	if(cart_s.innerHTML!='')
	{
		cart_s.innerHTML = '';	
	}else{
		if(cookie)
		{
			tmpbd = '<p /><form method="POST"><table width="100%" cellpadding="1" cellspacing="1" bgcolor="#ffffff">';
			prekes = cookie.split(':|:');
			prekes.pop();
			suma_lt = 0;
			suma_eur = 0;
			tmpbd += '<tr><td width="300" class="cart_list_top">Title</td><td width="100" class="cart_list_top">Item number</td><td width="70" class="cart_list_top">Quantity</td></tr>';
			for(var ct=0; ct < prekes.length; ct++)
			{
				preke = prekes[ct].split(';');
				tmpbd += '<tr>';
				tmpbd += '<td style="background-color:#f5f5f5;color:black;"><input type="text" name="pavadinimas_'+ct+'" value=\''+preke[0]+'\' style="border:0;width:100%;background-color:#f5f5f5;" readonly></td>';
				tmpbd += '<td style="background-color:#f5f5f5;color:black;"><input type="text" name="numeris_'+ct+'" value="'+preke[3]+'" style="border:0;width:100px;background-color:#f5f5f5;" readonly></td>';
				tmpbd += '<td style="background-color:#f5f5f5;color:black;"><input type="text" name="kiekis_'+ct+'" value="'+preke[2]+'" style="border:0;width:70px;background-color:#f5f5f5;" readonly></td>';
				tmpbd += '</tr>';		
			}
			
			tmpbd += '</table><p /><table border="0" width="100%" bgcolor="#ffffff"><tr><td align="left" colspan="5" style="padding:5px;background-color:#f5f5f5;color:black;"><span style="width:110px;">Name, surname:*</span> <input type="text" name="uzsak_vardas" style="border:1px solid black;"></td></tr>';
			tmpbd += '<tr><td align="left" colspan="5" style="padding:5px;background-color:#f5f5f5;color:black;"><span style="width:110px;">Telephone:*</span> <input type="text" name="uzsak_telefonas" style="border:1px solid black;"></td></tr>';
			tmpbd += '<tr><td align="left" colspan="5" style="padding:5px;background-color:#f5f5f5;color:black;"><span style="width:110px;">E-mail:</span> <input type="text" name="uzsak_emailas" style="border:1px solid black;"></td></tr>';
			tmpbd += '<tr><td align="left" valign="top" colspan="5" style="padding:5px;background-color:#f5f5f5;color:black;"><span style="width:110px;vertical-align:top;">Comments:</span> <textarea name="uzsak_komentarai" style="border:1px solid black;width:200px;height:70px;"></textarea><br />'+"<br /><div align='right'>*- it's necessary to fill marked fields</div>"+'</td></tr>';
			
			tmpbd += '<tr><td align="right" colspan="5" style="padding:5px;background-color:#f5f5f5;color:black;"><input type="submit" name="submit_uzsakymas" value=" Send " class="button"> <input type="button" value=" Cancel " onclick="window.close()" class="button"></td></tr>';
			tmpbd += '</table></form>';
			cart_s.innerHTML = tmpbd;
			//document.getElementById('catalogs_list0').innerHTML='';
		}	
		
	}
		
}

function delete_cart_item(item_id)
{
	var cookie = getCookie('add_preke');
	if(cookie)
	{
		prekes = cookie.split(':|:');
		prekes.pop();
		delete prekes[item_id];
		var prekes_new = '';
		for(var ct=0; ct < prekes.length; ct++)
		{
			if(prekes[ct])
			{
				prekes_new += prekes[ct]+':|:';
			}
		}
		setCookie('add_preke',prekes_new);
		show_cart();
		update_cart_info();
		show_cart();
		//alert(prekes_new);
	}
}

function update_cart_info()
{
	var cookie = getCookie('add_preke');
	if(cookie)
	{
		prekes = cookie.split(':|:');
		var suma_lt = 0;
		for(var ct=0; ct < prekes.length; ct++)
		{
			preke = prekes[ct].split(';');
			if(preke[3])
			{
				suma_lt += parseFloat(preke[3]);
			}
			
		}
		setCookie('viso_pirkiniu',suma_lt+'');
		if(document.getElementById('prekiu_suma'))
      {         
         toSet = parseFloat(getCookie('viso_pirkiniu'));
         document.getElementById('prekiu_suma').value = parseelement(toSet+'');
      }
	}else{
      if(document.getElementById('prekiu_suma'))
      {
         document.getElementById('prekiu_suma').value = 0;
      }
	}
}

function set_selected_kaina()
{
	var selected = getCookie('kaina_selected');
	if(selected!='')
	{
		if(selected == 'kaina1')
		{
			if(document.getElementById('kaina1'))
			{
				document.getElementById('kaina1').checked = true;
			}
		}
		if(selected == 'kaina2')
		{
			if(document.getElementById('kaina2'))
			{
				document.getElementById('kaina2').checked = true;
			}
		}
		if(selected == 'kaina3')
		{
			if(document.getElementById('kaina3'))
			{
				document.getElementById('kaina3').checked = true;
			}
		}
	}
}

function setCookie(name, value) {
  var curCookie = name + "=" + escape(value);
  document.cookie = curCookie;
}

function addToCookie(name, value) {
	if(getCookie(name))
	{
		var cookis = getCookie(name);
		var curCookie = name + "=" + escape(cookis+value);
	}else{
		var curCookie = name + "=" + escape(value);
	}
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


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";
  }
}
//-->
