function popup(url, width, height, window_name)
{
  if (!window_name) window_name = 'new_win';
  var left = (screen.width - width) / 2;
  var top = (screen.height - height) / 2;
  var new_win = window.open(url, window_name, "width="+width+",height="+height+",left="+left+",top="+top+",scrollbars=yes,resizable=yes");
  return new_win;
}

function confirmDelete(message, url)
{
    toggleObjects(false);
    Ext.Msg.confirm('Please Confirm', message, function(btn){
        if (btn == 'yes')
        {
            location.href = url;
        }
        toggleObjects(true);
    });
}

function massModifyConfirmDelete (message, selectEl, formId)
{
    toggleObjects(false);
    Ext.Msg.confirm('Please Confirm', message, function(btn){
        if (btn == 'yes')
        {
            formEl = document.getElementById(formId);
            if (formEl)
            {
                // this "action" is the hidden input field
                formEl.action.value = selectEl.value;
                formEl.submit();
            }
        }
        toggleObjects(true);
    });   
}

function toggleObjects(state)
{
    var obj_tags = Ext.DomQuery.select('object');
    for (var i=0; i<obj_tags.length; i++)
    {
        var obj = Ext.get(obj_tags[i]);
        if (obj.select('param[name=movie]').elements.length > 0)
        {
            obj.dom.style.visibility = state ? '' : 'hidden';
        }
    }
}


function parseScriptTags(obj, scope)
{
//    if (!scope) scope = window;
    tags = obj.getElementsByTagName('script');
    for (var i=0; i<tags.length; i++)
    {
        if (tags[i].src == "")
        {
            // Here is our old code:
            //scope.eval(tags[i].innerHTML);

            // The old code didn't work in IE. See the following two articles for the new solution:
            // see: http://blog.modp.com/2008/11/javascript-eval-in-global-scope.html
            // ... read comments as well.
            // also: http://josephsmarr.com/2007/01/31/fixing-eval-to-use-global-scope-in-ie/
            
            // this should register the given script in the global scope
            if (window.execScript) // IE likes to do things differently
                window.execScript(tags[i].innerHTML);
            else
                eval.call(null, tags[i].innerHTML);
        }
        else
        {
            include_once(tags[i].src);
        }
    }
}

function include(file)
{
    var head = document.getElementsByTagName('head').item(0);
    var script = document.createElement('script');
    script.setAttribute('language', 'javascript');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', file);
    head.appendChild(script);
}

function include_once(file)
{
    var scripts = document.getElementsByTagName('script');
    var found = false;
    for (var i=0; i<scripts.length; i++)
    {
        if (scripts[i].src == file)
        {
            found = true;
        }
    }

    if (!found)
    {
        included_js_files.push(file);
        include(file);
    }
}

function evalResponse(response)
{
    obj = getJSON(response.responseText);
    if (obj && obj.isError && obj.msg)
    {
        Ext.MessageBox.setIcon(Ext.MessageBox.ERROR);
        Ext.MessageBox.alert('Error', obj.msg);
    }
    if (obj && obj.isAlert && obj.msg)
    {
        Ext.MessageBox.setIcon(Ext.MessageBox.ERROR);
        Ext.MessageBox.alert('Alert', obj.msg);
    }
    return obj;
}

function getJSON(txt)
{
    try 
    {
        obj = Ext.util.JSON.decode(txt);
    }
    catch(e)
    {
        return {};
    }

    return obj;
}

/*
 * You can pass in either a single id and show value, or you can pass in an array
 * of IDs and an array of show values.
 */
function toggleRow(id, show)
{
	// if we were not passed in arrays, then make them arrays
	if (!(id instanceof Array))
	{
		id = new Array(id);
	}
	if (!(show instanceof Array))
	{
		show = new Array(show);
	}
	
	for(var i=0; i<id.length; i++)
	{
	    var e = Ext.get(id[i]);
	
	    if (show[i])
	    {
	        e.removeClass('hidden');
	    }
	    else
	    {
	        e.addClass('hidden');
	    }
    
        updateRowParity(e.findParentNode('table'));
	}
}

function updateRowParity(table, oddClass, evenClass)
{
    if (!oddClass) oddClass = 'odd';
    if (!evenClass) evenClass = 'even';

    if (typeof(table) == 'string')
    {
        table = document.getElementById(table);
    }
    
    var rows = table.getElementsByTagName('tr');
    var p = oddClass;
    for (var i=0; i<rows.length; i++)
    {
        if (Ext.get(rows[i]).hasClass('headers'))
        {
            var p = evenClass;
        }
        else
        {
            if (rows[i].style.display != 'none' && !Ext.get(rows[i]).hasClass('hidden'))
            {
                Ext.get(rows[i]).removeClass(p==oddClass ? evenClass : oddClass);
                Ext.get(rows[i]).addClass(p);
                p = (p == oddClass) ? evenClass : oddClass;
            }
        }
    }
}

