﻿// returns formatted link to search engine
//  uses field names as in specification 
//  (searches fields by 'name' attribute)
//  requires prototype framework (can be detached in future if too complicated)
//  searchForm - id or element containing the form)
//  fields -    object whos property contain element IDs or static values (v:value): 
//              object property names are matched to standard form names (See names var).
//              example {'PageMethod' : 'v:search', 'KW': 'someTextbox' } 
//  globals - (optional) baseSearchAddress - address of search engine request (default: '/WynikiWyszukiwania.aspx')
function formatSearchLink(searchForm, fields) {
    searchForm = $(searchForm);
    var BASE_SEARCH_ADDRESS_KEY = 'baseSearchAddress';
    
    var names = [
        'PageMethod',
        'SE',
        'PN',
        'ROP',
        'KW',
        'S',
        'T',
        'CI',
        'C',
        'M',
        'CO'];
    fields = fields || {}; // default value
        
    var addr = window[BASE_SEARCH_ADDRESS_KEY] || '/WynikiWyszukiwania.aspx';
    var delim = '?';
    for(var i=0; i< names.length; i++) {
        var name = names[i];
        var field = fields[name];
        var val = null;
        
        // get from collection, read static value or find by name
        if(field) {
            if(field.substr(0,2) == "v:") {
                val = field.substr(2);
            } else {
                var elem = $(fields[name]) || searchForm.down('[name='+name+']');
                if(elem) {
                    val = $F(elem);
                }
            }
        }
        
        if(val) {
            addr += delim + name + '=' + val;
            if (delim=='?') delim = '&';
        }
    }
    return addr;
}
