function mylink(href){
  self.location.href=href;
}
function myopen(href, name){
  mywin=window.open(href, name, 'width=200, height=200, screenX=500, screenY=300, resizable=yes, location=no, scrollbars=yes, status=yes');
  mywin.focus();
}
function myfocus(){  
  window.document.forms[0].elements[0].focus();
}
function mysize(x, y){  
  maxWidth = screen.availWidth-0;
  maxHeight = screen.availHeight-0;
  if( x > maxWidth ){
    x = maxWidth;
  }
  if( y > maxWidth ){
    y = maxHeight;
  }
  myLeft = Math.round((maxWidth-x)/2);
  myTop = Math.round((maxHeight-y)/2);
  window.resizeTo(x, y);
  window.moveTo(myLeft, myTop);
}


  function readURL(input, target) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();
                reader.onload = function (e) {
                    $("#"+target)
                        .attr('src', e.target.result)
//                        .attr('style', "width: 10%");
                };

                reader.readAsDataURL(input.files[0]);
            }
        }


function reloadpreview(image, file){
  myimage = document.getElementsByName(image)[0];
  myfile = document.getElementsByName(file)[0];
//  myimage.src = "file:///"+myfile.value;
  myimage.src = "\/" + myfile.value;
alert(myimage.src);
}
function checkform(){
  var ok = true;
  if( javascript_enabled == false ){
    return true;
  }
  if( ok == true ){
    if( !checkvalues(necessary_name_array) ){
      ok = false;
    }
  }
  if( ok == true ){
    if( !checkintegers(integer_name_array, integer_min_array, integer_max_array) ){
      ok = false;
    }
  }
  if( ok == true ){
    if( !checkfloats(float_name_array, float_min_array, float_max_array) ){
      ok = false;
    }
  }
  if( ok == true ){
    if( !checktimes(time_name_array) ){
      ok = false;
    }
  }
  if( ok == true ){
    if( !checkdates(date_name_array) ){
      ok = false;
    }
  }
  if( ok == true ){
    if( !checkfiles(file_name_array, file_allowed_ext_array) ){
      ok = false;
    }
  }
  if( ok == true ){
    if( !checkfiles(image_name_array, image_allowed_ext_array) ){
      ok = false;
    }
  }
  if( ok == true ){
    mysubmit = document.getElementsByName('submit')[0];
    mycancel = document.getElementsByName('cancel')[0];
    mysubmit.disabled=true;
    mycancel.disabled=true;
    return true;
  }else{
    return false;
  }
}
function mycancel(){
  window.close();
}
function myend(){
  window.opener.location.reload();
  window.close();
}
function myredirect(url){
  window.location.href=url;
}

function checkvalues(necessary_name_array){
  for(i=0; i<necessary_name_array.length; ++i){
    myelement = document.getElementsByName(necessary_name_array[i])[0];
    if(myelement.type == "radio" ){
      allradios = document.getElementsByName(necessary_name_array[i]);
      isvalue = false;
      for(j=0; j<allradios.length; ++j){
        myradio = document.getElementsByName(necessary_name_array[i])[j];
        myradio = allradios[j];
        if(myradio.checked == true ){
          isvalue = true;
        }
      }
      if(!isvalue){
        alert("Es wurde ein Auswahlfeld vergessen!");
        myelement.focus();
        return false;
      }
    }
    if(myelement.value == "" ){
      alert("Es wurden nicht alle erforderlichen Felder ausgefüllt!");
      myelement.focus();
      return false;
    }
  }
  return true;
}
function isintegerinrange(value, min, max){
  if( value == "" ){
    return true;
  }  
  myvalue = parseInt(value,10);
  if( isNaN(value) ){
    return false;
  }
  if( !isNaN(min) && !isNaN(max) ){
    if( (myvalue < min) || (myvalue > max) ){
      return false;
    }
  }
  if( !isNaN(min) ){
    if( myvalue < min ){
      return false;
    }
  }
  if( !isNaN(max) ){
    if( myvalue > max ){
      return false;
    }
  }
  return true;
}
function isfloatinrange(value, min, max){
  if( value == "" ){
    return true;
  }  
  myvalue = parseFloat(value);
  if( isNaN(value) ){
    return false;
  }
  if( !isNaN(min) && !isNaN(max) ){
    if( (myvalue < min) || (myvalue > max) ){
      return false;
    }
  }
  if( !isNaN(min) ){
    if( myvalue < min ){
      return false;
    }
  }
  if( !isNaN(max) ){
    if( myvalue > max ){
      return false;
    }
  }
  return true;
}
function checkintegers(integer_name_array, integer_min_array, integer_max_array){
  for(i=0; i<integer_name_array.length; ++i){
    myelement = document.getElementsByName(integer_name_array[i])[0];
    myelement.value = myelement.value.replace(/\s+/g, "");
    myvalue = myelement.value;
    mymin = parseInt(integer_min_array[i],10);
    mymax = parseInt(integer_max_array[i],10);
    alertstring = "Bitte ganze Zahl eingeben.";
    if( !isNaN(mymin) ){
      alertstring = "Bitte ganze Zahl ab "+ mymin + " eingeben.";
    }
    if( !isNaN(mymax) ){
      alertstring = "Bitte ganze Zahl bis " + mymax + " eingeben.";
    }
    if( !isNaN(mymin) && !isNaN(mymax) ){
      alertstring = "Bitte ganze Zahl zwischen "+ mymin + " und " + mymax + " eingeben.";
    }
    if( !isintegerinrange(myvalue, mymin, mymax)){
      alert(alertstring);
      myelement.select();
      return false;
    }else{
      myelement.value = myvalue;
    }
  }
  return true;
}
function checkfloats(float_name_array, float_min_array, float_max_array){
  for(i=0; i<float_name_array.length; ++i){
    myelement = document.getElementsByName(float_name_array[i])[0];
    myelement.value = myelement.value.replace(/\s+/g, "");
    myvalue = myelement.value;
    if( myvalue != "" ){
      myvalue = myvalue.replace(/,/,".");
      myvalue = parseFloat(myvalue);
      mymin = parseFloat(float_min_array[i]);
      mymax = parseFloat(float_max_array[i]);
      alertstring = "Bitte Zahl eingeben.";
      if( !isNaN(mymin) ){
        alertstring = "Bitte Zahl ab "+  mymin.toString() + " eingeben.";
      }
      if( !isNaN(mymax) ){
        alertstring = "Bitte Zahl bis " + mymax.toString() + " eingeben.";
      }
      if( !isNaN(mymin) && !isNaN(mymax) ){
        alertstring = "Bitte Zahl zwischen "+  mymin.toString() + " und " +  mymax.toString() + " eingeben.";
      }
      if( !isfloatinrange(myvalue, mymin, mymax)){
        alert(alertstring);
        myelement.select();
        return false;
      }else{
        myelement.value = myvalue;
      }
    }
  }
  return true;
}
function checktimes(time_name_array){
  var ok;
  for(i=0; i<time_name_array.length; ++i){
    myelement = document.getElementsByName(time_name_array[i])[0];
    myelement.value = myelement.value.replace(/\s+/g, "");
    myvalue = myelement.value;
    if( myvalue == "" ){
      ok = true;
    }else{
      ok = true;
      mytimearray = myvalue.split(".");
      if (mytimearray.length < 2 ){
        mytimearray = myvalue.split(":");
      }
      if (mytimearray.length < 2 || mytimearray.length > 3){
        ok = false;
      }else{
        if( !isintegerinrange(parseInt(mytimearray[0],10), 0, 23) ){
          ok = false;
        }
        for(j=1; j<mytimearray.length; ++j){
          if( !isintegerinrange(parseInt(mytimearray[j],10), 0, 59) ){
            ok = false;
          }
        }
      }
    }
    alertstring = "Bitte Uhrzeit wie z.B. 14.45 oder 14:45 eingeben.";
    if( ok == false ){
      alert(alertstring);
      myelement.select();
      return false;
    }
  }
  return true;
}
function checkdates(date_name_array){
  var ok;
  for(i=0; i<date_name_array.length; ++i){
    myelement = document.getElementsByName(date_name_array[i])[0];
    myelement.value = myelement.value.replace(/\s+/g, "");
    myvalue = myelement.value;
    if( myvalue == "" ){
      ok = true;
    }else{
      ok = true;
      mytimearray = myvalue.split(".");
      if (mytimearray.length != 3){
        ok = false;
      }else{
        myday = parseInt(mytimearray[0],10);
        mymonth = parseInt(mytimearray[1],10);
        if( mytimearray[2] == ""){
          now = new Date();
          myyear = now.getFullYear();
        }else{
          myyear = parseInt(mytimearray[2],10);
        }
        if( !isintegerinrange(myday, 1, 31) ){
          ok = false;
        }
        if( !isintegerinrange(mymonth, 1, 12) ){
          ok = false;
        }
        if( isintegerinrange(myyear, 1, 99) ){
          myyear = myyear + 2000;
        }
        if( !isintegerinrange(myyear, 1970, 2037) ){
          ok = false;
        }
        check = new Date(myyear, mymonth-1, myday );
	// Der Monat geht von 0 bis 11
        if(check.getDate() !=  myday ){
          ok = false;
        }
      }
      alertstring = "Bitte Datum wie z.B. 4.12.2007 oder 4.12.07 eingeben.";
      if( ok == false ){
        alert(alertstring);
        myelement.select();
        return false;
      }else{
        myelement.value = mytimearray[0]+"."+mytimearray[1]+"."+mytimearray[2];
      }
    }
  }
  return true;
}
function checkfiles(file_name_array, file_allowed_ext_array){
  var ok;
  for(i=0; i<file_name_array.length; ++i){
    myelement = document.getElementsByName(file_name_array[i])[0];
    myvalue = myelement.value;
    if( myvalue == "" ){
      ok = true;
    }else{
      ok = false;
      myfilearray = myvalue.split(".");
      ext = myfilearray[myfilearray.length-1].toLowerCase();
      for(j=0; j<file_allowed_ext_array.length; ++j){
        if( ext == file_allowed_ext_array[j] ){
          ok = true;
        }
      }
      alertstring = "Der Filetyp \" *."+ext+"\" ist nicht für den Upload vogesehen";
      if( ok == false ){
        alert(alertstring);
        myelement.select();
        return false;
      }
    }
  }
  return true;
}

