﻿function prepareLinkButtonClicks()
{
    $$('a.defaultbutton').each(function(tag) {
        if (tag && typeof (tag.click == 'undefined')) {
            tag.click = function() {
                var result = true;
                if (tag.onclick) result = tag.onclick();
                if (typeof (result) == 'undefined' || result) {
                    //eval(tag.href);
                    eval(unescape(tag.href));
                }
            }
        }
    });
}



var comboKeypressTimeout;
function openCombobox(name, suffix, comboType, numChars) {
    

    var doOpenCombobox = function() {
        var objName = "combo" + name + suffix; 
        var ajaxName = "Suggest" + name;
        //alert(ajaxName);
        var e = eval(objName);
        
        var processAjaxResults = function(sData) {            
            e.clearOptions();
            e.addCustomOption( sData );
            //sorttable.makeSortable($("suggest" + name + suffix + "Grid"));
            JQ("#suggest" + name + suffix + "Grid").tablesorter({
                widgets: ['zebra']
            });   
            
        };
        
        var processXML = function(sData) { 
                        
            //alert(sData); 
            //this happens automatically with obout control, but we need this function for some reason for the hide spinner code to work
            //we could move the hide spinner code here           
        };
        
        
        
        if (e) { 
	        var oTextbox = document.getElementById("ob_" + objName + "Textbox");
    	    
    	    
	        //this block puts in an empty option when user deletes all characters after previously making a selection
	        //(it used to "stick" unless you tabbed out of the combobox)
	        if(oTextbox.value.length == 0) {	                 
	            e.clearOptions();	           
	            e.addOption("", "", "", ""); 
	            e.setSelectedIndex(0);
	            e.close();   
	        }

    	    
	        if(oTextbox.value.length >= numChars) {	    
	            $('spin' + name + suffix).style.visibility = 'visible';
    	        //e.clearOptions(); // remove previous results before hitting the server again.   
    	        
	            if (comboType=="grid") {     
	                e.clearOptions(); // remove previous results before hitting the server again.   TODO: why doesn't this work for regular xml-based options box?           
	                ob_post.AddParam("sText", oTextbox.value);
	                ob_post.AddParam("suffix", suffix);
			        ob_post.post(null, ajaxName, processAjaxResults);   			        
	            } else {
	                //e.o_o5 = ajaxName;	   
	                ob_post.AddParam("sText", oTextbox.value);
			        ob_post.post(null, ajaxName, processXML);                   
	            }
    	        e.open();    		    
    		    
	        } else {
	            // this was in obout's example, but actually messes up results after user empties combobox 
		        //e.o_o5 = "";                   
	        }
	    }
    };
    
    
    
    clearTimeout(comboKeypressTimeout);
    comboKeypressTimeout = setTimeout(doOpenCombobox, 575);
}







function setComboGridSelected(el, comboName, comboSuffix, value, text) {
    var comboObj = eval("combo" + comboName + comboSuffix);
    
    JQ("#suggest" + comboName + comboSuffix + "Grid > tbody > .alternateRowed").addClass("alternateRow");
    JQ("#suggest" + comboName + comboSuffix + "Grid > tbody > .gridSelected").removeClass("gridSelected alternateRowed");
    
    if (JQ(el).attr("class").indexOf("alternateRow") >= 0) {
        JQ(el).removeClass("alternateRow");
        JQ(el).addClass("alternateRowed");
    }
    JQ(el).addClass("gridSelected");
    
    comboObj.setValue(value);
    comboObj.setText(text);        
 
}




