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() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

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();
setActiveStyleSheet(title);

function CheckForm () { 

	//Initialise variables
	var errorMsg = "";

	//Check for a first name
	if (document.frmEnquiry.user_name.value == ""){
		errorMsg += "\n\tFirst Name \t- Enter your Name";	
	}
	
	//Check for a last name
	if (document.frmEnquiry.user_subject.value == ""){
		errorMsg += "\n\tSubject \t\t- Please enter the subject of enquiry";
	}
	 	
	//Check for an e-mail address and that it is valid
	if ((document.frmEnquiry.user_email.value == "") || (document.frmEnquiry.user_email.value.length > 0 && (document.frmEnquiry.user_email.value.indexOf("@",0) == - 1 || document.frmEnquiry.user_email.value.indexOf(".",0) == - 1))) { 
		errorMsg += "\n\tE-mail Address \t- Enter your valid e-mail address";
	}
			
	//Check for an enquiry
	if (document.frmEnquiry.question.value == "") { 
 		errorMsg += "\n\tEnquiry \t\t- Enter an enquiry";
	}
		
	//If there is aproblem with the form then display an error
	if (errorMsg != ""){
		msg = "______________________________________________________________\n\n";
		msg += "Your enquiry has not been sent because there are problem(s) with the form.\n";
		msg += "Please correct the problem(s) and re-submit the form.\n";
		msg += "______________________________________________________________\n\n";
		msg += "The following field(s) need to be corrected: -\n";
		
		errorMsg += alert(msg + errorMsg + "\n\n");
		return false;
	}
	
	return true;
}

function SignUp () { 

	//Initialise variables
	var erroMsg = "";

	//Check for a first name
	if (document.frmEmail.name.value == ""){
		erroMsg += "\n\tName \t- Enter your Name";	
	}
	
	//Check for an e-mail address and that it is valid
	if ((document.frmEmail.email.value == "") || (document.frmEmail.email.value.length > 0 && (document.frmEmail.email.value.indexOf("@",0) == - 1 || document.frmEmail.email.value.indexOf(".",0) == - 1))) { 
		erroMsg += "\n\tE-mail Address \t- Enter your valid e-mail address";
	}
			
	//If there is aproblem with the form then display an error
	if (erroMsg != ""){
		msg = "______________________________________________________________\n\n";
		msg += "Your enquiry has not been sent because there are problem(s) with the form.\n";
		msg += "Please correct the problem(s) and re-submit the form.\n";
		msg += "______________________________________________________________\n\n";
		msg += "The following field(s) need to be corrected: -\n";
		
		erroMsg += alert(msg + erroMsg + "\n\n");
		return false;
	}
	
	return true;
}

function AddNews () { 

	//Initialise variables
	var erroMsg = "";

	//Check for a Heading
	if (document.frmNews.heading.value == ""){
		erroMsg += "\n\tHeading \t- Enter a Heading";	
	}
	//Check for a Date
	if (document.frmNews.date.value == ""){
		erroMsg += "\n\tDate \t- Enter a Date";	
	}
	//Check for a Body
	if (document.frmNews.body.value == ""){
		erroMsg += "\n\tBody \t- Enter the news item.";	
	}
	
	//If there is aproblem with the form then display an error
	if (erroMsg != ""){
		msg = "______________________________________________________________\n\n";
		msg += "Your enquiry has not been sent because there are problem(s) with the form.\n";
		msg += "Please correct the problem(s) and re-submit the form.\n";
		msg += "______________________________________________________________\n\n";
		msg += "The following field(s) need to be corrected: -\n";
		
		erroMsg += alert(msg + erroMsg + "\n\n");
		return false;
	}
	
	return true;
}