/* var menuids=["airreviewmenu1","treemenu1"]  ids of menus, separated by commas */

/*****************************************************************************************************/
/* Function to build up the drop down menus, defined in CSS */
/******************************************************************************************************/

function buildsubmenus_horizontal(){
for (var i=0; i<menuids.length; i++){
  var ultags=document.getElementById(menuids[i]).getElementsByTagName("ul")
    for (var t=0; t<ultags.length; t++){
		if (ultags[t].parentNode.parentNode.id==menuids[i]){ //if this is a first level submenu
			ultags[t].style.top=ultags[t].parentNode.offsetHeight+"px" //dynamically position first level submenus to be height of main menu item
			ultags[t].parentNode.getElementsByTagName("a")[0].className="mainfoldericon"
		}
		else{ //else if this is a sub level menu (ul)
		  ultags[t].style.left=ultags[t-1].getElementsByTagName("a")[0].offsetWidth+"px" //position menu to the right of menu item that activated it
    	ultags[t].parentNode.getElementsByTagName("a")[0].className="subfoldericon"
		}
    ultags[t].parentNode.onmouseover=function(){
    this.getElementsByTagName("ul")[0].style.visibility="visible"
    }
    ultags[t].parentNode.onmouseout=function(){
    this.getElementsByTagName("ul")[0].style.visibility="hidden"
    }
    }
  }
}

if (window.addEventListener)  
window.addEventListener("load", buildsubmenus_horizontal, false)
else if (window.attachEvent)  
window.attachEvent("onload", buildsubmenus_horizontal)

/*****************************************************************************************************
/* Functions to load HTML into pages rather than Divs, so the DIVs in the loaded pages can be sensed */
/* Parms are url of html.page or if just raw text just document.writes it.                           */
/* 2nd pram, either null if html all, or eg: table, if remove all tables using getGravy */ 
/* Thanks to http://www.xul.fr/ajax/responseHTML-attribute.html for some of the guts of the code     */
/* but it's taken weeks to get it working reliably cross browser 
******************************************************************************************************/

function LoadExternal(urltoload, toremove)
  { 
  var reqxhr = null; 
  try { reqxhr = new ActiveXObject('Msxml2.XMLHTTP'); }     /* Firefox, Opera, and new IE 8 */
  catch (err2) {
    try { reqxhr = new ActiveXObject('Microsoft.XMLHTTP');  /* Old for Old IE: IE 8 fails Can't Access Domain err */ }
      catch (err3) {
      try { reqxhr = new XMLHttpRequest(); }
        catch (err1) { reqxhr = false;
      }
    }
  }


  /* If Firebollox, allow my disc via UniversalBrowserRead */
  try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  } catch (e) { /* Nothing - means we're in IE */  }

  try { reqxhr.open("GET", urltoload, false); } /* False=synchronous (wait) else Firefox cotinues load then clears when I'm done */
    catch (err) {document.write("Error with xhr.open") }
  try { reqxhr.send(null); }
   catch (err) {document.write("Airreview: <A HREF=\"http://www.airreview.com\">Home</a> <A HREF=\"http://www.airreview.com/News/index.htm\">News</a> <A HREF=\"http://www.airreview.com/Seatmap/index.htm\">Seatmaps</a> <A HREF=\"http://www.airreview.com/Contact/index.htm\">Contact</a> <BR><small>ERROR: Loading menu "+urltoload+" failed. XHR State + Status code:"+ reqxhr.readyState+","+reqxhr.status+"</small>") }

    if(reqxhr.readyState == 4)   /* Loaded: don't bother with reqxhr.status==0 as local drive ret 0: bug 331610*/
    {
      if (toremove=="Return") return getBody(reqxhr.responseText); /* For v4, spit top menus */
      if (toremove==null) {document.write(getBody(reqxhr.responseText))} else		/* No removal of HTML */
        { var wibble=""; wibble=getBody(reqxhr.responseText); document.write(getGravy(wibble,toremove))};   /* Need to remove table subsection */
      document.close(); 	/* Catch Firefox bug that leaves window permanently loading! */
    } ;
} 

function getBody(content) 
{
   test = content.toLowerCase();    /* Eliminate case sensitivity */
     var x = test.lastIndexOf("<body");  /* Gotta make it the LAST index, otherwide we'll trigger it here */
   if(x != -1)                      /* IE We do have a body, therefore it IS html */
      x = test.indexOf(">", x);   /* Continue incase we've got lots of style in the body */
     /* if(x == -1) return ""; */
   var y = test.lastIndexOf("</body>");
   if(y == -1) y = test.lastIndexOf("</html>");  /* Someone forgot the body */
   if(y == -1) y = content.length;    /* OK, raw text so grab everything till end */
   return content.slice(x + 1, y);   
}

/*****************************************  getGravy    ***********************************************/
/* Function to strip out sections of HTML we dont want... params are junk... the HTML, and cutword    */
/* the word which signifies the bit we want to removed: normally this will be table meaning, we cut   */
/* Everything between <table> and </table>. This will also remove NESTED tables, by detecting if a new*/
/* <table> appears before the </table>, and removing this, then the main <table>                      */
/* Function will run until it's removed all tables, etc   - JL Airreview March 2011                   */
/******************************************************************************************************/

function getGravy(junk,cutword) 
{
   var guts=junk;
   var gotone=-1;
   var endone =0;
   var doubler=0;

   do {
     gotone=guts.indexOf("<"+cutword);
     if (gotone > 0) {
       endone=guts.indexOf("</"+cutword+">");
       doubler=guts.indexOf("<"+cutword,gotone+1);
       if (doubler != -1 && doubler < endone) {		/* Curses, nested table */
         /* document.write("NESTED "+cutword+": First="+gotone+" Last="+endone+" doubler"+doubler+"<BR>");  */
	 gotone=doubler;				/* Just remove the inside nest */
       }
       guts=guts.slice(0,gotone)+guts.slice(endone+cutword.length+3); 
       /* document.write("First="+gotone+" Last="+endone+"<BR>");		DEBUG */
     }
   } while (gotone!=-1);
   return guts;
}


/*****************************************************************************************************/
/* Function to get the current filename (if blank) or the filename of the passed URL */
/* if URL does not have a filename (is just a directory) returns index */
/******************************************************************************************************/

function fileNoPath(where)
{
  var fullocation = where;
  if (fullocation==null) fullocation= self.location.pathname;
  var slashIndex = fullocation.lastIndexOf("/");
  if (slashIndex==-1) slashIndex = fullocation.lastIndexOf("\\");  /* IE only: backslash=On my C: */ 
  var dotIndex = fullocation.lastIndexOf("."); 
  if (dotIndex == -1) dotindex = fullocation.length;
  if (fullocation.substring(fullocation.length-1, fullocation.length)=="/") return "index";   /*Idiot got here without the htm filename*/
  return fullocation.substring(slashIndex+1, dotIndex);
}

var message = "Instead of using Right Click, please use the colourful share, print or Email buttons on the right hand side or at the bottom of the page, to customise what you want to do."; 
function rtclickcheck(keyp){ if (navigator.appName == "Netscape" && keyp.which == 3){ 	alert(message); return false; } 
if (navigator.appVersion.indexOf("MSIE") != -1 && event.button == 2) { 	alert(message); 	return false; } } 
document.onmousedown = rtclickcheck;

/*****************************************************************************************************/
/* Function to return the current screen width (to test if it is a mobile site */
/******************************************************************************************************/

function viewwidth() {
  var winW = 320;
  if (document.body && document.body.offsetWidth) winW = document.body.offsetWidth;  /* Really old browsers */
  if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth )
    winW = document.documentElement.offsetWidth;  /* IE 6 */
  if (window.innerWidth && window.innerHeight) winW = window.innerWidth;   /* New Browsers */
  return winW;
} 

/*****************************************************************************************************/
/* Function to check on screen size and relocate to mobile or table site if need be */
/******************************************************************************************************/
function MobileRelocate() {
  var xreg=0;
  var newlocation="";
  var currentlocation = document.location.href;
  xreg=viewwidth();
  if (xreg<=640) {newlocation="http://www.airreview.com/Low/Mobile.php?"+currentlocation.substring(25)+"&tablet"+(xreg-20);};
  if (xreg<=340) {newlocation="http://www.airreview.com/Low/Mobile.php?"+currentlocation.substring(25);};
  if (newlocation!='' && currentlocation.indexOf('?nomobile')==-1) {document.write ('Small screen detected: Size'+xreg+' - transfering to mobile site: <A HREF="'+newlocation+'">Click here if you are not redirected</A><br>'+newlocation+'<br>');
  window.location=newlocation; };
}



