// code from original weston news site

var dateString = new Array('','st','nd','rd','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','st','nd','rd','th','th','th','th','th','th','th','st');
  
string = 0
var mth = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");

now = new Date();
date = now.getDate();
year = now.getFullYear();
month = now.getMonth();
string = date
datestamp=(date + "-" + (mth[month]) + "-" + year);
function dateWrite() {
      document.msgAdd.date1.value=datestamp;
			document.msgAdd.MM_insert.value = "msgAdd";
      document.msgAdd.action = "messageadd.php";
    }



// ***********************************************************************
// ***********************************************************************	
// code to audit form input

//set-up Regular Expressions to audit the form input fields

var reEmail = /^[\w\.\-]+\@{1}[\w\.\-]+$/;
var reName = /^[A-Za-z\']{1}[A-Za-z\'\-\.\s]*$/;


// initialise (global) variables used in audit
var validSubmit = true;


// function to delete message text (if any) from previous validation
function deleteMsg(cellId) {
  var tableCell = document.getElementById(cellId);
  if (tableCell.firstChild) tableCell.removeChild(tableCell.firstChild);
} // end of function deleteMsg

// function to set the message text when needed
function setMsg(cellId, cellMsgId, msgText) {
  var tableCell = document.getElementById(cellMsgId);
  tableCell.appendChild(document.createTextNode(msgText));
  tableCell.style.color = "red";
  tableCell.style.fontWeight = "bold";
  validSubmit = false;
} // end of function setMsg

// function to clear all message texts prior to (re-)validation and when Reset is pressed
function clearMsgs() {
  deleteMsg("subjectMsg");
  deleteMsg("messageMsg");
  deleteMsg("nameMsg");
  deleteMsg("emailMsg");
  deleteMsg("submitMsg");
} //end of function clearMsgs

// function to audit form when submitted by user
function auditFormInput(){

  // Re-initialise message fields
  clearMsgs()
  validSubmit = true;

  // Check each input field against the relevant Regular Expression. If check fails, write message and set flag to false

	//if the value is null, undefined, 0,"", or NaN, it converts to false, so this statement will mark it as invalid
	if (!document.msgAdd.subject.value) {   
    setMsg("subject","subjectMsg","Enter the topic of your message");
  }

  //if the value is null, undefined, 0,"", or NaN, it converts to false, so this statement will mark it as invalid
	if (!document.msgAdd.message.value) { 
    setMsg("message","messageMsg","Enter the message");
  }
 
  if (!reName.test(document.msgAdd.name.value)){
    setMsg("name","nameMsg","Enter your name");
  }

	if (document.msgAdd.email.value) {   //if the value is null, undefined, 0,"", or NaN, it converts to false
	  if (!reEmail.test(document.msgAdd.email.value)){
      setMsg("email","emailMsg","Enter a valid email address, or leave blank");
		}
  }


  // Generate error message if any errors were found
  
  if (!validSubmit) {
    var msgNode=document.getElementById("submitMsg");  
    msgNode.appendChild(document.createTextNode("Form not submitted. Please correct fields in error as marked and press Submit again"));   
	 
    // emphasise the error message text
    msgNode.style.color="red";
    msgNode.style.fontWeight="bold"; 
	}
	
  return validSubmit;
	
} // end of auditFormInput function

	
