// glider code
/*************************************************************************
  This code is from Dynamic Web Coding at www.dyn-web.com
  Copyright 2003-4 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

/*
  dw_glide.js - requires dw_lib.js
  version date July 2004 
*/

// acc is number between -1 and 1 ( -1 full decelerated, 1 full accelerated, 0 linear, i.e. no acceleration)
dynObj.prototype.slideTo = function (destX,destY,slideDur,acc,endFn) {
  if (!document.getElementById) return;
  this.slideDur = slideDur || .0001; var acc = -acc || 0;
  if (endFn) this.onSlideEnd = endFn;
  // hold destination values (check for movement on 1 axis only)
 	if (destX == null) this.destX = this.x;	else this.destX = destX;
  if (destY == null) this.destY = this.y; else this.destY = destY;
  this.startX = this.x; this.startY = this.y;
	this.st = new Date().getTime();
	// control points for bezier-controlled slide (see www.youngpup.net accelimation)
  this.xc1 = this.x + ( (1+acc) * (this.destX-this.x)/3 );
	this.xc2 = this.x + ( (2+acc) * (this.destX-this.x)/3 );
  this.yc1 = this.y + ( (1+acc) * (this.destY-this.y)/3 );
	this.yc2 = this.y + ( (2+acc) * (this.destY-this.y)/3 );
	this.sliding = true;
  this.onSlideStart();
  dw_Animation.add(this.animString + ".doSlide()");
}

dynObj.prototype.doSlide = function() {
	if (!this.sliding) return;	
	var elapsed = new Date().getTime() - this.st;
	if (elapsed < this.slideDur) {
    var x = dw_Bezier.getValue(elapsed/this.slideDur, this.startX, this.destX, this.xc1, this.xc2);
    var y = dw_Bezier.getValue(elapsed/this.slideDur, this.startY, this.destY, this.yc1, this.yc2);
		this.shiftTo( Math.round(x) ,Math.round(y) );
		this.onSlide();
	} else {	// if time's up
    dw_Animation.remove(this.animString + ".doSlide()");
		this.shiftTo(this.destX,this.destY);
		this.onSlide();
		this.sliding = false;
		this.onSlideEnd();
	}
}

dynObj.prototype.slideBy = function(dx,dy,slideDur,acc,endFn) {
	var destX=this.x+dx; var destY=this.y+dy;
	this.slideTo(destX,destY,slideDur,acc,endFn);
}

dynObj.prototype.onSlideStart = function () {}
dynObj.prototype.onSlide = function () {}
dynObj.prototype.onSlideEnd = function () { if (this.el) this.el = null; }

/*************************************************************************
  This code is from Dynamic Web Coding at www.dyn-web.com
  Copyright 2003-4 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

/*
  dw_lib.js - used with dw_glide.js, dw_glider.js, ...
  version date July 2004 
*/

dynObj.holder = {}; 
// constructor
function dynObj(id,x,y,w,h) {
  var el = dynObj.getElemRef(id);
  if (!el) return;  this.id = id; 
  dynObj.holder[this.id] = this; this.animString = "dynObj.holder." + this.id;
  var px = window.opera? 0: "px";
	this.x = x || 0;	if (x) el.style.left = this.x + px;
	this.y = y || 0;	if (y) el.style.top = this.y + px;
	this.w = w || el.offsetWidth || 0;	this.h = h || el.offsetHeight || 0;
	// if w/h passed, set style width/height
	if (w) el.style.width = w + px; if (h) el.style.height = h + px;
}

dynObj.getElemRef = function(id) { 
  var el = document.getElementById? document.getElementById(id): null;
  return el;
} 

dynObj.getInstance = function(id) {
  var obj = dynObj.holder[id];
  if (!obj) obj = new dynObj(id);
  else if (!obj.el) obj.el = dynObj.getElemRef(id);
  return obj;
}

dynObj.prototype.shiftTo = function(x,y) {
  var el = this.el? this.el: dynObj.getElemRef(this.id)? dynObj.getElemRef(this.id): null;
  if (el) {
    if (x != null) el.style.left = (this.x = x) + "px";
    if (y != null) el.style.top = (this.y = y) + "px";
  }
}

dynObj.prototype.shiftBy = function(x,y) { this.shiftTo(this.x+x, this.y+y); }

dynObj.prototype.show = function() { 
  var el = this.el? this.el: dynObj.getElemRef(this.id)? dynObj.getElemRef(this.id): null;
  if (el) el.style.visibility = "visible"; 
}
dynObj.prototype.hide = function() { 
  var el = this.el? this.el: dynObj.getElemRef(this.id)? dynObj.getElemRef(this.id): null;
  if (el) el.style.visibility = "hidden"; 
}


// for time-based animations
// resources: www.13thparallel.org and www.youngpup.net (accelimation)
var dw_Bezier = {
  B1: function (t) { return t*t*t },
  B2: function (t) { return 3*t*t*(1-t) },
  B3: function (t) { return 3*t*(1-t)*(1-t) },
  B4: function (t) { return (1-t)*(1-t)*(1-t) },
  // returns current value based on percentage of time passed
  getValue: function (percent,startVal,endVal,c1,c2) {
    return endVal * this.B1(percent) + c2 * this.B2(percent) + c1 * this.B3(percent) + startVal * this.B4(percent);
  }
}

// adapted from accelimation.js by Aaron Boodman of www.youngpup.net
dw_Animation = {
  instances: [],
  add: function(fp) {
    this.instances[this.instances.length] = fp;
  	if (this.instances.length == 1) this.timerID = window.setInterval("dw_Animation.control()", 10);
  },
  
  remove: function(fp) {
    for (var i = 0; this.instances[i]; i++) {
  		if (fp == this.instances[i]) {
  			this.instances = this.instances.slice(0,i).concat( this.instances.slice(i+1) );
  			break;
  		}
  	}
  	if (this.instances.length == 0) {
  		window.clearInterval(this.timerID);	this.timerID = null;
  	}
  },
  
  control: function() {
    for (var i = 0; this.instances[i]; i++) {
  		if (typeof this.instances[i] == "function" ) this.instances[i]();
      else eval(this.instances[i]);
    }
  }
}


// end glider code

// cookie code
/* this file handles all the cookie stuff */

function getTimeNow() {
	// create an instance of the Date object and fix the bug in Navigator 2.0, Macintosh
	var now = new Date();
	fixDate(now);
	
	now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
	//var hits = getCookie("pageCount");
	return now;
}

function isEvenInteger(num) {
   var val;
   val = parseInt(num);
   return (val % 2) == 0;
}

function isOddInteger(num) {
   var val;
   val = parseInt(num);
   return (val % 2) == 1;
}


/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
     
  document.cookie = curCookie;  
}

/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

// end cookie code

// Global vars
var showIt;
var now;
var hits;

function setTheCookie(showIt) {
// DISPLAY CODE
	now = getTimeNow();
	
	// how many times have we been here before
	hits = getCookie("pageCount");
	if (!hits) {
		// This is your first visit
		hits = 1; // the value for the new cookie
	} else {
	  // increment the hit counter
		hits = parseInt(hits) + 1;
	}

	// set the new cookie
	setCookie("pageCount", hits, now,'/');
	setCookie("showQuestionnaire", showIt, now,'/');
	
	// END DISPLAY CODE
}

function init() {
	var cookie;
	if (cookie = canSetCookie()) {
		
		showIt = getQStatus();
		
		setTheCookie(showIt);
		
		if (showIt == 1) {
			initGlide();
		}
	} else {
		return;
	}	
}

function getQStatus() {
	// get the cookie. This may be the user's first time here so check for value
	var qStatus = getCookie("showQuestionnaire");	  
	var displayIt;
	
	if ( (qStatus == null) || (qStatus == 0) )  {
		displayIt = 1;
	}
	else if (qStatus == 1) {
		displayIt = 1;
	}
	else if (qStatus == 2) {
		displayIt = 2;	
	}
	
	return displayIt;
}
					
// need to check to see whether client can accept cookies
function canSetCookie() {
	setCookie("canSetCookie",1);
	var cookie = getCookie("canSetCookie");
	if (cookie) {
		deleteCookie("canSetCookie");
	}
	 
	return cookie;
}
				 

//SLIDE IN QUESTIONNAIRE
function initGlide() {
	
	// will need to get the cookie in order to possible change the value
	//var questionnaireStatus = getQStatus();
	
	//if the questionnaire is turned off then return
//	if (questionnaireStatus == 0) {
//		return;
//	}
	if ( (hits == 4) || ( (hits > 4) && (isEvenInteger(hits)))) {
		//alert("HITS: " + hits);	
		var winWd = getWinWidth();
		// arguments: id, x, y
		var glideLyr = new dynObj("glideDiv", winWd, 20);
		glideLyr.show();
		// slideTo arguments: destination x, destination y, duration of slide,
		// acceleration/deceleration factor (can be -1 to 1) 0 is linear, i.e., steady slide
		glideLyr.slideTo(winWd-242, 100, winWd, -.9); 
	}
}

function getWinWidth() {
	var winWd = 0;
	if (document.documentElement && document.documentElement.clientWidth) 
		winWd = document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth) 
		winWd = document.body.clientWidth;
	else if (document.body && document.body.offsetWidth) 
		winWd = document.body.offsetWidth; // ns6
	else if (window.innerWidth) winWd = window.innerWidth-18;
	return winWd;
}
// END SLIDE IN QUESTIONNAIRE


function turnOffSurvey(frm) {
	if (frm.name == "form1") {
			setCookie("showQuestionnaire", 2, now,'/');
			document.getElementById('glideDiv').innerText = "";
			return true;
	}
}
	
// popUp Code
function popWindow(frm) {

	var dataString = '';
	var URL = '';
	
	//test site
	//var windowURL =  'http://195.102.142.171:9999/questionnaire/qprocess.asp?';
	
	// live site
	var windowURL =  'http://www.myharmony.co.uk/survey/qprocess.asp?';
	
	var newWindow = '';
	
	for (var i = 0; i < frm.elements.length;i++) {
		if ( (frm.elements[i].type == "radio") && (frm.elements[i].checked)) {
			dataString = dataString + frm.elements[i].name + "=" + frm.elements[i].value + "&";
		}
	}
	
	// add siteDomain and clientId to the input string sent to the server.
	dataString = dataString + "clientId=" + clientId + "&" + "siteDomain=" + siteDomain;
	// add comments to data string
	dataString = dataString + "&" + "comments=" + frm.comments.value;
	
	URL = windowURL + dataString;

	newWindow = window.open(URL,"winQuestionnaire",'scrollbars=no,menubar=no,height=400,width=400,resizable=no,toolbar=no,location=no,status=no');
	newWindow.close();
	
	document.getElementById('glideDiv').innerHTML = "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"#A0A0A0\" bgcolor=\"#EEEEEE\"> <tr><td><Span style=\"font-family: Verdana, Arial, Helvetica, sans-serif;color: #333333;font-weight: bold;font-size: 12px;\">Thank you for your time!</Span></td>  </tr></table>"
	setTimeout("clearIt()",3000);
	
	// user has submitted the form so set cookie so that questionnaire is not displayed
	setCookie("showQuestionnaire", 2, now,'/');			
}

function clearIt() {
	document.getElementById('glideDiv').innerText = "";
	
}
// end popUp Code

