// ######## BROWSER DETECT STUFF
var isNS = (navigator.appName == "Netscape");
var isMacIE4 = ( (navigator.userAgent.indexOf("IE 4") > -1) && (navigator.userAgent.indexOf("Mac")  > -1) );
var isNS4 = (document.layers) ? true:false
var isIE4 = (document.all) ? true:false
var isIE5 = false;
var isIE6 = false;
var isIE = false;
if (isIE4)
	if (navigator.userAgent.indexOf('MSIE 5')>0)
		isIE5 = true;
	else
		if (navigator.userAgent.indexOf('MSIE 6')>0)
			isIE6 = true;
if (isIE4 || isIE5 || isIE6) isIE = true; else isIE = false;



// ######## SMALLER DOC WRITE
var d=document;
function w(c){d.write(c);}
function wl(c){d.writeln(c);}

// ######## NETSCAPE SIDE PANEL
function addNetscapePanel() { 
   if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) 
   { 
      window.sidebar.addPanel ("What's On TV Now with myDigiGuide", 
      "http://www.mydigiguide.com/dgx/nssidebar/index.html",""); 
   } 
   else 
   { 
      var rv = window.confirm ("This page is enhanced for use with Netscape 6.  " + "Would you like to upgrade now?"); 
      if (rv) 
         document.location.href = "http://home.netscape.com"; 
   } 
} 

// ######## NAVIGATION BAR FUNCTION
function fnHighlight(element, on)
{
	if (on==0)
	{
		element.style.color='black'; element.style.background='#F4E4AB';
		//document[img].src = '/i/p.gif';
	}
	else
	{
		element.style.color='blue'; element.style.background='white'; //#ffcc99';
		//document[img].src = '/i/arr.gif';
	}
	return true;
}


// ######## PRINTING
function printThis()
{
	if (typeof print == "object" || typeof print == "function")
		print();
}


// ######## Load into parent window and close active top window
function LoadAndCloseWin( strURL )
{
	window.top.opener.location.href( strURL );
	window.close();
}

// ######## Load popup window
function OpenWin( strURL, strWindowParams )
{
	var winParams = "resizable,scrollbars";
	if( strWindowParams == null || !strWindowParams.length )
		strWindowParams = "top=50,left=50,width=550,height=550";

	if( strWindowParams.charAt(0) != ',' )
		winParams += ',';

	winParams += strWindowParams;
	popupWin = window.open(strURL,'popup_win',winParams)
}
function OpenWinWide( strURL )
{
	popupWin = window.open(strURL,'popup_win','resizable,scrollbars,top=50,left=50,width=1000,height=550')
}


//
// COOKIE FUNCTIONS
function getCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

function SetCookie (name,value)
{
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;

	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;

/*
  alert( name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "") );
*/
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function DeleteCookie (name)
{
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  // This cookie is history
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// ######## BEGIN: CHECK FOR A VALID EMAIL
var whitespace = " ,;:~#'/!\t\n\r";

// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a whitespace character.
    // When we do, return true; if we don't, return false.

    for (i = 0; i < s.length; i++)
    {   
       // Check if current character is a whitespace.
       var c = s.charAt(i);
       if (whitespace.indexOf(c) != -1) return true;
    }

    // No whitespaces.
    return false;
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must only be one @
// * there must be at least one character before and after the .
// * the characters @ and . are both required

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return false;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;

    // Count number of @'s
    var i=0;
    var found=0;
    while ((i < s.length))
    { if (s.charAt(i) == "@")
    		found++;
    	i++;
    }
    // more than one @
    if (found > 1) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function validateEmail(frmName)
{
	with( frmName )
	{
		if( isEmail(Email.value) )
			return true;
		else
		{
			window.alert( "Please enter a valid e-mail address." );
			Email.focus();
			Email.select();
			return false;
		}
	}
}
// ######## END: CHECK FOR A VALID EMAIL


function validateAffiliate(frmName)
{
	with( frmName )
	{
		if (isEmpty(firstname.value))
		{
			window.alert( "Please enter your first name." );
			firstname.focus();
			firstname.select();
			return false;
		}
		if (isEmpty(lastname.value))
		{
			window.alert( "Please enter your last name." );
			lastname.focus();
			lastname.select();
			return false;
		}
		if (isEmpty(add1.value))
		{
			window.alert( "Please enter your address." );
			add1.focus();
			add1.select();
			return false;
		}
		if (isEmpty(add2.value))
		{
			window.alert( "Please enter your address." );
			add2.focus();
			add2.select();
			return false;
		}
		if (isEmpty(postcode.value))
		{
			window.alert( "Please enter your post code." );
			postcode.focus();
			postcode.select();
			return false;
		}
		if (isEmpty(telephone.value))
		{
			window.alert( "Please enter your telephone." );
			telephone.focus();
			telephone.select();
			return false;
		}
		if (isEmpty(web.value))
		{
			window.alert( "Please enter your web address." );
			web.focus();
			web.select();
			return false;
		}
		if (isEmpty(payable.value))
		{
			window.alert( "Please enter who you want any cheques payed to." );
			payable.focus();
			payable.select();
			return false;
		}

		if( isEmail(email.value) )
			return true;
		else
		{
			window.alert( "Please enter a valid e-mail address." );
			email.focus();
			email.select();
			return false;
		}
	}
}

function validateDetails(frmName)
{
	with( frmName )
	{
		if (isEmpty(firstname.value))
		{
			window.alert( "Please enter your first name." );
			firstname.focus();
			firstname.select();
			return false;
		}
		if (isEmpty(lastname.value))
		{
			window.alert( "Please enter your last name." );
			lastname.focus();
			lastname.select();
			return false;
		}
		
		if( isEmail(email.value) )
			return true;
		else
		{
			window.alert( "Please enter a valid e-mail address." );
			email.focus();
			email.select();
			return false;
		}
	}
}

function validateVPlus(frmName)
{
	with( frmName )
	{
		if (isEmpty(name.value))
		{
			window.alert( "Please enter your full name." );
			name.focus();
			name.select();
			return false;
		}
		if (isEmpty(message.value))
		{
			window.alert( "The e-mail message is empty. It seems you have deleted the text, please ensure that the box contains something." );
			message.focus();
			message.select();
			return false;
		}
		
		if( isEmail(email.value) )
			return true;
		else
		{
			window.alert( "Please enter a valid e-mail address." );
			email.focus();
			email.select();
			return false;
		}
	}
}

// Check that the newsletter subscription form to be submitted is correct
function validateNewsletter(frmName)
{
	with( frmName )
	{
		if (!CheckNewsletters(frmName)) return false;
		else if (!validateDetails(frmName)) return false;
	}
	return true;
}

// Check that at least one newsletter subscription checkbox is selected
function CheckNewsletters(frmName)
{
	var n = 1;
	for( ;; )
	{
		if( eval( "frmName.news_" + n ) )
		{
			if( eval( "frmName.news_" + n + ".checked" ) )
				return true;
		}
		else
			break;
		n++;
	}
	alert( "You need to select at least one newsletter." );
	return false;
}

function doOpenWindow( url, name )
{
	var popupWin = window.open(url,name,'resizable,scrollbars,top=10,left=10,width=720,height=525')
}

function getEl(strEl)
{
	var el = document.getElementById ? document.getElementById(strEl) : document.all[strEl];
	return(el);
}

function toggleEl(strEl)
{
	var el = document.getElementById ? document.getElementById(strEl) : document.all[strEl];
	alert(strEl);
	if( eval( el ) )
	{
		el.style.display = el.style.display == "none" ? "" : "none"	
	}
}


function showEl(strEl, bShow)
{
	var el = getEl(strEl);
	
	if(eval(el))
	{
	    el.style.display = bShow ? "" : "none"	
	}

}

function getStyleFromCsssClass(strClass, cssElement)
{
	var cssRules;
	 if (document.all) {
	  cssRules = 'rules';
	 }
	 else if (document.getElementById) {
	  cssRules = 'cssRules';
	 }
	
	for (var S = 0; S < document.styleSheets.length; S++)
	{
	    if(document.styleSheets[S].href.toString().indexOf("library.css") >= 0)
	    {
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) 
		{
		   if (document.styleSheets[S][cssRules][R].selectorText.toString().indexOf(strClass) >= 0) 
		   {
		    	return(document.styleSheets[S][cssRules][R].style[cssElement]);
		   }
		}
		
	     }
		 	
	}
	
	return(0);
}





function changeBgImg(el, value)
{
	if(eval(el))
	{	
		
		setStyleForKey('backgroundImage', el.style.background != 'undefined' ? el.style.background : '');
		
		el.attributes['class'].value = 'gradient_blue';
	}
}

function changeBgImg_gradient(el)
{
	if(eval(el))
	{	
		changeBgImg(el, 'url(/i/grad2_blue.jpg) repeat-x');
	}
}

function restoreBgImg(el)
{
	
	if(el)
	{
		el.attributes['class'].value = 'gradient';
	}
}

var g_PrevClass = "";
var g_prevObject = null;

function normalBg(el)
{
	 var e = el;
   if(el && g_PrevClass != "")
	 {
	 	e.attributes['class'].value = g_PrevClass;
		g_PrevClass = "";
		g_PrevObject = null;
	 }
}

function highlight_bg(el)
{
	var e = el;
	if(g_PrevClass != "")
	{
		normalBg(g_prevObject);
	}
	g_PrevClass = e.attributes['class'].value;
	g_prevObject = e;
	e.attributes['class'].value = 'highlight-red';
	
}

function highlightBg_Red(bReplace, el)
{
	var e = el;
	if(e)
	{
		if(g_PrevClass != "")
		{
			normalBg(g_prevObject);
		}
		g_PrevClass = e.className;
		g_prevObject = e;
		if(bReplace)
		{
			e.className = 'highlight-red';
		}
		else
		{
			e.className += ' highlight-red';
		}
	}
}

function restoreStyle(el)
{
   //alert(g_PrevClass);
	 var e = el;
   if(el && g_PrevClass != "")
	 {
	 	e.attributes['class'].value = g_PrevClass;
		g_PrevClass = "";
		g_PrevObject = null;
	 }
}

function changeStyle(el, strClass)
{
 
	var e = el;
	if(g_PrevClass != "")
	{
		normalBg(g_prevObject);
	}
	if(e.attributes['class'])
	{
		g_PrevClass = e.attributes['class'].value;
		e.attributes['class'].value = strClass;
	}
	g_prevObject = e;
	
	
}



	// The following Function is taken from http://prototype.conio.net/
	
  function getXMLHTTP() 
  {
    	var _ms_XMLHttpRequest_ActiveX = ""; 
    	    if (window.XMLHttpRequest) {
				// Not IE
			        AJAX = new XMLHttpRequest();
			    } else if (window.ActiveXObject) {
				// Hello IE!
			        // Instantiate the latest MS ActiveX Objects
			        if (_ms_XMLHttpRequest_ActiveX) {
			            AJAX = new ActiveXObject(_ms_XMLHttpRequest_ActiveX);
			        } else {
				    // loops through the various versions of XMLHTTP to ensure we're using the latest
				    var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
			                        "Microsoft.XMLHTTP"];
			
			            for (var i = 0; i < versions.length ; i++) {
			                try {
					    // try to create the object
					    // if it doesn't work, we'll try again
					    // if it does work, we'll save a reference to the proper one to speed up future instantiations
			                    AJAX = new ActiveXObject(versions[i]);
			
			                    if (self.AJAX) {
			                        _ms_XMLHttpRequest_ActiveX = versions[i];
			                        break;
			                    }
			                }
			                catch (objException) {
			                // trap; try next one
			                } ;
			            }
			
			            ;
			        }
    }
		 return(AJAX);
    }
    
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


		function resizeOverFlows()
		{
			var nWidth, nOffset;
			if(document.all)
			{
				nWidth = document.body.clientWidth;
				nOffset = 80;
			}
			else if(document.getElementById && !document.all)
			{
				nWidth = window.innerWidth;
				nOffset = 80;
			}
			var pre_nodes = document.getElementsByTagName("pre");
			var i;
			for(i = 0; i < pre_nodes.length; i++)
			{
				if(pre_nodes[i].parentNode)
				{
					if(pre_nodes[i].parentNode.className=="overflow")
					{
						pre_nodes[i].parentNode.style.width = (nWidth - (findPosX(pre_nodes[i]) + nOffset));
						if(document.getElementById && !document.all)
						{
							pre_nodes[i].style.width = parseInt(pre_nodes[i].parentNode.style.width) - 50;
						}
					}
				}
			}
		}
		
// Events
		function eventSubscription(){};
		var g_Events = new Array();
		function subscribeToEvent(strEvent, fFunc)
		{
			var nIndex = 0; 
			if (g_Events.length > 0) nIndex = g_Events.length + 1;
			g_Events[nIndex] = new eventSubscription();
		 	g_Events[nIndex].myFunc = fFunc;
		  g_Events[nIndex].myEvent = strEvent;
			var tmpFunc = ""; var tmpFuncCall = "";
			var i;
			for(i = 0; i < (nIndex - 1); i++)
			{
				if(g_Events[nIndex].myEvent == strEvent && g_Events[i])
				{
					tmpFunc += g_Events[i].myFunc + "\n";
					tmpFuncCall += g_Events[i].myFunc.toString().match(/\w*\(\)/) + "; ";
				}
			}
			tmpFunc += fFunc;
			tmpFuncCall += fFunc.toString().match(/\w*\(\)/) + "; ";
			eval(g_Events[nIndex].myEvent + " = function(){" + tmpFunc + "\n" + tmpFuncCall + "}");
		}

		subscribeToEvent( "window.onresize", resizeOverFlows );
		subscribeToEvent( "window.onload", resizeOverFlows );

