//this is a snippet of code from webtrends v7.5 that will not currently pass
//through the templating language, so we circumvent by putting it in a file
//we know must always be included on every page
if (window.RegExp){
	var RE={"%09":/\t/g,"%20":/ /g,"%23":/\#/g,"%26":/\&/g,"%2B":/\+/g,"%3F":/\?/g,"%5C":/\\/g};
}

//Functions handling remote-component loading
function remoteload_handler(source, target) {
  var data = source.contentWindow.document;

  if (data.title == "success") {
    var replacement = data.body.innerHTML;
    if (document.all) {
      document.all[target].innerHTML = replacement;
    } else {
      document.getElementById(target).innerHTML = replacement;
    }
  }
}

// Just return the HTML data for IFRAME

function remoteloaddata(url)
{
  var uniq = 'remote' + (Math.floor(Math.random() * 1000000));
  var data = '<iframe class="remoteload" src="' + url + '" id="iframe' + uniq + '" ';
  data += 'onload="remoteload_handler(this, \'' + uniq + '\');" ';
  data += 'style="display: block; top: -100px; left: -100px; ';
  data += 'position: absolute; width: 1px; height: 1px;"></iframe>';
  data += '<div id="'+uniq+'"></div>';
  return data;
}


//Remote load iframe styles now in general.css
/*
function remoteloaddata(url) {
  var uniq = 'remote' + (Math.floor(Math.random() * 1000000));
  var data = '<iframe src="' + url + '" id="iframe' + uniq + '" ';
  data += 'onload="remoteload_handler(this, \'' + uniq + '\');" ';
  data += '></iframe>';
  data += '<div id="' + uniq + '"></div>';
  alert(data)
  document.write(data);
}
*/
function remoteload(url) {
	  document.write(remoteloaddata(url));
}

//Download functionality added to work around Windows XP SP2 popup changes
function doDownload(thankyou, article, file, url) {
  var agent = navigator.userAgent.toLowerCase();
  if ((file == article) || (file == "") || (file == null)) {
    openNewWindow(url,"_blank",0,600,400,"all");
  } else if ((document.all) && (agent.indexOf("opera") == -1)) {
    openNewWindow(url,"_blank",0,10,10,"none");
  }
  location = thankyou;
}

function doDownload2() {
  if ((download_file == null) ||
      (download_url == null) ||
      (download_article == null) ||
      (download_file == download_article) ||
      (download_file == "")) {
    return;
  }

  var agent = navigator.userAgent.toLowerCase();
	if ((document.all) && (agent.indexOf("opera") == -1)) return;
  location = download_url;
}

function getSkinDir() {
	var skindirs = [
		["crn.vnunet.com","crn"],
		["vnunet.com","vnunet"],
		["pcw.co.uk","pcw"],
		["computeractive.co.uk","computeractive"],
		["webactivemagazine.co.uk","computeractive"],
		["itweek.co.uk","itweek"],
		["networkitweek.co.uk","vnunet"],
		["computing.co.uk","computing"],
		["pcmag.co.uk","pcmag"],
		["infomaticsonline.co.uk","infomatics"],
		["whatpc.co.uk","whatpc"]
	];
	for (var i = 0; i < skindirs.length; i++) {
		if (location.href.indexOf(skindirs[i][0]) != -1)
			return skindirs[i][1];
	}
	return "vnunet";
}


var pocketUA = ["PDA","Windows CE","Palm","Pocket"];

if (!GetCookie("pocketrefused") && location.href.indexOf("vnunet.com") != -1) {
	for (var i = 0; i < pocketUA.length; i++) {
		if (navigator.userAgent.indexOf(pocketUA[i]) != -1) {
			if (confirm("You are about to be redirected to the Pocket edition of vnunet. Press \"Okay\" to confirm, or \"Cancel\" to remain here and browse the main site")) {
				location = "http://mobile.vnunet.com/";
				break;
			} else {
				SetCookie("pocketrefused","true");
				break;
			}
		}
	}
}

var IE4 = document.all;
var NN7 = (!IE4 && document.getElementById);
var IE5 = (IE4 && navigator.userAgent.indexOf("Mac") == -1 && (navigator.userAgent.indexOf("MSIE 5") != -1 || navigator.userAgent.indexOf("MSIE 4") != -1));
//if (!(NN7 || IE4) && location.href.indexOf("nonstandard.htm") == -1) location.replace("nonstandard.htm");
if (IE4 && navigator.userAgent.indexOf("Opera") != -1) {
	NN7 = true;
	IE4 = false;
}
var safari = (navigator.userAgent.indexOf("Safari") != -1);

function getStyleObject(obj) {
	var theObj = (IE4?document.all[obj]:document.getElementById(obj));
	return (theObj?theObj.style:null);
}

function getObject(obj) {
	return (IE4?document.all[obj]:document.getElementById(obj));
}

var dartNum = Math.round(Math.random()*10000000000);

function printAdvert(dartUrl) {
  var dartRef = document.referrer;
  var dartSpl = dartRef.substring(dartRef.indexOf(':') + 3).split("/");
  var dartArt = dartSpl[dartSpl.length - 1];
  if (!(isNaN(parseInt(dartArt)))) dartUrl += (";ref=" + dartArt);
  dartUrl += (";ord=" + dartNum + "?");
  document.write("<SCRIPT language=\"JavaScript\" src=\"" + dartUrl + "\"><\/SCRIPT>");
  //document.write("<DIV style=\"color: red;\">" + dartUrl + "<\/DIV>");
}

var listcheck = false;
function getWeblist() {
	listcheck = true;
	getStyleObject("weblist").display = (getStyleObject("weblist").display == "block"?"none":"block");
	getStyleObject("listarrow").visibility = (getStyleObject("listarrow").visibility == "visible"?"hidden":"visible");
}

function webLight(obj,x) {
	obj.backgroundColor = (x?"#003366":"#FFFFFF");
	obj.color = (x?"#DCE5EE":"#0066CC");
	if (IE5)
		obj.cursor = "hand";
}

function checkMenuStatus(e) {
	if (listcheck)
		listcheck = false;
	else {
		e = (e?e:window.event);
		var element = (IE4?e.srcElement:e.currentTarget);
		if (element.className != "weblink" && element.id != "weblist" && element.id != "network" && element.id != "dropmenu") {
			getStyleObject("weblist").display = "none";
			getStyleObject("listarrow").visibility = "hidden";
		}
	}
}

// FUNCTIONS FOR EVERY PAGE
var globalpath = (typeof(globalpath) == "undefined"?"http://www.vnunet.com":globalpath);
function goToPage(s) {
	if (s.options[s.selectedIndex].value) {
		top.location = (s.options[s.selectedIndex].value.indexOf("http://") == -1?globalpath:"") + s.options[s.selectedIndex].value;
	}
}

// IMPORTANT INFO FOR USE OF NEW WINDOW FUNCTION BELOW
// Administrator can specify "all" i.e. all normal buttons for window or "none" for no buttons etc or put them in individually. All attributes except numbers must be in single quotes. The order of arguments is important:
// 1) target page of window ("/trousered/ragged/philanthropist.htm")
// 2) name of window ("Daisy")
// 3) Number of times window should appear (integer - if zero, the window will appear every time)
// 4) width of window (integer)
// 5) height of window (integer)
// 6) Features switch ["all" (all normal features enabled)|"none" (no features enabled)|"resize" (no features but resizable)|"scroll" (no features but scrollbars enabled)|"yes|no" (toolbar - first attribut of list)]
// 7) location bar ("yes|no")
// 8) status bar ("yes|no")
// 9) menubar ("yes|no")
// 10) scrollbars ("yes|no")
// 11) resizable ("yes|no")
// 12) directories ("yes|no")
// 13) top (integer) [optional - can also be included with "all|none" in attribute 6, so this would be 7]
// 14) left (integer) [optional - can also be included with "all|none" in attribute 6, so this would be 8]

// USAGE EG. <A HREF="javascript:openNewWindow('homepage.htm','ben',3,200,400,'none',100,400)">window 1</A><BR><BR>

function openNewWindow() {
	if (arguments[2]) {
		if ((GetCookie(arguments[1]) && parseInt(GetCookie(arguments[1])) >= arguments[2]) || (IE4 && !navigator.cookieEnabled))
			return;
		else {
			var expiry = new Date();
			expiry.setTime(expiry.getTime() + 31536000000);
			if (GetCookie(arguments[1]))
				SetCookie(arguments[1],(parseInt(GetCookie(arguments[1])) + 1),expiry,"/");
			else
				SetCookie(arguments[1],"1",expiry,"/");
		}
	}
	var str = "width=" + arguments[3] + ",height=" + arguments[4]
	if (arguments.length < 12) {
		if (arguments[5] == "none")
			str += ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no";
		else if (arguments[5] == "resize")
			str += ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes";
		else if (arguments[5] == "scroll")
			str += ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no";
		else
			str += ",toolbar=yes,location=yes,directories=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes";
		if (arguments.length == 8)
			str += ",top=" + arguments[6] + ",left=" + arguments[7];

	} else {
		str += ",toolbar=" + arguments[6] + ",location=" + arguments[7] + ",directories=" + arguments[8] + ",status=" + arguments[9] + ",menubar=" + arguments[10] + ",scrollbars=" + arguments[11] + ",resizable=" + arguments[12];
		if (arguments.length == 14)
			str += ",top=" + arguments[12] + ",left=" + arguments[13];
	}
	window.open(arguments[0],arguments[1],str);
}

function openScreenShot(f,w,h) {
  openNewWindow("http://images.vnunet.com/v7_static/popups/" + f,"screenshot",0,w,h,'none',100,100);
}

function popUnder(cookiename,url,n) {
	var expiry = new Date();
	expiry.setTime(expiry.getTime() + 604800000);
	if (cookiename && !GetCookie(cookiename) && navigator.cookieEnabled) {
		SetCookie(cookiename,"true",expiry,"/");
		openNewWindow(url,cookiename,n,700,600,"all",100,150);
		window.focus();
	}
}

//** COOKIE FUNCTIONS ** //

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
	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "")
}

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
}

//**  END OF COOKIE FUNCTIONS **//

function printThis() {
      if (typeof print == "object" || typeof print ==
"function")
            print();
}

function reSortSearch() {
	document.searchsortby.submit();
}

var site = "";
var jsite = "";
if (location.port == "8888" || location.href.indexOf("core.vnunet.com") != -1) {
	jsite = "http://www2." + location.hostname.substring(5);
	site = location.pathname.substring(1);
	site = "/" + site.substring(0,site.indexOf("/"));
}

function searchCheck(f) {
	var e = f.section;
	if (e.options[e.selectedIndex].value == "jobs") f.action = jsite + "/JobSearch";
	//if (e.options[e.selectedIndex].value == "fora") f.action = "http://forums.vnunet.com/search.jsp?search=true&forum=-1";
	if (e.options[e.selectedIndex].value == "whit") f.action = "http://vnu.bitpipe.com/data/search?site=vnu&qp=site_abbrev%3Avnu&cp=bpres&cr=bpres&qg=VENDOR&st=1";
	return true;
}

//above added so can be removed from v7 headers

//var brandnav = "news";

function pageInit() {
	if (getObject("weblist")) {
		if (NN7 && document.addEventListener)
			document.addEventListener("click", checkMenuStatus, false);
		else if (IE4)
			document.onclick = checkMenuStatus;
	}
//	if (typeof brandnav != "undefined") {
//		getStyleObject("brand" + brandnav).backgroundImage = "url(images/v7_vnunet_stripebg.gif)";
//		getStyleObject("brand" + brandnav).borderBottom = "none";
//		getStyleObject("brand" + brandnav + "2").backgroundColor = "#97B9DA";
//		}
//	if (location.href.indexOf("vnunet.com") != -1 || (location.port && location.port == "8888"))
//		popUnder("popunder1","http://www.vnunet.com/specials/1155459",1);
}

if (!window.onload)
	onload = pageInit;

// FUNCTION FOR ACCOUNTANCYA AGE DUN AND BRADSTREET VIEWER
function dandbViewer(nurl) {
	var wins = window.open(nurl,"dandb","resizable,scrollbars,width=620,height=450")
	}

// FUNCTION FOR ACCOUNTANCY AGE COLIN VIEWER
function colinViewer(nurl) {
	var wins = window.open(nurl,"colin","width=830,height=310,resizable=no,location=no,status=no,scrollbars=no,toolbar=no")
	}
	
// FUNCTION FOR COMPUTING CAREERS SEARCH
function checkRadius(){
  var f=document.advJobsearchForm;
  if (f.qMiles.selectedIndex == 0) {
    return true;
  } else {
    if (f.qCity.value=='') {
      alert("City/Zip is a required field when performing a radius search.");
      return false;
    }
  }
  return true;
}

// QUICK LINKS TO AN ARTICLE
function quickLink(form, host) {
    try {
      var id = parseInt(form.article.value);
      if (isNaN(id)) throw "Not A Number";
      if (id < 0) throw "Not an article ID";
      if (host == null) host = 'www.vnunet.com';
      var uri = 'http://' + host + (id < 2000000 ? '/news/' + id : '/' + id);
      window.location = uri;
    } catch (error) {
      alert("Sorry, the value provided doesn't appear to be correct: " + error);
    }
    return false;
}

// FUNCTION FOR AA/FD CURRENCY CONVERTER
function popup(url,winname,w,h,feat)
{
	if (!(isNaN(w) || isNaN(h)))
	{
		var adjust = 20;
		var x=parseInt((screen.width-w)/2);
		var y=parseInt(((screen.height-h)/2-adjust));
		if (x<0)	x=0;
		if (y<0)	y=0;
		
		if (feat!=null && feat!="")
		{
			feat=","+feat;
		}
		else
		{
			feat="";
		}
		feat="left="+x+",top="+y+",width="+w+",height="+h+feat;
	}
	var this_win = window.open(url,winname,feat);
	this_win.focus();
}

// GENERIC TEST STRING IS BLANK FUNCTION
function isEmpty(field) {
  var re = /^\s{1,}$/g;
  if ((field.length == 0) || (field == null) || ((field.search(re)) > -1)) {
    return true;
  }
  else {
    return false;
  }
}

function newLocationFunction() 
{ window.location = "voucher"; }

function newLocationFunction2() 
{ window.location = "register"; }

function bestpricesarticlelink(site) {

  var partner = 'vnunet';
  if(site == 'vnu') partner = 'vnunet';
  if(site == 'ca' || site == 'wa' || site == 'ah') partner = 'computeractive';
  if(site == 'wpc') partner = 'whatpc';
  if(site == 'pcm') partner = 'pcmag';
  if(site == 'pcw') partner = 'pcw'; 

  var divtags = document.getElementsByTagName("DIV")
  for(var i=0;i<divtags.length;i++) {
      var divtag = divtags[i];
      if(divtag.className == 'content') {
         var atags = divtag.getElementsByTagName("A")
         for(var j=0;j<atags.length;j++) {
             var atag = atags[j];
             if(atag.href.indexOf('kelkoo')!=-1) {
                if(atag.href.indexOf('?')!=-1) {
                   atag.href += '&partner='+partner;                
                } else {
                   atag.href += '?partner='+partner;
                }
             }
         }
      }
  }
}

function randomRemoteload(remoteloadUrl)
{
	var string = remoteloadUrl + (Math.floor(Math.random() * 1000000));
	document.write('<script>');
	document.write('remoteload("' + string + '")');
	document.write('</script>');
}

//function to check if current article category is part of intelliTxt agreement, and if so output javascript with corresponding id
function setUpIntelliTxt(categories)
{
	var intelliTxtCategories = new Array();
	intelliTxtCategories['software']='9803';
	intelliTxtCategories['memory']='10207';
	intelliTxtCategories['microprocessors']='10208';
	intelliTxtCategories['mobile']='10272';
	intelliTxtCategories['graphics']='10273';
	intelliTxtCategories['servers']='10274';
	intelliTxtCategories['emission-goals']='10275';
	intelliTxtCategories['storage']='10276';
	intelliTxtCategories['security']='10277';
	intelliTxtCategories['peripherals']='10278';
	intelliTxtCategories['desktops']='10279';
	intelliTxtCategories['notebooks']='10280';
	intelliTxtCategories['broadband']='10281';
	//alert(categories[0]);
	 for ( testKey in intelliTxtCategories ) {
	 		if (categories.search(testKey) != -1 ) {
	 			//alert("we have a match on " + testKey + intelliTxtCategories[testKey]);
	 			//return intelliTxtCategories[testKey];
	 			document.write('\n<!-- start Vibrant Media IntelliTXT script section -->\n');
	 			document.write('<script type="text/javascript" src="http://theinquirer.uk.intellitxt.com/intellitxt/front.asp?ipid=' + intelliTxtCategories[testKey] + '"></script>');
	 			document.write('\n<!-- end Vibrant Media IntelliTXT script section -->\n');
	 			break;
	 		}
	 }
}


//function to extract url variables after PHP form action
//developed primarily for inquirer newsletter subscription /en/GB/inq/subscribe/
function getURLVariables(variable) {
	var urlVars = window.location.search;
	var value;
	if (urlVars != '' && urlVars.search(variable)) {
		var keyValuePair = urlVars.substring(urlVars.indexOf(variable));
		
		if (keyValuePair.lastIndexOf('&') == -1) {
			//& not in string so its the last key value pair in the url
			value = keyValuePair.substring(keyValuePair.indexOf('=') + 1);
		} else {
			//& is present and so theres other key value pairs after it
			value = keyValuePair.substring(keyValuePair.indexOf('=') + 1, keyValuePair.lastIndexOf('&'));
		}
	} 
	return value;
}

//display user feedback depending on the value input. MessageHolderId is the id of the tag to contain the message
function outputMessage(returnResult, messageHolderId) {
	
	var message = document.getElementById(messageHolderId);
		
	switch (returnResult) {
		case 'join-success' : 
			message.innerHTML = 'Welcome to the The Inquirer!! You have successfully subscribed to The Inquirer newsletter.'
		break
		case 'subscribe-success' :
			message.innerHTML = 'You have successfully subscribed to The Inquirer newsletter.'
		break
		case 'unsubscribe-success' :
			message.innerHTML = 'You have successfully unsubscribed from The Inquirer newsletter.'
		break
		case 'join-failure' : case 'subscribe-failure' : case 'unsubscribe-failure' : case 'what-you-talking-about-willis' :
			message.innerHTML = 'There has been an error with your subscription, please try again later.'
		break
		default :
			message.innerHTML = 'Go on! You know you want to!'
		break
	}	
}

function addCssClass(className, elementId) {
	var myElement = document.getElementById(elementId);
	myElement.setAttribute("class", className);			//for browsers that follow specs
	myElement.setAttribute("className", className);	//for IE
	return false;
}