//
// Copyright (c) 2002 European Truck Brokers
// www.etb.nl
//
// This file contains generic JavaScript functions 
//

// These three functions are from Macromedia Dreamweaver to ensure a fluent
// transition for the mouseover event
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



function showimage(image) {
	temp = document.plaat.src;
	document.plaat.src=document[image].src;
	document[image].src=temp;
	}

// Open seperate window showing the url with the supplied dimensions
// Example:
// <script language="JavaScript" src="./javascript/etb.js"></script>
// <script>opentip("image.php?image=bla", 600, 400)</script>
// Attn: make sure picture is the right dimension
//
function opentip(url,width,height) {
	tipWin = window.open(url,'tipWin','toolbar=no,width=' + width + ',height=' + height + ',directories=no,status=no,scrollbars=no,resizable=no,menubar=no');
	window.open(url,'tipWin');
	tipWin.opener = self;
}

function openfoto(str) {
	newWindow = window.open(str,'newWindow','width=560,height=364,scrollbars=no,resizable=no');
	newWindow.opener = self;
}

// 
// Select a stylesheet for the given type of browser.
// Example:
// <script language="JavaScript" src="./javascript/etb.js"></script>
// <script>chooseStyleSheet("./css/etb_ie.css", "./css/etb_ns.css","./css/etb_op.css")</script>
// @param url_css_ie The URL of the stylesheet for: Microsoft Internet Explorer
// @param url_css_ns The URL of the stylesheet for: Netscape Navigator
// @param url_css_op The URL of the stylesheet for: Opera
//
function chooseStyleSheet(url_css_ie, url_css_ns, url_css_op) {
	var n = navigator;
	// string comparisons are much easier if we lowercase everything now.
	// to make indexOf() tests more compact/readable, we prepend a space 
	// to the userAgent string (to get around '-1' indexOf() comparison)
	var ua = ' ' + n.userAgent.toLowerCase();
	var pl = n.platform.toLowerCase(); // not supported in NS3.0
	var an = n.appName.toLowerCase();
	
	// browser version
	var version = n.appVersion;
	
	var is_nn = ua.indexOf('mozilla') > 0;
	
	// 'compatible' versions of "mozilla" aren't navigator
	if(ua.indexOf('compatible') > 0) {
		is_nn = false;
	}
	
	var is_opera = ua.indexOf('opera') > 0;
	var is_ie = ua.indexOf('msie') > 0;
	
	var is_major = parseInt( version );
	var is_minor = parseFloat( version );
	
	// platform
	// mac
	var is_mac = ua.indexOf('mac') > 0;
	var is_mac68k = (ua.indexOf('68k') > 0 || ua.indexOf('68000') > 0);
	var is_macppc = (ua.indexOf('ppc') > 0 || ua.indexOf('powerpc') > 0);
	// windows
	var is_win = ua.indexOf('win') > 0;
	var is_win16 = (ua.indexOf('16') > 0 && ua.indexOf('win') > 0);
	var is_win31 = is_win16;
	var is_win95 = (ua.indexOf('95') > 0 && ua.indexOf('win') > 0);
	var is_win98 = (ua.indexOf('98') > 0 && ua.indexOf('win') > 0);
	var is_winme = (ua.indexOf('win 9x 4.90') >0 && ua.indexOf('win') > 0);
	var is_winnt = (ua.indexOf('nt') > 0 && ua.indexOf('win') > 0);
	var is_win2k = (ua.indexOf('nt 5') > 0 && ua.indexOf('win') > 0);
	
	// workarounds
	// - IE5/Mac reports itself as version 4.0
	if(is_ie && is_mac) {
		if(ua.indexOf("msie 5")) {
			is_major = 5;
			var actual_index = ua.indexOf("msie 5");
			var actual_major = ua.substring(actual_index + 5, actual_index + 8);
			is_minor = parseFloat(actual_major);
		}
	}
	
//	document.write("User Agent:   " + navigator.userAgent );
//	document.write("version:      " + version );
//	document.write("nn:           " + is_nn );
//	document.write("opera:        " + is_opera );
//	document.write("webtv:        " + is_webtv );
//	document.write("ie:           " + is_ie );
//	document.write("aol:          " + is_aol );
//	document.write("omniweb:      " + is_omniweb );
//	document.write("galeon:       " + is_galeon );
//	document.write("major:        " + is_major );
//	document.write("minor:        " + is_minor );
	
	if (is_nn == true) {
		document.write("<link rel='styleSheet' href='" + url_css_ns + "' type='text/css' media='screen'>");
	}
	else if((is_opera == true)) {
		document.write("<link rel='styleSheet' href='" + url_css_op + "' type='text/css' media='screen'>");
	}
	else {
		// Use default stylesheet 
		document.write("<link rel='styleSheet' href='" + url_css_ie + "' type='text/css' media='screen'>");
	}
}

//
// Check these functions for usability
//
// Shows a tip for a word or specific section of a webpage. The text of the tip is
// in a document that will be opened in a separate browser window.
// @param STRING url The url of the document
// @param STRING width The width of the window
// @param STRING height The height of the window
//
function showTip(url, width, height) {
    window.open(url,'','toolbar=no,width=' + width + ',height=' + height + ',directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no');
}


// Closes the current window.
// @author Martijn Baels
//
function closeWindow() {
    window.close();
}


// Copies the value from field 1 to field 2
// @param field1 The field to copy from
// @param field2 The field to copy to
// @author Martijn Baels
function copyFieldValue(field1, field2) {
    if (field1 != null && field2 != null) {
        field2.value = field1.value;
    }
}



//
// Browser Capability Check: Netscape Browser Versions and Cookie Check
// @author Rob Versloot
// @author Martijn Baels
//
function checkBrowserCap() {	
	//  Version test to detect Navigator 4.x
	if (navigator.appName == "Netscape" && navigator.appVersion.substring(0,1) == "4") {
		alert("Warning: Problems are reported when using our calculation examples with your version of the Netscape Navigator 4.x browser. You can browse our website, but you are not advised to explore our product examples. You might consider upgrading to Navigator 6.x or the other dominant browser.");
	}	
	// Check for cookies enabled	
	if (!navigator.cookieEnabled) {
		alert("Warning: Cookies are turned off. If you want to use our calculation examples, change your browser settings to accept cookies.");
	}
}


