// JavaScript Document

function showdate() {
	var months=new Array("","January","February","March","April","May","June","July","August","September","October","November","December");
	var time=new Date();
	var lmonth=months[time.getMonth() + 1];
	var date=time.getDate();
	var year=time.getYear();
	if (year < 2000)    // Y2K Fix, Isaac Powell
	year = year + 1900; // http://onyx.idbsu.edu/~ipowell
    document.write(date + " ");
	document.write(lmonth + " " + year);
}

//opens a popup window
function openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

//to print the current window content
function PrintThis() {
 if (window.print)
	window.print()
else
alert("Sorry, your browser does not support this feature.");
} 

// to swap HTML content from divfrom to divto and make visible
function swapcontent(divfrom,divto) {
	//alert(divfrom + " and " +divto);
	document.getElementById(divto).innerHTML = document.getElementById(divfrom).innerHTML; 
	document.getElementById(divto).style.visibility = 'visible';
	document.getElementById(divto).style.display = 'block';
}

//contact form validation
function validateForm() {
	var errorStr = "";
	var checkArray = new Array("forename", "surname", "postcode1", "email", "age");
	var errorArray = new Array("forename\n", "surname\n", "poscode\n", "email address\n", "age range");
	var objRef;
	for (i=0,limit=checkArray.length;i<limit;i++) {
		objRef = eval("document.emailer." + checkArray[i]);
		if (objRef.value == "" || objRef.value == " " || objRef.value.length < 1) {
			errorStr = errorStr + errorArray[i];
		}
			
	}
	if (errorStr.length != 0) {
		alert("Please complete the following fields:\n" + errorStr);
		return;
	}
	//check for valid email address
	if (!validateEmail(document.emailer.email.value)){
		alert("Your Email Address is not valid.");
		document.emailer.email.focus();
		document.emailer.email.select();
		return;			
	}
    //submit the form
	document.emailer.submit();
}
//email validation
function validateEmail(sEmail) {	
	var checkEmail = 1;
	if (sEmail.indexOf("@") < 0) {
        checkEmail = 0;      
    }
	if (sEmail.length < 4) {
		checkEmail = 0;      
    }
    i = sEmail.indexOf("@");     
    if (sEmail.indexOf("@", i + 1) > 0) {  
		checkEmail = 0;
	}
	if (sEmail.indexOf(" ", i + 1) > 0){
		checkEmail = 0;
	}
	if (sEmail.indexOf('"') != - 1) {
        checkEmail = 0;     
    }
	if (sEmail.indexOf('..') != - 1) {
        checkEmail = 0;       
    }
	var sInvalidChars = "!#$%^&*()=+{}[]|\;:'/?>,< ";
	for (i=0; i < sInvalidChars.length; i++)	{
		if ( sEmail.indexOf(sInvalidChars.charAt(i)) >= 0 ) {
			 checkEmail = 0;
		}
	}	
	if (checkEmail == 0) {
		return false;
	}
	else {
		return true;
	}
}
function makeVisible(myId) {
	document.getElementById(myId).className = "visible";
}
function makeInvisible(myId) {
	document.getElementById(myId).className = "invisible";
}

//to cover users who have no javascript on but do have styles
var divstoHideArray = new Array("Q1A","Q2A","Q3A","Q4A","Q5A","Q6A","Q7A");
function makeDivsInvisible() {
	for (var n=1; n<=divstoHideArray.length; n++) {
		myId = divstoHideArray[n-1];
		document.getElementById(myId).className = 'invisible';
	}
}

