/******************************************************************************
**                                                                            
** Name:    UseFul.js  
**                                                                            
** Purpose: Contains various JScript utility functions (100% Java-script).                       
**                                                                            
**-----------------------------------------------------------------------------
**                      CHANGE HISTORY
**   Change No:   Date:          Author:   Description:
**   _________    ___________    ______    ____________________________________
**      001       25-Oct-2002    VBeXpress Created.
**                                         
******************************************************************************/


//
// Name:    trim
// Purpose: Removes leading and trailing spaces from a string
//
function trim(item) {
worker = new String(item);

  if (worker.indexOf(" ") != -1) {
    if (worker.indexOf(" ") == 0) {
      worker = worker.substring(1,worker.length);
      trim(worker);
    }
  }
  if (worker.lastIndexOf(" ") != -1) {
    if (worker.lastIndexOf(" ") >= worker.length-1) {
      worker = worker.substring(0,worker.length-1);
      trim(worker);
    }
  }
  return worker;
}


//
// Name:    ProcessError
// Purpose: Displays details of a JScript error to the user.
//
function ProcessError(ModuleName, ProcedureName, Error) {
   var msg = new String();
   msg = msg + 'A client-side JScript error has occurred. The details are as follows:\n'
   msg = msg + '\n' + '  Module:             ' + ModuleName;  
   msg = msg + '\n' + '  Proceedure:      ' + ProcedureName;
   msg = msg + '\n' + '  ErrorNumber:   ' + Error.number;
   msg = msg + '\n' + '  Message:          ' + Error.message;
   msg = msg + '\n\nPlease record these details and contact your system administrator.\nThis error has NOT been logged to the database.' 
   alert(msg);
}   


//
// Name:    ClearSearchControls
// Purpose: Clears the search controls on a search panel
//
function ClearSearchControls() {
   try
   {    
      oCtrls = document.forms[0].elements;
      for (i=0 ; i <= oCtrls.length - 1 ; i++)
      {
         if (oCtrls(i).type == 'text')  {
            oCtrls(i).value='';
         }
         if (oCtrls(i).type == 'select-one') {
		    oCtrls(i).selectedIndex = 0;
         }
      } 
   }
   catch ( exception )
   {
      ProcessError('UseFul.js', 'ClearSearchControls()', exception);
   }
}


//
// Name:    ValidateForBlank
// Purpose: Ensures that a textbox contains a non-blank value.
//          If not, an alert is displayed to the user.
//
function ValidateForBlank(txt, lbl) {
   try
   {
      if (trim(txt.value) == "") {
         alert('You must enter ' + lbl + '.');
	     txt.value = '';
         txt.focus();
	     return false;
	  }
	  return true;
   }
   catch ( exception )
   {
       ProcessError('UseFul.js', 'ValidateForBlank', exception);
       return false;
   }
}



//
// Name:    PostXML
// Purpose: Posts an XML string to the web server via xmlhttppost. 
//          Checks that the web server returns 'true'.
// Parms:   Page    - the page that we are posting to
//          xnlData - the XML data that will be posted  
function PostXML(page, xmlData) {
   try
   {
      var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      
      xmlhttp.Open("POST", page, false);
      xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xmlhttp.send(xmlData);
      
      var response = xmlhttp.responseXML.xml.toString().toUpperCase();
      if (response == '') 
      {
          alert('An internal error occurred when adding the record. Please try the operation again.');
          return false;
      }
      var s = response.indexOf("TRUE");
      if (s == -1) {
         alert('An internal error occurred when adding the record. Please try the operation again.');
      	 return false;
      }
      
      return true;
   }
   catch ( exception )
   {
       ProcessError('UseFul.js', 'PostXML', exception);
       return false;
   }
}  



//
// Name:    window_onkeydown
// Purpose: Used for dialog boxes. If user presses <enter> invoke the OK button.
//          If user pressed <ESC> invoke the Cancel button.
function window_onkeydown() {
   try
   {
      if (event.keyCode == 13) {
	     event.returnValue=false;
		 cmdOK_onclick();
	  }
	  if (event.keyCode == 27) {
         event.returnValue=false;
    	 cmdCancel_onclick();
      }
      return true;
   }
   catch ( exception )
   {
       ProcessError('UseFul.js', 'window_onkeydown', exception);
       return false;
   }
}