var floatingSurveyBeginDate = "2010/02/01";   //set survey begin date, format: yyyy/mm/dd
var floatingSurveyEndDate = "2010/05/27";   //set survey end date, format: yyyy/mm/dd
var floatingSurveyURL = "http://www.twmbroadband.com/survey/";   //set survey page URL
var floatingSurveyImgUrl = "../images/main/surveyicon.gif";   //set survey image
var floatingSurveyCookies = ["surveyFormCookie201002"];  //set cookies which should be checked
var floatingSurveyCookieDomain = "twmbroadband.com";   //set cookie domain
var floatingSurveyCookiePath = "/";   //set cookie path
var floatingSurveyCookieExpires = 7;   //set cookie expires date: 7 = 1 week

/*********************************************************************************************/

function addOnloadEventX(fnc){
  if (window.addEventListener) {
	// Mozilla, Opera and webkit nightlies currently support this event  
    window.addEventListener( "load", fnc, false );
  } else if (window.attachEvent) {
	// If IE event model is used	  
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else 
      window.onload = fnc;
  }
}

function floaingtStayHomeX() {
  var offsetY = 0;
  if (typeof(window.pageYOffset) == 'number') {
    //Netscape compliant
    offsetY = window.pageYOffset;
  } else if (document.body && document.body.scrollTop) {
    //DOM compliant
    offsetY = document.body.scrollTop;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    //IE6 standards compliant mode
    offsetY = document.documentElement.scrollTop;
  }
	document.getElementById('floatingSurvey').style.top = (floatDivPos + offsetY) + "px";;
}

function floatInitX(){
	floatDivPos = document.getElementById('floatingSurvey').offsetTop;
	document.getElementById('floatingSurvey').innerHTML = "<a href=\"\" onclick=\"return openSurveyX();\"><img BORDER=0 src=\"http://www.twmbroadband.com/images/main/surveyicon.gif\"></a>";	
}

function openSurveyX(){	
	window.open(floatingSurveyURL, 'surveyWindow', 'toolbar=no, menubar=no, resizable= yes, scrollbars=yes');	
	setCookieX("floatingSurveyClicked", "YES", floatingSurveyCookieExpires);
	document.getElementById('floatingSurvey').style.display = "none";
	return false;
}

function setCookieX(c_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + ";domain=" + floatingSurveyCookieDomain + ";path=" + floatingSurveyCookiePath;
}

function getCookieX(c_name) {
	if (document.cookie.length > 0) {
  	c_start = document.cookie.indexOf(c_name + "=");
  	if (c_start!= -1) {
    	c_start = c_start + c_name.length + 1;
    	c_end = document.cookie.indexOf(";",c_start);
    	if (c_end == -1) c_end=document.cookie.length;
    	return unescape(document.cookie.substring(c_start,c_end));
    }
  }
	return "";
}

var floatDivPos = 0;
var today = new Date();
var surveyBDateX = new Date();
var surveyEDateX = new Date();
// set beginning date
var floatingSBDate = floatingSurveyBeginDate.split("/");
surveyBDateX.setDate(parseInt(floatingSBDate[2]));
surveyBDateX.setMonth(parseInt(floatingSBDate[1])-1); // January = 0
surveyBDateX.setFullYear(parseInt(floatingSBDate[0])); 
// set end date
var floatingSEDate = floatingSurveyEndDate.split("/");
surveyEDateX.setDate(parseInt(floatingSEDate[2]));
surveyEDateX.setMonth(parseInt(floatingSEDate[1])-1); // January = 0
surveyEDateX.setFullYear(parseInt(floatingSEDate[0])); 

if ((today >= surveyBDateX && today <= surveyEDateX) && (getCookieX("floatingSurveyClicked") == "")) {	
	var doFloatingX = true;
	for (var i = 0; i < floatingSurveyCookies.length; i++) {
		if (getCookieX(floatingSurveyCookies[i]) != "") {		
			doFloatingX = false;
			break;
		}
	}
	
	if (doFloatingX) {
		window.onscroll = floaingtStayHomeX;
		addOnloadEventX(floatInitX);
	}
}