/// Rrr.js
 
var checkedCatObj  = null;
var alreadyClicked = false;
var ALLVALUESTR = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; 
var windowSpecs = 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=300';


function geoToggle(newToggledObj, newGeoTarget){
  for(var anchorN=0; anchorN< document.anchors.length; anchorN++){
    switch( document.anchors[anchorN].name  ){ 
    case '5':
    case '4':
    case '3':
    case '2':
    case '1':
      var anchorObj = document.anchors[anchorN];
      anchorObj.id = 'geoLink'; 
    }
  }
  newToggledObj.id='geoNoLink';         /// WHY CANT THIS BE LIKE changeCategory():   if(checkedCatObj != null){ checkedCatObj.id='unchecked'; } WITH LIKE: onLoad="initiateCat(0)
  setCookie('clickedGeoTarget', newGeoTarget);
  return false;
}


function geoToggleVisibility(newToggledObj, newGeoTarget){
  for(var anchorN=0; anchorN< document.anchors.length; anchorN++){
    if( document.anchors[anchorN].name > newGeoTarget ){
      document.anchors[anchorN].id = 'geoInvisible';
    }else{
      document.anchors[anchorN].id = 'geoVisible';
    }
  }
  document.MasterForm.visibility.value = newGeoTarget;
  return false;
}


function changeCategory(newCheckedObj){
  if(newCheckedObj==null){   return; }   /// Ghost view
  
  if(checkedCatObj==null){      /// Retrieve initial checkedCatObj to ensure same as cookie
    var oldCatID = getCookie('clickedCatID');
    for(var i=0; i<document.MasterForm.cat.length; i++){
      if(document.MasterForm.cat[i].value==oldCatID){
        checkedCatObj = document.MasterForm.cat[i];
        break;
      }
    }
  }
  
  checkedCatObj.id='unchecked';  
  checkedCatObj = newCheckedObj;
  
  checkedCatObj.checked=true;
  checkedCatObj.id='checked';
  setCookie('clickedCatID', checkedCatObj.value);
}

function goNull(){ 
  return false;
}



function initiateFocus(){
  document.MasterForm.zipInput.focus();
}


function goLocationButton(buttonObj, newHref){   
  buttonObj.value="Clicked";
  location = newHref;
  return false;
}


function goLocation(newHref){   
  location = newHref;
  return false;
}


function goSubmit(settings){   
  document.MasterForm.settings.value = settings;
  document.MasterForm.submit();
  return false;
}

function submitOneClick(buttonObj, settings){
  if( ! alreadyClicked){
    buttonObj.value="Clicked";
    alreadyClicked = true;
    document.MasterForm.settings.value = settings;
    document.MasterForm.submit();
  }
  return false;
}


function goAccess(accessLayer_S){
  setCookie('access', accessLayer_S);
  document.MasterForm.submit();
  return false;
}


function nospamforyou(emailName) { location = "mailto:" +emailName +"@rerere.us"; }


function setS_t(text){
   window.status=text; 
   return true;
}

function setS_geo(buttonGeoTarget, id){
   var currentGeoTarget = getCookie('clickedGeoTarget');
  
   if(buttonGeoTarget==currentGeoTarget){
      window.status="Currently selected geographic focus." +id;
      return true;
   }

   var verb = buttonGeoTarget>currentGeoTarget ? " focus on " : " expand geographic focus to "; 
   switch(buttonGeoTarget){
   case 5: window.status="Click to expand to the entire USA."; break;
   case 4: window.status="Click to " +verb +" the entire state." +id; break;
   case 3: window.status="Click to " +verb +" the area of your zip code." +id; break; 
   case 2: window.status="Click to " +verb +" the county of your zip code." +id; break;
   case 1: window.status="Click to focus on your zip code only." +id; break;
   }

   return true;
}


function setS_geoVisible(buttonGeoTarget){
   switch(buttonGeoTarget){
   case 5: window.status="Click for maximum visibility."; break;
   case 4: window.status="Click for statewide visibility (but not USA-wide)"; break;
   case 3: window.status="Click for areawide visibility (but not statewide)"; break; 
   case 2: window.status="Click for countywide visibility (but not areawide)"; break;
   case 1: window.status="Click for visibility within your zip code only."; break;
   }

   return true;
}


function setS(statusTextCode){
   switch(statusTextCode){
   case 0: return setS_t('');
   case 1: return setS_t('Return to main page.');
   case 2: return setS_t('Display sub-categories of this category.'); 
   case 3: return setS_t('Shifts between items available and items wanted.'); 
   case 4: return setS_t('Switch to another location.'); 
   case 5: return setS_t('Displays the subcategory page containing this posting.'); 
   case 9: return setS_t('This is the displayed category.'); 
   case 10: return setS_t('Free items to give or wanted.'); 
   case 11: return setS_t('Recycle channels, and recycling organizations and web sites'); 
   case 12: return setS_t('Repair shops, handypersons and web sites with repair instructions'); 
   case 13: return setS_t('Go directly to the Yard Sales page.'); 
   case 14: return setS_t('Go directly to the Haulers-Recyclers page.'); 
   case 15: return setS_t('Go directly to the Hanydpersons page.'); 
   case 16: return setS_t('Click to display the details of this posting.'); 
   }
}


function removeZip(){  
   var oldCookie = getCookie('geo');
   setCookie('geo', '#');
   document.MasterForm.submit();
   return false;
}


///____ COOKIE FUNCTIONS_____________________________________________________________________________
function setCookie(cookieName, cookieValue){
   var ok = false;
   var expires = new Date();
   var timeSpan = expires.getTime() + (90 * 24 * 60 * 60 * 1000 );
   if(cookieValue=="#") timeSpan = 90;  // Set expiry to a bitfor nulled cookie
   expires.setTime(timeSpan);

   SetCookieDortch(cookieName, cookieValue, expires, '/')
}


function SetCookieDortch (name,value,expires,path,domain,secure) {  ///Bill Dortches function
  document.cookie = name + "=" +value +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}


function getCookie(name){
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i=0;
   while(i<clen){
      var j = i + alen;
      if(document.cookie.substring(i,j)==arg)
         return getCookieVal(j);
      i = document.cookie.indexOf(" ",i) + 1;
      if(i==0) break;
   }
   return "#";
}


function getCookieVal(offset){
   var endstr = document.cookie.indexOf(";", offset);
   if(endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}

