
// *************************************************************************
// Original: This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.
//
// Modified: (9/2/07 for myPyramidMoms)
// --The input date is formatted as mm/dd/yyyy before calling this function
// --The function returns an empty string of error message if a valid date;
// non-empty string of error message if an invalid date
// *************************************************************************

function isDate(dateStr) {

var errMsg="";
var monthName=""

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");  
// unused. we restrict the input format as mm/dd/yyyy before calling this functiton.
//return false;
errMsg +=  "Please enter correct date input format"
}

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];


if (year < 2000 || year >= 2100) { // check month range
//alert("Month must be between 1 and 12.");
errMsg += "Please enter a valid year."
}

if (month < 1 || month > 12) { // check month range
//alert("Month must be between 1 and 12.");
errMsg += "Month must be between 1 and 12."
}

if (day < 1 || day > 31) {
//alert("Day must be between 1 and 31.");
errMsg += "Day must be between 1 and 31."

}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
//alert("Month "+month+" doesn't have 31 days!")

switch (month)
{
case "04": monthName="April";break;
case "06": monthName="June";break;
case "09": monthName="September";break;
case "11": monthName="November";break;
}
errMsg += monthName + " doesn't have 31 days!"
}

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("February " + year + " doesn't have " + day + " days!");
errMsg +=  "February in " + year + " doesn't have " + day + " days!"
}
}
return errMsg; // date is valid
}


function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}


function numOfCharInString (c, s)
{  
 var count=0;
 for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) 
        {count++;}
    }
    return count;
}



function checkDateFormat (c, s)   // user date input either in m/d/yy or mm/dd/yy
{ 
 
var numOfSlash=numOfCharInString(c,s);
//alert("numOfSlash"+numOfSlash);
for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c)
        //alert("charAT"+i);
        { 
        
         if (i==1)     // Slash position: (1,3) or (1,4)
          {
             if ((s.charAt(3)==c  || s.charAt(4)==c) && numOfSlash==2)
             return true;
          }
          if (i==2)   // Slash position: (2,4) or (2,5)
          {
             if ((s.charAt(4)==c  || s.charAt(5)==c) && numOfSlash==2)
             return true;
          }
          
           if (i==3)   // Slash positio: (3,5) or (3,6)
          {
             if ((s.charAt(5)==c  || s.charAt(6)==c) && numOfSlash==2)
             return true;
          }
       
        }
    }
    return false;
}



function getMonth(inputStr)           // mm/dd/yyyy input format
{
var date_array=inputStr.split("/");
return eval(date_array[0])-1; 
}

function getDay(inputStr)           // mm/dd/yyyy input format
{
var date_array=inputStr.split("/");
return eval(date_array[1]); 
}

function getYear(inputStr)          // mm/dd/yyyy input format
{
var date_array=inputStr.split("/");
if (date_array[2].length != 4)      // add prefix 20 if not entered the whole year
return eval("20" + date_array[2]);
else
return eval(date_array[2]); 
//return eval(date_array[2]);
}


function formatInputDate (inputDate)  // format user input date as mm/dd/yyyy for isDate function, it's not verified as a valid date
{
var errors;

var mm=getMonth(inputDate)+1;
var dd=getDay(inputDate);
var yyyy=getYear(inputDate);
var fmtDateStr;
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
fmtDateStr= mm + '/' + dd + '/' + yyyy;
return fmtDateStr;
}


function formatDate(dateObj){      // use for valid date

var dd = dateObj.getDate();
var mm = dateObj.getMonth()+1;     //January is 0!
var yyyy = dateObj.getFullYear();

if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
return mm + '/' + dd + '/' + yyyy;
//onload = function(){
//alert('dateObj is '+mm+'/'+dd+'/'+yyyy)
// }
}


function roundedToTwoHundred(kcal) {
return Math.round(kcal / 200) * 200;
}

function roundedToTenth(value) {
return Math.round(value * 10)/10;
}

function roundedToHundred(value) {
return Math.round(value * 100)/100;
}

function heightInInches(inputHeightFeet,inputHeightInches) {
var totalInches=parseInt(inputHeightFeet * 12) + parseInt(inputHeightInches);
//alert(totalInches);
return totalInches;
}

function convertToMeter(heightInInches) {
var inchToMeterConversionRate=0.0254;
var heightInMeter;
//heightInMeter= Math.round((heightInInches * inchToMeterConversionRate*100),2)/100;       //1.6256 rounded to 1.63
heightInMeter= roundedToHundred(heightInInches * inchToMeterConversionRate); 
//alert(heightInMeter);
return heightInMeter;
}

function convertToKilogram(weightInlbs) {
var lbsToMgConversionRate=0.45359237;
var weightInKg;
weightInKg= Math.round((weightInlbs * lbsToMgConversionRate*100),2)/100;                   //54.431 rounded to 54.43
//weightInKg= roundedToHundred(weightInlbs * lbsToMgConversionRate);  
//alert(weightInKg);
return weightInKg;
}


function BMI(heightInM,weightInKg) {
var BMI;

BMI=weightInKg/(heightInM * heightInM);
BMI= roundedToTenth(weightInKg/(heightInM * heightInM));                                   //20.48 rounded to 20.5
//alert("BMI=" + BMI);
BMI= weightInKg/(heightInM * heightInM); 
return BMI;
}


function BMI2(pHeightFeet,pHeightInch,pWeight) {
var BMI,pHeight,pH,pW;
var pHeight=heightInInches(pHeightFeet,pHeightInch);
pH=convertToMeter(pHeight);
pW=convertToKilogram(pWeight);
BMI=pW/(pH * pH);
//BMI= roundedToTenth(pW/(pH * pH));                         //20.48 rounded to 20.5
return BMI;
}




function convertToLb(weightInkg) {
var kgToLbConversionRate=2.20462262;
var weightInLb;
weightInLb= Math.round(weightInkg * kgToLbConversionRate);                   //116.844 rounded to 117
//weightInKg= roundedToHundred(weightInlbs * lbsToMgConversionRate);  
//alert("wgt=" + weightInLb);
return weightInLb;
}



function weightAtBMI(pHeightFeet,pHeightInch,BMI) {
var pHeight,pH,pW,pWlb;
var pHeight=heightInInches(pHeightFeet,pHeightInch);
pH=convertToMeter(pHeight);
pW=BMI * (pH * pH);
pWlb=convertToLb(pW);
return pWlb;
}

function getMonth2(inputStr)           // mm/dd/yyyy input format
{
var date_array=inputStr.split("/");
return eval(date_array[0]); 
}




