//*******************************************************************************
//AUTHOR:        JOHN CAZARES and Christian Longtin
//DATE:          JUNE 12, 200&
//FILE:          iPerceptionsInvitation_XXX.js
//DESCRIPTION:   This javascript file is put on the clients page. It checks if
//               the browser accepts cookies and if the survey has already been
//               served. It accordingly pops out the invitation for the survey.
//MODIFICATIONS:
//*******************************************************************************

//to keep this invitation active(true) or inactive(false)
var localSwitch= true;
//var localSwitch= false;

//Link to Global JS file http://localhost/Demos/LayerInvitation/
//var globalJSFile = "javascript/iPerceptions_Global.js";
var globalJSFile = "http://group11.iperceptions.com/Invitations/Javascripts/iPerceptions_Global_Layer.js";

loadScript(globalJSFile);

//*******************************************************************************
//AUTHOR:        JOHN CAZARES
//DATE:          JUNE 12, 200&
//FIUNCTION:     Hitit()
//DESCRIPTION:   This function checks the following before opening the invitation 
//               layer:
//	             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
//MODIFICATIONS:
//*******************************************************************************
function Hitit(){
	//>>>>>>>*****NEEDS TO BE CHANGED FOR EACH SURVEY*****<<<<<<<<
	//Survey Cookie 
	var surveyCookieName = "IPERCEPTIONS_442";
	var surveyCookieValue = "IPERCEPTIONS_442_COOKIE";
	var surveyCookieValueRet = "???";
	//>>>>>>>*********************************************<<<<<<<<
	
	//Test Cookie
	var testCookieName="IPERCEPTIONS_TEST";
	var testCookieValue="IPERCEPTIONS_TEST_COOKIE";
	var testCookieValueRet="???";
		
      //Set a test cookie to check if the browser accepts a cookie
	createCookie(testCookieName,testCookieValue,1);
	testCookieValueRet = readCookie(testCookieName); 
			
	//Check to see if there is a cookie has already been set by this survey
	surveyCookieValueRet = readCookie(surveyCookieName);
		
	//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 survey cookie doesnt already exist
	if (rndNum < 50 && testCookieValueRet != null && surveyCookieValueRet == null) {	
	
	    // Adjust path to invitation url from current webpage                    
        var invitationLink = "survey442/442invitation.html"
	    documentURL = String(document.location);
	    startPosition = documentURL.indexOf("//");
        endPosition = documentURL.indexOf("?");
        
        if (startPosition == -1) startPosition = 0
        if (endPosition == -1) endPosition = documentURL.length 
        
        documentURL = documentURL.substring(startPosition + 2, endPosition);   
        var nbDirs= documentURL.match(/(\/)/g);
               
        for (var i = 1; i < nbDirs.length; i++)     
            invitationLink = "../" + invitationLink      
	
		//If all tests are clear then set the cookie giving it a life of 90 days   
		createCookie(surveyCookieName, surveyCookieValue, 90);
		
		//Show the invitation Layer     
		// 1st parameter -> The html page for the invitation content 
        // 2nd param -> Width of the invitation layer
        // 3rd param -> Height of invitation layer
        // 4th param -> Whether or not to display a shadow background around invitation layer
        // 5th param -> Opacity of the background shadow - (Nb from 0.01 to 1) - Not used if param 5 is false
        // 6th param -> Specification on what to do with windowed elements - default Hide
        showLayer(invitationLink, 600, 518, true, 0.75, "");
	}

	//Delete the test cookie	
	eraseCookie(testCookieName);	
}

//*******************************************************************************
//AUTHOR:        JOHN CAZARES
//DATE:          JUNE 12, 2007
//FIUNCTION:     loadScript(file)
//DESCRIPTION:   This function loads an external javascript file into this file
//MODIFICATIONS:
//*******************************************************************************
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);
}



