var _OfbIsBrowserMSIE, _OfbIsBrowserNetscape;
var _OfbMouseX, _OfbMouseY;
var _strHideDisplayStyle;
var _strShowDisplayStyle;
//Constants
var WIDTH                    = 0;
var HEIGHT                   = 1;
var LEFT                     = 0;
var TOP                      = 1;
var MIN_OBJECT_HEIGHT        = 8;

_strHideDisplayStyle = 'none';
_strShowDisplayStyle = 'block';

_OfbIsBrowserMSIE = false;
_OfbIsBrowserNetscape = false;
_OfbMouseX = _OfbMouseY = 0;
_OfbCurObject = null;

if(navigator.userAgent.indexOf('MSIE')>=0) _OfbIsBrowserMSIE = true;
if (document.layers) _OfbIsBrowserNetscape = true;

//Capture mouse XY and retain so anyone ca use it.
if( _OfbIsBrowserNetscape)  document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = __ofbTrackMousePos;

function __ofbGetBrowserSize()
{
	var bodyWidth = document.documentElement.clientWidth;
	var bodyHeight = document.documentElement.clientHeight;
	
	var bodyWidth, bodyHeight; 
	if (self.innerHeight){ // all except Explorer 
	   bodyWidth = self.innerWidth; 
	   bodyHeight = self.innerHeight; 
	}  else if (document.documentElement && document.documentElement.clientHeight) {
	   // Explorer 6 Strict Mode 		 
	   bodyWidth = document.documentElement.clientWidth; 
	   bodyHeight = document.documentElement.clientHeight; 
	} else if (document.body) {// other Explorers 		 
	   bodyWidth = document.body.clientWidth; 
	   bodyHeight = document.body.clientHeight; 
	} 
	return [bodyWidth,bodyHeight];		
}

function __ofbGetClientResolution(strControl)
{
    var Width = screen.width;
    var Height = screen.height;

    var oField = document.getElementById(strControl);
    if (oField)
    {
        oField.value = Height + ', ' + Width;
    }		
}
                                                            
function __ofbGetScrollWidth()
{
   var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;
           
   return w ? w : 0;
} 
function __ofbGetScrollHeight()
{
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 0;
}
function __ofbTrackMousePos(e)
{
   if (_OfbIsBrowserNetscape){
        _OfbMouseX = e.pageX;
        _OfbMouseY = e.pageY;
    }
   else{
        _OfbMouseX = event.x;
        _OfbMouseY = event.y;
    }
    _OfbMouseX += __ofbGetScrollWidth();
    _OfbMouseY += __ofbGetScrollHeight();
}
function __ofbGetMouseX()
{
    return(_OfbMouseX);
}
function __ofbGetMouseY()
{
    return(_OfbMouseY);
}
//Div Related
function __ofbShowHideDiv(strDivID)
{
    if(!strDivID)
        return;
    var oDiv = document.getElementById(strDivID);
    if(oDiv){
        oDiv.style.display = (oDiv.style.display == _strHideDisplayStyle)? _strShowDisplayStyle: _strHideDisplayStyle;
    }
}
function __ofbShowDiv(strDivID)
{
    if(!strDivID)
        return;
    var oDiv = document.getElementById(strDivID);
    if(oDiv){
        oDiv.style.display = _strShowDisplayStyle;
    }
}
function __ofbHideDiv(strDivID)
{
    if(!strDivID)
        return;
    var oDiv = document.getElementById(strDivID);
    if(oDiv){
        oDiv.style.display = _strHideDisplayStyle;
    }
}
function __ofbHideDivWithLocationSense(strDivID)
{
    if(!strDivID)
        return;
    var oDiv = document.getElementById(strDivID);
    if(oDiv){
        oDiv.style.display = _strHideDisplayStyle;
    }
}
function __ofbHideDivWithDelay(strDivID, intDelay)  //Delay in milli seconds
{
    if(!strDivID)
        return;
    setTimeout( '__ofbHideDiv( \'' + strDivID + '\')', intDelay )
}
function __ofbEscapeHTML (strValue)
{
   var oDiv = document.createElement('div');
   var oText = document.createTextNode(strValue);
   oDiv.appendChild(oText);
   return oDiv.innerHTML;
}

function __ofbGetCurrentEvent( oEvent )
{
    if( !oEvent )   oEvent = event;
    return(oEvent);
}
function __ofbGetCurrentEventObject( oEvent )
{
    oEvent = __ofbGetCurrentEvent( oEvent );
    if(oEvent)  return oEvent.target || oEvent.srcElement;
    return( null )
}
function __ofbGetPercentHeight( intPercent, intOffsetTop )
{
    var arBrowserSize = __ofbGetBrowserSize();
    var intRetVal = 0;
    
    intPercent = __ofbStringToInt( intPercent );
    intOffsetTop = __ofbStringToInt( intOffsetTop );
    intRetVal = arBrowserSize[HEIGHT] - intOffsetTop;
    if( intPercent > 0 ){
        intRetVal = ((intRetVal / 100) * intPercent);
    }
    return( intRetVal );
}
function __ofbSetHeightByPercent( strControlId, intPercent )
{
    var oControl = document.getElementById(strControlId);
    var arControlOffset = null;
    var intPercentHeightPixels = 0;
    
    if( !oControl || oControl == null)
        return;

    arControlOffset = __ofbFindAbsolutePosition( oControl );
    intPercentHeightPixels = __ofbGetPercentHeight( intPercent, arControlOffset[TOP] );
    if(oControl && intPercentHeightPixels > 0){
        oControl.style.height = (intPercentHeightPixels + 'px');
    }
}
function __ofbSetHeightByBottomOffset( strControlId, intBottomOffset )
{
    var arViewPortSize = __ofbGetBrowserSize();
    var oControl = document.getElementById(strControlId);
    var arControlOffset = null;
    var intHeightPixels = 0;
    
    if( !oControl || oControl == null)
        return;
    intBottomOffset = __ofbStringToInt( intBottomOffset );
    arControlOffset = __ofbFindAbsolutePosition( oControl );
    intHeightPixels = (arViewPortSize[HEIGHT] - arControlOffset[TOP] - intBottomOffset);
    if( intHeightPixels <= 0 ){
        intHeightPixels = MIN_OBJECT_HEIGHT;
    }
    if(oControl && intHeightPixels > 0){
        oControl.style.height = (intHeightPixels + 'px');
    }
}
function __ofbSetHeightByBottomOffsetControlHeight( strControlId, strBottomOffsetControlId )
{
    var oControl = document.getElementById(strBottomOffsetControlId);
    var intBottomOffset = 0;
    
    if( oControl )
        intBottomOffset = oControl.offsetHeight;
    return( __ofbSetHeightByBottomOffset( strControlId, intBottomOffset ) );
}
function __ofbGetStyleProperty( oControl, strPropertyName ) {
    var oRetVal = '';
    
	if( typeof oControl == 'string')
	    oControl = document.getElementById(oControl);
	if( !oControl )
	    return( oRetVal );
	if (oControl.currentStyle) {
		oRetVal = oControl.currentStyle[strPropertyName];
	} else if (window.getComputedStyle) {
		oRetVal = document.defaultView.getComputedStyle(oControl,null).getPropertyValue(strPropertyName);
	}
	return oRetVal;
}



// The Following Methods are Copied from Designer to make sure that this generic code is not dependent on Designer Functions.
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//This method Converts any String to Integer this is mainly used to convert pixel sizes 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
function __ofbStringToInt( strInVal )
{
var intRetVal = 0, intCount = 0, strTemp = "", strVal ="";

    if( !strInVal )
        return( intRetVal );
    strVal = strInVal.toString();
    if( strVal ){
        if( strVal.length ){
            for( intCount = 0; intCount < strVal.length; intCount++ ){
                if( (strVal.charAt(intCount) >= '0' && strVal.charAt(intCount) <= '9') || strVal.charAt(intCount) == '-'  || strVal.charAt(intCount) == '+' ||strVal.charAt(intCount) == '.')
                    strTemp = strTemp.concat(strVal.charAt(intCount));  
                else break;
            } 
            if( strTemp.length > 0 )
                intRetVal = parseInt(strTemp, 0 );
         }
         else 
            intRetVal = parseInt(strTemp, 0 );
    }
    return intRetVal;
}
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//This method returns the Absolute Position of the Element 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
function __ofbFindAbsolutePosition( oElement) 
{
var intCurrLeft = intCurrTop = 0;

	if (oElement.offsetParent){
		intCurrLeft = oElement.offsetLeft;
		intCurrTop = oElement.offsetTop;
		while (oElement = oElement.offsetParent){
			intCurrLeft += oElement.offsetLeft;
			intCurrTop += oElement.offsetTop;
		}
	}
	return [intCurrLeft,intCurrTop];
}
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------
//This method calcualtes width and height of SrcElement based on diffent combinations and applies the style as well.  //TO DO this need to be tested for all scenarios
-------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
function __ofbGetControlCalcuatedPositionSize( oSrcElement )
{
var intCurrStyleLeft = 0, intCurrStyleTop = 0, intCurrStyleWidth = 0, intCurrStyleHeight = 0;
var strCurrStyle;

    if( oSrcElement ){
        //Calculate Left 
        //strCurrStyle = oSrcElement.currentStyle.left.toLowerCase();
        strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'left' ).toLowerCase();
        if(strCurrStyle != "auto" && strCurrStyle.indexOf("%") < 0 &&  __ofbStringToInt(strCurrStyle ) > 0 ){
    	    intCurrStyleLeft  = __ofbStringToInt(__ofbGetStyleProperty( oSrcElement, 'left'));
    	    strCurrStyle = __ofbGetStyleProperty(oSrcElement, 'padding-Left');
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleLeft += __ofbStringToInt(strCurrStyle);
    	    strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'border-Left-Width');
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleLeft  += __ofbStringToInt(strCurrStyle);
        }    	    
        else
    	    intCurrStyleLeft  = __ofbStringToInt(oSrcElement.offsetLeft);
        //Calculate Top
        strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'top' ).toLowerCase();
        if(strCurrStyle != "auto" && strCurrStyle.indexOf("%") < 0 &&  __ofbStringToInt(strCurrStyle ) > 0 ){
    	    intCurrStyleTop  = __ofbStringToInt(strCurrStyle);
    	    strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'padding-Top' );
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleTop  += __ofbStringToInt(strCurrStyle);
    	    strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'border-Top-Width');
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleTop  += __ofbStringToInt(strCurrStyle);
        }    	    
        else
    	    intCurrStyleTop  = __ofbStringToInt(oSrcElement.offsetTop);
        //Calculate Width 
        strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'width' ).toLowerCase();
        if(strCurrStyle != "auto" && strCurrStyle.indexOf("%") < 0 &&  __ofbStringToInt(strCurrStyle ) > 0 ){
    	    intCurrStyleWidth  = __ofbStringToInt(strCurrStyle);
    	    strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'padding-Left' );
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleWidth  += __ofbStringToInt(strCurrStyle);
    	    strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'padding-Right' );
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleWidth  += __ofbStringToInt(strCurrStyle);
    	    strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'border-Left-Width' );
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleWidth  += __ofbStringToInt(strCurrStyle);
    	    strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'border-Right-Width' );
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleWidth  += __ofbStringToInt(strCurrStyle );
        }    	    
        else
    	    intCurrStyleWidth  = __ofbStringToInt(oSrcElement.offsetWidth);
        //Calculate Height
        strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'height' ).toLowerCase();
        if(strCurrStyle != "auto" && strCurrStyle.indexOf("%") < 0 &&  __ofbStringToInt(strCurrStyle ) > 0 ){
    	    intCurrStyleHeight  = __ofbStringToInt(strCurrStyle);
    	    strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'padding-Top' );
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleHeight  += __ofbStringToInt(strCurrStyle);
    	    strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'padding-Bottom' );
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleHeight  += __ofbStringToInt(strCurrStyle);
    	    strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'border-Top-Width' );
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleHeight  += __ofbStringToInt(strCurrStyle);
    	    strCurrStyle = __ofbGetStyleProperty( oSrcElement, 'border-Bottom-Width' );
    	    if( strCurrStyle && strCurrStyle.length > 0 )
        	    intCurrStyleHeight  += __ofbStringToInt(strCurrStyle);
        }    	    
        else
    	    intCurrStyleHeight  = __ofbStringToInt(oSrcElement.offsetHeight);
    }            	    
    return[__ofbStringToInt(intCurrStyleLeft), __ofbStringToInt(intCurrStyleTop), __ofbStringToInt(intCurrStyleWidth), __ofbStringToInt(intCurrStyleHeight)];
}
