var SB_CODE_EMPTY   = -1;
var SB_CODE_NORMAL  = 0;
var SB_CODE_SPECIAL_1 = 1;
var SB_CODE_SPECIAL_2 = 2;

var actualCodeSB = SB_CODE_EMPTY;

function checkCodeItem(obj,codeItem,codeSB, msg){
  msgCannotSel=unescape(checkBoxOrderMessage[msg]);
  openWin=false
  if (!obj.checked) {
    listPart=document.all.partValue
    i=0;
    found=false
    if (listPart) {
      if  (listPart.length > 1){
        for (i=0;i<listPart.length;i++) {
          if (listPart[i].checked) {
            found=true;
            i=listPart.length
            //actualCodeSB=SB_CODE_NORMAL
          }
        }
      }
    }
    if (!found) {
      actualCodeSB=SB_CODE_EMPTY
    }
    return true
  }
  canSel=false;
    if (codeItem>0){
      if ( (codeSB==SB_CODE_EMPTY) && (actualCodeSB==SB_CODE_EMPTY) ) {
        canSel=true;
        typeCodeSB=codeItem;
      }else{
        if ((codeSB==codeItem)  || (actualCodeSB==codeItem)) {
          typeCodeSB=codeItem;
          canSel=true
        }else{
          canSel=false;
        }
      }
    }else{
        if ( (codeSB==SB_CODE_SPECIAL_1) || (actualCodeSB==SB_CODE_SPECIAL_1) ||
            (codeSB==SB_CODE_SPECIAL_2) || (actualCodeSB==SB_CODE_SPECIAL_2) ){

            canSel=false;
        }else {
            canSel=true;
            typeCodeSB=SB_CODE_NORMAL;
        }
    }

    if (canSel) {
      actualCodeSB=typeCodeSB
    }else{
      alert(msgCannotSel)
    }
    obj.checked=canSel
  return canSel
}

function addItems(url,varName,msg) {
  listPart=document.all.partValue
  listSelPart =""
  if (listPart){
    if  (listPart.length > 1){
      for (i=0;i<listPart.length;i++) {
        if (listPart[i].checked) {
          listSelPart +=listPart[i].value+"|"
        }
      }
    }else{
      if (listPart.checked) {
          listSelPart +=listPart.value+"|"
      }
    }

    if (actualCodeSB==SB_CODE_SPECIAL_1 || actualCodeSB==SB_CODE_SPECIAL_2) varName= "ADDSPECIAL"
    urlToGo=url + "&"+varName+"="+listSelPart
      if (listSelPart!=''){
        if (ambiguityCheck(alertMessage))
	  window.location.href=urlToGo
        else
          alert(alertMessage);
      }else{
	alert(msg);
      }
    }else{
      alert(msg);
    }
}

var totPrice=-1;
function updateTotalPriceSelected(partSelected,priceSelected,currency,
                                  groupingSeparator,decimalSeparator,nDecimalDigit) {

  if (totPrice<0) {
    totPrice = 1 * START_PRICE_PARTS_SELECTED.value;
  }

  if (partSelected.checked) totPrice+=priceSelected
  else totPrice-=priceSelected

  TOTALE_PRICE_PARTS_SELECTED.value=formatCurrency(totPrice,groupingSeparator,decimalSeparator,nDecimalDigit) + " " +currency
//  total = Math.floor(TOTALE_OP.value)  + totPrice
  total = parseFloat(TOTALE_OP.value) + parseFloat(totPrice)
  TOTALE_GENERAL_PRICE_PARTS_SELECTED.value= formatCurrency(total,groupingSeparator,decimalSeparator,nDecimalDigit) + " " +currency
}

var selectedRifs = "";
function updateSelectedOperation(partSelected,timeSelected,currency,
                                  groupingSeparator,decimalSeparator,nDecimalDigit) {
  var p = eval("document.all."+partSelected.name);
  ambiguityCheck(alertMessage);
  var val0 = parseFloat(p[0].time);
  var val1 = parseFloat(p[1].time);
  var pr0 = parseFloat(p[0].value);
  var pr1  = parseFloat(p[1].value);

  var totOpPrice = 1 * TOTALE_PRICE_OPERATIONS_SELECTED.value.replace(",",".");
  //totTime = 1 * TOTALE_TIME_SELECTED.value.replace(",",".");
  totTime = 1 * TOTAL_SEL_TIME.value.replace(",",".");
  if (typeof (partSelected.test) == 'undefined'){
    if (p[0].checked && (selectedRifs.indexOf(partSelected.name)!=-1)) {
      totTime-=val1;
      totTime+=val0;
      totOpPrice-=pr1;
      totOpPrice+=pr0;
    }
    else if (p[1].checked && (selectedRifs.indexOf(partSelected.name)!=-1)) {
      totTime-=val0;
      totTime+=val1;
      totOpPrice-=pr0;
      totOpPrice+=pr1;
    } else if (partSelected.checked) {
      totTime+=parseFloat(partSelected.time);
      totOpPrice+=parseFloat(partSelected.value);
    }
  } else {
    if (p[0].checked && (selectedRifs.indexOf(partSelected.name)!=-1)) {
      totTime+=val0;
      totOpPrice+=pr0;
    }
    else if (p[1].checked && (selectedRifs.indexOf(partSelected.name)!=-1)) {
      totTime-=val0;
      totOpPrice-=pr0;
    }
    else if (p[1].checked && (selectedRifs.indexOf(partSelected.name)==-1)) {
    }
    else if (partSelected.checked) {
      totTime+=parseFloat(partSelected.time);
      totOpPrice+=parseFloat(partSelected.value);
    }
  }
//  TOTALE_TIME_SELECTED.value=formatCurrency(totTime,groupingSeparator,decimalSeparator,nDecimalDigit);
  TOTAL_SEL_TIME.value=formatCurrency(totTime,groupingSeparator,decimalSeparator,nDecimalDigit);
  TOTALE_PRICE_OPERATIONS_SELECTED.value = formatCurrency(totOpPrice,groupingSeparator,decimalSeparator,nDecimalDigit);
  TOTALE_OP.value = totOpPrice;
  if (totPrice<0) {
    totPrice = 1 * START_PRICE_PARTS_SELECTED.value;
  }

  ambiguityCheck(alertMessage);
  total = parseFloat(TOTALE_OP.value) + parseFloat(totPrice)
  TOTALE_GENERAL_PRICE_PARTS_SELECTED.value= formatCurrency(total,groupingSeparator,decimalSeparator,nDecimalDigit) + " " +currency
  if (selectedRifs.indexOf(partSelected.name)==-1){
    selectedRifs+=partSelected.name+"|";
  }
}

function formatCurrency(num,groupingSeparator,decimalSeparator,nDecimalDigit) {
  //num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num)) num = "0";
  sign = (num == (num = Math.abs(num)));
  nDecDigit = Math.pow(10,nDecimalDigit)
  num = Math.floor(num*nDecDigit+0.50000000001);
  cents = num%nDecDigit;
  num = Math.floor(num/nDecDigit).toString();
  for (i=0;i<nDecimalDigit;i++) {cents="0" + cents;}
  cents=cents.substring(cents.length-nDecimalDigit,cents.length)

  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
          num = num.substring(0,num.length-(4*i+3))+groupingSeparator+
  num.substring(num.length-(4*i+3));

  return (  num + decimalSeparator  + cents);
}

var alertMessage;
var totAmbiguityRows;
function ambiguityCheck(msg,totAmbRows) {
  if (typeof (totAmbRows) != 'undefined') {
    totAmbiguityRows = totAmbRows;
  }
  alertMessage=msg;
  if (typeof (document.all.TOTAL_SEL_TIME) != 'undefined') {
    var totTimeSel=document.all.TOTAL_SEL_TIME.value;
    initTotTimeObj = TOTALE_TIME_SELECTED;
    if (typeof (opValue) == 'undefined'){return true;}
    listOps = opValue;
    totElCnt=0;
    totElSelCnt=0;
    for (x=0;x<listOps.length;x++){
      totElCnt++;
      var obj = eval("document.all."+listOps[x].name);
      if (typeof (obj.time) != 'undefined')
        listOps[x].checked=true;

      if (listOps[x].checked)
        totElSelCnt++;
    }
    if (totElSelCnt!=0 && (totElSelCnt==totAmbiguityRows)){
      TOTALE_TIME_SELECTED.value = totTimeSel;
      TOTAL_TIME_ANCHOR.style.visibility="hidden";
      TOTALE_TIME_SELECTED.style.visibility="visible";
      TOTALE_PRICE_OPERATIONS_SELECTED.style.visibility="visible";
      TOTAL_PRICE_ANCHOR.style.visibility="hidden";
      return true;
    }
    else{
      TOTALE_TIME_SELECTED.style.visibility="hidden";
      TOTALE_PRICE_OPERATIONS_SELECTED.style.visibility="hidden";
      TOTAL_PRICE_ANCHOR.style.visibility="visible";
      TOTAL_TIME_ANCHOR.style.visibility="visible";
      return false;
    }
  } else return true;
}