//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;
}

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,1000,400,"resize");
  }
  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("www.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 - 2];
  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=yes,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 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;
}

function javascriptRedirect(){
          var queryString = window.top.location.search.substring(1);
		  if( queryString.length >0 ){
		     var path = queryString.substring(4,queryString.length);
		     document.location.href = path;
		  }else{
		     document.location.href = history.back(-2);
		  }
}

// Return the specified request parameter value...
function getRequestParam(paramName){
          var queryString = window.top.location.search.substring(1);
		  if( queryString.length > 0 ){
		     var prms = queryString.split("&");
		     for( i=0; i<prms.length; i++ ){
		         var name  = prms[i].substring(0,prms[i].indexOf("="));
		         var value = prms[i].substring( prms[i].indexOf("=")+1, prms[i].length);
		         if(paramName == name){
		            return value;
		         }
		     }
		     return null;
		  }else{
		     return null;
		  }
}

function SetSearchSection(strSectionFormValue,strSearchDisplayText,formId){
//To get the correct parameter to pass into search 
//see article xml file for this category and look at @section 
//strDefaultSection should equal one of these values:
//news, reviews, forums, jobs, bestprices, whitepapers, softwareshop, video

	var formId = 'vnunetSearch';
	if (typeof(formId)!="undefined")
	{
		formId = formId;
	}
	
    var strSearchSections = ",downloads,news,reviews,forums,jobs,bestprices,whitepapers,softwareshop,video,";
    
    if(strSearchSections.indexOf(","+strSectionFormValue+",")!=-1){
      var strDefaultText = "Search vnunet.com";
      //set hidden value for server search 
      document.getElementById(formId).section.value = strSectionFormValue;
      
      //in download section sort by relevance (not date)
      if( strSectionFormValue == 'downloads' ){
    	  document.getElementById(formId).sort.value = "relevance";
      }

      //reset header.xhtml vnuSearchDefaultText to detect when a user clicks search without entering any search text.
      vnuSearchDefaultText = strDefaultText + ' > ' + strSearchDisplayText;

     //change text to show that the search is only searching in this section.
      var objSearchBox = document.getElementById('vnuSearchInput'); 
      objSearchBox.value = vnuSearchDefaultText;
       
    }else{
      alert("'" + strSectionFormValue + "' is not a recognised form section value.");
    }
}