/*
	Javascript Functions
	Six Serving Men : Stef - Last update Sept 2010
	
*/


// validate email Function

function validate_email(field,alerttxt)
{
with (field)
  {
  apos=value.indexOf("@");
  dotpos=value.lastIndexOf(".");
  if (apos<1||dotpos-apos<2)
    {alert(alerttxt);return false;}
  else {return true;}
  }
}

function validate_form(thisform)
{
with (thisform)
  {
  if (validate_required(email,"Require a valid email address!")==false)
  {email.focus();return false;}
  }
}


function emailCheck (emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null) {
		alert("The username doesn't seem to be valid.")
		return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
		  for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!")
			return false
			}
		}
		return true
	}
	
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("The domain name doesn't seem to be valid.")
		return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
	   alert("The address must end in a three-letter domain, or two letter country.")
	   return false
	}
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   alert(errStr)
	   return false
	}
	return true;
}

// Display Current Date Function

function display_date()
{
	if ($('date'))
	{
		var months = new Array(13);
		months[0]  = "January";
		months[1]  = "February";
		months[2]  = "March";
		months[3]  = "April";
		months[4]  = "May";
		months[5]  = "June";
		months[6]  = "July";
		months[7]  = "August";
		months[8]  = "September";
		months[9]  = "October";
		months[10] = "November";
		months[11] = "December";	
		
		var days = new Array(7);
		days[0] = "Sunday";
		days[1] = "Monday";
		days[2] = "Tuesday";
		days[3] = "Wednesday";
		days[4] = "Thursday";
		days[5] = "Friday";
		days[6] = "Saturday";
		
		var currentTime = new Date();
		var month_number = currentTime.getMonth();
		var dayname = days[currentTime.getDay()];
		var day = currentTime.getDate();
		var year = currentTime.getFullYear();
		//document.write(month + "/" + day + "/" + year)
		$('date').innerHTML =  dayname + " " + months[month_number] + " " + day + " " + year;
	}
}

addLoadEvent(function() {
	display_date();
});

//
// Contact Form Functions
//

function submit_contactform()
{
	// Setup Defaults
	var myform = document.contact_form;
	var ErrorColour = '#FF0000';
	
	// Reset all Background Colours
	
	myform.name.style.backgroundColor = '';
	myform.telephone.style.backgroundColor = '';
	myform.emailaddress.style.backgroundColor = '';
	
	// Check all fields
	
	var checked = 1;
		
	if (checked == 1 && myform.name.value == "")
	{
		myform.name.style.backgroundColor = ErrorColour;
		myform.name.focus();
		alert('You must enter a name.');
		checked = 0;
	}
	if (checked == 1 && myform.name.value.length < 3)
	{
		myform.name.style.backgroundColor = ErrorColour;
		myform.name.focus();
		alert('You must enter a name that is longer than 3 characters.')
		checked = 0;
	}
	if (checked == 1 && myform.emailaddress.value == "")
	{
		myform.emailaddress.style.backgroundColor = ErrorColour;
		myform.emailaddress.focus();
		alert('You must enter an email address.');
		checked = 0;
	}
	
	var emailaddress = myform.emailaddress.value;
	
	if (checked == 1 && emailCheck(emailaddress) != true)
	{
		myform.emailaddress.style.backgroundColor = ErrorColour;
		myform.emailaddress.focus();
		alert('You must enter a valid email address .');
		checked = 0;
	}
	if (checked == 1 && myform.telephone.value == "")
	{
		//alert('You must enter an email address.')
		var confirm_tel = confirm('Are you sure you want to submit the form without a telephone number included?');
		if (!confirm_tel)
		{
			myform.telephone.style.backgroundColor = ErrorColour;
			myform.telephone.focus();
			checked = 0;
		}
	}
	if (checked == 1 && myform.code.value == "")
	{
		myform.code.style.backgroundColor = ErrorColour;
		myform.code.focus();
		alert('You must enter the security code to continue.');
		checked = 0;
	}
	
	// If all fields checked, submit form via Ajax
	if (checked == 1)
	{
		if (AjaxForm == 1)
		{
			// Declare Variables
			var name = myform.name.value.escapeHTML();
			var emailaddress = myform.emailaddress.value.escapeHTML();
			var telephone = myform.telephone.value.escapeHTML();
			var message = myform.message.value.escapeHTML();
			
			var url = 'ajax.php?method=contact&name=' + name + '&emailaddress=' + emailaddress + '&telephone=' + telephone + '&message=' + message;
	
			new Ajax.Request(url, {
				method: 'get',
				onLoading: function() {
					createoverlayloader();
					$('contact_form_return').remove();
				},
				onComplete: function(request) {
					destroyoverlayloader();
					document.getElementById('contact_form').innerHTML = request.responseText;
					
				}
			});
		}
		else
		{
			document.myform.submit();
		}
	}
	
}

/*
	Function to open new window for external links
	Stef : Dec 2008
*/
var newwindow
function openNewWindow(url) 
{ 
newwindow = window.open(url,
 'name','menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0')
if (window.focus) {newwindow.focus()}
}
// end of function openNewWindow
