//////////////////////////////////////////////////////////////
// Help Settings are in the \bin\ConfigUtil.config.xml file
// Note: If no settings are entered, the default variable pwbHelpURL
//		   defined below will be used.
//////////////////////////////////////////////////////////////

// To use the integrated (local) Help:
//		- All LocalHelp settings (3) in the config file must be entered. If
//		  one setting is missing it will look to the RemoteHelpUrl 
//		  setting to see if that is set. If the RemoteHelpUrl is not
//		  entered, then the hardcoded pwbHelpURL setting will be used.
//		- those settings will override whatever RemoteHelpUrl value that is set, 
//		  so it is ok to leave the RemoteHelpUrl in tact.

// To use the regular Project Workbook Help:
//		- Set the RemoteHelpUrl in the config file
//		- Make sure that the LocalHelp settings are blanked out

// To use your own custom panel help:
//		- Set the RemoteHelpUrl in the config file
//		- The HelpFile property must be set on the DataPanel. If it is not
//		  then the panel key will be appended to the url instead of the 
//		  CustomHelpFile.

// To use your own custom page help
//    - Set the RemotePageHelpUrl in the config file

// Field Help
//		- If the FieldHelpUrl setting is not set then the built in javascript 
//		  field help will be used.
//		- To turn off field help all together then set the FieldHelpActivated
//		  setting to "false"

var pwbHelpURL = "http://usplddssc003.edsdomdssc.dssc.eds.com/CorePWB/Subsystem/Utils/iCHelp.asp?AdminMode=1&PanelKey=";
var pwbPageHelpURL = "http://usplddssc003.edsdomdssc.dssc.eds.com/CorePWB/Subsystem/Utils/iCHelp.asp?AdminMode=1&PanelKey=";
var fieldHelpPageWindow = null;

function showHelp(helpKey)
{
	var panelHelpURL;
	
	var i = helpKey.match('PanelKey');
	if (i == null)
	i = helpKey.match('Key');
	
	if (i!=null)
	{	
		var first = helpKey.indexOf(':',i.index);
		var last = helpKey.indexOf('&',i.index);
		if (first > 0)
		{
		helpKey = helpKey.substring(0,first) 
		if (last > first)
		helpKey = helpKey + helpKey.substring(last);
		helpKey = helpKey;
		}
	}
	
	if (document.body)
	{
		winHeight = document.body.offsetHeight;
		winWidth = document.body.offsetWidth;
		winTop = document.body.offsetTop + 50;
		winLeft = document.body.offsetLeft;
	}
	
	// LocalHelpUrl & is defined and set in the HelpLink.cs
	if ( LocalHelpUrl != "" )
	{
		panelHelpURL = helpKey;
		helpPageWindow = window.open(panelHelpURL,"HelpWindow", "menubar=no,scrollbars=yes,location=no,resizable=yes,status=no,toolbar=no,top=" + winTop + ",left=" + winLeft + ",height=" + winHeight + ",width=" + winWidth);
	}
	else 
	{
	    // Default Help, hard coded pwbHelpURL
		if ( RemoteHelpUrl != "" )
		    panelHelpURL = helpKey;
		else
            panelHelpURL = pwbHelpURL + helpKey;
		
		// seems that when you add the toolbar & menubar it does that on outside of the height given,
		// so adjust it the same amount and it was offsetted.
		winHeight += -50;
		helpPageWindow = window.open(panelHelpURL,"HelpWindow", "menubar=yes,scrollbars=yes,resizable=yes,status=no,toolbar=yes,top=" + winTop + ",left=" + winLeft + ",height=" + winHeight + ",width=" + winWidth);
	}
	
	helpPageWindow.focus();
}

//
//   Function to show the page help for the passed in pageHelpKey.
//   If the remote(custom) help is defined, the use it's URL otherwise
//   check if the local help is set, if so, use that URL.  If nothing is set
//   then use the hardcoded default URL above.
//
function showPageHelp(pageHelpKey)
{
	if (document.body)
	{
		winHeight = document.body.offsetHeight;
		winWidth = document.body.offsetWidth;
		winTop = document.body.offsetTop + 50;
		winLeft = document.body.offsetLeft;
	}
	
	if (RemotePageHelpUrl != "")
	{
	  // if the page designer set the pagehelp to a specific help page
	  if (typeof CustomHelpPage != 'undefined' && CustomHelpPage != "")
	    pageHelpKey = CustomHelpPage;
	    
		winHeight += -50;
		helpPageWindow = window.open(pageHelpKey,"HelpWindow", "menubar=yes,scrollbars=yes,resizable=yes,status=no,toolbar=yes,top=" + winTop + ",left=" + winLeft + ",height=" + winHeight + ",width=" + winWidth);
	}
	else if (LocalPageHelpUrl != "")
		helpPageWindow = window.open(pageHelpKey,"HelpWindow", "menubar=no,scrollbars=yes,location=no,resizable=yes,status=no,toolbar=no,top=" + winTop + ",left=" + winLeft + ",height=" + winHeight + ",width=" + winWidth);
  else
  {
	    winHeight += -50;
	    helpPageWindow = window.open(pwbPageHelpURL + pageHelpKey,"HelpWindow", "menubar=yes,scrollbars=yes,resizable=yes,status=no,toolbar=yes,top=" + winTop + ",left=" + winLeft + ",height=" + winHeight + ",width=" + winWidth);
  }
    	
	helpPageWindow.focus();
}

/* This function gets the element associated with the click and */
/* gets the field name from that.                               */
/* CO#14463 - JIL - Oct-25/2006 : Function modified to accept   */
/* full path name from server side.                             */
function showFieldHelp(panelKey, titlelessPanel, e)
{		
	
	var i = panelKey.match('PanelKey');
		if (i == null)
	i = panelKey.match('Key');
	
	if (i != null)
	{
	 var first = panelKey.indexOf(':',i.index);
	 var last = panelKey.indexOf('&',i.index);
	 if (first > 0)
	  {
	   var temp = panelKey.substring(0,first) 
	   if (last > first)
	   temp = temp + panelKey.substring(last);
	   panelKey = temp;
	  }	
	}
    var e = e || event;
    var ele = e.target || e.srcElement;
    
    // Size the popup window
    var winWidth = 400;
    var winHeight = 125;   
    
    // Represents the popup size plus space for the window title bar and border
    var padHeight = winHeight + 45;
    var padWidth = winWidth + 15;
    
    // Cross-browser positioning of popup window
    var coordinates = getAnchorWindowPosition(ele.id);
    var top = coordinates.y + ele.offsetHeight;
    var left = coordinates.x;    
    
    // Correct Y positioning to keep window on the screen
    if(top < 0)
        top = 1;
    else if(top + padHeight > screen.availHeight)
        top = top - (top + padHeight - screen.availHeight);   
        
    // Correct X positioning to keep window on the screen
    if(left < 0)
        left = 1;
    else if(left + padWidth > screen.availWidth)
        left = left - (left + padWidth - screen.availWidth);
        
    // If popup window still exists, clear it and re-position
    if ( typeof(fieldHelpPageWindow) != 'undefined' && fieldHelpPageWindow != null && !fieldHelpPageWindow.closed && fieldHelpPageWindow.document.body != null)
    {
        fieldHelpPageWindow.document.body.innerHTML = "";
        fieldHelpPageWindow.moveTo(left, top);
    }
	
	// Create popup window
	fieldHelpPageWindow = window.open(panelKey, "FieldHelpWindow", "menubar=no,scrollbars=no,location=no,resizable=yes,height=" + winHeight + ",width=" + winWidth + ",status=no,toolbar=no,top=" + top + ",left=" + left);
    fieldHelpPageWindow.focus();
}

function getAnchorWindowPosition(anchorname) {
    var coordinates = getAnchorPosition(anchorname); 
    var x = 0;
    var y = 0;
   
    if(document.getElementById) {
        if(isNaN(window.screenX)) {
            x = coordinates.x - document.documentElement.scrollLeft + window.screenLeft;
            y = coordinates.y - document.documentElement.scrollTop + window.screenTop;
        }
        else {
            x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset;
            y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset;
        }
    }
    else if(document.all) {
        x = coordinates.x - document.documentElement.scrollLeft + window.screenLeft;
        y = coordinates.y - document.documentElement.scrollTop + window.screenTop;
    }
    else if(document.layers) {
        x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset;
        y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset;
    }
    
    coordinates.x = x;
    coordinates.y = y; 
    return coordinates;
}

function getAnchorPosition(anchorname) {
    var useWindow = false;
    var coordinates = new Object();
    var x = 0, y = 0;
    var use_gebi = false, use_css = false, use_layers = false;
   
    if(document.getElementById) {
        use_gebi = true;
    }
    else if(document.all) {
        use_css = true;
    }
    else if(document.layers) {
        use_layers = true;
    }
    if(use_gebi && document.all) {
        x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
        y = AnchorPosition_getPageOffsetTop(document.all[anchorname]);
    }
    else if(use_gebi) {
        var o = document.getElementById(anchorname);
        x = AnchorPosition_getPageOffsetLeft(o);
        y = AnchorPosition_getPageOffsetTop(o);
    }
    else if(use_css) {
        x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
        y = AnchorPosition_getPageOffsetTop(document.all[anchorname]);
    }
    else if(use_layers) {
        var found = 0;
        for(var i = 0; i < document.anchors.length; i++) {
            if(document.anchors[i].name == anchorname) {
                found = 1;
                break;
            }
        }
        if(found == 0) {
             coordinates.x = 0;
             coordinates.y = 0;
             return coordinates;
        }
        x = document.anchors[i].x;
        y = document.anchors[i].y;
    }
    else {
        coordinates.x = 0;
        coordinates.y = 0;
        return coordinates;
    }
    coordinates.x = x;
    coordinates.y = y;
    return coordinates;
}
   
function AnchorPosition_getPageOffsetLeft(el) {
    var ol = el.offsetLeft;
    while((el = el.offsetParent) != null) {
        ol += el.offsetLeft;
    }
    return ol;
}   
   
function AnchorPosition_getPageOffsetTop(el) {
    var ot = el.offsetTop;
    while((el = el.offsetParent) != null) {
        ot += el.offsetTop;
    }
    return ot;
}   
   
function AnchorPosition_getWindowOffsetLeft(el) {
    return AnchorPosition_getPageOffsetLeft(el) - document.documentElement.scrollLeft;
}