/********************************************
* 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;
  	}
}

// 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;

var disc=null;
var selVisibility="visible";
var selDisplay="block";
var aTacs = [] ;

/********************************************
* Generic functions
********************************************/

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;
}

//MakeLayout("document",widthMin,widthMax,widthMaxClient);
function MakeLayout(){	
	if(document.getElementById){
    		var id = 'document';
    		// SetWidth(id,(widthMin - 30),(widthMax - 30),widthMaxClient);
    		// alert("test");
    		// onresize=function(){ SetWidth(id,minr,maxw,maxr);}
    	}
}

function SetWidth(id,a,b,c){	
	// alert(id);
	var w=getBrowserWidth();
	if(w==0) return;
	var el=document.getElementById(id);
	el.style.margin="0 auto";
	var d=el.style;
	if(w<=a) d.width=a+"px";
	else if(w>=c) d.width=b+"px";
	else{
    		var m=(b-a)/(c-a);
    		d.width=parseInt(m*w+a*(1-m))+"px";
   	}
}

function getBrowserWidth(){
	if (window.innerWidth) return window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth!=0)
    		return document.documentElement.clientWidth;
	else if (document.body) return document.body.clientWidth;
	return 0;
}
/********************************************
* This preloads the images. See the body tag for the syntax to do this for an image
********************************************/
function imageLoad() {
        if (document.images) {
                var imgFiles = imageLoad.arguments;
                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,location,toolbar,resizable,HEIGHT=500,WIDTH=700,TOP=10,LEFT=10");
        newWindow.focus();
   break
   
    
   case "win_external_site":
      	newWindow = window.open(winUrl,winName,"scrollbars,menubar,location,toolbar,resizable,HEIGHT=600,WIDTH=800,TOP=10,LEFT=10");
        newWindow.focus();
   break

    case "win_b":
        newWindow = window.open(winUrl,winName,"HEIGHT=250,WIDTH=445,TOP=10,LEFT=10");
        newWindow.focus();
        break

    case "win_b_no_scroll":
        newWindow = window.open(winUrl,winName,"HEIGHT=470,WIDTH=525,TOP=10,LEFT=10");
        newWindow.focus();
        break

    case "win_c":
        newWindow = window.open(winUrl,winName,",HEIGHT=241,WIDTH=550,TOP=10,LEFT=10");
        newWindow.focus();
        break

    case "win_c_no_scroll":
        newWindow = window.open(winUrl,winName,"HEIGHT=555,WIDTH=570,TOP=0,LEFT=0");
        newWindow.focus();
        break


	case "win_d":
   		newWindow = window.open(winUrl,winName,"HEIGHT=180,WIDTH=700,TOP=20,LEFT=20");
        newWindow.focus();
   		break
		
	case "win_e":
        newWindow = window.open(winUrl,winName,"HEIGHT=280,WIDTH=603,TOP=10,LEFT=10");
        newWindow.focus();
        break

	case "win_e_1":
        newWindow = window.open(winUrl,winName,"HEIGHT=250,WIDTH=603,TOP=10,LEFT=10");
        newWindow.focus();
        break	
			
    case "win_e_2":
        newWindow = window.open(winUrl,winName,"HEIGHT=293,WIDTH=603,TOP=10,LEFT=10");
        newWindow.focus();
        break
	case "win_e_3":
        newWindow = window.open(winUrl,winName,"HEIGHT=308,WIDTH=603,TOP=10,LEFT=10");
        newWindow.focus();
        break
		
	case "win_e_4":
        newWindow = window.open(winUrl,winName,"HEIGHT=380,WIDTH=603,TOP=10,LEFT=10");
        newWindow.focus();
        break
		
	case "win_e_5":
        newWindow = window.open(winUrl,winName,"HEIGHT=395,WIDTH=620,scrollbars=yes,TOP=10,LEFT=10");
        newWindow.focus();
        break	
		
		case "win_f":
   		newWindow = window.open(winUrl,winName,"scrollbars, HEIGHT=600,WIDTH=800,TOP=20,LEFT=20");
        newWindow.focus();
   		break

    case "win_g":
        newWindow = window.open(winUrl,winName,"HEIGHT=100,WIDTH=300,TOP=250,LEFT=250");
        newWindow.focus();
	break
        
	case "win_h":
        newWindow = window.open(winUrl,winName,"HEIGHT=250,WIDTH=430,TOP=10,LEFT=10");
        newWindow.focus();
	break

	case "win_i":
        newWindow = window.open(winUrl,winName,"HEIGHT=700,WIDTH=700,TOP=5,LEFT=5");
        newWindow.focus();
	break

	case "win_j":
        newWindow = window.open(winUrl,winName,"HEIGHT=108,WIDTH=298,TOP=10,LEFT=10");
        newWindow.focus();
	break
	case "win_u":
	  	newWindow = window.open(winUrl,winName,"scrollbars,menubar,toolbar,resizable,HEIGHT=500,WIDTH=700,TOP=10,LEFT=10");
	       	newWindow.focus();
		showDisclaimer("../../disclaimers/disclaimer.htm");
	break
	case "win_v":
	  	newWindow = window.open(winUrl,winName,"scrollbars,menubar,toolbar,resizable,HEIGHT=500,WIDTH=700,TOP=10,LEFT=10");
	       	newWindow.focus();
		showDisclaimer("../../../disclaimers/disclaimer.htm");
	break
	case "win_x":
	  	newWindow = window.open(winUrl,winName,"scrollbars,menubar,toolbar,resizable,HEIGHT=500,WIDTH=700,TOP=10,LEFT=10");
	       	newWindow.focus();
	break
	case "win_y":
	  	newWindow = window.open(winUrl,winName,"scrollbars,menubar,toolbar,resizable,HEIGHT=500,WIDTH=700,TOP=10,LEFT=10");
	       	newWindow.focus();
		showDisclaimer("../../../disclaimers/disclaimer.htm");
	break
    default:
        newWindow = window.open(winUrl,winName,properties);
        newWindow.focus();
    }
}  

/********************************************
* Closes the current window
********************************************/
function windowClose(){
	window.close();
}

/********************************************	
* This closes a window	
********************************************/
function closeWindow() 
{
	window.opener.focus();
	window.close();
}

/********************************************
* name - name of the cookie
* value - value of the cookie
* [expires] - expiration date of the cookie (defaults to end of current session)
* [path] - path for which the cookie is valid (defaults to path of calling document)
* [domain] - domain for which the cookie is valid (defaults to domain of calling document)
* [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
* * an argument defaults when it is assigned null as a placeholder
* * a null placeholder is not required for trailing omitted arguments
********************************************/
function setCookie(name, value, expires, path, domain, secure) {
  	var curCookie = name + "=" + escape(value) +
     	((expires) ? "; expires=" + expires.toGMTString() : "") +
      	((path) ? "; path=" + path : "") +
      	((domain) ? "; domain=" + domain : "") +
      	((secure) ? "; secure" : "");

  	document.cookie = curCookie;
}

/********************************************
* name - name of the desired cookie
* * return string containing value of specified cookie or null if cookie does not exist
********************************************/
function getCookie(name) {
  	var dc = document.cookie;
  	var prefix = name + "=";
  	var begin = dc.indexOf("; " + prefix);
  	if (begin == -1) {
    		begin = dc.indexOf(prefix);
    		if (begin != 0) return null;
  	} else
    		begin += 2;
  	var end = document.cookie.indexOf(";", begin);
  	if (end == -1)
    		end = dc.length;
  	return unescape(dc.substring(begin + prefix.length, end));
}

function argItems () {
	if (argItems.arguments[0]==null||argItems.arguments[0]==""){
	r=location.search.slice(1);
	}else{
		theArgName=argItems.arguments[0];
		sArgs = location.search.slice(1).split('&');
		r = '';
		for (var i = 0; i < sArgs.length; i++) {
			theVal = sArgs[i].split('=');
			if(theVal[0]== theArgName){
				r=theVal[1];
	        	}
	    	}
	}
	return (r.length > 0 ? unescape(r) : '')
}

function showDisclaimer(disclaimer){
	var x = 0, y = 0; // default values
	if (document.all) {
		x = window.screenLeft;
	  	y = window.screenTop;
	}else{
		if (document.layers) {
	 		x = window.screenX;
		   	y = window.screenY;
		}
	}
	if (disc && disc.open && !disc.closed){
		disc.focus();
	}else{
		x+=180;
		y+=150;
		disc=window.open(disclaimer,'','HEIGHT=250,WIDTH=430,status=no,resizable=no,toolbar=no,menubar=no,location=no,scrollbars=no,top='+y+',screenY='+y+',left='+x+',screenX='+x);
		disc.focus();
	}
}
		
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

/********************************************
* Previous link
********************************************/
function prev_page() {
	if (history.length > 0)
	{history.back()}
} 

function makeBabies(withMe){
	//alert("making babies with " + withMe);
	var does_exist = true;
	var baby_val = 0;
	var my_baby = "";

	//Now we must open the immediate offspring of this selection.
	while(does_exist){
		baby_val++;
		my_baby = withMe + "." + baby_val;
		if (!document.getElementById(my_baby) || document.getElementById(my_baby)==null||document.getElementById(my_baby)=="undefined"){
			does_exist=false;
		}else{
			document.getElementById(my_baby).style.visibility=selVisibility;
			document.getElementById(my_baby).style.display=selDisplay;
		}
	}

}

function informBrothers(myBruvva){
	//alert("informing brothers of " + myBruvva);
	var does_exist = true;
	var bruvva_val = 0;
	var my_bruvva = "";

	//Now we must open the brothers of this selection
	while(does_exist){
		bruvva_val++;
		my_bruvva = myBruvva + "." + bruvva_val;
		if (!document.getElementById(my_bruvva) || document.getElementById(my_bruvva)==null||document.getElementById(my_bruvva)=="undefined"){
			does_exist=false;
		}else{
			document.getElementById(my_bruvva).style.visibility=selVisibility;
			document.getElementById(my_bruvva).style.display=selDisplay;
		}
	}

}

function showLayer(myLayer,myLayers,myArea){
	var myAreaRef = myArea;
	var mySwitch = 'off';
	if (myLayer == 1){
		mySwitch = 'on';
		
	}
	switchLayer(myAreaRef,mySwitch);
	
	var i = 1;
	while (i < myLayers ){
		// alert(myLayer);
		mySwitch = 'off';
		myAreaRef = myArea + i;
		if (i == (myLayer - 1)){
			mySwitch = 'on';
		
		}
		switchLayer(myAreaRef,mySwitch);
		i++;
	}
}

function switchLayer(myAreaRef,mySwitch){
	if (mySwitch == "on"){
		document.getElementById(myAreaRef).style.visibility="visible";
		document.getElementById(myAreaRef).style.display="block";
	}else{
		document.getElementById(myAreaRef).style.visibility="hidden";
		document.getElementById(myAreaRef).style.display="none";
	}
}

function transitionWindow(url){	
	window.open(url,"def","Height=300,width=400");
}

function editWindow(url){
	window.location=url;
}

function switchCommonContent(winUrl) {
	var position = winUrl.indexOf("/",23);
	var newLocation = "/" + application + "/generated/pages/" + application + "" + winUrl.substring(position,winUrl.length);
	window.location =  newLocation;
}

function setJourney(path) {
	setCookie("journeypath",path,null,"/");
}

function showJourneyLHS(){
	var path = getCookie("journeypath");
}


/********************************************
* returns the string value of a cookie and removes the session id
*   if any is present
*******************************************/
function getCookieValue() {
	//alert("getCookieValue() >> pre-cookie check: " +  document.cookie ) ;
  	var search = "TACAgree=" ;
  	var returnString = "";
  	if (document.cookie.length > 0) {
    		var offset = document.cookie.indexOf(search)
    		// check to see if cookie exists
    		if (offset != -1) { 
      			offset += search.length
      			//set index of beginning of value
      			end = document.cookie.indexOf(";", offset);
      			// set index of end of cookie value avoiding sessionId
      			if (end == -1){
      				end = document.cookie.length;
      			} 
      		returnString=unescape(document.cookie.substring(offset, end)) ;
    	}
  	return returnString ;
  	}else{ 
      		return "" ;
  	}
}//end of getCookieValue

/********************************************
* sets the appropriate cookie when the user
*   accepts the T and Cs
*******************************************/
function acceptTACs(){
	var splitString = "site" ;
	var currentLoc = document.location.href ;
	var splitIndex = currentLoc.indexOf("/", 9 ) ;
	var currentWebApp = currentLoc.substring(0, splitIndex) ;
	var paramLoc = document.location.search.split("=")[1] ;
	var startIndex = 15 ;
	var splitIndexParam = paramLoc.indexOf("/", startIndex ) ;
	var paramPage = unescape( paramLoc.substring(splitIndexParam) ) ;	
	var originPage = currentWebApp + paramPage ;
	/*
	alert("acceptTACs\r\n"
		+"currentWebApp=" + currentWebApp + "\r\n" 
		+"paramLoc=" + paramLoc + "\r\n" 
		+"startIndex" + startIndex +"\r\n"
		+"splitIndexParam=" + splitIndexParam + "\r\n" 
		+"paramPage=" + paramPage + "\r\n" 
		+"originPage=" + originPage + "\r\n" 
	) ;
	*/
	//loop through the aTacs array looking for matching area to the origin url
	for ( var ii = 0 ; ii < aTacs.length ; ii++ ){
		if( aTacs[ii].tacRegEx.test( originPage ) ) {
			tacPage = aTacs[ii].tacPath ;
			areaNumber = ii ;
			break ;
		}
	}
	var strNewCookieVal = getCookieValue() ;
	//create an array of the areas in the cookie
	var arrayNewCrumbs = strNewCookieVal.split("::") ;
	var strUpdatedCrumbs = "" ;
	//now loop through and set this area to true leaving the others alone if they are undefined
	for ( var ii = 0 ; ii < aTacs.length ; ii++ ){
	    if ( ii != areaNumber ){
	    	if ( arrayNewCrumbs[ii] == ( "true" || "false" ) ){
	    		strUpdatedCrumbs += arrayNewCrumbs[ii] ;
		}else{
			strUpdatedCrumbs += "false" ;
		}  	
	    }else{
	    	strUpdatedCrumbs += "true" ;
	    }
	    if ( ii < ( aTacs.length-1 ) ){
	        strUpdatedCrumbs += "::" ;	
	    } 
	}
	//set cookie and return to the refering page
	var finalCookieString = "TACAgree=" +  escape( strUpdatedCrumbs ) + "; path=/;"  ;
	document.cookie = finalCookieString ;
	document.location.href = originPage ;
}//end of acceptTACs

/*
* Sets up the JavaScript Object array for the Terms and Conditions
*/
function tac ( tacRegEx , tacPath ){
	this.tacRegEx =  tacRegEx  ;
	this.tacPath = tacPath ;
}
	