// #############################################################################
// GENERAL FUNCTIONS
// #############################################################################

  // Image pre-loader - from Dreamweaver hence it's ugly as hell
  function MM_preloadImages() { //v3.0
  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_swapImgRestore() { //v3.0
  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_findObj(n, d) { //v4.01
  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);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
  }
  function MM_swapImage() { //v3.0
  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];}
  }
  

// -----------------------------------------------------------------------------      
  // Preload functions for specific pages as specified by 'pagename'
  function preLoad(pagename) {
  
  
    if (pagename == 'expertise') {
    ToggleTab(1);
    }
    
  
  }

// -----------------------------------------------------------------------------   
  // Function to hide/unhide a given DIV
  function toggleLayer( whichLayer )
  {
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
  }  

// -----------------------------------------------------------------------------    
  // Function to set the opacity of an object in different browsers
  function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
  }

// -----------------------------------------------------------------------------    
  // Function to fade in a given Div
  function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
  }

// -----------------------------------------------------------------------------    
  // ... and to fade one out
  function fadeOut(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity >= 0) {
      setOpacity(obj, opacity);
      opacity -= 10;
      window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 100);
    }
  }
  }
  
// -----------------------------------------------------------------------------   
  // This function allows me to take parameters from the URL
  // Example URL: http://www.foo.com/index.html?bob=123&frank=321&tom=213#top
  // Usage: to get the value of "frank" you use:
  // var frank_param = gup( 'frank' );

  function gup( name )
  {  
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec( window.location.href );  
    
    if( results == null )    
      return "";  
    else    
      return results[1];    
  }

// #############################################################################
// EXPERTISE PAGE 
// #############################################################################

// Tab toggler
  function ToggleTab(TabID) {
  var DivToDisplay = 'expertise' + TabID;
  var TabToActivate = 'tab' + TabID;
  // Reset all tabs, then activate the right one
  var Div1 = document.getElementById('expertise1').style; 
  var Div2 = document.getElementById('expertise2').style;  
  var Div3 = document.getElementById('expertise3').style;  
  var Div4 = document.getElementById('expertise4').style;  
  
  var Tab1 = document.getElementById('tab1');   
  var Tab2 = document.getElementById('tab2'); 
  var Tab3 = document.getElementById('tab3'); 
  var Tab4 = document.getElementById('tab4'); 
  
  Div1.display = 'none';
  Div2.display = 'none';
  Div3.display = 'none';
  Div4.display = 'none';
  document.getElementById(DivToDisplay).style.display = 'block';
  
  Tab1.innerHTML = (TabID == 1) ? 'Management Consultancy' : '<a href="javascript:ToggleTab(1);">Management Consultancy</a>';
  Tab2.innerHTML = (TabID == 2) ? 'Financial Services' : '<a href="javascript:ToggleTab(2);">Financial Services</a>';
  Tab3.innerHTML = (TabID == 3) ? 'Practice areas' : '<a href="javascript:ToggleTab(3);">Practice areas</a>';
  Tab4.innerHTML = (TabID == 4) ? 'Typical roles' : '<a href="javascript:ToggleTab(4);">Typical roles</a>';
  
  Tab1.className = (TabID == 1) ? 'tab_box_active' : 'tab_box';
  Tab2.className = (TabID == 2) ? 'tab_box_active' : 'tab_box';
  Tab3.className = (TabID == 3) ? 'tab_box_active' : 'tab_box';
  Tab4.className = (TabID == 4) ? 'tab_box_active' : 'tab_box';
  
  }

// #############################################################################
// CONTACT FORM 
// #############################################################################

  // Quick function to restored the buttons on the Contact form if there is an error
  function RestoreButtonsCForm()
  {
  var RestoreButtons = '<input type="submit"  class="send-button" value="Submit Form &raquo;" name="submit"> or <a href="#" onClick="javascript:document.ContactForm.reset();return false">reset form</a>';
  document.getElementById('SubmitTag').innerHTML=RestoreButtons;
  }
  
// -----------------------------------------------------------------------------   
  // Function to check if the e-mail address is valid
  function CheckContactEmail() {
    email = document.ContactForm.EmailAddress.value
    AtPos = email.indexOf("@")
    StopPos = email.lastIndexOf(".")
      
    if (email == "") {
    return false;
    }
    
    if (AtPos == -1 || StopPos == -1) {
    return false;
    }
    
    if (StopPos < AtPos) {
    return false;
    }
    
    if (StopPos - AtPos == 1) {
    return false;
    } 
    
    return true;
  }

// ----------------------------------------------------------------------------- 
  // Function to validate the contact form
  function validateContactForm()
  {
    
    document.getElementById('SubmitTag').innerHTML='Submitting...';
    if (document.ContactForm.YourName.value == '')
  	{
  		// No name given
  		alert('ERROR: Please enter your name.');
      RestoreButtonsCForm();
      return false;
  	}
  	else if (CheckContactEmail() == false) {
      alert('ERROR: Please enter a valid e-mail address');
      RestoreButtonsCForm();
      return false;
    }
    else if ((document.ContactForm.Comments.value == '') || (document.ContactForm.Comments.value == 'Please provide some detail to help us respond to your enquiry.'))
  	{
      alert('ERROR: Please enter something into the comments box');
      RestoreButtonsCForm();
      return false;
    }
    
  	// If the script gets this far through all of your fields
  	// without problems, it's ok and you can submit the form
  
  	return true;
  }
  


