
<script type="text/javascript">
/* generic functions */
function redirect(redirecturl) {
    fxlog("redirect: to='" + redirecturl + "'");
    if (top && top.location) {
        top.location = redirecturl;
    } else {
        window.location = redirecturl;
    }
}
 

/* extracts the text child nodes (null if node is null empty string when none)
 */
function extract_text(node) {
    var text;
    if (node==null) {
        text = null;
    } else {
        var acc = "";
        var children = node.childNodes;
        for (var i=0; i<children.length; i++) {
            acc += "" + children[i].nodeValue;
        }
        text = acc;
    }
    return text;
}

/*
 * finds first child for given tag name (null when root is null or none found)
 */
function find_first(root, name) {
    var fst;
    if (root==null) {
        fst = null;
    } else {
        var es = root.getElementsByTagName(name);
        fst = (es.length>0) ? es[0] : null;
    }
    return fst;
}

    
/* extracts the text nodes from the first child of the given name */
function text_of_first(root, name) {
    return extract_text(find_first(root, name));
}


/* unescapes XML */
function unescape_xml(escaped) {
    var unescaped = String(escaped);
    unescaped = unescaped.replace("&amp;", "&");
    unescaped = unescaped.replace("&lt;", "<");
    unescaped = unescaped.replace("&gt;", ">");
    unescaped = unescaped.replace("&quot;", "\"");
    unescaped = unescaped.replace("&#34;", "\"");
    unescaped = unescaped.replace("&apos;", "'");
    unescaped = unescaped.replace("&#39;", "'");
    
    return unescaped;
}

 
/* extracts selection values from jQuery set
 * joining multiple values with the given delimiter.
 *   */
function extract_elem_values(elems, delim) {
    return jQuery.makeArray(elems.map(function() { 
        var value;
        if (this.val) {
            value = this.val();
        } else if(this.value) {
            value = this.value;
        } else {
            value = null;
        }
        return value;
    }).filter(function(x){ return x != null; })).join(delim);
}    

  
/* finds the selected values from a selector 
 * joined with the given delimiter if needed.
 * (null if none selected or if selector is null). */
function find_selected_values(selector, delim) {
    var selected;
    if (selector==null) {
        selected = null;
    } else {
        var value_selector = $(selector).find("option:selected");
        if (value_selector.length!=0) {
            selected = extract_elem_values(value_selector, delim);
        } else {
            selected = null;
        }
    }
    return selected;
}


/* makes a dummy element to store a name-value pair */
function make_dummy_elem(name, value) {
    var new_elem = document.createElement("input");
    new_elem.type = "hidden";
    new_elem.name = name;
    new_elem.value = value;
    fxlog("make elem: name=" + name + " value=" + value + "; new elem is " + new_elem);
    return new_elem;
}

/* Jameco special  */

/* products display panel functions */

/* gets the current state of show all for sort options */
function is_showall_sort() {
    return $("input.sortlinkmore").val() == "1";
}


/* toggles search order where applicable */
function toggle_sort_order(order) {
    fxlog("toggle sort order: original order: " + order);
    var new_order = null;
    if (order) {
        var asc_ix = order.indexOf("(Asc");
        if (asc_ix>0) {
            new_order = order.substring(0, asc_ix) + "(Descending)";
        } else {
            var desc_ix = order.indexOf("(Des");
            if (desc_ix>0) {
                new_order = order.substring(0, desc_ix) + "(Ascending)";
            } else {
                new_order = order;
            }
        }
    }
    
    return new_order;
}



/* Parametric search panel functions */

/* parametric options panel: hide until updated */
function parametric_panel_enable() {
    fxlog("parametric panel disable: enabling...");
    $("#parametricoptionspanel").hide();
    $(".paramsearchprogressbox").show();
    fxlog("parametric panel enable: enabled.");
}

/* parametric options panel: restore view after update. */
function parametric_panel_disable() {
    fxlog("parametric panel disable: disabling...");
    $(".paramsearchprogressbox").hide();
    $("#parametricoptionspanel").show();
    fxlog("parametric panel disable: disabled.");
}

function is_empty(a_string) {
  var empty = true;
  if (a_string) {
      if (a_string.length != 0) {
          for (var i=0; i<a_string.length; i++) {
              var cur = a_string[i];
              if (cur != " " && cur != "\r" && cur != "\n") {
                  empty = false;
                  break;
              }
          }
      }
  }
  
  return empty;
}


/* Updates the parametric page
 * 
 */
function parametric_panel_update(xml){
    fxlog("update parametric: xml: " + xml);
    var updates = find_first(xml, "updates");
    fxlog("update parametric: updates: " + updates);
    
    var redirecturl = text_of_first(updates, "redirecturl");
    
    /* load and replace history url */
    /* var histurl = text_of_first(updates, "historyurl");
    fxlog("update parametric: histurl: " + histurl);
    if (histurl!=null) {
        historyUrl = histurl;
    } */

    /* load and replace task query */
    var taskq = text_of_first(updates, "taskquery");
    fxlog("update parametric: taskq: " + taskq);
    if (taskq!=null) {
        $("#debugtaskquery").each(function(){
            this.innerHTML= taskq;
        });
    }
    /* load and replace parametric options panel */
    var paramsearch = text_of_first(updates, "parametricsearch");
    fxlog("update parametric: paramsearch: " + paramsearch);
    if (!is_empty(paramsearch)) {
        $("#parametricoptionspanel").replaceWith(paramsearch);
    }
}


/* makes asynchronous call to updates parametric filters.
 * 
 */
function parametric_update_async(path, params, action_success, action_failure) {
    
    $.ajax({
        url: path,
        type: 'POST',
        data: params,
        dataType: 'xml',
        timeout: 20000,
        error: function(ev, req, ajopt, te){
            alert('Error loading XML document. ev=' + ev + "; req=" + req 
                + "; op=" + ajopt + "; te=" + te );
            action_failure();
        },
        success: function(xml) { action_success(xml); }
      });
}


/* gets the current state of show all for parametric search */
function is_showall_parametric() {
    return $("input.paramsearchmore").val() == "1";
}




/* search functions */


/* Loads filter selections from the given optionboxes 
 * selections are returned with objects with properties
 * named pType, pName, and pValue
 */
function xs4search_load_selections(items) {
    return items.map(
    function(){
        var param_type = $(this).find("input[@type='hidden'][@class='paramsearchtype']").val();
        fxlog("xs4search load selections: paramType=" + param_type);
        var selector = $(this).find("select[@class='paramsearchvalues']:first")[0];
        fxlog("xs4search load selections: selector=" + selector);
        var param_name = null;
        var param_value = null;
        
        if (selector) {
            param_name = $(selector).attr("name");
            fxlog("xs4search load selections: param name=" + param_name);
            param_value = find_selected_values(selector, "!!");
            fxlog("xs4search load selections: param value=" + param_value);
        } else {
            fxlog("xs4search load selections: trying input type radio...");
            selector = $(this).find("input[@type='hidden'][@class='paramsearchvalues']:first")[0];
            if (selector) {
                fxlog("xs4search load selections: potential radio type input");
                var selector_id = selector.value;
                if (selector_id) {
                    fxlog("xs4search load selections: potential radio input; name=" +selector_id);
                    param_name = selector_id;
                    param_value = $(this).find("input[@type='radio'][@name='" + selector_id +"']:checked").val();
                    fxlog("xs4search load selections: potential radio input; name=" + selector_id + " value=" + param_value);
                }
            }
        }
            
        if (param_type!=null && param_name!=null && param_value!=null) {
            return { pType: param_type, pName: param_name, pValue: param_value};
        } else {
            return null;
        }
    });
}



/**  performs search
 */
function xs4search(items, params_builder, action) {
    var filter_selections = xs4search_load_selections(items);
    fxlog("xs4search: options fetched:" + filter_selections);
    
    var params = params_builder(filter_selections);
    fxlog("xs4search search: params filled: " + params);
    
    if (params==null) {
        fxlog("xs4search search: no action taken.");
    } else {
        action(params);
    }
}
</script>
    