// Bijou Web Functions 
// by Matt R

function goto(loc)
{
	window.location = loc;
}


function addloader() {
	new Insertion.Before('contact_form', "<div id=\"contact_loader\" class=\"centre\">Loading...<br><img src=\"assets/images/loader.gif\" /></div>");
}

function removeloader()
{
	if ($('contact_loader'))
	{
		$('contact_loader').remove();
	}
}

//
// Contact Form Styles
//
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;
}
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 all fields checked, submit form via Ajax
	if (checked == 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&type=ajax&name=' + name + '&emailaddress=' + emailaddress + '&telephone=' + telephone + '&message=' + message;

		new Ajax.Request(url, {
			method: 'get',
			onLoading: function() {
				//addloader('contact_form_loader');
			},
			onComplete: function(request) {
				document.getElementById('contact_form_return').innerHTML = request.responseText;
				removeloader(contact_form_loader);
				//$(DIV).remove();
			}
		});

	}
	
}

// Google Maps Functions

if (window.location == "http://www.thebijou.co.uk/contact_us.html")
{
	function load() 
	{
		if (GBrowserIsCompatible()) 
		{	
			// Create our "tiny" marker icon
			var icon = new GIcon();
			icon.image = "http://www.thebijou.co.uk/assets/images/bijousmaller.gif";
			icon.iconSize = new GSize(25, 25);
			icon.shadowSize = new GSize(22, 20);
			icon.iconAnchor = new GPoint(4, 20);
			icon.infoWindowAnchor = new GPoint(5, 1);
		
			var map = new GMap2(document.getElementById("map"));
			map.addControl(new GSmallMapControl());
			// Do Pan
			map.setCenter(new GLatLng(53.993794, -1.540983), 15);
			window.setTimeout(function() 
			{
				map.panTo(new GLatLng(53.998688, -1.547077));
			}, 3000);
			var point = new GLatLng(53.998521, -1.54797);
			map.addOverlay(new GMarker(point, icon));

		}
	}
}
