//* FILE **********************************************************************
//*                                                                          **
//* NAME: debugToolBox.js                                                    **
//*                                                                          **
//* GOAL: Provide JS function to perform Debug on PCS JS code                **
//*                                                                          **
//*****************************************************************************
//*                                                                          **
//* MODIFICATIONS:                                                           **
//*                                                                          **
//* Version  Date         Author  Comment                                    **
//*                                                                          **
//* 1.0      20 Jun 2000  MV      Original version                           **
//* 1.1      04 Jul 2000  DL      Add prefix management + LogTrace           **
//* 1.2      07 Jul 2000  MV      Use fSetCookie                             **
//* 1.4      07 Nov 2000  MV      Use fDeleteCookiesOfprefixe('PCS_');       **
//* 1.5      23 Nov 2000  MV      Merged som DL modifs                       **
//* 1.6      05 Dec 2000  MV      Protected use of CDialog                   **
//* 1.7      10 Jul 2001  MV      Debug fTraceFunctionCallDebug              **
//* 1.8      27 Nov 2001  MV      Added fCheckInputs                         **
//* 1.9      04 Dec 2001  MV      Debug fCheckInputs                         **
//* 1.10     26 Feb 2002  MV      fCheckInputs works with Html screens       **
//* 1.11     05 Mar 2002  MV      fPrintDebugSettingsManagement param        **
//* 1.12     13 Mar 2002  MV      fCheckInputs Dirty Patch                   **
//*                                                                          **
//* FILE **********************************************************************


// 0: no DEBUG, 1: minimal DEBUG, >1 more debug
var DEBUG = 0;
var DEBUG_COOKIE_NAME = 'PCS_JS_DEBUG_LEVEL';
var debugCookieDurationDays = 10;

var debugBuffers = new Array();
var debugPrefixe = new Array();

//*************************
// FUNCTIONS DECLARATIONS *
//*************************


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fSetDebugLevelCookie                                               **
//* GOAL: set JS DEBUG level in a cookie                                     **
//*                                                                          **
//***************************************************************** FUNCTION **
function fSetDebugLevelCookie(piValue)
{
  var val = parseInt(piValue);
  if (isNaN(val))
  {
    val = 0;
  }

  fSetCookie(DEBUG_COOKIE_NAME, val.toString(), debugCookieDurationDays);

} // fSetDebugLevelCookie

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintDebugSettingsManagement                                      **
//* GOAL: Print Html code to set JS DEBUG level                              **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintDebugSettingsManagement(piIsFormToBePrinted)
{
  var ret='';
  var cookieVal = GetCookie(DEBUG_COOKIE_NAME);

  ret+= 'JS Debug Level: <INPUT TYPE=TEXT NAME=DEBUGLEVEL VALUE="'+ ((cookieVal) ? cookieVal : '') +'" onChange="javascript:top.fSetDebugLevelCookie(this.value);">';
  ret+= '<INPUT TYPE=BUTTON NAME=SHOWCOOKIES VALUE="Show Cookies" onClick="javascript:alert(top.document.cookie);">';
  ret+= '<INPUT TYPE=BUTTON NAME=RESETCOOKIES VALUE="Delete ALL PCS Cookies" onClick="javascript:top.fDeleteCookiesOfprefixe(\'PCS_\');">';

  if (piIsFormToBePrinted)
  {
    ret = '<FORM>'+ ret +'</FORM>';
  }

  return ret;

} // fPrintDebugSettingsManagement


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fInitDebug                                                         **
//* GOAL: init DEBUG variable from piDebugLevel if defined                   **
//*       or from url search string, Ex: ?DEBUG=12                           **
//*       or from DEBUG_COOKIE_NAME if defined                               **
//*                                                                          **
//***************************************************************** FUNCTION **
function fInitDebug(piDebugLevel)
{
  if (typeof(piDebugLevel) !='undefined')
  {
    DEBUG = piDebugLevel;
  }
  else
  {
    var searchstr = window.location.search;
    if (searchstr.slice(0,7) == '?DEBUG=')
    {
      DEBUG = parseInt(searchstr.slice(7));
    }
    else
    {
      DEBUG = parseInt(GetCookie(DEBUG_COOKIE_NAME));
    }
  }

  // if (DEBUG) alert('JS DEBUG LEVEL: ' + DEBUG);

} // fInitDebug


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fDebug                                                             **
//* GOAL: Alert given text if DEBUG is defined                               **
//*       pilevel is the minimum DEBUG value to display this info            **
//*                                                                          **
//***************************************************************** FUNCTION **
function fDebug(piText, piLevel)
{
  if (DEBUG)
  {
    if (piLevel != undefined)
    {
      if (DEBUG<piLevel)
      {
        return;
      }
    }
     
    if (typeof(CDialog) != 'undefined')
    {
      alertDialog('<FONT face="helvetica,arial,sans-serif" color=darkblue size=-1>'+piText+'</FONT>');
    }
    else
    {
      alert(piText);
    }
  }
} // fDebug


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fShowHidden                                                        **
//* GOAL: Alert the content of all INPUT HIDDEN of the page form             **
//*                                                                          **
//***************************************************************** FUNCTION **
function fShowHidden(piForm)
{
  if (DEBUG)
  {
    var ret='';
    var i;

    for (i=0; i<piForm.length; i++)
    {
      if (piForm[i].type == 'hidden')
      {
        ret+= piForm[i].name + ' :   ' + piForm[i].value + '\n';
      }
    }

    alert(ret);
  }
} // fShowHidden

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fTraceDebug                                                        **
//* GOAL: concatenation of piBuffer in debugBuffers[piNumBuffer]             **
//*       trace all debug with pilevel < DEBUG. when no piLevel defined Trace**
//*                                                                          **
//***************************************************************** FUNCTION **
function fTraceDebug(piBuffer, piLevel, piNumBuffer)
{  
  if (DEBUG)
  {	
	  if ( (piLevel=='undefined') || (piLevel && piLevel>DEBUG) )
    {	
		  return;
    }

	  var numBuffer=piNumBuffer||0;

	  if (debugBuffers[numBuffer])
    {
	    debugBuffers[numBuffer]+=fPrintPrefixeDebug()+piBuffer;
    }
	  else
    {
	    debugBuffers[numBuffer]=fPrintPrefixeDebug()+piBuffer;
    }
	
	//alert(debugBuffers[numBuffer]);
  }    
} // fTraceDebug

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintDebug                                                        **
//* GOAL: return the debug buffer given (by default 0)          			       **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintDebug(piNumBuffer, piPrint)
{
  if (DEBUG)
  {
    var numBuffer = piNumBuffer||0;

    if (piPrint)
	  {
      if (typeof(CDialog) != 'undefined')
		  {
        alertDialog('<FONT face="helvetica,arial,sans-serif" color=darkblue size=-1>'+debugBuffers[numBuffer]+'</FONT>');       
		  }
		  else
		  {
		    alert(piText);
		  }
	  }
	
    return debugBuffers[numBuffer];
  }    
} // fPrintDebug

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fAddPrefixeDebug                                                   **
//* GOAL: Add a prefix in the list of prefix (stack management)	             **
//*                                                                          **
//***************************************************************** FUNCTION **
function fAddPrefixeDebug(piPrefixe)
{
  if (DEBUG)
  {
    debugPrefixe[debugPrefixe.length] = piPrefixe;
  }    
} // fAddPrefixeDebug

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fPrintPrefixeDebug                                                 **
//* GOAL: return the current prefix                                          **
//*                                                                          **
//***************************************************************** FUNCTION **
function fPrintPrefixeDebug()
{
  if (DEBUG)
  {
    var i=0;
    var msg='';

    for (i = 0; i < debugPrefixe.length; i++)
    {
      msg += debugPrefixe[i];
    }

    return msg;
  }   
} // fPrintPrefixeDebug

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fRemovePrefixeDebug                                                **
//* GOAL: Remove the last prefix added (stack management)                    **
//*                                                                          **
//***************************************************************** FUNCTION **
function fRemovePrefixeDebug()
{
  if (DEBUG)
  {
    debugPrefixe.splice(debugPrefixe.length-1, 1);
  }   
} // fRemovePrefixeDebug


//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fTraceFunctionCallDebug                                            **
//*                                                                          **
//* GOAL: Usefull to trace the function call			                           **
//*                                                                          **
//* PARAMETERS: Object function, level debug, num buffer				             **
//*                                                                          **
//* OUTPUT    : nameoffunction(param1, param2, ....)   				               **
//*                                                                          **
//***************************************************************** FUNCTION **
function fTraceFunctionCallDebug(piFunction, piLevel, piNumBuffer)
{
  if (DEBUG)
  {
	  var  msg='';

    if (piFunction)
    {      
	    msg += '<FONT color=blue>'+piFunction.name+'</FONT>(';
      if (piFunction.arguments)
      {
        for (var i = 0; i < piFunction.arguments.length;i++)
	      {
		      msg+=piFunction.arguments[i];
		      if (i != piFunction.arguments.length-1)
		      {
			      msg += ',';
		      }
	      }
      }
	    msg += ')'+fFinishLine();
    }
    else
    {
      msg += fPrintText('Error in fTraceFunctionCallDebug: piFunction is undefined', 'default', 'red') + fFinishLine();
    }

	  fTraceDebug(msg, piLevel, piNumBuffer);
  }   
} // fTraceFunctionCallDebug

//* FUNCTION ******************************************************************
//*                                                                          **
//* NAME: fCheckInputs                                                       **
//* GOAL: Check all inputs of given frame, for onchange/onclick method       **
//*       If piFrameName param is undefined, assume it is a Html screen      **
//*       return true if every input has it                                  **
//*                                                                          **
//***************************************************************** FUNCTION **
function fCheckInputs(piFrameName)
{
  var theform;
  if (piFrameName)
  {
    // MultiFrame screen
    theform = top.frames[piFrameName].document.forms[0];
  }
  else
  {
    // Simple Html screen
    theform = top.document.forms[0];
  }

  if (theform && theform != undefined && typeof(theform) != 'undefined')
  {
    var allright = true;
    var i;

    for (i=0; i<theform.length; i++)
    {
      var inp = theform.elements[i];
      if (inp.type != 'hidden')
      {
        if (inp.onchange == undefined && inp.onclick == undefined)
        {
          // initially checked item of the radio has no onChange/onClick handler sometimes, it's normal
          if (inp.type != 'radio' || !inp.checked)
          {
            // Exclude special inputs which does not modify the screen status
            if (
                 !((top.screenName == 'NPR128' || top.screenName == 'NPR129') && (inp.name == 'SELECT_ACTIVITY' || inp.name.substr(0,7) == 'NATURE_'))
              && !(top.fGetInput('NPR141'))
              && !(top.fGetInput('NPR20'))
              && !((top.screenName == 'NPR71') && (inp.name == 'choice1' || inp.name == 'choice2'))
              )
            {
              fDebug('PCS JS Warning:\n'+ inp + "\n"+ inp.name +  '\nhas no OnChange handler !', 10);
              allright = false;
            }
          }
        }
      }
    }

    if (!allright)
    {
      // Conditionnal Save functionalty is deactivated because onChange is not fully reliable on all configs (laptop bug)
      fDebug('onChange Bug detected => Conditional Save deactivated.');
      setScreenModif(true);

      // MV: Dirty Patch, to do the same thing with PC Sales flags which may not work on some lapTop PCs. Should be deleted when the bug identified
      if (top.screenName == 'NPR52_RW')
      {
        top.fSetHiddenValue('salesForecastYearChanged', 1);  
        top.fSetHiddenValue('salesForecastCostChanged', 1);
        top.fSetHiddenValue('packageSalesModif', 1);
        for (i=0; i<theform.length; i++)
        {
          var inp = theform.elements[i];
          if (inp.type == 'hidden')
          {
            if (inp.name.substr(0, 11) == 'salesModif_')
            {
              top.fSetHiddenValue(inp.name, 1);
            }
          }
        }
      } // End of Dirty Patch
    }
  }

} // fCheckInputs



