

// === Version Detection ===
iv_isDOM  = (document.getElementById);
iv_isNS4  = (document.layers);
iv_isIE   = (document.all);
iv_isIE4  = (iv_isIE && !iv_isDOM);
iv_isMac  = (navigator.appVersion.indexOf("Mac") != -1);
iv_isIE4M = (iv_isIE4 && iv_isMac);
iv_isIE5M = iv_isDOM && iv_isIE && iv_isMac;
iv_isIE5W = iv_isDOM && iv_isIE && !iv_isMac;
iv_isNS6 =  iv_isDOM && (navigator.vendor == ("Netscape6") || navigator.product == ("Gecko"));
iv_verticalID = "th";
iv_sageHost = "https://subscriber.ivillage.com";
iv_channelHomepageURL = "http://health.ivillage.com";
var agt=navigator.userAgent.toLowerCase();
is_safari = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;

// === Scriptlets ===
if (typeof iv_scriptletIncluded_navigation == "undefined") { document.write("<scr"+"ipt language=\"javascript\" type=\"text/javascript\" src=\"http://a820.g.akamai.net/f/820/822/1d/www.ivillage.com/shared/js/scriptlets/0,,navigation,00.js\"></scr" + "ipt>"); var iv_scriptletIncluded_navigation = 1; }
if (typeof iv_scriptletIncluded_search == "undefined") { document.write("<scr"+"ipt language=\"javascript\" type=\"text/javascript\" src=\"http://a820.g.akamai.net/f/820/822/1d/www.ivillage.com/shared/js/scriptlets/0,,search,00.js\"></scr" + "ipt>"); var iv_scriptletIncluded_search = 1; }
if (typeof iv_scriptletIncluded_cookies == "undefined") { document.write("<scr"+"ipt language=\"javascript\" type=\"text/javascript\" src=\"http://a820.g.akamai.net/f/820/822/1d/www.ivillage.com/shared/js/scriptlets/0,,cookies,00.js\"></scr" + "ipt>"); var iv_scriptletIncluded_cookies = 1; }
if (typeof iv_scriptletIncluded_cta == "undefined") { document.write("<scr"+"ipt language=\"javascript\" type=\"text/javascript\" src=\"http://a820.g.akamai.net/f/820/822/1d/www.ivillage.com/shared/js/scriptlets/0,,cta,00.js\"></scr" + "ipt>"); var iv_scriptletIncluded_cta = 1; }
if (typeof iv_scriptletIncluded_utilities == "undefined") { document.write("<scr"+"ipt language=\"javascript\" type=\"text/javascript\" src=\"http://a820.g.akamai.net/f/820/822/1d/www.ivillage.com/shared/js/scriptlets/0,,utilities,00.js\"></scr" + "ipt>"); var iv_scriptletIncluded_utilities = 1; }
if (typeof iv_scriptletIncluded_json == "undefined") { document.write("<scr"+"ipt language=\"javascript\" type=\"text/javascript\" src=\"http://a820.g.akamai.net/f/820/822/1d/www.ivillage.com/shared/js/scriptlets/0,,json,00.js\"></scr" + "ipt>"); var iv_scriptletIncluded_json = 1; }
if (typeof iv_scriptletIncluded_nbc == "undefined") { document.write("<scr"+"ipt language=\"javascript\" type=\"text/javascript\" src=\"http://a820.g.akamai.net/f/820/822/1d/www.ivillage.com/shared/js/scriptlets/0,,nbc,00.js\"></scr" + "ipt>"); var iv_scriptletIncluded_nbc = 1; }
if (typeof iv_scriptletIncluded_flash == "undefined") { document.write("<scr"+"ipt language=\"javascript\" type=\"text/javascript\" src=\"http://a820.g.akamai.net/f/820/822/1d/www.ivillage.com/shared/js/scriptlets/0,,flash,00.js\"></scr" + "ipt>"); var iv_scriptletIncluded_flash = 1; }
if (typeof iv_scriptletIncluded_interstitial == "undefined") { document.write("<scr"+"ipt language=\"javascript\" type=\"text/javascript\" src=\"http://a820.g.akamai.net/f/820/822/1d/www.ivillage.com/shared/js/scriptlets/0,,interstitial,00.js\"></scr" + "ipt>"); var iv_scriptletIncluded_interstitial = 1; }
// === Send To A Friend OR "Email this page" functionality===
// iv_openS2F - Opens S2F window. Use iv_S2FOverride to override standard page location.
function iv_openS2F() {
 
    iv_S2FUrl = typeof iv_S2FOverride == "undefined" ? window.document.location.href : iv_S2FOverride;
	    

    var w = 505;
    var h = 610;
    var cw = 252;
    var ch = 203;

    if (window.screen) {
        cw = Math.floor((screen.availWidth - w) / 2);
        ch = Math.floor((screen.availHeight - h) / 3);
    }

    var mywin = window.open ('http://www.ivillage.com/s2f/form/0,,j,00.html?s2fURL=' + escape(iv_S2FUrl) + '&s2fTitle=' + escape(window.document.title), 's2f', 'width=505,height=610,top=' + ch + ',left=' + cw + ',scrollbars=yes');
} 


// === ZAG ===
// iv_SetdccCookie - Sets the ZAG cookie
// @param zip
// @param age - Year of birth 
// @param gender - 'M' or 'F'
// @return  nothing
function iv_SetdccCookie (zip, age, gender) {
  if (zip == null) { zip = ""; } 
  if (age == null) { age = ""; }
  if (gender == null) { gender = ""; }
  iv_dcc= "Z:" + zip + "|A:"  + age + "|G:"  + gender;
  iv_setCookie("iv_dcc", iv_dcc, 3600, "/", ".ivillage.com");
}



// === Fourth column astrology ===
// iv_SetSunSign - Used by fourth column to set ZAG cookie. 
// @param sunSign
// @param year 
// @return  nothing
function iv_SetSunSign (sunSign, year) {
  iv_SetdccCookie("", "", year);
  iv_setCookie("iv_sunsign", sunSign, 3600, "/", ".ivillage.com");
  iv_doTracking("iv,4c,iv,as_go,hor");
}

// === CIMS form handler ===
// Checks the email field, and sets cid and cid fields. All CIMS forms MUST have 
// these fields.
function processCIMS (theForm,popUp) {
   // See if we have any spaces.
	var space = theForm.email.value.indexOf(" ");
	if (space != -1) { 
	alert("Please check your email address");
	return false; 
	}
	
	var pos1 = theForm.email.value.indexOf("@");
    if (pos1 < 1) { 
	alert("Please check your email address");
	return false; 
	}
    var email = theForm.email.value.substr(pos1 + 1);
    var pos2 = theForm.email.value.indexOf(".");
    if (pos2 < 1 || theForm.email.value.substr(pos2 + 1) == "" || (pos2 - pos1) == 1) { 
	alert("Please check your email address");
	return false; 
	}
	

  if (typeof(cimsCid) != 'undefined') {
    theForm.cid.value=cimsCid;
  }
  
  if (typeof(cimsUid) != 'undefined') {
	theForm.uid.value=cimsUid;
  }
  
  // Append the email address to the rurl.
  //theForm.rurl.value += "?email=" + theForm.email.value;
  
  // If popUp is specified, open CIMS in a new window.
  if (popUp == 'y') {
    cimsWindow = window.open('','cimsWindow',"WIDTH=770,HEIGHT=600,scrollbars=yes,toolbar=yes,resizable=yes,location=yes,menubar=yes,statusbar=yes"); 
    theForm.target = 'cimsWindow';
    return true;
  } else {
    return true;
  }
}

function pollSubmit (myForm) {

  var leftplacement = screen.width / 2 - 152;
  var topplacement = screen.height / 2 - 95;
  var myWin = window.open('', 'pollResultsWindow', 'HEIGHT=190,WIDTH=305,top=' + topplacement + ',left=' + leftplacement);
  myForm.target='pollResultsWindow';
  myForm.submit();
  myWin.focus();
  return true;
}

// === Open Window Wrapper ===
// iv_OpenWin - Used to open link to new window. 
// @param url
// @param params
// @return  nothing
function iv_OpenWin(url, params) {
   window.open(url, 'iv_window', params);
}

// === Open Window JS ===
// iv_OpenWin - Used to open link to new window. 
function iv_pleasedontGo(box) {
  if (box.selectedIndex == -1) { alert('Please select a value first.'); return; }
  val = box.options[box.selectedIndex].value; window.open (val,'_top'); 
}
  

// Joins numerous form values into one string. 
// Delimiters (if any) should be set in the individual form values
// Created for sweeps entry.

function concatEntryData(thisForm) {

	//create temporary string
	tempString = new String();
    // Test if field exists
    if (!thisForm.txtEntryNames) {
        alert("DEVELOPER ERROR: Missing entry data inputs field.");
        return false;
    }

    if (trimSpace(thisForm.txtEntryNames.value) == "") {
        alert("DEVELOPER ERROR: Required entry data inputs field is empty.");
        return false;
    }

    var entryInputs = thisForm.txtEntryNames.value.split(" ");
	
	
	for (var i = 0; i < entryInputs.length; i++) { 
		var formItem = entryInputs[i];
		var concatList = new Array;   // sublist of form fields to validate
		
		if (formItem.indexOf("*") > 0) {
            formItem = formItem.replace(/\*/,"1");  // dynamic node array is based on first element name
            var curObject = eval("thisForm." + formItem);
            // object will not have a name if it is an array (possible if only 1 element in multi)
            if (!curObject.name) {
                for (var j = 0; j < curObject.length; j++) { concatList[formItem + "[" + j + "]"] = 1; }
            } else {
                concatList[formItem] = 1;
            }
        } else {
            concatList[formItem] = 1;
        }
		
		for (formElement in concatList) {
				
            curObject = eval("thisForm." + formElement);
			
            if (!curObject) {
                alert("DEVELOPER ERROR: " + formElement + " is an non-existant form element.");
                return false;
            }
			
			// check text and textareas
            if (curObject.type == "text" || curObject.type == "textarea" || curObject.type == "password" || curObject.type == "hidden") {
                var tempValue = trimSpace(curObject.value);
                tempString += tempValue;
				
						
            } else if (curObject.type == "select-one"  || curObject.type == "select-multiple") {
				tempValueIndex = curObject.selectedIndex;
				tempValue = curObject[tempValueIndex].value;
				tempString += tempValue;
				
				
            } else if (curObject.type == "radio" || curObject.type == "checkbox") {
                tempValue = curObject.checked.value;
                tempString.concat(tempValue);  
				    
            } else if (!curObject.type) {
				var multiValue;
				tempValue = '';
                for (var j = 0; j < curObject.length; j++) {
                    if (curObject[j].checked) {
					multiValue = curObject[j].value;
					tempValue += multiValue;
					}
                }
			tempString += tempValue;
                
            }
		}		
	}
thisForm.entryData.value = tempString;
}	


// isValidEmail - Checks to see if email format is correct.
// @param email Email address to validate.
// @returns boolean depending on success.
function isValidEmail(email) {
    // See if we have any spaces.
	var space = email.indexOf(" ");
	if (space != -1) { return false; }

    // max length of email
    if (email.length > 75) { return false; }

    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!email.match(re)) { return false; }
    return true;
}

// @param thisForm Form object to be tested
// @param popUp checks for popUp window
// @return true/false
function doNewsletterSubmit(theForm,popUp) {
    theForm.ip.value = document.location.href;
    theForm.action="https://subscriber.ivillage.com/sagews"    //Get Server Environment
	if (!isValidEmail(theForm.email.value)) {
	  alert("Enter valid email address");
	  return false;
    }	

    if (theForm.r.value == "") { theForm.r.value = "http://www.ivillage.com/ivillage/thankyou/newsletters"; }
	if (popUp == 'y') {
	    sageWindow = window.open('','sageWindow',"WIDTH=500,HEIGHT=500,scrollbars=yes,toolbar=yes,resizable=yes,location=yes,menubar=yes,statusbar=yes"); 
    	theForm.target = 'sageWindow';
		theForm.submit();
    	return false;
  	} else {
		theForm.submit();
    	return false;
  	}
}

// @param thisForm Form object to be tested
// @param popUp checks for popUp window
// @return true/false
function doFunnel4Submit(theForm,popUp) {
    var redirectURL = document.location;
    theForm.action="https://subscriber.ivillage.com/funnels/4?vrt=" + iv_verticalID + "&dd=" + escape(redirectURL);
	if (!isValidEmail(theForm.email.value)) {
	  alert("Enter valid email address");
	  return false;
    }	

    if (popUp == 'y') {
	    sageWindow = window.open('','sageWindow',"WIDTH=720,HEIGHT=500,scrollbars=yes,toolbar=yes,resizable=yes,location=yes,menubar=yes,statusbar=yes"); 
    	theForm.target = 'sageWindow';
		theForm.submit();
    	return false;
  	} else {
	    return true;
  	}
}

// javascript code needed for the Glossary dictionary
// @param index accepts ID for glossary
function iv_showDef(index) { 
    if (index != 'null') { 
        window.open('/glossary/definition/0,,' + index + ',00.html', 'glossary', 'width=350,height=180,scrollbars');
    } 
}

//sets pixel for 4th column tracking
function iv_set4thImp() {
    return;			
}

// === Sweeps Submit ===
// iv_sweepsSubmit Used to submit sweeps form. 
// @param frm form being submitted to webservice
function iv_sweepsSubmit(frm) {
    frm.action="https://subscriber.ivillage.com/sagews"    //Get Server Environment
    frm.submit();
    return false;
}

function iv_sweepsPostSubmit(frm) {
    frm.action="https://subscriber.ivillage.com/sagews"    //Get Server Environment
	frm.method = "post";
	frm.submit();
    return false;
}

// === cobrand stub
// used to prevent js errors if the arrival js is not included. 
function iv_drawCobrand() {}

	
// === following functions are used in iVillage Refresh and iVillage Live templates ===
// adding this onclick function to set the current page as default homepage in I.E.
function iv_setHomepage() {
		document.body.style.behavior='url(#default#homepage)';		
		document.body.setHomePage(window.document.location.href);
}

//=== CALLED FROM THE TOP LEVEL iVillage Live TEMPLATE'S WINDOW ONLOAD EVENT ===
function CS_displayHomePageLink(login)
{
	if (iv_isIE) {
		if (login == false) {
			var divider = document.createTextNode(' | ');
			document.getElementById('topJoinNow').appendChild(divider);
		}
		var homepageLink = document.createTextNode('MAKE THIS MY HOMEPAGE');
		var anchorTag = document.createElement('a');		
		anchorTag.href = '#';		

		anchorTag.onclick = iv_setHomepage;
		
		anchorTag.className = 'globalNavLink2';
		anchorTag.appendChild(homepageLink);
		document.getElementById('topJoinNow').appendChild(anchorTag);
	}
}

function CS_clearField(field, text)
{
	if(field.value == text)
	{
		field.value = '';
	}
}

function CS_fillField(field, text)
{
	if(field.value == '')
	{
		field.value = text;
	}
}

function CS_hoverButtonGo(button,state)
{
	if(state=='active')
	{
		button.style.backgroundColor = '#EF6F0F';
	}
	else
	{
		button.style.backgroundColor = '#C84865';
	}
}

// === this function is used in iVillage Live Flash header navigation ===
function iv_handleFlashSearch(restrictStr, searchStr) {
  if (searchStr == "") {
    alert('Your search could not be completed because no search words were entered.');
  } else {
    if (restrictStr == "web") {
	  var sendValues = "sk=ivi&restrict=" + restrictStr + "&q=" + searchStr;
	  document.location.href = iv_searchWebUrl + '?' + sendValues;
    } else if (restrictStr == "ivillage") {
	  var sendValues = "sk=ivi&restrict=" + restrictStr + "&q=" + searchStr;
	  document.location.href = iv_searchIVUrl + '?' + sendValues;	  
	} else {
	  var sendValues = "sk=ivi&restrict=" + restrictStr + "&q=" + searchStr;
	  document.location.href = iv_searchIVUrl + '?' + sendValues;
	}
  }  
}