function emailChecker(email){
  var at="@"; var dot="."; var lat=email.indexOf(at); var lstr=email.length; var ldot=email.indexOf(dot);
  if (email.indexOf(at)==-1){ return false; }
  if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr){ return false; }
  if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr){ return false; }
  if (email.indexOf(at,(lat+1))!=-1){ return false; }
  if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot){ return false; }
  if (email.indexOf(dot,(lat+2))==-1){ return false; }
  if (email.indexOf(" ")!=-1){ return false; }			
}
function emailValidator(){
  var enteredEmail = document.newsletters.email; var enteredConfirm = document.newsletters.confirm_email;
  if ((enteredEmail.value==null)||(enteredEmail.value=="")){ enteredEmail.focus(); return false; }
  if (emailChecker(enteredEmail.value)==false){ enteredEmail.value=""; enteredEmail.focus(); return false; }
  if((enteredConfirm.value==null) || (enteredConfirm.value=="")){ enteredConfirm.focus(); return false; }
  if(emailChecker(enteredConfirm.value)==false){ enteredConfirm.value=""; enteredConfirm.focus(); return false; }
}
function formValidator(){
  var newsletters = document.newsletters;
  var Information = new Array();
  var enteredCaptcha = document.newsletters.recaptcha_response_field;
  Information[1] = newsletters.email;
  Information[2] = newsletters.confirm_email;
  if(emailValidator()==false){ alert("Please enter a valid email address."); return false; }
  if(Information[1].value != Information[2].value){ alert("Your email and confirmation email do not match."); Information[2].focus(); return false; }
  if((enteredCaptcha.value==null) || (enteredCaptcha.value=="")){ enteredCaptcha.focus(); alert("Please enter the security code."); return false; }
  return true;
}
function EmailInput(id,checked){
  var inputId = document.getElementById(id);
  if(checked == 1){
    if(inputId.value == "Enter Email Address" || inputId.value == ""){
      inputId.value="";
    }
  }else{
    if(inputId.value == "Confirm Email Address" || inputId.value == ""){
      inputId.value="";
    }
  }
  
}


// JavaScript Document
//graybox stuff
function fadeBox(divName, endOpac){
  var grayBox = document.getElementById(divName);
    if(!grayBox) {
        window.gbinterval = clearInterval(window.gbinterval);
        return false;
    }
  var i = parseFloat(grayBox.style.opacity);
  var step = 0.1;
  if(endOpac){
    if(i < endOpac){
      i += step;
      document.getElementById(divName).style.filter = "alpha(opacity=" + i*100 + ")";
      document.getElementById(divName).style.opacity = i;
    }else{
            window.gbinterval = clearInterval(window.gbinterval);
        }
  }else{
    if(i > endOpac){
      i -= step;
      document.getElementById(divName).style.filter = "alpha(opacity=" + (i*100) + ")";
      document.getElementById(divName).style.opacity = i;
    }else {
      window.gbinterval = clearInterval(window.gbinterval);
      showhidediv(divName,2);
    }
  }
}

function showhidediv(divname,type){
  box=document.getElementById(divname);
  box.style.display=(type==1) ? "block":"none";
}

//use this for a greybox fadeout
function smokeScreen(divId,endOpac){
    var topscroll=(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    if(endOpac>0){
        // hide scroll bars
        windowscroll=document.getElementsByTagName('html')[0];
        windowscroll.style.overflow="hidden";
        document.documentElement.scrollTop=topscroll;
        //initialize opacity
        box=document.getElementById(divId);
        box.style.opacity=0;
        box.style.filter="alpha(opacity=0)";
        //make div visible
        showhidediv(divId,1);
    }
    else{
        // put back the scrolls
        windowscroll=document.getElementsByTagName('html')[0];
        windowscroll.style.overflow="auto";
        // and for safari and chrome
        document.body.scrollTop += 1;
        document.body.scrollTop -= 1;
        document.documentElement.scrollTop=topscroll;
    }
    // fade in/out
    window.gbinterval=setInterval("fadeBox('"+divId+"',"+endOpac+")",25);
}
function spotLight(highlightId,smokeScreenId,highlightElementHeight,endOpacity){
    if(endOpacity>1) endOpacity = 1;
    else if(endOpacity<0) endOpacity = 0;
    var height = (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight;
    var eTop = document.getElementById(highlightId).offsetTop;
    var scrollLoc = eTop + (highlightElementHeight - height)/2.0;
    if(scrollLoc<0) scrollLoc = 0;
    smokeScreen(smokeScreenId,endOpacity);
    document.getElementById(smokeScreenId).style.top = scrollLoc+'px';
    document.body.scrollTop = scrollLoc;
    document.documentElement.scrollTop  = scrollLoc;
}
//greybox popper
window.grayBoxLoading = false;
function popgbox(type,desc,width,height,popHeadCode,divpop){
    if(typeof(divpop) == "undefined"){
        divpop = false;
    }
    if(type == 2 && typeof(window.popperBox) != 'undefined') {
        boxname=window.popperBox.name;
        divpop=window.popperBox.isdiv;
    }
    else {
        var boxname = (typeof(desc) == 'undefined') ? 'popcontent' : (desc == '') ? 'popcontent2' : desc;
        boxname = (type == 1) ? 'popcontent' : boxname;
        if(divpop) {
            window.popperBox= {name:boxname, isdiv:divpop};
        }
    }
    if(desc != ''){
        var testUrl = new String(desc);
        if (testUrl.indexOf('be-the-first.html') > 0 && popHeadCode != 'onclick') {
            return false;
        }
    }
    if(window.grayBoxLoading==false) {
    window.grayBoxLoading = true;
    setTimeout('window.grayBoxLoading=false;',1000);

        // default width and height if not set
        width=(typeof(width) == "undefined") ? 500:width;
        height=(typeof(height) == "undefined") ? 500:height;

        leftscroll=(document.documentElement.scrollLeft) ?  document.documentElement.scrollLeft : document.body.scrollLeft;
        topscroll=(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        browseheight=(window.innerHeight) ? window.innerHeight:document.documentElement.clientHeight;
        browsewidth=(window.innerWidth) ? window.innerWidth:document.documentElement.clientWidth;
        // if the browser is smaller than the window will be..
        height=(browseheight < height) ? browseheight:height;
        width=(browsewidth < width) ? browsewidth:width;

        browseheight=(browseheight < height) ? 0:browseheight;
        boxtop=(browseheight/2) + topscroll;
        boxtop=boxtop-(height/2);
        boxtop=(boxtop <=0) ? 0:boxtop;

        browsewidth=(browsewidth < width) ? 0:browsewidth;
        boxleft=(browsewidth/2) + leftscroll;
        boxleft=boxleft-(width/2);
        boxleft=(boxleft <= 0) ? 0:boxleft;

        contbox=document.getElementById(boxname);
        contbox.style.top=boxtop + "px";
        contbox.style.left=boxleft + "px";
        contbox.style.height=height + "px";
    contbox.style.width=width + "px";

        var out=false;
    if(type == 1){
            ifm=document.getElementById('popfrm');
      // show the content area with the iframe
      ifm.src=desc;
      if(popHeadCode==-1)ifm.style.height = height + "px";
            else ifm.style.height = (height - 25) + "px";
      ifm.style.width = width + "px";

            if(typeof(popHeadCode) == 'undefined' || popHeadCode!=-1){
                var pophead=document.getElementById('pophead');
                pophead.contentWindow.document.body.style.padding="0";
                pophead.contentWindow.document.body.style.margin="0";
                pophead.contentWindow.document.body.style.background="#ABABC2";
                pophead.contentWindow.document.body.style.border="0";
                pophead.contentWindow.document.getElementsByTagName('html')[0].style.overflow="hidden";
                pophead.style.width=width + "px";
                pophead.style.height=25 + "px";
                pophead.style.border="0px";
                if(typeof(popHeadCode)=="undefined" || popHeadCode == 'onclick'){
                    pophead.contentWindow.document.body.innerHTML='<div style="width:45px; margin-top:5px; float:right;"><a style="font-size:12px; color:#000000; text-decoration:none;" href="javascript:top.popgbox(2);"><img src="http://www.toptenreviews.com/i/community/w_close.gif" border="0" alt="close box"/> Close</a></div>';
                } else {
                    pophead.contentWindow.document.body.innerHTML = popHeadCode;
                    pophead.contentWindow.document.body.style.background="#FFFFFF";
                }
            } else {
                document.getElementById('pophead').style.height="0px";
            }
    } else if(type == 3) {
            // don't do anything, we just don't want it to say it is fading out.
        } else {
            if(!divpop){
                ifm=document.getElementById('popfrm');
                ifm.src='http://www.toptenreviews.com/community/greybox/blank.html';
            }
            out=true;
    }
        // fade
        var endopac=(out == true) ? 0:0.6;
        showhidediv(boxname,(out == true) ? 2 : 1);
        smokeScreen('faderbox',endopac);
  }
}
//end graybox stuff


function createCookie(name,value,days) {
  var docurl=document.URL;
  if(docurl.indexOf(".toptenreviews.com")>0)
    var dmn = "; domain=.toptenreviews.com;";
  else
    var dmn = ";";
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/"+dmn;
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return "";
}
