﻿<!-- Original:  Ronnie T. Moore, Editor -->
<!-- Modified:  growingpeople.se -->
<!-- Web Site:  The JavaScript Source -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin-->

function isValidISODate(dateStr) {
// Date validation function courtesty of 
// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
// Checks for the following valid date formats:
// YY/MM/DD YYYY/MM/DD YY-DD-MM YYYY-MM-DD
if (dateStr.length == 0) {
  window.alert("Angiv dato for din seneste menstruations første dag.");
  return false;
}

var datePat = /^(\d{2})(\/|-)(\d{1,2})(\/|-)(\d{1,4})$/; // requires 4 digit year
var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
  window.alert("Ugyldigt datoformat. Din dato skal være angivet som: \"DD-MM-ÅÅÅ\". Der skal være bindestreg imellem. \"Fx 31-12-2009\"") 
  return false;
}

month = matchArray[3]; // parse date into variables
day   = matchArray[1];
year  = matchArray[5];

// window.alert('year: ' + year + '\n' + 'month: ' + month + '\n' + 'day: ' + day);

if (month < 1 || month > 12) { // check month range
  window.alert("Måneden skal angives med tal mellem 1 og 12.");
  return false;
}
if (day < 1 || day > 31) {
  window.alert("Dagen skal angives med tal mellem 1 og 31.");
  return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
  window.alert("Måned "+month+" har ikke 31 dage.")
  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)) {
    window.alert("Februar " + year + " har ikke " + day + " dage.");
    return false;
  }
}
return true;
}


function formatMDYDate(dateStr) {
// Date validation function courtesty of 
// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
// Checks for the following valid date formats:
// YY/MM/DD YYYY/MM/DD YY-DD-MM YYYY-MM-DD

var datePat = /^(\d{2})(\/|-)(\d{1,2})(\/|-)(\d{1,4})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
month = matchArray[3]; // parse date into variables
day   = matchArray[1];
year  = matchArray[5];

return (month + "/" + day + "/" + year);
}


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 (month + "/" + day + "/" + year);
}

function dispISODate(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 pregnancyCalc1(pregForm, pregLinks) {
menstrual = new Date(); // creates new date objects
ovulation = new Date();
ovstart = new Date();
ovend = new Date();
duedate = new Date();
today = new Date();
cycle = 0; // sets variables to invalid state ==> 0

var arrPregLinks = pregLinks.split(",");
var ePlaceHolder = document.getElementById("oWeekLink1");

//Clear placeHolder text & Link (if available)
while (ePlaceHolder.firstChild) ePlaceHolder.removeChild(ePlaceHolder.firstChild);

if (isValidISODate(document.aspnetForm.menstrual.value)) { // Validates menstual date 
  menstrualInput = new Date(formatMDYDate(document.aspnetForm.menstrual.value));
  menstrual.setTime(menstrualInput.getTime());
}
else return false; // otherwise exits

cycle = (document.aspnetForm.cycle.value == "" ? 28 : document.aspnetForm.cycle.value); // defaults to 28
// validates cycle range, from 22 to 45
if (document.aspnetForm.cycle.value != "" && (document.aspnetForm.cycle.value < 22 || document.aspnetForm.cycle.value > 45)) {
alert("Din månedscyklus er enten for kort eller for lang til, at \n"
+ "beregningen kan blive præcis. Vi vil alligevel forsøge at \n"
+ "foretage beregningen med dine angivne tal.");
}

// sets ovulation date to menstrual date + cycle days - 14 days
// the '*86400000' is necessary because date objects track time
// in milliseconds;  86400000 milliseconds equals one day
ovulation.setTime(menstrual.getTime() + (cycle*86400000) - (14*86400000));
document.aspnetForm.conception.value = dispISODate(ovulation);

//UTGÅR!
//ovstart.setTime(ovulation.getTime() - (5*86400000));
//pregForm.ovstart.value = dispISODate(ovstart);
//ovend.setTime(ovulation.getTime() + (5*86400000));
//pregForm.ovend.value = dispISODate(ovend);

// sets due date to ovulation date plus 266 days
duedate.setTime(ovulation.getTime() + 266*86400000);
document.aspnetForm.duedate.value = dispISODate(duedate);

// sets fetal age to 14 + 266 (pregnancy time) - time left
//var fetalage = 14 + 266 - ((duedate - today) / 86400000);
var fetalage = 281 - ((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
if (( weeks >= 0 ) && ( days >= 0 )) {
  fetalage = weeks + (weeks > 1 ? " uger" : " uge") + ", " + days + (days > 1 ? " dage" : " dag");
  document.aspnetForm.fetalage.value = fetalage;
}
else { document.aspnetForm.fetalage.value = ""; }

//Sätt länk till rätt sida "gravid vecka för vecka". Obs att arrPregLinks första index är [0].
if (( weeks >= 0 ) && ( days >= 0 ) && ( weeks <= arrPregLinks.length )) { 
  var eAnchor = document.createElement("a");
  eAnchor.setAttribute("class", "innehall");
  eAnchor.setAttribute("style", "font-weight: bolder;");

  var pregLinkHRef = "";
  if (weeks <= 1) {
  	pregLinkHRef = arrPregLinks[0];
  }
  else {
  	pregLinkHRef = arrPregLinks[weeks-1];
  }  			   	

  if ( pregLinkHRef.length > 0 ) { 
  	if (weeks == 0) {weeks = 1;}
    eAnchor.setAttribute("href", pregLinkHRef);
    var eTextNode = document.createTextNode("Läs mer om din graviditet i vecka " + weeks + " »");
    eAnchor.appendChild(eTextNode);
    ePlaceHolder.appendChild(eAnchor);
  }  
}

document.getElementById('resultDiv').style.display = "";


return false; // form should never submit, returns false
}

//--------------------------------------------------------------------------------------------------
function pregnancyCalc2(pregForm, pregLinks) {
  var scanningDueDate = new Date(); // creates new date objects  
  var today = new Date();
  var arrPregLinks = pregLinks.split(",");
  var ePlaceHolder = document.getElementById("oWeekLink2");

  //Clear placeHolder text & Link (if available)
  while (ePlaceHolder.firstChild) ePlaceHolder.removeChild(ePlaceHolder.firstChild);

  if (isValidISODate(document.aspnetForm.scanning.value)) { // Validates menstual date 
    scanningInput = new Date(formatMDYDate(document.aspnetForm.scanning.value));
    scanningDueDate.setTime(scanningInput.getTime());
  }
  else return false; // otherwise exits

  // sets fetal age to 279 (Scanning DueDate) - time left
  // NB! All dates are calculated in milliseconds; 86400000 milliseconds = One Day

  var fetalage = 279 - ((scanningDueDate - today) / 86400000);
  var weeks = parseInt(fetalage / 7); // sets weeks to whole number of weeks
  var days = Math.floor(fetalage % 7); // sets days to the whole number remainder

  // fetal age message, automatically includes 's' on week and day if necessary
  if (( weeks >= 0 ) && ( days >= 0 )) {
    fetalage = weeks + (weeks > 1 ? " uger" : " uge") + ", " + days + (days > 1 ? " dage" : " dag");
    document.aspnetForm.fetalage2.value = fetalage;
  }	
  else { document.aspnetForm.fetalage2.value = ""; }

  //Sätt länk till rätt sida "gravid vecka för vecka". Obs att arrPregLinks första index är [0].
  var pregLinkHRef = "";
  
  if (( weeks >= 0 ) && ( weeks <= arrPregLinks.length )) {
    var eAnchor = document.createElement("a");
    eAnchor.setAttribute("class", "innehall");
    eAnchor.setAttribute("style", "font-weight: bolder;");
  
    if (weeks <= 1) {
      pregLinkHRef = arrPregLinks[0];	
    }
    else {
      pregLinkHRef = arrPregLinks[weeks-1];
    }  			   	
    
    if ( pregLinkHRef.length > 0 ) {
      if ( weeks == 0 ) {weeks = 1;}	
      eAnchor.setAttribute("href", pregLinkHRef);
      var eTextNode = document.createTextNode("Läs mer om din graviditet i vecka " + weeks + " »");
      eAnchor.appendChild(eTextNode);
      ePlaceHolder.appendChild(eAnchor);
    }  
  }

  return false; // form should never submit, returns false
}

//--------------------------------------------------------------------------------------------------
function fertilityCalc(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; // sets variables to invalid state ==> 0
if (isValidISODate(document.aspnetForm.menstrual.value)) { // Validates menstual date 
  menstrualInput = new Date(formatMDYDate(document.aspnetForm.menstrual.value));
  menstrual.setTime(menstrualInput.getTime());
}
else return false; // otherwise exits
cycle = (document.aspnetForm.cycle.value == "" ? 28 : document.aspnetForm.cycle.value); // defaults to 28
// validates cycle range, from 22 to 45
if (document.aspnetForm.cycle.value != "" && (document.aspnetForm.cycle.value < 22 || document.aspnetForm.cycle.value > 45)) {
  alert("Din månedscyklus er enten for kort eller for lang til, at \n"
    + "beregningen kan blive præcis. Vi vil alligevel forsøge at \n"
    + "foretage beregningen med dine angivne tal.");
}

// sets ovulation date to menstrual date + cycle days - 14 days
// the '*86400000' is necessary because date objects track time
// in milliseconds;  86400000 milliseconds equals one day
ovulation.setTime(menstrual.getTime() + (cycle*86400000) - (14*86400000));
document.aspnetForm.conception.value = dispISODate(ovulation);
//return false; // form should never submit, returns false
return false; // IE style

}


