var price_base;//globale Variable für setVariant() in Detailansicht
//für Überprüfung von nachkommastellen
var last_checked_step=0;

function pricecalcInputOnBlur(input_id, check_id)
{
   var error_msg="Bitte korrigieren Sie ungültigen Wert!";
   var check_input_value=false;
   check_input_value=PricecalcCheckInputValue(input_id,check_id);

    if(check_input_value)
    {
     //nachkommastellen prüfen
     if(last_checked_step!=0)
     {
       updateCheckedValue(input_id);
     }
      pricecalcCalculatePrices("blur");    
    }
    else
    {
      if(typeof(PricecalcErrorMsg_inputValue)!="undefined")
        error_msg = PricecalcErrorMsg_inputValue;
      alert(error_msg);
  
      //std_value for this field
      if(typeof(PricecalcProductData[check_id]["std_value"])!="undefined")
        std_value  = PricecalcProductData[check_id]["std_value"]; 
      $(input_id).value=std_value;  
      $(input_id).focus();
    }
}

function setPriceTotal()
{
  var obj = jsGeneral.XML_Parse();

  if(obj.items.price_base.value)
  {
    price_base = obj.items.price_base.value;
    if($("price"))
     $("price").innerHTML = checkDecimalsToString(price_base) +" "+Currency;
  } 
	
  if(obj.items.price_total.value)
  {
    price_total=obj.items.price_total.value;
    if($("gesamtpreis"))
      $("gesamtpreis").innerHTML = checkDecimalsToString(price_total) +" "+Currency;  
   
    if($("price") && SelectedVariantsIDs.length==0)
      $("price").innerHTML = checkDecimalsToString(price_total) +" "+Currency;
  }
  /*else alert('no exists obj.items.price_total.value');*/

  if(obj.items.price_total_nd.value)
  {
    price_total_nd = obj.items.price_total_nd.value;
    if($("priceND"))
     $("priceND").innerHTML = checkDecimalsToString(price_total_nd) +" "+Currency;
  }

  if(obj.items.tmp_results)
  {
    i=0;
    while(typeof(eval("obj.items.tmp_results.tmp_resultkey_"+i))!="undefined") 
    {
     tmp_result_id = eval("obj.items.tmp_results.tmp_resultkey_"+i).value;
     if($("tmp_result_"+tmp_result_id))
     {
        value_to_float = eval("obj.items.tmp_results.tmp_result_"+tmp_result_id).value;
        value_to_float = value_to_float.replace('.',',');
        $("tmp_result_"+tmp_result_id).innerHTML = value_to_float;
     }
     i++;
    }
  } 
 
  if(obj.items.prices)
  {
    i=0;
    while(typeof(eval("obj.items.prices.priceskey_"+i))!="undefined") 
    {
     price_id = eval("obj.items.prices.priceskey_"+i).value;
     if($("price_"+price_id))
     {
        value_to_float = eval("obj.items.prices.price_"+price_id).value;
        value_to_float = value_to_float.replace('.',',');
        sep_pos = value_to_float.indexOf(",");
        if(sep_pos>0)
        {
          endSlice=sep_pos+3;
          value_to_float = value_to_float.slice(0,endSlice);
        }
        $("price_"+price_id).innerHTML = value_to_float;
     }
     i++;
    }
  }  

  pricecalcCalculateSended = false;
}


function PricecalcCheckInputValue(input_id, check_id)
{
 last_checked_step=0;

 var std_value='';
 //std_value for this field
 if(typeof(PricecalcProductData[check_id]["std_value"])!="undefined")
 {
  std_value  = PricecalcProductData[check_id]["std_value"];
  std_value  = inputValueToFloat(std_value); 
 }

 var value_to_check = inputValueToFloat($(input_id).value);

 if(isNaN(value_to_check))
  return false;

 if(value_to_check==std_value || typeof(PricecalcProductData[check_id]["areas"])=="undefined")
  return true;
 else
 {
   var res=false;

   for(var i=0;i<PricecalcProductData[check_id]["areas"].length;i++)   
   {
     PricecalcProductData[check_id]["areas"][i]["min"] = inputValueToFloat(PricecalcProductData[check_id]["areas"][i]["min"]);
     PricecalcProductData[check_id]["areas"][i]["max"] = inputValueToFloat(PricecalcProductData[check_id]["areas"][i]["max"]);
     PricecalcProductData[check_id]["areas"][i]["step"] = inputValueToFloat(PricecalcProductData[check_id]["areas"][i]["step"]);

     //Rest einer ganzzahlige Division
     var x=value_to_check;
     var y=PricecalcProductData[check_id]["areas"][i]["step"];

     x=x+"";
     y=y+"";
    
     var pos_x=0;
     var pos_y=0;
 
     if(x.indexOf(".")>=0)
      pos_x = x.length-x.indexOf(".")-1;
     if(y.indexOf(".")>=0)
      pos_y = y.length-y.indexOf(".")-1;

      x=x.replace(".","");
      y=y.replace(".","");

     if(pos_x>pos_y)
     {
       for(var j=0;j<(pos_x-pos_y);j++)
       y+="0";
     }
     else if(pos_x<pos_y)
     {
        for(var j=0;j<(pos_y-pos_x);j++)
        x+="0";
     } 

     x=parseInt(x);
     y=parseInt(y); 

     //alert(x+"%"+y+"="+x%y);

     if(value_to_check >= PricecalcProductData[check_id]["areas"][i]["min"] && value_to_check <= PricecalcProductData[check_id]["areas"][i]["max"] && x%y==0)
      {
        res=true;
        last_checked_step = inputValueToFloat(PricecalcProductData[check_id]["areas"][i]["step"]);
      }
   }
   return res;
 }
}

function inputValueToFloat(value_to_float)
{
 value_to_float += "";
 value_to_float = value_to_float.replace(',','.');
 value_to_float = parseFloat(value_to_float);
 
 if(isNaN(value_to_float))
  return false;
 else return value_to_float;
}

function checkDecimalsToString(strNumber)
{
 strNumber=""+strNumber+"";

 var erg=0;
 erg = strNumber.search(/,/);
 if(erg==-1)
  strNumber+=",00";
 
 arr = strNumber.split(",");
 if(arr[1].length==1)
  strNumber+="0";

 return strNumber;
}

var pricecalcCalculateSended=false;

function pricecalcCalculatePrices(modus)
{
  if(!pricecalcCalculateSended)
  {
   pricecalcCalculateSended=true;
   //request senden um gesamtpreis zu berechnen
   prodid = document.product.prodid.value;
   selvars = "";
   for (i = 0; i < document.product.elements.length; i++)
   {
      if (document.product.elements[i].type == "radio" && document.product.elements[i].checked == true)
      {
         selvars += ((selvars!="")?"_":"") + document.product.elements[i].value; 
      }
   }
   inputvars="";
   for(i=0;i<PricecalcProductInputs.length;i++)
   {
      inputvars+= ((inputvars!="")?"__":"") + PricecalcProductInputs[i] + "_" + $("pricecalc_input_"+PricecalcProductInputs[i]).value;
   } 

     if(modus=="blur")  
     {
//alert("blur http://www.1a-werbeprofi.de/shop_getpricecalc_data.php?prodid="+prodid+"&selvars="+selvars+"&inputvars="+inputvars);
      jsGeneral.loadFile({url:"http://www.1a-werbeprofi.de/shop_getpricecalc_data.php?prodid="+prodid+"&selvars="+selvars+"&inputvars="+inputvars,rf:"setPriceTotal()"});
     }
     else if(modus=="changevar")
     {
//alert("changevar http://www.1a-werbeprofi.de/shop_getpricecalc_data.php?prodid="+prodid+"&selvars="+selvars+"&inputvars="+inputvars);
    jsGeneral.loadFile({url:"http://www.1a-werbeprofi.de/shop_getpricecalc_data.php?prodid="+prodid+"&selvars="+selvars+"&inputvars="+inputvars,rf:"setVariantByPricecalc()"}); 
     }
  }
  else return false;
}

function setVariantByPricecalc()
{
  var obj = jsGeneral.XML_Parse();

  if(obj.items.price_base.value)
  {
    price_base = obj.items.price_base.value;
    setNewVariant(tmpNewVarID, tmpGroupID); 
  }

  if(obj.items.price_total.value)
  {
    price_total=obj.items.price_total.value;
    if($("gesamtpreis"))
      $("gesamtpreis").innerHTML = checkDecimalsToString(price_total) +" "+Currency;
  }  

  if(obj.items.price_total_nd.value)
  {
    price_total_nd = obj.items.price_total_nd.value;
    if($("priceND"))
     $("priceND").innerHTML = checkDecimalsToString(price_total_nd) +" "+Currency;
  } 

  if(obj.items.tmp_results)
  {
    i=0;
    while(typeof(eval("obj.items.tmp_results.tmp_resultkey_"+i))!="undefined") 
    {
     tmp_result_id = eval("obj.items.tmp_results.tmp_resultkey_"+i).value;
     if($("tmp_result_"+tmp_result_id))
     {
        value_to_float = eval("obj.items.tmp_results.tmp_result_"+tmp_result_id).value;
        value_to_float = value_to_float.replace('.',',');
       

        $("tmp_result_"+tmp_result_id).innerHTML = value_to_float;
     }
     i++;
    }
  }

  if(obj.items.prices)
  {
    i=0;
    while(typeof(eval("obj.items.prices.priceskey_"+i))!="undefined") 
    {
     price_id = eval("obj.items.prices.priceskey_"+i).value;
     if($("price_"+price_id))
     {
        value_to_float = eval("obj.items.prices.price_"+price_id).value;
        value_to_float = value_to_float.replace('.',',');
        sep_pos = value_to_float.indexOf(",");
        if(sep_pos>0)
        {
          endSlice=sep_pos+3;
          value_to_float = value_to_float.slice(0,endSlice);
        }
        $("price_"+price_id).innerHTML = value_to_float;
     }
     i++;
    }
  } 
 
  pricecalcCalculateSended = false; 
}


function pricecalcGetBpriceValue(BPriceID)
{
  strVarKey="";
  BPricevalue=0;
  if(typeof(PricecalcProductData2[BPriceID]['std_value'])!="undefined")
   BPricevalue = PricecalcProductData2[BPriceID]['std_value'];

  if(typeof(SelectedVariantsIDs)!="undefined")
  {
    for(var i=0;i<SelectedVariantsIDs.length;i++)
    {
     if(i>0)
      strVarKey += "_";
     strVarKey += SelectedVariantsIDs[i];
    }

    if(typeof(PricecalcProductData2[BPriceID]['exceptions'][strVarKey])!="undefined")
       BPricevalue=PricecalcProductData2[BPriceID]['exceptions'][strVarKey];   
  }
 
  return BPricevalue;
}

function isInt(x) { 
   var y=parseInt(x); 
   if (isNaN(y)) return false; 
   return x==y && x.toString()==y.toString(); 
 } 

function updateCheckedValue(input_id)
{
  //Anzahl nachkommastellen ermitteln
  str = last_checked_step+"";
  pos = str.lastIndexOf(".");
  if(pos!=-1)
   count = str.length-pos-1; 
  else return false;

//alert("müssen sovielee nachkomastellen sein: "+count);

  //fehlende stellen mit 0 füllen
  var last_checked_value = $(input_id).value;
  var tmp = inputValueToFloat(last_checked_value)+"";
  pos = tmp.lastIndexOf(".");
  if(pos!=-1)
   count2 = tmp.length-pos-1;
  else count2=0;

//alert("soviele nachkomastellen gibt es: "+count2);

  var tmp_count = count-count2;
  
  tmp +="";
  if(count2 == 0)
   tmp +=",";
  for(var i=0;i<tmp_count;i++)
   tmp +="0";
  //Wert setzen 
  tmp = tmp.replace(".",",");
  $(input_id).value = tmp;
}
