function str_replace ( search, replace, subject ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
 
    if(!(replace instanceof Array)){
        replace=new Array(replace);
        if(search instanceof Array){//If search    is an array and replace    is a string, then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
 
    if(!(search instanceof Array))search=new Array(search);
    while(search.length>replace.length){//If replace    has fewer values than search , then an empty string is used for the rest of replacement values
        replace[replace.length]='';
    }
 
    if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
        for(k in subject){
            subject[k]=str_replace(search,replace,subject[k]);
        }
        return subject;
    }
 
    for(var k=0; k<search.length; k++){
        var i = subject.indexOf(search[k]);
        while(i>-1){
            subject = subject.replace(search[k], replace[k]);
            i = subject.indexOf(search[k],i);
        }
    }
 
    return subject;
 
}

function calculateProduct(cp)
{ 
 var eq = new Array();
 
 if(cp[0]!=null)eq.push(cp[0].value);
 if(cp[1]!=null)eq.push(cp[1].value);
 if(cp[2]!=null)eq.push(cp[2].value);
 if(cp[3]!=null)eq.push(cp[3].value);
 if(cp[4]!=null)eq.push(cp[4].value);
 if(cp[5]!=null)eq.push(cp[5].value);
 if(cp[6]!=null)eq.push(cp[6].value);
 
 var product = 1;
 
 for(var i=0;i<eq.length;i++)
 {
  if(isNaN(eq[i]))
  {
   eq[i] = str_replace(',','.',eq[i]);   
   if(isNaN(eq[i]))
   eq[i] = 1;
  }
  product*=eq[i];
 }
 return product;
}

function dot2coma(str)
{
 return str;
}

function coma2dot(str)
{
 return str;
}

function recalculateSize(object_id,field,event)
{
 var keynum = 0;
 var keychar = '0';
  
 if(window.event)
 {
  keynum = event.keyCode;
 }
 else if(event.which)
 {
  keynum = event.which;
 }
 keychar = String.fromCharCode(keynum);

 if(isNaN(field.value))
 {
  field.value = str_replace(',','.',field.value);
  if(isNaN(field.value))
  {
   field.value = 1;
  }
 }

 updateCalculator(object_id);
}

function updateProductSize(object_id,size)
{
 var output = document.getElementById('object_'+object_id+'_ProductSummarySize');
 if(output)
 {
  output.value = number_format(size,2,'.',' ');
 }
}

function updateProductPrice(object_id)
{
 var bp = getNode('object_'+object_id+'_ProductBasePrice');
 var bvp = getNode('object_'+object_id+'_ProductBaseVatPrice');
 var ppc = getNode('object_'+object_id+'_ProductPriceCurrency');
 var ppc = getNode('object_'+object_id+'_ProductPriceCurrency');
 var cp = new Array();
 var i = 0;
 for(i=0;i<6;i++)
 cp[i] = getNode('object_'+object_id+'_pai'+i+'_id');
 cp[6] = getNode('object_'+object_id+'_q');
 var size = calculateProduct(cp);
 
 var pp = getNode('object_'+object_id+'_ProductPrice');
 var pvp = getNode('object_'+object_id+'_ProductVatPrice');
 
 pp.value = number_format(bp.value*size,2,'.',',') +' '+ ppc.value;
 pvp.value = number_format(bvp.value*size,2,'.',',') +' '+ ppc.value;
}

function updateCalculator(object_id)
{
 var cp = new Array();
 var i = 0;
 for(i=0;i<6;i++)
 cp[i] = getNode('object_'+object_id+'_pai'+i+'_id');

 updateProductSize(object_id,calculateProduct(cp));
 updateProductPrice(object_id);
}

function updateCalculatorArray(ida)
{
 for(var i=0;i<ida.length;i++)
 {
  updateCalculator(ida[i]);
 }
}
