// Atkinson Builders Ltd Javascript Functions
// Wriiten by Matt R 18th May 2007

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
	var currentTime = new Date();
	var year = currentTime.getFullYear();
	if ($('year'))
	{
		$('year').innerHTML = year;
	}
});

addLoadEvent(function() {
	var email_1 = 'info';
	var email_2 = 'atkinson-builders';
	var email_3 = '.com';
	var email = email_1 + "@" + email_2 + email_3;
	if ($('email'))
	{
		$('email').innerHTML = "<a href=\"mailto:" + email + "\">" + email + "</a>";
	}
});

//
// Contact Form Functions
//
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 (checked == 1 && myform.code.value == "")
	{
		myform.code.style.backgroundColor = ErrorColour;
		myform.code.focus();
		alert('You must enter the security code.')
		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 code = myform.code.value.escapeHTML();
		
		var url = 'ajax.php?method=contact&submit=true&name=' + name + '&emailaddress=' + emailaddress + '&telephone=' + telephone + '&message=' + message + '&code=' + code;

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

	}
	
}

function submit_mailingform()
{
	// Setup Defaults
	var myform = document.mailing_form;
	var ErrorColour = '#FF0000';
	
	// Reset all Background Colours
	
	myform.name.style.backgroundColor = '';
	myform.address.style.backgroundColor = '';
	myform.postcode.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.address.value == "")
	{
		myform.address.style.backgroundColor = ErrorColour;
		myform.address.focus();
		alert('You must enter an address.')
		checked = 0;
	}
	if (checked == 1 && myform.postcode.value == "")
	{
		myform.postcode.style.backgroundColor = ErrorColour;
		myform.postcode.focus();
		alert('You must enter a postcode.')
		checked = 0;
	}
	if (checked == 1 && myform.code.value == "")
	{
		myform.code.style.backgroundColor = ErrorColour;
		myform.code.focus();
		alert('You must enter the security code.')
		checked = 0;
	}	
	
	// If all fields checked, submit form via Ajax
	if (checked == 1)
	{
		// Declare Variables
		var name = myform.name.value.escapeHTML();
		var address = myform.address.value.escapeHTML();
		var postcode = myform.postcode.value.escapeHTML();
		var code = myform.code.value.escapeHTML();
		
		var url = 'ajax.php?method=mailing&submit=true&name=' + name + '&address=' + address + '&postcode=' + postcode + '&code=' + code;

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

	}
	
}

// This script will emulate the css :hover effect on the menu elements in IE

// The variables
var cssid = "primary-nav"; // CSS ID for the menuwrapper
var menuadd = "h";  // Character to be added to the specific classes upon hovering. So for example, if this is set to "h", class "menuparent" will become "menuparenth" when hovered over.
var menuh = "menuh"; // Classname for hovering over all other menu items

if (window.attachEvent) window.attachEvent("onload", cssHover);

function cssHover() {
	var sfEls = document.getElementById(cssid).getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {

		sfEls[i].onmouseover=function() {
			if (this.className != "") {
				this.className = this.className + menuadd;
			}
			else {	
				this.className = menuh;
			}
		}

		sfEls[i].onmouseout=function() {
			if (this.className == menuh) {
				this.className = "";
			}
			else {
				this.className = this.className.replace(new RegExp(menuadd + "$"), "");
			}
		}
	}
}

function dynamic_slidedown_menu_init()
{	
	$$('#primary-nav li.menuparent').each(function(element)
	{
		element.childElements().each(function(child_element)
		{						
			if (child_element.inspect().startsWith('<a'))
			{				
				child_element.onclick = function()
				{
					if (child_element.next().getStyle('display') == 'none')
					{
						new Effect.BlindDown(child_element.next(), {duration: 1});
					}
					else
					{
						new Effect.BlindUp(child_element.next(), {duration: 1});
					}
					
					return false;
				}
			}
		});
	});
	
	
	$$('#primary-nav li.menuparent ul').each(function(child_element, index)
	{
		child_element.setStyle({
			  display: 'none'
		});						
	});
	
}

function change_mainpic(img)
{
	//$('mainpic').innerHTML = '<div id="loader"><img src="assets/images/loader.gif" /></div>';

	$('mainpic').innerHTML = '<img src="assets/images/developments/' + img + '" />';
}

addLoadEvent(function()
{
	dynamic_slidedown_menu_init();
});




