$(document).ready(function(){
	
	
	
});

increase_quantity = function(obj, id, list_id)
{	
	var quantity = $(obj).val();
	
    //if( checkNumber(obj) != false )
//    {
//	    if(is_int(quantity))
//	    {
		    $.post(
	                base_url + index_page + 'lt/shopping_list/increase_quantity',
	                {id : id, quantity: quantity, list_id: list_id},
	                function(response){
	            	    $('#shopping_cart_div').html(response);
	                }
		    );
	    //}
//	    else
//	    {
//		    $(this).val(quantity);
//	    }
//    }
}

function is_int(val)
{
    return /^[0-9]+$/.test(val); // Return true or false
}

//calculate the ASCII code of the given character
function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}

checkNumber = function(val)
{
	  var strPass = val.value;
      
	  var strLength = strPass.length;
	  var lchar = val.value.charAt((strLength) - 1);  
	  var cCode = CalcKeyCode(lchar);

	  /* Check if the keyed in character is a number
	     do you want alphabetic UPPERCASE only ?
	     or lower case only just check their respective
	     codes and replace the 48 and 57 */

      //alert(cCode);
         
	  if (cCode < 48 || cCode > 57 ) {
	    var myNumber = val.value.substring(0, (strLength) - 1);
	    val.value = myNumber;
	  }
	  return false;
}



shopping_list = function(obj, url)
{
	var list_id = $(obj).val();
    
    $.post(
            base_url + index_page + 'lt/shopping_list/change_list',
            {list_id: list_id},
            function(response)
            {
//                $('#shopping_cart_div').html(response);
                window.location.reload();
            }
    );
}

delete_shopping_item = function(obj, list_id)
{
	var item_id = $(obj).attr('id');
	
	$.post(
            base_url + index_page + 'lt/shopping_list/delete',
            {item_id : item_id, list_id: list_id},
            function(response){
            	$('#shopping_cart_div').html(response);
	        }
	);
}

clear_shopping_list = function(list_id, user_id)
{
	$.post(
            base_url + index_page + 'lt/shopping_list/clear_list',
            {list_id : list_id, user_id: user_id},
            function(response){
            	$('#shopping_cart_div').html(response);
            }
	);
}

delete_shopping_list = function(list_id)
{
	$.post(
            base_url + index_page + 'lt/shopping_list/delete_list',
            {list_id: list_id},
            function(){
            	$(location).attr('href', base_url + index_page + 'lt/pirkiniu-sarasai');
            }
	);
}

increase_quantity_main = function(obj, id, user_id, list_id)
{	
	var quantity = $(obj).val();
		
	if(is_int(quantity))
	{
		$.post(
	            base_url + index_page + 'lt/shopping_list/increase_quantity_main',
	            {id: id, quantity: quantity, user_id: user_id, list_id: list_id},
	            function(){
	            	$(location).attr('href', base_url + index_page + 'lt/pirkiniu-sarasas/' + list_id);
	            }
		);
	}
	else
	{
		$(this).val(quantity);
	}
}

clear_shopping_list_main = function(list_id, user_id)
{
	$.post(
            base_url + index_page + 'lt/shopping_list/clear_list_main',
            {list_id : list_id, user_id: user_id},
            function(){
            	$(location).attr('href', base_url + index_page + 'lt/pirkiniu-sarasas/' + list_id);
            }
	);
}

delete_shopping_item_main = function(item_id, list_id)
{
    $.post(
            base_url + index_page + 'lt/shopping_list/delete',
            {item_id : item_id, list_id: list_id},
            function(){
                $(location).attr('href', base_url + index_page + 'lt/pirkiniu-sarasas/' + list_id);
            }
    );
}

set_active = function(list_id, user_id)
{
    $.post(
            base_url + index_page + 'lt/shopping_list/set_active',
            {list_id : list_id, user_id: user_id}
    );
}

