
//alert ("iPerceptions test");
/* wvXXXredirect.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";

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://dsvr.iperceptions.com/Invitations/Javascripts/GlobalShutOffDellSupport.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 = "PNcookie359";
	var sValue = "PNcookie359_COOKIE";
	var sValueRet = "???";
	
	//Test Cookie
	var gCookieName="AVCOOKIETEST";
	var gCookieValue="AV_TESTING_COOKIE";
	var gCookieValueRet="???";
	
	//Session Cookie
    var sessionCName = "IPE_DellSession"; var sessionCV = "IPE_DellSession_COOKIE";
    var sessionCVR; sessionCVR = GetCookie(sessionCName);

	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()*10));	
	
	//alert ("HitIt " + rndNum);
	//if (gCookieValueRet!==null)
	//	alert("Cookie: " + gCookieValueRet);
	//else
	//	alert("This browser doesnot accept Cookies");	
	//if (sValueRet!==null)
	//	alert("Cookie: " + sValueRet);
	//else
	//	alert("Cookies not set");
	
	
	//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 < 2 && gCookieValueRet!==null && sValueRet==null && sessionCVR === null)  
	{
		SetCookie(sName, sValue);
		//Open the invititation
		//OpenPopup();
		if(navigator.userAgent.indexOf("Firefox")!=-1)
        {
            //Browser is Firefox
            setTimeout('OpenPopup()', 1000);
        }
        else
        {
           OpenPopup();
        }
	}	
	//Create Session Cookie
    SetTestCookie(sessionCName, sessionCV,1);
    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.dell.com/html/global/iperception/study359/360invitation_la_en.html";
		top.location="http://support.dell.com/support/topics/global.aspx/sitelets/feedback/iperceptions/iperceptions?c=my&l=en";
}


// 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);
};