function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if (node == null) node = document;
    if (tag == null) tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className) ) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

function order_by(condition) {
    document.fast_search.order_by.value = condition;
        document.fast_search.submit();
}
function page(n) {
    document.fast_search.page.value = n;
        document.fast_search.submit();
}
function toggle(id) {
    var e = document.getElementById(id);
    if (e) {
        e.style.display = e.style.display=='block' ? 'none' : 'block';
    }
    return false;
}

function varToggle(link, id) {
    toggle(id);
    var s = link.getElementsByTagName('span')[0];
    var uarr = String.fromCharCode(0x25b6);
    var darr = String.fromCharCode(0x25bc);
    s.innerHTML = s.innerHTML == uarr ? darr : uarr;
    return false;
}

function popup(id) {
    var elem = document.getElementById(id);
    elem.style.left='30%';
    elem.style.top ='30%';
    elem.style.marginLeft ='auto';
    elem.style.marginRight='auto';
    elem.style.minWidth='25%';
    elem.style.padding ='15px';
    elem.style.display = "block";
    return false;
}

function popup_delete(url, title) {
    //href = "<a href=\"#\" onclick=\"show_confirm_delete('"+url+"');\">";
    href = "<a href=\"#\" onclick=\"javascript:window.open('"+ url +"', '"+ title +"', 'top=200, left=200, width=500, height=100, status=no, menubar=no, toolbar=no');return false;\">";
    img = '<img src="/static/img/delete.gif" alt="'+title+'">';
    return href + img + '<\a>'
}

function show_confirm_delete(url){
    Ext.Ajax.request({
       url: url,
       success: showAlert
    });        
}

function showAlert(result) {
    Ext.MessageBox.show({
        title: 'Save Changes?',
        msg: result.responseText,
        icon: Ext.MessageBox.QUESTION,
        minWidth: 400
    });    
}

function go_to_url_or_cancel(ask, url) {
    var answer = confirm(ask);
    if (answer){
        window.location = url;
    }
    else {
        return false;
    }
}

function setPopUp(node, tag) {
    // setup an onclick listener for all tags 'a' in div 'content' with class 'popup'
    node = document.getElementById(node);
    var popup_list = []
    popup_list = getElementsByClass('popup', node, tag);
    for (i = 0; i < popup_list.length; i++) {
        if (popup_list[i]) {
            popup_list[i].onclick = openPopUp;
            }
        }
    }

function openPopUp() {
    window.open(this.href, '', 'top=200, left=200, width=480, height=320, status=no, menubar=no, toolbar=no scrollbar=no');
    return false;
}

function get_querystring_parameter(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

// blank image
Ext.BLANK_IMAGE_URL = '/static/js/extjs/resources/images/gray/s.gif';
    
Ext.onReady(function(){
    // Abilito i tooltip
    //
    Ext.QuickTips.init();
    enableTooltips('content');
});
