$(document).ready(function() {

$("input[type='submit']").live('click', function() {	
	var action=this.getAttribute('id');
	$("input[name='action']").val(action);
})

$('#saveStavka').submit(function() {
	var err=0;
	$("select").each(function () {
		if ($(this).val()=='odaberi') {
        	$(this).css('background-color','red')
        	err=1;
        } else {
        	$(this).css('background-color','')
		}
    });
	if($("input[name='amount']").val()=='') {
		$("input[name='amount']").css('background-color','red')
		err=1;
	} else {
		$("input[name='amount']").css('background-color','')
	}
	if (err==1) {
		return false;
	}
	$.ajax({
		type : "POST",
		cache : false,
		url : "index.php?id=28",
		//url : "index.php?id=28&action=save_stavka",
		data : $('#saveStavka').serialize(),
		dataType : 'json',
		beforeSend: function() { 
			$('#message').html('Spremam').css('background-color','red').css('display','block'); 
			$("input[type='submit']").attr('disabled','disabled');
		},
		success : function(response) {
			$('#message').html(response.message).css('background-color','#50D200');
			$('#savedHolder').html(response.lastn);
			$("input[type='submit']").removeAttr('disabled');
		}
	});
	return false; 
}); 

$(".delete").live('click', function() {
	var id=this.getAttribute('id');
	var projectId=this.getAttribute('name');
	$.ajax( {
		type : "POST",
		cache : false,
		url : "index.php?id=28&action=delete_stavka",
		data : {stav_id: id, proj_id: projectId},
		dataType : 'json',
		success : function(response) {
			//$("#poruke").html("").html(data);
			alert (response.message);
			$('#savedHolder').html(response.lastn);
		}
	});
	return false;
});

$("input[name='amount']").keydown(function(event) {
    // Allow only backspace and delete
    if ( event.keyCode == 46 || event.keyCode == 8  || event.keyCode == 188   || event.keyCode == 110) {
        // let it happen, don't do anything
    }
    else {
        // Ensure that it is a number and stop the keypress
        if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
            event.preventDefault(); 
        }   
    }
});
	
})