function  DoSearch(CurrentLoc, SearchURL) {

    //**************************************************************************
    //Function seraches through all form fields and builds a querystring 
    //search query to pass back to the same page.
    //the form values included are those with a SearchName attribute.  
    //The value of tyhis attribute is used
    //as the keyname for the querystring parameter.
    //**************************************************************************

    (CurrentLoc==null) ? CurrentLoc = window : false;
    
    var QS = SearchURL;
    
    //Build QueryString
    
    //Get all form controls into an object array
    var obj = document.forms[0];

        //Loop through all form controls
    for (i=0;i<obj.length;i++) {
        
        var FieldName = null;
        
        //Get the searchname attribute from the control (if it exists)
        // Checkboxes have the SearchName attribute int he parent span so look there for it        
        if (obj[i].tagName == 'INPUT' && obj[i].getAttribute("type") == 'checkbox') {
            FieldName = obj[i].parentNode.getAttribute("SearchName")
        } else {
            FieldName = obj[i].getAttribute("SearchName")
        };

        //If the FieldName has a value then carry on
        if (FieldName && FieldName.length > 0) {
            var Value = '';
            
            //Get the value of the contol depending on the control type
            if (obj[i].tagName == 'INPUT') {
                if (obj[i].getAttribute("type") == 'checkbox') {
                    Value = obj[i].checked ? 'on' : 'off'
                    if (Value == null) { Value = obj[i].getAttribute("checked") ? 'on' : 'off' }
                } else {
                    //Check if this is a date field
                    if (obj[i].getAttribute("SearchType") && obj[i].getAttribute("SearchType").substring(0,4) == 'Date') {
                        //This is date field
                        //Only do something for the Day filed, ignore the Month and year fields as they are processed 
                        // when the DD field is processed.
                        if (obj[i].getAttribute("SearchType") == 'DateDD') {
                            //Make sure there is a month and year field
                            if (obj[i+1] && obj[i+2] && obj[i+1].getAttribute("SearchType") == 'DateMM' && obj[i+2].getAttribute("SearchType") == 'DateYYYY') {
                            
                                //ensure that the date fields have values
                                if (obj[i].getAttribute("value") && obj[i].getAttribute("value").length > 0 &&
                                    obj[i+1].getAttribute("value") && obj[i+1].getAttribute("value").length > 0 &&
                                    obj[i+2].getAttribute("value") && obj[i+2].getAttribute("value").length > 0) {
                                   Value = obj[i].getAttribute("value") + '/' + obj[i+1].getAttribute("value") + '/' + obj[i+2].getAttribute("value")     
                                    if (Value == null) {Value = obj[i].value + '/' + obj[i+1].value + '/' + obj[i+2].value }
                                }   
                            }
                        }
                    } else {
                        Value = obj[i].getAttribute("value")
                        if (Value == null) {Value = obj[i].value}
                    }
                };
            } else if (obj[i].tagName == 'SELECT') {
                Value = obj[i].value;
            };
            
            //Add the value to the Querystring of the page
            QS = addToFriendlyQuerystring(QS, FieldName, Value);
        
        };
    };
    
    //Redirect to the correct new page with the correct querystring values
    CurrentLoc.location.href = QS; 
    
    return QS;
};

function addToQuerystring(URL, keyname, value) {
    //Adds a querystring name/value to an existing Querystring
    //If the key already exists it is replaced
    
    (value==null) ? value = '' : false;
    
    var PageElement = keyname + '=' + escape(value);
    
    var exp = '[&\?](' + keyname + '=[a-z0-9_\+\/\%\-]+)&'
    var exp2 = '[&\?](' + keyname + '=[a-z0-9_\+\/\%\-]+)$'
    
    var re = new RegExp(exp, 'i');
    var re2 = new RegExp(exp2, 'i');
    
    //Get the matches
    var m = re.exec(URL);
    var m2 = re2.exec(URL);
    
    if (m || m2) {
        //Keyname already exists in Querystrings so update
        if (m) {
                //The querystring has been found in the middle of the querystring
                if (value && value.length > 0 ) {
                    PageElement = m[0].substring(0,1) + PageElement + '&';
                    URL = URL.replace(m[0], PageElement);
                } else {
                    URL = URL.replace(m[0], m[0].substring(0,1));
                }
        } else {
            if (m2) {
                //The querystring has been found at the end of the querystring
                if (value && value.length > 0 ) {
                    PageElement = m2[0].substring(0,1) + PageElement;
                    URL = URL.replace(m2[0], PageElement);
                } else {
                    URL = URL.replace(m2[0], '');
                }
            }        
        }
                
    } else {
        //Keyname does not exist in Querystrings so insert it
        if (value && value.length > 0 ) {
            if (URL.match(/\?/)) {
                URL += '&' + PageElement
            } else {
                URL += '?' + PageElement
            };
        };
    };
    return URL;
};
function addToFriendlyQuerystring(URL, keyname, value) {
    //Adds a querystring name/value to an existing Querystring
    //If the key already exists it is replaced
    
    (value==null) ? value = '' : false;
    
    var PageElement = keyname + '/' + escape(value);
    
    var exp = '/(' + keyname + '/[a-z0-9_\+\%\-]+)/'
    var exp2 = '/(' + keyname + '/[a-z0-9_\+\%\-]+)$'
    
    var re = new RegExp(exp, 'i');
    var re2 = new RegExp(exp2, 'i');
    
    //Get the matches
    var m = re.exec(URL);
    var m2 = re2.exec(URL);
    
    if (m || m2) {
        //Keyname already exists in Querystrings so update
        if (m) {
                //The querystring has been found in the middle of the querystring
                if (value && value.length > 0 ) {
                    PageElement = m[0].substring(0,1) + PageElement + '/';
                    URL = URL.replace(m[0], PageElement);
                } else {
                    URL = URL.replace(m[0], m[0].substring(0,1));
                }
        } else {
            if (m2) {
                //The querystring has been found at the end of the querystring
                if (value && value.length > 0 ) {
                    PageElement = m2[0].substring(0,1) + PageElement;
                    URL = URL.replace(m2[0], PageElement);
                } else {
                    URL = URL.replace(m2[0], '');
                }
            }        
        }
                
    } else {
        //Keyname does not exist in Querystrings so insert it
        if (value && value.length > 0 ) {
            if (URL.lastIndexOf("/") == (URL.length-1)) {
             //Already has atrailing slash so don't need one here
             URL += PageElement
            } else {
             URL += '/' + PageElement
            }
        };
    };
    return URL;
};
