//alert ("iperceptions test");
/* wvXXXredirect.js
	This js file is included in the client homepage.
	It 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";



var date = new Date();
date.setTime(date.getTime()+(90*24*60*60*1000));
var cookExpire = date.toUTCString();

//Link to Global JS file
var globalJSFile = "http://group11.iperceptions.com/Invitations/Javascripts/GlobalShutOff.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 = "ipcks464";
	var sValue = "ipcks464_COOKIE";
	var sValueRet = "???"
	
	//Test Cookie
	var gCookieName="AVCOOKIETEST";
	var gCookieValue="AV_TESTING_COOKIE";
	var gCookieValueRet="???"

	//Check if the browser accepts cookies by
	//setting a test cookie and trying to retreive the 
	//name back
	SetTestCookie(gCookieName,gCookieValue);
	gCookieValueRet=Ip_Sophus_GetCookie(gCookieName);
	
	//Check to see if the cookie has not alreay been
	//set
	sValueRet=Ip_Sophus_GetCookie(sName);
	
	//Generate a random number betweeen 0 and 9
	var rndNum;
	rndNum=Math.floor ((Math.random()*10));	
		
	//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 < 4 && gCookieValueRet!=null && sValueRet==null ) 
	{
		//If all tests are clear then set the cookie
		SetCookie(sName, sValue);
		//Open the Invitation
		if(navigator.userAgent.indexOf("Firefox")!=-1)
        {
            //Browser is Firefox
            setTimeout('OpenPopup()', 1000);
        }
        else
        {
           OpenPopup();
        }
	}
	//Delete the test cookie	
	deleteCookie(gCookieName);	
}

// This function gets the cookie if it exists
function Ip_Sophus_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
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)
{ 
  //delete the cookie by setting its expiry date to now.
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = Ip_Sophus_GetCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// This function opens the Invitation popup
function OpenPopup() {
		var urlLink = document.location;
		top.location="http://www.honda.it/sophus3survey/survey464/464invitation_it.html";
}


// This function loads an external javascript file into 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);
}















