//****************************************************************
// Plausibilitäts-Check per JavaScript
//
// Frank Hempel,             frank.hempel@mbv.tu-chemnitz.de
//
//****************************************************************
// Variablen-Initialisierung
//var browser = "anyone";
//var bName = navigator.appName;
//var bVersion = parseInt(navigator.appVersion);

// Netscape ?
//if (bName == "Netscape"){
//  browser = "ns" + bVersion;}
//else {
  // MSIE ?
//  if (bName == "Microsoft Internet Explorer"){
//    if (bVersion >= 4){
 //     browser = "ie" + bVersion;}
//        else {
//      browser = "ie3";}
//   }}

function openWin(url,name) {
  popupWin = window.open(url,name,"width=590,height=580");
 // if ((browser == "ns3") || (browser == "ns4") || (browser == "ie5"))
    popupWin.focus(); }
// }



// This function sets and defines the submit-action .
function toSubmit(){
   openWin('imc_answer.htm','answer');
//toSubmitval = "http://pmweb.mb3.tu-chemnitz.de/cgi-bin/mailer/mailer.pl";
  toSubmitval = "/cgi-bin/mailer/mailer.pl";
   this.document.forms["myBestellFormular"].action = toSubmitval;
}

// Clears fields and puts focus on name-form
function Reset(){
   document.forms["myBestellFormular"].name.select();
   document.forms["myBestellFormular"].name.focus();
}


// This function is used to submit the forms if they pass the appropriate checks. It passes 'true' if all
// is OK and 'false' if something is wrong, to the onSubmit="submitForms()" in the <FORM> tag below.
function submitForms(){
   if ( (isAnyNumber()) && (isName()) && (isPreName()) && (isStreet()) && (isPlz()) && (isCity()) && (isTel()) ){
      if (confirm("\nSie sind dabei die Bestellung abzuschicken.\n\nKlicken Sie auf OK um das zu tun.\n\nAnsonsten Klicken Sie auf ABBRECHEN .")){
             alert("\nIhre Angaben werden an uns gesendet.\n\n\nVielen Dank!")
             toSubmit();
             return true;
          }
      else{
             alert("\nSie haben die Übertragung abgebrochen.");
         return false;
      }
   }
   else{
      return false;
   }
}

// Checks using only letters and spaces ...
// Umlauts are handled by the perl-script, here threated
// as spaces :-)
function check_alpha (str,feld){
   var fieldname="";

   if (feld=="name") fieldname = "NAME";
   if (feld=="prename") fieldname = "\"VORNAME\"";
   if (feld=="street") fieldname = "\"STRASSE\"";
   if (feld=="plz") fieldname = "\"PLZ\"";
   if (feld=="city") fieldname = "\"WOHNORT\"";
   for (var i = 0; i < str.length; i++){
      var ch = str.substring(i, i + 1);
    // Umlaute und Sonderzeichen fuer Mac und PC
          if ((escape(ch)=="%80")||(escape(ch)=="%C4") || // Ä
             (escape(ch)=="%8A")||(escape(ch)=="%E4") || // ä
                 (escape(ch)=="%85")||(escape(ch)=="%D6") || // Ö
                  (escape(ch)=="%9A")||(escape(ch)=="%F6") || // ö
                  (escape(ch)=="%86")||(escape(ch)=="%DC") || // Ü
                  (escape(ch)=="%9F")||(escape(ch)=="%FC") || // ü
             (escape(ch)=="%A7")||(escape(ch)=="%DF") || // ß
                 (ch == ".") || (ch == "-"))                                  // .-
        // einfach als SPACE ansehen und damit Eingabe erlauben ....
             ch = ' ';
          if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch != ' ')){
             alert("\nSie können in das Feld " + fieldname + " nur\n Buchstaben, Leerzeichen, Punkt und Bindestrich eingeben !\n\nBitte wiederholen Sie die Eingabe.");
         if (feld == "name"){
            document.forms["myBestellFormular"].name.value = "";
                document.forms["myBestellFormular"].name.select();
                document.forms["myBestellFormular"].name.focus();
             }
             if (feld == "prename"){
            document.forms["myBestellFormular"].prename.value = "";
                document.forms["myBestellFormular"].prename.select();
                document.forms["myBestellFormular"].prename.focus();
             }
             if (feld == "city"){
            document.forms["myBestellFormular"].city.value = "";
                document.forms["myBestellFormular"].city.select();
                document.forms["myBestellFormular"].city.focus();
             }
             return false;
      }
   }
   return true;
}

// Checks if any number of paperback is selected...
function isAnyNumber(){
   var i=0;
   var error=0;
   var pieces=0;  //ausgewaehlt?

   while ( error==0 ){
      if (document.forms["myBestellFormular"].elements[i].name.match(/buch/)){
         if (document.forms["myBestellFormular"].elements[i].selectedIndex != 0){
             pieces++;
         }
          }
      i++;
      if (document.forms["myBestellFormular"].elements[i].name == "phone") error=1;
   }

   if (pieces==0){
      alert("\nSie haben keine PUBLIKATION ausgewählt !\n\nBitte treffen Sie eine Auswahl.");
      return false;
   }
   else{
      return true;
   }
}

// Checks the NAME field.
function isName(){
   var str = document.forms["myBestellFormular"].name.value;
   var feld = "name";

   if (str == ""){
      alert("\nSie haben keinen NAMEN angegeben.\n\nBitte geben Sie Ihren Namen ein.");
      document.forms["myBestellFormular"].name.focus();
      return false;
   }
  // Return false if characters are not a-z, A-Z, or a space.
   if (check_alpha (str,feld)) return true
   else return false;
}

// Checks the VORNAME field.
function isPreName(){
   var str = document.forms["myBestellFormular"].prename.value;
   var feld = "prename";

   // Return false if prename field is blank.
   if (str == ""){
      alert("\nSie haben keinen VORNAMEN angegeben.\n\nBitte geben Sie Ihren Vornamen ein.");
      document.forms["myBestellFormular"].prename.focus();
      return false;
   }
   // Return false if characters are not a-z, A-Z, or a space.
   if (check_alpha (str,feld)) return true
   else return false;
}

// Checks the STRASSE field.
function isStreet(){
   var str = document.forms["myBestellFormular"].street.value;
   var feld = "street";
  // Return false if street field is blank.
   if (str == ""){
      alert("\nSie haben keinen STRASSE angegeben.\n\nBitte geben Sie Strasse und Hausnummer ein.");
      document.forms["myBestellFormular"].street.value = "";
      document.forms["myBestellFormular"].street.focus();
      return false;
   }
   else return true;
}

// Checks the PLZ field
function isPlz(){
   var str = document.forms["myBestellFormular"].plz.value;

   if (str == ""){
      alert("\nSie haben keine POSTLEITZAHL eingegeben !\n\nBitte geben Sie Ihre PLZ ein.");
      document.forms["myBestellFormular"].plz.focus();
      return false;
   }
   else{
     // Problem : "0" am Beginn der Postleitzahl !
      if (str.substring(0,1) == "0"){
             var str1 = str.substring(1,str.length);
         var i = parseInt (str1);
                 var k = "0" + i.toString();
      }
      else{
             var i = parseInt (str);
         var k = i.toString();
      }
      if ( (isNaN (i)) || (i==0) || (k!=str) || (str.length!=5) ){
         alert("\n\nDie angegebene PLZ ist nicht korrekt!\nBitte wiederholen Sie die Eingabe.");
         document.forms["myBestellFormular"].plz.value = "";
         document.forms["myBestellFormular"].plz.focus();
         return false;
      }
      else{
             return true;
      }
   }
}

// Checks the WOHNORT field.
function isCity(){
   var str = document.forms["myBestellFormular"].city.value;
   var feld = "city";
   // Return false if city field is blank.
   if (str == ""){
      alert("\nSie haben keinen WOHNORT angegeben.\n\nBitte geben Sie Ihren Wohnort ein.");
      document.forms["myBestellFormular"].city.focus();
      return false;
   }
   // Return false if characters are not a-z, A-Z, or a space.
   if (check_alpha (str,feld)) return true
      else return false;
}


// Checks the TELEFON field.
function isTel(){
   var dummy = 1;
   var latch = 0;
   var str = document.forms["myBestellFormular"].phone.value;

   for (var i = 0; i < str.length; i++){
      var ch = str.substring(i, i + 1);

          if ( (ch == "/") || (ch == "-") || (ch == " ") || ( (ch <= "9") && (ch >= "0"))) { dummy = 0; }
       else {dummy = 1;}
      if (dummy == 1){ latch = 1;}
   }
   if ( (latch == 1) && (str != "") ){
      alert("\n\nDie angegebene TELEFONNUMMER ist nicht korrekt!\nBitte wiederholen Sie die Eingabe.");
      document.forms["myBestellFormular"].phone.value = "";
      document.forms["myBestellFormular"].phone.focus();
      return false;
   }
   else{
      return true;
   }
}