var update_price = false;
var stoc_limitat = false;
function setStocLimitat( val )
{
	if( val == 1 )
	{
		stoc_limitat = true;
	}	
}
function show_update_price( pid )
{
	var div = document.getElementById("update_price_" + pid );
	div.style.display = "inline";
	setValue( pid );
	update_price = true;			
}
 function check_actualizat( alert_stoc )
 {
	/*if( stoc_limitat )
	{
		alert("Pentru unul sau mai multe produse stocul nu acopera cantitatea comandata. Va rugam sa actualizati cantitatile in functie de stocul afisat" );
		return false;
	}*/
	if( update_price )
	{
		alert("Cantitatile comandate au fost modificate. Va rugam sa actualizati preturile");
		return false;
	}
	
	window.location.href = "finalizare-comanda.html";
 }

function setValue( pid )
{
	var input = document.getElementById("qty_" +pid);
	if( !isNaN( parseInt(input.value) ) )
	{
		input.value = parseInt(input.value);
	}
	else
	{
		input.value = 0;
	}
	
	//var stoc = document.getElementById("stoc_" + pid ).value;
	//stoc = parseInt( stoc );
		
	//var div_stoc = document.getElementById("alert_stoc_" + pid + "_" + culoare );
	
	/*if( stoc < input.value )
	{
		div_stoc.style.display = "inline";
		stoc_limitat = true;
	}
	else
	{
		div_stoc.style.display = "none";
		stoc_limitat = false;	
	}*/
	
	updateCosCumparaturi(pid);
}

function toFloat( nr )
{
	if( nr.match(",") )
	{
		nr = nr.replace( '.', '' );
		nr = nr.replace( ',', '.' );
	}

	return parseFloat(nr);
}

function apply_discount( sum )
{
	var len = ( discount_comenzi ) ? discount_comenzi.length : 0;
    var discVal = 0;

    if ( sum == 0 )
    {
        return discVal;
    }

    for( var dd=0; dd<len; dd++ )
    {
        var from = parseFloat( discount_comenzi[dd]['from'] );
        var to = parseFloat( discount_comenzi[dd]['to'] );
        var amount = parseFloat( discount_comenzi[dd]['amount'] );
        var proc = parseFloat( discount_comenzi[dd]['proc'] );

        if ( from <= sum && sum < to )
        {
            document.getElementById("limita_discount").innerHTML = parseFloat(from);

			if( amount > 0 )
			{
				var val = amount + " lei";
			}
			if( proc > 0 )
			{
				var val = proc + " %";
			}
			document.getElementById("proc_disc").innerHTML = val;

			discVal = amount + Math.round(proc * sum) / 100;
            return discVal;
        }
    }

    return discVal;
}

function updateCosCumparaturi( pid )
{
	var buc = document.getElementById( "qty_" + pid ).value;
	var pret = document.getElementById( "pret_" + pid ).value;

	pret = toFloat(pret);

	var pret_total_vechi = document.getElementById( "pret_total_" + pid ).innerHTML;
	pret_total_vechi = toFloat(pret_total_vechi);

	//var cost_vechi = document.getElementById("cost_produse").innerHTML;
	//cost_vechi = toFloat(cost_vechi);

	var pret_total = pret*buc;
	var cost_nou = pret_total_vechi + pret_total;

	document.getElementById("pret_total_"+pid ).innerHTML= view_number(pret_total, 'float');
	//document.getElementById("cost_produse" ).innerHTML = view_number(cost_nou, 'float');

	/*if( typeof(eval["valoare_discount"]) != "undefined" )
	{
		var val_discount_old = document.getElementById("valoare_discount").innerHTML;
		val_discount_old = toFloat(val_discount_old);
	}
	else
	{
		var val_discount_old = 0;
	}

	var val_discount = apply_discount( cost_nou );

	document.getElementById("valoare_discount").innerHTML = view_number(val_discount, 'float');

	var display_discount = document.getElementById("display_discount");

	if( val_discount == 0 )
	{
		display_discount.style.display = "none";
	}
	else
	{
		display_discount.style.display = "inline";
	}
*/
	//var div_free = document.getElementById("free_transport" );
	//var div_not_free = document.getElementById("val_transport" );

	/*if( (cost_nou - val_discount) >= parseFloat( limita_transport ) )
	{
		div_not_free.style.display = "none";
		div_free.style.display = "inline";
		transport = 0;
	}
	else
	{
		div_not_free.style.display = "inline";
		div_free.style.display = "none";
		transport = val_transport;
	}*/

	var valoare_comanda = cost_nou;// - val_discount;// + parseFloat(transport);
	document.getElementById("valoare_comanda" ).innerHTML = view_number(valoare_comanda, 'float');
}
var can_make_request = true;

function ajx_can_request()
{
	can_make_request = true;
}

function cod_make_request( url )
{
	if ( can_make_request )
	{
		can_make_request = false;
		$.get(url,
		    function(data)
    		{
			   if ( !data )
				{
					setTimeout( "ajx_can_request()" , 10 );
					return;
				}
				var err = document.getElementById("err");

				switch( data )
				{
					case "0":
						err.innerHTML = "Introduceti un cod de promotie";
						err.style.display = "block";
					break;
					case "1":
						err.innerHTML = "Acest cod nu este valid. Va rugam sa verificati daca l-ati introdus corect. Daca aveti nelamuriri puteti contacta departamentul de relatii cu clientii al Panaosnic Romania, la telefon: 021.316.41.87 de luni pana vineri intre orele 9:00 - 17:15";
						err.style.display = "block";
					break;
					case "2":
						err.innerHTML = "Acest cod nu este valid. Va rugam sa verificati daca l-ati introdus corect. Daca aveti nelamuriri puteti contacta departamentul de relatii cu clientii al Panaosnic Romania, la telefon: 021.316.41.87 de luni pana vineri intre orele 9:00 - 17:15";
						err.style.display = "block";
					break;
					default:
						window.location.href = url_base + "cumparaturi.html";
				}
				setTimeout( "ajx_can_request()" , 10 );
    		}
		);
	}
	else
	{
		setTimeout( 'cod_make_request("'+url+'")' , 10 );
	}
}

function valideaza_cod()
{
	var cod = document.getElementById("promo_value").value;

	var url = "server/valideaza_cod.php?cod=" + cod;
	setTimeout( 'cod_make_request("'+url+'")' , 10 );

}
