function onBlurZip(id, classname){
  if(classname == undefined){
    classname = 'inputzip';
  }

  if(document.getElementById(id).value=="") {
    document.getElementById(id).className=classname;
  }
}

function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) +
  ((expires) ? "; expires=" + expires.toGMTString() : "") +
  ((path) ? "; path=" + path : "") +
  ((domain) ? "; domain=" + domain : "") +
  ((secure) ? "; secure" : "");
}

function setsplash() {
  var exp = new Date();
  var expDays = 365;
  exp.setTime(exp.getTime() + (expDays * 24 * 60 * 60));
  setCookie("splash", "1", exp, "/");
}


function echeck(str) {
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1){
    return "Enter a valid e-mail address.";
  }

  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
    return "Enter a valid e-mail address."
  }

  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
    return "Enter a valid e-mail address.";
  }

  if (str.indexOf(at,(lat+1))!=-1) {
    return "Enter a valid e-mail address.";
  }

  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
    return "Enter a valid e-mail address.";
  }

  if (str.indexOf(dot,(lat+2))==-1) {
    return "Enter a valid e-mail address.";
  }

  if (str.indexOf(" ")!=-1) {
    return "Enter a valid e-mail address.";
  }

  return true;
}


function validateZIP(field) {
  var valid = "0123456789-";
  var hyphencount = 0;
  
  if (field.length!=5 && field.length!=10) {
    return "Please enter your 5 digit or 5 digit+4 zip code.";
  }
  for (var i=0; i < field.length; i++) {
    temp = "" + field.substring(i, i+1);
    if (temp == "-") hyphencount++;
    if (valid.indexOf(temp) == "-1") {
      return "Invalid characters in your zip code.  Please try again.";
    }
    if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
      return "The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.";
    }
  }
  return true;
}


function validate_subscribe()
{
    id=document.getElementById('email');
    h_id='hid_'+'email';
    document.getElementById(h_id).style.display='none';
    document.getElementById(h_id).innerHTML='';
    if(id.value=='')
    {
      document.getElementById(h_id).style.display='';
      document.getElementById(h_id).innerHTML='Enter e-mail address.';
      id.focus();
      return false;
    }
    if(echeck(id.value)!=true)
    {
      document.getElementById(h_id).style.display='';
      document.getElementById(h_id).innerHTML=echeck(id.value);
      id.focus();
      return false;
    }
    /*
    id=document.getElementById('zip');
    h_id='hid_'+'zip';
    document.getElementById(h_id).style.display='none';
    document.getElementById(h_id).innerHTML='';
    if(validateZIP(id.value)!=true)
    {
      document.getElementById(h_id).style.display='';
      document.getElementById(h_id).innerHTML=validateZIP(id.value);
      id.focus();
      return false;
    }
    */
    return true;
}

function clearthis(id){
  if(document.getElementById(id).value=="Email") {
    document.getElementById(id).value="";
  }
  if(document.getElementById(id).value=="Zip") {
    document.getElementById(id).value="";
  }
}

function submit_subscription_form(redirect_to_home){
  if(validate_subscribe()){
    return next_page(redirect_to_home);
  } else {
    return false; 
  }
}

function next_page(redirect_to_home){
  var email = document.getElementById('email').value;
  var zip = document.getElementById('zip').value;
  var zoebeautiful = getCheckboxValue('zoebeautiful');
  var accesszoeries = getCheckboxValue('accesszoeries');

  return continue_to_next_page(email, zip, redirect_to_home, '', '', '', zoebeautiful, accesszoeries);
}

function next_page_from_facebook(email, zip) {
  continue_to_next_page(email, zip, true);
}

function next_page_from_full_facebook(email, zip, age, gender, hhi) {
  continue_to_next_page(email, zip, true, age, gender, hhi);
}

function continue_to_next_page(email, zip, redirect_to_home, age, gender, hhi, zoebeautiful, accesszoeries) {
  var encoded_email = email.replace(/\+/, "%2B");
  var options = {
    href:"/friendmail?email="+encoded_email+"&zip="+zip+"&age="+age+"&gender="+gender+"&household_income="+hhi+"&zoebeautiful="+zoebeautiful+"&accesszoeries="+accesszoeries,
    open:true,
    width:"675px",
    height:"580px",
    scrolling:false,
    iframe:true
  }
  if(redirect_to_home == true) {
    options['onClosed'] = function(){document.location.href='/';}
  }
  if (window.parent !== window) {
  	  window.location.href = options.href;
  } else {
	  jQuery.fn.colorbox(options);
  }
  return false;
}

function getCheckboxValue(checkbox_id) {
  if (document.getElementById(checkbox_id).checked) {
    return document.getElementById(checkbox_id).value;
  } else {
    return '';
  }
}

