///////////////////////////////////////////////////////////////////////////
//Constants...

var SURVEY_FORMAT_SINGLEQUESTION 	= "survey";
var SURVEY_FORMAT_SINGLESECTION 	= "survey_Section";
var SURVEY_FORMAT_ALLSECTIONS 		= "survey_AllSections";

///////////////////////////////////////////////////////////////////////////
//Class vars...
//showEveryXHits determines teh frequency the survey is shown, it is basically 1 out of X
//The higher X is, the LESS frequently the survey is shown.
//For testing, use 1 to guarantee seeing it every time you refresh on a client page...

var surveyFormat 					= SURVEY_FORMAT_SINGLEQUESTION;
var showEveryXHits 					= 5;
	
var cid;

///////////////////////////////////////////////////////////////////////////
//CID Setter, called from injected page to initiate survey setup...

function SetCID(id)
{
	cid = id;
	
	if (readCookie("EMG_SURVEY_LOCKOUT") == null || readCookie("EMG_SURVEY_LOCKOUT") == "undefined")
	{
		StartSurvey();
	}
}

///////////////////////////////////////////////////////////////////////////
//Method to actually construct and show survey as DIV on client site...

function StartSurvey()
{
	var currentFrequency = 1 / showEveryXHits;
	var rnd = Math.random();
	
	if (rnd <= currentFrequency)
	{
		var xPos = getViewportSize()[0] / 2 - 350;
		var yPos = getViewportSize()[1] / 2 - 310;
		
		if (xPos < 20) { xPos = 20; }
		if (yPos < 20) { yPos = 20; }
				
		document.write("<div id='EMG_Survey' style='width:700px; height:620px; z-index:500; position:absolute; left:" + xPos + "px; top:" + yPos + "px; border:5px solid #0072bc; background-color:#0072bc;'><iframe name='I1' src='http://www.ecomarketing.com.au/survey/" + surveyFormat + ".asp?clnid=" + cid + "' frameborder='no' width='700' height='590'></iframe><div style='padding:5px;'><a style='font-family:Arial; font-size:9pt; color:white;' href='javascript:;' onclick='CloseSurvey();'>Close</a> | <a style='font-family:Arial; font-size:9pt; color:white;' href='javascript:;' onclick='LockOut();'>Close, and please don\'t ask me again...</a></div></div>")
	}
}

///////////////////////////////////////////////////////////////////////////
//Closes survey on client site...

function CloseSurvey()
{
	document.getElementById("EMG_Survey").style.display = "none";
}

///////////////////////////////////////////////////////////////////////////
//Creates cookies to prevent survey from re-appearing again for a period of 'X' days...

function LockOut()
{
	createCookie("EMG_SURVEY_LOCKOUT", "True", (5 * 365.25));
	CloseSurvey();
}

///////////////////////////////////////////////////////////////////////////
//Service method to create cookies...

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=/";
}

///////////////////////////////////////////////////////////////////////////
//Service method to read a 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;
}

///////////////////////////////////////////////////////////////////////////
//Service method to erase a cookie...

function eraseCookie(name) {
	createCookie(name,"",-1);
}

///////////////////////////////////////////////////////////////////////////
//Service UI/UX method to determine browser window/available size...

function getViewportSize()
{
  var size = [0,0];

  if (typeof window.innerWidth != 'undefined')
  {
    size = [
        window.innerWidth,
        window.innerHeight
    ];
  }
  else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth != 'undefined'
      && document.documentElement.clientWidth != 0)
  {
    size = [
        document.documentElement.clientWidth,
        document.documentElement.clientHeight
    ];
  }
  else
  {
    size = [
        document.getElementsByTagName('body')[0].clientWidth,
        document.getElementsByTagName('body')[0].clientHeight
    ];
  }

  return size;
}

///////////////////////////////////////////////////////////////////////////
