/**
 * company /retail interactive functions
 */

function showHierarchy(id,company)
{
    window.open("/sinatra/reports/retail_rankings/?screen=company_info&file=hierarchy.html&id="+id+"&company="+company,new Date().getTime(),'screenX=10,screenY=10,top=10,left=10,height=480,width=800,resizable=1,status=0,scrollbars=1');
}

function showRetailNetwork(id,company)
{
    window.open("/sinatra/reports/retail_rankings/?screen=company_info&file=network.html&id="+id+"&company="+company,new Date().getTime(),'screenX=10,screenY=10,top=10,left=10,height=480,width=800,resizable=1,status=0,scrollbars=1');
}

function showLogo(id,company)
{
    window.open("/sinatra/reports/retail_rankings/?screen=company_info&file=photo.html&id="+id+"&company="+company,new Date().getTime(),'screenX=10,screenY=10,top=10,left=10,height=580,width=680,resizable=1,status=0,scrollbars=1');
}

function showCompany(company)
{
    window.open("/sinatra/reports/retail_rankings/?screen=company_info&file=bottom.html&id=",company,new Date().getTime(),'screenX=10,screenY=10,top=10,left=10,height=480,width=800,resizable=1,status=0,scrollbars=1');
}


/**
 * standard site functions
 */

function refresh(anchor)
{
    // does not refresh page if location the same.
    // will need to modify url to use this.
    /***
    var url = window.location.href;
    var pos = url.indexOf('#');

    if (pos != -1)
        url = url.substring(0,pos);

    url += '#' + anchor;
    location.replace(url);
    **/

    // Javascript 1.2 only
    location.reload(false);
}


function selectAllCheckboxes(form_name)
{
    var inputs = document.forms[form_name].elements.length;

    for (var x=0; x<inputs; x++)
    {
        if (document.forms[form_name].elements[x].type == 'checkbox')
            document.forms[form_name].elements[x].checked = true;
    }
}

function deselectAllCheckboxes(form_name)
{
    var inputs = document.forms[form_name].elements.length;

    for (var x=0; x<inputs; x++)
    {
        if (document.forms[form_name].elements[x].type == 'checkbox')
            document.forms[form_name].elements[x].checked = false;
    }
}


function selectAllCheckboxesOnPage()
{
    var num_forms = document.forms.length;
    var num_inputs;

    for (var x=0; x<num_forms; x++)
    {
        num_inputs = document.forms[x].elements.length;

        for (var i=0; i<num_inputs; i++)
        {
            if (document.forms[x].elements[i].type == 'checkbox')
                document.forms[x].elements[i].checked = true;
        }
    }
}


function deselectAllCheckboxesOnPage()
{
    var num_forms = document.forms.length;
    var num_inputs;

    for (var x=0; x<num_forms; x++)
    {
        num_inputs = document.forms[x].elements.length;

        for (var i=0; i<num_inputs; i++)
        {
            if (document.forms[x].elements[i].type == 'checkbox')
                document.forms[x].elements[i].checked = false;
        }
    }
}


function selectCheckboxesWithHits()
{
    var num_forms = document.forms.length;
    var num_inputs;

    for (var x=0; x<num_forms; x++)
    {
        num_inputs = document.forms[x].elements.length;

        for (var i=0; i<num_inputs; i++)
        {
            if ((document.forms[x].elements[i].type  == 'checkbox') &&
                (document.forms[x].elements[i].value == 'hit') )
            {
                document.forms[x].elements[i].checked = true;
            }
        }
    }
}


// Gets text selected by user, does not work on Opera and Konqueror
function getSelectedText()
{
    var txt = "";

    if (window.getSelection) txt = window.getSelection();
    else if (document.selection) txt = document.selection.createRange().text;

    return txt;
}


function removeExportItems()
{
    //could have used this syntax but netscape 4 cant handle it as usual.
    //document.forms['select_items'].elements['remove_items'].value = 'true';
    document.forms['select_items'].elements[0].value = true;
    document.forms['select_items'].submit();
}


function submitForm(form_name, action, zipped)
{
    var ext;

    if (zipped)
        ext = '.zip';
    else
        ext = '.rtf';

    // note this relies on the form having a hidden value named 'zipped'
    document.forms[form_name].elements['zipped'].value = zipped;
    document.forms[form_name].action = '/sinatra/reports/' + action + '/report' + ext;
    document.forms[form_name].submit();
}


function deleteSelected(form_name, action)
{
/* DF::[11/04/05] No longer need to check this as ensure there are no orphaned
   RSItems in the server code now.

    divTags = document.getElementsByTagName('input');
    fs = new String("fs");
    for (var i = 0; i < divTags.length; i++)
    {
        // If a cover item is checked, check all children as well
        if (divTags.item(i).title == divTags.item(i).name && divTags.item(i).checked)
        {
            fs = "RSItem-" + divTags[i].name;
        }
        if (divTags.item(i).title == fs)
        {
            divTags.item(i).checked = true;
        }
    }
*/

    document.forms[form_name].zipped.value = 'false';
    document.forms[form_name].action = action;
    document.forms[form_name].submit();
}


function submitAllForms(action, zipped)
{
    var num_forms = document.forms.length;
    var num_inputs;
    var first = true;
    var url = '/sinatra/reports/' + action + '/report';
    var ext;

    if (zipped)
        url += '.gz?zipped=true&';
    else
        url += '.rtf?';

    for (var x=0; x<num_forms; x++)
    {
        num_inputs = document.forms[x].elements.length;

        for (var i=0; i<num_inputs; i++)
        {
            if ((document.forms[x].elements[i].type  == 'checkbox') &&
                (document.forms[x].elements[i].checked == true) )
            {
                if (first == true)
                    first = false;
                else
                    url += '&';

                url += document.forms[x].elements[i].name;
            }
        }
    }

    open(url, '_self');
}


function loadPageItem()
{
    var index = document.forms['page_list_form'].elements['page_list'].selectedIndex;
    var href  = document.forms['page_list_form'].elements['page_list'].options[index].value;
    open(href, '_self');
}


//these functions are form generating form input fields
function printOption(name, value, thevar)
{
    if (thevar == value)
        document.write('<option value=' + value + ' selected>' + name);
    else
        document.write('<option value=' + value + '>' + name);
}


var std_window_attributes = 'resizable=yes,copyhistory=no,menubar=no,directories=no,toolbar=no,location=no,scrollbars=yes,statusbar=yes'
var new_ann_window_attributes = std_window_attributes + ',left=100,top=50,width=800,height=650'
var ann_window_attributes = std_window_attributes + ',left=100,top=50,width=400,height=600'
var del_window_attributes = std_window_attributes + ',left=0,top=0,width=1,height=1'
var print_window_attributes = std_window_attributes + ',left=50,top=50,width=650,height=450,menu=yes,menubar=yes,scrollbars=yes,resizable=yes'

function delConfirm( )
{
    if ( window.confirm( 'Delete this comment?' ) )
    {
        document.comment_form.comment_text.value='';
        document.comment_form.submit();
    }
}


function newComment( item_id, atom_id, parent_id, def_prof, scope, subject_type )
{
    if ( def_prof == 'false' )
    {
        self.name = 'mintel_reports.main';
        window.open( '/sinatra/premier/annotate/comment/edit?' + 
                        'item_id=' + item_id + 
                        '&atom_id=' + atom_id + 
                        '&parent_id=' + parent_id + 
                        (scope ? ('&scope=' + scope) : '') +
                        (subject_type ? ('&subject_type=' + subject_type) : ''),
                     'annotations',
                     ((parent_id && (scope!='group') && subject_type != 2) || scope=='mintel') ? ann_window_attributes : new_ann_window_attributes );

        return false;
    }
    return true;
}


function editComment( item_id, atom_id, comment_id, parent_id, last_comment_id, scope, subject_type )
{
    self.name = 'mintel_reports.main';
    window.open( '/sinatra/premier/annotate/comment/edit?' +
            'item_id=' + item_id +
            '&atom_id=' + atom_id +
            '&comment_id=' + comment_id +
            '&parent_id=' + parent_id +
            (last_comment_id ? ('&last_comment_id=' + last_comment_id) : '') +
            '&scope=' + scope +
            (subject_type ? ('&subject_type=' + subject_type) : ''),
                 'annotations', 
                 (scope=='group' ? new_ann_window_attributes : ann_window_attributes) );
    return false;
}


function editUserList( item_id, atom_id, comment_id, scope )
{
    self.name = 'mintel_reports.main';
    window.open( '/sinatra/premier/annotate/comment/edit_user?item_id=' + item_id + '&atom_id=' + atom_id + '&comment_id=' + comment_id + '&scope=' + scope,
                 'annotations', ann_window_attributes );
    return false;
}


function deleteComment( item_id, atom_id, comment_id, last_comment_id, scope, subject_type )
{
    self.name = 'mintel_reports.main';
    if ( window.confirm( 'Delete this comment?') )
    {
        window.open( '/sinatra/premier/annotate/comment/delete?' +
                'item_id=' + item_id +
                '&atom_id=' + atom_id +
                '&comment_id=' + comment_id +
                (last_comment_id ? ('&last_comment_id=' + last_comment_id) : '') +
                '&scope=' + scope +
                (subject_type ? ('&subject_type=' + subject_type) : ''),
                'del_comment', del_window_attributes );
        return false;
    }
    return false;
}

function printWindow( item_id )
{
    self.name='mintel_vanilla_main';
    window.open( '/sinatra/reports/print/id=' + item_id, 'print_item', print_window_attributes );
    return false;
}

function printTables( item_id )
{
    self.name='mintel_vanilla_main';
    window.open( '/sinatra/premier/displaytables/target=printer&id=' + item_id,
                 'print_item', print_window_attributes );
    return false;
}


function setEmailAlert(search_id)
{
    var list_name = 'alert' + search_id;
    var index = document.forms['searches'].elements[list_name].selectedIndex;
    var value = document.forms['searches'].elements[list_name].options[index].value;
    var url = '?set_alert&search_id=' + search_id + '&interval=' + value + '#' + search_id;
    location = url;
}


function setPresetAlert(id)
{
    var list_name = 'preset_alert' + id;
    var index = document.forms['searches'].elements[list_name].selectedIndex;
    var value = document.forms['searches'].elements[list_name].options[index].value;
    var url = '?set_preset_alert&index=' + id + '&interval=' + value + '#' + id;
    location = url;
}

//creates an array of any type - given a size (n)
function List(n)
{
    this.length = n;
    for (var i=1; i<=n; i++)
    {
        this[i] = 0;
        return this;
    }
}

function printList(list, code_list, val)
{
    for (var i=0; i<list.length; i++)
    {
        if (val == code_list[i])
            document.write('<option value=' + code_list[i] + ' selected>' + list[i]);
        else
            document.write('<option value=' + code_list[i] + '>' + list[i]);
    }
}

function printListValue(list, code_list, val)
{
    for (var i=0; i<list.length; i++)
    {
        if (val == code_list[i])
        {
            document.write(list[i]);
            break;
        }
    }
}


function setAccessFilter(req_cur_url)
{
    var cur_url_loc = document.location;
    var cur_url = document.location;

    if (cur_url_loc.pathname=="/" && cur_url_loc.search=="")
    {
       cur_url = cur_url_loc.href + "index.html";
    }
    else
    {
       cur_url = cur_url_loc.href;
    }

    var index = document.forms['access_filter'].elements['set_access_filter'].selectedIndex;
    var value = document.forms['access_filter'].elements['set_access_filter'].options[index].value;
    var set   = cur_url.indexOf('set_access_filter=');

    while (set >= 0)
    {
        cur_url = cur_url.substring(0,set-1) + cur_url.substring(set+25,cur_url.length);
        set = cur_url.indexOf('set_access_filter=');
    }

    // Because some URLs are like "reports.mintel.com/.../view/blah&1234#567"
    // it's not ok to just append the & param on the end as it'll get treated as part
    // of the # anchor bit.  So need to find the last instance of a '/' in the url and insert
    // the &set_access_filter there to guarantee if gets noticed.
    if (cur_url.indexOf('#') >= 0)
    {
        var slash_index = cur_url.lastIndexOf('/');
        if (slash_index >= 0 && slash_index < cur_url.length )
        {
        var rest_of_string = cur_url.substring(slash_index+1, cur_url.length);
        if (rest_of_string.charAt(0) != '&') rest_of_string = '&' + rest_of_string;
            location = cur_url.substring(0,slash_index+1) + '&set_access_filter=' + value + rest_of_string;
        }
        else
        {
            location = cur_url + '&set_access_filter=' + value;
        }
    }
    else
    {
        location = cur_url + '&set_access_filter=' + value;
    }


}

function setSortOrder()
{
    var cur_url = document.location.href;

    var index = document.forms['select_items'].elements['order_by'].selectedIndex;
    var value = document.forms['select_items'].elements['order_by'].options[index].value;
    var set   = cur_url.indexOf('sort=');

    while (set >= 0)
    {
        // remove the sort parameter value (i.e. look for the 
        var rest = cur_url.substring(set+5,cur_url.length);
        var next_parm = rest.indexOf('&');
        if (next_parm >= 0) 
        {
            rest = rest.substring(next_parm,rest.length);
        }
        else
        {
            rest = '';
        }
        
        // reconstruct the string without the &sort=something
        cur_url = cur_url.substring(0,set-1) + rest;
        set = cur_url.indexOf('sort=');
    }
    
    location = cur_url + '&sort=' + value;
}



function changeUrl(args)
{
    var index = document.forms['versions'].elements['version_list'].selectedIndex;
    var value = document.forms['versions'].elements['version_list'].options[index].value;
    var url   = './' + 'id=' + value + args.substring(args.indexOf('&'));
    location  = url;
}


/**
 * image functions
 */

function MM_swapImgRestore()
{
    var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages()
{
    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d)
{
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage()
{
    var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
    if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/** Show and hide table rows by id
 *  @author Matt Hall
 */

function showTag(id, tag)
{
    divTags = document.getElementsByTagName(tag);

    for (var i = 0; i < divTags.length; i++)
    {
        if (divTags.item(i).id == id)
            divTags[i].style.display = '';
    }
}

function hideTag(id, tag)
{
    divTags = document.getElementsByTagName(tag);

    for (var i = 0; i < divTags.length; i++)
    {
        if (divTags.item(i).id == id)
                divTags[i].style.display = 'none';
    }
}

function showhideTag(id, tag)
{
    divTags = document.getElementsByTagName(tag);

    for (var i = 0; i < divTags.length; i++)
    {
        if (divTags.item(i).id == id)
        {
            divTags[i].style.display == '' ?
                divTags[i].style.display = 'none' : divTags[i].style.display = '';
        }
    }
}

// show/hide a layer by id alone, no need to specify tag name
function showhideUniq( id )
{
    tag = document.getElementById(id);
    tag.style.display == '' ? tag.style.display = 'none' :
                              tag.style.display = '';
}

// Show/hide a layer and its associated +/- buttons
// (which are prefixed with add_ and sub_
function showHideAll( id )
{
    showhideUniq(id);
    showhideUniq('add_'+id);
    showhideUniq('sub_'+id);
}

/** Check and disable checkboxes by id
 *  @author Matt Hall
 */

function selectenable(checkid, myid, sel)
{
    divTags = document.getElementsByTagName('input');

    for (var i = 0; i < divTags.length; i++)
    {
        // Alter disabled/checked attributes for child checkboxes

        if (divTags.item(i).title == checkid)
        {
            //divTags[i].disabled = sel;
            divTags[i].checked = sel;
        }

        // Make sure any other checkboxes of the same id match this one

        if (divTags.item(i).title == myid)
        {
            divTags[i].checked = sel;
        }
    }
}

// Enable element 'id' if element 'if_checked' is checked: otherwise, disable it.
function enableIfChecked( id, if_checked )
{
    var layer = document.getElementById( id )
    var check = document.getElementById( if_checked )
    if ( layer && check )
    {
        layer.disabled = !check.checked;

        if ( layer.className.search("(disabled|enabled)") == -1 )
        {
            layer.className += (check.checked ? " enabled" : " disabled");
        }
        else
        {
            if ( check.checked )
                layer.className = layer.className.replace("disabled","enabled");
            else
                layer.className = layer.className.replace("enabled","disabled");
        }
    }
    return false;
}

// Focus on the given element
function setFocus( id )
{
    var layer = document.getElementById(id)
    if ( layer )
        layer.focus();
    return false;
}

function openGraphWizard(item_id, figure, graph)
{
    var url = '/sinatra/reports/graphing/item_id=' + item_id + '&figure=' + figure;

    if (graph != "0_0")
        url += '&mode=draw&load=' + graph;

    open( url,
         'graphing'+graph,
         'toolbar=no,menu=no,status=yes,scrollbars=yes,resizable=yes,top=30,left=30,height=650,width=870');
}


function popupNewWindow(url, name)
{
   open(url, name, 'menubar=yes,toolbar=yes,scrollbars=yes');
}

// Global used to jump between hits on a page
hitID=1;

function jumpHit(jump, numHits)
{
    hitID += jump;

    if (jump != 0)
    {
        window.location.hash = "hit" + hitID;
    }

    if (hitID > 1)
    {
        showTag('prevbutton','span');
        hideTag('prevgrey','span');
    }
    else
    {
        hideTag('prevbutton','span');
        showTag('prevgrey','span');
    }

    if (hitID < numHits)
    {
        showTag('nextbutton','span');
        hideTag('nextgrey','span');
    }
    else
    {
        hideTag('nextbutton','span');
        showTag('nextgrey','span');
    }
}

// Function used by jump to site dropdown on each page
function jumpSite()
{
  var idx=document.forms['sites'].elements['site_list'].selectedIndex;
  var url=document.forms['sites'].elements['site_list'].options[idx].value;

  if (url != 'none')
  {
    var winname='dogs';
    window.open(url,winname);
  }
}

function define() {}

function getCookie(NameOfCookie){
    if (document.cookie.length > 0) {
    begin = document.cookie.indexOf(NameOfCookie+"=");
    if (begin != -1) {
      begin += NameOfCookie.length+1;
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
        return unescape(document.cookie.substring(begin, end));
    }
  }
  return null;
}

function setCookie(NameOfCookie, value) {

  document.cookie = NameOfCookie + "=" + escape(value) + '; path=/sinatra/premier;';
}


function flipHighlight()
{
    Preference=getCookie('Highlight');

    // If highlighting is on or we don't have a cookie,
    // turn it off
    if (Preference=='true' || Preference==null )
    {
        setCookie('Highlight', 'false');
        showTag('hloff','div');
        hideTag('hlon','div');

        divTags = document.getElementsByTagName('span');

        for (var i = 0; i < divTags.length; i++)
        {
            if(  divTags[i].className == 'hit_word' )
                divTags[i].className = 'hit_word_off';
        }
    }
    // If higlighting is off, turn it on
    else
    {
        setCookie('Highlight', 'true', 1);
        showTag('hlon','div');
        hideTag('hloff','div');

        divTags = document.getElementsByTagName('span');

        for (var i = 0; i < divTags.length; i++)
        {
            if(  divTags[i].className == 'hit_word_off' )
                divTags[i].className = 'hit_word';
        }
    }
}

/* Function for switching between the different price types and deciding whether or not to show the TVA code */

function showprice( selecta )
{
    switch(selecta)
    {
        case 0:
            document.getElementById( '1ALL_UK' ).style.display = '';
            document.getElementById( '2BUS_EU' ).style.display = 'none';
            document.getElementById( '3PRI_EU' ).style.display = 'none';
            document.getElementById( '4ALL_US' ).style.display = 'none';
            document.getElementById( '5ALL_XX' ).style.display = 'none';
            document.booking.tvacode.value="Not required";
            document.booking.tvacode.disabled = true;
            break;
        case 1:
            document.getElementById( '1ALL_UK' ).style.display = 'none';
            document.getElementById( '2BUS_EU' ).style.display = '';
            document.getElementById( '3PRI_EU' ).style.display = 'none';
            document.getElementById( '4ALL_US' ).style.display = 'none';
            document.getElementById( '5ALL_XX' ).style.display = 'none';
            document.booking.tvacode.value="";
            document.booking.tvacode.disabled = false;
            break;
        case 2:
            document.getElementById( '1ALL_UK' ).style.display = 'none';
            document.getElementById( '2BUS_EU' ).style.display = 'none';
            document.getElementById( '3PRI_EU' ).style.display = '';
            document.getElementById( '4ALL_US' ).style.display = 'none';
            document.getElementById( '5ALL_XX' ).style.display = 'none';
            document.booking.tvacode.value="Not required";
            document.booking.tvacode.disabled = true;
            break;
        case 3:
            document.getElementById( '1ALL_UK' ).style.display = 'none';
            document.getElementById( '2BUS_EU' ).style.display = 'none';
            document.getElementById( '3PRI_EU' ).style.display = 'none';
            document.getElementById( '4ALL_US' ).style.display = '';
            document.getElementById( '5ALL_XX' ).style.display = 'none';
            document.booking.tvacode.value="Not required";
            document.booking.tvacode.disabled = true;
        case 4:
            document.getElementById( '1ALL_UK' ).style.display = 'none';
            document.getElementById( '2BUS_EU' ).style.display = 'none';
            document.getElementById( '3PRI_EU' ).style.display = 'none';
            document.getElementById( '4ALL_US' ).style.display = 'none';
            document.getElementById( '5ALL_XX' ).style.display = '';
            document.booking.tvacode.value="Not required";
            document.booking.tvacode.disabled = true;
            break;
    }
}

function load_slide_image()
{
  if ((obj=document.getElementById("slide_img")) && Player.currentMedia)
  {
    var new_url=Player.currentMedia.getItemInfo('HTMLView');
    if (new_url && obj.src!=new_url)
    {
      obj.src=new_url;
    }
  }
}

