// This function is called on the onload event being fired for every page
function initialisePage()
{
   try
   {
      if(START_FIELD!="")
         setFocus(START_FIELD);
   }
   catch(e)
   {
      // Ignore error
   }
}

// This function places the focus on the supplied object
function setFocus(fieldid)
{
   document.getElementById(fieldid).focus();
}

// This function is called by buttons on the forms
// any validation is applied and the form is submitted.
function submitForm(formid, actionurl, validate)
{
   _form = document.all(formid);
   if(validate)
   {
      if(validateForm(formid))
      {
         if(actionurl != "")
            _form.action = actionurl;
         _form.submit();
      }
   }
   else
      _form.submit();
}

// This function returns true/false depending on whether the data entry
// items have been populated correctly.
function validateForm(formid)
{
   _form = document.all(formid);
   _valid = true;
   _object = null;
   if(clearFailedValidation())
   {
      for(i=0;i<_form.all.length & _valid;i++)
      {
         _object = _form.all(i);

         try
         {
            _value = _object.validationstyle;

            switch(_value.toLowerCase())
            {
               case "textbox":
               {
                  _valid = validateTextbox(_object);
                  break;
               }
               case "numberbox":
               {
                  _valid = validateNumberbox(_object);
                  break;
               }
               case "datebox":
               {
                  _valid = validateDatebox(_object);
                  break;
               }
               case "combobox":
               {
                  _valid = validateCombobox(_object);
                  break;
               }
               default:
               {
                  break;
               }
            }
         }
         catch(e)
         {
            // Ignore error
         }
      }
   }

   // The form validation failed so display message label
   // associated with the data entry field.
   if(!_valid)
   {
      if(_object.failedvalidationmessage!="")
      {
         document.getElementById("lblFailedValidation").innerText = "Form Error: " + _object.failedvalidationmessage;
         document.getElementById("tblFailedValidation").style.display = "block";
      }
      _object.focus();
   }

   return _valid;
}

// This function simply resets the failed validation
// message box, returns true if the object exists and false if not
function clearFailedValidation()
{
   _result = true;
   try
   {
      _object = document.getElementById("lblFailedValidation");
      _object.value = "";

      _object = document.getElementById("tblFailedValidation");
      _object.style.display = "none";
   }
   catch(e)
   {
      _result = false;
   }
   return _result;
}

// This function validates a text to ensure that it has
// been populated with some data, not just SPACES!
function validateTextbox(textbox)
{
   textbox.value = trim(textbox.value);
   return(textbox.value!="");
}

// This function takes the supplied value and returns true if it is a valid number
// and false if it is not.
function validateNumberbox(numberbox)
{
   return!(isNaN(parseFloat(numberbox.value)));
}

// Validates a date entry
function validateDatebox(dateinput)
{
   try
   {
      _date = dateinput.value;
      _result = (Date.parse(_date)>0);
   }
   catch(err)
   {
      _result = false;
   }

   return _result;
}

function validateCombobox(comboinput)
{
   try
   {
      _result = (comboinput.selectedIndex > -1);
   }
   catch(err)
   {
      _result = false;
   }
   return _result;
}

// Validates a time entry
function validateTimebox(timeinput)
{
  return /^([01]?[0-9]|[2][0-3])(:[0-5][0-9])?$/.test(timeinput)
}


// This funtion removes the leading and trailing spaces from a String object.
function trim(str)
{
   var newitem = "" + str;

   //Remove spaces from start
   while((newitem.substr(0,1)==" ") && (newitem.length>0))
   newitem=newitem.substr(1);

   //Remove spaces from end
   while((newitem.substr(newitem.length-1,1)==" ") && (newitem.length>0))
   newitem=newitem.substr(0,newitem.length-1);

   return newitem;
}

// This function takes the supplied number and rounds it to the specified number of decimal places
function roundNumber(num, places)
{
   if (places > 0)
   {
        if ((num.toString().length - num.toString().lastIndexOf('.')) > (places + 1))
        {
            var Rounder = Math.pow(10, places);
            return Math.round(num * Rounder) / Rounder;
        }
        else return num;
   }
   else return Math.round(num);
}