$(document).ready( function() {
    $('.select-checkbox').click( function() {
        if ( $(this).is(':checked') ) {
            select( $(this) );
        } else {
            unselect( $(this) );
        }
    });
    $('#toggle-select').click( function() {
        var is_checked = $(this).is(':checked');
        $('.select-checkbox').each( function() {
            if (is_checked) {
                $(this).attr('checked','checked');
                select( $(this) );
            } else {
                $(this).removeAttr('checked');
                unselect( $(this) );
            }
        });
    });
    $('#limit-button').change( function() {
	var url = $.query.set('limit', $(this).val()); 
	// TODO - fix this hack
        url = url.toString().replace(/[%]2B/g, "+");
        window.location = url;
    });
});
function onDialogOpen() { 
    var p = $(this).parent();
    p.find('.ui-dialog-buttonpane button:first').addClass('primary');
    $(this).parents('.ui-dialog').wrapInner( '<div class="ui-dialog-inner"></div>' ); 
}
function select( e ) {
    e.parents('li').addClass('selected');
    $('#list-operators button.hideable').removeAttr('disabled').show();
}
function unselect( e ) {
    e.parents('li').removeClass('selected');
    if ( ! $('.select-checkbox:checked').length ) {
        $('#list-operators button.hideable').attr('disabled','true').hide();
    };
}
function buildIdList() {
    var idsStr = '';
    $('.select-checkbox:checked').each( function() {
        if (idsStr != '') { idsStr += ','; } 
        idsStr += $(this).val(); 
    });
    return idsStr;
}

function _find_selected( a, id ) {
  for (var i=0; i<a.length; i++) { 
    if (a[i] == id) { return i; }
  }
  return -1;
}

function select_user(id) {
  window.open("select_user.php?func=set_user&id="+id,
              "select_user",
              "fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=550,height=600");
}
function set_user(id,newValue) {
  getByID(id).value = newValue;
}

function select_assignee(id,tpid,tcid) {
  window.open("testcase_assign.php?id="+id+"&tpid="+tpid+"&tcid="+tcid,
              "testcase_assign",
              "fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=550,height=600");
}
function set_assignee(id,newValue) {
  getByID(id).innerHTML = newValue;
}

function do_confirm( string, url ) {
  var answer = confirm(string);
  if (answer) {
    window.location = url;
  } else { 
    return false;
  }
  return true;
}

function toggleCheckBoxes(id,selector) {
  var checks = document.getElementsByName(id);
  var sel = YAHOO.util.Dom.get(selector);
  for (var i=0;i<checks.length;i++) {
    checks[i].checked = sel.checked;
  } 
}

function print_r(x, max, sep, l) {
    l = l || 0;
    max = max || 10;
    sep = sep || ' ';
    if (l > max) {
        return "[WARNING: Too much recursion]\n";
    }
    var i,r = '',t = typeof x,tab = '';
    if (x === null) {
        r += "(null)\n";
    } else if (t == 'object') {
        l++;
        for (i = 0; i < l; i++) { tab += sep; }
        if (x && x.length) { t = 'array'; }
        r += '(' + t + ") :\n";
        for (i in x) {
            try {
                r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
            } catch(e) {
                return "[ERROR: " + e + "]\n";
            }
        }
    } else {
        if (t == 'string') {
            if (x == '') { x = '(empty)'; }
        }
        r += '(' + t + ') ' + x + "\n";
    }
    return r;
};

$.extend({
    URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
                          while(x<c.length){
                              var m=r.exec(c.substr(x));
                              if(m!=null && m.length>1 && m[1]!=''){
                                  o+=m[1];x+=m[1].length;
                              }else{
                                  if(c[x]==' ') o+='+';
                                  else { var d=c.charCodeAt(x);var h=d.toString(16);
                                         o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
    URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
                          while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){
                              b=parseInt(m[1].substr(1),16);
                              t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});

jQuery.createXMLDocument = function(string){
    var browserName = navigator.appName;
    var doc;
    if (browserName == 'Microsoft Internet Explorer')
    {
        doc = new ActiveXObject('Microsoft.XMLDOM');
doc.async = 'false'
        doc.loadXML(string);
    } else {
        doc = (new DOMParser()).parseFromString(string, 'text/xml');
    }
    return doc;
};

