/* CLIENT-ALTERABLE VARIABLES */
//ST_sample should be the fraction of the whole sample that you want to survey ie a value of 2 would mean half of the visitors
var ST_startDate = new Date(2009,5,5);
var ST_endDate = new Date(2009,6,19);
var ST_sample = 2;
var ST_pageText = 'At the moment we\'re conducting a survey about the use of our site. For this reason a <strong>popup</strong> will be opened shortly.'

/* DON'T CHANGE BELOW THIS LINE */

var ST_cookiesSupported = false;
createCookie('ST_test','supported',1);

if (readCookie('ST_test')) {
	ST_cookiesSupported = true;
	eraseCookie('ST_test');
}

ST_startDate.setMonth(ST_startDate.getMonth() - 1);
ST_endDate.setMonth(ST_endDate.getMonth() - 1);

var ST_arrivalTime = new Date();
var ST_surveyPeriod = parseInt((ST_endDate.getTime() - ST_arrivalTime.getTime())/86400000); // in days
var ST_surveyStarted = (ST_startDate.getTime() < ST_arrivalTime.getTime());

/* Initialisation main script */

var W3CDOM = document.createElement && document.getElementsByTagName;

addEventSimple(window,"load",ST_init);
var ST_newWindow;
var ST_loaded = false;

function ST_init() {
	if (!W3CDOM) return;
	if (!ST_cookiesSupported) return;
	var text = document.getElementById('sitetrekText');
	if (text) text.style.display = 'none';
	if (!ST_surveyStarted) return;
	if (ST_surveyPeriod < 0 ) return;
	var currentStatus = readCookie('ST_status');
	if (currentStatus == 'no') return;
	ST_loaded = true;
	addEventSimple(window,"unload",ST_exit); 
	eraseCookie('ST_temp_store');
	if (currentStatus == 'yes') return;
	//var ST_selectedForSurvey = (Math.random()*ST_sample < 1)
	var ST_selectedForSurvey = (Math.floor(Math.random()*ST_sample+1) == 1)
	if (!ST_selectedForSurvey) {
		createCookie('ST_status','no',1);
		return;
	}
	if (text) {
		text.innerHTML = ST_pageText;
		text.style.display = 'block';
	}
	createCookie('ST_status','yes',ST_surveyPeriod);
	addEventSimple(document,"click",ST_openPopup);
	createCookie('ST_referrer',top.document.referrer,1);
}

function ST_openPopup() {
	if (readCookie('ST_popup')) return;
	ST_newWindow = window.open('/popup.html','ST_window','width=300,height=300,resizable=yes,scrollbars=yes');
	//ST_newWindow.blur();
	removeEventSimple(document,"click",ST_openPopup);
}

function ST_exit() {
	ST_loaded = false;
	if (readCookie('ST_popup') == 'opened' && ST_newWindow)
		ST_newWindow.trackMain(location.href);
	else
		createCookie('ST_temp_store',location.href,1);
}

function removeCookie() {
	eraseCookie('ST_status'); 
	location.reload();
}

/********************

      UTILITIES
      
********************/

function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

function removeEventSimple(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}

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 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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
window.onload=function()
		{
			// check if DOM is available
			if(!document.getElementById || !document.createTextNode){return;}
			// check if there is a "No JavaScript" message
			var nojsmsgprint=document.getElementById('nojsprint');
			var nojsmsgfav=document.getElementById('nojsfav');

			//if(!nojsmsgprint){return;}

			// create a new paragraph and link to the application and replace
			// the non-JavaScript message with it.
                                                var newtxt;
                                                var newa;

                                                if(nojsmsgprint){
			newtxt='Print';	
			newa=document.createElement('a');
			newa.href='javascript:printpage()';
			newa.className='print';

			newa.appendChild(document.createTextNode(newtxt));
			nojsmsgprint.parentNode.replaceChild(newa,nojsmsgprint);
                                                }

			if(nojsmsgfav){
			//Reuse the existing variables for the fav link.

			newtxt='Add to favourites';	
			newa=document.createElement('a');
			newa.href='javascript:AddToBookmarks(\"Southampton City Council\", self.location.href)';


			newa.appendChild(document.createTextNode(newtxt));
			nojsmsgfav.parentNode.replaceChild(newa,nojsmsgfav);
                                                }

var SearchBox = document.getElementById("searchbox");
//var azBox = document.getElementById("azBox");
//var azLink = document.getElementById("azLink");
var WhereILiveBox = document.getElementById("PostCode");
if (SearchBox.addEventListener){
	SearchBox.addEventListener('focus', clearInput, false);
	SearchBox.addEventListener('blur', restoreInput, false);
	WhereILiveBox.addEventListener('focus', clearInput, false);
	WhereILiveBox.addEventListener('blur', restoreInput, false);
	//azBox.addEventListener('mouseover', mcancelclosetime, false);
	//azBox.addEventListener('mouseout', mclosetime, false);
	//azLink.addEventListener('mouseover', goAZ, false);
	//azLink.addEventListener('mouseout', mclosetime, false);
} else if (SearchBox.attachEvent){
	SearchBox.attachEvent('onfocus', clearInput);
	SearchBox.attachEvent('onblur', restoreInput);
	WhereILiveBox.attachEvent('onfocus', clearInput);
	WhereILiveBox.attachEvent('onblur', restoreInput);
	//azBox.attachEvent('onmouseover', mcancelclosetime);
	//azBox.attachEvent('onmouseout', mclosetime);
	//azLink.attachEvent('onmouseover', goAZ);
	//azLink.attachEvent('onmouseout', mclosetime);
}

//document.onclick = mclose;
};

function clearInput(el) {
var targ = (el.target) ? el.target : el.srcElement;
if (targ.defaultValue==targ.value) {targ.value = "";targ.style.color="#000000";}
}
function restoreInput(el) {
var targ = (el.target) ? el.target : el.srcElement;
if (targ.value=="") {targ.value=targ.defaultValue;targ.style.color="#999999";}
}

function AddToBookmarks (title,url)
{
	if(document.getElementById){
		if (document.all){
			window.external.AddFavorite(url, title);
			} else if (window.sidebar) {
			window.sidebar.addPanel(title, url, '');
			}
} else {
		window.location='/tools/favourites.asp';
	}
}
function printpage()
	{
	if(document.getElementById){
		window.print();
	} else {
		window.location='/tools/print.asp';
	}
}

function goAZ() {mopen("azBox");}

var timeout	= 400;
var closetimer	= 0;
var ddmenuitem	= 0;

function mopen(id)
{	
	mcancelclosetime();
	//if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
function mclose()
{
if (document.getElementById('calBox')) document.getElementById('calBox').style.visibility = 'hidden';
if (document.getElementById('calBoxT')) document.getElementById('calBoxT').style.visibility = 'hidden';
if (document.getElementById('ajaxEventsBox')) document.getElementById('ajaxEventsBox').style.visibility = 'hidden';
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function setAjax() {
var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	return ajaxRequest;
}

function updateCal(boxID, month, year, qString, qinput){

document.getElementById('thMonth' + boxID).innerHTML = 'Loading';
ajaxRequest = setAjax();
ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
document.getElementById(boxID).innerHTML = ajaxRequest.responseText;
	}
};
ajaxRequest.open("GET", "/includes/asp/ajaxcal.asp?boxID=" + boxID + "&Month=" + month + "&Year=" + year + "&href=" + qString + "&input=" + qinput, true);
ajaxRequest.send(null);
}

function updateInput(inputID, inputValue) {
document.getElementById(inputID).value = inputValue;
mclose();
}

function getEvents(eDate) {
document.getElementById("ajaxEventsBox").innerHTML="<div id='aEvents'>" +  "Loading Events for " + eDate + "</div>";
mopen("ajaxEventsBox");
ajaxRequest = setAjax();
ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
document.getElementById("ajaxEventsBox").innerHTML = "<div id='aEvents'>" +  ajaxRequest.responseText + "</div>";
	}
};
ajaxRequest.open("GET", "/includes/asp/ajaxcal.asp?oneday=" + eDate, true);
ajaxRequest.send(null);
}
