/* wv402redirect_boats.js
	This js file is included in the client homepage.
	If performs tests before poping and invitation for
	the respondent on the client homepage
*/
//to keep this invitation active(true) or inactive(false)
var keepThisInvitationActive = "true";
//var keepThisInvitationActive = "false";
//@OG - 08/31/2009 - close invitation

var date = new Date();
date.setTime(date.getTime()+(90*24*60*60*1000));
var cookExpire = date.toUTCString();

//Link to Global JS file
//Feb. 4th 2010 - switch to aicollect global shut off script
//var globalJSFile = "http://group11.iperceptions.com/Invitations/Javascripts/GlobalShutOff.js";
var globalJSFile = "http://ipinvite.iperceptions.com/Invitations/Javascripts/GlobalShutOff_aicollect.js";
loadScript(globalJSFile);

// This function checks the following before opening the invitation page:
//	1. It checks if the document/browser accepts cookies.
//	2. It checks if the cookie already exists, if it does then it does nothing
//	3. Check if the random number is generated within the invitation rate
//	4. If it passes all three tests then Display Invitation

function Hitit(){
	//Survey Cookie
	var sName = "IPERCEPTIONS_402";
	var sValue = "IPERCEPTIONS_402_COOKIE";
	var sValueRet = "???"
	
	//Test Cookie
	var gCookieName="AVCOOKIETEST";
	var gCookieValue="AV_TESTING_COOKIE";
	var gCookieValueRet="???"

	SetTestCookie(gCookieName,gCookieValue,1);
	gCookieValueRet=GetCookie(gCookieName);
	sValueRet=GetCookie(sName);
	
	//Generate a random number betweeen 0 and 9
	var rndNum;
	rndNum=Math.floor ((Math.random()*100));	
	
	
	//Perform tests before setting cookie and opening invitation:
	// 1. Check whether random number is within range of pre-defined value
	// 2. Check if document accepts cookies
	// 3. Check if the cookie doesnt already exist
	if (rndNum < 97 && gCookieValueRet!==null && sValueRet==null ) 
	{
		SetCookie(sName, sValue);
		//Open the invititation
		//OpenPopup();
		if(navigator.userAgent.indexOf("Firefox")!=-1)
        		{
           		 //Browser is Firefox
            		setTimeout('OpenPopup()', 1000);
        		}
        	else
			{
           		OpenPopup();
			}
	}	
	deleteCookie(gCookieName);	
}


// This function gets the cookie if it exists
function GetCookie(name) {
	var nameEQ = name + "=";
	var arrCookie = document.cookie.split(';');
	for(var i=0;i < arrCookie.length;i++) 
	{
		var c = arrCookie[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;
}


// This function sets the cookie if it exists
function SetCookie(name, value)
{ 
	 document.cookie = name + "=" + escape(value) + ";path=\; expires=" + cookExpire + ";"
}


//This function is used to set the cookie with default expiry date
function SetTestCookie(name, value)
{ 
	document.cookie = name + "=" + escape(value)
}


// This function deletes the cookie 
function deleteCookie(name)
{ 
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}


// This function opens the Invitation popup
function OpenPopup() {
	top.location="http://www.yamaha-motor.com/survey402/402invitation.html?div=outboard";
}


// This function loads an external javascript file into this file when called
// from this file
function loadScript(file)
{
	// Create script DOM(Document Object Model) element
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = file;

	// Alert when the script is loaded
	if (typeof(script.onreadystatechange) == 'undefined') // W3C
		script.onload = function(){ this.onload = null;  };
	else // IE
		script.onreadystatechange = function(){ if (this.readyState != 'loaded' && this.readyState != 'complete') return; this.onreadystatechange = null;  }; // Unset onreadystatechange, leaks mem in IE

	// Add script DOM(Document Object Model) element to document tree
	document.getElementsByTagName('head')[0].appendChild(script);
};







