// Browser Detection
var TICM_na = parseFloat(navigator.appVersion);	
var TICM_napN = navigator.appName.toLowerCase();
var TICM_nu = navigator.userAgent.toLowerCase();

var TICM_isIE=((document.all)&&(TICM_nu.indexOf('opera')==-1))?true:false;
var TICM_isNN4=((document.layers)&&(TICM_na >= 4.03))?true:false;
var TICM_isOpera5up=(TICM_nu.indexOf('opera 5')!=-1)?true:false;
var TICM_isGecko=(TICM_nu.indexOf('gecko')!=-1)?true:false;
var TICM_isDom = document.getElementById?true:false;

var TICM_GeckoMinor=TICM_isGecko?TICM_nu.slice(TICM_nu.indexOf('rv:')+3,TICM_nu.indexOf('rv:')+8):0;
var TICM_isNS62up=(TICM_GeckoMinor>='0.9.4')?true:false;
var TICM_ieMajor=TICM_isIE?parseInt(TICM_nu.slice(TICM_nu.indexOf('msie')+4)):0;
var TICM_isIE4up=TICM_ieMajor >= 4;
var TICM_isIE5up=TICM_ieMajor >= 5;

var TICM_isWin=(TICM_nu.indexOf('win')!=-1)?true:false;
var TICM_isWin98=(TICM_nu.indexOf('windows nt')!=-1)?true:false;
var TICM_isWinNT=(TICM_nu.indexOf('windows 98')!=-1)?true:false;
var TICM_isMac=(TICM_nu.indexOf('mac')!=-1)?true:false;

var TICM_goodClient = (TICM_isNN4 || TICM_isIE4up || TICM_isOpera5up || TICM_isNS62up || TICM_isDom);

// Form field sizing
var TICM_inpWidthLg=15; 
var TICM_inpWidthSm=5;

if (TICM_isWin) {
  if (TICM_isIE4up||TICM_isOpera5up) {TICM_inpWidthLg=21; TICM_inpWidthSm=9;}
  if (TICM_isGecko){TICM_inpWidthLg=21; TICM_inpWidthSm=10;}
  if (TICM_isNS62up){TICM_inpWidthLg=21; TICM_inpWidthSm=8;}
}
else if (TICM_isMac) {
  if (TICM_isIE4up) {TICM_inpWidthLg=21; TICM_inpWidthSm=10;}
  if (TICM_isIE5up||TICM_isOpera5up) {TICM_inpWidthLg=19; TICM_inpWidthSm=10;}
  if (TICM_isNN4){TICM_inpWidthLg=19; TICM_inpWidthSm=10;}
  if (TICM_isGecko){TICM_inpWidthLg=13; TICM_inpWidthSm=10;}
  if (TICM_isNS62up){TICM_inpWidthLg=14; TICM_inpWidthSm=10;}
}

function clearField(fi,la){
	if(fi.form[TICM_nameLabels[2]].value == "")fi.form[TICM_nameLabels[2]].value = TICM_nameLabels[0];
	if(fi.form[TICM_homeLabels[2]].value == "")fi.form[TICM_homeLabels[2]].value = TICM_homeLabels[0];
	if(fi.form[TICM_cityLabels[2]].value == "")fi.form[TICM_cityLabels[2]].value = TICM_cityLabels[0];
	if(fi.form[TICM_zipLabels[2]].value == "")fi.form[TICM_zipLabels[2]].value = TICM_zipLabels[0];
	if(fi.form[TICM_emailLabels[2]].value == "")fi.form[TICM_emailLabels[2]].value = TICM_emailLabels[0];
	if(la.contains(fi.value))fi.value="";
}

function TICM_arrayContains(str){
	for (var i = 0; i < this.length; i++){
		if (this[i] == str) return true;
	}
	return false;
}

Array.prototype.contains = TICM_arrayContains;

var TICM_nameLabels = ["NAME:","**NAME**", "name"];
var TICM_homeLabels = ["ADDRESS:","**ADDRESS**", "address1"];
var TICM_cityLabels = ["CITY:","**CITY**", "city"];
var TICM_zipLabels = ["ZIP/PC:","**ZIP/PC**", "zipCode"];
var TICM_emailLabels = ["E-MAIL:","**E-MAIL**", "email"];

// Error checking
function TICM_check2(f){   // the '2' is a hack to resolve the problem that occurs 
                           // when two ofies exist on the same page
  var states = f.state;
  var state;
  var canstates = ['AB','BC','MB','NB','NF','NS','NT','ON','PE','QC','SK','YT']
  var t, msg, good;

  msg = "";
  state = states.options[states.selectedIndex].value;

	if ((f.elements["name"].value.search(/[A-Za-z]\s[A-Za-z]/) == -1) || (f.elements["name"].value == null) || (TICM_nameLabels.contains(f.elements["name"].value)) || (f.elements["name"].value == '') || isblank(f.elements["name"].value)) {msg += "Please enter your First and Last Name.\n"; f.elements["name"].value=TICM_nameLabels[1];}
	if ((f.address1.value == null) || (TICM_homeLabels.contains(f.address1.value)) || (f.address1.value == '') || isblank(f.address1.value)) {msg += "Please enter your Address.\n"; f.address1.value=TICM_homeLabels[1];}	
	if ((f.city.value == null) || (TICM_cityLabels.contains(f.city.value)) || (f.city.value == '') || isblank(f.city.value)) {msg += "Please enter your City.\n"; f.city.value=TICM_cityLabels[1];}	
  if (state=='')msg+="Please enter your State/Province.\n"
    
  good=1;
  t = f.zipCode.value;
  if (t==null || t=='' || 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"; f.elements["zipCode"].value=TICM_zipLabels[1];}

  good=1;
  t=f.email.value;
  if (t==null || t=='' || isblank(t) || TICM_emailLabels.contains(t)) good=1;
  else good = (t.search(/^(\w|-)+(\.(\w|-)+)?@((\w|-)+\.){1,}[a-zA-Z]{2,3}$/)==-1)?0:1;
    
  if(!good){msg+="Your E-mail address is incorrect.\n\n"; f.email.value=TICM_emailLabels[1];}
    
  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,\nTime Inc. Consumer Services");
    return false;
  }
  return true;
}
  
function 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;
}

// begin html for subscription form 
var sFormPath = "https://subs.timeinc.net/"
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/"


function submitContinue2(f) {   // the '2' is a hack to resolve the problem that occurs 
                                // when two ofies exist on the same page
  if (TICM_check2(f)) {
	if(document.si_ccofieform.email.value == "E-MAIL:" || document.si_ccofieform.email.value == "**E-MAIL**")document.si_ccofieform.email.value = "";
    
	//50% split to a different form:	
	if ((Math.random() * 100) <= 50) f.action = sFormPath+"SI/si_ofie0305sicom.jhtml";	

	var ofieWin = window.open('','si_ccofie','width=376,height=590,scrollbars=yes,status=yes');
    document.si_ccofieform.target = 'si_ccofie';
    return true;
  }
  else return false;
}



var form_body = '<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="160" HEIGHT="213">'+
'<form name="si_ccofieform" method="post" action="'+sFormPath+'SI/si_ofie0405sicom.jhtml" onsubmit="return submitContinue2(this);">'+
'<input type="hidden" name="emailEC" value="CHECK_ON_REQUIRED_OFF"><input type="hidden" name="passVariables" value="MSCSIGTP_E">'+
'	<TR>'+
'		<TD BACKGROUND="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/si/si_ccofie_bg.jpg" VALIGN="TOP" ALIGN="CENTER"><IMG SRC="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/si/si_ccofie_head.gif" WIDTH="160" HEIGHT="70" border="0">'+
'		<TABLE BORDER="0" CELLSPACING="1" CELLPADDING="0">'+
'			<TR>'+
'				<TD COLSPAN="2"><INPUT onfocus="clearField(this,TICM_nameLabels);" TYPE="text" NAME="'+TICM_nameLabels[2]+'" SIZE="'+TICM_inpWidthLg+'" VALUE="'+TICM_nameLabels[0]+'" maxlength="30"></TD>'+
'			</TR>'+
'			<TR>'+
'				<TD COLSPAN="2"><INPUT onfocus="clearField(this,TICM_homeLabels);" TYPE="text" NAME="'+TICM_homeLabels[2]+'" SIZE="'+TICM_inpWidthLg+'" VALUE="'+TICM_homeLabels[0]+'" maxlength="30"></TD>'+
'			</TR>'+
'			<TR>'+
'				<TD COLSPAN="2"><INPUT onfocus="clearField(this,TICM_cityLabels);" TYPE="text" NAME="'+TICM_cityLabels[2]+'" SIZE="'+TICM_inpWidthLg+'" VALUE="'+TICM_cityLabels[0]+'" maxlength="20"></TD>'+
'			</TR>'+
'			<TR VALIGN="top">'+
'				<TD><SELECT NAME="state"><option value="">STATE</option>'+
'<option value="AA">AA </option>' +
'<option value="AB">AB </option>' +
'<option value="AE">AE </option>' +
'<option value="AK">AK </option>' +
'<option value="AL">AL </option>' +
'<option value="AP">AP </option>' +
'<option value="AR">AR </option>' +
'<option value="AS">AS </option>' +
'<option value="AZ">AZ </option>' +
'<option value="BC">BC </option>' +
'<option value="CA">CA </option>' +
'<option value="CO">CO </option>' +
'<option value="CT">CT </option>' +
'<option value="DC">DC </option>' +
'<option value="DE">DE </option>' +
'<option value="FL">FL </option>' +
'<option value="GA">GA </option>' +
'<option value="HI">HI </option>' +
'<option value="IA">IA </option>' +
'<option value="ID">ID </option>' +
'<option value="IL">IL </option>' +
'<option value="IN">IN </option>' +
'<option value="KS">KS </option>' +
'<option value="KY">KY </option>' +
'<option value="LA">LA </option>' +
'<option value="MA">MA </option>' +
'<option value="MB">MB </option>' +
'<option value="MD">MD </option>' +
'<option value="ME">ME </option>' +
'<option value="MI">MI </option>' +
'<option value="MN">MN </option>' +
'<option value="MO">MO </option>' +
'<option value="MP">MP </option>' +
'<option value="MS">MS </option>' +
'<option value="MT">MT </option>' +
'<option value="NB">NB </option>' +
'<option value="NC">NC </option>' +
'<option value="ND">ND </option>' +
'<option value="NE">NE </option>' +
'<option value="NF">NF </option>' +
'<option value="NH">NH </option>' +
'<option value="NJ">NJ </option>' +
'<option value="NM">NM </option>' +
'<option value="NV">NV </option>' +
'<option value="NS">NS </option>' +
'<option value="NT">NT </option>' +
'<option value="NY">NY </option>' +
'<option value="ON">ON </option>' +
'<option value="OH">OH </option>' +
'<option value="OK">OK </option>' +
'<option value="OR">OR </option>' +
'<option value="PA">PA </option>' +
'<option value="PE">PE </option>' +
'<option value="PR">PR </option>' +
'<option value="QC">QC </option>' +
'<option value="RI">RI </option>' +
'<option value="SC">SC </option>' +
'<option value="SD">SD </option>' +
'<option value="SK">SK </option>' +
'<option value="TN">TN </option>' +
'<option value="TX">TX </option>' +
'<option value="UT">UT </option>' +
'<option value="VA">VA </option>' +
'<option value="VI">VI </option>' +
'<option value="VT">VT </option>' +
'<option value="WA">WA </option>' +
'<option value="WI">WI </option>' +
'<option value="WV">WV </option>' +
'<option value="WY">WY </option>' +
'<option value="YT">YT </option>' +
'</select>' +
'				<TD><INPUT onfocus="clearField(this,TICM_zipLabels);" TYPE="text" NAME="'+TICM_zipLabels[2]+'" SIZE="'+TICM_inpWidthSm+'" VALUE="'+TICM_zipLabels[0]+'" maxlength="6"></TD>'+
'			</TR>'+
'			<TR>'+
'				<TD COLSPAN="2"><INPUT onfocus="clearField(this,TICM_emailLabels);" TYPE="text" NAME="'+TICM_emailLabels[2]+'" SIZE="'+TICM_inpWidthLg+'" VALUE="'+TICM_emailLabels[0]+'" maxlength="50"></TD>'+
'			</TR>'+
'		</TABLE>'+
'		<input type="image" SRC="https://a248.e.akamai.net/f/1016/606/1d/image.timeinc.net/subs2/images/si/si_ccofie_submit.gif" WIDTH="146" HEIGHT="29" VSPACE="2" border="0"></TD>'+
'	</TR>'+
'</form>'+
'</TABLE>';

// Write out the OFIE
document.write(form_body);
