var gameFront = new Object();
var fantasyUser = new Object();
fantasyUser.loggedIn = false;
fantasyUser.maxTeamsAllowed = 5;
if (fuid.length > 0 || euid.length > 0) {
  fantasyUser.loggedIn = true;
  if (typeof(fbu) == "undefined") {
    var fbu = new Object();
    fbu.teams = new Array(0);
  }
}
if (typeof(ff) == "undefined") {
  var ff = new Object();
  ff.leagues = new Array(0);
}

gameFront.leagueTeamPullList = function() {
  var _t = '<div class="moduleComponentGridContainer">';
  if (typeof fbu == 'undefined' || typeof fbu.teams == 'undefined') return;
  for (i = 0; i < fbu.teams.length; i++) {
    var teamId = fbu.teams[i];
    var teamName = fbu.teamName[teamId];
    var leagueId = fbu.teamOwnership[teamId];
    var leagueName = fbu.leagueName[leagueId];
    var teamUrl = "/fantasy/football/commissioner/team/home?teamId=" + teamId;
    var leagueUrl = "/fantasy/football/commissioner/league/home?leagueId=" + leagueId;
    
    if (typeof(leagueName) != "undefined") {
      _t += (fbu.teams.length - 1 == i && !(showCreateLeague && fbu.hasLeaguesToReactivate && fbu.teams.length < maxTeamsPerUser)) ? 
            '<div class="moduleComponentGrid last"><div class="leagueName"><a href="' + leagueUrl + '">' + leagueName + '</a></div><div><div class="tStandings"></div><div class="tName">Team: <a href="' + teamUrl + '">' + teamName + '</a></div></div></div>' :
            '<div class="moduleComponentGrid"><div class="leagueName"><a href="' + leagueUrl + '">' + leagueName + '</a></div><div><div class="tStandings"></div><div class="tName">Team: <a href="' + teamUrl + '">' + teamName + '</a></div></div></div>';
    }
		else {
		   _t += (fbu.teams.length - 1 == i && !(showCreateLeague && fbu.hasLeaguesToReactivate && fbu.teams.length < maxTeamsPerUser)) ? 
            '<div class="moduleComponentGrid last"><div class="leagueName">League To Be Assigned</div><div><div class="tStandings"></div><div class="tName">Team: <a href="' + teamUrl + '">' + teamName + '</a></div></div></div>' :
            '<div class="moduleComponentGrid"><div class="leagueName">League To Be Assigned</div><div><div class="tStandings"></div><div class="tName">Team: <a href="' + teamUrl + '">' + teamName + '</a></div></div></div>';
    }
  }
  _t += '<div class="moduleComponentGrid last">&nbsp;</div>';
  /*
  if (showCreateLeague && fbu.hasLeaguesToReactivate && fbu.teams.length < maxTeamsPerUser) {
    _t += '<div class="moduleComponentGrid last"><span class="new">NEW!</span> <a href="/fantasy/football/commissioner/manage">Reactivate your 2007 League</a></div>';
  } */ 
  document.getElementById("myTeamsLoggedInWithTeamsArea").innerHTML = _t;
}
gameFront.myTeamsUpdate = function() {  //combine this with the displayDivs function below
    var _teamList = document.getElementById('myTeamsLoggedInWithTeamsArea');
    if (_teamList) {
        gameFront.leagueTeamPullList();
    }
}

function validate(form_id) {
  if(validateEmail(form_id) &&  validateBday(form_id)){
   document.forms[form_id].submit();
   return true;
  }
  return false;
}
function validateEmail(form_id) {   
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements['email'].value;
   if(reg.test(address) == false) {
      alert('The email address you submitted is invalid. Please try submitting your email address again. Thank you.');
      document.forms[form_id].elements['email'].value = "";
      return false;
   }
   return true;
}

function validateBday(form_id) {
 var day = document.forms[form_id].elements['dd'].value;
 var month = document.forms[form_id].elements['mm'].value;
 var year = document.forms[form_id].elements['yy'].value; 
 var cookieVal = month+"-"+day+"-"+year; 
 //check if cookie exists and check email against it
 var currentCookieVal = readCookie(document.forms[form_id].elements['email'].value);
 if(currentCookieVal){
   alert("We're sorry. You must be at least 13 years old to participate in our email submit campaign. You will not be able to register your email address at this time.");
   return false;
 }else{//if no cookie exists verify valid date of birth  
       if(month == '0' || day == '0' || month == '2' && (((parseInt(year)%4 == 0) && parseInt(day) > 29) || ((parseInt(year)%4 != 0) && parseInt(day) > 28))){
         alert("We are not recognizing the date of birth you submitted as valid. Please try submitting your info again.");
         return false;
     }else if((month == '4' || month == '6' || month == '9' || month == '11') && day == '31'){
         alert("We are not recognizing the date of birth you submitted as valid. Please try submitting your info again.");
         return false;
     }else if((year == '1995'  && (parseInt(month) > bMonth || (parseInt(month) == bMonth && parseInt(day) > bDay))) || parseInt(year) > 1995){
       
        //create a cookie for 30 days.
        createCookie(document.forms[form_id].elements['email'].value,cookieVal,30);
        alert("We're sorry. You must be at least 13 years old to participate in our email submit campaign. You will not be able to register your email address at this time.");
        return false;
     }
       return true;
  }//if cookie does not exist    
 
}

//cookie creation
function createCookie(name,value,days) {
  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=/";
}

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 null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}

function goSignIn() {
    window.location.href = generateLoginUrl();
}

if ($('myTeamsLoggedInWithTeamsArea')) gameFront.myTeamsUpdate();
