﻿// This JScript File should contain all javascript used by controls

// MUST NOT USE PROTOTYPE FUNCTIONS as this is included in clients websites 
// and prototype can clash with existing scripts.

// *************************************************************************** //
// This section of the JS file contains generic javascript functions for
// manipulating controls
// *************************************************************************** //

//****************************************
// Function:  PUControl_ShowHideControl
// Purpose:   This function hides or displays the control specified in sID, depending on the value of the 
//            control oControl, and the value of oControl, specified by sValue.
//            If the value of oControl and sValue match, the control sID is displayed
// Param:     oControl - the control the user is interacting with
//            sValue - value you want oControl to be so sID is shown, otherwise it is hidden
//            sID - ID of the control you want to show/hide
// Returns:   none
//****************************************
function PUControl_ShowHideControl( oControl, sValue, sID)
{
   // Find the object
   var oObject = document.getElementById(sID);

   // Check if the value of oControl equals sValue
   if (oControl.value == sValue)
   {
     // Show the object
     oObject.style.display = "";
   }
   else
   {
     // Hide the object
     oObject.style.display = "none";
   }
}


//****************************************
// Function:  PUControl_ClearContols
// Purpose:   This function clears the values in oClearControls, if the value oControl matches sValue
// Param:     oControl - the control the user is interacting with
//            sValue - Value of which you want the control to be for the contols in oClearContols to then be cleared
//            oClearContols - IDs of the controls you want to clear
// Returns:   none
//****************************************
function PUControl_ClearContols(oControl, sValue, oClearContols )
{
  // Check if the value of oControl equals sValue
  if (oControl.value == sValue)
  {
    // Loop through the controls and clear their values
    for ( var lCounter=0, lSize=oClearContols.length; lCounter<lSize; ++lCounter )
    {
      document.getElementById(oClearContols[lCounter]).value = "";
    }
  }
}

// *************************************************************************** //
// This section of the JS file contains javascript functions used by PUFORM
// *************************************************************************** //

//****************************************
// Function:  PUFORM_UpdateCharactersRemaining
// Purpose:   This function updates the character text that follows a text box or text area
//            informing a user how many characters remaining when entering text into a control
// Param:     oControl - the control the user is entering text into
//            lmaxLength - Number of characters that are allowed
//            sCharCountLabel - Text that is displayed saying how many characters have been entered 
// Returns:   True if a character can't be added
//****************************************
function PUFORM_UpdateCharactersRemaining(oControl, lmaxLength, sCharCountLabel)
{
  var bClipped = false;
  
  //Check if we have reached the character limit
  if (oControl.value.length > lmaxLength)
  {
    // Yes, truncate the input to the limit
    oControl.value = oControl.value.substring(0,lmaxLength)

    // Update the text to say how many characters remaining, hightlighted in red
    document.getElementById(oControl.name + '_lCharCountLabel').innerHTML = '<font color="red">' + sCharCountLabel + ':</font>'
    document.getElementById(oControl.name + '_lCharCounter').innerHTML = '<font color="red">0</font>'
    bClipped = true;
  }
  else
  {
    // No, Update the text to say how many characters remaining
    document.getElementById(oControl.name + '_lCharCountLabel').innerHTML = sCharCountLabel + ':'
    document.getElementById(oControl.name + '_lCharCounter').innerHTML = lmaxLength - oControl.value.length
  }
  return bClipped;
}

//****************************************
// Function:  PUDateField_ValidateDate(oDateField)
// Purpose:   Converts a date in the format or 12/5/1982 to 12 May 1982 so no confusion between USA and AUS dates
// Param:     oDateField - The text box that the date is displayed in
// Returns:   Nothing
//****************************************
  function PUDateField_ValidateDate(oDateField)
  {
    //used to determine which date to highlight on the calendar
    var bDateValid;
    bDateValid = false;
    var bFlipDate;
    var sDate;
    if(oDateField.value != '')
    {
      sDate = oDateField.value;
      //replace out .'s for dates formated 1.06.04
      var aMonths = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
      var aWeekdays = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
      sDate = sDate.replace(/[.]/g,"/");
      //remove days of week;
      sDate = sDate.replace("Monday ","");
      var x=0;
      for (x=0; x<7; x++)
      {
        sDate = sDate.replace(aWeekdays[x] + " ","");
        sDate = sDate.replace(aWeekdays[x].toLowerCase() + " ","");
        sDate = sDate.replace(aWeekdays[x].substr(0,3) + " ","");
        sDate = sDate.replace(aWeekdays[x].substr(0,3).toLowerCase() + " ","");
        sDate = sDate.replace(aWeekdays[x].substr(0,4) + " ","");
        sDate = sDate.replace(aWeekdays[x].substr(0,4).toLowerCase() + " ","");
      }
      var oDate = new Date(sDate)

      if(oDate.toString() != 'NaN')
      {
        bDateValid = true;
        var oRE = new RegExp('[^A-Za-z0-9_\s]');
        var aDelimiter = oRE.exec(sDate);
        // was a delimiter found?
        if(aDelimiter != null && aDelimiter.length > 0)
        {
          // yes, split the date
          var aDate = sDate.split(aDelimiter[0]);
          // are there 3 components?
          if(aDate.length == 3)
          {
            // yes, are the date and the month numeric
            if(!isNaN(aDate[0]) && !isNaN(aDate[1]) && !isNaN(aDate[2]))
            {
              oDateField.value = aDate[0] + ' ' + aMonths[aDate[1] - 1] + ' ' + aDate[2];
              return;
            }
          }
          else
          {
              return;
          }
        }
        else
        {
          bDateValid = false;
          return;
        }
        oDateField.value = oDate.getDate() + ' ' + aMonths[oDate.getMonth()] + ' ' + PUDateField_GetCorrectYear(oDate);
      }
      oDateField.blur;
    }
    else
    {
        bDateValid = true;
        return;
    }
  }

function PUDateField_GetCorrectYear(oDate)
{ 
    return (oDate.getYear() < 1000) ? oDate.getYear() + 1900 : oDate.getYear(); 
}

function PopulateSearchField(strSearchDescID, strSearchID, strClientID, strId, strDescription, strInfoTitle, strInfo, blnShowInfoPanel)
{
    document.getElementById(strSearchID).value=strId;
    document.getElementById(strSearchDescID).value=strDescription;

    if(blnShowInfoPanel)
    {
        if(strInfo == '')
        {
            document.getElementById(strSearchDescID + '_InfoDetailTitle').innerHTML=strInfoTitle
        }
        else
        {
            document.getElementById(strSearchDescID + '_InfoDetailTitle').innerHTML= "<a onClick=\"toggleFieldDetails('" + strClientID + "');return false;\" class=\"blueLink\">" + strInfoTitle + "</a>";
        }
        
        document.getElementById(strSearchDescID + '_InfoDetail').innerHTML=strInfo;
        document.getElementById(strSearchDescID + '_InfoDetailImage').style.display="";
    }
    else
    {
        document.getElementById(strSearchDescID).style.textDecoration = 'underline';
    }
}

Event.observe(window, 'load', function() {
    if(typeof Sys != 'undefined')
    {
        var pgRegMgr = Sys.WebForms.PageRequestManager.getInstance();
        pgRegMgr.add_endRequest(EndAsyncHandler);
    }
});

function EndAsyncHandler(pgRegMgr) {
    var panelsToRefresh = $(pgRegMgr._panelsToRefreshIDs);
    if (panelsToRefresh != null) {
        panelsToRefresh.each(function(panel) {
            //Because the panelsToRefresh only return .NET unique IDs 
            //we need to convert the ID to a client ID
            var index = pgRegMgr._updatePanelIDs.indexOf(panel);
            var ID = pgRegMgr._updatePanelClientIDs[index];
            PUCalendar.initWithinElement(ID);
        });
    }
}
function BeginAsyncHandler(pgRegMgr) {
}