/*
 * !ignoreUrls
 */

/**
 * 2006-03-08   Dan Bettles     Created
 */
function isBlank (p_var)
{
    return (((p_var == '') || (p_var == null)) ? true : false);
}



/**
 * 2006-03-08   Dan Bettles     Created
 */
function openPlainWindow (p_url, p_width, p_height)
{
    var oWindow = null;

    if (p_url && ! isBlank (p_url)) {
        var windowFeatures = '';

        if ((p_width && (p_width > 0)) || (p_height && (p_height > 0))) {
            var outerWidth = p_width || 100;  /* Defaults */
            var outerHeight = p_height || 100;
            windowFeatures = 'width=' + outerWidth + ',height=' + outerWidth + ',';
        }

        windowFeatures = windowFeatures + 'location=no,menubar=no,directories=no,toolbar=no,status=no,resizable=yes,scrollbars=yes,';
        var oDate = new Date();
        var winName = 'plainWindow_' + oDate.getTime();
        var oWindow = window.open (p_url, winName, windowFeatures);
    }

    return oWindow;
}



/**
 * This function mirrors the appendQueryString() method in the CustomUrlHandler
 *
 * This function should be used to append all query strings to any url, because
 * it needs to observe the rules of when a custom url has rewritten query strings
 * or not
 *
 * @param string url The url to append args to
 * @param array args String in the format of key=var& or associative array of args to append
 * @return string The new url
 */
function appendQueryString(url, args) {
    // There are a few states the url could be in
    // 1 - It could be in any format (normal, rewritten, or custom) but with a query string already appended - append normal format
    // 2 - It's a custom url with a rewritten query string - append rewritten format
    // 3 - It's a rewritten url with a rewritten query string - append rewritten format
    // 4 - It's a normal url - append normal format

    // First construct the query string
    var query_str = isArray(args) ? http_build_query(args) : args;

    if (url.indexOf('?') != -1 || url.indexOf('server.php') != -1) {
        var rewrite = false;
    }
    else if (url.indexOf('server/') != -1 || url.indexOf('/*/') != -1) {
        var rewrite = true;
    }
    else {
        var rewrite = true; // since we haven't matched any known paths, assume it's a custom url
    }
    
    if (rewrite) {
        var isCategory = (url.charAt(url.length - 1) == '/');
        if (isCategory === false) {
            url += "/";
        }

        // If this is a custom url, we need to separate the args out from the custom url
        if (url.indexOf('server/') == -1 && url.indexOf('/*/') == -1) {
            url += "*/";
        }

        var rewritten_query_str = query_str.replace(/(&(amp;)?|=)/g, '/');

        // Strip off the last '/' if one is present and this isn't a category - it has a special meaning with custom urls
        var endsWithSlash = rewritten_query_str.charAt(rewritten_query_str.length - 1) == '/';
        if (endsWithSlash && !isCategory) {
            rewritten_query_str = rewritten_query_str.substr(0, rewritten_query_str.length - 1);
        }
        // otherwise append if it needs to be
        else if (!endsWithSlash && isCategory) {
            rewritten_query_str += "/";
        }

        url += rewritten_query_str;
    } else {
        url += (url.indexOf('?') == -1 ? '?' : '&') + query_str;
    }

    return url;
}


/**
 * This is based on the function of the same name in common.func.php
 */
function http_build_query(formdata, numeric_prefix, arg_separator, _key) {
    // Do this here to prevent recursive calls later
    if (arg_separator === undefined) {
        arg_separator = '&';
    }

    var aValues = [];

    for (var key in formdata) {
        if (!formdata.hasOwnProperty(key)) {
            continue;
        }

        var value = formdata[key];
        key = escape(key);

        // Add the numeric prefix to numerical keys, only when _key is empty
        if (_key === undefined) {
            if (/^\d+$/.test(key) && (numeric_prefix !== undefined)) {
                key = numeric_prefix + key;
            }
        } else {
            key = unescape(_key) + '[' + key + ']';
            key = escape(key);
        }

        if (isArray(value)) {
            aValues[aValues.length] = http_build_query(value, '', arg_separator, key);
        }
        else {
            aValues[aValues.length] = key + '=' + escape(value);
        }
    }

    return aValues.join(arg_separator);
}

/**
 * Determines if subject is an array
 *
 * @var mixed subject Variable to test
 * @return bool
 */
function isArray(subject) {
    return subject.constructor.toString().indexOf('Array') != -1;
}
