var DAYS = 365; //the number of days the cookie will expire
var ARRAYMAXSIZE = 4; //max size of the array for last visited pages
var PAGESCOOKIE = 'csupages'; //Name of the cookie that hold the last visited pages
var SHOWLASTPAGES = false; //insert as html the last pages as links in the div with the id SHOWLASTPAGESID
var SHOWLASTPAGESID = 'lastPages'; //where the last pages nav will be inserted. if SHOWLASTPAGES is true
var POPUPCOOKIE = 'survey'; //Name of the cookie, if it's a new survey it chould be changed

var RUNONDOMAIN =  'hsc.csu.edu.au'; //'www.csu.edu.au';
var POPUPONCEONLY = true; //if the popup will be fired once only
var POPUPCHANCE = 80; //chance in percent of the popup happen
var POPUPWHEN = 3; //how many pages till popup fired

var USEPOPUPWINDOW = false; //true will bring the popwindow and false will bring the popup layer
var POPUPTITLE = 'Survey'; //Popup window title
var POPUPURL = 'http://www.cvent.com/d/mcqn8b/3B'; //Popup window URL
var POPUPWIDTH = 600; //Popup window Width
var POPUPHEGHT = 460; //Popup window Height

//String When Using a POPUP layer
surveyPopLayerStr = '<div id="survey" style="border:3px solid #666;width:150px;margin:0;padding:1em;background:white;position:absolute;top:250px;right:10px;z-index:1000;font-size:0.7em"><span id="close"><a href="#" title="Close" onclick="javascript:getElementById(\'survey\').style.display=\'none\'" style="display:block;float:right;clear:both;margin:0;padding:0 4px;font-size:0.6em;border:1px solid #ccc;text-decoration:none;color:#333;" >x</a></span><strong>Give us your feedback on the NSW HSC Online website</strong><br /><a href="http://www.cvent.com/d/mcqn8b/3B" target="_blank" title="Launch survey">Yes, take me to the survey</a><br /><a href="#" title="Close" onclick="javascript:getElementById(\'survey\').style.display=\'none\'">No, don&#8217;t ask me again</a></div>'



/*	this function call all sequence of functions of lastpage and popups
	it will wait for the page be completely loaded to start	*/
var _addINIT_survey = window.onload;
window.onload = function(){
       setCookie(PAGESCOOKIE);
       lastVisitedPages(SHOWLASTPAGESID,PAGESCOOKIE);
       if (_addINIT_survey){_addINIT_survey();};
}


/*Fires the popup window*/
function firePopup()
{
	/*Calculate the chance of the popup be fired.*/
	if(POPUPCHANCE < Math.floor(Math.random()*100+1))
		{return;}

	/*make sure the page is past of the RUNONDOMAIN */
	if (getDocURL().indexOf(RUNONDOMAIN)==-1)
		{return;}

	/*if the popup has been fired before and if POPUPONCEONLY*/
	if(!(readCookie(POPUPCOOKIE)==null) && POPUPONCEONLY)
		{return;}

	if(USEPOPUPWINDOW)
	{popWindow();}
	else
	{popLayer();}
	
	
	
	/*create a cookie when the window popup, so it will not popup again if POPUPONCEONLY is true*/
	createCookie(POPUPCOOKIE,"poped",DAYS);
}


function popLayer()
{
	
	var d = document.createElement('div');
	d.id = 'surveyholder';
	document.body.appendChild(d);
	d.innerHTML = surveyPopLayerStr;
	
}


function popWindow()
{
	/*TRY to open the new window. If popup is bloaked it may (POPUPCHANCE) try again in a new request*/
	try
	{
		var popupwindow = window.open (POPUPURL,POPUPTITLE,'menubar=1,scrollbars=1,resizable=1,width=' + POPUPWIDTH + ',height=' + POPUPHEGHT); 	
		if ( popupwindow == null )
			return;
		if ( window.opera )
			if (!popupwindow.opera)
				return;
		}
		catch(err)
		{
			return;
	}	
}



/*Alerts when in implementation that the variables must be change*/
if (POPUPWHEN > ARRAYMAXSIZE)
{alert('The constant POPUPWHEN must be the same size or smaller than ARRAYMAXSIZE');}


//set the current page as the cookie value;
function setCookie(name)
{
	//the pair name/value is divied by the char sequence |y|
	var value = getDocURL() + '|y|' + getDocTitle();
	//call the appendToCookie function
	appendToCookie(name,value,DAYS)
}

//append the value (if unique) to the array;
function appendToCookie(name,value,days)
{
	var visitedURLs=new Array();
	if(!(readCookie(name)==null))
	{	visitedURLs = readCookie(name).split('|x|');}

	/*fire popup*/
	if(visitedURLs.length >= POPUPWHEN-1)
		{firePopup();}

	/*if the current page is not on the list of visited pages it is pushed to the list*/
	if(searchArray(visitedURLs,value)<0)
	{
		visitedURLs.push(value);
		limitSize(visitedURLs,ARRAYMAXSIZE);
		var newValue = ''
		createCookie(name,getArrayAsString(visitedURLs,'|x|'),days)	
		limitSize(visitedURLs,ARRAYMAXSIZE);
	}
}
//create the cookie as staged by the functions setCookie and appendToCookie
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//read the content of the cookie
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[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;
}

//remove cookie
function eraseCookie(name) {
	createCookie(name,"",-1);
}

/*get current window URL*/
function getDocURL()
{return location.href;}

/*get current document title*/
function getDocTitle()
{return document.title}



function getArrayAsString(myArray,separator)
{
	var value = ''
	for(var i=0;i < myArray.length;i++)
	{	
		if(value.length>0)
			{value += separator;}
		value += myArray[i];	
	}
	return value;
}



function searchArray(myArray, value)
{
	if(myArray.length>0){
		for(var i=0;i < myArray.length;i++) {
			//value exit is array return index
			if(myArray[i] == value)
				{return i;}
		}
	}
	//value not found
	return -1;
}


/*uses the implementation of array.remove to keep 
the list of pages on its max size (ARRAYMAXSIZE), FIFO (First In, First Out)*/
function limitSize(visitedURLs,ARRAYMAXSIZE)
{if(visitedURLs.length>ARRAYMAXSIZE)
	{visitedURLs.remove(0,(visitedURLs.length-ARRAYMAXSIZE)-1);	}}


function lastVisitedPages(objID,name)
{
	try{	
	var obj = document.getElementById(objID);
		if (!SHOWLASTPAGES)
		{
			obj.style.display = 'none';
			return;
		}
		else
		{
		var h = ''
		var visitedURLs=new Array();
		if(!(readCookie(name)==null))
		{	
			visitedURLs = readCookie(name).split('|x|');	
			if(visitedURLs.length>0)
				{h += '<ul>';}
			
			for(var i=visitedURLs.length-1 ; i>-1;i--)
			{
				var lk = visitedURLs[i].split('|y|');
				h += '<li><a href="' + lk[0] + '">' + lk[1] + '</a></li>'
			}
			if(h.length>0)
				{h += '</ul>';}
		}
		obj.innerHTML = h;
	}
	}catch(e)
	{return;}
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
