
Date.prototype.getTimeDiff = function(unit, dateRef, doNotRound) {
	if(!(dateRef instanceof Date)) {
		return
	}
	
	this.unitValues = this.getUnitsInMilliseconds();

	//offset value always in minutes
	var offset = this.unitValues["mi"] * (this.getTimezoneOffset() - dateRef.getTimezoneOffset());
	
	var oms = dateRef.getTime();
	var mms = this.getTime();

	var diff = mms - oms;
	diff -= offset;
	return (Math.floor(diff / this.unitValues[unit]));
}

Date.prototype.getUnitsInMilliseconds = function() {
	var obj = new Object();
	obj.ms = 1;
	obj.ss = 1000;
	obj.mi = obj.ss * 60;
	obj.hh = obj.mi * 60;
	obj.dd = obj.hh * 24;
	obj.wk = obj.dd * 7;
	obj.mm = obj.dd * 30;
	obj.yyyy = obj.dd * 365;
	return obj;
}

//------------------------------------------------------
// Will add the amount of time specified to the current date
// object in the units passed in. See above for supported
// units.
Date.prototype.addTime = function(unit, amt)
{
	var passedDateOffset = this.getTimezoneOffset();
	
	if(isNaN(amt))
		return
	else
		var amt = Number(amt);

	if(!this.customInited)
		this.customInit();

	if(unit == "mm") {
		this.setMonth( this.getMonth() + amt );
	} else if(unit == "yyyy") {
		this.setFullYear( this.getFullYear() + amt );
	} else {
		var actual = this.unitValues[unit] * amt;
		this.setTime( this.getTime() + actual );
	}
	var newDateOffset = this.getTimezoneOffset();
	var offset = this.unitValues["mi"] * (passedDateOffset - newDateOffset);
	
	this.setTime( this.getTime() + offset );
}

Date.prototype.customInit = function()
{
	this.unitValues = this.getUnitsInMilliseconds();
	this.customInited = true;
}
//------------------------------------------------------





today = new Date();

// deleting iv_par cookie because as of 1/26/05, parenting info will be saved in session based cookie only
iv_deleteCookie('iv_par','/',".ivillage.com");

monthArray = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
weekArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

//Base 64 decryption code starts here
var base64 = [
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' ];


// ***** SWITCH BASE64 CODE ***** 
function switchBase64 () {
  var zz = new Object();
  for (var i = 0; i < 64; i++) {
    zz[base64[i]] = i;
  }
  return zz;
}
var switchedBase64 = switchBase64();

// ***** DECODE THE STRING *****
function decode (B64Str) {
  if (B64Str == "") {
    return ""
  }
  var charIs = new Array();
  var NewStr = "";
  for (var i = 0; i < B64Str.length; i++){
    charIs[i] = switchedBase64[B64Str.charAt(i)];
  }
  for (var i = 0; i < B64Str.length; i += 4) {
    var b24  = (charIs [i] & 0xFF) << 18; 
    b24 |= (charIs[i + 1] & 0xFF) << 12; 
    b24 |= (charIs[i + 2] & 0xFF) << 6;
    b24 |= (charIs[i + 3] & 0xFF) << 0;
    NewStr += String.fromCharCode((b24 & 0xFF0000) >> 16);
    if (B64Str.charAt(i + 2) != '='){
	  NewStr += String.fromCharCode((b24 & 0xFF00) >> 8);
    }
    if (B64Str.charAt(i + 3) != '='){
	  NewStr += String.fromCharCode((b24 & 0xFF) >> 0);
    }
  }
  return NewStr;
}


//checks to see if the user is logged in
function isLoggedIn() {
	var iv_loggedin = iv_getCookie("iv_tkt");
	if (!iv_loggedin || iv_loggedin == "NA") {
		return false;
	}
return true;
}



//returns bool based on if date passed is a due date; 
function isPregnant(dateObj) {

	daysPreg = dateObj.getTimeDiff('dd',new Date());
	if (daysPreg >= 279) {
		return false;
	}
return true;
}

function stillPreg(dateObj) {
	if (dateObj.getTime() <= today.getTime()) {
		return false;
	}
return true;
}

function validatePreg(formObj) {
	
	year = formObj.year[formObj.year.selectedIndex].value;
	month = formObj.month[formObj.month.selectedIndex].value;
	day = formObj.day[formObj.day.selectedIndex].value;
	tempMonth = month;
	tempDay = day;
	if(!iv_isValidDate(tempDay,tempMonth,year)) {
	return false;
	}
	month = tempMonth;
	day = tempDay;
	
	bDateObj = new Date(month+'/'+day+'/'+year);
	//alert(bDateObj);
	
	if (!isPregnant(bDateObj)) {
		messageTxt = '<p><span class="errTxt">You have entered a due date that is more than 40 weeks in the future. Please check your information. If you are trying to get pregnant, please visit our <a href="http://parenting.ivillage.com/ttc/topics/0,,4rwd,00.html">Trying to Conceive area</a> for tools and information created with your needs in mind.</span></p><br />';
		
		if(messageTargetRef!=null){
			messageTargetRef.innerHTML = messageTxt;
		}else{
			//alert('cannot find target');
		}
	return false;
	}
	
	if (!stillPreg(bDateObj)) {
		messageTxt = '<p><span class="errTxt">You have entered a due date that has already passed. Please check your information. If you have a newborn or baby, please try out our <a href="http://parenting.ivillage.com/baby/calendar">First Year of Life Calendar</a>!</span></p><br />';
		if(messageTargetRef!=null){
			messageTargetRef.innerHTML = messageTxt;
		}else{
			//alert('cannot find target');
		}
		return false;
	}
	
	bDateStr = "s=|n1=|b1="+year+month+day;	
	iv_setCookie("iv_tpar", bDateStr, "", "/", ".ivillage.com");
	//give time for cookie setting i/o
	setTimeout("goLogin()",500);
	
	return false;
}