// This file contains the JavaScript code for the Familiworks website
// Copyright 2006 Barbara Tripp - Tripp Web Solutions
// Some of these scripts are open source scripts obtained from others

// The following variables are used for showing the disclaimer info
// they are used in tips.php, disclaimer.php and the various tip pages

var disclaimwindow;
var tiphref="";
// The following is used to open a new window
function MM_openBrWindow(theURL,winName,features) { //v2.0
  disclaimwindow = window.open(theURL,winName,features);
 // window.open(theURL,winName,features);
}

// The next two functions are used to open the disclaimer window
// This can be either in the tips main page (tips.php) or from an individual
//  tip page if the referrer is from outside familiworks
function set_tiphref(newhref)
{
	tiphref=newhref;
}
function open_disclaimerwindow()
{
	// If the disclaimer has already been agreed to, we don't need to open the disclaimer window
	// and we return true so that the link from the main page is executed
	// If the disclaimer has not been agreed to, we open the window and return false to prevent the 
	// main page from executing the link to the linkpage
	if (checkdisclaimeragreed() == 1) {
		return true;
	}
		
	MM_openBrWindow('disclaimer.php','disclaimer','scrollbars=yes,resizable=yes,width=500,height=320');
	// in case the window was already open, change the focus.  Note however that not all browsers
	// support the focus() method
	disclaimwindow.focus();
	return false;
}	

// the following function replaces the href of the opener window and closes the current window 
function loadopener() {
// 		They get here by clicking on I agree
//   If the visitor got to this page other than via the main page, the tiphref won't 
//   be available.  In this case, we return true so that the url in the href is used to take  them back to main.php and we assume there is no other window open so we don't close this one
//   If tiphref is defined, clicking on I agree will take them to the correct page
//   In that case, we return false to prevent the link from taking them to main.php

	var newhref;
	var foundhref = 1;
	
	// get the href that we want to switch to
	try
	{
		
		newhref = window.opener.tiphref;
	}
	catch (e)
	{
		foundhref = 0;
	}
	
	//  set a cookie to keep track of their having agreed
	setCookie("disclaim", 1, "", "");
//  verify that the cookie was set
	var cookieValue = getCookieValue("disclaim");
	//alert("in the disclaim function, cookieValue is " + cookieValue);
	
	// if the cookie has not been set and the newhref is known, we append a string so that the linked page knows the visitor has agreed
	// The string is comprised of the current time plus the visitor's IP address
	
	if (foundhref == 1 && cookieValue != 1)
		{
			var currDate = new Date;
			var dateinvalue = currDate.valueOf();
			newhref=newhref + "?d" + dateinvalue + "i" + userip;
		}
		
		
	if (foundhref == 0)
		{
			return true;
		}
	else
		{
			opener.location.href = newhref;
			self.close();
			return false;
		}
}
//

		
// The following two functions are for getting and setting cookies
function getCookieValue(cookieName)
{
	var cookieValue = document.cookie;
	var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
	if (cookieStartsAt == -1)
	{
		cookieStartsAt = cookieValue.indexOf(cookieName + "=");
	}
	if (cookieStartsAt == -1)        // cookie name was not found
	{
		cookieValue=null;
	}
	else      // cookie name was found
	          // get the value starting in the next positin
	{
		cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
		cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
		if (cookieEndsAt == -1)      // its the last cookie name and not followed by :
		{
			cookieEndsAt = cookieValue.length;    
		}
		cookieValue = unescape(cookieValue.substring(cookieStartsAt,
				cookieEndsAt));
	}
	return cookieValue;
}


function setCookie(cookieName, cookieValue, cookieExpires, cookiePath)
{
	// if cookieExpires and cookiePath are null it will use the default values
	// the escape function encodes values that are not alpha or numeric
	cookieValue = escape(cookieValue);
	// The following set an expiration one month in the future if the expiration parameter is null
	// Uncomment this code if you want to default to one month.  Otherwise the cookie will exist only 
	// for the current browser session
	//if (cookieExpires == "")
	//{
		//var nowDate = new Date();
		//nowDate.setMonth(nowDate.getMonth() + 1);
		//cookieExpires = nowDate.toGMTString();
	//}	
	if (cookiePath != "")
	{
		cookiePath = ";Path=" + cookiePath;
	}
	document.cookie = cookieName + "=" + cookieValue + 
		";expires=" + cookieExpires + cookiePath;
}

// The following checks to see if the visitor agreed to the disclaimer
// Returns 1 if the cookies exists or if you are in a linkpage and the search parameter indicates
// they came from the disclaimer page after clicking on "I agree"
// Otherwise it returns a 0 - meaning no cookie set and url search parameter does not indicate they agreed
function checkdisclaimeragreed()
{
	// first check if the disclaim cookie exists.  If it contains 1, then they agreed
	// To test the processing of the search string comment out the following statement
	if (getCookieValue("disclaim") == 1)
		{
			return 1;
		}
	// if the cookie didn't exist, we check to see if the search string
	// contains the time of agreeing to the disclaimer and the ip address
	// In this case, we limit access to the tips to 2 hours from the time they accepted the disclaimer
	// We add the check on ip address to cover the situation where the link was sent to someone else
	// (via email or other method).  By including the ip address the new visitor must agree to the disclaimer on their computer
	var passedinvalues = document.location.search.substring(1);
	var datestartloc = passedinvalues.indexOf("d")+1;
	var ipstartloc = passedinvalues.indexOf("i")+1;
	var dateendloc = ipstartloc - 1;
	var passedindate = passedinvalues.substring(datestartloc, dateendloc);
	var passedinip = passedinvalues.substring(ipstartloc);
	//alert("passedindate=" + passedindate + ", passedinip =" + passedinip);
	var nowdate = new Date;
	var elapsedtime = nowdate.valueOf() - passedindate;
	//alert ("elapsedtime=" + elapsedtime);
	
	// check the ip address
	if (passedinip == userip)
		{
			//alert("ip matches");
		}
	else
		{
			//alert("ip does not match");
		}
		
	// check the allowable elapsed time  there are 36000000 milliseconds in an hour.  So we have it set to 2 hours with the following statement.  Override it by uncommenting the second statement
	// you can change this if you want a shorter or longer time
	var maxallowedtime = 3600000 * 2;
	//maxallowedtime = 0;
	if ((elapsedtime < maxallowedtime) && (passedinip == userip))
		{
			//alert("passed the validation");
			return 1;
		}
	else
		{
			//alert("its a different machine or its been too much elapsed time");
			return 0;
		}
}

//  If the visitor got to the current page without having agreed to the disclaimer
//  we need to send them to an alternate page.  For Familiworks, we send them to 
//  the main tips page where they will have the opportunity to click on the links to 
//  open the disclaimer page
function altpage_checkandswitch()
{
	//var disclaim = document.location.search.substring(1);
	//var cookieValue = getCookieValue("disclaim");
	
	// check if they agreed to disclaimer
	// If not it sends you to an alternate page, passing in the current href with all its passed values
	// You can set it to go back to the main page by changing the setting of the href below
	//  to whatever page your want
	if (checkdisclaimeragreed() == 0)
		{
	//		alert ("they did not agree to the disclaimer");
			var newhref = "tips.php";
			document.location.href = newhref;
		}
	
}
//
//

