// Set Side Nav Menu
function setMenu(e) {
	// Store the browser type in a variable
	var browser = whichBrs();

	e = e ? e : window.event;

	var el = e.target ? e.target : e.srcElement;
	
	var toggleme;
	
	// Check if the next sibling from the clicked anchor is a text value, if so use the next sibling as toggleme
	if (el.nextSibling.nodeName == '#text'){
			toggleme = el.nextSibling.nextSibling;
		}
		else {
			toggleme = el.nextSibling;
	}

	// Grandparent node of the anchor called (should always be a UL)
	var thenode = el.parentNode.parentNode;

	var wasOpen = toggleme.style.display != 'none';

	for (i = 0; i < thenode.childNodes.length; i++){

		var thisChild = thenode.childNodes[i];
		
		for (j = 0; j < thisChild.childNodes.length; j++){
			
			var thisGrandchild = thisChild.childNodes[j];
			
			if (thisGrandchild.nodeName == '#text')
				continue;
			
			if (thisGrandchild.style.display == "block")
				thisGrandchild.style.display = "none";	
		}
	}
	// If the style is empty or none then display menu as a block and vice versa
	if (!wasOpen) {
		toggleme.style.display = "block";
	}
	else {
		toggleme.style.display = "none";
	}
}
//Collapses or expands nav dependant on display	and moves the dropper image up or down
function moveDropper(){
	
	var sidenav = $('sidenav');
	var dropper = $('dropper');
	
	if(sidenav.style.display == '' || sidenav.style.display == 'block'){
		sidenav.style.display = "none";
		dropper.style.top = "96px";
	}
	else {
		sidenav.style.display = "block";
		dropper.style.top = "596px";
	}
}

//Used to validate form fields
function validateFields(container)
{
	return new Validation(container).validate();
} 

document.observe('dom:loaded', function(){	
    Validation.addAllThese([	
	    ['name', 'Please enter your name.', function(v) {
				    return !Validation.get('IsEmpty').test(v);
			    }],
	    ['fname', 'Please enter your first name.', function(v) {
				    return !Validation.get('IsEmpty').test(v);
			    }],
	    ['lname', 'Please enter your last name.', function(v) {
			    return !Validation.get('IsEmpty').test(v);
		    }],
	    ['email', 'Please enter your email.', function(v) {
			    return !Validation.get('IsEmpty').test(v);
		    }],
		['enquiry', 'Please enter an enquiry.', function (v) {
				return !Validation.get('IsEmpty').test(v);
			}],
        ['phone', 'Please enter a valid phone number.', function(v) {
				return Validation.get('IsEmpty').test(v) ||  !/[^\d]/.test(v);
			}],
        ['validate-myemail', 'Please enter a valid email address.', function (v) {
				return Validation.get('IsEmpty').test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v)
			}],
	    ['validate-name', 'Please use characters a-z, spaces or hypens in this field.', function (v) {
				return Validation.get('IsEmpty').test(v) || /^[a-zA-Z \-]+$/.test(v)
			}],
	    ['validate-phone', 'Please enter your phone number.', function(v) {
			    return !Validation.get('IsEmpty').test(v);
		    }]	
	]);});
Event.observe(window, 'load', function() { new Validation('aspnetForm', { immediate: true, onSubmit : false}); });

// Browser Detection Javascript
// copyright 1 February 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function whichBrs() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("opera") != -1) return 'Opera';
	if (agt.indexOf("staroffice") != -1) return 'Star Office';
	if (agt.indexOf("webtv") != -1) return 'WebTV';
	if (agt.indexOf("beonex") != -1) return 'Beonex';
	if (agt.indexOf("chimera") != -1) return 'Chimera';
	if (agt.indexOf("netpositive") != -1) return 'NetPositive';
	if (agt.indexOf("phoenix") != -1) return 'Phoenix';
	if (agt.indexOf("firefox") != -1) return 'Firefox';
	if (agt.indexOf("safari") != -1) return 'Safari';
	if (agt.indexOf("skipstone") != -1) return 'SkipStone';
	if (agt.indexOf("msie") != -1) return 'Internet Explorer';
	if (agt.indexOf("netscape") != -1) return 'Netscape';
	if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
	if (agt.indexOf('\/') != -1) {
	if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
	return navigator.userAgent.substr(0,agt.indexOf('\/'));}
	else return 'Netscape';} else if (agt.indexOf(' ') != -1)
	return navigator.userAgent.substr(0,agt.indexOf(' '));
	else return navigator.userAgent;
}

/* Hacky-hack to make ie accept its min-width*/
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("msie 6.")!=-1) {
    Event.observe(window, 'resize', function(){

        var img = $('alphaBg');

        img.style.width = document.body.clientWidth;

    });
}