﻿// ################################################
//  purpose: general function for the app
//
//      isEnter: checks whether the keypress is enter
//      addCommas: adds commas in currency <unused>
//      findLayerByName: finds a layer by name in a dynamic map layer
//
// #################################################


function createRequestObject() {
    FORM_DATA = new Object();
    // The Object ("Array") where our data will be stored.
    separator = ',';
    // The token used to separate data from multi-select inputs
    query = '' + this.location;
    qu = query
    // Get the current URL so we can parse out the data.
    // Adding a null-string '' forces an implicit type cast
    // from property to string, for NS2 compatibility.
    query = query.substring((query.indexOf('?')) + 1);
    // Keep everything after the question mark '?'.
    if (query.length < 1) { return false; }  // Perhaps we got some bad data?
    keypairs = new Object();
    numKP = 1;
    // Local vars used to store and keep track of name/value pairs
    // as we parse them back into a usable form.
    while (query.indexOf('&') > -1) {
        keypairs[numKP] = query.substring(0, query.indexOf('&'));
        query = query.substring((query.indexOf('&')) + 1);
        numKP++;
        // Split the query string at each '&', storing the left-hand side
        // of the split in a new keypairs[] holder, and chopping the query
        // so that it gets the value of the right-hand string.
    }
    keypairs[numKP] = query;
    // Store what's left in the query string as the final keypairs[] data.<
    for (i in keypairs) {
        keyName = keypairs[i].substring(0, keypairs[i].indexOf('='));
        // Left of '=' is name.
        keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
        // Right of '=' is value.
        while (keyValue.indexOf('+') > -1) {
            keyValue = keyValue.substring(0, keyValue.indexOf('+')) + ' ' + keyValue.substring(keyValue.indexOf('+') + 1);
            // Replace each '+' in data string with a space.
        }
        keyValue = unescape(keyValue);
        // Unescape non-alphanumerics
        if (FORM_DATA[keyName]) {
            FORM_DATA[keyName] = FORM_DATA[keyName] + separator + keyValue;
            // Object already exists, it is probably a multi-select input,
            // and we need to generate a separator-delimited string
            // by appending to what we already have stored.
        } else {
            FORM_DATA[keyName] = keyValue;
            // Normal case: name gets value.
        }
    }
    return FORM_DATA;
}

function isEnter(e) {
    if (window.event)  // IE
        if (window.event.keyCode == 13)
        return true;
    else
        return false;
    else if (e.which) // Netscape/Firefox/Opera
    {
        //alert(e.which);
        if (e.which == 13)
            return true;
        else
            return false;
    }
}

// FORMAT NUMBERS WITH COMMAS
function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}


function findLayerByName(layername, mapservice) {

    var id = -1;
    var gLayer = map.getLayer(mapservice);
    if (gLayer != null) {
        dojo.forEach(gLayer.layerInfos, function(layerinfo) {
            //console.debug(layerinfo);
            if (layerinfo.name.toUpperCase() == layername.toUpperCase()) {
                id = layerinfo.id;
                return id;
            }
        });
    }
    return id
}

function findLayersByName(layername, mapservice) {
    // same as above, but returns an array of layers
    var ids = [];
    
    var gLayer = map.getLayer(mapservice);
    if (gLayer != null) {
        dojo.forEach(gLayer.layerInfos, function(layerinfo) {
            //console.debug(layerinfo);
            if (layerinfo.name.toUpperCase() == layername.toUpperCase()) {
                ids.push(layerinfo.id);
            }
        });
    }
    return ids;
}

function getFieldValue(graphic, fieldName) {
    // return the value of a named field
    var res = esri.substitute(graphic.attributes, "${" + fieldName + "}");
    return (res);
}