function getCookieExpireDate(){
  //var expr = new Date('5/7/2009');
  var expr = new Date();
  var days = 730;
  expr.setTime(expr.getTime() + days*24*60*60*1000); 
  return  expr.toGMTString() 
}
  

function makeCookie(favs){
  var name = "frch-save-favorites";
  var data = favs;
  var cookieStr = name + "="+ data +";expires=" + getCookieExpireDate() + ";path=/;domain=frch.com";
  document.cookie = cookieStr;
  
   //alert(favs);
  
}



var chkSelectData = "";
    
function makeCookieCheckboxSelect(selected){
  var name = "frch-select-checkbox-favorites";
  
  chkSelectData = "";
   var inputs = document.getElementsByTagName("input"); //or document.forms[0].elements;  
    for (var i = 0; i < inputs.length; i++){  
      if (inputs[i].type == "checkbox" && inputs[i].id != 'chkAll'){  
        if (inputs[i].checked){  
          chkSelectData = chkSelectData + "," + inputs[i].id;
        }  
      }  
    }  
    
  if(chkSelectData.charAt(0) == ','){
     chkSelectData = chkSelectData.substring(1);
  }

  var cookieStr = name + "="+ chkSelectData +";expires=" + getCookieExpireDate() + ";path=/;domain=frch.com";
  document.cookie = cookieStr;

  //alert("got here:" + chkSelectData);
}



function readCookie(cookieName){
   var searchName = cookieName + "="
   var cookies = document.cookie
   var start = cookies.indexOf(cookieName)
   if (start == -1){ // cookie not found 
     return ""
     }
   start += searchName.length //start of the cookie data
   var end = cookies.indexOf(";", start)
   if (end == -1){
     end = cookies.length
     }
   return cookies.substring(start, end)
 }

       
       
       


function updateFavsSavedCount(theCount){
  var tempurl = document.URL
  var newtempurl = tempurl.match("/br/");

  if (newtempurl != null)
  {
    document.getElementById("favCount").innerHTML = "<a href='/br/projetos-salvos.aspx'>Projetos Salvos ("+theCount+")</a>";
    favsListAlert('add','item');
  }
  else
  {
    document.getElementById("favCount").innerHTML = "<a href='/us/saved-projects.aspx'>Saved Projects ("+theCount+")</a>";
    favsListAlert('add','item');
  }
  
}

function hideRemoveFavAlert(){
  //$('#projectRemoveFavoritesAlert').fadeOut('slow');
  //$('#projectRemoveFavoritesAlertBox').fadeOut('slow');
  $('#projectRemoveFavoritesAlert').hide();
  $('#projectRemoveFavoritesAlertBox').hide();
}


function removeFromFavsList(theProject,theCount,theFavList){
  //alert("got here");
  //theName = theName.replace(''','`');
  //$('#projectRemoveFavoritesAlert').fadeIn('slow');
  $('#projectRemoveFavoritesAlert').show();
  theAlertDiv(theProject,theCount,theFavList);
}


function theAlertDiv(theProject,theCount,theFavList){
  
  var tempurl = document.URL
  var newtempurl = tempurl.match("/br/");

  if (newtempurl != null)
  {
    document.getElementById("projectRemoveFavoritesAlertBox").innerHTML = "<div><div style='margin:25px;text-align:center;'><h6 style='display:block;font-size:14px;color:#000;margin:25px;margin-top:25px;margin-bottom:5px;'>REMOVER PROJETO</h6><span style='color:#666;'>Você tem certeza que deseja remover esse projeto dos seus Projetos Salvos?</span></div><div style='display:block;width:322px;margin:auto;margin-top:23px;text-align:center;'><a href='#' id='removeAlertYes' onclick=hideRemoveFavAlert();favsListAlert('remove','"+theProject+"');fadeFavsListItem('"+theProject+"','"+theCount+"','"+theFavList+"');>SIM</a>&nbsp;&nbsp;&nbsp;<a href='#' id='removeAlertNo' onclick=hideRemoveFavAlert();>NÃO</a></div></div>";
  }
  else
  {
    document.getElementById("projectRemoveFavoritesAlertBox").innerHTML = "<div><div style='margin:25px;text-align:center;'><h6 style='display:block;font-size:14px;color:#000;margin:25px;margin-top:25px;margin-bottom:5px;'>REMOVE PROJECT</h6><span style='color:#666;'>Are you sure you want to remove this project from your Saved Projects?</span></div><div style='display:block;width:322px;margin:auto;margin-top:23px;text-align:center;'><a href='#' id='removeAlertYes' onclick=hideRemoveFavAlert();favsListAlert('remove','"+theProject+"');fadeFavsListItem('"+theProject+"','"+theCount+"','"+theFavList+"');>YES</a>&nbsp;&nbsp;&nbsp;<a href='#' id='removeAlertNo' onclick=hideRemoveFavAlert();>NO</a></div></div>";
  }

  //$('#projectRemoveFavoritesAlertBox').fadeIn('slow');
  $('#projectRemoveFavoritesAlertBox').show();
}


  
  

function fadeFavsListItem(theProject,theCount,theFavList){
  //alert(theFavList);
  var newCount = theCount - 1;
  var tempurlrem = document.URL
  var newtempurlrem = tempurlrem.match("/br/");

  if (newtempurlrem != null)
  {
    document.getElementById("favCount").innerHTML = "<a href='/br/projetos-salvos.aspx'>Projetos Salvos ("+newCount+")</a>";
  }
  else
  {
    document.getElementById("favCount").innerHTML = "<a href='/us/saved-projects.aspx'>Saved Projects ("+newCount+")</a>";
  }
  $('input[id='+theProject+']').attr('checked', false);
  $('#'+theProject).delay(1000).fadeOut('slow');
  makeCookie(theFavList);
  makeCookieCheckboxSelect();
  
  if(theFavList == ''){    
    $('#favHolder').delay(1250).fadeOut('slow');
    $('#favButtons').delay(1250).fadeOut('slow');
    $(".cufonIntro").delay(1250).fadeOut('slow');
    $('#howToSave').delay(1400).fadeIn('slow');
  }
  
}

function favsListAlert(mode,theProject){
  if(mode == 'remove'){
    
    var tempurlrem = document.URL
    var newtempurlrem = tempurlrem.match("/br/");
  
    if (newtempurlrem != null)
    {
      document.getElementById("projectDeleteFavoritesAlert_"+theProject).innerHTML = "Projeto Removido";
    }
    else
    {
      document.getElementById("projectDeleteFavoritesAlert_"+theProject).innerHTML = "Project Removed";
    }
    
    
    $('#projectDeleteFavoritesAlert_'+theProject).fadeIn('slow').delay(800).fadeOut('slow');
     //window.setTimeout(theReload,1800); 
  }
  
  if(mode == 'add'){   
    //alert("got here");
    var tempurladd = document.URL
    var newtempurladd = tempurladd.match("/br/");
  
    if (newtempurladd != null)
    {
      document.getElementById("projectSaveFavoritesAlert").innerHTML = "Projeto Salvo";
      
    }
    else
    {
      document.getElementById("projectSaveFavoritesAlert").innerHTML = "Project Saved";
    }
    $('#projectSaveFavoritesAlert').fadeIn('slow').delay(800).fadeOut('slow');
    //document.getElementById('saveFav').addClass('hidden');
    //document.getElementById('savedFav').removeClass('hidden');
    
  }
  
   if(mode == 'exists'){   
    //alert("got here");
    document.getElementById("projectSaveFavoritesAlert").innerHTML = "Project Already Saved";
    $('#projectSaveFavoritesAlert').fadeIn('slow').delay(800).fadeOut('slow');
  }
}



function allFavsChk(){
  
  var inputs = document.getElementsByTagName("input"); //or document.forms[0].elements;  

  if(document.getElementById("chkAll").checked == false){
    for (var i = 0; i < inputs.length; i++){  
      if (inputs[i].type == "checkbox"){  
        //if (inputs[i].checked){  
          inputs[i].checked = false;
        //}
      }  
    }  
  }else{
      for (var i = 0; i < inputs.length; i++){  
        if (inputs[i].type == "checkbox"){  
          //if (inputs[i].checked){  
            inputs[i].checked = true;
          //}
        }  
      }  
  }
  
      makeCookieCheckboxSelect();
  
}


function uncheckAllCheckbox(){
   document.getElementById("chkAll").checked = false; 
}




