function product_recalculate_price(id_product) {
  
  var id_product = parseInt(id_product);
  var qty = parseInt($('#qty_'+id_product).val());
  
  if((id_product>0) && (qty>0)) 
  {
    $.ajax({
            type : 'POST',
            url : './forms/product_ajax.php',
            dataType : 'JSON',
            data: {
                op: 'recalculate_item_price',
                id: id_product,
                i_qty: qty
            },
            success : function(data){
                data = eval(data);
                
                for ( key in data[0] )//get key => value
                    {    
                        $('#item_count').text(data[0][key]);
                    }
                for ( key in data[1] )//get key => value
                    {    
                        $('#item_price_'+id_product).text(data[1][key]);    
                    }
                for ( key in data[2] )//get key => value
                    {    
                        $('#subtotal').text(data[2][key]);    
                        //$('#top_subtotal').text(data[2][key]);
                    }              
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                $('#item_price').text("-1");
            }
          });    
  }
  else 
    {   
        if(qty==0)
            {
                $('#item_price_'+id_product).text("$0");    
            }
            else
                {
                    window.alert("Entered quantity is not a valid number");
                }
    }
  
}
  
function calculate_freight(action) {
  var ok = true;
  
  for(i=0;i<$('#postcode').val().length;i++)
    {   
        if(($('#postcode').val().charAt(i)<'0')||($('#postcode').val().charAt(i)>'9'))
            {                   
                ok=false;
            }
    }
  if(($('#postcode').val().length>0)&&($('#postcode').val()!='Delivery Postcode') && ok==true)
    {
        $.ajax({
            type : 'POST',
            url : './forms/product_ajax.php',
            dataType : 'JSON',
            data: {
                op: 'calculate_freight',
                post_code: $('#postcode').val()
            },
            success : function(data){
                
                if((data!='NOT VALID'))
                    {
                      if(!isNaN(data)){
                        $('#freight').text(data);  
                        if(action=='manual')
                            {
                                $('.chck_btn img').css('display','block');                
                                $('#checkout_row').css('background-color','black');                
                            }
                      }else{
                        $('#freight_error').html(data);
                        $('#freight_error').parent().parent().show();
                      }
                    }
                    else
                        {
                            window.alert("Entered postcode is not valid");
                        }                   
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                $('#freight').text("Error");
                //alert("Some error occured during the freight calculation! Please try again.");
                $('#freight_error').html("Some error occured during the freight calculation! Please try again.");
                $('#freight_error').parent().parent().show();
            }
          });    
    }
    else
        {
            window.alert("Postcode is a required field!");
        }   
}

function highlight_thumb(element){
  $(element).parent().parent().children().removeAttr("id");
  $(element).parent().attr("id", "current-img");
}

function change_main_image(image_name){
  $("#main_image").attr("src","admin/product_images/medium/"+image_name);
  $("#zoom1").attr("href","admin/product_images/full/"+image_name);
}

function check_qty(qty_step, element_id){
  var step = parseInt(qty_step);
  var qty = 0;
  if($("#"+element_id)!=null)
    qty = parseInt($("#"+element_id).val());
  var ok = false;
  
  if(step>1 && qty>0){
    if(qty%step == 0){
      ok = true;
    }else{
      ok = false;
    }
  }else{
    ok = true;
  }
  if(!ok && step>1){
    alert("Please note that you can increase quantity only by "+step+"!");
    return false;
  }
}
