// This is a hidden Javascript function to send an email to a WCS email addressee - this way our
// email addresses never appear in the source of a website page
// The argument is the name of the recipient within our domain
// To invoke this file include in the page header:
// <script src="scripts.js" language="Javascript" type="text/javascript"></script>
// To use this function:
// <a href="#" onclick="startHiddenMsg('name')">send an email</a>
	function startHiddenMsg(who) {
		Mail="mailto:" + who + "@wokingham-choral" + "-society.org.uk?subject=Wokingham Choral Society Enquiry"
	    window.location.href=Mail
	}


// This function can send an email to anyone.
// To invoke this file include in the page header:
// <script src="scripts.js" language="Javascript" type="text/javascript"></script>
// Arguments: 'who' is the recipient
// 'where1' and 'where2' are two parts of the recipient's email address.
// 'subj' is the Subject of the email
// The '@' character is not passed as an argument.
	function startHiddenMsgAll(who, where1, where2, subj) {
		Mail="mailto:" + who + "@" + where1 + where2 + "?subject=" + subj
	    window.location.href=Mail
	}

// Displays NEW icon if the current date is earlier than the given expiry date (version for top-level pages)
  function newIcon(eDate,eMonth,eYear)  {
	/* eYear is e.g. 2010, eMonth is 1-12, eDate is 1-31 */
	now = new Date					/* Get current date & time */
	nowTime = now.getTime()			/* Convert current to millisecs since 1970 */
	expire = new Date(eYear,eMonth-1,eDate)		/* Get expiry date (y,m-1,d) */
	expireTime = expire.getTime()	/* Express expiry date as millisecs since 1970 */

	if (nowTime < expireTime) {
		document.write("<img src='images/newIcon.png' class='icon' alt='' title='Recently added to this site' />");
		}
  }

// Displays NEW icon if the current date is earlier than the given expiry date (version for pages in sub-folder)
  function newIconAlt(eDate,eMonth,eYear)  {
	/* eYear is e.g. 2010, eMonth is 1-12, eDate is 1-31 */
	now = new Date					/* Get current date & time */
	nowTime = now.getTime()			/* Convert current to millisecs since 1970 */
	expire = new Date(eYear,eMonth-1,eDate)		/* Get expiry date (y,m-1,d) */
	expireTime = expire.getTime()	/* Express expiry date as millisecs since 1970 */

	if (nowTime < expireTime) {
		document.write("<img src='../images/newIcon.png' class='icon' alt='' title='Recently added to this site' />");
		}
  }


//---------------------------------
// Functions to manage 'logon' form
//---------------------------------

// Validate the form - if username & password correct, return TRUE
// Else display error message
   function validForm (myform)
{
	user = myform.username.value
	pass = myform.password.value
	// Put new username & password Emmbrook2011 here
	if ((user == 'w' + 'c' + 's') && (pass == 'Emm' + 'bro' + 'ok' + '2011'))
	{
		// alert ("Setting cookies")
		setCookie()			//remember they have logged in successfully
		return true
	}
//	else
	// keep old username & password here for a bit
//	if ((user == 'w' + 'c' + 's') && (pass == 'emm' + 'bro' + 'ok'))
//	{
		// alert ("Setting cookies")
//		setCookie()			//remember they have logged in successfully
//		return true
//	}
	else
	{
		// alert('Incorrect username or password - access denied');
		showError ()
		return false
	}
}

// Show the error message when username or password are incorrect
 function showError ()
 {
	document.getElementById('error_content').style.visibility = "visible"
	document.getElementById('username').focus()
	document.getElementById('username').select()
 }


// Initialise the form, when login page is loaded
 function initForm ()
 {
	document.getElementById('username').focus()
	document.getElementById('username').select()
	document.getElementById('error_content').style.visibility = "hidden"
 }

// function to set up two cookies
 function setCookie ()
 {
	 expireDate = new Date													// get current date
	 expireDate.setDate(expireDate.getDate()+5)								// make cookie expire in 5 days time
	 nextCookie = "loggedOn=yes;expires=" + expireDate.toGMTString()		// assemble first cookie
	 nextCookie = nextCookie + ";path=/"									// make cookie accessible throughout the site
	 document.cookie = nextCookie											// Save first cookie
	 currentDate = new Date													// get current date again
	 nextCookie = "loggedOnDate=" + currentDate.toGMTString()				// assemble second cookie showing when they logged in
	 nextCookie = nextCookie + ";expires=" + expireDate.toGMTString()		// and tomorrow as the expiry date
	 nextCookie = nextCookie + ";path=/"									// make cookie accessible throughout the site
	 document.cookie = nextCookie											// Save second cookie
	 return
 }

// function to see if they logged in, by looking for non-expired cookie
// Argument 1 is URL to go to if not logged on recently
 function checkLogin (blockedURL)
 {
	 if (document.cookie == "") {
		   // alert ("No cookies found")
		 location.href = blockedURL
	 }
	 else {
		 thisCookie = document.cookie.split("; ")							// cookies exist.  Get them all
		 for (i=0; i<thisCookie.length; i++){								// examine each cookie
			  // alert ("Found cookie: " + thisCookie[i])
			 if (thisCookie[i].split("=")[0] == "loggedOn" && thisCookie[i].split("=")[1] == "yes") {	// found our valid cookie, he is logged on
			 document.getElementById('private_content').style.visibility = "visible"	// reveal the whole page
			 return
			 }
		 }
		 location.href = blockedURL											// didn't find our valid cookie, block him
	 }
 }

// function to delete cookies
 function deleteCookie () {
 	 if (document.cookie == "") {
		 alert ("No cookies found")
		 return
	 }
	 else {
		 thisCookie = document.cookie.split("; ")							// cookies exist.  Get them all
		 expireDate = new Date												// get today's date
		 expireDate.setDate(expireDate.getDate()-1)							// set yesterday's date
		 EXPIRY = expireDate.toGMTString()
		 var textCookie = "Deleted cookies:\n\n"
		 for (i=0; i<thisCookie.length; i++){
			 cookieName = thisCookie[i].split("=")[0]
			 cookieValue = thisCookie[i].split("=")[1]
			 textCookie = textCookie + cookieName + "=" + cookieValue + "\n\n"
			 document.cookie = cookieName + "=" + cookieValue + ";expires=" + EXPIRY	// write expired cookie
		 }
		 alert (textCookie)
		 return
	 }
 }

// function to read cookies
// document.cookie only returns the first two fields, i.e. name and value of each cookie
 function readCookie () {
 	 if (document.cookie == "") {
		 alert ("No cookies found")
		 return
	 }
	 else {
		 // alert("Entire document.cookie:\n" + document.cookie)
		 var thisCookie = document.cookie.split("; ")							// cookies exist.  Get them all
		 var textCookie = "Cookies found:\n\n"
		 for (i=0; i<thisCookie.length; i++){
			 cookieName = thisCookie[i].split("=")[0]
			 cookieValue = thisCookie[i].split("=")[1]
			 textCookie = textCookie + cookieName + "=" + cookieValue + "\n\n"
		 }
			 alert (textCookie)							// show each cookie name & value
		 return
	 }
 }









