
var tmp_count = 0;

function prepareValue(el){
	tmp_count = el.value; 
    if (el.value == '0') el.value = '';
}

function validateNumber (el) {
	el.value = parseInt(el.value);
	if (isNaN(el.value)) el.value = 0;
	if (el.value<0) el.value = 0;
}

function validateInput(el){
	val = parseInt(el.value);
	if (isNaN(val)) val = 0;
	if (val < 0) val = 0;
	if ((val != el.value) && (el.value != '')) {
		val = tmp_count;
		el.value = tmp_count;
	} else tmp_count = val;
    updatePrice();
    return true;  
}

function updatePrice () {
    list = document.getElementById('prod_list').getElementsByTagName('INPUT');
    var price = 0;
    var prod_id = 0;
    for (var i in list){
        if (list[i].type == 'text' && list[i].id.match("prod_count_")) {
            prod_id = list[i].id.substr(11, 15);
            price += document.getElementById('prod_price_'+prod_id).value * list[i].value;
        }
	}
    document.getElementById('prod_price_all').innerHTML = formatFloat(price);
}

// kaina apvalina iki litu
function formatFloat(n) {
	//return String(n).match(/\./)?(n+"0").match(/\d+\.\d\d/):n+".00"
    //return n.toFixed(2);
    return n;
}

function addToCart () {
    if (parseFloat(document.getElementById('prod_price_all').innerHTML) > 0) {
        document.prod_form.submit();
    }
    return true;
}

function prodDelete (id) {
    document.getElementById('action_cart').value = 'item_delete';
    document.getElementById('cart_item_id').value = id;
    document.prod_form.submit();
}

function cartAction (action) {
    document.getElementById('action_cart').value = 'cart_'+action;
    document.prod_form.submit();
}

function OpenPopup(url, name, width, height) {
    var top=Math.round((screen.height-height)/2);
    var left=Math.round((screen.width-width)/2);
    wnd = window.open(url, name, "top="+top+",left="+left+",width="+width+",height="+height+",buttons=no,scrollbars=yes,location=no,menubar=no,resizable=yes,status=no,directories=no,toolbar=no");
	wnd.focus();
}

function get_product(item_id, link)
{
    document.getElementById('counter_content_p').style.display = '';
    document.getElementById('counter_content').style.display = '';
    document.getElementById('page').style.display = '';
    document.getElementById('counter_content').innerHTML = '<iframe src="'+ link +''+ item_id +'" class="iframe" frameborder="0" scrolling="auto"></iframe>';
}

function hideLayer()
{
    document.getElementById('counter_content_p').style.display = 'none';
    document.getElementById('page').style.display = 'none';
	document.getElementById('counter_content').innerHTML = '';
}

function move_box()
{
    if(document.getElementById('counter_content_p') != null && document.getElementById('counter_content_p').style.display != 'none')
        document.getElementById('counter_content_p').style.top = (document.documentElement.scrollTop + 0) + 'px';
    if(document.getElementById('page') != null && document.getElementById('page').style.display != 'none')
        document.getElementById('page').style.top = (document.documentElement.scrollTop + 0) + 'px';
    if(document.getElementById('prod_all') != null && document.getElementById('prod_all').style.display != 'none')
    {
        document.getElementById('prod_all').style.top = 0 + 'px';
        document.getElementById('prod_all').style.top = (document.documentElement.scrollTop + document.documentElement.clientHeight - 80) + 'px';
    }
}