/* Add rounded corners */
	
	// array(blockid, color, hide_tr, hide_tl, hide_br, hide_bl)
	var rounded = new Array(
		new Array("helpline","pink"),
		new Array("reminders","pink"),
		new Array("celebs","peach",false,false,true,false)
	);
	
	function roundBlocks() {
		for(x=0; x<rounded.length; x++) {
			if(document.getElementById(rounded[x][0]) || typeof(rounded[x][0]) != "string") {
				
				if(typeof(rounded[x][0]) == "string") {
					var element = document.getElementById(rounded[x][0]);
				} else if(typeof(rounded[x][0]) == "object") {
					var element = rounded[x][0];
				}
				
				if(typeof(element) == "object") {
					if(!rounded[x][2]) {
						var crn_tr = document.createElement('span');
						crn_tr.className="corner crn-tr";
						crn_tr.style.backgroundImage="url(/www.fightcervicalcancer.org.uk/images/assets/corners/"+rounded[x][1]+"-tr.gif)";
						element.appendChild(crn_tr); 
					}
					if(!rounded[x][3]) {
						var crn_tl = document.createElement('span');
						crn_tl.className="corner crn-tl";
						crn_tl.style.backgroundImage="url(/www.fightcervicalcancer.org.uk/images/assets/corners/"+rounded[x][1]+"-tl.gif)";
						element.appendChild(crn_tl);
					}
					if(!rounded[x][4]) {
						var crn_br = document.createElement('span');
						crn_br.className="corner crn-br";
						crn_br.style.backgroundImage="url(/www.fightcervicalcancer.org.uk/images/assets/corners/"+rounded[x][1]+"-br.gif)";
						element.appendChild(crn_br); 
					}
					if(!rounded[x][5]) {
						var crn_bl = document.createElement('span');
						crn_bl.className="corner crn-bl";
						crn_bl.style.backgroundImage="url(/www.fightcervicalcancer.org.uk/images/assets/corners/"+rounded[x][1]+"-bl.gif)";
						element.appendChild(crn_bl);
					}
					
				}
			}	
		}
	}

/* Global function for misc styles to be applied on window load */

	function evalMisc() {
		
		/* Assign default value switch to search field */
		if(document.getElementById('search_field')) {
			var search_field = document.getElementById('search_field');
			search_field.value = "Search..";
			search_field.onfocus=function(){defaultValue('Search..',this)}
			search_field.onblur=function(){defaultValue('Search..',this)}
		}
		
		/* template specific */
		var template = document.getElementById('content').className;
		var copyHeadings = getElementsByClassName(document.getElementById('HeadContent'),"heading");
		if(template=="homepage") {
			rounded.splice(rounded.length,1,new Array(copyHeadings[0],"blue",false, false, false, false));
		} else if(template=="gateway" || template=="gateway form" || template=="page pageform") {
			rounded.splice(rounded.length,1,new Array(copyHeadings[0],"blue"));
		}
		
		if(template=="homepage") {
			rounded.splice(rounded.length,1,new Array("downloads","peach", false, false, true, true));
			rounded.splice(rounded.length,1,new Array("tertiary_content","peach", true, true, false, false));		
		}
		
		if(template=="gateway form") {
			rounded.splice(rounded.length,1,new Array("stories","peach", true, true, false, false));
			rounded.splice(rounded.length,1,new Array("reckoner","peach", false, false, true, true));
		} else if(template=="gateway" || template=="page" || template=="section" || template=="page pageform" || template=="page stories fight-it-dvd") {
			rounded.splice(rounded.length,1,new Array("stories","peach", false, false, true, true));
			rounded.splice(rounded.length,1,new Array("downloads","peach", true, true, false, false));
		}
		
		/* ready reckoner */
		function checkReck() {
			var year = document.getElementById('year')[document.getElementById('year').selectedIndex].value;
			var day = document.getElementById('day')[document.getElementById('day').selectedIndex].value;
			var month = document.getElementById('month')[document.getElementById('month').selectedIndex].value;
			var gender = document.getElementById('gender')[document.getElementById('gender').selectedIndex].value;
			
			var days = 0;
			var difference = 0;
			
			birthday = new Date(year,month-1,day);
			today = new Date();
			difference = today - birthday;
			days = difference/(1000*60*60*24);
			years = Math.floor(days/365.25);
			
			if(gender == "female" && ((years>=16 && years<=18)) && parseInt(year)+parseFloat((month/100))>=1990.09) {
				document.getElementById('postcode-label').style.display="block";
				document.getElementById('postcode-input').style.display="block";
			if(document.getElementById('postcode-input-other')) { document.getElementById('postcode-input-other').style.display="block"; }
				document.getElementById('postcode-note').style.display="block";
			} else {
				document.getElementById('postcode-label').style.display="none";
				document.getElementById('postcode-input').style.display="none";
				if(document.getElementById('postcode-input-other')) { document.getElementById('postcode-input-other').style.display="none"; }
				document.getElementById('postcode-note').style.display="none";
			}
		}
		
		if(document.getElementById('gender') && document.getElementById('year')) {
			document.getElementById('gender').onchange=function() {checkReck();	}
			document.getElementById('year').onchange=function() {checkReck(); }
			document.getElementById('month').onchange=function() {checkReck(); }
			document.getElementById('day').onchange=function() {checkReck(); }
		}
		
		/* Eval the rounding array */
		roundBlocks();
		if(document.getElementById('gender') && document.getElementById('year')) {
			checkReck();
		}
	 	redirectSearch();
		
	}
	
	addLoadEvent(evalMisc);
	
/**
 * Slideshow Object
 *
 * Usage: 
 * var tips = initSlideshow('header','h3','includes/js/tips.xml');
 * Returns: Reference to the slideshow object attached to the HTML element specified.
 * Use header.next() or .previous(); etc.
 *
 * 'this.slides' is an array of 'slide' hashtables. 
 *
 */
 
	function slideshow(parent,titleElement,resource) {

		/* make params global */
		this.parentElement = document.getElementById(parent);
		this.titleElement = this.parentElement.getElementsByTagName(titleElement)[0];
		this.resource = resource;
		
		/* load initial slides array */
		this.slides = new Array();
		
		/* globals to store current slide */
		this.slideLocation;

	}

	slideshow.prototype.populate = function() {
		/* send xml request */
		xmlhttp=null
		slideshow = this;
		if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest() } 
		else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") }
		if (xmlhttp!=null) {
			nocache = new Date; params = "?nocache="+nocache.getTime();
			xmlhttp.onreadystatechange=function(){ slideshow.parse(slideshow); }
			xmlhttp.open('GET', this.resource+params, true);
			xmlhttp.setRequestHeader('Content-Type','text/xml'); 
			xmlhttp.send(null);
		} 
	}
	
	/* parse response */
	slideshow.prototype.parse = function() {

		if (xmlhttp.readyState==1) { 
			// show loading 
		}
		
		if (xmlhttp.readyState==4) { 
			if (xmlhttp.status==200) { 
										
				/* load the content blocks */
				
					var tips = xmlhttp.responseXML.getElementsByTagName("SLIDE");
					for(i=0;i<tips.length;i++) {
						var slideHash = {
							"heading": xmlhttp.responseXML.getElementsByTagName("SLIDE")[i].getElementsByTagName('HEADING')[0].firstChild.nodeValue,
							"paragraph": xmlhttp.responseXML.getElementsByTagName("SLIDE")[i].getElementsByTagName('PARAGRAPH')[0].firstChild.nodeValue,
							"link": xmlhttp.responseXML.getElementsByTagName("SLIDE")[i].getElementsByTagName('LINK')[0].firstChild.nodeValue,
							"image": xmlhttp.responseXML.getElementsByTagName("SLIDE")[i].getElementsByTagName('IMAGE')[0].firstChild.nodeValue
						};
						this.slides[i] = slideHash;	
					}
					
				/* show a random content block */
				var random = Math.floor(Math.random()*i)
				this.jump(random);	
			}
		}
		
	}
	
	slideshow.prototype.jump = function(location) {
		if(location >= 0 && location < this.slides.length) {
			
			/* create heading link */
			var heading_link = document.createElement('a');
			heading_link.setAttribute("href",this.slides[location]["link"]);
			heading_link.innerHTML = this.slides[location]["heading"];
			this.parentElement.getElementsByTagName('P')[0].innerHTML="";
			this.parentElement.getElementsByTagName('P')[0].appendChild(heading_link);
			
			/* populate second paragraph */
			this.parentElement.getElementsByTagName('P')[1].innerHTML = this.slides[location]["paragraph"];
			
			/* image link */
			var pars = this.parentElement.getElementsByTagName('a');
			for(i=0;i<pars.length;i++) {
				if(pars[i].className=="blocklink") {
					pars[i].setAttribute("href",this.slides[location]["link"]);
					this.parentElement.getElementsByTagName('img')[0].src=this.slides[location]["image"];
					this.parentElement.getElementsByTagName('img')[0].alt=this.slides[location]["heading"];
					pars[i].appendChild(this.parentElement.getElementsByTagName('img')[0]);
				}
			}	
			
			this.slideLocation = location;
		}
	}
	
	slideshow.prototype.next = function() {
		if(this.slideLocation == this.slides.length-1) {
			this.jump(0);
		} else {		
			this.jump(this.slideLocation+1);
		}
	}
	
	slideshow.prototype.previous = function() {
		if(this.slideLocation == 0) {
			this.jump(this.slides.length-1);
		} else {
			this.jump(this.slideLocation-1);
		}
	}
	
	function initSlideshow(parent,titleElement,resource) {
 		var parentElement = document.getElementById(parent);
 		try { parentElement.slideshow = new slideshow(parent,titleElement,resource); }
 		catch (e) {}
 		parentElement.slideshow.populate();
 		return parentElement.slideshow;
 	}
	
	function loadSlideshows() {
		if(document.getElementById('stories')) {
			var stories = initSlideshow('stories','h2','/uploads/images/530-stories.xml');
		} 
	}

	addLoadEvent(loadSlideshows);
	