//////////////////////////////////////////////////////
//				Start Ofie Handler Code
/////////////////////////////////////////////////////

function HostPageLoaded()
{
	CM_bPageLoaded=true
}

function SetParams(n)
{		
	ofie = CM_arrOfies[n]

  	//*** SET MOTION PARAMETERS HERE***  (x1,y1) -> (x2,y2) = Start/End Pos.Relative to Trigger 
	// tmrInterval = motion freq in millisec, speed=pix/sec (in theory only -- mozilla timers are less responsive)
	//	set imgTrigOn.src only if there is a rollover image on the trigger
	// Behavior (form opens on)  {1=mouseover, 2=click, 3=mouseover once/click thereafter on page, 4=mouseover once, click thereafter throughout session across site}

	with (ofie)
	{
		Behavior = 4;
		tmp.HideDropDownName = 'archive_list';
		x1 = -370;	y1 = -245;
		x2 = -370; y2 = 78;
		speed = (CM_isIE) ? 300 : 600;
		tmrInterval = (CM_isIE) ? 30 : 40;
		tmrAutoCloseSec = -1;
		clipX = 0;	clipY = 78;

		imgTrigOn.src = 'https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_tab_on.gif'
	}
}

function attachEvents(n)
{
	ofie = CM_arrOfies[n]
	if (!ofie) 	return //FatalError()
	if (tmpTrig = ofie.parentNode.firstChild)		//tmp = ofie.parentElement.childNodes("objTrigger")
	{
		addEvent(tmpTrig, "mouseover", Function("MouseOverTrigger("+n+")"))
		addEvent(tmpTrig, "mouseout", Function("MouseOutTrigger("+n+")"))
		addEvent(tmpTrig, "click", Function("TriggerClick("+n+")"))		
		if (tmpTrig.style) {tmpTrig.style.cursor = "url,pointer,hand"; ;}
	}
	
	var tmpImgSwitch;
	tmpImgs = tmpTrig.getElementsByTagName("img")
	for (i=0; i<tmpImgs.length; i++) {if (tmpImgs.item(i).id=='imgSwitch') {tmpImgSwitch=tmpImgs.item(i); break;}}
	if (tmpImgSwitch)
	{
		ofie.imgTrig = tmpImgSwitch;
		ofie.imgTrigOff.src = tmpImgSwitch.src;
	}
	
	var tmpBtnClose;
	tmpDivs = ofie.getElementsByTagName("div")
	for (i=0; i<tmpDivs.length; i++) {if (tmpDivs.item(i).id=='btnClose') {tmpBtnClose=tmpDivs.item(i); break;}}
	if (tmpBtnClose) 
	{
		addEvent(tmpBtnClose, "click", Function("CloseForm("+n+")"))
		if (tmpBtnClose.style) tmpBtnClose.style.cursor = "url,pointer,hand"
	}
	
	addEvent(ofie.parentNode, "keydown", Function("ClearAutoCloseTimer("+n+")"))
	addEvent(ofie.parentNode, "keyup", Function("StartAutoCloseTimer("+n+")"))
	addEvent(ofie.parentNode, "mouseover", Function("ClearAutoCloseTimer("+n+")"))
	addEvent(ofie.parentNode, "mouseout", Function("StartAutoCloseTimer("+n+")"))
}

function MouseOverTrigger(n)
{
	ofie = CM_arrOfies[n]
	with (ofie)
	{
		if (!CM_bPageLoaded) return
		if (ofie.imgTrigOn.src !='') ofie.imgTrig.src = ofie.imgTrigOn.src
		switch(Behavior)
		{
			case 1:	if ( (!bFormOpen) && (!bInFlight) ) OpenForm(n)
			case 2:	return
			case 3:	if ( (!bFormOpen) && (!bInFlight) && (!bFormEverOpened) ) OpenForm(n)
			case 4: if ( (!bFormOpen) && (!bInFlight) && (!/CM_ofieShown/.test(document.cookie)) ) OpenForm(n)
		}
	}
}
function MouseOutTrigger(n)
{
	ofie = CM_arrOfies[n]
	if (ofie.imgTrigOn.src !='') ofie.imgTrig.src = ofie.imgTrigOff.src
}

function TriggerClick(n)
{
	ofie = CM_arrOfies[n]
	if (CM_bPageLoaded)  OpenForm(n)
}

function OpenForm(n)
{
	ofie = CM_arrOfies[n]
	if ((!ofie) || (!CM_bPageLoaded)) return;

	with (ofie)
	{	
		if (HideDropDownName) DocDropDownsDisp(false,HideDropDownName)
		if (bInFlight) clearTimeout(tmrID);
		ClearAutoCloseTimer(n)
		bInFlight = true; bFormEverOpened = true; setCookie('CM_ofieShown', '1','SESSION', '/', CMdomain, false);
	
		dist = Math.sqrt( Math.pow((y2-y1),2) + Math.pow((x2-x1),2) )
		totSteps = dist/(speed*(tmrInterval/1000) )
	
		xPos=style.top =x1;	yPos=style.left =y1; 
		style.display="block";
		xStep = (x2-x1) / totSteps; yStep = (y2-y1) / totSteps;
		StartAutoCloseTimer(n)
		MoveForm(n)
	}
}

function MoveForm(n)
{
	ofie = CM_arrOfies[n]
	with (ofie)
	{
		if ( (parseInt(style.left) == x2) && (parseInt(style.top) == y2) ) {bInFlight = false; bFormOpen=true; return;} //we made it, we're there 
		
		xPos += xStep; 	yPos += yStep;
		if ( ((xStep>0) && (xPos>x2)) || ((xStep<0) && (xPos<x2)) ) xPos = x2
		if ( ((yStep>0) && (yPos>y2)) || ((yStep<0) && (yPos<y2)) ) yPos = y2
		
		if ( (clipX) || (clipY) )
		{
			var tp=rt=btm=lft="auto"
			if (clipX>1) {	(xStep>1) ? lft=clipX-xPos : rt=clipX-xPos}
			if (clipY>1) {	(yStep>1) ? tp=clipY-yPos : btm=clipY-yPos}
			style.clip="rect("+tp+","+rt+","+btm+","+lft+")"
		}
		style.left = xPos; style.top = yPos;
		tmrID = setTimeout("MoveForm("+n+")",tmrInterval)
	}
}

function CloseForm(n)
{
	ofie = CM_arrOfies[n]
	if (ofie.tmrID) clearTimeout(ofie.tmrID);
	if (ofie.HideDropDownName) DocDropDownsDisp(true, ofie.HideDropDownName)
	ClearAutoCloseTimer(n)
	if (tmpFrm  = document.forms["subsOrderForm"+n]) tmpFrm.reset()
	ofie.bFormOpen = false;
	ofie.style.display = "none";
}
function StartAutoCloseTimer(n)
{
	ofie = CM_arrOfies[n]
	if (ofie.tmrAutoCloseSec<=0) return
	clearTimeout(ofie.tmrCloseFormID);
	ofie.tmrCloseFormID = setTimeout("CloseForm("+n+")", ofie.tmrAutoCloseSec*1000)
}
function ClearAutoCloseTimer(n)
{
	ofie = CM_arrOfies[n]
	if (ofie.tmrCloseFormID) clearTimeout(ofie.tmrCloseFormID);
}

function ID(objID)
{
	if (document.getElementById) return document.getElementById(objID)
	else if (document.all) return document.all[objID]
	else return false
}

function addEvent(obj, evType, fn)
{ 
	if (obj.addEventListener) 	{obj.addEventListener(evType, fn, true); return true;}
	else if (obj.attachEvent)	{var r = obj.attachEvent("on"+evType, fn); return r; }
	else return false; 
}

function DocDropDownsDisp(bShow, sName)
{
	DropDowns = document.getElementsByTagName("select")
	for (i=0; i<DropDowns.length; i++)
	{
		if (DropDowns[i].name == sName){
			if (bShow) DropDowns[i].style.visibility = "visible";
			if (!bShow) DropDowns[i].style.visibility = "hidden"
		}
	}
}




var CMhost=self.location.hostname.toLowerCase().split(".");
var CMdomain;

if(CMhost.length>2){
  if(/com|edu|net|org|gov|mil|int/.test(CMhost[CMhost.length-1]))CMdomain=CMhost.slice(CMhost.length-2).join(".");
  else if(CMhost.length>3)CMdomain=CMhost.slice(CMhost.length-3).join(".");
}



function setCookie(cookieName, cookieValue, expires, path, domain, secure){

  switch(typeof expires == "string"){
    case false:  expires = '; EXPIRES=' + expires.toGMTString();
    break;
    case true:   expires = (expires == "KILL") ? ("; EXPIRES=Thu, 01-Jan-1970 00:00:01 GMT") : (expires == 'SESSION') ? ('') : ("; EXPIRES=" + expires);  
    break;
    default:     expires = '';
    break;
  }

  document.cookie = escape(cookieName) + '=' + escape(cookieValue)
    + (expires)
    + (path ? '; PATH=' + path : '')
    + (domain ? '; DOMAIN=' + domain : '')
    + (secure ? '; SECURE' : '');

}




//////////////////////////////////////////////////////
//					End Ofie Handler Code
/////////////////////////////////////////////////////


///////////////////////////////////////////////////////////
// Global variables used for form labeling and validation
// Begin client detection
var CM_na = parseFloat(navigator.appVersion);   
var CM_napN = navigator.appName.toLowerCase();
var CM_nu = navigator.userAgent.toLowerCase();

//OS Detection
var CM_isMac=(CM_nu.indexOf("mac")!=-1)? true : false;
var CM_NS = (CM_nu.indexOf("netscape") != -1)? true : false;
var CM_NS6 = (CM_nu.indexOf("netscape6") != -1)? true : false;

var CM_isIE = document.all ? true : false;

//bad browser/OS combos
var CM_isMacIE = CM_isMac && CM_isIE;
var CM_isDom = false;
        
if (!CM_isMacIE){
  CM_isDom = document.getElementById ? true : false;
}

// Data handling functions
function ofieSubmit2(theForm,n) {
  if (ofieCheck2(theForm)) {
  	//take out 'Email:' string:
	//formLabels = CM_arrOfies[n].formLabels
	 //if (theForm.email.value == formLabels["email"][0] || theForm.email.value == formLabels["email"][1]) theForm.email.value = "";
    var upsellWin = window.open("", "upsellWin", "width=400 ,height=500,top=2,left=2,scrollbars=yes,status=yes,resizable=yes");
   theForm.target = "upsellWin";
    window.setTimeout("CloseForm("+n+")", 3500);
    return true;
  }
  else return false;
}

//form validation
function ofieCheck2(f){
   f = f.elements;
   var state = f["state"].value;
   var canstates = ['AB','BC','MB','NB','NL','NS','NT','ON','PE','QC','SK','YT']
	
   var t, msg, good;
   msg = '';
	
    var t, msg, good;
    msg = '';
	if ((f["firstName"].value == null) || (f["firstName"].value == '') || CM_isblank(f["firstName"].value)) {msg += 'Please enter your First Name.\n';}
	if ((f["lastName"].value == null) || (f["lastName"].value == '') || CM_isblank(f["lastName"].value)) {msg += 'Please enter your Last Name.\n';}
	if ((f["address1"].value == null) || (f["address1"].value == '') || CM_isblank(f["address1"].value)) {msg += 'Please enter your Address.\n';}	
	if ((f["city"].value == null) || (f["city"].value == '') || CM_isblank(f["city"].value)) {msg += 'Please enter your City.\n';}	

	if (state=='')msg+='Please enter your State/Province.\n'

	//validate zipCode
	good=1; t = f["zipCode"].value;
	if (t==null || t=='' || CM_isblank(t) || t.length<5 || t.length>6 )good=0;
	else{if (state!=''){
			if (t.length==5 && (t.search(/^\d{5}$/) != -1)){ 
				for (d = 0; d<canstates.length;d++){
					if (state==canstates[d])good=0;
				}
			}else good=0;
			if (t.length==6 && t.search(/^[a-zA-Z]\w{5}$/)!=-1){
				good=0;
				for (d = 0; d<canstates.length;d++){
					if (state == canstates[d])good=1;
				}
			}
		}
	}
     
	if (!good){msg+='Zip/Postal code is blank or incorrect,\nor it doesn\'t match the State/Province you selected.\n';}
	
	//validate email
    good=1; t=f["email"].value;
	if (t==null || t=='' || CM_isblank(t)) good=0;
	else good=(t.search(/^(\w|-)+(\.(\w|-)+)*@((\w|-)+\.)+[a-zA-Z]{2,3}$/)==-1)?0:1;
    
	if(!good){msg+='Your E-mail address is blank or incorrect.\n\n';}

	if (msg.length) {
		alert ('The form was not submitted because of the following error(s):\n\n' + msg + '\n\nPlease correct these error(s) and resubmit your form.\n\nThank you,\nPARENTING MAGAZINE');
		return false;
	}
	return true;
}


function CM_isblank(s){
	for(var i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
return true;
}
// End of Data handling functions


function writeOfie(n)
{
	// Array of US States / Canada Provinces
	var usStates = ["AA", "AE", "AK", "AL", "AP", "AR", "AS", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "GU", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VI", "VT", "WA", "WI", "WV", "WY"];
	var canProvs = ["AB", "BC", "MB", "NB", "NL", "NS", "NT", "NU", "ON", "PE", "QC", "SK", "YT"];
	// Array of all States/Provinces
	var statesProvs = usStates.concat(canProvs).sort();
	var sFormPath = "https://subs.timeinc.net/"
	
	var stateOptionsList = "";
	for(var i = 0; i < statesProvs.length; ++i){
		stateOptionsList += '<option value="'+statesProvs[i]+'">'+statesProvs[i]+'</option>';
	}


	sPgLoc = document.location.href
	if (sPgLoc.indexOf("/develop/subs2") !=-1) sFormPath = "https://dev.subs.timeinc.net/" //vk - local dev
	if (sPgLoc.indexOf("timeinc.net/subs2/dev/") !=-1) sFormPath = "https://dev.subs.timeinc.net/"
	if (sPgLoc.indexOf("timeinc.net/subs2/stage/") !=-1) sFormPath = "https://stage.subs.timeinc.net/"

	
var cssFormtxt = "font-family: Verdana, Arial, sans-serif; font-size: 10px; line-height: 5px; color: #000000; text-decoration: none; margin-top: 10px; margin-right: 0px; margin-bottom: 2px; margin-left: 0px; padding-top: 10px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px;"
var cssFormfield = "font-family: Verdana, Arial, sans-serif; font-size: 10px; color: #000000; text-decoration: none; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border: 1px #000000 solid;"
var cssFormfieldtd = "font-family: Verdana, Arial, sans-serif; font-size: 10px; color: #000000; text-decoration: none; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px;"
var cssForm = "margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px;"

var ofieBody ='<div id="objOfie" style="position:relative;left:0px; top:0px;font-size:0px;width:171px;height:77px;text-align:left;z-index:1000">'+
'<div id="objTrigger" style="position:relative; left:0px; top:0px; width:171px; height:77px;font-size:0px;text-align:left">'+
'<a href="#"><img src="http://i.timeinc.net/parenting/web/images/parenting_90w.gif" width="57" height="77" border="0">'+
'<img id=imgSwitch src="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_tab2.gif" width="114" height="77" border="0"></a>'+
'</div>'+
'<div id="objForm" name="objForm" style="position:absolute; width:370px; height:245px; left:-370px; top:78px;display:none;" >'+
'<TABLE WIDTH="370" BORDER="0" CELLSPACING="0" CELLPADDING="0"><TR> '+
'    <TD ROWSPAN="3" VALIGN="TOP"><IMG SRC="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_ofe_lt.jpg" WIDTH="111" HEIGHT="245" USEMAP="#Map'+n+'" BORDER="0"></TD>'+
'    <TD><IMG SRC="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_ofe_bnr_2.gif" WIDTH="256" HEIGHT="54"></TD>'+
'    <TD><IMG SRC="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_ofe_bnr2.gif" WIDTH="3" HEIGHT="54"></TD></TR>'+
'  <TR><TD ALIGN="CENTER" BGCOLOR="F9FFE6" VALIGN="TOP"> '+
'      <form method="post" name="subsOrderForm'+n+'" action="'+sFormPath+'PA/pa_ofielooney_ccupsell.jhtml" onsubmit="return ofieSubmit2(this,'+n+');">'+
'      <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">'+
'          <TR><TD style="'+cssFormtxt+'">First Name:</TD><TD ROWSPAN="6" WIDTH="10" style="'+cssFormfieldtd+'"><IMG SRC="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_spacer.gif" WIDTH="10" HEIGHT="1"></TD>'+
'            <TD style="'+cssFormtxt+'">Last Name:</TD></TR>'+
'          <TR><TD style="'+cssFormfieldtd+'"> '+ 
'              <input type="text" name="firstName" size="20" maxlength="14" style="'+cssFormfield+'">'+
'            </TD><TD style="'+cssFormfieldtd+'"> '+
'              <input type="text" name="lastName" size="20" maxlength="15" style="'+cssFormfield+'">'+
'            </TD></TR>'+
'          <TR><TD style="'+cssFormtxt+'">Address:</TD><TD style="'+cssFormtxt+'">City:</TD></TR>'+
'          <TR><TD style="'+cssFormfieldtd+'"> '+
'              <input type="text" name="address1" size="20" maxlength="30" style="'+cssFormfield+'">'+
'            </TD><TD style="'+cssFormfieldtd+'"> '+
'              <input type="text" name="city" size="20" maxlength="20" style="'+cssFormfield+'">'+
'            </TD></TR>'+
'          <TR><TD style="'+cssFormtxt+'">State/Province</TD><TD style="'+cssFormtxt+'">Zip/Postal Code:</TD></TR>'+
'          <TR><TD style="'+cssFormfieldtd+'"> '+
'			<select style="'+cssFormfield+'" name="state">'+
'			<option value="">--</option> '+stateOptionsList+' </select>'+
'			</select>'+
'            </TD><TD style="'+cssFormfieldtd+'"> '+
'              <input type="text" name="zipCode" size="20" maxlength="6" style="'+cssFormfield+'">'+
'            </TD></TR>'+
'          <TR><TD style="'+cssFormtxt+'" COLSPAN="3">E-mail:</TD></TR>'+
'          <TR><TD COLSPAN="3" style="'+cssFormfieldtd+'"> '+
'              <input type="text" name="email" size="20" maxlength="50" style="'+cssFormfield+'">'+
'              <IMG SRC="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_spacer.gif" WIDTH="1" HEIGHT="1"></TD>'+
'          </TR><TR><TD COLSPAN="3" ALIGN="CENTER"><IMG SRC="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_spacer.gif" WIDTH="1" HEIGHT="8"></TD></TR>'+
'          <TR><TD COLSPAN="3" ALIGN="CENTER" style="'+cssFormfieldtd+'">'+
'			 <input type="image" src="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_ofe_submit_btn.gif" width="134" height="27" border="0" alt="Continue">'+
'			</TD></TR></FORM></TABLE></TD>'+
'    <TD><IMG SRC="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_ofe_rt.jpg" WIDTH="3" HEIGHT="189"></TD></TR>'+
'	<TR><TD BGCOLOR="#CC6633"><IMG SRC="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_spacer.gif" WIDTH="1" HEIGHT="2"></TD>'+
'    <TD BGCOLOR="#CC6633"><IMG SRC="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_spacer.gif" WIDTH="1" HEIGHT="2"></TD></TR>'+
'	</TABLE><MAP NAME="Map'+n+'"><AREA SHAPE="RECT" COORDS="8,222,94,238" HREF="javascript:CloseForm('+n+')"></MAP>'+
'</div></div>'
	
	document.write(ofieBody);
}

function writeStaticOfie()
{
	strStaticAd = '<a href="http://subs.timeinc.net/CampaignHandler/parent_nb?source_id=39" target="_blank" '+
'><img src="http://i.timeinc.net/parenting/web/images/parenting_90w.gif" width="57" height="77" border="0" '+
'><img src="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/pa/pa_ofielooney_tab.gif" width="114" height="77" border=0></a>'

	document.write(strStaticAd);
}

//Unsupported Browser
//if (!(CM_isDom && !CM_NS6))
if (! (CM_isDom && CM_isIE) ) 
{ 
	writeStaticOfie()
}
else	//Write OFIE, Initialize Variables
{
	if (!CM_arrOfies) //Collection of OFIES if hosting page shows more than one
	{	var CM_arrOfies = new Array(); var CM_nOfie=0; }
	else CM_nOfie++

	writeOfie(CM_nOfie);
		
	tmpCollForms = document.getElementsByName("objForm")		//if (tmpCollForms.length!=nOfie) //form not labeled objForm

	if (CM_arrOfies[CM_nOfie] = tmp = tmpCollForms.item(CM_nOfie))		//objWidth, objHeight; rad, theta, pi=Math.PI ;	//	var currObj;
	{ 
		tmp.Behavior = 1 
		tmp.x1 = tmp.y1 = tmp.x2 = tmp.y2 = 0;	
		tmp.xPos = tmp.yPos = tmp.xStep = tmp.yStep = tmp.clipX = tmp.clipY = 0;
		tmp.tmrID = tmp.tmrCloseFormID = tmp.tmrAutoCloseSec = tmp.tmrInterval=0;
		tmp.speed = tmp.dist = tmp.totSteps = 0;
		tmp.bInFlight = tmp.bFormOpen = tmp.bFormEverOpened = false;
		tmp.imgTrigOn = new Image();tmp.imgTrigOff = new Image();
		tmp.imgTrig = new Object();	
		tmp.HideDropDownName = '';
		var CM_bPageLoaded = false;

		SetParams(CM_nOfie)
		attachEvents(CM_nOfie)	
		//CM_arrOfies[CM_nOfie].formLabels = formLabelsObject()
				
		addEvent(window, "load", Function("HostPageLoaded()") ) 
	}
//	else writeStaticOfie()
}

//<!--CLD live=04-18-06-17:03 stage=04-18-06-17:02 -->
