var isFlashNS = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)?true:false;
var isMoz2 = (navigator.userAgent.indexOf("Mozilla/2") != -1)?true:false;
var isIE = (navigator.appName == "Microsoft Internet Explorer")?true:false;
var isGecko = (navigator.userAgent.indexOf("Gecko") != -1)?true:false;
var isNetscape6 = (isGecko && parseInt(navigator.appVersion) >= 5)?true:false;
var isDOM = (document.getElementById) ? true : false;
var isIE4 = (navigator.userAgent.indexOf("MSIE 4.0") != -1)?true:false;
var isIE45 = (navigator.userAgent.indexOf("MSIE 4.5") != -1)?true:false;
var isIE5 = (navigator.userAgent.indexOf("MSIE 5.0") != -1)?true:false;
var isIE6 = (navigator.userAgent.indexOf("MSIE 6.0") != -1)?true:false;
var isMac = (navigator.userAgent.indexOf("Mac") != -1)?true:false;
var isMacIE4 = (isMac && isIE4) ? true : false;
var isMacIE45 = (isMac && isIE45) ? true : false;
var isMacIE5 = (isMac && isIE5) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1)?true:false;
var isOpera3 = (navigator.userAgent.indexOf("Opera 3.")!= -1)?true:false;
var isOpera4 = (navigator.userAgent.indexOf("Opera 4.")!= -1)?true:false;
var isNetscape = (!isOpera && (navigator.appName.indexOf("Netscape") != -1 && navigator.userAgent.indexOf("4.") != -1))?true:false;
var isNS3 = (!isOpera && (navigator.userAgent.indexOf("Netscape") != -1 && navigator.userAgent.indexOf("3.") != -1)) ? true : false;
var isNSDIV = (!isOpera && (isNetscape && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("4.")+2)) > 5)) ? true : false;
var isWin2k = (navigator.userAgent.indexOf("Windows NT") != -1) ? true : false;

var image_cache_arr = new Array();
// generic_image_caching
image_cache_arr[0] = new Array("/library/images/","-over","gif","btn-articles","btn-home","btn-myarticles","btn-myblog","btn-mymac","btn-myphotos","btn-photogallery","btn-profile");
ImageCache(0);
// /generic_image_caching

function getFileExtension(fileSource) {
	var newSource = '';
	if (fileSource.lastIndexOf(".") != -1) {
		newSource = fileSource.substring(fileSource.lastIndexOf("."));
	}
	return newSource;
}

// Mouse Rollover function
function MouseOver(image) {
	if (image.src.indexOf("-over") == -1) {
		var imageExt = getFileExtension(image.src);
		image.src = image.src.substring(0,image.src.indexOf(imageExt)) + "-over"+imageExt;
	}
}

// Mouse Rollout function
function MouseOut(image) {
	if (image.src.indexOf("-over") >=0) {
		var imageExt = getFileExtension(image.src);
		image.src = image.src.substring(0,image.src.indexOf("-over"+imageExt)) + imageExt;
	}
}

// Returns a nested Layer or a document object
function getNestedLayer(nest1, nest2) {
	return (document.layers) ? (nest1) ? (nest2) ? document.layers[nest1].layers[nest2].document : document.layers[nest1].document : document : document;
}

// function to cache images on a web page
// To use the Image Caching function, use the following syntax:
//	image_cache_arr[num] = new Array(imagepath, state "-over" etc... , extension, images);
// Ex:	image_cache_arr[0] = new Array("/services/images", "yes", "gif", "international", "additional");
//
// Once you have set-up the array, you can call ImageCache(); to cache the images.
//
var showImagesCached = false;
var imageCache = "";
function ImageCache() {
	if(document.images) {
		for(i=0; i<image_cache_arr.length; i++) {
			var imagepath = image_cache_arr[i][0];
			var imageExt = image_cache_arr[i][2];
			var mouseoverimages = new Array(image_cache_arr[i].length-1);
			var numofoverstates=(ImageCache.arguments[i]) ? ImageCache.arguments[i] : 0;
			var overstate = new Array();
			for(os=1; os<numofoverstates+1; os++) {
				overstate[os] = image_cache_arr[i][os];
			}
			var ext = image_cache_arr[i][numofoverstates+1] + "." + imageExt;

			for(j=(numofoverstates+3); j<image_cache_arr[i].length; j++) {
				if(image_cache_arr[i][j].indexOf('"') == -1 && image_cache_arr[i][j].indexOf(',') == -1) {
					mouseoverimages[image_cache_arr[i][j]] = new Image();
					if(isNSDIV || isIE || isNetscape6) {
						mouseoverimages[image_cache_arr[i][j]].name = image_cache_arr[i][j];
					}
					mouseoverimages[image_cache_arr[i][j]].src = imagepath + image_cache_arr[i][j] + ext;
					imageCache+=mouseoverimages[image_cache_arr[i][j]].src+"\n";
					for(os=1; os<numofoverstates+1; os++) {
						mouseoverimages[image_cache_arr[i][j]].src = imagepath + image_cache_arr[i][j] + overstate[os] + ext;
						imageCache+=mouseoverimages[image_cache_arr[i][j]].src+"\n";
					}
				}
			}
		}
	}
	if (showImagesCached == true) { alert(imageCache); }
}

// Left nav image preload

navarrow2Over = new Image();
navarrow2Over.src = "/library/images/icn-navarrow2-over.gif"

navarrow2 = new Image();
navarrow2.src = "/library/images/icn-navarrow2.gif"

// Mouse Rollover function
function MouseOverLi(image, nest, nest2) {
	
	if (nest == null) {
		image.style.backgroundImage = 'url(' + navarrow2Over.src + ')';
	} else if (nest == 'rhs') {
		image.style.backgroundImage = 'url(/library/images/icn-arrowboxbblue-over.gif)';
	} else if (nest == 'rhsS') {
		image.style.backgroundImage = 'url(/library/images/icn-arrowboxwblue-over.gif)';
		image.style.backgroundColor = '#ffffff';
	} else if (nest == 'rhsD') {
		image.style.backgroundImage = 'url(/library/images/icn-arrowboxbblue-over.gif)';
	} else if (nest == 'pn') {
		image.style.background = '#FFCE00 url(/library/images/img-pnav-folder3-over.gif) no-repeat 0% 100%';
	} else {
		image.style.backgroundColor = '#FFE401';
	}
}
// Mouse Rollout function
function MouseOutLi(image, nest, nest2) {
	if (nest == null) {
		image.style.backgroundImage = 'url(' + navarrow2.src + ')';
	} else if (nest == 'rhs') {
		image.style.backgroundImage = 'url(/library/images/icn-arrowboxbblue.gif)';
	} else if (nest == 'rhsS') {
		image.style.backgroundImage = 'url(/library/images/clear.gif)';
		image.style.backgroundColor = '#CCD6EB';
	} else if (nest == 'rhsD') {
		image.style.backgroundImage = 'url(/library/images/icn-arrowboxbblue.gif)';
	} else if (nest == 'pn') {
		image.style.background = '#FFE401 url(/library/images/img-pnav-folder3.gif) no-repeat 0% 100%';
	} else {
		image.style.backgroundColor = '';
	}
}

// the following fixed an error in IE4 which causes backgrounds to shift left by 15 pixels
var ieVer=((navigator.userAgent.indexOf ( "MSIE " )+1)&&(document.all))?4:0;
if ((ieVer) && (window.clipboardData)) ieVer++;
if (ieVer) { 
	ieVer = String(navigator.userAgent).substr(String(navigator.userAgent).indexOf('MSIE')+5,1);
}
if (ieVer == 4) { // returns 0 for IE 3x-, 4 for IE4x, 5 for IE5+
	document.write('<style type="text/css" media="all">');
	document.write('	UL LI,UL.links LI							{ background-position: 15px 2px; }');
	document.write('	UL LI.pdf									{ background-position: 15px 0px; }');
	document.write('	UL LI.arrow									{ background-position: 16px 0px; }');
	document.write('	.footerBox									{ width: 96%; padding-right: 18px; }');
	document.write('	.headerApply,.footerApply 					{ width:95% }');
	document.write('	.formBox			 						{ width:95%; }');
	document.write('	.nav LI, .navSub LI 						{ background-position: 15px 0px; }');
	document.write('	.navSub LI									{ background-position: 26px 1px; }');
	document.write('	.navRHS LI,.navRHSsub LI,.navRHS .selected	{ background-position:20px 6px;border-bottom:2px solid #ffffff;text-align:left; }');
	document.write('	.navRHSsub LI,.navRHSsub LI.selected		{ border-bottom:0px; background-position: 29px 4px; }');
	document.write('	.header										{ display:block; }');
	document.write('	.pageNav TABLE DIV							{ background-position: 0% 2px; }');
//	document.write('	DIV.contentPad								{ padding: 11px 0px 11px 0px; margin: 0px 11px 0px 11px; /*fix for IE/MOZ*/ text-align: left;}');
	/* The following code is specifically to clean up the navcontainer for IE4 so it looks like a normal linked list and not a mish-mash */
	document.write('	#navcontainer								{ background: none; }');
	document.write('	#navcontainer UL							{ background: none; }');
	document.write('	#navcontainer LI							{ padding-left:8px; }');
	document.write('	#navcontainer LI SPAN						{ background: none; }');
	document.write('	#navcontainer LI A							{ background: none; text-decoration: underline; }');
	document.write('	#navcontainer LI A:hover					{ background: none; text-decoration: none; }');
	document.write('	#navcontainer LI.selected					{ font-weight: bold; }');
	document.write('	#navcontainer LI.selected A					{ background: none; }');
	document.write('	#navcontainer LI.selected A:hover			{ background: none; }');
	document.write('	TD,TH										{ font-size: 71%!important; }');
	document.write('</style>');
}
// the following fixes an issue with NS6 and inheritance of font size in data tables
if (navigator.userAgent.indexOf("Netscape6/6") != -1) {
	document.write('<style type="text/css" media="all">');
	document.write('	TD,TH										{ font: 71%/120% arial; }');
	document.write('</style>');
}


function validateMe() {
	var valWin = window.open('http://validator.w3.org/check?uri='+escape(document.location.href)+'&outline=1','_val','left=25,top=25,width=800,height=600,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
}


// **********************************************************************************
// querystring.js
// Javascript functions to manipulate the documents querystring
// **********************************************************************************


// **********************************************************************************
// Get the value of a specified query string item
// **********************************************************************************
function RequestQueryString( strName ) {
	var strQuery = document.location.href;
	var strResult = "" ;

	var intPos = strQuery.indexOf( strName );

	if (intPos > 0) {
		intPos += strName.length + 1 ;
		strResult = intPos ;

		var intEnd = strQuery.indexOf( "&", intPos )

		// value could be at the end of the QueryString
		if (intEnd <= 0) intEnd = strQuery.length ;

		strResult = strQuery.substring( intPos, intEnd ) ;
	}

	return URLDecode(strResult) ;
}
// **********************************************************************************
function URLDecode( strQuery ) {
	var intPos = 0 ;
	var strResult1 = "" ;
	var strResult2 = "" ;
	var intOldPos

	// replace + (spaces)
	intPos = strQuery.indexOf( "+" ) ;

	if (intPos == -1) strResult1 = strQuery ;
	else strResult1 = strQuery.substring(0, intPos) ;

	while (intPos >= 0) {
		strResult1 += " " ;

		intOldPos = intPos + 1 ;

		// get next + (space)
		intPos = strQuery.indexOf( "+", intOldPos ) ;

		if (intPos == -1) strResult1 += strQuery.substring( intOldPos, strQuery.length ) ;
		else strResult1 += strQuery.substring( intOldPos, intPos ) ;
	}

	// replace character codes
	intPos = strResult1.indexOf( "%" ) ;

	if (intPos == -1) strResult2 = strResult1 ;
	else strResult2 = strResult1.substring(0, intPos) ;

	while (intPos >= 0) {
		strResult2 += unescape(strResult1.substring( intPos, intPos + 3 )) ;

		intOldPos = intPos + 3 ;

		// get next character code
		intPos = strResult1.indexOf( "%", intOldPos ) ;

		if (intPos == -1) strResult2 += strResult1.substring( intOldPos, strResult1.length ) ;
		else strResult2 += strResult1.substring( intOldPos, intPos ) ;
	}

	return strResult2 ;
}

// *********************************************************************************
// *********************************************************************************
// ********** Questionnaire
// *********************************************************************************
// *********************************************************************************

//window.onload = begin;
//window.onunload = exitPopup;

	  
var strExitPopup = true;


function exitPopup(){

//	if( strExitPopup && Math.round(Math.random()*10)==1){
	if( strExitPopup && getCookie( 'USABILITY' )==1  ){
//		alert("open popup window");
		window.open('http://www.norwichunion.com/usability-questionnaire/index.cgi','_val','left=25,top=25,width=800,height=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
		setCookieWithExpiry( 'USABILITY', 0 ,90 );
	}
}

function setLocal() {
	strExitPopup=false;
}

function begin() {
	
	// If the user dosnt have a cookie then write one
	if( getCookie( 'USABILITY' )==null ) {
		setCookieWithExpiry( 'USABILITY', Math.round(Math.random()*10) ,90 );
	}

	// Change all links
	var strLink = '';
	for(var i=0; i<document.links.length; i++) {
		strLink = (''+document.links[i]);
		strLink = strLink.substr(0,strLink.indexOf(".co",7));
		
		strHost = (''+document.location);
		strHost = strHost.substr(0,strHost.indexOf(".co",7));
		
		if( ( strLink == strHost ) && ( document.links.onclick == null ) ){
			document.links[i].onclick = setLocal;
		}
	}
	
}

// **********************************************************************************
// cookie.js
// Javascript functions for cookie manipulation
// **********************************************************************************


// **********************************************************************************
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 writeCookie( name, value, expiry ) {
	var expDate = new Date();

	if(expiry) {
		expDate.setTime (expDate.getTime() + expiry);
		document.cookie = name + "=" + escape (value) + "; path=/; expires=" + expDate.toGMTString();
	}
	else {
		document.cookie = name + "=" + escape (value) + "; path=/;";
	}
}
// **********************************************************************************
function setCookie (name, value) {
	// num is the number of days until cookie expires
	var num = 7;

	if(num == 0)
		writeCookie(name, value, 0);
	else
		writeCookie(name, value, 1000 * 60 * 60 * 24 * num);
}
// **********************************************************************************
function setCookieWithExpiry( name, value, expiry ) {
	var days = 0;

	if( expiry==-1 ) {
		// default
		days = 1000 * 60 * 60 * 24 * 7;
	} else {
		if( expiry>0 ) {
			days = (((1000 * 60) * 60) * 24) * expiry;
		}
	}
	writeCookie( name, value, days );
}
// **********************************************************************************
function deleteCookie( name ) {  
	var exp = new Date();  
	var cval = getCookie (name);  

	exp.setTime (exp.getTime() - 1);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
// **********************************************************************************

            // Global variable which shows if quote has been requested
            // from this page within this page's session.
            
            var g_QuoteClicked = false;
            
            // This will fire when the quote window is closed.
            
//            function asyncReceive() {
//                if( g_QuoteClicked ) {
//					g_QuoteClicked = false;
//                    popUp( "/nud/essential/index.htm" );
//                }
//            }
            
            function popUp( url ) {
                day = new Date();
                id = day.getTime();
                eval("page" + id + " = window.open( url, '" + id + "', 'toolbar=0,scrollbars=yes,location=0,statusbar=yes,menubar=0,resizable=yes,width=590,height=440');");
            }
            
            function openPopupMax( url ) {
                // set flag to say a quote has been requested
                g_QuoteClicked = true;
				
				//Opens Window to full screen
				//Note, checks for Car insurance links and displays toolbar
				
				var carCheck = url.indexOf("/car-insurance/filter.htm")
				
				if(carCheck != -1){
					objWindow = window.open('' ,'Quote','toolbar=yes,scrollbars=yes,status=yes,resizable=yes');
				}
				else{objWindow = window.open('' ,'Quote',',scrollbars=yes,status=yes,resizable=yes');}
				
				objWindow.location = url;
				if( objWindow ) {
					objWindow.moveTo(0,0);
					objWindow.resizeTo(screen.availWidth,screen.availHeight);
				}
				return false;
            }

/* Used for JS Pulldown for quotes on Motor-Health hubs */

function redirects(){
		var frm = document.quoteRedirects;
		var url = frm.redirect.value;
		//openPopWin(url, 640, 450, 'scrollbars=yes', 50, 50);
		openPopupMax(url);
}

function redirectstwo(){
		var frm = document.quoteRedirects;
		var url = frm.redirect.value;
		openPopWin(url, 770, 550, 'scrollbars=yes', 50, 50);
}