//*****************************************************************************
//This function is used to get the URL Parameters
//NOTE: Parameters for this must all be numbers...
//This needs an update for handling strings...
//*****************************************************************************
function Get_URL_Param(Param_2_Find) {
  var webParams = document.location.search;
  var pos=-1;
  if (webParams!="") {
    //Add special handler for mangled URL that LIMA produces
    //NOTE, the parameter should be uppercase... as this is a quick hack to make it work right
    var lima_check = -1;
    lima_check = webParams.indexOf("LIMA_CHECK");
    if (lima_check!=-1){
      webParams = webParams.replace("LIMA_CHECK=LIMA_GNIS?","LIMA_CHECK=LIMA_GNIS&")
      webParams = webParams.replace("LIMA_CHECK=LIMA?","LIMA_CHECK=LIMA&")
      webParams = webParams.replace("LIMA_CHECK=?","LIMA_CHECK=&")
    }
    var cmdString2 = webParams.toUpperCase();
    if (Param_2_Find.charAt(Param_2_Find.length-1)=="=") {
      pos = cmdString2.indexOf(Param_2_Find.toUpperCase());
    } else {
      pos = cmdString2.indexOf(Param_2_Find.toUpperCase()+"=");
    }
  }
  if (pos!=-1) {
    if (Param_2_Find.charAt(Param_2_Find.length-1)=="=") {
      startpos = pos + Param_2_Find.length;
    } else {
      startpos = pos + Param_2_Find.length + 1;
    }
    endpos = cmdString2.indexOf("&",startpos);
    if (endpos==-1) { endpos = cmdString2.length; }
    if (is_number(cmdString2.substring(startpos,endpos))) {
      return sigDigits(cmdString2.substring(startpos,endpos),4);
    } else {
      return URLEncode(webParams.substring(startpos,endpos));
    }
  }  else {
    return ""
  }
}

//*****************************************************************************
//This is a regular expression test to make sure input is a number
//*****************************************************************************
function is_number(test) {
  if (test!=null && test!="") {
    var Testing = Test_RegExp("^((\\d?)|(([-+]?\\d+\\.?\\d*)|([-+]?\\d*\\.?\\d+))|(([-+]?\\d+\\.?\\d*\\,\\ ?)*([-+]?\\d+\\.?\\d*))|(([-+]?\\d*\\.?\\d+\\,\\ ?)*([-+]?\\d*\\.?\\d+))|(([-+]?\\d+\\.?\\d*\\,\\ ?)*([-+]?\\d*\\.?\\d+))|(([-+]?\\d*\\.?\\d+\\,\\ ?)*([-+]?\\d+\\.?\\d*)))$", test);
    return (Testing);
  } else {
    return false;
  }
}

//*****************************************************************************
//This is a Javascript function to test a regular expression and a value
//*****************************************************************************
function Test_RegExp(RegExp_2_Use, Value_2_Test) {
  var re = new RegExp(RegExp_2_Use);
  var re_test = re.exec(Value_2_Test);
  //if ((re_test==null)) { return false; } else { return true; }
  return (re_test!=null);
}

//*****************************************************************************
// Function sigDigits
// given a string of a number, return a significant digit string based upon numDigits
//*****************************************************************************
function sigDigits(inStr, numDigits) {
    var add2 = false;
    var add1 = false;
    var multiplier = Math.pow(10.0,numDigits);
    var val_int = Math.floor( Math.round(inStr * multiplier) );
    var val_fin = val_int / multiplier;
    return(val_fin);
}

//*****************************************************************************
// Function URLEncode
//   This function is based upon the code from http://cass-hacks.com/articles/code/js_url_encode_decode/
//   That code was modified slightly to account for some oddities in how ArcIMS does URLEncoding
//*****************************************************************************
function URLEncode(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
      output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        //Modify code to account for the strange encoding ArcIMS does
        var tempOutput = '%' + hexVal.toUpperCase();
        if (tempOutput=='%2D') {
          output += '-';
        } else if (tempOutput=='%D' || tempOutput=='%A') {
          //Do nothing Skip this
        } else {
          output += tempOutput;
        }
      }
      x++;
    }
  }
  return output;
}

//*****************************************************************************
//This function is parse an XML string for an attribute value and return the
// first one that matches
//*****************************************************************************
function Parse_XML_4_Attribute(Attribute_2_Find, XML_String) {
  var pos=-1;
  if (XML_String!="") {
    var XML_String2 = XML_String.toUpperCase();
    pos = XML_String2.indexOf(Attribute_2_Find.toUpperCase());
  }
  if (pos!=-1) {
    //Find start of value - NOTE: it must be properly quoted...
    startpos = XML_String2.indexOf('"',pos) + 1;
    endpos = XML_String2.indexOf('"',startpos+1);
    if (endpos==-1) { endpos = startpos+1; }
    return XML_String.substring(startpos,endpos);
  }  else {
    return ""
  }
}