
// String trim functions, from http://www.developingskills.com/ds.php?article=jstrim&page=1
function strltrim() {
	return this.replace(/^\s+/,'');
}

function strrtrim() {
	return this.replace(/\s+$/,'');
}
function strtrim() {
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;
  

// Width allowed for sidebar on pages with an iframe
var intSidebarWidth = 320; 

function new_popwindow(url, width, height){
	var w;
	if(width == "-1" || height == "-1"){
	   w = window.open(url,"","dependent=yes,toolbar=0,location=0,directories=0, status=0,menubar=0,scrollbars=1,resizable=1");
	}
	else{
	   w = window.open(url,"","dependent=yes,toolbar=0,location=0,directories=0, status=0,menubar=0,scrollbars=1,resizable=1,width="+width+",height="+height+"");
	}
	if (w != null)  // SPR 2339
        w.focus();
}


function urlDecode(str){
    str=str.replace(new RegExp('\+','g'),' ');
    return unescape(str);
}

function urlEncode(str){
    str=escape(str);
    str=str.replace(new RegExp('\+','g'),'%2B');
    return str.replace(new RegExp('%20','g'),'+');
}

var END_OF_INPUT = -1;

var base64Chars = new Array(
    'A','B','C','D','E','F','G','H',
    'I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X',
    'Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3',
    '4','5','6','7','8','9','+','/'
);

var reverseBase64Chars = new Array();
for (var i=0; i < base64Chars.length; i++){
    reverseBase64Chars[base64Chars[i]] = i;
}

var base64Str;
var base64Count;
function setBase64Str(str){
    base64Str = str;
    base64Count = 0;
}
function readBase64(){    
    if (!base64Str) return END_OF_INPUT;
    if (base64Count >= base64Str.length) return END_OF_INPUT;
    var c = base64Str.charCodeAt(base64Count) & 0xff;
    base64Count++;
    return c;
}
function encodeBase64(str){
    setBase64Str(str);
    var result = '';
    var inBuffer = new Array(3);
    var lineCount = 0;
    var done = false;
    while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[ inBuffer[0] >> 2 ]);
        if (inBuffer[1] != END_OF_INPUT){
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
            if (inBuffer[2] != END_OF_INPUT){
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
                result += (base64Chars [inBuffer[2] & 0x3F]);
            } else {
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
                result += ('=');
                done = true;
            }
        } else {
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount >= 76){
            result += ('\n');
            lineCount = 0;
        }
    }
    return result;
}
function readReverseBase64(){   
    if (!base64Str) return END_OF_INPUT;
    while (true){      
        if (base64Count >= base64Str.length) return END_OF_INPUT;
        var nextCharacter = base64Str.charAt(base64Count);
        base64Count++;
        if (reverseBase64Chars[nextCharacter]){
            return reverseBase64Chars[nextCharacter];
        }
        if (nextCharacter == 'A') return 0;
    } 
}

function ntos(n){
    n=n.toString(16);
    if (n.length == 1) n="0"+n;
    n="%"+n;
    return unescape(n);
}

function decodeBase64(str){
    setBase64Str(str);
    var result = "";
    var inBuffer = new Array(4);
    var done = false;
    while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT
        && (inBuffer[1] = readReverseBase64()) != END_OF_INPUT){
        inBuffer[2] = readReverseBase64();
        inBuffer[3] = readReverseBase64();
        result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
        if (inBuffer[2] != END_OF_INPUT){
            result +=  ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));
            if (inBuffer[3] != END_OF_INPUT){
                result +=  ntos((((inBuffer[2] << 6)  & 0xff) | inBuffer[3]));
            } else {
                done = true;
            }
        } else {
            done = true;
        }
    }
    return result;
}


function setSize(id) {
// Sets the height and width of the iframe object with the specifed id
// Height is specified as total available browser height, minus an offset calculated as the
// Y coordinate of the iframe itself plus a buffer of 20 pixels.
// Width is specified as total available browser width, minus an offset calculated as the
// value of intSidebarWidth (see comment above). 

	var intWinHeight, intWinWidth, blnIsIE;
	//debugger;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		intWinWidth = window.innerWidth;
		intWinHeight = window.innerHeight;
		blnIsIE = false;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		intWinWidth = document.documentElement.clientWidth;
		intWinHeight = document.documentElement.clientHeight;
		// Does not have 'box model bug'
		blnIsIE = false;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		intWinWidth = document.body.clientWidth;
		intWinHeight = document.body.clientHeight;
		blnIsIE = true;
	}		

  	var objFrameContainer = document.getElementById(id);
	//debugger;
  	if (objFrameContainer) {
		var intOffsetY = findPosY(objFrameContainer);
		if (intWinWidth > intSidebarWidth)
			intWinWidth -= intSidebarWidth;
		if (intWinHeight > intOffsetY)	
			intWinHeight -= intOffsetY;
			
		// SPR 3283: set both style.width & width, style.height & height
		objFrameContainer.width = intWinWidth;
		objFrameContainer.style.width = intWinWidth;
		if (intWinHeight > 15) {
			objFrameContainer.height = intWinHeight - 15;
			objFrameContainer.style.height = intWinHeight - 15;
			}
		//var frame = document.frames[id];
		var frame = document.getElementById(id);
		if (frame.blnResizable) {
			if (frame.blnIsFrameset) {
				frame.frameDisplay.setBody();
			} else {
				frame.setBody();
			}
		}
	} 
	
	// Set height of divFocalScroll, if it exists; used only where the focal div should be a fixed-height, 
    // scrollable div that has form-commit buttons below it
	var intFocalHeight;
	var objFocalDiv = document.getElementById('divFocalScroll');

	if (objFocalDiv) {
		var intIEOffset = 0;
		// Addresses IE's box-model CSS problem (accounts for padding as part of div height)
		if (blnIsIE) intIEOffset = 50;	
		
		// Check whether there are form buttons at the bottom of the page; if so, leave a buffer for them
		var intButtonOffset = 0;
		if (document.getElementById('divFormButtons')) { 
			intButtonOffset = 120;
		} else {
			intButtonOffset = 72;
		}
				 
		var intOffsetY = findPosY(objFocalDiv) + intButtonOffset - intIEOffset;
		intFocalHeight = intWinHeight - intOffsetY;
		if (intFocalHeight<300) intFocalHeight = 300;  // Enforce minimum height
		objFocalDiv.style.height = intFocalHeight + "px";
	} 	
}

function findPosX(obj) {
// Function for finding the x coordinate of an object whose pointer is given as an argument.
// Returns the x coordinate as an integer, relative to the top left origin.
	var intCurlLeft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			intCurlLeft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		intCurlLeft += obj.x;
	return intCurlLeft;
}	

function findPosY(obj) {
// Function for finding the y coordinate of an object whose pointer is given as an argument.
// Returns the y coordinate as an integer, relative to the top left origin.
	var intCurlTop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			intCurlTop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		intCurlTop += obj.t;
	return intCurlTop;
}	

function setBody(id) {
// Sets the width of a web page that is framed inside an iframe in a main page.
// Finds the width of the parent page (as determined by the width of the browser window),
// and subtracts an offset amount specified by a Javascript variable 'intSidebarOffset'
// specified in the parent page (this offset accounts for the width of the sidebar
// that exists on all framed pages).

	var objFrameContainer = top.document.getElementById(id);
	if (objFrameContainer) {
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			newWidth = top.window.innerWidth - parent.intSidebarOffset;
			// @ TO DO: Need to find out about frmResults
			var objForm=document.getElementById('frmResults');
			objForm.style.width=newWidth+"px";
		} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			//IE 6+ in 'standards compliant mode'
			newWidth = top.document.body.offsetWidth - (parent.intSidebarOffset + 20);
			document.body.style.width=newWidth+"px";
		} 	
	} 
}

// SPRID 210: typically need to 'return clickbutton(..) in keypress handler to suppress usual keypress handling to retain effect of <control>.focus()
function clickButton(e, buttonid){ 
      var bt = document.getElementById(buttonid); 
      if (typeof bt == 'object'){ 
            if(navigator.appName.indexOf("Netscape")>(-1)){ 
                  if (e.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } 
            if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){ 
                  if (event.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } 
      }
} 

function findFrame(frameID, IFrameID) {
	if ((IFrameID == null) || (IFrameID == ''))
		return top.document.getElementById(frameID);
	var iframeFrame = top.document.getElementById(IFrameID);
	return findFrameAux(frameID, iframeFrame);
}

function findFrameAux(frameID, parentFrame) {
	//debugger;
	var ret = null;
	if (navigator.appName.indexOf('Netscape')>(-1)) {
		if (parentFrame != null)
			{
			var doc = parentFrame.contentDocument;
			if (doc != null)
				ret = doc.getElementById(frameID);
			}
		return ret;
	} 
	if (navigator.appName.indexOf('Microsoft Internet Explorer')>(-1)) {
		if (parentFrame != null)
			{
			if (parentFrame.contentWindow != null)
				ret = parentFrame.contentWindow.frames[frameID];
			if (ret == null)
				ret = parentFrame.document.getElementById(frameID);
			}
		return ret; 
	} 
}

function getDocument(frameObj) {
	if (frameObj.contentWindow != null)
		return frameObj.contentWindow.document;
	else
		return frameObj.document;
	}


var blnScrollbusybox;

function setScrimHeight() {
// Sets the height of the scrim layer, and positions the busybox.
// Height is specified as total available browser height.

    var objScrim = document.getElementById('tblScrim');
    if (objScrim) {
		// make sure Scrim cover full screen with clientHeight&clientWidth or 
		// full scrollHeight if there is scroll bar
        var intScrimHeight = getWinHeight();
        if (intScrimHeight > document.body.clientHeight)
			objScrim.style.height = intScrimHeight + "px";
	    else 
			objScrim.style.height = document.body.clientHeight + "px";
	     
        objScrim.style.width = document.body.clientWidth + "px";
   }     
    
   setBusyboxPosition();
}

function setBusybox() {
// Positions the busybox in the center of the window.

	if (blnScrollbusybox)
		setBusyboxPosition();
	else
		return;
}

function setBusyboxPosition() {
		
    var scrollPosition;
    
    if (navigator.appName == "Microsoft Internet Explorer"){
        scrollPosition = document.body.scrollTop ;
    } else {
        scrollPosition = window.pageYOffset;
    }
            
    var intWinHeight = getWinHeight();
    var intWinWidth = getWinWidth();
    
    var objBusybox = document.getElementById('divBusybox');
    if (objBusybox) {
        var intBusytop = (intWinHeight-objBusybox.offsetHeight);
        var intBusyleft = (intWinWidth-objBusybox.offsetWidth);
        intBusytop = Math.round(intBusytop/2) - 60 + scrollPosition;
        intBusyleft = Math.round(intBusyleft/2);
        if (intBusytop < 10) intBusytop = 10;
        if (intBusyleft < 10) intBusyleft = 10;
        var strBusytop = intBusytop + "px";
        var strBusyleft = intBusyleft + "px";
        objBusybox.style.top = strBusytop;
        objBusybox.style.left = strBusyleft;
    }
}

function getWinHeight() {
// Returns the integer height of the visible area of the browser window, in pixels
	var intHeight, blnIsIE;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		intHeight = window.innerHeight;
		blnIsIE = false;
	} else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		intHeight = document.documentElement.clientHeight;
		// Does not have 'box model bug'
		blnIsIE = false;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		intHeight = document.body.clientHeight;
		blnIsIE = true;
	}			
	return intHeight;
}

function getWinWidth() {
// Returns the integer width of the visible area of the browser window, in pixels
	var intWidth, blnIsIE;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		intWidth = window.innerWidth;
		blnIsIE = false;
	} else if(document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		intWidth = document.documentElement.clientWidth;
		// Does not have 'box model bug'
		blnIsIE = false;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		intWidth = document.body.clientWidth;
		blnIsIE = true;
	}			
	return intWidth;
}

function showBusybox(message, progressMessage) {
// Rewrites the text message shown in the busybox, and makes it visible
    if (frames["frameBusybox"] != null)
        {
	    frames["frameBusybox"].setMessage(message, progressMessage);
	    frames["frameBusybox"].restartAnimation();
	    setScrimHeight();
	    show("tblScrim");
	    }
}

function hideBusybox() {
// Hides the busybox and scrim
	var objHide = document.getElementById('tblScrim');
	if (objHide)
	{
        if (frames["frameBusybox"] != null)
	    	hide(objHide, frames["frameBusybox"]);
	}
	else 
	{
		objHide = top.document.getElementById('tblScrim');
		if (objHide)
		{
            if (frames["frameBusybox"] != null)
		    	hide(objHide, top.frames["frameBusybox"]);
		}
	}
}

function autoHideBusybox() {
// Hides the busybox and scrim if there are no child frames
	//debugger;
	if(window.frames.length > 0) {
		var objHide = document.getElementById('tblScrim');
		if (objHide)
		{ 
			hide(objHide, frames["frameBusybox"]);
		}
	}
}

function setBusyBoxDefaultMessage(defaultMessage) {
    //debugger;
	frames["frameBusybox"].setDefaultMessage(defaultMessage);
}

function show(objID) {
	var objShow = document.getElementById(objID);
	objShow.style.visibility = "visible";
	blnScrollbusybox = true;
}
		
function hide(objHide, frameAnimation) {
// Makes an object with ID objID hidden
	objHide.style.visibility = "hidden";
	frameAnimation.stopAnimation();
	blnScrollbusybox = false;
}

function containsDOM (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee))
      break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}

// Fixes a decoding error in ComponentArt Grid columns which leaves '<' undecoded (but not '>')
function decodeLBrackets(uri) {
    var ret = uri;
    while (ret.search('#%cLt#%') >= 0)
        ret = ret.replace('#%cLt#%', '<');
    if (ret == "")
        // SPR 1712
        return "&nbsp;";
    else
        return ret;
}

function OpenHelpWindow(helpUrlForIE)
{
	w=window.open(helpUrlForIE, '', "left=650,top=650,width=1,height=1,toolbar=0,status=0,menubar=0");
	w.blur();
	setTimeout("w.close()", 3000);
}
