
function showNextProduct() {
 var nextNum;
 nextNum = currentNum + 1;
 document.getElementById("productinformation" + nextNum).style.display = "inline";
 currentNum = nextNum;
 document.getElementById("productRegistration:numberOfVisibleDevices").value = currentNum;
 if(currentNum == 10){
   document.getElementById("addDeviceForm").style.visibility = "hidden";
 }
}

var errormsgText = "";

function checkForm() {
 var errormsg = document.getElementById("errormsg");
 var errormsgTextIntro = errormsg.innerHTML;
 errormsgText = "";

 checkProdInfo();
 checkAddress("", true);
 checkAddress("s", false);

 if (errormsgText != "") {
  errormsgText = errormsgTextIntro + errormsgText;
  errormsg.innerHTML = errormsgText;
  errormsg.className = "errorvisible";
  document.location.href = "#error";
 } else {
  errormsg.innerHTML = "";
  errormsg.className = "errorhidden";

  var check = confirm("Do you really want to send the form?");
  if (check) {
    document.formular.submit();
  }
 }
}

function checkProdInfo() {
 checkProductInformation("", true);
 
 for (var i = 2; i <= 10; i++) {
  checkProductInformation(i, false);
 }
}

function checkProductInformation(num, isMandatory) {
 var fieldLabel = null;
 var fieldModell = document.getElementById("modell"+num);
 var fieldLaufnummer = document.getElementById("laufnummer"+num);
 var fieldInbetriebnahmetag = document.getElementById("inbetriebnahmetag"+num);
 var fieldInbetriebnahmemonat = document.getElementById("inbetriebnahmemonat"+num);
 var fieldInbetriebnahmejahr = document.getElementById("inbetriebnahmejahr"+num);

 if (isMandatory) {
  // check modell field
  if (isEmpty(fieldModell) || fieldModell.value.length != 3 ||
    !isNumeric(fieldModell.value)) {
   fieldLabel = document.getElementById("lbl_fabrikationsnummer"+num);
   appendErrorMsg(getLabelName(fieldLabel));
   fieldModell.className = "error";
  } else {
   fieldModell.className = "";
  }
  // check laufnummer field
  if (isEmpty(fieldLaufnummer) || fieldLaufnummer.value.length != 6 ||
    !isNumeric(fieldLaufnummer.value)) {
   fieldLabel = document.getElementById("lbl_fabrikationsnummer"+num);
   appendErrorMsg(getLabelName(fieldLabel));
   fieldLaufnummer.className = "error";
  } else {
   fieldLaufnummer.className = "";
  }
  
  // check inbetriebnahmetag field
  if (isEmpty(fieldInbetriebnahmetag) || fieldInbetriebnahmetag.value.length != 2 ||
    !isNumeric(fieldInbetriebnahmetag.value) || fieldInbetriebnahmetag.value < 1 ||
    fieldInbetriebnahmetag.value > 31) {
   fieldLabel = document.getElementById("lbl_inbetriebnahmedatum"+num);
   appendErrorMsg(getLabelName(fieldLabel));
   fieldInbetriebnahmetag.className = "error";
  } else {
   fieldInbetriebnahmetag.className = "";
  }
  // check inbetriebnahmemonat field
  if (isEmpty(fieldInbetriebnahmemonat) || fieldInbetriebnahmemonat.value.length != 2 ||
    !isNumeric(fieldInbetriebnahmemonat.value) || fieldInbetriebnahmemonat.value < 1 ||
    fieldInbetriebnahmemonat.value > 12) {
   fieldLabel = document.getElementById("lbl_inbetriebnahmedatum"+num);
   appendErrorMsg(getLabelName(fieldLabel));
   fieldInbetriebnahmemonat.className = "error";
  } else {
   fieldInbetriebnahmemonat.className = "";
  }
  // check inbetriebnahmejahr field
  var date = new Date();
  if (isEmpty(fieldInbetriebnahmejahr) || fieldInbetriebnahmejahr.value.length != 4 ||
    !isNumeric(fieldInbetriebnahmejahr.value) || fieldInbetriebnahmejahr.value < 1900 ||
    fieldInbetriebnahmejahr.value > date.getFullYear()) {
   fieldLabel = document.getElementById("lbl_inbetriebnahmedatum"+num);
   appendErrorMsg(getLabelName(fieldLabel));
   fieldInbetriebnahmejahr.className = "error";
  } else {
   fieldInbetriebnahmejahr.className = "";
  }
 } else {
  if (isEmpty(fieldModell) && isEmpty(fieldLaufnummer) &&
    isEmpty(fieldInbetriebnahmetag) && isEmpty(fieldInbetriebnahmemonat) &&
    isEmpty(fieldInbetriebnahmejahr)) {
   fieldModell.className = "";
   fieldLaufnummer.className = "";
   fieldInbetriebnahmetag.className = "";
   fieldInbetriebnahmemonat.className = "";
   fieldInbetriebnahmejahr.className = "";
   
   return;
  } else {
   checkProductInformation(num, true);
  }
 }
}

function checkAddress(prefix, isMandatory) {
 var fieldLabel;
 var fieldName = document.getElementById(prefix + "name");
 var fieldVorname = document.getElementById(prefix + "vorname");
 var fieldStrasse = document.getElementById(prefix + "strasse");
 var fieldOrt = document.getElementById(prefix + "ort");
 var fieldPlz = document.getElementById(prefix + "plz");
 var fieldLand = document.getElementById(prefix + "land");
 var fieldEmail = document.getElementById(prefix + "email");
 var fieldTel = document.getElementById(prefix + "tel");

 if (isMandatory) {
  // check name
  if (isEmpty(fieldName)) {
   fieldLabel = document.getElementById("lbl_name");
   appendErrorMsg(getLabelName(fieldLabel));
   fieldName.className = "error";
  } else {
   fieldName.className = "";
  }
  // check first name
  if (isEmpty(fieldVorname)) {
   fieldLabel = document.getElementById("lbl_vorname");
   appendErrorMsg(getLabelName(fieldLabel));
   fieldVorname.className = "error";
  } else {
   fieldVorname.className = "";
  }
  // check street
  if (isEmpty(fieldStrasse)) {
   fieldLabel = document.getElementById("lbl_strasse");
   appendErrorMsg(getLabelName(fieldLabel));
   fieldStrasse.className = "error";
  } else {
   fieldStrasse.className = "";
  }
  // check city
  if (isEmpty(fieldOrt)) {
   fieldLabel = document.getElementById("lbl_plz_ort");
   appendErrorMsg(getLabelName(fieldLabel));
   fieldOrt.className = "error";
  } else {
   fieldOrt.className = "";
  }
  // check zip
  if (isEmpty(fieldPlz)) {
   fieldLabel = document.getElementById("lbl_plz_ort");
   appendErrorMsg(getLabelName(fieldLabel));
   fieldPlz.className = "error";
  } else {
   fieldPlz.className = "";
  }
  // check country
  if (isEmpty(fieldLand)) {
   fieldLabel = document.getElementById("lbl_land");
   appendErrorMsg(getLabelName(fieldLabel));
   fieldLand.className = "error";
  } else {
   fieldLand.className = "";
  }
  // check email
  if (!isEmpty(fieldEmail) && !isValidEmail(fieldEmail.value)) {
   fieldLabel = document.getElementById("lbl_email");
   appendErrorMsg(getLabelName(fieldLabel));
   fieldEmail.className = "error";
  } else {
   fieldEmail.className = "";
  }
  // check tel
  if (!isEmpty(fieldTel) && !isValidPhonenumber(fieldTel.value)) {
   fieldLabel = document.getElementById("lbl_tel");
   appendErrorMsg(getLabelName(fieldLabel));
   fieldTel.className = "error";
  } else {
   fieldTel.className = "";
  }
 } else {
  if (isEmpty(fieldName) && isEmpty(fieldVorname) && isEmpty(fieldStrasse) &&
    isEmpty(fieldOrt) && isEmpty(fieldPlz) && isEmpty(fieldLand) &&
    isEmpty(fieldEmail) && isEmpty(fieldTel)) {
   fieldName.className = "";
   fieldVorname.className = "";
   fieldStrasse.className = "";
   fieldOrt.className = "";
   fieldPlz.className = "";
   fieldLand.className = "";
   fieldEmail.className = "";
   fieldTel.className = "";
   
   return;
  } else {
   checkAddress(prefix, true);
  }
 }
 
}

function appendErrorMsg(msg) {
 if (errormsgText.search(msg) == -1) {
  errormsgText += "<br>" + msg;
 }
}

function isEmpty(textField) {
 if ((textField.value.length==0) || (textField.value==null) ||
   (textField.id.search("inbetriebnahmetag") != -1 && textField.value == "DD") ||
   (textField.id.search("inbetriebnahmemonat") != -1 && textField.value == "MM") ||
   (textField.id.search("inbetriebnahmejahr") != -1 && textField.value == "YYYY")) {
  return true;
 }
 
 return false;
}

function isNumeric(str) {
   var validChars = "0123456789";
   var c;

   for (i = 0; i < str.length; i++) { 
    c = str.charAt(i); 
    if (validChars.indexOf(c) == -1) {
      return false;
    }
   }
   return true;
}

function isValidPhonenumber(str) {
   var validChars = "0123456789'/ ";
   var c;

   for (i = 0; i < str.length; i++) { 
    c = str.charAt(i); 
    if (validChars.indexOf(c) == -1) {
      return false;
    }
   }
   return true;
}

function isValidEmail(str) {
 var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

 return reg.test(str);
}

function getLabelName(label) {
 var str = label.innerHTML;
 var arr = str.split("<br>");
 if (arr[0].indexOf("(*)") > -1) {
  return arr[0].substring(0, arr[0].length-4);
 } else {
  return arr[0];
 }
}
