/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com */

/*******************************************************************************
 * Project: RestoreLastVisitedSecP.js
 * Author: Dmitriy Simonchik (mailto: Dmitriy.Simonchik(at)gmail.com)
 * Modified: 29 Oct 2007
 * Web: http://www.thebridge.ru/last-visited-page/
 * Licence: LGPL
 * Remarks: If you improve this code, please share your modification
 *          Send your changes to me and I'll add them to the distribution.
 *          Thanks.
 ******************************************************************************/
// block of functions for work with cookies
function createCookie(nameS,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 = nameS+"="+value+expires+"; path=/";
}

function readCookie(nameS) {
	 var nameSEQ = nameS + "=";
 	var cs = document.cookie.split(';');
 	for(var i=0;i < cs.length;i++) {
  		var s = cs[i];
  		while (s.charAt(0)==' ') s = s.substring(1,s.length);
  		if (s.indexOf(nameSEQ) == 0) return s.substring(nameSEQ.length,s.length);
 	}
	return null;
}

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

// script body
// setting default values for parameters
if(typeof(Fav3MainPage) == "undefined") {
  var loc = document.location.toString().split('');
  var Fav3MainPage = "http://www.ifind2.com/Selection03.php4";
}

if(typeof(sePicNotification) == "undefined")
  var sePicNotification = "You have been automatically redirected to last visited page on the site.";

//redirecting user to last visited page if he
//enters the site through main page
if(document.location == Fav3MainPage) {
  //redirect only if user navigates to main page first time
  //in current session (if referrer not contains main page address)
  var storedData = readCookie("Fav3");
  if(document.referrer.indexOf(Fav3MainPage) == -1 && storedData != null) {
    LastVisitedSecPURL = decodeURI(storedData);
    if(LastVisitedSecPURL != null && LastVisitedSecPURL != Fav3MainPage) {
      document.location = LastVisitedSecPURL;
      sePicMessage = sePicNotification;
    }
  }
}
// storing last visited page for a year in cookie
if(typeof(sePicDisabled) == "undefined")
  createCookie("Fav3", encodeURI(document.location), 365);

