//////////////////////
// Provides a set of functions to be used through SIS admin area.
// Created in Phase 3 for better gloabl useage of JS between the
// various jsps.
//
// @author $Author: Kaine $
// @version Version: 1.0
///////////////////////
///////////////////////
//Function Index:
//validateAgencyForm
//validateServiceForm
//validateSurveyForm
//validateVacancyForm
//validateLanguageForm
//
//checkAgencyRegions
//checkCCCISCoverage
//nextVacancy
//areaSelected
///////////////////////


//Global values
//The object to focus on if there is a field not complying with mandatory settings
var m_focusObject;
//The entire message to use to laert the user with.
var m_message = "";
//used for various counting requirements :)
var m_count = 0;

var m_waiting = false;

//Agency Constants.
//Since we can't use SisConstants, we have to create our own JS Constants. Make
//sure these COINCIDE with sc!
var AGENCY_TYPE_ACF = "ACF";
var AGENCY_TYPE_SRF = "SRF";
var AGENCY_TYPE_ILU = "ILU";
var AGENCY_TYPE_SP  = "Other";
////
//Function: validateAgencyForm
//Desc: Runs through a list of fields deemed mandatory for SIS Phase Three
///
function validateAgencyForm(formName, p_agencyType)
{
  m_message = "";
  var l_proceed = true;
  m_focusObject = null;
  
  checkTextField(formName.f_agency_name, "You must enter an Agency Name");
  checkSelectBox(formName.f_service_provider_type, "1", "You must enter an Agency Type");
  checkSelectBox(formName.f_fee_type, "1", "You must enter a Fee Type");
  checkTextField(formName.f_agency_phone, "You must enter a Facility Phone Number");
  checkTextField(formName.f_agency_address_1, "You must enter a value in Address Line 1");
  checkTextField(formName.f_postal_address_1, "You must enter a value in Postal Address Line 1");   
  checkTextField(formName.f_postal_suburb, "You must enter a value in Postal Suburb");    
  checkTextField(formName.f_postal_postcode, "You must enter a value in Postal Postcode");   
  checkTextField(formName.f_postal_state, "You must enter a value in Postal State");   
  checkTextField(formName.f_custodian_name, "You must enter a Custodian Name");   
  checkTextField(formName.f_custodian_phone, "You must enter a Custodian Phone Number");   

  checkCCCISCoverage(formName, formName.f_cccis_coverage_nationwide, formName.f_cccis_coverage);
  
  checkMultiSelectBox(formName.f_selected_target_groups, "You must select at least ONE Tagret Group");
  checkTextField(formName.f_between_edits, "You must enter a value for Max. Between Edits");
  
  //////////////
  //Some Agency Specific requiremnts
  if(p_agencyType == AGENCY_TYPE_ACF)
  {
    checkSelectBox(formName.f_sort_region, "0", "You must select a Display Category");
  }
  else if(p_agencyType == AGENCY_TYPE_SRF)
  {
    checkSelectBox(formName.f_hacc_region, "0", "You must select a HACC Region");
  }
  else if(p_agencyType == AGENCY_TYPE_SP)
  {
    checkSelectBox(formName.f_sort_region, "0", "You must select a Display Category");
    checkSelectBox(formName.f_hacc_region, "0", "You must select a HACC Region");
    checkTextField(formName.f_agency_suburb, "You must enter a Suburb");
    checkTextField(formName.f_agency_postcode, "You must enter a Postcode");
    checkTextField(formName.f_agency_state, "You must enter a State");
  }

  
  if(m_message.length == 0)
    l_proceed = true;
  else
  {
    l_proceed = false;
    if(m_focusObject != null)
      m_focusObject.focus();
    alert('There are some Mandatory fields that are empty:\n' + m_message);
  }
  
  return l_proceed
}

///
//Function: validateServiveForm
//Desc: Used to validate the services forms (add_edit_service_X.jsp)
//Params:- p_form: Form name
//       - p_agencyType: 'acf', 'srf', 'other'
//       - p_isEdit: If editing, or adding (ediitng there is no category option
///
function validateServiceForm(p_form, p_agencyType, p_isEdit)
{
  m_message = "";
  var l_proceed = true;
  m_focusObject = null;
  
  if(!p_isEdit)
    checkSelectBox(p_form.category_id, "-None-", "You must select a Category");
  checkTextField(p_form.f_ser_fees, "You must enter a value for Fee for Service");
  
  if(p_form.f_ser_client_aged.checked       == false && 
     p_form.f_ser_client_dementia.checked   == false &&
     p_form.f_ser_client_disability.checked == false &&
     p_form.f_ser_client_mental.checked     == false &&
     p_form.f_ser_client_carers.checked     == false && 
     p_form.f_ser_client_postacute.checked  == false &&
     p_form.f_ser_client_veteran.checked    == false &&
     p_form.f_ser_client_general.checked    == false)
  {
    setFocusObjectByTabIndex(p_form.f_ser_client_aged);
    m_message += "- " + "You must select a Client focus option" + "\n";
  }
  
  if(p_form.f_ser_cultural_aboriginal.checked == false && 
     p_form.f_ser_cultural_ethnic.checked     == false &&
     p_form.f_ser_cultural_general.checked    == false)
  {
    setFocusObjectByTabIndex(p_form.f_ser_cultural_aboriginal);
    m_message += "- " + "You must select a Cultural focus option" + "\n";
  }
  
  if(m_message.length == 0)
    l_proceed = true;
  else
  {
    l_proceed = false;
    if(m_focusObject != null)
      m_focusObject.focus();
    alert('There are some Mandatory fields that are empty:\n' + m_message);
  }
  
  return l_proceed;
}

function validateMessage()
{
  if(m_message.length == 0)
    l_proceed = true;
  else
  {
    l_proceed = false;
    if(m_focusObject != null)
      m_focusObject.focus();
    alert('There are some Mandatory fields that are empty:\n' + m_message);
  }
  
  return l_proceed;
}

////
//Function: validateSurveyForm
//Desc: Used to validate the services forms (add_edit_survey_X.jsp)
//Params:- p_form: Form name
//       - p_agencyType: 'acf', 'srf', 'other'
//       - p_isEdit: If editing, or adding (ediitng there is no category option
///
function validateSurveyForm(p_form, p_agencyType)
{
  var l_proceed = true;
  
  m_message     = "";
  m_focusObject = null;
  
  //These fields are used for all survey_forms
  checkTextFieldAsNumber(p_form.f_total_beds, "You must enter a value for Total No. of Beds", "Total No. of Beds");
  checkTextFieldAsNumber(p_form.f_single_rooms, "You must enter a value for Number of Single Rooms", "Number of Single Rooms");
  
  //acf
  if(p_agencyType == AGENCY_TYPE_ACF)
  {
    checkTextFieldAsNumber(p_form.f_extra_service_beds, "You must enter a value for Number of Extra Service Beds", "Number of Extra Service Beds");
    checkTextFieldAsNumber(p_form.f_double_rooms, "You must enter a value for Number of Double Rooms", "Number of Double Rooms"); 
  }
  
  if(m_message.length == 0)
    l_proceed = true;
  else
  {
    l_proceed = false;
    m_focusObject.focus();
    alert('There are some Mandatory fields that are empty:\n' + m_message);
  }
  
  return l_proceed;
}

////
//Function: validateVacancyForm
//Desc: Used to validate the vacancies forms (add_edit_vacancy_X.jsp)
//Params:- p_form: Form name
//       - p_agencyType: 'acf', 'srf', 'other'
//       - p_isEdit: If editing, or adding (ediitng there is no category option
///
function validateVacancyForm(p_form, p_agencyType)
{
  var l_proceed = true;
  
  m_message     = "";
  m_focusObject = null;
  
  //Fields that are used on all vacancy forms
  checkSelectBox(p_form.f_vac_type, "-1", "You must select a Bed Type");
  checkSelectBox(p_form.f_vac_gender, "-1", "You must select a Gender");
  checkTextField(p_form.f_vac_comments, "You must enter a Comment");
  
  //Fields on specific forms
  //ACF
  if(p_agencyType == AGENCY_TYPE_ACF)
  {
    checkSelectBox(p_form.f_vac_high_low_type, "-1", "You must select a Vacancy type");
    checkSelectBox(p_form.f_vac_beds, "-1", "You must specify the Number of beds in room");
  }
  
  if(m_message.length == 0)
    l_proceed = true;
  else
  {
    l_proceed = false;
    m_focusObject.focus();
    alert('There are some Mandatory fields that need correction:\n' + m_message);
  }
  
  return l_proceed;
}

////
//Function: validateLanguageForm
//Desc: Used to validate the language forms (add_edit_language_X.jsp)
//Params:- p_form: Form name
//       - p_agencyType: 'acf', 'srf', 'other'
//       - p_isEdit: If editing, or adding (ediitng there is no category option
///
function validateLanguageForm(p_form, p_agencyType)
{
  var l_proceed = true;
  
  m_message     = "";
  m_focusObject = null;
  
  checkTextField(p_form.f_lang_staff, "You must enter a value for Staff");
  checkTextField(p_form.f_lang_males, "You must enter a value for Males");
  checkTextField(p_form.f_lang_females, "You must enter a value for Females");
    
  if(m_message.length == 0)
    l_proceed = true;
  else
  {
    l_proceed = false;
    m_focusObject.focus();
    alert('There are some Mandatory fields that need correction:\n' + m_message);
  }
  
  return l_proceed;
}

////
//Function: validateIluSiteForm
//Desc: Used to validate the Ilu Site Form
//Params:- p_form: Form name
///
function validateIluSiteForm(p_form)
{
  var l_proceed = true;
  
  m_message     = "";
  m_focusObject = null;
  
  checkTextField(p_form.f_site_name, "You must enter a Site Name");
  checkSelectBox(p_form.f_site_service_provider, "1", "You must select an option for Service Provider");
  checkTextField(p_form.f_site_address, "You must enter an Address");
  checkSelectBox(p_form.f_site_area, "0", "You must select an option for Area");
  
  if(m_message.length == 0)
    l_proceed = true;
  else
  {
    l_proceed = false;
    m_focusObject.focus();
    alert('There are some Mandatory fields that need correction:\n' + m_message);
  }
  
  return l_proceed;
}


///
//Function: checkCCCISCoverage
//Desc: Does a few checks on the Radio Options that have checkboxes asocciated 
//      with them. It essentially requires at least one check box to be selected.
///
function checkCCCISCoverage(p_formName, p_filedNationWide, p_fieldCoverages)
{
  var l_proceed = true;
  var l_message = "";
  if(p_fieldCoverages == null && p_filedNationWide == null)
  {
    l_message += "- You must select a CCCIS Coverage Area\n";
  }
  else 
  {
    var l_cccisObj;
    var l_isCcisObj     = false;
    var l_cccisChecked  = false;
    var l_localChecked  = false;
    var l_msgPart       = "";
    var l_nationWide    = 0;
    var l_stateWide     = 1;
    var l_coverageWide  = 2;
    var l_local         = 3;
    
    ///
    //if nation wide, or local wide, just checl if they are checked, and continue
    if(p_filedNationWide.checked == true)
    {
      l_cccisChecked = true;
    }
    else
    {
      //else, if either statewide or coverage wide, need to organise var's 
      //so there corresponding values can be checked.
      if(getIsCheckboxChecked(p_fieldCoverages, "statewide"))
      {
        l_cccisObj = p_formName.sc_states;
        l_msgPart = "Statewide Coverage";
        l_isCcisObj = true;
      }
      
      if(getIsCheckboxChecked(p_fieldCoverages, "ccc"))
      {
        l_cccisObj = p_formName.sc_ccc;
        l_msgPart = "CCC Service Coverage";
        l_isCcisObj = true;
      }
      
      if(getIsCheckboxChecked(p_fieldCoverages, "local"))
      {
        l_localChecked = true;
      }
      
      if(l_isCcisObj == true || l_localChecked == true)
      {
        l_cccisChecked = true;
      }
    }
  
    if(!l_cccisChecked)
    {
      l_message = "- You must select a CCCIS Coverage field \n";
      setFocusObjectByTabIndex(p_filedNationWide);
    }
      
    //if there is some objects to check, run through them all to make sure at 
    //leats one has been selected
    if( l_isCcisObj)
    { 
      //Safety check, shouldn't be null
      if(l_cccisObj == null)
        l_message += "- You have selected " + l_msgPart + ". Please select one of its options\n";
      else
      {
        var l_idx = 0;
        //Use this to see if at least one has been checked.
        var l_atLeastOne = false;
        //loop through all elements until one is found to be checked.
        while(l_idx < l_cccisObj.length && !l_atLeastOne)
        {
          if(l_cccisObj[l_idx].checked == true)
          {
            //At least one is checked.          
            l_atLeastOne = true;         
          }
          l_idx++;
        }
        
        //if none were checked, we need to ask them to validate it.
        if(!l_atLeastOne)
        {
          l_message += "- There must be a value checked for " + l_msgPart + " \n   as it has been selected \n";
          setFocusObjectByTabIndex(l_cccisObj[0]);
        }
      }
    }
  }
  
  m_message += l_message;
}

////////
//
//
////////
function confirmDeleteItem(p_action, p_itemId, p_item, p_form, p_divId, p_divMessage)
{
  confirm("Yes NO");
  if(p_action == "confirm")
  {
    if(p_divMessage == "")
      p_divMessage = "Are you sure you want to delete?";
    var el = document.getElementById(p_divId);
    el.innerHTML = p_divMessage +
                   " <a href=\"Javascript:confirmDeleteItem('delete', " + p_itemId + ", " + p_item + ", " + p_form + ", '" + p_divId + "', '" + p_divMessage + "');\">" + 
                   " <img src=\"/admin/images/tick_icon.gif\" border='0' alt='Delete this item'></a>" + 
                   "&nbsp; <a href=\"Javascript:confirmDeleteItem('cancel', " + p_itemId + ", " + p_item + ", " + p_form + ", '" + p_divId + "', '" + p_divMessage + "');\">" + 
                   " <img src=\"/admin/images/cross_icon_green.gif\" border='0' alt='Cancel'></a>";
  }
  else if(p_action == "cancel")
  {
    var el = document.getElementById(p_divId);
    el.innerHTML = "<a href=\"Javascript:confirmDeleteItem('confirm', " + p_itemId + ", " + p_item + ", " + p_form + ", '" + p_divId + "', '" + p_divMessage + "');\">" + 
                   " <img src=\"/admin/images/delete.gif\" border='0' alt='Confirm Delete'></a>";
  }
  else if(p_action == "delete")
  {
    p_item.value = p_itemId;
    p_form.submit();
  }
}

////
//Function: checkCheckBoxes
//Desc: Uses the atLeastOneCheck to look through all text boxes to make
//      sure at least one has been selected.
////
function checkCheckBoxes(p_field, p_message)
{
  var l_atLeastOne = atLeastOneCheck(p_field);
  if(!l_atLeastOne){
    m_message += "- " + p_message + "\n";
    setFocusObjectByTabIndex(p_field[0]);
  }
}

////
//Function: atLeastOneCheck
//Desc: Will loop through all elements in p_field (afet checking it of course)
//      and test if there is at least one item selected.
//      Stops check and returns true.
////
function atLeastOneCheck(p_field)
{
  var l_atLeastOne = false;
  //if there is some objects to check, run through them all to make sure at 
  //leats one has been selected
  if(p_field)
  { 
    var l_idx = 0;
    //Use this to see if at least one has been checked.

    //loop through all elements until one is found to be checked.
    while(l_idx < p_field.length && !l_atLeastOne)
    {
      if(p_field[l_idx].checked == true)
      {
        //At least one is checked.          
        l_atLeastOne = true;         
      }
      l_idx++;
    }
  }
  
  return l_atLeastOne;
}

///
//Function: checkTextField
//Desc: Will check if the field is empty, if not it displays a message.
///
function checkTextField(p_field, p_message)
{
  if(p_field.value == "")
  {
    setFocusObjectByTabIndex(p_field);
    m_message += "- " + p_message + "\n";
  }
}

///
//Function: checkTextFieldAsNumber
//Desc: Will check if the field is empty, if not it displays a message.
//      If it is not empty, it ensures it is a number
//Params:- p_message: message to display if field is empty
//       - p_numberMessageName: Name to diplay in generic "- _name_ must contain a number"
///
function checkTextFieldAsNumber(p_field, p_message, p_numberMessageName)
{
  if(p_field.value == "")
  {
    setFocusObjectByTabIndex(p_field);
    m_message += "- " + p_message + "\n";
  }
  else if(isNaN(p_field.value))
  {
    setFocusObjectByTabIndex(p_field);
    m_message += "- " + p_message + " must contain a number\n";
  }
} 

////
//Function: setFocusObjectByTabIndex
//Desc: Suppose to set the focus object depending on the value of tabIndex.
//      In an ideal world, the tabIndex will be autocalculated, however,
//      it is not. 
//      Now it only sets it once (in an attempt to focus on the first object
////
function setFocusObjectByTabIndex(p_object)
{
  //if(m_focusObject == null || m_focusObject.tabIndex < p_object.tabIndex)
    //m_focusObject = p_object;
  if(m_focusObject == null)
    m_focusObject = p_object;
}

function setFocusObject(p_object)
{
  m_focusObject = p_object;
}

function getIsCheckboxChecked(p_checkBoxes, p_checkBox)
{
  var l_isMultlipleCheckBoxes = p_checkBoxes.length;
  var l_isChecked = false;
  if(l_isMultlipleCheckBoxes)
  {
    var i = 0;
    for(i = 0; i < l_isMultlipleCheckBoxes && !l_isChecked; i++)
    {
      var l_checkBoxName = p_checkBoxes[i].value
      if(l_checkBoxName == p_checkBox)
        l_isChecked = p_checkBoxes[i].checked;
    } 
  }
  
  return l_isChecked;
}

function getIsCoverageCheckboxesSet(p_checkBoxes, p_checkBox)
{
  var l_isMultlipleCheckBoxes = p_checkBoxes.length;
  var l_isChecked = false;
  if(l_isMultlipleCheckBoxes)
  {
    var i = 0;
    for(i = 0; i < l_isMultlipleCheckBoxes && !l_isChecked; i++)
    {
      var l_checkBoxName = p_checkBoxes[i].value
      if(l_checkBoxName == p_checkBox)
        l_isChecked = p_checkBoxes[i].checked;
      else
        l_isChecked = p_checkBoxes[i].checked;
    } 
  }
  
  return l_isChecked;
}

///
//Function: toggleCoverageFields
//Desc: Used to disable/enable the area's assocatied with f_cccis_coverage.
//      If a certain radio button is selected, then some areas are diabled,
//      while other areas are enabled.
///
function toggleCoverageFields(p_form, p_field)
{    
  if(p_field == 'nationwide')
  {
    var l_isEnabled = p_form.f_cccis_coverage_nationwide.checked;
    toggleCheckBoxes(p_form.f_cccis_coverage, l_isEnabled);
    toggleCheckBoxes(p_form.sc_states, !getIsCheckboxChecked(p_form.f_cccis_coverage, 'statewide'));
    toggleCheckBoxes(p_form.sc_ccc, !getIsCheckboxChecked(p_form.f_cccis_coverage, 'ccc'));
  }
  else if(p_field == 'local')
  {
    var l_isEnabled = getIsCoverageCheckboxesSet(p_form.f_cccis_coverage, 'local');
    p_form.f_cccis_coverage_nationwide.disabled = l_isEnabled;
  }
  else if(p_field == 'statewide')
  {
    var l_isEnabled = getIsCoverageCheckboxesSet(p_form.f_cccis_coverage, 'statewide');
    p_form.f_cccis_coverage_nationwide.disabled = l_isEnabled;
    toggleCheckBoxes(p_form.sc_states, !getIsCheckboxChecked(p_form.f_cccis_coverage, 'statewide'));
  }
  else if(p_field == 'ccc')
  {
    var l_isEnabled = getIsCoverageCheckboxesSet(p_form.f_cccis_coverage, 'ccc');
    p_form.f_cccis_coverage_nationwide.disabled = l_isEnabled;
    toggleCheckBoxes(p_form.sc_ccc, !getIsCheckboxChecked(p_form.f_cccis_coverage, 'ccc'));
  }
}

///
//Function:
//Desc: This is suppose to do field toggle on loading, i.e so the page already
//      has items enabled, disabled. But it don't work yet.
///
function autoToggleCoverageFields(p_form)
{    
  var l_toToggle = "";
  if(p_form != null){
    if(p_form.f_cccis_coverage_nationwide.checked)
      toggleCoverageFields(p_form, 'nationwide');
    else
    {
      var i = 0;
      for(i = 0; i < p_form.f_cccis_coverage.length; i++)
      {
       // if(p_form.f_cccis_coverage[i].checked) 
          toggleCoverageFields(p_form, p_form.f_cccis_coverage[i].value);
      }
//    if(p_form.f_cccis_coverage[0].checked == true)
//      toggleCoverageFields(p_form, 'local');
//    else if(p_form.f_cccis_coverage[1].checked == true)
//      toggleCoverageFields(p_form, 'statewide');'';
//    else if(p_form.f_cccis_coverage[2].checked == true)
//      toggleCoverageFields(p_form, 'ccc');'';
//    else if(p_form.f_cccis_coverage[3].checked == true)
//      toggleCoverageFields(p_form, 'nationwide');'';
//    
//    toggleCoverageFields(p_form, l_toToggle);
    }
  }
}

////
//Function: checkTextAreaSizeDivWarning
//Desc: Long name, it will do for now. First approach at handling a max limit on text area's.
//      (Example use in add_edit_vacancy_ACF.jsp)
//      Note: This is a little different from the rest fo the validate functions. Its actaully
//      called in the on_event_x of the field in the .jsp.
//
//Param:- p_form:
//      - p_textArea:
//      - p_textAreaMaxSize:
//      - p_warningDivId: The id of the div that will change to display a warning
//      - p_normalMessage: The Normal message to display in the div
//      - p_warningMessage: The message to display when limit has been reached
//      - p_cutText: Option to cut the extra text, brutal, but fair?
////
function checkTextAreaSizeDivWarning(p_textArea, p_textAreaMaxSize, p_warningDivId, p_normalMessage, p_warningMessage, p_showNumber, p_cutText)
{
  var l_textArea    = p_textArea;
  var l_string      = l_textArea.value;
  var l_warningDiv  = document.getElementById(p_warningDivId);
  var l_number = "";
  if(l_textArea.textLength > p_textAreaMaxSize)
  {
    if(p_cutText == true){
      l_string = l_string.substring(0, p_textAreaMaxSize);
      l_textArea.value = l_string;
    }

    if(p_showNumber)
      l_number = " " + l_textArea.textLength + "/" + p_textAreaMaxSize;
    l_warningDiv.innerHTML = p_normalMessage + "<br />" + "(" + p_warningMessage + l_number + ")";
  }   
  else
    l_warningDiv.innerHTML = p_normalMessage ;
}

///
//Function: checkTextAreaSize
//Desc: Makes sure the text area size is under the limit. Will add mesage to the global messages
//      if it exceeds it.
//      (This is req now of Mysql 5, as it throws an error if data is too large for the varchar field.)
///
function checkTextAreaSize(p_field, p_maxSize, p_message)
{
  if(p_field.textLength > p_maxSize)
  {
    setFocusObjectByTabIndex(p_field);
    m_message += "- " + p_message + "\n";
  }
}

///
//Function: toggleCheckBoxes
//Desc: Will disabled/undisable all checkboxes associtaed with the control.
///
function toggleCheckBoxes(p_checkBoxes, p_isDisabled)
{
  var l_idx;
  for(l_idx = 0; l_idx < p_checkBoxes.length; l_idx++)
  {
    p_checkBoxes[l_idx].disabled = p_isDisabled;
  }
}

function checkSelectBox(p_field, p_wrongValue, p_message)
{
  if(p_field.value == p_wrongValue)
  {
    m_message += "- " + p_message + "\n";
    setFocusObjectByTabIndex(p_field);
  }
}

function checkMultiSelectBox(p_field, p_message)
{
  if(p_field.length <= 0)
  {
    m_message += "- " + p_message + "\n";
    setFocusObjectByTabIndex(p_field);
  }
}

function checkAgencyRegions(p_type, p_selectedRegions, p_formName, p_funcSubmitFunction)
{
  if(m_waiting)
    return;
  var l_isItTrue = false;
  var l_regions = "";
  var i = 0;
  
  //m_waiting = ;
  for(i = 0; i < p_selectedRegions.length && !l_isItTrue; i++)
  {
    if(i > 0)
      l_regions += ",";
    l_regions += p_selectedRegions[i].value;
  }
  
  l_complete = true;
  l_qstr = "regions=" + l_regions + "&type=" + p_type;
  var l_submitFunc = p_funcSubmitFunction;
  var l_url = '/custom/sis/validate_agency.jsp';
  var l_parameters = l_qstr.toString();
  m_waiting = true;
  new Ajax.Request(l_url, {
  method: 'get',
  parameters: l_parameters,
  onException: function(r, e){
    //alert("Error: " + e.message);
  },
  onSuccess: function(transport) {
      if (transport.responseText.indexOf("submit") > -1){
        //p_funcSubmitFunction();
        submitFunction();
      }
      else{
        if (transport.responseText.indexOf("no_area") > -1){        
          m_message = "- You must select an Area in Regions\n" +
                      "- You must select ONE Postcode, Council, Funding region, or HACC region";        
        }
        else if (transport.responseText.indexOf("no_coverageArea") > -1){        
          m_message = "- You must select ONE Postcode, Council, Funding region, or HACC region";        
        }
        m_focusObject = p_formName.f_selected_regions;
        validateMessage();
      }
        
      m_waiting = false;
    }    
  });       
}

///
//Function: autoCheckAreaSuburbStatus()
//Desc: To be used in conjunctio with...
//      This will automatically check if a suburb
//      or area hs been disbaled (for 'Back' button pushes)
///
function autoCheckAreaSuburbStatus(p_suburbField, p_areaField)
{
  m_count = 0;
  var l_tempSuburb = p_suburbField;
  if(l_tempSuburb != null)
  {
    if(l_tempSuburb.value == '-1')
      disableAreas(false, p_areaField);
    else
      disableAreas(true, p_areaField);
  } 
}

///
//Function: areaSelected(e)              
//Desc: When a checkbox is clicked, we need to 
//      test if it is being unchecked,        
//      so we can decide wether or not        
//      to disable the suburb select box      
///
function areaSelected(e, p_areaField, p_suburbField)
{ 
  var l_checkBox = p_areaField;   
  var l_selectBox = p_suburbField;   
  var l_target; 
  if (!e) var e = window.event; 
  if (e.target) l_target = e.target; 
  else if (e.srcElement) l_target = e.srcElement; 
  if (l_target.nodeType == 3) // defeat Safari bug 
  l_target = l_target.parentNode; 
  if(l_target.checked == true){ 
    m_count--;
  }
  else { 
    m_count++;
  } 
  if(m_count == 0){ 
    l_selectBox.disabled = false; 
  } 
  else if(l_selectBox.disabled == false && m_count != 0) 
  { 
    l_selectBox.disabled = true; 
  } 
}

////
//Function: disableAreas(e)                 
//Desc: When a value besides 'any' is selected
//      we will need to disable the entire    
//      area's checkboxex                    
////                
function disableAreas(doDisable, p_areaField)
{ 
  var l_checkBoxes = p_areaField;            
  var i = 0; 
  for(i = 0; i < l_checkBoxes.length; i++)
  { 
    l_checkBoxes[i].disabled = doDisable; 
  }
} 

function suburbSelected(p_suburbField, p_areaField){
  var l_checkBoxes = p_areaField; 
  var l_selectBox = p_suburbField;   
  if(l_selectBox.value == '-1')
    disableAreas(false, p_areaField); 
  else
    disableAreas(true, p_areaField); 
}

function resetForm(p_formName, p_areaField, p_suburbField)
{                
   disableAreas(false, p_areaField);
   m_count = 0;
   p_suburbField.disabled = false; 
   p_formName.reset(); 
}

///////////////////////////
//Phase 3, Item 12: Next Vacancy functionality
///////////////////////////

///
//Function: nextVacancy()
//Desc: Accepts a Vacancy Id, a list of all the Ids for the Agency, and the current
//      position the particular vacacy is at. It then makes sure there are a list
//      of vacancies, appends them to the url query string, and changes the
//      windows location. i.e moves the window to the next vacancy.
///      
function nextVacancy(p_vacancyId, p_allVacancyIds, p_vacancyPos)
{
  var l_saveAndNext = "";
  var l_action      = "";
  if(p_allVacancyIds != "")
    l_saveAndNext = "&allVacancyIds=" + p_allVacancyIds + "&vacancyPos=" + p_vacancyPos;            
  l_action = "add_edit_vacancy_ACF.jsp?vacancy_id=" + p_vacancyId + l_saveAndNext;
  window.location = l_action;
}

