<script type="text/javascript">
  function getXMLHTTP() {
    var x = false;
    try {
      x = new XMLHttpRequest();
    } catch (e) {
      try {
        x = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (ex) {
        try {
          req = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e1) {
          x = false;
        }
      }
    }
    return x;
  }
  function getprods(prodid) {
    var strURL = "getthings.php?action=getprods&prodid=" + prodid;
    var req = getXMLHTTP();
    if (req) {
      req.onreadystatechange = function () {
        if (req.readyState == 4) {
          // only if "OK"
          if (req.status == 200) {
            var strs = req.responseText;
            var srr = strs.split('|');

            document.getElementById('prodname').value = srr[0];
            document.getElementById('chrates').value = srr[1];

          } else {
            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
          }
        }
      }
      req.open("GET", strURL, true);
      req.send(null);
    }
  }


  function getstb(vcno) {
    var strURL = "getthings.php?action=getstb&vcno=" + vcno;
    var req = getXMLHTTP();
    if (req) {
      req.onreadystatechange = function () {
        if (req.readyState == 4) {
          // only if "OK"
          if (req.status == 200) {
            var strs = req.responseText;

            if (document.getElementById('stbno').value == '') {

              document.getElementById('stbno').value = strs;
            }




          } else {
            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
          }
        }
      }
      req.open("GET", strURL, true);
      req.send(null);
    }
  }

  function getvc(vcno) {
    var strURL = "getthings.php?action=getvc&stbs=" + vcno;
    var req = getXMLHTTP();
    if (req) {
      req.onreadystatechange = function () {
        if (req.readyState == 4) {
          // only if "OK"
          if (req.status == 200) {
            var strs = req.responseText;

            if (document.getElementById('vcno').value == '') {
              document.getElementById('vcno').value = strs;
            }

          } else {
            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
          }
        }
      }
      req.open("GET", strURL, true);
      req.send(null);
    }
  }

  function setmulti() {

    var prodid = document.getElementById('srch').value;
    var strURL = "getthings.php?action=getchmulti&prodid=" + prodid;
    var req = getXMLHTTP();
    if (req) {
      req.onreadystatechange = function () {
        if (req.readyState == 4) {
          // only if "OK"
          if (req.status == 200) {
            var strs = req.responseText;
            document.getElementById('alamulti').innerHTML = strs;


          } else {
            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
          }
        }
      }
      req.open("GET", strURL, true);
      req.send(null);
    }



  }

  function getcust() {
    var tp = document.getElementById('lnarid').value;
    var prodid = document.getElementById('srch').value;
    var strURL = "getthings.php?action=getcust&prodid=" + prodid + "&tp=" + tp;
    var req = getXMLHTTP();
    if (req) {
      req.onreadystatechange = function () {
        if (req.readyState == 4) {
          // only if "OK"
          if (req.status == 200) {
            var strs = req.responseText;
            var srr = strs.split('|');

            document.getElementById('cname').innerHTML = '<b>Name: </b>' + srr[0];
            document.getElementById('cacno').innerHTML = '<b>Ac NO: </b>' + srr[1];
            document.getElementById('cphone').value = srr[2];
            if (srr[6] == 1) {
              document.getElementById('cvc').innerHTML = '<b>VC NO: </b>' + srr[3];
              document.getElementById('cstb').innerHTML = '<b>STB NO: </b>' + srr[4];

            } else {

              document.getElementById('cvc').innerHTML = '<b>IP Address: </b>' + srr[3];
              document.getElementById('cstb').innerHTML = '<b>Userid: </b>' + srr[4];
            }
            document.getElementById('cprice').innerHTML = '<b>Bill Amount: </b>' + srr[5];
            document.getElementById('cexpiry').innerHTML = '<b>Expiry Date: </b>' + srr[7];
            document.getElementById('cpack').innerHTML = '<b>MSO: </b>' + srr[8];
            document.getElementById('clpaydt').innerHTML = '<b>Last Paydt: </b>' + srr[12];
            document.getElementById('clpayamt').innerHTML = '<b>Last Pay Amt: </b>' + srr[13];
            document.getElementById('caddr').innerHTML = '<b>Area: </b>' + srr[14];
            document.getElementById('custid').value = srr[9];
            document.getElementById('submt').disabled = false;
            document.getElementById('amtpaid').value = srr[5];
            document.getElementById('billamt').value = srr[5];
            document.getElementById('prevdue').value = srr[10];
            document.getElementById('amtpaid').value = srr[5];
            //document.getElementById('cpaychannels').value=srr[15];
            document.getElementById('multiprice').value = srr[5];
            document.getElementById('discamt').value = srr[18];
            var bmOpt = document.getElementById('billing_months');
            if (bmOpt) {
                bmOpt.value = '1';
            }

            var objSelect = document.getElementById("lineman");
            //var objChannel = document.getElementById("alacarte");

            //Set selected
            setSelectedValue(objSelect, srr[16]);
            var strng = srr[17].split(",");
            // var strng=srr[17];
            setmultichk(strng);
            //var strarr=strng.replace('@', ',');
            // var arrayLength = strng.length;

            //$("#alacarte").val([srr[17]]);
            // var channelSelect = $('#alacarte');
            // var option = new Option(data.full_name, data.id, true, true);



            //channelSelect.append(option).trigger('change');


            //document.getElementById('cpaychannels').value=srr[17];
            // for (var i = 0; i < arrayLength; i++) {

            //    setSelectedValue(objChannel, strng[i]);

            //  }

            //setmulti();





            document.getElementById('billamt').value = srr[5];
            document.getElementById('tbillamt').value =srr[5];
            var nogst = (parseFloat(srr[5]) * parseFloat(100)) / 118;
            var gstamt = parseFloat(srr[5]) - parseFloat(nogst);
            //document.getElementById('tbillamt').value = srr[5].toFixed(2);
            //document.getElementById('billdetails').innerHTML = "(Pack amt:" + nogst.toFixed(2) + " and gst amt: " + gstamt.toFixed(2);
            document.getElementById('amtpaid').value = document.getElementById('billamt').value;
            genGST(srr[5].toFixed(2));
            setpayamt();
            




          } else {
            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
          }
        }
      }
      req.open("GET", strURL, true);
      req.send(null);
    }
  }

  function setmultichk(strs) {
    //alert(strs);
    $('#alacarte').val(strs);
    $('#alacarte').trigger('change');


  }

  function setSelectedValue(selectObj, valueToSet) {

    for (var i = 0; i < selectObj.options.length; i++) {
      if (selectObj.options[i].value == valueToSet) {
        //alert(valueToSet);
        selectObj.options[i].selected = true;
        return;
      }
    }
  }


  // function setmultiple(selectObj, valueToSet){
  // $(document).ready(function(){
  // $("#multiple").val(["Multiple2", "Multiple3"]);
  // });

  // }


  function getothercust() {
    var tp = document.getElementById('lnarid').value;
    var prodid = document.getElementById('srch').value;
    var strURL = "getthings.php?action=getcust&prodid=" + prodid + "&tp=" + tp;
    var req = getXMLHTTP();
    if (req) {
      req.onreadystatechange = function () {
        if (req.readyState == 4) {
          // only if "OK"
          if (req.status == 200) {
            var strs = req.responseText;
            var srr = strs.split('|');

            document.getElementById('cname').innerHTML = '<b>Name: </b>' + srr[0];
            document.getElementById('cacno').innerHTML = '<b>Ac NO: </b>' + srr[1];
            document.getElementById('cphone').innerHTML = '<b>Phone: </b>' + srr[2];
            if (srr[6] == 1) {
              document.getElementById('cvc').innerHTML = '<b>VC NO: </b>' + srr[3];
              document.getElementById('cstb').innerHTML = '<b>STB NO: </b>' + srr[4];

            } else {

              document.getElementById('cvc').innerHTML = '<b>IP Address: </b>' + srr[3];
              document.getElementById('cstb').innerHTML = '<b>Userid: </b>' + srr[4];
            }
            document.getElementById('cprice').innerHTML = '<b>Bill Amount: </b>' + srr[5];
            document.getElementById('cexpiry').innerHTML = '<b>Expiry Date: </b>' + srr[7];
            document.getElementById('cpack').innerHTML = '<b>MSO: </b>' + srr[8];
            document.getElementById('clpaydt').innerHTML = '<b>Last Paydt: </b>' + srr[12];
            document.getElementById('clpayamt').innerHTML = '<b>Last Pay Amt: </b>' + srr[13];
            document.getElementById('caddr').innerHTML = '<b>Area: </b>' + srr[14];
            document.getElementById('custid').value = srr[9];
            document.getElementById('submt').disabled = false;
            // document.getElementById('amtpaid').value=srr[5];
            //  document.getElementById('billamt').value=srr[5];
            //   document.getElementById('prevdue').value=srr[10];
            //   document.getElementById('amtpaid').value=srr[11];
            document.getElementById('cpaychannels').value = srr[15];




          } else {
            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
          }
        }
      }
      req.open("GET", strURL, true);
      req.send(null);
    }
  }

  function getcustp() {
    var tp = document.getElementById('lnarid').value;
    var prodid = document.getElementById('srch').value;
    var strURL = "getthings.php?action=getcustp&prodid=" + prodid + "&tp=" + tp;
    var req = getXMLHTTP();
    if (req) {
      req.onreadystatechange = function () {
        if (req.readyState == 4) {
          // only if "OK"
          if (req.status == 200) {
            var strs = req.responseText;
            var srr = strs.split('|');

            document.getElementById('cname').innerHTML = '<b>Name: </b>' + srr[0];
            document.getElementById('cacno').innerHTML = '<b>Ac NO: </b>' + srr[1];
            document.getElementById('cphone').innerHTML = '<b>Phone: </b>' + srr[2];
            if (srr[6] == 1) {
              document.getElementById('cvc').innerHTML = '<b>VC NO: </b>' + srr[3];
              document.getElementById('cstb').innerHTML = '<b>STB NO: </b>' + srr[4];

            } else {

              document.getElementById('cvc').innerHTML = '<b>IP Address: </b>' + srr[3];
              document.getElementById('cstb').innerHTML = '<b>Userid: </b>' + srr[4];
            }
            document.getElementById('cprice').innerHTML = '<b>Amount: </b>' + srr[5];
            document.getElementById('cexpiry').innerHTML = '<b>Last Pay Date: </b>' + srr[11];
            document.getElementById('cpack').innerHTML = '<b>MSO: </b>' + srr[8];
            document.getElementById('custid').value = srr[9];
            document.getElementById('cadd').innerHTML = '<b>Address: </b>' + srr[10];
            document.getElementById('billamt').value = Math.round(srr[5]);
            if (parseFloat(srr[5]) > 0) {
              document.getElementById('submt').disabled = false;
            }

            document.getElementById('amtpaid').value = Math.round(srr[13]);
            document.getElementById('totaldues').value = Math.round(srr[13]);
            document.getElementById('caddr').innerHTML = '<b>Last Payamt: </b>' + srr[12];
            document.getElementById('cdue').innerHTML = '<b>Due: </b>' + srr[13];
            var objSelect = document.getElementById("lineman");

            //Set selected
            setSelectedValue(objSelect, srr[14]);



          } else {
            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
          }
        }
      }
      req.open("GET", strURL, true);
      req.send(null);
    }
  }

  function prodtots() {


    var a = document.getElementById('chrates').value;
    var b = document.getElementById('chquan').value;

    var c = parseFloat(a) * parseFloat(b);
    document.getElementById('chtotal').value = c;

  }

  function countClicks() {

    // if( !counter ){//counter is not set

    counter = setInterval(generatebills, 30000);
    var element = document.getElementById("sendbtn");
    element.classList.add("running");
    // clearInterval(counter);

    // } else {


    // }
  }

  function generatebills() {



    var strURL = "getthings.php?action=generatebills";

    // var strURL="getthings.php?req=shootemails&sub=" +esub+"&from=" +efrom+"&cont=" +editorText+"&uname="+eusername+"&domains="+edomain+"&attch="+eattachment;


    var req = getXMLHTTP();
    if (req) {
      req.onreadystatechange = function () {
        if (req.readyState == 4) {
          // only if "OK"
          if (req.status == 200) {

            //document.getElementById("manufac").value = strs[0];
            if (req.responseText.trim() != "<i class='fa fa-tick'></i>&nbsp;All Bills Generated") {


              document.getElementById("shootres").innerHTML = req.responseText;
              // countClicks();
            } else {
              //alert(req.responseText);
              var element = document.getElementById("sendbtn");
              element.classList.remove("running");
              document.getElementById("shootres").innerHTML = req.responseText;


            }



          } else {
            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
          }
        }
      }

      req.open("GET", strURL, true);
      req.send(null);
    }
  }

</script>
