var useRight = false;

function getXMLHTTPObj(){
//Create a boolean variable to check for a valid IE instance.
	var xmlhttp = false;

	//Check if we are using IE.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using IE.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}

	//If we are using a non-IE browser, create a JavaScript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
return xmlhttp;
}

   /**
    *
    * @access public
    * @return void
    **/
   function generateContent( intLink, yes ){
   	getPic();
   		//the var yes indicates there are sub-menu items to display
   	  document.getElementById('subMenu').style.display='none';
   	  xmlhttp = getXMLHTTPObj();
      var objID = "displayInfo";
      var serverPage = "../classes/textController.php";
      if( intLink != undefined ){
         serverPage += "?l="+intLink;
      }
  	  obj = document.getElementById(objID);
	  obj.style.display = "block";
	  obj.innerHTML = "Retrieving information ...";
   	  
   	  xmlhttp.open("GET", serverPage);
   	  xmlhttp.onreadystatechange = function() {
   	   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
   		 obj.innerHTML = xmlhttp.responseText;
   		 if(yes)showSubMenu(intLink);
   	     }
   	  }
   		xmlhttp.send( null );
		swapBannerImage(yes);
		
   	}
	
	function swapBannerImage(yes){
		var p = location.protocol;
		var h = location.host;
		if (document.getElementById('bvaBanner')) {
			var b = document.getElementById('bvaBanner');
			
			if (b.src == p+'//'+h+'/img/banners/benefits.gif') {
				b.src = p+'//'+h+'/img/banners/learn.gif';
			}else {
				b.src = p+'//'+h+'/img/banners/benefits.gif';
			}
		} else {
			var subMenu = document.getElementById('subMenu').style.display;
			if (subMenu == 'none' && !yes ) {
				var a = document.createElement('a');
				a.setAttribute('href', 'http://www.bva.co.uk');
				a.setAttribute('target', '_blank');
				a.setAttribute('title', 'link to BVA');
				a.setAttribute('id', 'bvaBannerLink');
				var i = document.createElement('img');
				i.src = p+'//'+h+'/img/banners/benefits.gif';
				i.setAttribute('id', 'bvaBanner');
				i.setAttribute( 'alt','bva banner');
				i.className = 'banner';
				a.appendChild(i);
				document.getElementById('pageContainer').appendChild( a );
			}
		}
	}
	
	function create( elem ){
				
	}
   	
   	function showSubMenu( intLink ){
//	 var sMenu = document.getElementById('subMenu');
   	  for( i=0; i<document.links.length; i++){
   	  		e = document.links[i].id;
   	  		if( e == intLink ){
   	  			document.links[i].className='selected';
   	  		}else{
   	  			document.links[i].className='';
   	  		}
   	  	}
      obj = document.getElementById('subMenu');
      obj.style.display = "block";
	  swapBannerImage();
   	}
   	
   	function generateEvent( intLink, intId ){
   	 //document.getElementById('subMenu').style.display='none';
   	  xmlhttp = getXMLHTTPObj();
      objID = "displayInfo";
      serverPage = "../classes/textController.php";
      if( intLink != undefined ){
         serverPage += "?e="+intLink;
      }
  	  obj = document.getElementById(objID);
	  obj.style.display = "block";
	  obj.innerHTML = "Event Details Loading...";
   	  
   	  xmlhttp.open("GET", serverPage);
   	  xmlhttp.onreadystatechange = function() {
   	   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
   		 obj.innerHTML = xmlhttp.responseText;
   	     }
   	  }
   		xmlhttp.send( null );
   		getPic();
   	}
	
	function getPic(){
		if( useRight ){
			useRight = false;
		}else{
			useRight = true;
		}
		var p = location.protocol;
		var h = location.host;
		var seed = 9;
		//generate a random number
		var leftRan = getRandomNumber( seed );
		var rightRan = getRandomNumber( seed );
		while( leftRan == rightRan ){
			rightRan = getRandomNumber( seed );
		}
		//get the current images
		var leftImg = document.getElementById( 'frontLeftImage' );
		var rightImg = document.getElementById( 'topRightImage' );
		
		var picPage = "../classes/helpers/imageController.php?rn="+rightRan+'&ln='+leftRan;
	   	xmlpic = getXMLHTTPObj();
   	    xmlpic.open("GET", picPage);
	    xmlpic.onreadystatechange = function() {
	   	if (xmlpic.readyState == 4 && xmlpic.status == 200) {
			imgs = xmlpic.responseText;
			//split imgs into the two images.
			rightsource = p+'//'+h+'/img/front/'+getSubStringUpTo( imgs );
			leftsource = p+'//'+h+'/img/front/'+getImageFromPath( imgs );
			if( useRight ){
				if( rightImg.src != leftsource ){
					leftImg.src = leftsource;	
				}else{
					leftImg.src = rightsource;
				}
				
			}else{
				if( leftImg.src != rightsource ){
					rightImg.src = rightsource;
				}else{
					rightImg.src = leftsource;
				}
			}
	   	  	
	   	}
	   }
	   	xmlpic.send( null );
		
   	}
	
	function getRandomNumber( limit ){
		 return Math.floor( Math.random()*limit );
	}
	
	function getImageFromPath( thePath ){
		var x = thePath.lastIndexOf( '/' );
		return( thePath.substr( x+1 ) );
	}
	
	function getSubStringUpTo( thePath ){
		var x = thePath.lastIndexOf( '/' );
		return thePath.substr( 0, x );
	}
   	
   	function showDropDown(){
   		theObject = document.getElementById('dropDown');
   		theObject.style.visibility='visible';
   		var posx = 0;
   		var posy = 0;
   		posx = e.clientX + document.body.scrollLeft;
   		posy = e.clientY + document.body.scrollTop;
   		theObject.style.left = posx+'px';
   		theObject.style.top  = posy+'px';
   	}
	
	function checkUpload(){
		theFile = document.getElementById( 'theFile' ).value;
		regex = '[ ]';
		if( theFile.match( regex )){
			alert( 'please rename the file without spaces' );
			return false;
		}
		testThese = new Array( ".doc", ".pdf", ".xsl", ".txt", ".jpg", ".gif", ".png" );
		var i;
		var ext=theFile.substr(-4);
		var bGood = false
		for( i in testThese ){
			if ( testThese[i] == ext ) bGood = true;
		}
		if (!bGood) {
			alert("please use one of these file types only\n.doc, .pdf, .xsl, .txt, .jpg, .gif, .png");
			return false;
		}
		document.form[0].submit();
	}
   	
