// Library for Compatibility function

//return an object with members to recognize the browser
function xnetCompatibilityBrowserRecognize()
{

	if( '' + this.ver == 'undefined' ) {
		this.ver=navigator.appVersion;
		this.agent=navigator.userAgent;
		this.dom=document.getElementById?1:0;
		this.opera5=this.agent.indexOf("Opera 5")>-1;
		this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
		this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
		// TODO diegoc: quick compatibility fix
		this.ie6= this.ie7 || (this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
		this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
		this.ie=this.ie4 || this.ie5 || this.ie6 || this.ie7;
		this.mac=this.agent.indexOf("Mac")>-1;
		this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	  this.ffox1=(this.navigator != null && this.agent.indexOf("Firefox")!=-1 && parseInt(this.agent.charAt( this.agent.indexOf("Firefox")+8 ))>=1 ) ?1:0;
		this.ns6 = (this.ns6==1 && this.ffox1==0)?1:0;
		this.ns4=(document.layers && !this.domk)?1:0;
		this.bw=(this.ie7 || this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	}
}

//Return a reference to an Object by the name
function xnetCompatibilityObjFind(n, d, findOne)
{//v4.02

  xnetCompatibilityBrowserRecognize();

	//debugger;
	var p,i,x =null;
	var strN = '' + n;
  var searchDone = false;

	// davide 21.09.2005 check if n (name is invalid)
	if( n == null || strN.length <= 0 )
		return( null );
		
	if(strN.toLowerCase().indexOf('[object') >= 0 )
		return n
	else	
	{
		if(!d) d=document; 
		
		if( (p=n.indexOf("?"))>0 && parent.frames.length )
		{
			d = parent.frames[n.substring(p+1)].document;
			n = n.substring(0,p);
		}

		if( ! findOne ) {
			if(!(x=d[n]) && ( d.all /*|| this.ffox1*/ ))  // HACK diegoc: does not work!!
			{
//			  if( this.ffox1 )
//			    x = xnGetElementsById(n);
//			  else
			    x = d.all[n];
				if (ie) searchDone = true;
      }
      
			if(!x && !searchDone)
			{
				var res = xnetGetElementsById_XPath(n);
				if ( res != -1 )
				{
				  x = res;
				  searchDone = true;
				}
      }
			if(!x && !searchDone)
				x = xnetGetElementsById_TagName(n);

			if (!x && d.forms!=null)
			{
				for( i=0; !x && i<d.forms.length; i++ )
					x = d.forms[i][n];
			}	

			if (!x && d.layers!=null)
			{
				for( i=0; !x && d.layers&&i<d.layers.length; i++ )
					x = xnetCompatibilityObjFind(n, d.layers[i].document);
			}

		}

		if(!x && !searchDone && d.getElementById )
			x = d.getElementById(n); 

		return x;
		
	}	
}

// xnFind: a shorter name for xnetCompatibilityObjFind (!),
//  but does not use XPath search or any other "naive" method to search for objects in the DOM
//  NOTE: it will *not* return an array when more than one object with same ID exists in the DOM
function xnFind(n, d)
{
	return xnetCompatibilityObjFind(n,d,true);
}

// xnFindAll: works like xnFind,
//  but returns an array when more than one object with same ID exists in the DOM
//  NOTE: slow, use *only* when strictly necessary
function xnFindAll(n, d)
{
	return xnetCompatibilityObjFind(n,d,false);
}

function xnetCompatibilityJsObjFind(n, direction)               // n:          name of the js object;
                                                                // direction:  up or down recursion;
                                                                // pathObj:    path of the object location in the DOM, useful if combined with "eval" function
{
  var jsObj;
  direction = ( direction ? direction : 'up' );   // default recursion direction = 'up'
  
  switch ( direction )
  {
    case 'up':
      jsObj   = eval("window."+n);
      if ( jsObj )
        return jsObj;
            
      if ( window != top )
        return window.parent.xnetCompatibilityJsObjFind(n, direction);
      break;
      
    case 'down':
      jsObj = eval("window."+n);
      if ( jsObj )
        return jsObj;
      
      if ( window.frames )
      {
        for ( var f=0; f<window.frames.length; f++ )
        {
          jsObj = window.frames[f].window.xnetCompatibilityJsObjFind(n, direction);
          if ( jsObj )
            return jsObj;
        }
      }
      break;
      
  }
  return null;
}

function xnetCompatibilityJsObjFindSetValue(n, direction, objToSet)               // n:          name of the js object;
                                                                // direction:  up or down recursion;
                                                                // pathObj:    path of the object location in the DOM, useful if combined with "eval" function
{
  var jsObj;
  direction = ( direction ? direction : 'up' );   // default recursion direction = 'up'
  
  switch ( direction )
  {
    case 'up':
      jsObj   = eval("window."+n);
      if ( jsObj )
        jsObj = objToSet;
            
      if ( window != top )
        window.parent.xnetCompatibilityJsObjFindSetValue(n, direction, objToSet);
      break;
      
    case 'down':
			//debugger;
      jsObj = eval("window."+n);
      if ( jsObj )
        jsObj = objToSet;
      
      if ( window.frames )
      {
        for ( var f=0; f<window.frames.length; f++ )
        {
          jsObj = window.frames[f].window.xnetCompatibilityJsObjFind(n, direction);
          if ( jsObj )
						{
							jsObj = objToSet;
							break;
						}
        }
      }
      break;
      
  }
  return null;
}

//Find an object in all form and subforms and set to that object the value specified
function xnetCompatibilityObjFindDeepSetValue(n, newvalue, w)
{
	if( w == null ) w = top;
	
  var ctrl = w.xnetCompatibilityObjFind(n);
  
  if ( ctrl )
  {
    ctrl.value = newvalue;
    w.XNetManageControl_SetDataChanged();
    return true;
  }
  else
  {
		for(var k = 0; w.frames && (k < w.frames.length); k++) 
			if ( xnetCompatibilityObjFindDeepSetValue(n, newvalue, w.frames[k].window) )
			  return true;
	}
  return false;
}

//Find an object in all form and subforms and set to that object the value specified
function xnetCompatibilityObjFindDeepSetValueUnescape(n, value, d)
{
  return xnetCompatibilityObjFindDeepSetValue(n, unescape(value), (d ? d : top.document) );
}

function xnetGetElementsById_XPath(term)
{
  if ( !document.evaluate ) return -1;
  
  var xpathString = "//*[@id='" + term.toString() + "']"
  var xpathResult = document.evaluate(xpathString, document, null, 0, null);
  var outArray = new Array();
  var htmlel;
  while ((htmlel = xpathResult.iterateNext())) 
  { 
    if (htmlel)
      outArray[outArray.length] = htmlel;
  }

  switch( outArray.length )
  {
    case 0:
      return null;
    case 1:
      return outArray[0];
    default:
      return outArray;
  }

}

function xnetGetElementsById_TagName(term)
{
  var strId = ( term.id ? term.id : term )
  var c = document.getElementsByTagName("*"); //[0].getElementsByTagName("*");
  var outArray = new Array();
  for (var x=0; x<c.length; x++)
    if (c[x].id == strId) // || c[x].name == strId)
      outArray[outArray.length] = c[x];

  switch( outArray.length )
  {
    case 0:
      return null;
    case 1:
      return outArray[0];
    default:
      return outArray;
  }
}

function xnGetElementsById(tagName) {
	var unFiltered = document.getElementsByTagName('*');
	var filtered = [];
	for (i = 0; i < unFiltered.length; i++) if (unFiltered[i].getAttribute('id') == tagName) filtered.push(unFiltered[i]);
	return filtered;
}

function xnGetElementsByClass(tagName) {
	var unFiltered = document.getElementsByTagName('*');
	var filtered = [];
	for (i = 0; i < unFiltered.length; i++) if (unFiltered[i].getAttribute('class') == tagName) filtered.push(unFiltered[i]);
	return filtered;
}


// set object property
function xnetCompatibilityPropertySet(objName,theProp,theValue) 
{ 
  var obj = xnFind(objName);
	xnSet(obj,theProp,theValue);
}
xnSetByName = xnetCompatibilityPropertySet;

// set object property
function xnetCompatibilityPropertySetI(obj,theProp,theValue) 
{ 
	xnetCompatibilityBrowserRecognize();
	
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)) {
//    if( this.ns6 )
//			obj.setAttribute(theProp,theValue);
//		else
			eval("obj." + theProp + "='"+theValue + "'");
	}
}
xnSet = xnetCompatibilityPropertySetI;

// get object property
function xnetCompatibilityPropertyGet(objName,theProp) 
{ 
  var obj = xnFind(objName);
	return xnGet(obj,theProp);
}
xnGetByName = xnetCompatibilityPropertyGet;

// get object property
function xnetCompatibilityPropertyGetI(obj,theProp) 
{ 
	xnetCompatibilityBrowserRecognize();
  
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)) {
//    if( this.ns6 )
//			return obj.getAttribute(theProp);
//		else
			return eval("obj." + theProp);
	}
}
xnGet = xnetCompatibilityPropertyGetI;

/*******************************************************************************************************************/
function AdjustIFrameSize (iframeWindow) {
  if (iframeWindow.document.height)
  {
    var iframeElement = document.getElementById(iframeWindow.name);
    iframeElement.style.height = iframeWindow.document.height + 'px';
    iframeElement.style.width = iframeWindow.document.width + 'px';
  }
  else if (document.all)
  {
    var iframeElement = document.all[iframeWindow.name];
    if (iframeWindow.document.compatMode && iframeWindow.document.compatMode != 'BackCompat')
    {
      iframeElement.style.height = iframeWindow.document.documentElement.scrollHeight + 5 + 'px';
      iframeElement.style.width = iframeWindow.document.documentElement.scrollWidth + 5 + 'px';
    }
    else
    {
      iframeElement.style.height = iframeWindow.document.body.scrollHeight + 5 + 'px';
      iframeElement.style.width = iframeWindow.document.body.scrollWidth + 5 + 'px';
    }
  }
}

function GetDialogBoxObj(){	
  return top._spnModalWindow
}

function XNetSetIframeHeight()
{
  XNetSetIframeSize(false, true);
}

function XNetSetIframeSize(bWidth, bHeight)
{
  var minWidth      = -1;
  var maxWidth      = -1;
  var minHeight     = -1;
  var maxHeight     = -1;
  var resizeWidth   = bWidth;
  var resizeHeight  = bHeight;
  var offsetWidth   = 0;
  var offsetHeight  = 0;
  var SCROLLBAR_WIDTH = 3;
  
// debugger;
	if ( window == top || window.name == null || window.name == '' ) return;

  var iframeName = window.name;
	var iframeWin = parent.frames[iframeName];
	var iframeEl = parent.document.getElementById ? parent.document.getElementById(iframeName): parent.document.all ? parent.document.all[iframeName] : null;

  if (iframeEl && iframeEl.getAttribute && iframeEl.getAttribute('xnAutoResize') == 'false') return;
  
	if ( window.name == 'popupFrame' )    // Modal window
	{
//	  debugger;
	  if ( ! GetDialogBoxObj().AutoResizeWidth && ! GetDialogBoxObj().AutoResizeHeight ) 
	  {
      // Manage scrollbar width
      var docHt = XNetGetViewportHeight(iframeWin.document.body);
		  if ( docHt && docHt > parseInt(iframeEl.style.height) )
        iframeWin.document.body.style.marginRight = SCROLLBAR_WIDTH;
      else
        iframeWin.document.body.style.marginRight = 0;
	    return;
    }
    
    resizeWidth   = GetDialogBoxObj().AutoResizeWidth;
    resizeHeight  = GetDialogBoxObj().AutoResizeHeight;
    if (GetDialogBoxObj().AutoResizeWidth)
    {
      minWidth  = parseInt( (GetDialogBoxObj().MinWidth == "*" ? -1 : GetDialogBoxObj().MinWidth) );
      maxWidth  = parseInt( (GetDialogBoxObj().MaxWidth == "*" ? -1 : GetDialogBoxObj().MaxWidth) );
  	  GetDialogBoxObj().AutoResizeWidth = false;
	    // Set a large size to popup frame because it resizes only if frame starting size is greater than destination size
	    top.gPopFrame.style.width = "750px";
    }
    if (GetDialogBoxObj().AutoResizeHeight)
    {
	    minHeight = parseInt( (GetDialogBoxObj().MinHeight == "*" ? -1 : GetDialogBoxObj().MinHeight) );
	    maxHeight = parseInt( (GetDialogBoxObj().MaxHeight == "*" ? -1 : GetDialogBoxObj().MaxHeight) );
  	  GetDialogBoxObj().AutoResizeHeight = false;
	    // Set a large size to popup frame because it resizes only if frame starting size is greater than destination size
	    top.gPopFrame.style.height = "1500px";
	  }
	  offsetWidth   = (ie ? 10 : 20);
	  offsetHeight  = (ie ? 10 : 20);
	}
	
//  var iframeName = window.name;
//	var iframeWin = parent.frames[iframeName];
//	var iframeEl = parent.document.getElementById ? parent.document.getElementById(iframeName): parent.document.all ? parent.document.all[iframeName] : null;

	if ( iframeEl && iframeWin ) 
	{
		// S.M. removed try if ecerything still working fine
		var docHt;
		iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous  
		// need to add to height to be sure it will all show
		docHt = XNetGetViewportHeight(iframeWin.document.body);
    if (resizeHeight)
		  if (docHt) iframeEl.style.height = docHt + 16 + offsetHeight + "px";
		//iframeEl.style.width = "auto"; // helps resize (for some) if new doc shorter than previous  
		var docWt = XNetGetViewportWidth(iframeWin.document.body);
		
		try
		{
			iframeEl.setAttribute('width', '100%');
		}
		catch(ex)
		{
		
		}
		
		if (resizeWidth)
		{
		  if (docWt && docWt > iframeEl.offsetWidth)
		  {
				//debugger;
				iframeEl.setAttribute('width', (docWt + offsetWidth));
			  //iframeEl.style.width = docWt + offsetWidth + "px";
		  } 
		}
		else
		{
      // Do operation only if parent container isn't modal window
      if ( !IsModalChildWindow(window) )
      {
  		  iframeEl.style.width = "100%";
        iframeEl.style.width = iframeEl.clientWidth;
      }
		}
		
		//iframeEl.style.height = "100%"; // helps resize (for some) if new doc shorter than previous  
		// need to add to height to be sure it will all show
		docHt = XNetGetViewportHeight(iframeWin.document.body);
		if (resizeHeight)
			if (docHt) iframeEl.style.height = docHt + 16 + offsetHeight + "px";
		
		// ---- START MODAL WINDOW SETTINGS ---- //
    if ( window.name == 'popupFrame' )
    {
		  // Check min and max sizes of modal window
		  if ( minWidth != -1 && parseInt(iframeEl.style.width.replace("px","")) < minWidth )
		    iframeEl.style.width = minWidth + "px";
		  if ( maxWidth != -1 && parseInt(iframeEl.style.width.replace("px","")) > maxWidth )
		    iframeEl.style.width = maxWidth + "px";
		  if ( minHeight != -1 && parseInt(iframeEl.style.height.replace("px","")) < minHeight )
		    iframeEl.style.height = minHeight + "px";
		  if ( maxHeight != -1 && parseInt(iframeEl.style.height.replace("px","")) > maxHeight )
		    iframeEl.style.height = maxHeight + "px";

      // Manage scrollbar width
		  if ( docHt && docHt > parseInt(iframeEl.style.height) )
        iframeWin.document.body.style.marginRight = SCROLLBAR_WIDTH;
      else
        iframeWin.document.body.style.marginRight = 0;

      // Init modal position and size
  	  top.XNetModalInitPositionAndSize(parseInt(iframeEl.style.width.replace("px","")), parseInt(iframeEl.style.height.replace("px","")));
  	}
  	// ---- END MODAL WINDOW SETTINGS ---- //
  	
  	if ( IsModalChildWindow(window) )
    {
		  iframeEl.style.width = "100%";
    }
	}

  if ( parent.XNetSetIframeSize)
    parent.XNetSetIframeSize(bWidth, bHeight);
    
 }

function IsModalChildWindow(w)
{
  if (w == top)
    return false;
  else
  {
		if ( (xnFind('isModal') && xnFind('isModal').value == '1') )
		  return true;
    return IsModalChildWindow(w.parent);
  }
}

function XNetGetObjectLeft( objectID )
{
	//debugger;
	var left = 0;
	
	var layer = xnFind(objectID);
	
	if ( layer != document.body )
	{
		var currentLeft = 0;
		object = layer;
		while ( object )
		{
			currentLeft += object.offsetLeft;
			object = object.offsetParent;
		}
		
		left = currentLeft;
	}
	
	return left;
}

function XNetGetObjectTop( objectID )
{
	//debugger;
	var top = 0;
	
	layer = xnFind(objectID);
	
	if ( layer != document.body )
	{
		var currentTop = 0;
		object = layer;
		while ( object )
		{
			currentTop += object.offsetTop;
			object = object.offsetParent;
		}
		
		top = currentTop;
	}
	
	return top;	
}

function XNetGetScreenWidth()
{
	if ( parseInt(navigator.appVersion)>3 ) 
		return screen.width;
	else 
		if ( navigator.appName == "Netscape" && parseInt(navigator.appVersion)==3 && navigator.javaEnabled() ) 
		{
			var jToolkit = java.awt.Toolkit.getDefaultToolkit();
			var jScreenSize = jToolkit.getScreenSize();
			return jScreenSize.width;
		}
}

function XNetGetScreenHeight()
{
	if ( parseInt(navigator.appVersion)>3 ) 
		return screen.height;
	else 
		if ( navigator.appName == "Netscape" && parseInt(navigator.appVersion)==3 && navigator.javaEnabled() )
		{
			var jToolkit = java.awt.Toolkit.getDefaultToolkit();
			var jScreenSize = jToolkit.getScreenSize();
			return jScreenSize.height;
		}
}

function XNetGetScreenAvailHeight()
{
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' )
    //Non-IE
    myHeight = window.innerHeight;
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  return myHeight;
}

function XNetGetScreenAvailWidth()
{
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' )
    //Non-IE
    myWidth = window.innerWidth;
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
    //IE 4 compatible
    myWidth = document.body.clientWidth;

  return myWidth;
}

// Get full Height of specified object
function XNetGetViewportHeight(obj) 
{
  var objRef = xnetCompatibilityObjFind(obj);
	var xScrollHeight = objRef.scrollHeight;
	var xOffsetHeight = objRef.offsetHeight
	
	if (xScrollHeight > xOffsetHeight) // all but Explorer Mac
		return objRef.scrollHeight;
	else // Explorer Mac; //would also work in Explorer 6 Strict, Mozilla and Safari
		return objRef.offsetHeight;
	return window.undefined;
}

// Get full Width of specified object
function XNetGetViewportWidth(obj) 
{				
  var objRef = xnetCompatibilityObjFind(obj);
	var xScrollWidth = objRef.scrollWidth;
	var xOffsetWidth = objRef.offsetWidth
	
	if (xScrollWidth > xOffsetWidth) // all but Explorer Mac
		return objRef.scrollWidth;
	else // Explorer Mac; //would also work in Explorer 6 Strict, Mozilla and Safari
		return objRef.offsetWidth;		
	return window.undefined; 
}

// Used to set height of iframe used on tab control
function XNetSetWindowHeight()
{
	if (parent == window) return;
	else parent.XNetSetIframeHeight('ifrTabs');
}
/*******************************************************************************************************************/


/*
# Region " Progress Bar " 
*/
							 

function progressHide()
{
	try {
		_progressDialog.outerHTML = ""
	}
	catch(e) {}
} 

function progressFinalStatus(Message)
{
	_progressCurrEl.outerHTML = "";
	_progressBarB.outerHTML = '<center><span class="progressFinalText">' + Message + '</span><br><br><button class="manageButton" onclick="progressHide()">Close</button></center>'
} 

function progressInit(steps, heading)
{	
	_progressDialog.maxSteps = steps;
	_progressDialog.currSteps = 0;
	if (progressInit.arguments.length > 1)
		_progressHeading.innerHTML = heading;
}

function progressProgress(steps, element, heading)
{
	var cw = parseInt(_progressBarB.style.width, 10);
	var currSteps = parseInt(_progressDialog.currSteps, 10);
	var maxSteps = parseInt(_progressDialog.maxSteps, 10);
	currSteps += steps;
	_progressDialog.currSteps = currSteps;
	var w = Math.floor(cw * (currSteps/maxSteps));
	_progressBarF.style.width = w;
	if (element) _progressCurrEl.innerText = element;
	if (progressProgress.arguments.length > 2)
		_progressHeading.innerHTML = heading;	
}

function progressControlsAdd ( Message ) // [ Control, [ Control ] ]
{
	var strHTML = '';
	strHTML = '<center><span class="progressFinalText">' + Message + '</span><br/><br/>';
	for ( var i = 1; i < progressControlsAdd.arguments.length; i++ )
	{
		strHTML += "&nbsp;" + progressControlsAdd.arguments[i];
	}
	strHTML += '</center>';
	
	_progressBarB.outerHTML = strHTML ;
		
} 

/*
# End Region " Progress Bar " 
*/

// window.close() function to close top window( cross-browser compatible )
function closeWindow()
{
  if (!ie && !confirm('The Web page you are viewing is trying to close the window.\nDo you want to close this window?')) return;
  window.open('','_parent','');
  top.close();
}

function queryString() 
{
	var data = [];

	_parse(window.location.search.substr(1, window.location.search.length));
	
	function _parse(qs)
	{ 
		qs = qs.replace(/\+/g, ' ');
		
		var pairs = qs.split("&");	
		
		for (var i = 0 ; i < pairs.length; i++)
		{
			var pair = pairs[i].split("=");
			
			var name = unescape(pair[0]);
			var value;
			
			if(pair.length == 2)
			{
				value = unescape(pair[1]);
			}
			
			data[name] = value;
		}
	}
	
	function _getValueCollection()
	{
		return data;
	}
	
	this.getValue = function( key, defaultValue )
	{
		var ret = data[key];
		
		if(ret == null && defaultValue != null)
		{
			ret = defaultValue
		}
		
		return ret;
	}
	
	this.parse = function ( qs )
	{
		_parse(qs); 
	}
	
	this.getValueCollection = function ()
	{
		return _getValueCollection();
	}
}



