//-----------------------UTILITY FUNCTIONS----------------

// This function is used if inputs giving the user a choice of 
// columns to display are provided with the map
function GetShowColumns() {
  var ShowColumns = ""
  var blShowAllColumns = 0
  blShowAllColumns = GetFormElementValue('search_form','show_all_columns');
  if (blShowAllColumns)
  {
    ShowColumns = GetFormElementAllValues('search_form','show_column');
  }
  else
  {
    ShowColumns = GetFormElementValue('search_form','show_column');
  } 
  return ShowColumns;
}

// This function is used if inputs giving the user a choice of 
// columns to display are provided with the map
function GetSQLString() {
  var strSQL = "";
  var SELECTColumns = ""
  var blShowAllColumns = 0
  var StateValue = GetFormElementValue('search_form','state_value');
  blShowAllColumns = GetFormElementValue('search_form','show_all_columns');
  if (blShowAllColumns)
  {
    SELECTColumns =  GetFormElementAllValues('search_form','show_column');
  }
  else
  {
    SELECTColumns = GetFormElementValue('search_form','show_column');
  }
  strSQL = "SELECT "+SELECTColumns+ " FROM "+TABLE_NAME+" WHERE "+WHERE_COLUMN+" = '"+StateValue+"'";
  return strSQL;
}
//--------------------------------------------------------

  
//--------------------PROCESS QUERY FUNCTIONS-------------

// used to run the query from the map
// This function is used if inputs giving the user a choice of 
// columns to display are provided with the map
function ShowResults(st_abrev) {
  var SRCHMAP = document.search_form
  SRCHMAP.xmlvp.value = XMLVP;
  SRCHMAP.table_name.value = TABLE_NAME;
  SRCHMAP.shwclmns.value = GetShowColumns();
  SRCHMAP.state_value.value = st_abrev;
  SRCHMAP.sql.value = GetSQLString();
  SRCHMAP.header.value = st_abrev;
  var ShowColumns = GetFormElementValue('search_form','shwclmns')
  if (ShowColumns == "")
    {alert('No fields to display have been selected.');}
  else
    {document.search_form.submit();}
}

// used to run the query from the list box
// This function is used if inputs giving the user a choice of 
// columns to display are provided with the map
function ShowResultsFromBox() {
  var STBox = document.search_form.state;
  var ST = STBox.options[STBox.options.selectedIndex].value;
  ShowResults(ST);
}


// used to run the query from the map
// This function is used if no inputs are provided for the user 
// to select the columns to display
function ShowResults2(st_abrev) {
  var SRCHMAP = document.search_form;
  var SQL = SRCHMAP.predefined_sql.value;
  var WHRCLS = " WHERE (["+WHERE_COLUMN+"] = '"+st_abrev+"')";
  SQL += WHRCLS;
  SRCHMAP.sql.value = SQL;
  SRCHMAP.whrcls.value = WHRCLS;
  var ShowColumns = GetFormElementValue('search_form','shwclmns');
  if (ShowColumns == "")
    {alert('No fields to display have been selected.');}
  else
    {document.search_form.submit();}
}


function ShowResults3(st_abrev,st_name) {
  var SRCHMAP = document.search_form;
  var SQL = SRCHMAP.predefined_sql.value;
  var regEx = new RegExp("%%\\w+%%","gi")  
  while ((aMatches = regEx.exec(SQL)) != null) {
    switch (aMatches[0]) {
      case "%%STATE%%":
        rplcvlu = st_abrev;
        break;  
    }      
    SQL = SQL.replace(aMatches[0],rplcvlu)
  }
  SRCHMAP.sql.value = SQL;
  SRCHMAP.header.value = st_name;
  //SRCHMAP.whrcls.value = WHRCLS;
  var ShowColumns = GetFormElementValue('search_form','shwclmns');
  if (ShowColumns == "")
    {alert('No fields to display have been selected.');}
  else
    {document.search_form.submit();}
}

// used to run the query from the list box
// This function is used if no inputs are provided for the user 
// to select the columns to display
function ShowResultsFromBox2() {
  var STBox = document.search_form.state;
  var ST = STBox.options[STBox.options.selectedIndex].value;
  ShowResults2(ST);
}

function ShowResultsFromBox3() {
  var STBox = document.search_form.state;
  var ST = STBox.options[STBox.options.selectedIndex].value;
  var STtext = STBox.options[STBox.options.selectedIndex].text;
  ShowResults3(ST,STtext);
}
//--------------------------------------------------------





function GetFormElementValue(frm,el) {
  var DataValue = ""
  var DataEl = document[frm][el]

  if (DataEl) {
    var DataElType = DataEl.type
    if (DataElType == "select-one") {
	DataValue = DataEl.options[DataEl.selectedIndex].value
    }
    else if (DataElType == "select-multiple") {
      var intL = DataEl.options.length
      for (var x = 0;x<intL;x++) {
        if (DataEl.options[x].selected == true) {
          if (DataValue != "")
            {DataValue = DataValue + ",";}
          DataValue = DataValue + DataEl.options[x].value;
        }
      }
    }
    else if (DataElType == "text" || DataElType == "textarea" || DataElType == "hidden") {
      DataValue = DataEl.value;
    }
    else if (DataElType == "radio" || DataElType == "checkbox") {
      if (DataEl.checked == true) {
        DataValue = DataEl.value;
      }
    }
    else {
      for (var x = 0;x<DataEl.length;x++) {
        if (DataEl[x].checked == true) {
          if (DataValue != "") {
            DataValue = DataValue + ",";
          }
          DataValue = DataValue + DataEl[x].value;
        }
      }
    }
  }
  return DataValue;
}

function GetFormElementAllValues(frm,el) {
  var DataValue = ""
  var DataEl = document[frm][el]

  if (DataEl) {
    var DataElType = DataEl.type
    if (DataElType == "select-one") {
	  DataValue = DataEl.options[DataEl.selectedIndex].value
    }
    else if (DataElType == "select-multiple") {
      var intL = DataEl.options.length
      for (var x = 0;x<intL;x++) {
        if (DataValue != "")
          {DataValue = DataValue + ",";}
        DataValue = DataValue + DataEl.options[x].value;
      }
    }
    else if (DataElType == "text" || DataElType == "textarea" || DataElType == "hidden") {
      DataValue = DataEl.value;
    }
    else if (DataElType == "radio" || DataElType == "checkbox") {
      if (DataEl.checked == true) {
        DataValue = DataEl.value;
      }
    }
    else {
      for (var x = 0;x<DataEl.length;x++) {
        if (DataValue != "") {
          DataValue = DataValue + ",";
        }
        DataValue = DataValue + DataEl[x].value;
      }
    }
  }
  return DataValue;
}

function GetFormElementAllText(frm,el) {
  var DataValue = ""
  var DataEl = document[frm][el]

  if (DataEl) {
    var DataElType = DataEl.type
    if (DataElType == "select-one") {
	  DataValue = DataEl.options[DataEl.selectedIndex].value
    }
    else if (DataElType == "select-multiple") {
      var intL = DataEl.options.length
      for (var x = 0;x<intL;x++) {
        if (DataValue != "")
          {DataValue = DataValue + ",";}
        DataValue = DataValue + DataEl.options[x].text;
      }
    }
    else if (DataElType == "text" || DataElType == "textarea" || DataElType == "hidden") {
      DataValue = DataEl.value;
    }
    else if (DataElType == "radio" || DataElType == "checkbox") {
      if (DataEl.checked == true) {
        DataValue = DataEl.value;
      }
    }
    else {
      for (var x = 0;x<DataEl.length;x++) {
        if (DataValue != "") {
          DataValue = DataValue + ",";
        }
        DataValue = DataValue + DataEl[x].outerText;
      }
    }
  }
  return DataValue;
}

function SelectAllValues(frm,el) {
  var DataEl = document[frm][el]
  if (DataEl) {
    var DataElType = DataEl.type
    if (DataElType == "select-multiple") {
      var intL = DataEl.options.length
      for (var x = 0;x<intL;x++) {
        DataEl.options[x].selected = true
      }
    }
    else if (DataElType == "radio" || DataElType == "checkbox") {
      DataEl.checked = true
    }
    else {
      for (var x = 0;x<DataEl.length;x++) {
        DataEl[x].checked = true;
      }
    }
  }
}


