﻿

function cmd(sCMD) {
    if (sCMD.search(/\.aspx|\.html/) != -1) {
        document.location.href = sCMD;
    } else {
        eval(sCMD);
    }
  }

  function $e(elm) {
    return document.getElementById(elm)
  }

  function toggleChk(o, elm, inv) {
    if (null != o) {
      var elm = $e(elm);

      if (o.checked == inv) {
        elm.style.display = "block";
      } else {
        elm.style.display = "none";
      }      
    }        
  }

  function bgc(elm,cssclass) {
    elm.className = cssclass;
  }
  
  function validateTextLen(o, errlbl, len) {
    if (null != o && errlbl) {
      var lbl = $e(errlbl);
      var bValid = false;

      if (o.value.length > len)
        bValid = true;

      if (lbl != null) {
        o.className = (!bValid) ? 'borderEr' : '';
        lbl.style.display = (!bValid) ? "block" : "none";

      }
    }
  }

  function validateCbo(o, errlbl, errid) {
    if (null != o && errlbl) {
      var lbl = $e(errlbl);
      var bValid = false;

      if (o.value != errid)
        bValid = true;

      if (lbl != null) {
        o.className = (!bValid) ? 'borderEr' : '';
        lbl.style.display = (!bValid) ? "block" : "none";

      }
    }
  }

  function validateUsername(o, u, errlbl, len) {
    if (null != o && null != u && errlbl) {
      var regname = new RegExp($e(u).value, 'i');
      var lbl = $e(errlbl);
      var bValid = false;

      if (o.value.length > len && !regname.test(o.value))
        bValid = true;

      if (lbl != null) {
        o.className = (!bValid) ? 'borderEr' : '';
        lbl.style.display = (!bValid) ? "block" : "none";

      }
    }
  }

  function validateEMail(o, errlbl) {
    regmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,20})+$/;

    if (null != o && errlbl) {
      var sEmail = o.value;
      var lbl = $e(errlbl);
      var bValid = false;

      bValid = regmail.test(sEmail);

      if (lbl != null) {
        o.className = (!bValid) ? 'borderEr' : '';
        lbl.style.display = (!bValid) ? "block" : "none";

      }    
    }
  }

  function validatePhone(o, errlbl, len) {
    if (null != o && errlbl) {
      var lbl = $e(errlbl);
      var regnum = new RegExp("^[0-9]+$");
      var bValid = false;
      var tmpnum = o.value.replace(/ /g, "");
      if (tmpnum.length > len && regnum.test(tmpnum))
        bValid = true;

      if (lbl != null) {
        o.className = (!bValid) ? 'borderEr' : '';
        lbl.style.display = (!bValid) ? "block" : "none";

      }
    }
  }

  function check4num(o, errlbl) {
    if (null != o && errlbl) {
      var lbl = $e(errlbl);
      var tmpnum = o.value.replace(/ /g, "");
      var gotnum = tmpnum.match(/\D+/);
      var bValid = false;

      if (null == gotnum)
        bValid = true;

      if (lbl != null) {
        o.className = (!bValid) ? 'borderEr' : '';
        lbl.style.display = (!bValid) ? "block" : "none";
      }
    }
  }

  function validateDecimal(o, errlbl, minval) {
    if (null != o && errlbl) {
      var lbl = $e(errlbl);
      
      if (o.value.indexOf(",") >= 0) 
        o.value = o.value.replace(/\,/g,".");
      
      var regnum = new RegExp("^[0-9]+(\.[0-9]+)?$");
      var bValid = false;

      if (o.value > minval && regnum.test(o.value))
        bValid = true;

      if (lbl != null) {
        o.className = (!bValid) ? 'borderEr' : '';
        lbl.style.display = (!bValid) ? "block" : "none";

      }
    }
  }

  function validateStringCompare(o, s2, errlbl) {
    if (null != o && errlbl) {
      var lbl = $e(errlbl);
      var s2 = $e(s2);
      var bValid = false;

      if (o.value == s2.value)
        bValid = true;

      if (lbl != null) {
        o.className = (!bValid) ? 'borderEr' : '';
        lbl.style.display = (!bValid) ? "block" : "none";

      }
    }
  }

  function charCount(o, cntLbl, n) {
    if (null != o) {
      var lbl = $e(cntLbl);
      var n_chars = n - o.value.length;
      lbl.innerHTML = n_chars;
    }
  }

  function ctryPrefix(o, elm) {
    if (null != o) {
      if (o.value != '0')
        show($e(elm));
      else
        hide($e(elm));
    }
  }

  function validateEMailOrPhone(o, errlbl, elm, len) {
    if (null != o) {
      if ($e(elm).value == '0')
        validateEMail(o, errlbl);
      else
        validatePhone(o, errlbl, len);
    }
  }

  function show(o) {
    
      if (o != null) {
          o.style.display = "block";
      }
  }

  function hide(o) {
      if (o != null) {
          o.style.display = "none";
      }
  }

  function toggleView(o, disp) {
      if (disp=='1')
          show(o);
      else
          hide(o);
  }


  function closePriceUnit() {
    $e('perhour').style.display = "none";
    $e('PerMeter2').style.display = "none";
    $e('PerPiece').style.display = "none";
    $e('PerMeter').style.display = "none";
    $e('PerKubik').style.display = "none";
  }

  function togglePriceUnit(o) {
    if (o.value != 1) {
      closePriceUnit();
      switch (o.value) {
        case "2":
          $e('perhour').style.display = "block";
          break;
        case "3":
          $e('PerMeter2').style.display = "block";
          break;
        case "4":
          $e('PerPiece').style.display = "block";
          break;
        case "5":
          $e('PerMeter').style.display = "block";
          break;
        case "6":
          $e('PerKubik').style.display = "block";
          break;

      }
    } else {
      closePriceUnit();
    }
  }

  function toggleReoccurrence(o) {
    if (o.value != 1) {
      $e('freq').style.display = "block";
    } else {
      $e('freq').style.display = "none";
    }
  }

  function toggleQA(hideE,showE,focusE) {
    $e(hideE).style.display = "none";
    $e(showE).style.display = "block";
    $e(focusE).focus();
  }

  function toggleFreq(o) {
    if (o.value != 1) {
      $e('services').style.display = "block";
    } else {
      $e('services').style.display = "none";
    }
  }

  function toggleContactAvailability(o) {
    if (o.value == 1) {
      $e('WrapAvailabilityCustomize').style.display = "none";
      $e('WrapAvailabilityEr').style.display = "none";
    } else if (o.value == 2) {
      $e('WrapAvailabilityCustomize').style.display = "block";
      $e('WrapAvailabilityEr').style.display = "none";
    } else if (o.value == 0) {
      $e('WrapAvailabilityCustomize').style.display = "none";
      $e('WrapAvailabilityEr').style.display = "block";
    }
  }

  function showUpload(o, el1, el2) {
    if (o.value.length > 0 && $e(el1).value.length > 0 && $e(el2).value != "-1") {
      $e('controlContainer').style.visibility = "visible";
      $e('WrapControlContainer').className = "brdr9 bc9";
    } else {
      $e('controlContainer').style.visibility = "hidden";
      $e('WrapControlContainer').className = "brdr1 bc8";
    }
  }

  function roundCHF(val) {
    return (Math.round(val * 20) / 20);
  }

  function calculateProvision(o, el1, el2, ol1, ol2, ol3, ol4) {
    if (o.value.length > 0 && $e(el1).value.length > 0 && $e(el2).value.length > 0) {
      $e(ol1).innerHTML = o.value;
      var vol = o.value * $e(el1).value * $e(el2).value;
      $e(ol2).innerHTML = roundCHF(vol);
      var pct = ((vol <= 2000) * 4 + (vol > 2000) * (vol <= 10000) * 3 + (vol > 10000) * (vol <= 50000) * 2.5 + (vol > 50000) * 2);
      $e(ol3).innerHTML = roundCHF(  Math.max(5,o.value * $e(el1).value * $e(el2).value * pct / 100)  ).toFixed(2);
      $e(ol4).innerHTML = pct.toFixed(1);
    }
  }

  function cleanIfNot(o, s) {
    if (o.value == s) {
      o.value = '';
      o.style.color = '#000';
      o.focus();
    }
  }

  function restateIfEmpty(o, s) {
    if (o.value == '') {
      o.value = s;
      o.style.color = '#aaa';
    }
  }
