// Copyright © 1999, Cognos Incorporated. All rights reserved. 
// This forms the javascript functions used for PowerPlay Web 6.6 Toolbar
// The functions handle any minor browser differences.

// Browser detection code
var version = parseInt(navigator.appVersion);
var isIE = navigator.appVersion.indexOf("MSIE")>0
var isNS = navigator.appName.indexOf("Netscape")>=0;

if( navigator.appVersion == "" )
	isIE = true;
	
var isIE3 = isIE && version<4  
var isIE4 = isIE && version>=4
var isIE5 = isIE && version>4 
var isNS3 = isNS && version<4
var isNS4 = isNS && version>=4 

// Variables and constants
var _helpwin = null;
var _helpTopic = "pwr_web_explorer";
var _helpSection = "";
var _helpFrame = "pwr_help_tab_frame";
var _helpProd = "";
//var _postable = isIE5 || isNS;  //uncomments this line if support IE version before IE4 SP2. 4.72,3612,1713 update version SP2, 3283
var _postable = isIE4 || isNS;;  //comments this line if support IE version before IIE4 SP2. 4.72,3612,1713 update version SP2, 3283.

function winFocus(w){
	if(w!=null&&w.focus!=null)
		w.focus();
}

function compTail(s1, s2) {
	if (s1.indexOf(s2) == s1.length - s2.length)
		return true;
	return false;
}

function doOver(n,r){
	if (iconState[n] != -1/* hidden */) {
		if (!isIE3 && !bRenderAsIE3) {
			if(iconState[n]==2) // ignore rollover if disabled
				r=0;
			if ( bDTL ) {
				img[n][iconState[n]*2+r].src = imgNm[n][iconState[n]*2+r];
				if ( n == bmnum ) {
					img[bmnum][0].src = imgNm[bmnum][0];
					syncbm();
				}
			}
			if (isNS4 && bDTL )
				document.layers.DTLToolbar.document.images[iconName[n]].src=img[n][iconState[n]*2+r].src;
			else
				document.images[iconName[n]].src=img[n][iconState[n]*2+r].src;
		}
		if(iconState[n]==1&&iconHelp2[n]!=null)
			status=iconHelp2[n];
		else
			status=iconHelp1[n];
	}
	return true;
}

function set(n){
	if(iconPress[n]){
		for(i=0;i<max;i++){
			if(iconPress[i]==iconPress[n]){
				if (iconState[i] != -1) {
					iconState[i]=0;
					doOver(i,0);
				}
			}
		}
		iconState[n]=1;
	}else{
		doOver(n,2); // show depressed state of icon without changing state variable
	}
}

function clear(n){
	if(iconUnpress[n]){
		for(i=0;i<max;i++){
			if(iconUnpress[i]==iconUnpress[n]){
				if (iconState[i] != -1) {
					iconState[i]=1;
					doOver(i,0);
				}
			}
		}
		iconState[n]=0;
	}
}

function doClick(n){
	if(iconState[n]==2)
		return;
	else if(iconState[n]==1)
		clear(n);
	else
		set(n);
	eval(iconAction[n]);
}

function doit(s){
	var winFX = parent.getWindow();
	if(parent.FXloaded!=null && parent.FXloaded==1)
		winFX.doit(s);
	if ( winFX.bRAE && winFX.fhidden.RE.value == "3" ) {
		if (parent.parent && parent.name) parent.parent.syncCommand(parent.name, "doit", s);
	}				 
}

// Render as 0=HTML, 1=CSV, 2=PDF, 3=DrillThru, 7=bookmark
function ra(as){
	var winFX = parent.getWindow();
	if(parent.FXloaded!=null && parent.FXloaded==1) {
		var fh = winFX.document.fhidden;
		if ( !winFX._fhchanged ) {
			winFX._fhchanged = true;
			winFX._fhtarget = fh.target;
			winFX._fhaction = fh.action;
			winFX._fhRA = fh.RA.value;
		}
		var index = 0;
		if ( fh.BACK )
			index = fh.BACK.value.indexOf( "://" );		
		if ( as == 1 && _postable ) {
			FTok=false;			
			fh.action += "?MIME=.CSV";
			fh.RA.value = "1";
			if ( parent.parent == parent) {			
				fh.target = "_top";
			}
			else {
				fh.target = "_blank";
			}
			fh.submit();
			return;	
		}
		else if ( as == 2 && _postable ) {
			FTok=false;
			fh.action += "?MIME=.PDF";
			if ( parent.parent == parent )
				fh.target = "_parent";
			else
				fh.target = "_blank";
			if ( index == -1 || index > 10 ) {
				fh.BACK.value = location.protocol + "//" + location.host + fh.BACK.value
			} 	
			fh.RA.value = "2";
			fh.CO.value = "";
			fh.submit();
			return;	
		}
		else if ( as == 3 ) {
			FTok=false;
			if ( parent.parent == parent )
				fh.target = "_parent";
			else
				fh.target = "_blank";
			if ( index == -1 || index > 10 ) {
				fh.BACK.value = location.protocol + "//" + location.host + fh.BACK.value
			} 
			fh.RA.value = "3";
			fh.submit();
			return;	
		}
		else if ( as == 4 ) {
			FTok=false;
			if ( parent.parent == parent )
				fh.target = "_parent";
			else
				fh.target = "_blank";
			if ( index == -1 || index > 10 ) {
				fh.BACK.value = location.protocol + "//" + location.host + fh.BACK.value
			} 
			fh.RA.value = "4";
			fh.submit();
			return;	
		}			
		else if ( as == 7 ) {
			FTok=false;
			if ( winFX.bmTrnctd == 0 && parent.parent == parent )
				fh.target = "_parent";
			else
				fh.target = "_blank";
			fh.RA.value = "7";
			fh.submit();
			return;	
		}		
		else {  //all the other cases,  if there is
			var parm;
			if (winFX.bmurl != null) 
				parm = winFX.bmurl;
			if (as) parm += "&RA=" + as;
			if (fh && fh.LA) parm += "&LA=" + fh.LA.value;
			if (fh && fh.LO) parm += "&LO=" + fh.LO.value;
			if (fh && fh.CXOBJ) parm += "&CXOBJ=" + fh.CXOBJ.value;
			if (window.backurl != null && as != 0) {
				// if the back url doesn't already have protocol://server:port, add it
				index = window.backurl.indexOf( "://" );
				if ( index == -1 || index > 10 ) {
					parm += "&BACK=" + escape(location.protocol + "//" + location.host + backurl);
				} else {
					parm += "&BACK=" + escape(backurl);
				}
			}
			if (as != 0 || parent.parent == parent) {
				parent.location.search=parm;
			} else {
				// Frame Set -- open new browser window
	  			var newwin=top.window.open(parent.location.protocol+"//"+parent.location.host+parent.location.pathname+parm,"bm","location=yes,menubar=yes,scrollbars=yes,resizable=yes,width=512,height=448");
	  			winFocus(newwin);
			}
		}
	}
}	

function save(){
	var winFX = parent.getWindow();
	if(parent.FXloaded!=null && parent.FXloaded==1)
		winFX.save();
}

function saveAs(){
	var winFX = parent.getWindow();
	if(parent.FXloaded!=null && parent.FXloaded==1)
		winFX.saveAs();
}

function doParent(action){
	var winFX = parent.getWindow();
	if(parent.FXloaded!=null && parent.FXloaded==1)
		winFX.doParent(action);
}

function doSelect(action){
	var winFX = parent.getWindow();
	if(parent.FXloaded!=null && parent.FXloaded==1)
		winFX.doSelect(action);
}

function what(t){
	status=t;
	return true;
}

function ret()
{
	if( document.pqr != null )
		document.pqr.submit();
}

function hp(sTopic, sSection, sTabs, sProd) {
	// ensure optional parameters are always a string
	sTopic = sTopic ? sTopic : _helpTopic;
	sSection = sSection ? sSection : _helpSection;
	sTabs = sTabs ? sTabs : _helpFrame;
	sProd = sProd ? sProd : _helpProd;

	if(isIE3 || _helpwin == null || _helpwin.closed || _helpwin.helpTopic == null) {
		// create new help window
		var sFeatures = "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=450,height=500";
		var sURL = helpurl + "#topic="+sTopic+"&section="+sSection+"&tabs="+sTabs+"&prod="+sProd;

		_helpwin = window.open(sURL, "ppwbhelp", sFeatures);
	} else {
		if(_helpwin != null && _helpwin.helpTopic != null) {
			winFocus(_helpwin);
			_helpwin.helpTopic(sTopic, sSection);
		}
	}
}

function seperate( s1, s2, s3 ){
    wExplWindow = window.open( s1, s2, s3 );
	if( wExplWindow != null && wExplWindow.opener == null )
		wExplWindow.opener=self; 
}

function syncpg(){
	if (ftr && ftr.R) {
		for(i=0;i<ftr.R.length;i++){
			if(compTail(ftr.R.options[i].value, ':' + fh.G.value))
				ftr.R.selectedIndex=i;
		}
	}
	if (ftc && ftc.C) {
		for(i=0;i<ftc.C.length;i++){
			if(compTail(ftc.C.options[i].value, ':' + fh.H.value))
				ftc.C.selectedIndex=i;
		}
	}
}

function syncbm(){
	var winFX=parent.getWindow();
	iconState=winFX.iconState;
	if ( iconState[bmnum] >= 0 && img[bmnum][0]) {		
		var original = img[bmnum][0].src;
		var indx = original.indexOf( "/bookmark" );
		var path  = original.substr(0, indx+1);
		if ( winFX.bmTrnctd == 0 ) {
			iconHelp1[bmnum] = notTrnctdHelp;
			if (img[bmnum][0]) img[bmnum][0].src = path + "bookmark.gif";
			if (img[bmnum][1]) img[bmnum][1].src = path + "bookmark_r.gif";
			if (img[bmnum][2]) img[bmnum][2].src = path + "bookmark_d.gif";
		}
		else {
			iconHelp1[bmnum] = trnctdHelp;
			if (img[bmnum][0]) img[bmnum][0].src = path + "bookmark_o.gif";
			if (img[bmnum][1]) img[bmnum][1].src = path + "bookmark_o_r.gif";
			if (img[bmnum][2]) img[bmnum][2].src = path + "bookmark_o_d.gif";
		}
		if (isNS4 && bDTL )
			document.layers.DTLToolbar.document.images[iconName[bmnum]].src=img[bmnum][0].src;
		else
			document.images[iconName[bmnum]].src=img[bmnum][0].src;
	}	
}

function sync(){
	var winFX=parent.getWindow();
	iconState=winFX.iconState;
	if (isIE3) {
		ftr=document.ftoolbarr;
		ftc=document.ftoolbarc;
	} else {
		ftr=document.ftoolbar;
		ftc=document.ftoolbar;
		for(i=0;i<max;i++)
			if( !bDTL )
				doOver(i,0);
	}
	fh=winFX.document.fhidden;
	syncpg();
	if( !bDTL )
		syncbm();
	defaultstatus="";
	winFocus(winFX);
}

function init(){
	if(parent.FXloaded!=null){
		parent.FTloaded=1;
		parent.sync();
	}
}

function finish(){
	if(parent.FXloaded!=null)
		parent.FTloaded=0;
}
