// Standards for Highways Javascript file

// JavaScript primer tests for JavaScript quickly, if it's there applies a
// class to the specific tags that need CSS rules to follow
function javaScriptPrimer(){
	if (!(document.getElementById('navigation') && document.createElement)){return;}
	
	var nav = document.getElementById('navigation');
	nav.className += 'javaScriptEnabled';
}

// Date script for header file
	function headerDate()
	{
		var now = new Date();
	
		var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	
		var date = now.getDate();
		var month = months[now.getMonth()];
		var year = now.getFullYear();
	
		document.write(date + ' | ' + month + ' | ' + year);
		
	}
	
	/********* High contrast JS */	
	function highContrastSwitch(elementHook){
		// test to see if the element is there and the browser can read/write from the DOM
		if (!(document.getElementById(elementHook) && document.createElement)){return;}
			
		var y = document.getElementById(elementHook);						
		
		// create the p block and populate with link switch
		var p=y.appendChild(document.createElement('p'));
		var a=p.appendChild(document.createElement('a'));
		
		// add any p block features
		p.id="contrastControl";
		
		// Add the building blocks of the <a> element
		a.id = 'contrastSwitchOn';
		a.href='#contrastSwitchOn';
		a.innerHTML = 'Switch to high contrast view';
		// end <a>		
		
		// Prepare the document hooks
		var bod = document.getElementsByTagName("body")[0];
		var headID = document.getElementsByTagName("head")[0];         
		getActiveStyleSheet("High Contrast", "Disable");
		
		// Check if cookie exists, if so read what the pref was,
		// if not then create a cookie
		if (readCookie("standardsForHighwaysContrast")){
			a.id = readCookie("standardsForHighwaysContrast");
			if (a.id == 'contrastSwitchOn'){							
				bod.className = 'highcontrast';					
				a.id = 'contrastSwitchOff';
				a.href='#contrastSwitchOff';
				a.innerHTML = 'Switch to normal view';		
				createCookie("standardsForHighwaysContrast","contrastSwitchOn");
				getActiveStyleSheet("High Contrast", "Enable");
			} else if (a.id=='contrastSwitchOff') {					
				bod.className = '';				
				a.id = 'contrastSwitchOn';
				a.href='#contrastSwitchOn';
				a.innerHTML = 'Switch to high contrast view';
				createCookie("standardsForHighwaysContrast","contrastSwitchOff");
				getActiveStyleSheet("High Contrast", "Disable");
			}
		} else {
			createCookie("standardsForHighwaysContrast","contrastSwitchOff");
			getActiveStyleSheet("High Contrast", "Disable");
		}
		
		// look for an event
		a.onclick=function(){
			if (a.id == 'contrastSwitchOn'){							
				bod.className = 'highcontrast';					
				a.id = 'contrastSwitchOff';
				a.href='#';
				a.innerHTML = 'Switch to normal view';	
				getActiveStyleSheet("High Contrast", "Enable");
				createCookie("standardsForHighwaysContrast","contrastSwitchOn");
				return false;				
			} else if (a.id=='contrastSwitchOff') {					
				bod.className = 'normal';				
				a.id = 'contrastSwitchOn';
				a.href='#';
				a.innerHTML = 'Switch to high contrast view';	
				getActiveStyleSheet("High Contrast", "Disable");
				createCookie("standardsForHighwaysContrast","contrastSwitchOff");
				return false;
			}			
		}
	}
	/* end of High Contrast JS */
	
	/* Add drop down menus for MCHW and DMRB if needs be */
	function dropDownMenus(){
		
		// test to see if the element is there and the browser can read/write from the DOM
		if (!(document.getElementById('navigation') && document.createElement)){return;}	
		
		var navType = '';
		var navlength;
		
		// prepare the elements
		var navArea = document.getElementById('navigation');	
			
		// determine type of menu needed
		if(document.getElementById('mchw')){
			navType='mchw';
			navLength=6;
			var mchwLabel = navArea.appendChild(document.createElement('label'));
			mchwLabel.innerHTML = 'Select a <abbr title="Manual of Contract Documents for Highway Works">MCHW</abbr> Volume';	
			
			try {
				mchwLabel.htmlFor = navType + '_volume_list';
			} catch (ex) {
				mchwLabel.setAttribute('for', navType + '_volume_list');
			}
		} else if (document.getElementById('dmrb')){
			navType='dmrb';
			navLength=15;
			var dmrbLabel = navArea.appendChild(document.createElement('label'));
			dmrbLabel.innerHTML = 'Select a <abbr title="Design Manual for Roads and Bridges">DMRB</abbr> Volume';
			try {
				dmrbLabel.htmlFor = navType + '_volume_list';
			} catch (ex) {
				dmrbLabel.setAttribute('for', navType + '_volume_list');
			}
		} else {
			return;
		}
		
		var form=navArea.appendChild(document.createElement('form'));
		  
		form.id = navType + '_form';
		
		var selectHTML = document.createElement('select');
		selectHTML.id = navType + '_volume_list';
		
		var initialItem = new Option('Please select');
		
		try { // W3C method
			selectHTML.add(initialItem,selectHTML.options[selectHTML.options.length]);
		} catch (e) { // MS method
			selectHTML.add(initialItem,0);
		}
		
		// populate the select using DOM 2, this overcomes an incompatibilies between
		// IE and W3C standards
		for (var i=0; i<=navLength; i++) {
			var newOption = document.createElement('option');
			newOption.setAttribute('value', '/ha/standards/' + navType + '/vol' + i + '/index.htm');
			newOption.appendChild(document.createTextNode('Volume ' + i));			
			selectHTML.appendChild(newOption);
		}		
		form.appendChild(selectHTML);
		
		// Create the button and add event listener.
		// The following method is used because Firefox does not apply the onclick
		// event properly to a button, only an input, whereas 
		var viewButton = document.createElement('input');		
		
		try {
			viewButton.setAttribute('type','button'); 
			viewButton.setAttribute('value', 'View');			
		} catch (e) {
			viewButton = document.createElement('button');
			viewButton.innerHTML = 'View';
		}
		form.appendChild(viewButton);
		
		// Move the update text to under the form
		var amendDate = document.getElementById(navType + 'UpdateTxt');
		amendDate.parentNode.removeChild(amendDate);
		navArea.appendChild(amendDate);
		
		viewButton.onclick = function(){
			//check for valid selection
			if (selectHTML.selectedIndex>0){
			  	//execute link
				window.location = selectHTML.options[selectHTML.selectedIndex].value;
			}else{
				window.alert("Please select a volume from the drop down list before clicking on the View button.");
			}
		}		
	}
	
	// Cookies to add some persistance
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	function getActiveStyleSheet(title, action) {
		// action can either be enable/disable
		// title is the title of the stylesheet
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("title")==title) {
				if (action == "Enable"){
					a.disabled = false;
				} else {
					a.disabled = true;
				}
			}			  	
		}
		return null;
	}

	// the function list for onload event
	window.onload=function(){
		javaScriptPrimer();
		dropDownMenus();
		highContrastSwitch('header');		
	}
