/*Change Log*/
/*
20060210:ER:0.0.101: Added progress bar functionality
*/

/*************************************************************************************************************
                                                 OBJECT AND OBJECT PROPERTIES
*************************************************************************************************************/

/*
Returns the object with id objId
*/
function GetObject(objId)
{
  if (document.getElementById) {
    return document.getElementById(objId);
  } 
  
  alert("document.getElementById not supported");
  
  return 0;
}

/*
Returns the absolute X position of object obj
*/
function GetPosX(obj)
{
  var left = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      left+=obj.offsetLeft;
      obj=obj.offsetParent;
    }
  } else if (obj.x) {
    left+=obj.x;
  }
  return left;
}

/*
Returns the absolute Y position of object obj
*/
function GetPosY(obj)
{
  var top = 0;
  if (obj.offsetParent)	{
    while (obj.offsetParent) {
      top+=obj.offsetTop;
      obj=obj.offsetParent;
    }
  } else if (obj.y) {
    top+=obj.y;
  }
  return top;
}

/*
Returns the width of object obj
*/
function GetWidth(obj)
{
  if (obj.offsetWidth) {
    return obj.offsetWidth;
  }
  
  alert("obj.offsetWidth not supported");
  
  return 0;
}

/*
Returns the height of object obj
*/
function GetHeight(obj)
{
  if (obj.offsetHeight) {
    return obj.offsetHeight;
  }
  
  alert("obj.offsetHeight not supported");
  
  return 0;
}

/*************************************************************************************************************
                                                  COOKIE RELATED
*************************************************************************************************************/
/*
Attempts to set a cookie according to the passed on parameters.
*/
function CreateCookie(name,value,days)
{
 if (days) {
    var date=new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  } else {
    var expires = "";
   }
   document.cookie = name+"="+escape(value)+expires+"; path=/";
}

/*
Returns the value of cookie "name" or "null" if the cookie does not exist or cannot be obtained
*/
function ReadCookie(name)
{
  var nameEQ=name+"=";
  var ca=document.cookie.split(';');
  for (var i=0;i < ca.length;i++)
  {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
  }
  return null;
}

function EraseCookie(name)
{
  CreateCookie(name,"",-365);
}

/*
Returns true if cookies are enabled
*/
function CookiesEnabled()
{
  var randomSuffix=Math.floor(Math.random()*1000000);
  var randomValue=Math.floor(Math.random()*1000000);
  
  CreateCookie("test"+randomSuffix,randomValue,0);
  
  if (ReadCookie("test"+randomSuffix)!=randomValue) {
    return false;
  }
 
  EraseCookie("test"+randomSuffix);
  return true;
}


/*************************************************************************************************************
                                                  MENU RELATED
*************************************************************************************************************/

/*
Loads menu with main menu tkey "amm" (Active Main Menu), sub menu tkey "asm" (Active Sub Menu) and function menu
"afm" (Active Function Menu) in target "target" (main window/submit factory). 

Menu actions are those menu items that need to be executed in the submit factory, so all menu items with a defined
(setup or fixed) target of __SUBMIT_FACTORY__ will be present in the array "menuActions", indexed by the menu
item's tkey. An example is the "Go to login page" function menu in the error main menu
*/
function ShowMenu(amm,asm,afm,target,actionParameter)
{
  var cookieError=false;
  var actionIndex=0;
  
  if (window.menuActions[amm]) {
    actionIndex=amm;
  } else  if (window.menuActions[asm]) {
    actionIndex=asm;
  } else  if (window.menuActions[afm]) {
    actionIndex=afm;
  }
  
  /*Erase the help cookie*/
  EraseCookie(__PROGRAM_PREFIX__+"Help");
  
  /*Set the list reset indicator. This will delete the temporary list settings*/
  CreateCookie(__PROGRAM_PREFIX__+"ListReset","1",365);
   
  /*There is always an active main menu*/
  CreateCookie(__PROGRAM_PREFIX__+"MainMenu",amm,0);
    
  /*If there is no active sub menu, we let the menu system decide the default asm according to the
  user's access rights. This would happen when a user clicks on a main menu tab*/
  if (asm!=0) {
    CreateCookie(__PROGRAM_PREFIX__+"SubMenu",asm,0);
  } else {
    /*Go for the default asm*/
    EraseCookie(__PROGRAM_PREFIX__+"SubMenu");
  }
  
  EraseCookie(__PROGRAM_PREFIX__+"ContentIncludeFile");
    
  /*If there is no active function menu, we let the menu system decide the default afm according to the
  user's access rights. This would happen when a user clicks on a sub menu tab*/
  if (afm!=0){
    CreateCookie(__PROGRAM_PREFIX__+"FunctionMenu",afm,0);
  } else {
    /*Go for the default afm*/
    EraseCookie(__PROGRAM_PREFIX__+"FunctionMenu");
  }
  
   
  /*If we're already in an error situation, we need to pass the error information on*/
  var urlErrorSuffix="";
  if (__ERROR_CATEGORY__!="") {
    urlErrorSuffix="&errorCategory="+__ERROR_CATEGORY__+"&errorUniqueId="+__ERROR_UNIQUE_ID__;
  }
   
   /*Load the appropriate target*/
  if (target==__MAIN_WINDOW__) {
    parent.location="admin.php?language="+__LANGUAGE__+"&rows="+__ROWS__+urlErrorSuffix;
  } else if (target==__SUBMIT_FACTORY__) {
    parent.submit_factory.location=__BASE_URL__+"/backend/php/"+menuActions[actionIndex]+"?language="+__LANGUAGE__+"&rows="+__ROWS__+urlErrorSuffix;
    return;
  } else {
    alert("Invalid menu target");
    return;
  }
}

/*
This timer determines how long after mouseout the language window will be closed
*/
function StartLanguageMenuTimer()
{
  /*Set the timer to close the language menu after 250ms*/
  if (!window.__timerIds__) {
    return;
  }
  
  __timerIds__["languageTimer"]=setInterval("CloseLanguageMenu()",250);
}

/*
Opens the language menu following a click on the flag
*/
function OpenLanguageMenu()
{
  StopTimer("languageTimer");
  var flagObj=GetObject("languageFlag");
  var flagX=GetPosX(flagObj);
  var flagWidth=GetWidth(flagObj);
  var contentY=GetPosY(GetObject("content"));
  
  var languageObj=GetObject("languageSelect");
  var languageWidth=GetWidth(languageObj);
  
  languageObj.style.top=contentY+"px";
  languageObj.style.left=Number(flagX-languageWidth+flagWidth)+"px";
  languageObj.style.visibility="visible";
}

/*
Closes the language menu
*/
function CloseLanguageMenu()
{
  StopTimer("languageTimer");
  var languageObj=GetObject("languageSelect");
  languageObj.style.visibility="hidden";
  languageObj.style.left="0px";
  languageObj.style.top="0px";
}

/*
Highlight the "mouseover" language
*/
function HighlightLanguage(language)
{
  StopTimer("languageTimer");
  lNameObj=GetObject("language_"+language+"_name");
  lFlagObj=GetObject("language_"+language+"_flag");
  lNameObj.style.backgroundColor="gray";
  lNameObj.style.color="white";
  lFlagObj.style.backgroundColor="gray";
}

/*
Lowlights the "mouseover" language
*/
function LowlightLanguage(language)
{
  StopTimer("languageTimer");
  StartLanguageMenuTimer();
  lNameObj=GetObject("language_"+language+"_name");
  lFlagObj=GetObject("language_"+language+"_flag");
  lNameObj.style.backgroundColor="#3687d9";
  lNameObj.style.color="black";
  lFlagObj.style.backgroundColor="#3687d9";
}

/*
Changes the language to "language"
*/
function ChangeLanguage(language)
{
  CloseLanguageMenu();
  StopHelpTextTimer("helpTextTimer");
  
  /*If we're currently using the required language, we're done*/
  if (language==__LANGUAGE__) {
    ShowFlashMessage("usingThisLanguage","__flashMessages__",__STATUS_ICON_WARNING__);
    return;
  }
    
  /*Change the language in the user settings cookie*/
  var settings=new String();
  settings=ReadCookie(__PROGRAM_PREFIX__+"Settings");

  if (settings.indexOf("language=")==-1) {
    if (settings!="") {
      settings+="~";
    }
    settings+="language="+language;
  } else {
    settings=settings.replace(/(language=){1}([a-z]{2})/,"language="+language);
  }
  
  /*User settings cookie valid for one year*/
  CreateCookie(__PROGRAM_PREFIX__+"Settings",settings,365);
  
  /*Load the page in the new language*/
  var l=__BASE_URL__+"/backend/php/admin.php?language="+language+"&rows="+__ROWS__;
  if (__ERROR_CATEGORY__!="") {
    l+="&errorCategory="+__ERROR_CATEGORY__+"&errorUniqueId="+__ERROR_UNIQUE_ID__;
  }
  
  document.location=l;
}

function ShowRelatedHelpTopic(helpTopic)
{
  CreateCookie(__PROGRAM_PREFIX__+"Help",helpTopic,0);
  document.location=__BASE_URL__+"/backend/php/admin.php?language="+__LANGUAGE__+"&rows="+__ROWS__;
}

function DisplayHelp()
{
  StopHelpTextTimer("helpTextTimer");
  if (!window.__helpFile__) {
    ShowFlashMessage("noHelpAvailable","__flashMessages__",__STATUS_ICON_WARNING__);
    return;
  }
  
  CreateCookie(__PROGRAM_PREFIX__+"Help",__helpFile__,0);
  document.location=__BASE_URL__+"/backend/php/admin.php?language="+__LANGUAGE__+"&rows="+__ROWS__;
}

function CloseHelp()
{
  StopHelpTextTimer("helpTextTimer");
  EraseCookie(__PROGRAM_PREFIX__+"Help");
  document.location=__BASE_URL__+"/backend/php/admin.php?language="+__LANGUAGE__+"&rows="+__ROWS__;
}

/*************************************************************************************************************
                                           STATUS, HELP AND FLASH MESSAGE RELATED
*************************************************************************************************************/

/*
Start the help text timer to show help text message "index" from array "helpTextArray. The timer will fire after
2 seconds until stopped by StopHelpTextTimer.

Note that in a way, the help text is also a flash message, but "in reverse": a help text message is only displayed
after so many seconds, whereas a flash message is displayed for so many seconds.
*/
function StartHelpTextTimer(index,helpTextArray)
{
  
  if (!StopTimer("helpTextTimer")) {
    return;
  }
  
 
  try {
    eval("var helpText="+helpTextArray+"[\""+index+"\"];");
  }
  
  catch(e) {
    return;
  }
  
  var contextHelp=GetObject("contextHelp");
  contextHelp.style.left="0px";
  contextHelp.style.top="0px";
  contextHelp.innerHTML="<table style=\"font-family: Tahoma, Arial; font-size: 10pt; font-weight: bold; border: 1px solid black; color: black; background-color: #ffffcc;\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr><td nowrap id=\"contextHelpText\" style=\"padding: 0px 2px 0px 2px;\">"+helpText+"</td></tr></table>";
 
  /*Set the timer to show the help text after 2 seconds*/
  __timerIds__["helpTextTimer"]=setInterval("ShowHelpText(\""+index+"\",\""+helpTextArray+"\")",2000);
}

/*
Stop the active help text timer
*/
function StopHelpTextTimer()
{

  /*Stop the helptext timer and display the current status message*/
  if (!StopTimer("helpTextTimer")) {
    return;
  }
  var contextHelp=GetObject("contextHelp");
  contextHelp.style.visibility="hidden";
  contextHelp.style.left="0px";
  contextHelp.style.top="0px";
}

/*
Stop timer with id "timerId"
*/
function StopTimer(timerId)
{
  if (!window.__timerIds__) {
    return false;
  }
  
  if (__timerIds__[timerId]) {
    clearInterval(__timerIds__[timerId]);
    __timerIds__[timerId]=null
  } 
  
  return true;
}

/*
Show the help text with index "index" in help text array "helpTextArray" using icon "icon"

"icon" defaults to __STATUS_ICON_HELP__
*/
function ShowHelpText(index,helpTextArray)
{
  
  /*Remove and/or clear the index helpTextTimer from __timerIds__*/
  if (!StopTimer("helpTextTimer")) {
    return false;
  }

  var contextHelp=GetObject("contextHelp");
  var contextHelpText=GetObject("contextHelpText");
  var contextHelpX=__mouseMoveEventX__;
  var contextHelpY=__mouseMoveEventY__;
   
  /*We need to decide whether or not to display the context help text to the left
  of the cursor or to the right (top/bottom). This depends on the cursor position 
  and the width (height) of the help text*/
  var midScreenX=(GetWindowInnerWidth()>>1);
  var contextWidth=GetWidth(contextHelpText);
  var contextHeight=GetHeight(contextHelpText);
    
  if (contextHelpX>midScreenX) {
    /*On the right side of the screen*/
    if ((contextHelpX-contextWidth-5)>0) {
      contextHelp.style.left=(contextHelpX-contextWidth-5)+"px";
    } else {
      contextHelp.style.left="2px";
    }
  } else {
    /*On the left side of the screen*/
    if ((contextHelpX+contextWidth)<GetWindowInnerWidth()) {
      contextHelp.style.left=(contextHelpX-5)+"px";
    } else {
      contextHelp.style.left=(GetWindowInnerWidth()-contextWidth)+"px";
    }
  }
  
  /*Only if the context help doesnt fit on the screen, we put it above the pointer
  otherwise below*/
  if ((contextHelpY+contextHeight*6)>GetWindowInnerHeight()) {
    /*Above the pointer*/
    contextHelp.style.top=(contextHelpY-contextHeight-12)+"px";
  } else {
    /*Below the pointer*/
    contextHelp.style.top=(contextHelpY+10)+"px";
  }

  contextHelp.style.visibility="visible";
}

/*Returns the x position of a mouse event (must be captured first), relative
to the browser window. Used in calculating the correct help text position*/
function GetEventX(e)
{
  if (!e) {
    var e = window.event;
  }
  
  if (e.pageX) {
    return e.pageX;
  }
  
  return e.clientX+document.body.scrollLeft;
}

/*Returns the y position of a mouse event (must be captured first), relative
to the browser window. Used in calculating the correct help text position*/
function GetEventY(e)
{
  if (!e) {
    var e = window.event;
  }
  
  if (e.pageY) {
    return e.pageY;
  }
  
  return e.clientY;
}

/*Returns the inner width of the browser window. Used in calculating the correct 
help text position*/
function GetWindowInnerWidth()
{
  if (document.body.offsetWidth) {
    return document.body.offsetWidth;
  } 
  
  return window.innerWidth;
}

/*Returns the inner height of the browser window. Used in calculating the correct 
help text position*/
function GetWindowInnerHeight()
{
  if (document.body.offsetHeight) {
    return document.body.offsetHeight;
  } 
  
  return window.innerHeight;
}

/*Customized mouse move event handler. This function will continue to
set __mouseMoveEventX__ and __mouseMoveEventY__ to the actual cursor 
position.  Used in calculating the correct help text position*/
function MouseMoveEvent(e)
{
  __mouseMoveEventX__=GetEventX(e);
  __mouseMoveEventY__=GetEventY(e);
}

/*Capture the mouse move event.  Used in calculating the correct 
help text position*/
document.onmousemove=MouseMoveEvent;

/*
Shows flash message with index "index" in flash message array "flashMessageArray" using icon "icon"

When a flash message is showing, no help text will be displayed. Flash messages take priority over help text
*/
function ShowFlashMessage(index,flashMessageArray,icon)
{
  if (!StopTimer("flashTimer")) {
    return;
  }
      
  /*Display the requested flash message text*/
  var statusIconObj=GetObject("statusIcon");
  var statusTextObj=GetObject("statusText");
  eval("var helpText="+flashMessageArray+"[\""+index+"\"];");
  statusTextObj.innerHTML=helpText;
  if (icon) {
    statusIconObj.innerHTML="<img src=\""+icon+"\" border=\"0\">";
  } else {
    statusIconObj.innerHTML="<img src=\""+__STATUS_ICON_HELP__+"\" border=\"0\">";
  }
 
  __timerIds__["flashTimer"]=setInterval("HideFlashMessage()",1000);
}

/*
Hides the currently showing flash message and replace it with the status message that was displaying (if any) before
the flash message fired.
*/
function HideFlashMessage()
{
  if (!StopTimer("flashTimer")) {
    return;
  }
  DisplayCurrentStatusMessage();
}

/*
Displays the current status message (and icon) in the status area or clears the area if there are no
status messages displaying
*/
function DisplayCurrentStatusMessage()
{
  var statusIconObj=GetObject("statusIcon");
  var statusTextObj=GetObject("statusText");
  
  if (!window.__statusMessageDisplaying__) {
    return;
  }
  
  if (__statusMessageDisplaying__=="FALSE") {
    statusIconObj.innerHTML="<img width=\"32\" src=\""+__BASE_URL__+__ICON_BLANK__+"\" border=\"0\">";
    statusTextObj.innerHTML="&nbsp;";
    return;
  }
  
  statusIconObj.innerHTML="<img src=\""+__statusIcons__[Number(__currentStatusMessage__-1)]+"\" border=\"0\">";
  statusTextObj.innerHTML=__statusMessages__[Number(__currentStatusMessage__-1)];
}

/*
Return true if a status message is displaying
*/
function StatusMessageDisplaying()
{
  if (!window.__statusMessageDisplaying__) {
    return false;
  }
  
  return (!((__statusMessageDisplaying__=="FALSE") || (__numStatusMessages__==0)));
}

/*
Hides the current status message
*/
function HideStatusMessage()
{
  if (!window.__statusMessageDisplaying__) {
    return;
  }
  
  if ((__statusMessageDisplaying__=="FALSE") || (__numStatusMessages__==0)) {
    ShowFlashMessage("noMessageToHide","__flashMessages__",__STATUS_ICON_WARNING__);
    return;
  }
    
  __statusMessageDisplaying__="FALSE";
  __currentStatusMessage__="-";
  
  DisplayCurrentStatusMessage();
  
  var statusIndicatorObj=GetObject("statusIndicator");
  statusIndicatorObj.innerHTML=__currentStatusMessage__+"&nbsp;/&nbsp;"+__numStatusMessages__;
}

/*
Show the previous status message from the stack
*/
function ShowPreviousStatusMessage()
{
  if (__numStatusMessages__==0) {
    ShowFlashMessage("noMessageToShow","__flashMessages__",__STATUS_ICON_WARNING__);
    return;
  }
  
  if (!window.__statusMessageDisplaying__) {
    return;
  }
  
  if (__statusMessageDisplaying__=="FALSE") {
    __currentStatusMessage__=__numStatusMessages__;
  } else {
    __currentStatusMessage__--;
    if (__currentStatusMessage__==0) {
      __currentStatusMessage__=1;
      ShowFlashMessage("firstMessageShowing","__flashMessages__",__STATUS_ICON_WARNING__);
      return;
    }
  }
  
  __statusMessageDisplaying__="TRUE";
  DisplayCurrentStatusMessage();
  var statusIndicatorObj=GetObject("statusIndicator");
  statusIndicatorObj.innerHTML=__currentStatusMessage__+"&nbsp;/&nbsp;"+__numStatusMessages__;
  
}

/*
Show the next status message from the stack
*/
function ShowNextStatusMessage()
{
  if (__numStatusMessages__==0) {
    ShowFlashMessage("noMessageToShow","__flashMessages__",__STATUS_ICON_WARNING__);
    return;
  }
  
  if (!window.__statusMessageDisplaying__) {
    return;
  }
  
  if (__statusMessageDisplaying__=="FALSE") {
    __currentStatusMessage__=1;
  } else {
    __currentStatusMessage__++;
    if (__currentStatusMessage__>__numStatusMessages__) {
      __currentStatusMessage__=__numStatusMessages__;
      ShowFlashMessage("lastMessageShowing","__flashMessages__",__STATUS_ICON_WARNING__);
      return;
    }
  }
  
  __statusMessageDisplaying__="TRUE";
  DisplayCurrentStatusMessage();
  var statusIndicatorObj=GetObject("statusIndicator");
  statusIndicatorObj.innerHTML=__currentStatusMessage__+"&nbsp;/&nbsp;"+__numStatusMessages__;
 
}

/*
Deletes the status message that is currently displaying
*/
function DeleteStatusMessage()
{
  if (__numStatusMessages__==0) {
    ShowFlashMessage("noMessageToDelete","__flashMessages__",__STATUS_ICON_WARNING__);
    return;
  }
  
  if (__currentStatusMessage__=="-") {
    ShowFlashMessage("selectMessageToDelete","__flashMessages__",__STATUS_ICON_WARNING__);
    return;
  }
 
  var updatedStatusMessages=new Array();
  var updatedStatusIcons=new Array();
  var updatedNumStatusMessages=0;
  
  for (var i=0; i<__numStatusMessages__; i++) {
  
    if (i==(__currentStatusMessage__-1)) {
      continue;
    }
    
    updatedStatusMessages[updatedNumStatusMessages]=__statusMessages__[i];
    updatedStatusIcons[updatedNumStatusMessages]=__statusIcons__[i];
    updatedNumStatusMessages++;
  }
  
  for (var i=0; i<__numStatusMessages__; i++) {
    __statusMessages__.pop();
    __statusIcons__.pop();
  }
  
  __statusMessages__=updatedStatusMessages;
  __statusIcons__=updatedStatusIcons;
  
  __numStatusMessages__=updatedNumStatusMessages;
  __currentStatusMessage__="-";
  
  __statusMessageDisplaying__="FALSE";
  
  if (__numStatusMessages__!=0) {
    __statusMessageDisplaying__="TRUE";
    __currentStatusMessage__=__numStatusMessages__;
  }
  
  var statusIndicatorObj=GetObject("statusIndicator");
  statusIndicatorObj.innerHTML=__currentStatusMessage__+"&nbsp;/&nbsp;"+__numStatusMessages__;
  
  var newStatusMessageCookie="";
  var newStatusIconCookie="";
  
  EraseCookie(__PROGRAM_PREFIX__+"StatusMessages");
  EraseCookie(__PROGRAM_PREFIX__+"StatusIcons");
  
  newStatusMessageCookie=__statusMessages__.join("~");
  newStatusIconCookie=__statusIcons__.join("~");
    
  if (__numStatusMessages__!=0) {
    CreateCookie(__PROGRAM_PREFIX__+"StatusMessages",newStatusMessageCookie,0);
    CreateCookie(__PROGRAM_PREFIX__+"StatusIcons",newStatusIconCookie,0);
  } 
    
  ShowFlashMessage("messageDeleted","__flashMessages__",__STATUS_ICON_SUCCESS__);
}

/*Adds a status message 'statusMessage' with corresponding icon 'statusIcon' to the
top of the status messages stack*/
function AddStatusMessage(statusMessage,statusIcon)
{
  /*Max 9 status messages, so if we go over that we need to delete the first one,
  and shift all the remaining status messages and icons one position back in the stack*/
  if (__numStatusMessages__==9) {
       
    /*Shift all the status messages and icons one position back, thereby removing
    the first one*/
    for (var i=1; i<9; i++) {
      __statusMessages__[i-1]=__statusMessages__[i];
      __statusIcons__[i-1]=__statusIcons__[i];
    }
    
     /*Remove the last message (is in there double now)*/
    __statusMessages__.pop();
    __statusIcons__.pop();
    
    /*And adjust the number of status messages to reflect the new state*/    
    __numStatusMessages__=8;
  } 
  
  /*Add the new status message and icon on top of the stack*/
  __statusMessages__.push(statusMessage);
  __statusIcons__.push(statusIcon);
  
  /*Pretend we __were__ displaying the last message in the stack*/
  __currentStatusMessage__=__numStatusMessages__;
  /*We've added a status message*/
  __numStatusMessages__++;
  
  __statusMessageDisplaying__="TRUE";
  /*Simply display the next status message, since we were pretending that we were 
  displaying the last message, so the new message automatically becomes the 'next'*/
  ShowNextStatusMessage();
  
  /*Adjust the corresponding cookies*/
  CreateCookie(__PROGRAM_PREFIX__+"StatusMessages",__statusMessages__.join("~"),0);
  CreateCookie(__PROGRAM_PREFIX__+"StatusIcons",__statusIcons__.join("~"),0);
}

/*************************************************************************************************************
                                                    STORED SETTINGS
*************************************************************************************************************/

/*Change the setting 'name' to 'value' and give the corresponding cookie name 'settings' 
a life of 'cookieLife'*/
function ChangeStoredSetting(settings,name,value,cookieLife)
{
  var storedSettings=new Array();
  var arrayKeys=new Array();
  var details=new Array();
  
  /*Get the value of cookie 'settings'*/
  var cv=ReadCookie(settings);
  
  /*Format is: name1=value1~name2=value2~....., so split it up in name/value pairs*/
  if (cv) {
    details=cv.split("~");
  }
        
  /*Split each name/value pair up in the corresponding variables and their values*/
  for (var i=0; i<details.length; i++) {
  
    if (details[i]=="") {
      continue;
    }
    
    /*Split the variable/value pairs*/
    var settingsDetails=new Array();
    settingsDetails=details[i].split("=",2);
    storedSettings[settingsDetails[0]]=settingsDetails[1];
    
    /*Maintain the array keys, so we can reconstruct back the new cookie value*/
    arrayKeys[i]=settingsDetails[0];
  }
  
  /*If 'name' does not exist, it's a new setting*/
  if (!storedSettings[name]) {
    arrayKeys.push(name);
  }
   
  /*Assign (or change) the value of 'name'*/  
  storedSettings[name]=String(value);
  
  var newSettings="";
  
  /*Construct the new settings*/
  for (var i=0; i<arrayKeys.length; i++) {
  
    if (storedSettings[arrayKeys[i]]=="") {
      continue;
    }
     
    if (newSettings!="") {
      newSettings+="~";
    }
    
    newSettings+=arrayKeys[i]+"="+storedSettings[arrayKeys[i]];
  }
  
  /*If all the settings have been removed, erase the cookie, otherwise update the cookie*/
  if (newSettings=="") {
    EraseCookie(settings);
  } else {
    CreateCookie(settings,newSettings,cookieLife);  
  }
}

/*Delete 'name' from 'settings' and give the resulting cookie a life 
of 'cookieLife'*/
function DeleteStoredSetting(settings,name,cookieLife)
{
  /*Get the cookie value*/
  var cv=ReadCookie(settings);
  
  /*If the cookie does not exist, we're done*/
  if (!cv) {
    return;
  }
  
  var storedSettings=new Array();
  var arrayKeys=new Array();
  
  /*Format is: name1=value1~name2=value2~....., so split it up in name/value pairs*/
  var details=cv.split("~");
  
  for (var i=0; i<details.length; i++) {
  
    if (details[i]=="") {
      continue;
    }
    
    /*Split the variable/value pairs*/
    var settingsDetails=new Array();
    settingsDetails=details[i].split("=",2);
    storedSettings[settingsDetails[0]]=settingsDetails[1];
    
    /*Maintain the array keys, so we can reconstruct back the new cookie value*/
    arrayKeys[i]=settingsDetails[0];
  }
  
  var newSettings="";
  
  /*Construct the new settings*/
  for (var i=0; i<arrayKeys.length; i++) {
    
    /*But exclude the to be deleted variable*/
    if (arrayKeys[i]==name) {
      continue;
    }
     
    if (newSettings!="") {
      newSettings+="~";
    }
    
    newSettings+=arrayKeys[i]+"="+storedSettings[arrayKeys[i]];
  }
  
  /*If all the settings have been removed, erase the cookie, otherwise update the cookie*/
  if (newSettings=="") {
    EraseCookie(settings);
  } else {
    CreateCookie(settings,newSettings,cookieLife);  
  }
}

/*Return the value of variable 'name' in 'settings' or the empty string if 'name'
does not exist*/
function GetStoredSetting(settings,name)
{
  var storedSettings=new Array();
  
  /*Get the cookie value*/
  var cv=ReadCookie(settings);

  /*If the cookie does not exist, we return the empty string*/
  if (!cv) {
    return "";
  } 
  
  /*Format is: name1=value1~name2=value2~....., so split it up in name/value pairs*/
  var details=cv.split("~");
  
  /*Go over each name/value pair and find 'name'*/         
  for (var i=0; i<details.length; i++) {
  
    if (details[i]=="") {
      continue;
    }
    
    /*Split the variable/value pairs*/
    var settingsDetails=new Array();
    settingsDetails=details[i].split("=",2);
    
    /*Return 'name'*/
    if (settingsDetails[0]==name) {
      return settingsDetails[1];
    }
  }
  
  /*'name' does not exist, return the empty string*/
  return "";
}

/*Added:20060210:ER: Added progress bar functionality*/
/*************************************************************************************************************
                                                    PROGRESS BAR
*************************************************************************************************************/

function GetProgressBar() {
  if (navigator.appName.indexOf ("Microsoft") !=-1) {
    return window["progressBar"];
  } else {
    return document["progressBar"];
  }
}

function StartPhpProgressBar()
{
  GetProgressBar().GotoFrame(1);
}

function StartCppProgressBar()
{
  GetProgressBar().GotoFrame(2);
}

function ClearProgressBar()
{
  GetProgressBar().GotoFrame(0);
}
/*End*/


/*************************************************************************************************************
                                                    VARIOUS
*************************************************************************************************************/
function strtrim() 
{
  return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

String.prototype.trim=strtrim;

function StripFormData(f)
{
  for (var i=0; i<f.elements.length; i++) {
    /*Form types text, password, file and textarea.*/
    if ((f.elements[i].type=="text") || (f.elements[i].type=="password") || (f.elements[i].type=="textarea")) {
      var v=f.elements[i].value;
      v=v.trim();
      f.elements[i].value=v;
    }
  }
}


  
  







