// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// |
// | JavaScript functions for the WMI reports application
// | (c) 2005-2006 Steve Jordan, BSRIA Ltd.
// |
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-

var temp_array=new Object();
var isOpera = navigator.userAgent.indexOf("Opera") > -1;
var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera;
var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera; 
var _oldloadingy = -1;
var _oldloadingx = -1;
var _status = true;

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// | Initialise the page
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function init(basket_count){
 check_search_list(document.f1.country,document.f1.checkbox_country);
 check_search_list(document.f1.sector,document.f1.checkbox_sector);
 display_basket(basket_count);
 putstatusontop(true);
 var w=document.getElementById('status').offsetWidth;
 var cw=document.documentElement.clientWidth;
 var g=Math.round((cw-document.getElementById('mainout').offsetWidth) / 2);
 document.getElementById('status').style.left=cw-g-w;
 document.getElementById('istatus').style.left=cw-g-w;
}

function display_basket(count){
 if(document.getElementById("basket")){
  document.getElementById("basket").innerHTML='<a href=\".?a=b\"><img src=\"/reports/gfx/icons/basket.gif\" width=16 height=11 border=0></a>&nbsp;<a href=\".?a=b\">Basket: ';  
  if(count==0)
   document.getElementById("basket").innerHTML=document.getElementById("basket").innerHTML+'Empty</a>';
  else
   if(count==1)
    document.getElementById("basket").innerHTML=document.getElementById("basket").innerHTML+count+' report</a>';
   else
    document.getElementById("basket").innerHTML=document.getElementById("basket").innerHTML+count+' reports</a>';
  }
}


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// | 
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function highlight_all_options(_checkbox,_option){
 if (_checkbox.checked == true){
  for(var i=0;i<_option.length;i++){
   if (_option.options[i].value != '-1')
    _option.options[i].selected = true;
  }
 }
 else
  for(var i=0;i<_option.length;i++)
    _option.options[i].selected = false;
}


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// | 
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function check_search_list(_option,_checkbox){
var _notselected=0;

 if(_option){
  if(_option.length!=0){
   for(var i=0;i<_option.length;i++)
    if(_option.options[i].selected==false && _option.options[i].value != '-1')
      _notselected++;
  }else
   _notselected=-1;
 }

 if (_notselected==0)
  //_checkbox.checked=true;
  _checkbox.value='true';
 else
  //_checkbox.checked=false;
  _checkbox.value='false';
}


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// | 
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function check_search(_country,_category,_sector,_search){
var _return=true;

 if (_category.selectedIndex == -1) _return=false;
 if (_country.selectedIndex == -1)  _return=false;
 if (_sector.selectedIndex == -1)   _return=false;
 //if (_search.value.length < 1)      _return=false;

 return _return;
}


// ************************************************************************************
// FORM FUNCTION EVENTS
// ************************************************************************************


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// | Triggered when RETURN is pressed in the keyword input box
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function keyword_enter(){
 if (window.event && window.event.keyCode == 13){
  if(check_search(document.f1.category, document.f1.country, document.f1.sector, document.f1.keywords) == false){
   alert('An error occurred while submitting your search.\n\nPlease ensure you have selected at leat one item in the category,\ncountry and sector list.');
   return false;
  }
  else{
   document.f1.a.value='s';
   return true;
  }
 }
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// | Triggered when the 'sort by' is changed...submit form
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function order_change(){
 if(check_search(document.f1.category, document.f1.country, document.f1.sector, document.f1.keywords) == false){
   alert('An error occurred while submitting your search.\n\nPlease ensure you have selected at leat one item in the category,\ncountry and sector list.');
 }
 else{
  document.f1.a.value='s';
  document.f1.submit();
 }
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// | Triggered when the Go button/image is clicked
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function Search() {
 if(check_search(document.f1.category, document.f1.country, document.f1.sector, document.f1.keywords) == false){
   alert('An error occurred while submitting your search.\n\nPlease ensure you have selected at leat one item in the category,\ncountry and sector list.');
  return false;
 }
 else{
  set_status('retieving reports...');status_show(); 
  document.f1.a.value='s';
  return true;
 }
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// | Triggered when back or next buttons are clicked
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function search_page(direction,action){
 document.f1.a.value=action;
 document.f1.direction.value=direction;
 return true;
}

function search__page(direction,action){
 document.f1.a.value=action;
 document.f1.direction.value=direction;
 document.f1.submit();
 return true;
}

function jump_2_page(page,action){
 document.f1.a.value=action;
 document.f1.page.value=page;
 document.f1.submit();
 return true;
}


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// | Change an action type and submit form
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function action(a){
 document.f1.a.value=a;
 return true;
}


// ************************************************************************************
// FUNCTIONS TO HANDLE THE RIGHT-HAND SIDE SELECT BOXES
// ************************************************************************************

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// Display the Countries from the returned HTTPXML function
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
display_countries=function(_id,_name,_count){
 for(var i=0;i<document.f1.country.length;i++)
  document.f1.country.options[i]=null;

 document.f1.country.length = 0;
 document.f1.country.length = _id.length;

 if(_id.length==0)
  alert('No reports in this category');
 else{
  for(var i=0;i<_id.length;i++){
   document.f1.country.options[i].value=_id[i];
   document.f1.country.options[i].text=_name[i];// + ' (' + _count[i] + ')';
   document.f1.country.options[i].selected=true;
  }
  check_search_list(document.f1.country,document.f1.checkbox_country); 
  xmlhtttp_get_sectors();
 }
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// Display the Sectors from the returned HTTPXML function
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
display_sectors=function(_id,_name,_count){
 for(var i=0;i<document.f1.sector.length;i++)
  document.f1.sector.options[i]=null;

 document.f1.sector.length = 0;
 document.f1.sector.length = _id.length;

 if(_id.length==0)
  alert('No reports in this country');
 else{
  for(var i=0;i<_id.length;i++){
   document.f1.sector.options[i].value=_id[i];
   document.f1.sector.options[i].text=_name[i];// + ' (' + _count[i] + ')';
   document.f1.sector.options[i].selected=true;
  }
  check_search_list(document.f1.sector,document.f1.checkbox_sector);
 }
}



function advancedsearch(){

 if(document.getElementById('searchmore').style.visibility=='visible')
  document.getElementById('searchmore').style.visibility='hidden';
 else
  document.getElementById('searchmore').style.visibility='visible';
}

function url(url){
 window.location.replace(url);
}

// ************************************************************************************
// HTTP-XML FUNCTIONS
// ************************************************************************************

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// Use XMLHTTP to retrieve search option data
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-

function xmlhtttp_get_countries() {set_status('retieving data...');status_show(); do_xmlhttp('/reports/xmlhttp/countries/?c='+document.f1.category.options[document.f1.category.selectedIndex].value);}
function xmlhtttp_get_sectors()   {set_status('retieving data...');status_show(); do_xmlhttp('/reports/xmlhttp/sectors/?ca='+document.f1.category.options[document.f1.category.selectedIndex].value+'&co='+return_comma_selections(document.f1.country));}
function add2basket(i)            {do_xmlhttp('/reports/xmlhttp/basket/?i='+i);}
function save(i)                  {set_status('saving report...');status_show();do_xmlhttp('/reports/xmlhttp/save/?i='+i);}
function email(i)                 {set_status('emailing report...');status_show();do_xmlhttp('/reports/xmlhttp/email/?i='+i);}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// Do the XML HTTP processing
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function do_xmlhttp(u){
 initxmlhttp();
 if(req!=null){
  req.onreadystatechange = processxmlhttp;
  req.open("GET",u,true);
  req.send(null);
 }
}

function post_xmlhttp(u,c){
 initxmlhttp();
 if(req!=null){
  req.onreadystatechange = processxmlhttp;
  req.open("POST",u,true);
  req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  req.send(c);
 }
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// Initialise the XML HTTP component in the browser to send the request
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function initxmlhttp(){
 try{req=new ActiveXObject("Msxml2.XMLHTTP");}
 catch(e){try{req=new ActiveXObject("Microsoft.XMLHTTP");}
 catch(oc){req=null;}}
 if(!req&&typeof XMLHttpRequest!="undefined")
  req=new XMLHttpRequest();
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// Process the XML HTTP status
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function processxmlhttp(){
 //floater_show();
 if(req.readyState==4){
  if(req.status==200){
   if(req.responseText=="")
    alert('error1');
   else{
    if(req.responseText=='eof')
     alert('no results');
    else{
     eval(req.responseText);
     //floater_hide();
    }
   }
  }
  else{
   alert('A technical error occurred. resulted output was:'+req.responseText);
  }
 }
}


// ************************************************************************************
// MISC FUNCTIONS
// ************************************************************************************

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// Return a string of all selected items in a select box - comma delimited.
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function return_comma_selections(item){
var total=0,selections='';

 for(var i=0;i<item.length;i++)
  if(item.options[i].selected==true)
   total++;

 for(var i=0;i<item.length;i++)
  if(item.options[i].selected==true){
   selections+=item.options[i].value;
   if(total>1)
    selections+=',';
   total--;
  }
 return(selections);
}



function topover(col,over){
 if(over==true)
  col.style.backgroundImage='url(/reports/gfx/fade02_1x50.jpg)';
 else
  col.style.backgroundImage='';


}





function putstatusontop(state)
{
 var DivRef = document.getElementById('status');
 var IfrRef = document.getElementById('istatus');
 if(state)
 {
  DivRef.style.display = "block";
  IfrRef.style.width = DivRef.offsetWidth;
  IfrRef.style.height = DivRef.offsetHeight;
  IfrRef.style.top = DivRef.style.top;
  IfrRef.style.left = DivRef.style.left;
  IfrRef.style.zIndex = DivRef.style.zIndex - 1;
  IfrRef.style.display = "block";
 }
 else
 {
  DivRef.style.display = "none";
  IfrRef.style.display = "none";
 }
}






// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// Set the text of the floating layer which is used when processing
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function set_status(m){
 document.getElementById("statustxt").innerHTML='&nbsp;'+m+'&nbsp;';
 status_update();
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// Show the floating layer
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function status_show(){
 _oldloadingy=-1;
 _oldloadingx=-1;
 
  if(_status){
  status_update();
  document.getElementById('status').style.visibility="visible";
  document.getElementById('istatus').style.visibility="visible";
 }
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// Hide the floating layer after a delay
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function status_hide(t){
  var temp=setTimeout('status_hide2()',100);
}

function status_hide_slow(){
  var temp=setTimeout('status_hide2()',5000);
}

function status_hide2(){
 document.getElementById('status').style.visibility="hidden";
 document.getElementById('istatus').style.visibility="hidden";
}

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
// Keep setting the y value of the floating layer to keep it at the top when scrolling
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-
function status_update(){
 var y=document.documentElement.scrollTop;
 if(y!=_oldloadingy){
   document.getElementById('status').style.top=y;
   document.getElementById('istatus').style.top=y;
  _oldloadingy=y;
 }

 var w=document.getElementById('status').offsetWidth;
 var cw=document.documentElement.clientWidth;
 var g=Math.round((cw-document.getElementById('mainout').offsetWidth) / 2);
 document.getElementById('status').style.left=cw-g-w+'px';
 document.getElementById('istatus').style.left=cw-g-w+'px';

 putstatusontop(true);
 var temp=setTimeout('status_update()',100);
}