function toggleLayer(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	} else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	} else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}

function toggleLayer2(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "block":"none";
	} else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "block":"none";
	} else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "block":"none";
	}
}

function fontsizeup() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'A--' : 
      setActiveStyleSheet('A-');
      break;
    case 'A-' : 
      setActiveStyleSheet('A');
      break;
    case 'A' : 
      setActiveStyleSheet('A+');
      break;
    case 'A+' : 
      setActiveStyleSheet('A++');
      break;
    case 'A++' :
      break;
    default :
      setActiveStyleSheet('A');
      break;
  }
}

function fontsizedown() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'A++' : 
      setActiveStyleSheet('A+');
      break;
    case 'A+' : 
      setActiveStyleSheet('A');
      break;
    case 'A' : 
      setActiveStyleSheet('A-');
      break;
    case 'A-' : 
      setActiveStyleSheet('A--');
      break;
    case 'A--' : 
       break;
    default :
      setActiveStyleSheet('A--');
      break;
  }
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");

  }
  return null;
}

function getPreferredStyleSheet() {
  return ('A-');
}

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 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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
if (title == 'null') {
  title = getPreferredStyleSheet();
}

function popUp(strURL,strType,strHeight,strWidth) {
	var strOptions="";
	if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
	if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
	if (strType=="scroller") strOptions="scrollbars,menubar,status,height="+strHeight+",width="+strWidth;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
	window.open(strURL, 'newWin', strOptions);
}

function showImage(imageID, strHeight, strWidth) {
	strOptions="status,height="+strHeight+",width="+strWidth
	window.open('images.php?imageID='+imageID, 'newWin', strOptions);
}


function confirmEmail(field1, field2) {
	/* basic function to compare two email addresses */
	if (field1.value != field2.value || !(field1.value.length) ){
		document.getElementById('email_error').innerHTML = '<p class="errors">The email addresses entered do not match.</p>';
		field1.focus();
		return false;
	}else{
		return true;
	}
}


var fieldstocheck = new Array(); 
fieldnames = new Array(); 
function checkform() { 
for (i=0;i<fieldstocheck.length;i++) { 
if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value") == "") { 
alert("Please enter your "+fieldnames[i]); 
eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()"); 
return false; 
} 
} 
return true; 
} 
function addFieldToCheck(value,name) { 
fieldstocheck[fieldstocheck.length] = value; 
fieldnames[fieldnames.length] = name; 
} 

function ajaxEventCal(dt){
	
		document.getElementById('calFormErr').innerHTML = "";
		
		var serverpage = "http://www.thewire.co.uk/test/elements/ajaxEventCalendar.php?";

		serverpage += "dummy=" + new Date().getTime(); // IE AJAX caching fix
		
		if (arguments.length > 1 && arguments[1]=="showCurrent"){
			serverpage += "&showCurrent=1"
		}
		
		if (dt != null) {
			serverpage += "&start_date="+encodeURI(dt);
		}
		var foundOne = false;
		for (var i=0;i<document.forms['calForm'].elements.length;i++){ // unselect them all first
			var el = document.forms['calForm'].elements[i]; 
			if(el.type == "checkbox" && el.name == "calCheck[]"){
				if (el.checked) {
					serverpage += "&eventCalCatID[]="+el.value;
					foundOne = true;
				}
			}
		}
				
		if (!(foundOne)) {
			document.getElementById('calFormErr').innerHTML = "Please select at least one category!";
			return;	
		}
		
		try
		{
			xmlhttp=new XMLHttpRequest();
		} catch (e) 
			{
			try
				{
  				// if IE<8 && the javascript version is greater than 5
  				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
		}

		if (!xmlhttp) {
			// very old browser, do nothing
			return;
		}


		
		xmlhttp.open("GET", serverpage, true);

		xmlhttp.onreadystatechange = function(removePara)
		{
	  	if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
	  		{
	  			eval(xmlhttp.responseText);
	   			xmlhttp = null;
	  		}
 		}
  

	xmlhttp.send(null);
	
	

} 

function highlightCurrentDay(curDay){
	// event cal function
	if($chk($('ng-date-'+curDay))) {
		$('ng-date-'+curDay).addClass('currentDay');
	}
}

function highlightSelectedDay(selectedDay){
	if (!(selectedDay.length)){
		return false;
	}
	// event cal function
	if($chk($('ng-date-'+selectedDay))) {
		$('ng-date-'+selectedDay).addClass('selectedDay');
	}
}

function updateCalCheckboxes(){
	// event cal function
	
	for (var i=0;i<document.forms['calForm'].elements.length;i++){ // unselect them all first
		var el = document.forms['calForm'].elements[i]; 
		if(el.type == "checkbox" && el.name == "calCheck[]"){
			el.checked = false;
		}
	}
	for (var i=0;i<document.forms['calForm'].elements.length;i++){ // now select those in the catIDarr array
		var el = document.forms['calForm'].elements[i]; 
		for (var x=0;x<catIDarr.length;x++){
			if(el.type == "checkbox" && el.name == "calCheck[]"){
				if (catIDarr[x]==el.value)
					el.checked = true;
				}
			}
	}
}

function calCheckAll(onOrOff){
	// event cal function
	if (onOrOff){
		for (var i=0;i<document.forms['calForm'].elements.length;i++){ // unselect them all first
			var el = document.forms['calForm'].elements[i]; 
			if(el.type == "checkbox" && el.name == "calCheck[]"){
				el.checked = true;
			}
		}
	}
}

function updateCal(dt){
	// event cal function
	var url = window.location.href.split("?")[0]; // get rid of current query string
	
	// following replacements needed for moving from individual article to main article listing
	url = url.replace(/\/[0-9]+\//,"\/"); // get rid of numeric article ID
	if (url.indexOf("news/")==-1){
		url = url +"news/";
	}
	
	if (dt != null) {
			url += "?start_date="+encodeURI(dt);
	}
	
	var foundOne = false;
	for (var i=0;i<document.forms['calForm'].elements.length;i++){ // unselect them all first
		var el = document.forms['calForm'].elements[i]; 
		if(el.type == "checkbox" && el.name == "calCheck[]"){
			if (el.checked) {
				url += "&catID[]="+el.value;
					foundOne = true;
			}
		}
	}
	
	if (foundOne){
		window.location.href=url;
	}
	
}

function updateCal1(dt){
	// event cal function
	var url = window.location.href.split("?")[0]; // get rid of current query string
	
	if (dt != null) {
			url += "?dt="+encodeURI(dt);
	}
	
	var foundOne = false;
	for (var i=0;i<document.forms['calForm'].elements.length;i++){ // unselect them all first
		var el = document.forms['calForm'].elements[i]; 
		if(el.type == "checkbox" && el.name == "calCheck[]"){
			if (el.checked) {
				url += "&catID[]="+el.value;
					foundOne = true;
			}
		}
	}
	
	if (foundOne){
		window.location.href=url;
	}
	
}
