
function getElement(id) {
    var idHandle = (document.getElementById ? document.getElementById(id) : (document.all ? document.all[id] : false));
    return (idHandle ? idHandle : false);
}

function showPanel(id, w, h, randomise, opacity) {
    var randomnumber = Math.floor(Math.random()*frequencyOfSurvey);//frequencyOfSurvey is set on page
    if (randomise == 'yes') { if (randomnumber != 0) { return false; } }
    var oPanel = getElement(id);
    opacity = opacity || 100;
    if (oPanel) {
        grayOut(false);
        // w is a width of the newsletter panel
        w = w || 800;
        // h is a height of the newsletter panel
        h = h || 730;
        // get the x and y coordinates to center the newsletter panel
        xc = Math.round((document.body.clientWidth / 2) - (w / 2))
        yc = Math.round((document.body.clientHeight / 2) - (h / 2))
        // show the newsletter panel
        //oPanel.style.left = xc + "px";
        //oPanel.style.top = yc + "px";
		oPanel.style.left = "50px";
        oPanel.style.top = "70px";
        oPanel.style.width = "522px";
        oPanel.style.height = "177px";
        //oPanel.style.display = 'block';
        fade(id, true, opacity);
    }
}

function hidePanel(id) {
    var oPanel = getElement(id);
    if (oPanel) {
        //oPanel.style.display = 'none';
        grayOut(false);
        fade(id, false);
    }
}

var fadeOpacity = new Array();
var fadeTimer = new Array();
var fadeInterval = 30;  // milliseconds

function fade(o, d, m) {
    // o - Object to fade in or out.
    // d - Display, true =  fade in, false = fade out
    m = m || 100;

    var obj = getElement(o);

    if ((fadeTimer[o]) || (d && obj.style.display != 'block') || (!d && obj.style.display == 'block')) {
        if (fadeTimer[o])
            clearInterval(fadeTimer[o]);
        else
            if (d)
            fadeOpacity[o] = 0;
        else
            fadeOpacity[o] = m;

        obj.style.opacity = fadeOpacity[o].toString() / 100;
        obj.style.filter = "alpha(opacity=" + fadeOpacity[o].toString() + ")";
        obj.style.MozOpacity = fadeOpacity[o].toString() / 100;

        if (d) {
            obj.style.display = 'block';
            fadeTimer[o] = setInterval('fadeAnimation("' + o + '",5,' + m + ');', fadeInterval);
        } else {
            fadeTimer[o] = setInterval('fadeAnimation("' + o + '",-5,' + m + ');', fadeInterval);
        }
    }
}

function fadeAnimation(o, i, m) {
    // o - o - Object to fade in or out.
    // i - increment, 1 = Fade In

    var obj = getElement(o);
    fadeOpacity[o] += i;
    obj.style.opacity = fadeOpacity[o].toString() / 100;
    obj.style.filter = "alpha(opacity=" + fadeOpacity[o].toString() + ")";
    obj.style.MozOpacity = fadeOpacity[o].toString() / 100;

    //alert ('o='+o+' op='+fadeOpacity[o]+' m='+m);

    if ((fadeOpacity[o] >= m) | (fadeOpacity[o] <= 0)) {
        if (fadeOpacity[o] <= 0)
            obj.style.display = 'none';
        else {
            obj.style.opacity = m.toString() / 100;
            //obj.style.filter  = "alpha(opacity="+((m+1)*10)+")";
            obj.style.filter = "alpha(opacity=" + m.toString() + ")";
            obj.style.MozOpacity = m.toString() / 100;
        }

        clearInterval(fadeTimer[o]);
        delete (fadeTimer[o]);
        delete (fadeTimer[o]);
        delete (fadeOpacity[o]);
    }
}

function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {};
  var zindex = options.zindex || 20000;
  var opacity = options.opacity || 80;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#64829D';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.parentNode.scrollWidth+'px';
        var pageHeight = document.body.parentNode.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }
    //set the shader to cover the entire page and make it visible.
    //dark.style.opacity=opaque;
    //dark.style.MozOpacity=opaque;
    //dark.style.filter='alpha(opacity='+opacity+')';
    dark.style.opacity='0';
    dark.style.MozOpacity='0';
    dark.style.filter='alpha(opacity=0)';
    dark.style.zIndex=zindex;
    dark.style.backgroundColor=bgcolor;
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    //dark.style.display='block';
    fade('darkenScreenObject',true, opacity)
  } else {
     //dark.style.display='none';
     fade('darkenScreenObject',false, opacity)
  }
}

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=/";
}

function readNunwoodCookie(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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

//event handlers
function close_pressed(){
	createCookie('305773d5-4b4a-44c6', 'no', '150');
	hidePanel('popupPanel');
	return false;
}
function yes_pressed(){
	createCookie('305773d5-4b4a-44c6', 'yes', '150');
	window.open ('https://www.nunwoodsurveys.com/webprod/cgi-bin/askiaext.dll?Action=StartSurvey&SurveyName=survey4479&ErrorPage=../pages/pgError.asp','questionnaire','menubar=0, scrollbars=1,resizable=0,width=800,height=730');
	hidePanel('popupPanel');
	self.focus();
	return false;
}
function no_pressed(){
	createCookie('305773d5-4b4a-44c6', 'no', '150');
	hidePanel('popupPanel');
	return false;
}
function launchSurvey(){
	showPanel('popupPanel',435,215,'yes'); 
}
function init_body(){
	if (readNunwoodCookie('305773d5-4b4a-44c6') != 'yes') {
		var t
		t = setTimeout("launchSurvey()", 2000);
		return false; 
	}
}

addEvent(window, "load", init_body, false);
