// JavaScript Document
<!-- Original:  Ronnie T. Moore, Editor -->
<!-- Web Site:  The JavaScript Source -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function isValidDate(dateStr) {
// Date validation function courtesty of 
// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
// Tarih formatını doğrulama:
// DD.MM.YY   DD.MM.YYYY   DD-MM-YY   DD-MM-YYYY

  var datePat = /^(\d{1,2})(\.|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

  var matchArray = dateStr.match(datePat); // is the format ok?
  if (matchArray == null) {
    alert("Girdiğiniz tarih doğru değildir. Tekrar deneyiniz.")
    return false;
  }
  day = matchArray[1];
  month = matchArray[3]; // parse date into variables
  year = matchArray[4];
  if (month < 1 || month > 12) { // check month range
    alert("Girilen ay 1 ile 12 arasında olmalıdır.");
    return false;
  }
  if (day < 1 || day > 31) {
    alert("Girilien gün 1 ile 31 arasında olmalıdır.");
    return false;
  }
  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
    alert(+month+" ayında 31 gün bulunmamaktadır!")
    return false;
  }
  if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day>29 || (day==29 && !isleap)) {
      alert("Şubat " + year + " ," + day + " gün bulunmamaktadır.");
      return false;
   }
  }
  return true;
}

function dispDate(dateObj) {
  month = dateObj.getMonth()+1;
  month = (month < 10) ? "0" + month : month;

  day   = dateObj.getDate();
  day = (day < 10) ? "0" + day : day;

  year  = dateObj.getYear();
  if (year < 2000) year += 1900;

  return (day + "." + month + "." + year);
}

function makeDate(dateStr){
  var datePat = /^(\d{1,2})(\.|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
  var matchArray = dateStr.match(datePat); // is the format ok?
  if (matchArray == null) {
    alert("Girdiğiniz tarih hatalıdır. Lütfen tekrar deneyiniz.")
    return false;
  }
  day = matchArray[1];
  month = matchArray[3]; // parse date into variables
  year = matchArray[4];
  return new Date(year,month-1,day);
}

function pregnancyCalc(pregform) {
  menstrual = new Date(); // creates new date objects
  ovulation = new Date();
  ovstart = new Date();
  ovend = new Date();
  duedate = new Date();
  today = new Date();
  cycle = 0, luteal = 0; // sets variables to invalid state ==> 0

  if (isValidDate(pregform.menstrual.value)) { // Validates menstual date 
    menstrualinput = makeDate(pregform.menstrual.value);
    menstrual.setTime(menstrualinput.getTime())
    if (menstrual > today){
      alert("Son Adet Tarihiniz BUGÜNDEN önce olmalıdır.");
      return false; // exits
    }
  } else {
    return false; // otherwise exits
  }

  cycle = (pregform.cycle.value == "" ? 28 : pregform.cycle.value); // defaults to 28
  // validates cycle range, from 22 to 45
  if (pregform.cycle.value != ""){
    if (pregform.cycle.value < 22){
      alert("Cycle lengths les than 22 days are too short for calculations to be very accurate.\n"
      + "We will still try to complete the calculation with the figure you entered. ");
    }
    if (pregform.cycle.value > 45){
      alert("Cycle lengths exceeding 45 days are too long for calculations to be very accurate.\n"
      + "We will still try to complete the calculation with the figure you entered. ");
    }
  }

  luteal = (pregform.luteal.value == "" ? 14 : pregform.luteal.value); // defaults to 14
  // validates luteal range, from 9 to 16
  if (pregform.luteal.value != ""){
    if (pregform.luteal.value < 9) {
      alert("A luteal phase less than 9 days too short for calculations to be very accurate.\n"
      + "We will still try to complete the calculation with the figure you entered. ");
    }
    if (pregform.luteal.value > 16) {
      alert("A luteal phase exceeding 16 days is too long for calculations to be very accurate.\n"
      + "We will still try to complete the calculation with the figure you entered. ");
    }
  }

// sets ovulation date to menstrual date + cycle days - luteal days
// the '*86400000' is necessary because date objects track time
// in milliseconds;  86400000 milliseconds equals one day
  ovulation.setTime(menstrual.getTime() + (cycle*86400000) - (luteal*86400000));
  pregform.conception.value = dispDate(ovulation);
  
  ovstart.setTime(ovulation.getTime() - (5*86400000));
  pregform.ovstart.value = dispDate(ovstart);

  ovend.setTime(ovulation.getTime() + (5*86400000));
  pregform.ovend.value = dispDate(ovend);

// sets due date to ovulation date plus 266 days
  duedate.setTime(ovulation.getTime() + 266*86400000);
  pregform.duedate.value = dispDate(duedate);

// sets fetal age to 14 + 266 (pregnancy time) - time left
  var fetalage = 14 + 266 - ((duedate - today) / 86400000);
  weeks = parseInt(fetalage / 7); // sets weeks to whole number of weeks
  days = Math.floor(fetalage % 7); // sets days to the whole number remainder

// fetal age message, automatically includes 's' on week and day if necessary
  fetalage = weeks + " hafta" + ", " + days + " gün";
  pregform.fetalage.value = fetalage;

  return false; // form should never submit, returns false
}
//  End -->