// COUNTDOWN TIMER
// Based on code written by: JamesMVenglarikII
// Seriously Modified by: Adrian Ashdown (aashdown@boltblue.com), 2005


// set this to the maximum required width for timers (0 = unlimited)
var CDT_MAX_WIDTH = 500;

// set this to the maximum required height for timers (0 = unlimited)
var CDT_MAX_HEIGHT = 150;

// set this to the maximum required border width for timers (0 = unlimited)
var CDT_MAX_BORDERWIDTH=10;

// set this to the maximum required inner border spacing for timers (0 = unlimited)
var CDT_MAX_INNERBORDERSPACING=10;

// set this to the root URL allowed for timer background images, i.e:
// "http://www.wdisneyw.com/forums/" to allow only images contained in this path
// you could also use the relative url style, "/forums/", which may be preferable.
// or leave it blank to allow any URL to be accepted

// to restrict to allowed locations of  background images, set the CDT_RESTRICT_IMAGEURLSTOLIST
// option to 'true', and then supply a list of allowed image url's in the aCDTImageRootURLs array.
// the url's can be specified as full http urls, or relative paths such as '/' or '/myfolder/'

// if CDT_RESTRICT_IMAGEURLSTOLIST is set to 'false' then any URL will be allowed.
var CDT_RESTRICT_IMAGEURLSTOLIST = true;

var aCDTImageRootURLs = new Array();
aCDTImageRootURLs[0] = "/";
aCDTImageRootURLs[1] = "http://www.wdisneyw.com";
aCDTImageRootURLs[2] = "http://www.wdisneyw.co.uk";



var aCDTPresetCounterStyles = new Array();
// html / text only
aCDTPresetCounterStyles[0] = new Array("Basic","Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Arial;2;B;#483D8B;;Arial;3;;#000000;;300;40;0;;0;;");
aCDTPresetCounterStyles[1] = new Array("Basic 2","Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Arial;2;B;#483D8B;;Arial;3;;;;300;40;1;#483D8B;0;;");
aCDTPresetCounterStyles[2] = new Array("Plum Pudding","Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Arial;2;B;#800080;;Arial;3;;#FFFFFF;;300;40;1;#9932CC;0;#DDA0DD;");
aCDTPresetCounterStyles[3] = new Array("Calm Green","Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Arial;2;B;#008080;;Arial;3;;#008000;;300;40;1;#2E8B57;0;#98FB98;");
aCDTPresetCounterStyles[4] = new Array("Pepermint Blue","Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Arial;2;B;#4169E1;;Arial;3;;#000080;;300;40;1;#00BFFF;0;#AFEEEE;");
aCDTPresetCounterStyles[5] = new Array("Banoffee Pie","Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Arial;2;B;#A0522D;;Arial;3;;#FF8C00;;300;40;1;#A0522D;0;#FFFACD;");
aCDTPresetCounterStyles[6] = new Array("Pretty Pink","Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Arial;2;B;#9932CC;;Arial;3;;#FF0000;;300;40;1;#DDA0DD;0;#FFC0CB;");
// graphical countdowns
aCDTPresetCounterStyles[7] = new Array("Night Sky","Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Arial;2;B;#FFFFFF;;Arial;2;B;#00FFFF;;400;70;0;;0;;/forums/cdt/nightsky1_anim.gif");
aCDTPresetCounterStyles[8] = new Array("UOG", "Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Arial;2;B;#800080;;Arial;2;B;#4169E1;;400;70;0;;0;;/forums/cdt/uog_countdown1.gif");
aCDTPresetCounterStyles[9] = new Array("LCD Panel","Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Terminal;3;B;#2F4F4F;;Terminal;2;B;#2F4F4F;;400;70;0;;0;;/forums/cdt/lcd_panel1.gif");
aCDTPresetCounterStyles[10] = new Array("Beach Paradise","Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Arial;2;B;#008080;;Arial;2;B;#A0522D;;400;70;0;;0;;/forums/cdt/beach_paradise1.gif");
aCDTPresetCounterStyles[11] = new Array("Fairy Magic","Sun Dec 25 00:00:00 UTC 2005;Christmas 2005;Having Fun!;Arial;2;B;#9932CC;;Arial;2;B;#DDA0DD;;400;70;1;#F5DEB3;0;;/forums/cdt/fairy1_anim.gif");

function cdtShowCountdown(sPresetID,sTargetDate,sTitle,sExpiryMessage){
	//set default (ie very basic) format
	var sCDTCode = "Sat Feb 19 01:33:00 UTC 2005;YOUR TITLE HERE;Having Fun!;Arial;2;;;Arial;2;;;;;;;;;";
	
	//find requested preset CDT Code
	if(aCDTPresetCounterStyles.length>0){
		for(var i=0;i<aCDTPresetCounterStyles.length;i++){
			if(aCDTPresetCounterStyles[i][0]==sPresetID){
				sCDTCode = aCDTPresetCounterStyles[i][1];
				break;
			}
		}
	}
	
	//unpack CDT Code, and modify elements
	var aSettings = sCDTCode.split(";");
	aSettings[0] = sTargetDate;
	aSettings[1] = sTitle;
	aSettings[2] = sExpiryMessage;
	
	//repack modified CDT Code
	sCDTCode="";
	for(var i=0;i<aSettings.length;i++){
		sCDTCode+=aSettings[i] + ";";
	}
	
	//display results
	cdtCountdownTimer(sCDTCode);
}


function cdtClassicCountdown(cdtTargetDate,cdtTitleText,cdtExpiryMessage){
	//the magic sauce...
	var rndNum = Math.floor(Math.random() * 10000);
	eval("var CDTDate" + rndNum + " = '" + cdtTargetDate + "';");
	
	cdtTitleText = cdtTitleText.replace('&','&amp;');
	cdtExpiryMessage = cdtExpiryMessage.replace('&','&amp;');
	
	var sHtml="<form name='CDTform" + rndNum + "' action='javascript:void(0);'>";
	sHtml+="<font color=\"darkblue\">" + cdtTitleText + "</font><br>";
	sHtml+='<input type="text" size="40" id="cdtClassicCountdownDisplay" name="cdtClassicCountdownDisplay" value="Starting Countdown..." />';
	sHtml+="</form>";
	document.writeln(sHtml);
	
	if (theFunction) theFunction += "var d = new Date();";
	else var theFunction = "var d = new Date();";
	theFunction += "var c = new Date('" + eval("CDTDate" + rndNum) + "');";
	theFunction += "var diff = (c - d)/1000;";
	theFunction += "var days = Math.floor(diff / 86400);";
	theFunction += "var rem = diff - (days * 86400);";
	theFunction += "var hours = Math.floor(rem / 3600);";
	theFunction += "rem = rem - (hours * 3600);";
	theFunction += "var minutes = Math.floor(rem / 60);";
	theFunction += "rem = rem - (minutes * 60);";
	theFunction += "var seconds = Math.floor(rem);";
	theFunction += "if ((days < 0) || (hours < 0) || (minutes < 0) || (seconds < 0)) ";
	theFunction += 'var out = "' + cdtExpiryMessage + '";';
	theFunction += "else ";
	theFunction += "var out = ' ' + days + ' days ' + hours + ' hrs. ' + minutes + ' mins. ' + seconds + ' secs.';";
	theFunction += "document.forms.CDTform" + rndNum + ".elements.cdtClassicCountdownDisplay.value = out;";
	
	setInterval(theFunction, 1000);
}


function cdtCountdownTimer(sSettings){
	// decode the settings string
	var aSettings = sSettings.split(";");
	if(aSettings.length<20) return(0);
	
	// load the settings
	var cdtTargetDate = aSettings[0];
	var cdtTitleText = aSettings[1];
	var cdtExpiryMessage = aSettings[2];
	var cdtTitleFontName = aSettings[3];
	var cdtTitleFontSize = aSettings[4];
	var cdtTitleFontStyle = aSettings[5];
	var cdtTitleFontColor = aSettings[6];
	var cdtTitleBGColor = aSettings[7];
	var cdtCounterFontName = aSettings[8];
	var cdtCounterFontSize = aSettings[9];
	var cdtCounterFontStyle = aSettings[10];
	var cdtCounterFontColour = aSettings[11];
	var cdtCounterBGColor = aSettings[12];
	var cdtWidth = aSettings[13]; 
	var cdtHeight = aSettings[14];
	var cdtBorderWidth = aSettings[15];
	var cdtBorderColor = aSettings[16];
	var cdtInnerBorderSpacing = aSettings[17];
	var cdtBGColour = aSettings[18];
	var cdtBGPictureURL = aSettings[19];
	
	// check and enforce limits
	if(CDT_MAX_WIDTH>0 && parseInt(cdtWidth)>CDT_MAX_WIDTH) cdtWidth = CDT_MAX_WIDTH.toString();
	if(CDT_MAX_HEIGHT>0 && parseInt(cdtHeight)>CDT_MAX_HEIGHT) cdtHeight = CDT_MAX_HEIGHT.toString();
	if(CDT_MAX_BORDERWIDTH>0 && parseInt(cdtBorderWidth)>CDT_MAX_BORDERWIDTH) cdtBorderWidth = CDT_MAX_BORDERWIDTH.toString();
	if(CDT_MAX_INNERBORDERSPACING>0 && parseInt(cdtInnerBorderSpacing)>CDT_MAX_INNERBORDERSPACING) cdtInnerBorderSpacing = CDT_MAX_INNERBORDERSPACING.toString();
	
	// check to see if background image url is on the allowed list, if url restriction is on
	// and disable image if the url is not authorised
	if(CDT_RESTRICT_IMAGEURLSTOLIST){
		if(aCDTImageRootURLs.length>0){
			var bURLMatched = false;
			for(var i=0; i<aCDTImageRootURLs.length; i++){
				if(cdtBGPictureURL.indexOf(aCDTImageRootURLs[i])==0){
					bURLMatched=true;
				}
			}
			if(!bURLMatched) cdtBGPictureURL="";
		} else {
			cdtBGPictureURL="";
		}
	}

	// decode bold, italic, and underline font styles
	var cdtTitleTextPrefix="";
	var cdtTitleTextSuffix="";
	if(cdtTitleFontStyle.indexOf('B')>=0){
		cdtTitleTextPrefix = cdtTitleTextPrefix + '<b>';
		cdtTitleTextSuffix = '</b>' + cdtTitleTextSuffix;
	}
	if(cdtTitleFontStyle.indexOf('I')>=0){
		cdtTitleTextPrefix = cdtTitleTextPrefix + '<i>';
		cdtTitleTextSuffix = '</i>' + cdtTitleTextSuffix;
	}
	if(cdtTitleFontStyle.indexOf('U')>=0){
		cdtTitleTextPrefix = cdtTitleTextPrefix + '<u>';
		cdtTitleTextSuffix = '</u>' + cdtTitleTextSuffix;
	}
	
	var cdtCounterTextPrefix="";
	var cdtCounterTextSuffix="";
	if(cdtCounterFontStyle.indexOf('B')>=0){
		cdtCounterTextPrefix = cdtCounterTextPrefix + '<b>';
		cdtCounterTextSuffix = '</b>' + cdtCounterTextSuffix;
	}
	if(cdtCounterFontStyle.indexOf('I')>=0){
		cdtCounterTextPrefix = cdtCounterTextPrefix + '<i>';
		cdtCounterTextSuffix = '</i>' + cdtCounterTextSuffix;
	}
	if(cdtCounterFontStyle.indexOf('U')>=0){
		cdtCounterTextPrefix = cdtCounterTextPrefix + '<u>';
		cdtCounterTextSuffix = '</u>' + cdtCounterTextSuffix;
	}
	
	cdtTitleText = cdtTitleText.replace('&','&amp;');
	cdtExpiryMessage = cdtExpiryMessage.replace('&','&amp;');
	
	//the magic sauce...
	var rndNum = Math.floor(Math.random() * 10000);
	eval("var CDTDate" + rndNum + " = '" + cdtTargetDate + "';");
	
	var sHtml="<form name='CDTform" + rndNum + "' action='javascript:void(0);'>";
	sHtml+="<table width='" + cdtWidth + "' height='" + cdtHeight + "' border='" + cdtBorderWidth + "' bordercolor='" + cdtBorderColor + "' cellspacing='" + cdtInnerBorderSpacing + "' bgcolor='" + cdtBGColour + "' background='" + cdtBGPictureURL + "'>";
	sHtml+="<tr><td align='center'>";
	sHtml+="<span style='background-color:" + cdtTitleBGColor + "'>";
	sHtml+="<font face=\"" + cdtTitleFontName + "\" size=\"" + cdtTitleFontSize + "\" color=\"" + cdtTitleFontColor + "\">" + cdtTitleTextPrefix + cdtTitleText + cdtTitleTextSuffix + "</font><br>";
	sHtml+='</span><font face=' + cdtCounterFontName + ' size=' + cdtCounterFontSize + ' color=' + cdtCounterFontColour + '>' + cdtCounterTextPrefix;
	sHtml+="<span style='background-color:" + cdtCounterBGColor + "' id='cdtFunkyCountdownDisplay" + rndNum + "' name='cdtFunkyCountdownDisplay" + rndNum + "'>Starting Countdown...</span>";
	sHtml+=cdtCounterTextSuffix + "</font></td></tr>";
	sHtml+="</table>";
	sHtml+="</form>";
	document.writeln(sHtml);
	
	if (theFunction) theFunction += "var d = new Date();";
	else var theFunction = "var d = new Date();";
	theFunction += "var c = new Date('" + eval("CDTDate" + rndNum) + "');";
	theFunction += "var diff = (c - d)/1000;";
	theFunction += "var days = Math.floor(diff / 86400);";
	theFunction += "var rem = diff - (days * 86400);";
	theFunction += "var hours = Math.floor(rem / 3600);";
	theFunction += "rem = rem - (hours * 3600);";
	theFunction += "var minutes = Math.floor(rem / 60);";
	theFunction += "rem = rem - (minutes * 60);";
	theFunction += "var seconds = Math.floor(rem);";
	theFunction += "if ((days < 0) || (hours < 0) || (minutes < 0) || (seconds < 0)) ";
	theFunction += 'var out = "' + cdtExpiryMessage + '";';
	theFunction += "else ";
	theFunction += "var out = ' ' + days + ' days ' + hours + ' hrs. ' + minutes + ' mins. ' + seconds + ' secs.';";
	//theFunction += "var cdt =  CDTform" + rndNum + ".children;";	//.cdtFunkyCountdownDisplay;
	theFunction += "var cdt =  document.getElementById('cdtFunkyCountdownDisplay" + rndNum + "');";
	theFunction += "cdt.innerHTML = out;";
	
	setInterval(theFunction, 1000);
}

