// Javascript for GM8 site
//
//	gm8launch(): 	countdown for site launch (Edinburgh only - add two days for Glasgow when needed)
//	change():		temporary code for CSS style changes 

var datetoday = new Date();
var dateglasgowlaunch = new Date("May 16 2008 19:30:00");
var dateedinburghlaunch = new Date("May 16 2008 19:30:00");

var days;
var hours;
var minutes;
var seconds;

function writeGM8banner(pathchange) {
	pathchange=(pathchange==undefined) ? '' : pathchange;
	document.write('<div id="gm8">');
	document.write('<a href="http://www.gm8.org.uk"><img src="'+pathchange+'images/g_100.jpg" alt="GM8" /><img src="'+pathchange+'images/m_100.jpg" alt="GM8"/><img src="'+pathchange+'images/8_100.jpg" alt="GM8"/></a>');
	document.write('<ul>');
	document.write('<li><a href="'+pathchange+'archive/">download GM8</a></li>');
	document.write('<li><a href="http://gm8magazine.wordpress.com">GM8 blog</a></li>');
	document.write('<li><a href="'+pathchange+'about">about GM8</a></li>');
	document.write('<li><a href="'+pathchange+'contact">contact GM8</a></li>');
	document.write('<li><a href="http://www.gmh.org.uk">about GMH</a></li>');
	//document.write('<li><a href="'+pathchange+'gm8_2.html">page 2</a></li>');
	//document.write('<li><a href="'+pathchange+'gm8_3.html">page 3</a></li>');
	//document.write('<li><a href="'+pathchange+'menu.html" onMouseover="dropdownmenu(this, event, menu1, ' + "'165px'" + ')" onMouseout="delayhidemenu()">Style Change -</a></li>');
	document.write('</ul>');
	document.write('</div> <!--gm8 end-->');
}

function writeGM8footer(pathchange) {
	pathchange=(pathchange==undefined) ? '' : pathchange;
	document.write('<div id="footer">');
	document.write('<ul>');
	document.write('<li><a href="'+pathchange+'contact">contact GM8</a> | </li>');
	document.write('<li><a href="'+pathchange+'about">about GM8</a> | </li>');
	document.write('<li><a href="http://www.gmh.org.uk">GMH</a></li>');
	document.write('</ul>');
	document.write('</div> <!-- footer end -->');
}



function gm8launch() {
	datetoday = new Date();
	days = Math.round((dateedinburghlaunch - datetoday) / 1000 / 60 / 60 / 24);
	hours = Math.round((dateedinburghlaunch - datetoday) / 1000 / 60 / 60);
	minutes = Math.round((dateedinburghlaunch - datetoday) / 1000 / 60);
	seconds = Math.round((dateedinburghlaunch - datetoday) / 1000);
	document.form1.days.value = days + 2;
	document.form1.hours.value = hours - (days * 24) + 12;
	document.form1.minutes.value = minutes - (hours * 60) + 30; 
	document.form1.seconds.value = seconds - (minutes * 60) + 30;
	document.form2.days.value = days;
	document.form2.hours.value = hours - (days * 24) + 12;
	document.form2.minutes.value = minutes - (hours * 60) + 30; 
	document.form2.seconds.value = seconds - (minutes * 60) + 30;
	ID=window.setTimeout("gm8launch();",1000);
}


function change(strWhat) {
   switch (strWhat)
   {
      	case "bodyWhite":
      		document.body.style.background='#ffffff';
      	break;
      	case "bodyBlack":
      		document.body.style.background='#000000';
      	break;
      	case "bodyLightBlue":
      		document.body.style.background='#C0CEEB';
      	break;
      	case "bodyDarkBlue":
      		document.body.style.background='#667AB7';
      	break;
      	case "bodyLightBlueStripe":
      		document.body.style.backgroundImage='url(bhl.jpg)';
      	break;
      	case "bodyDarkBlueStripe":
      		document.body.style.backgroundImage='url(bhd.jpg)';
	break;
	case "borderWhite":
		document.getElementById('container').style.borderColor='#FFFFFF';
		document.getElementById('container').style.backgroundImage='url(bar_100.jpg)';
      	break;
	case "borderDarkBlue":
		document.getElementById('container').style.borderColor='#667AB7';
		document.getElementById('container').style.backgroundImage='url(bar_120.jpg)';
      	break;
	case "containerNormal":
		document.getElementById('gm8main').style.background='#FFFFFF';
		document.getElementById('h1').style.color='#667AB7';
		document.getElementById('h2').style.color='#667AB7';
		document.getElementById('p').style.color='#667AB7';
      	break;
	case "containerDarkBlue":
		document.getElementById('gm8main').style.background='#667AB7';
		var elements = document.getElementsByTagName('h1');
		for(var i = 0; i < elements.length; i++) {
			elements.item(i).style['color'] = '#FFFFFF';
		}
		var elements = document.getElementsByTagName('p');
		for(var i = 0; i < elements.length; i++) {
			elements.item(i).style['color'] = '#C0CEEB';
		}
      	break;
	case "fatBorders":
		setStyleByClass('div', 'speechleft', 'border', '5px solid #C0CEEB');
		setStyleByClass('div', 'speechright', 'border', '5px solid #C0CEEB');
		setStyleByClass('div', 'speechfull', 'border', '5px solid #C0CEEB');
		setStyleByClass('div', 'picwhoami', 'border', '5px solid #C0CEEB');
      	break;
   }
}

//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value

function setStyleByClass(t,c,p,v){
	var elements;
	elements = document.getElementsByTagName(t);
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" +v + "'");
				}
			}
		}
	}
}



function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function charcount(obj, countobj, maxcount) {
	if (obj.value.length > maxcount) {
		obj.value = obj.value.substring(0, maxcount);
		alert('Sorry, there is a limit on the number of letters for the comments!');
	}
	else { 
		countobj.value = maxcount - obj.value.length;
	}
}

function validateemail(emailaddress) {
var bFlag;
var bAt;
var bDot;
	bFlag=true;
	bAt=emailaddress.indexOf("@");
	bDot=emailaddress.indexOf(".",(bAt==-1)? 0 : bAt);
	if ((bAt==-1) || (bDot==-1)) {
		bFlag=false;
	}
	return bFlag;
}

function validatecomments(obj, countobj, maxcount) {
var bFlag;
var options;
var namepart='';
var emailpart='';
var commentpart='';
	options=0;
	bFlag=true;
	if (maxcount > obj.value.length) {
		if ((document.commentsform.author.value==null) || (document.commentsform.author.value=='')) {
			options=options+1;
			namepart='name';
		}
		if ((document.commentsform.email.value==null) || (document.commentsform.email.value=='') || (!(validateemail(document.commentsform.email.value)))) {
			options=options+1;
			emailpart='valid email';
		}
		if ((document.commentsform.comment.value==null) || (document.commentsform.comment.value=='')) {
			options=options+1;
			commentpart='comments';
		}
		if (options==3) {
			alert('Please enter your name, valid email and comments before submitting.');
			bFlag=false;
		}
		if (options==2) {
			alert('Please enter your ' + (namepart=='' ? emailpart + ' and ' + commentpart : namepart + ' and ' + (emailpart=='' ? commentpart : emailpart)) + ' before submitting.');
			bFlag=false;
		}
		if (options==1) {
			alert('Please enter your ' + namepart + emailpart + commentpart + ' before submitting.');
			bFlag=false;
		}
	}
	else {
		alert('Please limit your comments to the allocated letters');
		bFlag=false;
	}

	return bFlag;
}
