//
// Platform and browser detect variables
//

var isUnix = false;
var isLinux = false;
var isMacintoshPPC = false;
var isMacintosh68K = false;
var isWindows95 = false;
var isWindows98 = false;
var isWindowsNT= false;
var isWindows3x = false;

var isNew = false;
var isIE = false;
var isIE4 = false;
var isIE5 = false;
var isNetscape = false;
var isNetscape4 = false;
var isNetscape6 = false;

var userAgent = navigator.userAgent.toLowerCase();
var appName = navigator.appName;
var appVersion = parseInt(navigator.appVersion);
var platform = (navigator.platform).substring (0, 5);

// Browser detection
if (appVersion > 5) isNew = true;
else if (appName == "Netscape") {
  isNetscape = true;
  if (appVersion == 4) isNetscape4 = true;
  else if (appVersion == 5) isNetscape6 = true;
}
else if (appName == "Microsoft Internet Explorer") {
  isIE = true;
  if (appVersion == 4) {
    isIE4 = true;
    if (msieversion() == 5) isIE5 = true;
  }
}

function msieversion() {
  var ua = window.navigator.userAgent
  var msie = ua.indexOf ( "MSIE " )
  if ( msie > 0 ) return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
  else return 0;
}

// Platform detection
if ( ( userAgent.indexOf("win95") !=-1) || ( userAgent.indexOf("windows 95") !=-1) ) isWindows95 = true;
if ( ( userAgent.indexOf("win98") !=-1) || ( userAgent.indexOf("windows 98") !=-1) ) isWIndows98 = true;
if ( ( userAgent.indexOf("winnt") !=-1) || ( userAgent.indexOf("windows nt") !=-1) ) isWindowsNT= true;
if ( ( userAgent.indexOf("win16") !=-1) || ( userAgent.indexOf("windows 3.1") !=-1) ) isWindows3x = true;
if ( navigator.appVersion.indexOf("Mac") != -1 ) {
  if ( userAgent.indexOf("pc)") !=-1 ) isMacintoshPPC = true;
  else isMacintosh68K = true;
}
if ( platform == "SunOS" ) isUnix = true;
else if ( platform == "HP-UX" ) isUnix = true;
else if ( platform == "IRIX " ) isUnix = true;
else if ( ( ( navigator.platform ).substring (0, 3) ) == "AIX" ) isUnix = true;
else if ( userAgent.indexOf("linux") != -1 ) isLinux = true;

// Added by Chris to support existing hsbc functions

if(isIE || isIE4 || isIE5) ie=true;
else ie=false;
if(isMacintoshPPC || isMacintosh68K) mac=true;
else mac=false;
if(isWindowsNT) nt=true;
else nt=false;
if(isUnix || isLinux) unix=true;
else unix=false;
if(navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.indexOf("5.5") != -1) ie5_5=true;
else ie5_5=false;

//
// Generic functions
//

//This preloads the images. See the body tag for the syntax to do this for an image
function imageLoad() {
        if (document.images) {
                var imgFileArg = imageLoad.arguments;
                imgFileStr = imgFileArg[0];
                var imgFiles = imageLoad.arguments;
                if (imgFileStr != null && imgFileStr.indexOf("'") > -1 ) {
                        imgFileStr = imgFileStr.substring(1,imgFileStr.length-1);
                        imgFiles = imgFileStr.split("','");
                }
                if (document.preloadArray==null) document.preloadArray = new Array();
                var i = document.preloadArray.length;
                with (document) for (var j=0; j<imgFiles.length; j++){
                        preloadArray[i] = new Image;
                        preloadArray[i++].src = imgFiles[j];
                } 
        }
}

function swapImage(layerStem,imageSpace,imageName) {
  var ns4 = (document.layers)? true:false;
  var ie4 = (document.all)? true:false;
	if(!ie){	if(layerStem == ""){imageObj = document.images[imageSpace];}
				else
			{imageObj = eval(layerStem+".document.images[imageSpace]");}
		}
	if(ie){imageObj = document.all[imageSpace]}

        imageObj.src = imageName;

}

// Used for selecting window size
function openWindow(winUrl, winName, properties) {

switch (properties) {

    case "win_a":
        newWindow = window.open(winUrl,winName,"scrollbars,menubar,toolbar,resizable,HEIGHT=460,WIDTH=790");
        newWindow.focus();
	break

    case "win_b":
	  	var enhancePopup = true;	
		var checkPath1 = winUrl.indexOf("www.login.hsbc.com");		// hard coded path 1
		var checkPath2 = winUrl.indexOf("www.register.hsbc.com");	// hard coded path 2

		if(checkPath1 > -1 || checkPath2 > -1) // special handling for the URLs
		{
			var appName = navigator.appName.toLowerCase();
			var appVersion = parseFloat(navigator.appVersion);

			if(appName.indexOf("netscape") > -1 && !isNaN(appVersion) && appVersion >= 5 ) // netscape 6.0+
			{
				var appProduct = navigator.product.toLowerCase();
				if(appProduct.indexOf("gecko") > -1) // Gecko is being used
				{
					if(navigator.vendorSub != "6.2") // skip Netscape 6.2
					{
						if(!isNaN(parseFloat(navigator.vendorSub)) && parseFloat(navigator.vendorSub) >= 6.2) // Should be Netscape 6.21+
						{
							enhancePopup = false;
						}				
					}
				}
				else // Gecko is not being used
				{
					var ua = navigator.userAgent.toLowerCase();
					var ns6_index = ua.lastIndexOf("netscape6");
					var versionNum = 0;
				
					if(ns6_index > -1)  // Netscape 6
					{
						if(ua.substring(ns6_index+10, ua.length) != "6.2")
						{
							versionNum = parseFloat(ua.substring(ns6_index+10, ua.length));
						}
					}	
					else
					{
						var ns_index = ua.lastIndexOf("netscape");
						if(ns_index > -1)  // Netscape 7 or +
						{
							versionNum = parseFloat(ua.substring(ns_index+9, ua.length));
						}
					}
					if(!isNaN(versionNum) && versionNum >= 6.2)
					{
						enhancePopup = false;
					}
				}		
			}
		}
		if(enhancePopup) // usual case
		{
			newWindow = window.open(winUrl,winName,"scrollbars,HEIGHT=470,WIDTH=525");
			newWindow.focus();
		}
		else
		{
			self.location.href=winUrl;
		}
	break

    case "win_b_no_scroll":
        newWindow = window.open(winUrl,winName,"HEIGHT=470,WIDTH=525");
        newWindow.focus();
        break

    default:
        newWindow = window.open(winUrl,winName,properties);
        newWindow.focus();
    }
}  

//Closes the current window
function windowClose(){
  window.close();
}